src/message_api.c
author Volker Birk <vb@pep-project.org>
Sun, 31 May 2015 13:22:22 +0200
changeset 301 d8b44dc4b0c8
parent 300 55e8daedd030
child 304 c4976f5c95c2
permissions -rw-r--r--
streamline
vb@125
     1
#include "pEp_internal.h"
vb@37
     2
#include "message_api.h"
vb@37
     3
vb@130
     4
#include "platform.h"
vb@220
     5
#include "mime.h"
vb@85
     6
vb@37
     7
#include <assert.h>
vb@37
     8
#include <string.h>
vb@39
     9
#include <stdlib.h>
vb@39
    10
vb@190
    11
#ifndef MIN
vb@190
    12
#define MIN(A, B) ((B) > (A) ? (A) : (B))
vb@190
    13
#endif
vb@300
    14
#ifndef MAX
vb@300
    15
#define MAX(A, B) ((B) > (A) ? (B) : (A))
vb@300
    16
#endif
vb@190
    17
vb@235
    18
vb@235
    19
static bool string_equality(const char *s1, const char *s2)
vb@235
    20
{
vb@235
    21
    if (s1 == NULL || s2 == NULL)
vb@235
    22
        return false;
vb@235
    23
vb@235
    24
    assert(s1 && s2);
vb@235
    25
vb@235
    26
    return strcmp(s1, s2) == 0;
vb@235
    27
}
vb@235
    28
vb@235
    29
static bool is_mime_type(const bloblist_t *bl, const char *mt)
vb@235
    30
{
vb@235
    31
    assert(mt);
vb@235
    32
vb@235
    33
    return bl && string_equality(bl->mime_type, mt);
vb@235
    34
}
vb@235
    35
vb@235
    36
static bool is_fileending(const bloblist_t *bl, const char *fe)
vb@235
    37
{
vb@235
    38
    assert(fe);
vb@235
    39
vb@235
    40
    if (bl == NULL || bl->filename == NULL)
vb@235
    41
        return false;
vb@235
    42
vb@235
    43
    assert(bl && bl->filename);
vb@235
    44
vb@235
    45
    size_t fe_len = strlen(fe);
vb@235
    46
    size_t fn_len = strlen(bl->filename);
vb@235
    47
vb@235
    48
    if (fn_len <= fe_len)
vb@235
    49
        return false;
vb@235
    50
vb@235
    51
    assert(fn_len > fe_len);
vb@235
    52
vb@235
    53
    return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
vb@235
    54
}
vb@235
    55
vb@235
    56
void import_attached_keys(PEP_SESSION session, const message *msg)
vb@235
    57
{
vb@236
    58
    assert(session);
vb@235
    59
    assert(msg);
vb@235
    60
vb@235
    61
    bloblist_t *bl;
vb@301
    62
    for (bl = msg->attachments; bl && bl->value; bl = bl->next) {
vb@301
    63
        assert(bl && bl->value && bl->size);
vb@235
    64
vb@235
    65
        if (bl->mime_type == NULL ||
vb@235
    66
                    is_mime_type(bl, "application/octet-stream")) {
vb@235
    67
            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
vb@235
    68
                    is_fileending(bl, ".key") ||
vb@235
    69
                    string_equality(bl->filename, "key.asc"))
vb@301
    70
                import_key(session, bl->value, bl->size);
vb@235
    71
        }
vb@235
    72
        else if (is_mime_type(bl, "application/pgp-keys")) {
vb@301
    73
            import_key(session, bl->value, bl->size);
vb@235
    74
        }
vb@235
    75
        else if (is_mime_type(bl, "text/plain")) {
vb@235
    76
            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
vb@235
    77
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
vb@301
    78
                import_key(session, bl->value, bl->size);
vb@235
    79
        }
vb@235
    80
    }
vb@235
    81
}
vb@235
    82
vb@236
    83
void attach_own_key(PEP_SESSION session, message *msg)
vb@236
    84
{
vb@236
    85
    char *keydata;
vb@236
    86
    size_t size;
vb@236
    87
    bloblist_t *bl;
vb@236
    88
vb@236
    89
    assert(session);
vb@236
    90
    assert(msg);
vb@236
    91
vb@236
    92
    if (msg->dir == PEP_dir_incoming)
vb@236
    93
        return;
vb@236
    94
vb@236
    95
    assert(msg->from && msg->from->fpr);
vb@236
    96
    if (msg->from == NULL || msg->from->fpr == NULL)
vb@236
    97
        return;
vb@236
    98
vb@236
    99
    PEP_STATUS status = export_key(session, msg->from->fpr, &keydata, &size);
vb@236
   100
    assert(status == PEP_STATUS_OK);
vb@236
   101
    if (status != PEP_STATUS_OK)
vb@236
   102
        return;
vb@236
   103
    assert(size);
vb@236
   104
vb@236
   105
    bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
vb@236
   106
            "pEp_key.asc");
vb@285
   107
    if (msg->attachments == NULL && bl)
vb@236
   108
        msg->attachments = bl;
vb@236
   109
}
vb@236
   110
vb@284
   111
static void add_opt_field(message *msg, const char *name, const char *value)
vb@284
   112
{
vb@284
   113
    assert(msg);
vb@284
   114
vb@284
   115
    if (msg && name && value) {
vb@284
   116
        stringpair_t *pair = new_stringpair(name, value);
vb@284
   117
        if (pair == NULL)
vb@284
   118
            return;
vb@284
   119
vb@284
   120
        stringpair_list_t *field = stringpair_list_add(msg->opt_fields, pair);
vb@284
   121
        if (field == NULL)
vb@284
   122
            return;
vb@284
   123
vb@284
   124
        if (msg->opt_fields == NULL)
vb@284
   125
            msg->opt_fields = field;
vb@284
   126
    }
vb@284
   127
}
vb@284
   128
vb@258
   129
PEP_cryptotech determine_encryption_format(message *msg)
vb@250
   130
{
vb@250
   131
    assert(msg);
vb@250
   132
vb@258
   133
    if (is_PGP_message_text(msg->longmsg)) {
vb@250
   134
        msg->enc_format = PEP_enc_pieces;
vb@258
   135
        return PEP_crypt_OpenPGP;
vb@258
   136
    }
vb@250
   137
    else if (msg->attachments && msg->attachments->next &&
vb@261
   138
            is_mime_type(msg->attachments, "application/pgp-encrypted") &&
vb@301
   139
            is_PGP_message_text(msg->attachments->next->value)
vb@258
   140
        ) {
vb@250
   141
        msg->enc_format = PEP_enc_PGP_MIME;
vb@258
   142
        return PEP_crypt_OpenPGP;
vb@258
   143
    }
vb@258
   144
    else {
vb@258
   145
        msg->enc_format = PEP_enc_none;
vb@258
   146
        return PEP_crypt_none;
vb@258
   147
    }
vb@250
   148
}
vb@250
   149
vb@83
   150
static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
vb@62
   151
{
vb@62
   152
    char * ptext;
vb@81
   153
vb@83
   154
    assert(shortmsg);
vb@83
   155
    assert(strcmp(shortmsg, "pEp") != 0);
vb@62
   156
vb@83
   157
    if (longmsg == NULL)
vb@63
   158
        longmsg = "";
vb@63
   159
vb@83
   160
    ptext = calloc(1, strlen(shortmsg) + strlen(longmsg) + 12);
vb@109
   161
    assert(ptext);
vb@62
   162
    if (ptext == NULL)
vb@62
   163
        return NULL;
vb@62
   164
vb@85
   165
    strcpy(ptext, "Subject: ");
vb@83
   166
    strcat(ptext, shortmsg);
vb@62
   167
    strcat(ptext, "\n\n");
vb@63
   168
    strcat(ptext, longmsg);
vb@62
   169
vb@62
   170
    return ptext;
vb@62
   171
}
vb@44
   172
vb@82
   173
static int seperate_short_and_long(const char *src, char **shortmsg, char **longmsg)
vb@82
   174
{
vb@82
   175
    char *_shortmsg = NULL;
vb@82
   176
    char *_longmsg = NULL;
vb@82
   177
vb@82
   178
    assert(src);
vb@82
   179
    assert(shortmsg);
vb@82
   180
    assert(longmsg);
vb@82
   181
vb@82
   182
    *shortmsg = NULL;
vb@82
   183
    *longmsg = NULL;
vb@82
   184
vb@85
   185
    if (strncasecmp(src, "subject: ", 9) == 0) {
vb@82
   186
        char *line_end = strchr(src, '\n');
vb@82
   187
        
vb@82
   188
        if (line_end == NULL) {
vb@82
   189
            _shortmsg = strdup(src + 9);
vb@82
   190
            if (_shortmsg == NULL)
vb@82
   191
                goto enomem;
vb@82
   192
            // _longmsg = NULL;
vb@82
   193
        }
vb@82
   194
        else {
vb@82
   195
            size_t n = line_end - src;
vb@166
   196
vb@82
   197
            if (*(line_end - 1) == '\r')
vb@166
   198
                _shortmsg = strndup(src + 9, n - 10);
vb@82
   199
            else
vb@166
   200
                _shortmsg = strndup(src + 9, n - 9);
vb@166
   201
vb@82
   202
            if (_shortmsg == NULL)
vb@82
   203
                goto enomem;
vb@166
   204
vb@166
   205
            while (*(src + n) && (*(src + n) == '\n' || *(src + n) == '\r'))
vb@166
   206
                ++n;
vb@166
   207
vb@166
   208
            if (*(src + n)) {
vb@166
   209
                _longmsg = strdup(src + n);
vb@166
   210
                if (_longmsg == NULL)
vb@166
   211
                    goto enomem;
vb@166
   212
            }
vb@82
   213
        }
vb@82
   214
    }
vb@82
   215
    else {
vb@113
   216
        _shortmsg = strdup("");
vb@82
   217
        if (_shortmsg == NULL)
vb@82
   218
            goto enomem;
vb@82
   219
        _longmsg = strdup(src);
vb@82
   220
        if (_longmsg == NULL)
vb@82
   221
            goto enomem;
vb@82
   222
    }
vb@82
   223
    
vb@82
   224
    *shortmsg = _shortmsg;
vb@82
   225
    *longmsg = _longmsg;
vb@82
   226
vb@82
   227
    return 0;
vb@82
   228
vb@82
   229
enomem:
vb@82
   230
    free(_shortmsg);
vb@82
   231
    free(_longmsg);
vb@82
   232
vb@82
   233
    return -1;
vb@82
   234
}
vb@82
   235
vb@113
   236
static PEP_STATUS copy_fields(message *dst, const message *src)
vb@113
   237
{
vb@164
   238
    assert(dst);
vb@164
   239
    assert(src);
vb@164
   240
vb@113
   241
    free_timestamp(dst->sent);
vb@113
   242
    dst->sent = NULL;
vb@113
   243
    if (src->sent) {
vb@113
   244
        dst->sent = timestamp_dup(src->sent);
vb@113
   245
        if (dst->sent == NULL)
vb@113
   246
            return PEP_OUT_OF_MEMORY;
vb@113
   247
    }
vb@113
   248
vb@113
   249
    free_timestamp(dst->recv);
vb@113
   250
    dst->recv = NULL;
vb@113
   251
    if (src->recv) {
vb@113
   252
        dst->recv = timestamp_dup(src->recv);
vb@113
   253
        if (dst->recv == NULL)
vb@113
   254
            return PEP_OUT_OF_MEMORY;
vb@113
   255
    }
vb@113
   256
vb@113
   257
    free_identity(dst->from);
vb@113
   258
    dst->from = NULL;
vb@113
   259
    if (src->from) {
vb@113
   260
        dst->from = identity_dup(src->from);
vb@113
   261
        if (dst->from == NULL)
vb@113
   262
            return PEP_OUT_OF_MEMORY;
vb@113
   263
    }
vb@113
   264
vb@113
   265
    free_identity_list(dst->to);
vb@113
   266
    dst->to = NULL;
vb@274
   267
    if (src->to && src->to->ident) {
vb@113
   268
        dst->to = identity_list_dup(src->to);
vb@113
   269
        if (dst->to == NULL)
vb@113
   270
            return PEP_OUT_OF_MEMORY;
vb@113
   271
    }
vb@113
   272
vb@113
   273
    free_identity(dst->recv_by);
vb@113
   274
    dst->recv_by = NULL;
vb@113
   275
    if (src->recv_by) {
vb@113
   276
        dst->recv_by = identity_dup(src->recv_by);
vb@113
   277
        if (dst->recv_by == NULL)
vb@113
   278
            return PEP_OUT_OF_MEMORY;
vb@113
   279
    }
vb@113
   280
vb@113
   281
    free_identity_list(dst->cc);
vb@113
   282
    dst->cc = NULL;
vb@274
   283
    if (src->cc && src->cc->ident) {
vb@113
   284
        dst->cc = identity_list_dup(src->cc);
vb@113
   285
        if (dst->cc == NULL)
vb@113
   286
            return PEP_OUT_OF_MEMORY;
vb@113
   287
    }
vb@113
   288
vb@113
   289
    free_identity_list(dst->bcc);
vb@113
   290
    dst->bcc = NULL;
vb@274
   291
    if (src->bcc && src->bcc->ident) {
vb@113
   292
        dst->bcc = identity_list_dup(src->bcc);
vb@113
   293
        if (dst->bcc == NULL)
vb@113
   294
            return PEP_OUT_OF_MEMORY;
vb@113
   295
    }
vb@113
   296
vb@113
   297
    free_identity_list(dst->reply_to);
vb@113
   298
    dst->reply_to = NULL;
vb@274
   299
    if (src->reply_to && src->reply_to->ident) {
vb@113
   300
        dst->reply_to = identity_list_dup(src->reply_to);
vb@113
   301
        if (dst->reply_to == NULL)
vb@113
   302
            return PEP_OUT_OF_MEMORY;
vb@113
   303
    }
vb@113
   304
vb@113
   305
    free_stringlist(dst->in_reply_to);
vb@113
   306
    dst->in_reply_to = NULL;
vb@274
   307
    if (src->in_reply_to && src->in_reply_to->value) {
vb@113
   308
        dst->in_reply_to = stringlist_dup(src->in_reply_to);
vb@113
   309
        if (dst->in_reply_to == NULL)
vb@113
   310
            return PEP_OUT_OF_MEMORY;
vb@113
   311
    }
vb@113
   312
vb@113
   313
    free_stringlist(dst->references);
vb@113
   314
    dst->references = NULL;
vb@113
   315
    if (src->references) {
vb@113
   316
        dst->references = stringlist_dup(src->references);
vb@113
   317
        if (dst->references == NULL)
vb@113
   318
            return PEP_OUT_OF_MEMORY;
vb@113
   319
    }
vb@113
   320
vb@113
   321
    free_stringlist(dst->keywords);
vb@113
   322
    dst->keywords = NULL;
vb@274
   323
    if (src->keywords && src->keywords->value) {
vb@113
   324
        dst->keywords = stringlist_dup(src->keywords);
vb@113
   325
        if (dst->keywords == NULL)
vb@113
   326
            return PEP_OUT_OF_MEMORY;
vb@113
   327
    }
vb@113
   328
vb@113
   329
    free(dst->comments);
vb@113
   330
    dst->comments = NULL;
vb@113
   331
    if (src->comments) {
vb@113
   332
        dst->comments = strdup(src->comments);
vb@113
   333
        assert(dst->comments);
vb@113
   334
        if (dst->comments == NULL)
vb@113
   335
            return PEP_OUT_OF_MEMORY;
vb@113
   336
    }
vb@113
   337
vb@113
   338
    return PEP_STATUS_OK;
vb@113
   339
}
vb@113
   340
vb@81
   341
static message * clone_to_empty_message(const message * src)
vb@80
   342
{
vb@113
   343
    PEP_STATUS status;
vb@80
   344
    message * msg = NULL;
vb@80
   345
vb@81
   346
    assert(src);
vb@81
   347
vb@113
   348
    msg = calloc(1, sizeof(message));
vb@113
   349
    assert(msg);
vb@80
   350
    if (msg == NULL)
vb@80
   351
        goto enomem;
vb@80
   352
vb@82
   353
    msg->dir = src->dir;
vb@82
   354
vb@113
   355
    status = copy_fields(msg, src);
vb@113
   356
    if (status != PEP_STATUS_OK)
vb@113
   357
        goto enomem;
vb@81
   358
vb@80
   359
    return msg;
vb@80
   360
vb@80
   361
enomem:
vb@113
   362
    free_message(msg);
vb@80
   363
    return NULL;
vb@80
   364
}
vb@80
   365
vb@260
   366
static PEP_STATUS encrypt_PGP_MIME(
vb@260
   367
        PEP_SESSION session,
vb@260
   368
        const message *src,
vb@260
   369
        stringlist_t *keys,
vb@260
   370
        message *dst
vb@260
   371
    )
vb@260
   372
{
vb@260
   373
    PEP_STATUS status = PEP_STATUS_OK;
vb@260
   374
    bool free_ptext = false;
vb@260
   375
    char *ptext;
vb@288
   376
    char *ctext;
vb@288
   377
    char *_ctext = NULL;
vb@260
   378
    char *mimetext = NULL;
vb@260
   379
    size_t csize;
vb@260
   380
    assert(dst->longmsg == NULL);
vb@260
   381
    dst->enc_format = PEP_enc_PGP_MIME;
vb@260
   382
vb@260
   383
    if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@260
   384
        ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@260
   385
        if (ptext == NULL)
vb@260
   386
            goto enomem;
vb@260
   387
        free_ptext = true;
vb@260
   388
    }
vb@260
   389
    else if (src->longmsg) {
vb@260
   390
        ptext = src->longmsg;
vb@260
   391
    }
vb@260
   392
    else {
vb@260
   393
        ptext = "pEp";
vb@260
   394
    }
vb@260
   395
vb@260
   396
    message *_src = calloc(1, sizeof(message));
vb@260
   397
    assert(_src);
vb@260
   398
    if (_src == NULL)
vb@260
   399
        goto enomem;
vb@260
   400
    _src->longmsg = ptext;
vb@260
   401
    _src->longmsg_formatted = src->longmsg_formatted;
vb@260
   402
    _src->attachments = src->attachments;
vb@260
   403
    _src->enc_format = PEP_enc_none;
vb@260
   404
    status = mime_encode_message(_src, true, &mimetext);
vb@260
   405
    assert(status == PEP_STATUS_OK);
vb@260
   406
    if (free_ptext)
vb@260
   407
        free(ptext);
vb@260
   408
    free(_src);
vb@260
   409
    assert(mimetext);
vb@260
   410
    if (mimetext == NULL)
vb@260
   411
        goto pep_error;
vb@260
   412
vb@260
   413
    status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
vb@260
   414
            &ctext, &csize);
vb@260
   415
    free(mimetext);
vb@260
   416
    if (ctext == NULL)
vb@260
   417
        goto pep_error;
vb@260
   418
vb@260
   419
    dst->longmsg = strdup("this message was encrypted with p≡p "
vb@260
   420
            "http://pEp-project.org");
vb@260
   421
    if (dst->longmsg == NULL)
vb@260
   422
        goto enomem;
vb@260
   423
vb@260
   424
    char *v = strdup("Version: 1");
vb@260
   425
    if (v == NULL)
vb@260
   426
        goto enomem;
vb@260
   427
vb@260
   428
    bloblist_t *_a = new_bloblist(v, 11, "application/pgp-encrypted", NULL);
vb@260
   429
    if (_a == NULL)
vb@260
   430
        goto enomem;
vb@260
   431
    dst->attachments = _a;
vb@288
   432
vb@288
   433
    _ctext = malloc(csize);
vb@288
   434
    assert(_ctext);
vb@288
   435
    if (_ctext == NULL)
vb@288
   436
        goto enomem;
vb@288
   437
    memcpy(_ctext, ctext, csize);
vb@288
   438
vb@288
   439
    _a = bloblist_add(_a, _ctext, csize, "application/octet-stream",
vb@260
   440
            "msg.asc");
vb@260
   441
    if (_a == NULL)
vb@260
   442
        goto enomem;
vb@260
   443
vb@260
   444
    return PEP_STATUS_OK;
vb@260
   445
vb@260
   446
enomem:
vb@260
   447
    status = PEP_OUT_OF_MEMORY;
vb@260
   448
vb@260
   449
pep_error:
vb@260
   450
    if (free_ptext)
vb@260
   451
        free(ptext);
vb@288
   452
    free(_ctext);
vb@260
   453
    return status;
vb@260
   454
}
vb@260
   455
vb@260
   456
static PEP_STATUS encrypt_PGP_in_pieces(
vb@260
   457
        PEP_SESSION session,
vb@260
   458
        const message *src,
vb@260
   459
        stringlist_t *keys,
vb@260
   460
        message *dst
vb@260
   461
    )
vb@260
   462
{
vb@260
   463
    PEP_STATUS status = PEP_STATUS_OK;
vb@288
   464
    char *ctext;
vb@260
   465
    size_t csize;
vb@260
   466
vb@260
   467
    assert(dst->longmsg == NULL);
vb@260
   468
    assert(dst->attachments == NULL);
vb@260
   469
vb@260
   470
    dst->enc_format = PEP_enc_pieces;
vb@260
   471
vb@260
   472
    if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@260
   473
        char *ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@260
   474
        if (ptext == NULL)
vb@260
   475
            goto enomem;
vb@260
   476
vb@260
   477
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@260
   478
                &csize);
vb@260
   479
        free(ptext);
vb@288
   480
        if (ctext) {
vb@288
   481
            dst->longmsg = strndup(ctext, csize);
vb@288
   482
            assert(dst->longmsg);
vb@288
   483
            if (dst->longmsg == NULL)
vb@288
   484
                goto enomem;
vb@288
   485
        }
vb@288
   486
        else {
vb@260
   487
            goto pep_error;
vb@288
   488
        }
vb@260
   489
    }
vb@260
   490
    else if (src->longmsg) {
vb@260
   491
        char *ptext = src->longmsg;
vb@260
   492
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@260
   493
                &csize);
vb@288
   494
        if (ctext) {
vb@288
   495
            dst->longmsg = strndup(ctext, csize);
vb@288
   496
            assert(dst->longmsg);
vb@288
   497
            if (dst->longmsg == NULL)
vb@288
   498
                goto enomem;
vb@288
   499
        }
vb@288
   500
        else {
vb@260
   501
            goto pep_error;
vb@288
   502
        }
vb@284
   503
    }
vb@284
   504
    else {
vb@284
   505
        dst->longmsg = strdup("");
vb@284
   506
        if (dst->longmsg == NULL)
vb@284
   507
            goto enomem;
vb@260
   508
    }
vb@260
   509
vb@260
   510
    if (src->longmsg_formatted) {
vb@260
   511
        char *ptext = src->longmsg_formatted;
vb@260
   512
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@260
   513
                &csize);
vb@260
   514
        if (ctext) {
vb@288
   515
            char *_ctext = malloc(csize);
vb@288
   516
            assert(_ctext);
vb@288
   517
            if (_ctext == NULL)
vb@288
   518
                goto enomem;
vb@288
   519
            memcpy(_ctext, ctext, csize);
vb@288
   520
vb@288
   521
            bloblist_t *_a = bloblist_add(dst->attachments, _ctext, csize,
vb@284
   522
                    "application/octet-stream", "PGPexch.htm.pgp");
vb@284
   523
            if (_a == NULL)
vb@260
   524
                goto enomem;
vb@284
   525
            if (dst->attachments == NULL)
vb@284
   526
                dst->attachments = _a;
vb@260
   527
        }
vb@260
   528
        else {
vb@260
   529
            goto pep_error;
vb@260
   530
        }
vb@260
   531
    }
vb@260
   532
vb@260
   533
    if (src->attachments) {
vb@284
   534
        if (dst->attachments == NULL) {
vb@284
   535
            dst->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@284
   536
            if (dst->attachments == NULL)
vb@284
   537
                goto enomem;
vb@284
   538
        }
vb@260
   539
vb@284
   540
        bloblist_t *_s = src->attachments;
vb@284
   541
        bloblist_t *_d = dst->attachments;
vb@284
   542
vb@301
   543
        for (int n = 0; _s && _s->value; _s = _s->next) {
vb@260
   544
            int psize = _s->size;
vb@301
   545
            char *ptext = _s->value;
vb@260
   546
            status = encrypt_and_sign(session, keys, ptext, psize, &ctext,
vb@260
   547
                    &csize);
vb@260
   548
            if (ctext) {
vb@285
   549
                char *filename = NULL;
vb@285
   550
vb@285
   551
                if (_s->filename) {
vb@285
   552
                    size_t len = strlen(_s->filename);
vb@285
   553
                    filename = calloc(1, len + 5);
vb@285
   554
                    if (filename == NULL)
vb@285
   555
                        goto enomem;
vb@285
   556
vb@285
   557
                    strcpy(filename, _s->filename);
vb@285
   558
                    strcpy(filename + len, ".pgp");
vb@285
   559
                }
vb@285
   560
                else {
vb@285
   561
                    filename = calloc(1, 20);
vb@285
   562
                    if (filename == NULL)
vb@285
   563
                        goto enomem;
vb@285
   564
vb@285
   565
                    ++n;
vb@285
   566
                    n &= 0xffff;
vb@285
   567
                    snprintf(filename, 20, "Attachment%d.pgp", n);
vb@285
   568
                }
vb@285
   569
vb@288
   570
                char *_ctext = malloc(csize);
vb@288
   571
                assert(_ctext);
vb@288
   572
                if (_ctext == NULL)
vb@288
   573
                    goto enomem;
vb@288
   574
                memcpy(_ctext, ctext, csize);
vb@288
   575
vb@288
   576
                _d = bloblist_add(_d, _ctext, csize, "application/octet-stream",
vb@284
   577
                        filename);
vb@260
   578
                if (_d == NULL)
vb@260
   579
                    goto enomem;
vb@260
   580
            }
vb@260
   581
            else {
vb@260
   582
                goto pep_error;
vb@260
   583
            }
vb@260
   584
        }
vb@260
   585
    }
vb@260
   586
vb@260
   587
    return PEP_STATUS_OK;
vb@260
   588
vb@260
   589
enomem:
vb@260
   590
    status = PEP_OUT_OF_MEMORY;
vb@260
   591
vb@260
   592
pep_error:
vb@260
   593
    return status;
vb@260
   594
}
vb@260
   595
vb@48
   596
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
   597
        PEP_SESSION session,
vb@113
   598
        message *src,
vb@37
   599
        stringlist_t * extra,
vb@38
   600
        message **dst,
vb@81
   601
        PEP_enc_format enc_format
vb@37
   602
    )
vb@37
   603
{
vb@37
   604
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
   605
    message * msg = NULL;
vb@63
   606
    stringlist_t * keys = NULL;
vb@37
   607
vb@37
   608
    assert(session);
vb@37
   609
    assert(src);
vb@37
   610
    assert(dst);
vb@260
   611
    assert(enc_format != PEP_enc_none);
vb@81
   612
vb@260
   613
    if (!(session && src && dst && enc_format != PEP_enc_none))
vb@191
   614
        return PEP_ILLEGAL_VALUE;
vb@191
   615
vb@259
   616
    determine_encryption_format(src);
vb@260
   617
    if (src->enc_format != PEP_enc_none)
vb@260
   618
        return PEP_ILLEGAL_VALUE;
vb@259
   619
vb@37
   620
    *dst = NULL;
vb@67
   621
vb@236
   622
    status = myself(session, src->from);
vb@236
   623
    if (status != PEP_STATUS_OK)
vb@236
   624
        goto pep_error;
vb@236
   625
vb@80
   626
    keys = new_stringlist(src->from->fpr);
vb@63
   627
    if (keys == NULL)
vb@63
   628
        goto enomem;
vb@37
   629
vb@39
   630
    stringlist_t *_k = keys;
vb@39
   631
vb@39
   632
    if (extra) {
vb@39
   633
        _k = stringlist_append(_k, extra);
vb@63
   634
        if (_k == NULL)
vb@63
   635
            goto enomem;
vb@37
   636
    }
vb@39
   637
vb@299
   638
    bool dest_keys_found = true;
vb@299
   639
vb@37
   640
    identity_list * _il;
vb@299
   641
    for (_il = src->to; _il && _il->ident; _il = _il->next) {
vb@299
   642
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
   643
        if (_status != PEP_STATUS_OK) {
vb@299
   644
            status = _status;
vb@63
   645
            goto pep_error;
vb@299
   646
        }
vb@63
   647
vb@299
   648
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@39
   649
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
   650
            if (_k == NULL)
vb@63
   651
                goto enomem;
vb@37
   652
        }
vb@299
   653
        else {
vb@299
   654
            dest_keys_found = false;
vb@37
   655
            status = PEP_KEY_NOT_FOUND;
vb@299
   656
        }
vb@299
   657
    }
vb@299
   658
vb@299
   659
    for (_il = src->cc; _il && _il->ident; _il = _il->next) {
vb@299
   660
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
   661
        if (_status != PEP_STATUS_OK)
vb@299
   662
        {
vb@299
   663
            status = _status;
vb@299
   664
            goto pep_error;
vb@299
   665
        }
vb@299
   666
vb@299
   667
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@299
   668
            _k = stringlist_add(_k, _il->ident->fpr);
vb@299
   669
            if (_k == NULL)
vb@299
   670
                goto enomem;
vb@299
   671
        }
vb@299
   672
        else {
vb@299
   673
            dest_keys_found = false;
vb@299
   674
            status = PEP_KEY_NOT_FOUND;
vb@299
   675
        }
vb@37
   676
    }
vb@37
   677
vb@301
   678
    if (!dest_keys_found) {
vb@301
   679
        free_stringlist(keys);
vb@301
   680
        attach_own_key(session, src);
vb@301
   681
        return PEP_UNENCRYPTED;
vb@301
   682
    }
vb@301
   683
    else {
vb@299
   684
        msg = clone_to_empty_message(src);
vb@299
   685
        if (msg == NULL)
vb@299
   686
            goto enomem;
vb@299
   687
vb@81
   688
        switch (enc_format) {
vb@260
   689
        case PEP_enc_PGP_MIME:
vb@260
   690
            status = encrypt_PGP_MIME(session, src, keys, msg);
vb@260
   691
            if (status != PEP_STATUS_OK)
vb@63
   692
                goto pep_error;
vb@260
   693
            break;
vb@62
   694
vb@62
   695
        case PEP_enc_pieces:
vb@260
   696
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
vb@260
   697
            if (status != PEP_STATUS_OK)
vb@260
   698
                goto pep_error;
vb@260
   699
            attach_own_key(session, msg);
vb@38
   700
            break;
vb@38
   701
vb@81
   702
        case PEP_enc_PEP:
vb@81
   703
            // TODO: implement
vb@81
   704
            NOT_IMPLEMENTED
vb@81
   705
vb@38
   706
        default:
vb@38
   707
            assert(0);
vb@63
   708
            status = PEP_ILLEGAL_VALUE;
vb@63
   709
            goto pep_error;
vb@37
   710
        }
vb@37
   711
    }
vb@37
   712
vb@37
   713
    free_stringlist(keys);
vb@63
   714
vb@299
   715
    if (msg && msg->shortmsg == NULL)
vb@64
   716
        msg->shortmsg = strdup("pEp");
vb@64
   717
vb@63
   718
    *dst = msg;
vb@299
   719
    return status;
vb@63
   720
vb@63
   721
enomem:
vb@63
   722
    status = PEP_OUT_OF_MEMORY;
vb@63
   723
vb@63
   724
pep_error:
vb@63
   725
    free_stringlist(keys);
vb@63
   726
    free_message(msg);
vb@63
   727
vb@37
   728
    return status;
vb@37
   729
}
vb@37
   730
vb@113
   731
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@113
   732
{
vb@113
   733
    char *ext;
vb@113
   734
 
vb@113
   735
    assert(blob);
vb@113
   736
vb@113
   737
    if (blob->filename == NULL)
vb@113
   738
        return false;
vb@113
   739
vb@113
   740
    ext = strrchr(blob->filename, '.');
vb@113
   741
    if (ext == NULL)
vb@113
   742
        return false;
vb@113
   743
vb@113
   744
    if (strcmp(blob->mime_type, "application/octet-stream")) {
vb@113
   745
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@113
   746
                strcmp(ext, ".asc") == 0)
vb@113
   747
            return true;
vb@113
   748
    }
vb@164
   749
    else if (strcmp(blob->mime_type, "text/plain")) {
vb@113
   750
        if (strcmp(ext, ".asc") == 0)
vb@113
   751
            return true;
vb@113
   752
    }
vb@113
   753
vb@113
   754
    return false;
vb@113
   755
}
vb@113
   756
vb@113
   757
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@113
   758
{
vb@113
   759
    assert(blob);
vb@113
   760
    assert(blob->filename);
vb@113
   761
vb@113
   762
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@113
   763
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@113
   764
                strcmp(blob->filename + 11, ".asc") == 0)
vb@113
   765
            return true;
vb@113
   766
    }
vb@113
   767
vb@113
   768
    return false;
vb@113
   769
}
vb@113
   770
vb@159
   771
static char * without_double_ending(const char *filename)
vb@113
   772
{
vb@113
   773
    char *ext;
vb@113
   774
vb@113
   775
    assert(filename);
vb@113
   776
vb@113
   777
    ext = strrchr(filename, '.');
vb@113
   778
    if (ext == NULL)
vb@113
   779
        return NULL;
vb@113
   780
vb@113
   781
    return strndup(filename, ext - filename);
vb@113
   782
}
vb@113
   783
vb@251
   784
static PEP_color decrypt_color(PEP_STATUS status)
vb@251
   785
{
vb@251
   786
    switch (status) {
vb@251
   787
        case PEP_UNENCRYPTED:
vb@251
   788
        case PEP_VERIFIED:
vb@251
   789
        case PEP_VERIFY_NO_KEY:
vb@251
   790
        case PEP_VERIFIED_AND_TRUSTED:
vb@251
   791
            return PEP_rating_unencrypted;
vb@251
   792
vb@251
   793
        case PEP_DECRYPTED:
vb@251
   794
            return PEP_rating_unreliable;
vb@251
   795
vb@251
   796
        case PEP_DECRYPTED_AND_VERIFIED:
vb@251
   797
            return PEP_rating_reliable;
vb@251
   798
vb@267
   799
        case PEP_DECRYPT_NO_KEY:
vb@267
   800
            return PEP_rating_have_no_key;
vb@267
   801
vb@251
   802
        case PEP_DECRYPT_WRONG_FORMAT:
vb@251
   803
        case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@251
   804
            return PEP_rating_cannot_decrypt;
vb@251
   805
vb@251
   806
        default:
vb@251
   807
            return PEP_rating_undefined;
vb@251
   808
    }
vb@251
   809
}
vb@251
   810
vb@251
   811
static PEP_color _rating(PEP_comm_type ct)
vb@251
   812
{
vb@251
   813
    if (ct == PEP_ct_unknown)
vb@251
   814
        return PEP_rating_undefined;
vb@251
   815
vb@251
   816
    else if (ct == PEP_ct_compromized)
vb@251
   817
        return PEP_rating_under_attack;
vb@251
   818
vb@251
   819
    else if (ct >= PEP_ct_confirmed_enc_anon)
vb@251
   820
        return PEP_rating_trusted_and_anonymized;
vb@251
   821
vb@251
   822
    else if (ct >= PEP_ct_strong_encryption)
vb@251
   823
        return PEP_rating_trusted;
vb@251
   824
vb@251
   825
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@251
   826
        return PEP_rating_reliable;
vb@251
   827
    
vb@251
   828
    else if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel)
vb@251
   829
        return PEP_rating_unencrypted;
vb@251
   830
vb@251
   831
    else
vb@251
   832
        return PEP_rating_unreliable;
vb@251
   833
}
vb@251
   834
vb@251
   835
static PEP_color key_color(PEP_SESSION session, const char *fpr) {
vb@251
   836
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@251
   837
vb@251
   838
    assert(session);
vb@251
   839
    assert(fpr);
vb@251
   840
vb@251
   841
    PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
vb@251
   842
    if (status != PEP_STATUS_OK)
vb@251
   843
        return PEP_rating_undefined;
vb@251
   844
vb@251
   845
    return _rating(comm_type);
vb@251
   846
}
vb@251
   847
vb@251
   848
static PEP_color keylist_color(PEP_SESSION session, stringlist_t *keylist)
vb@251
   849
{
vb@251
   850
    PEP_color color = PEP_rating_reliable;
vb@251
   851
vb@251
   852
    assert(keylist && keylist->value);
vb@251
   853
    if (keylist == NULL || keylist->value == NULL)
vb@251
   854
        return PEP_rating_unencrypted;
vb@251
   855
vb@251
   856
    stringlist_t *_kl;
vb@251
   857
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@251
   858
        PEP_comm_type ct;
vb@251
   859
        PEP_STATUS status;
vb@251
   860
vb@260
   861
        color = key_color(session, _kl->value);
vb@260
   862
        if (color == PEP_rating_under_attack)
vb@251
   863
            return PEP_rating_under_attack;
vb@251
   864
vb@260
   865
        if (color >= PEP_rating_reliable) {
vb@260
   866
            status = least_trust(session, _kl->value, &ct);
vb@260
   867
            if (status != PEP_STATUS_OK)
vb@260
   868
                return PEP_rating_undefined;
vb@260
   869
            if (ct == PEP_ct_unknown)
vb@260
   870
                color = PEP_rating_unreliable;
vb@260
   871
            else
vb@260
   872
                color = _rating(ct);
vb@260
   873
        }
vb@251
   874
    }
vb@251
   875
vb@251
   876
    return color;
vb@251
   877
}
vb@251
   878
vb@281
   879
static char * keylist_to_string(const stringlist_t *keylist)
vb@281
   880
{
vb@281
   881
    if (keylist) {
vb@281
   882
        size_t size = stringlist_length(keylist);
vb@281
   883
vb@281
   884
        const stringlist_t *_kl;
vb@281
   885
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@281
   886
            size += strlen(_kl->value);
vb@281
   887
        }
vb@281
   888
vb@281
   889
        char *result = calloc(1, size);
vb@281
   890
        if (result == NULL)
vb@281
   891
            return NULL;
vb@281
   892
vb@281
   893
        char *_r = result;
vb@281
   894
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@281
   895
            _r = stpcpy(_r, _kl->value);
vb@281
   896
            if (_kl->next && _kl->next->value)
vb@281
   897
                _r = stpcpy(_r, ",");
vb@281
   898
        }
vb@281
   899
vb@281
   900
        return result;
vb@281
   901
    }
vb@281
   902
    else {
vb@281
   903
        return NULL;
vb@281
   904
    }
vb@281
   905
}
vb@281
   906
vb@281
   907
static const char * color_to_string(PEP_color color)
vb@281
   908
{
vb@281
   909
    switch (color) {
vb@281
   910
        case PEP_rating_cannot_decrypt:
vb@281
   911
            return "cannot_decrypt";
vb@281
   912
        case PEP_rating_have_no_key:
vb@281
   913
            return "have_no_key";
vb@281
   914
        case PEP_rating_unencrypted:
vb@281
   915
            return "unencrypted";
vb@281
   916
        case PEP_rating_unreliable:
vb@281
   917
            return "unreliable";
vb@281
   918
        case PEP_rating_reliable:
vb@281
   919
            return "reliable";
vb@281
   920
        case PEP_rating_trusted:
vb@281
   921
            return "trusted";
vb@281
   922
        case PEP_rating_trusted_and_anonymized:
vb@281
   923
            return "trusted_and_anonymized";
vb@281
   924
        case PEP_rating_fully_anonymous:
vb@281
   925
            return "fully_anonymous";
vb@281
   926
        case PEP_rating_under_attack:
vb@281
   927
            return "unter_attack";
vb@281
   928
        case PEP_rating_b0rken:
vb@281
   929
            return "b0rken";
vb@281
   930
        default:
vb@281
   931
            return "undefined";
vb@281
   932
    }
vb@281
   933
}
vb@281
   934
vb@281
   935
static void decorate_message(
vb@281
   936
        message *msg,
vb@281
   937
        stringlist_t *keylist,
vb@281
   938
        PEP_color color
vb@281
   939
    )
vb@281
   940
{
vb@281
   941
    assert(msg);
vb@281
   942
vb@281
   943
    add_opt_field(msg, "X-pEp-Version", "1.0");
vb@281
   944
    add_opt_field(msg, "X-EncStatus", color_to_string(color));
vb@281
   945
vb@281
   946
    char *_keylist = keylist_to_string(keylist);
vb@281
   947
    add_opt_field(msg, "X-KeyList", _keylist);
vb@281
   948
    free(_keylist);
vb@281
   949
}
vb@281
   950
vb@48
   951
DYNAMIC_API PEP_STATUS decrypt_message(
vb@37
   952
        PEP_SESSION session,
vb@113
   953
        message *src,
vb@241
   954
        message **dst,
vb@251
   955
        stringlist_t **keylist,
vb@251
   956
        PEP_color *color
vb@37
   957
    )
vb@37
   958
{
vb@37
   959
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
   960
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
   961
    message *msg = NULL;
vb@112
   962
    char *ctext;
vb@112
   963
    size_t csize;
vb@269
   964
    char *ptext = NULL;
vb@112
   965
    size_t psize;
vb@241
   966
    stringlist_t *_keylist = NULL;
vb@37
   967
vb@74
   968
    assert(session);
vb@74
   969
    assert(src);
vb@74
   970
    assert(dst);
vb@241
   971
    assert(keylist);
vb@251
   972
    assert(color);
vb@73
   973
vb@251
   974
    if (!(session && src && dst && keylist && color))
vb@191
   975
        return PEP_ILLEGAL_VALUE;
vb@191
   976
vb@259
   977
    import_attached_keys(session, src);
vb@259
   978
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
   979
vb@74
   980
    *dst = NULL;
vb@251
   981
    *keylist = NULL;
vb@251
   982
    *color = PEP_rating_undefined;
vb@81
   983
 
vb@261
   984
    switch (src->enc_format) {
vb@269
   985
        case PEP_enc_none:
vb@301
   986
            import_attached_keys(session, src);
vb@301
   987
            *color = PEP_rating_unencrypted;
vb@301
   988
            return PEP_UNENCRYPTED;
vb@269
   989
vb@261
   990
        case PEP_enc_PGP_MIME:
vb@301
   991
            ctext = src->attachments->next->value;
vb@269
   992
            csize = src->attachments->next->size;
vb@117
   993
vb@265
   994
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
vb@265
   995
                    csize, &ptext, &psize, &_keylist);
vb@261
   996
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
vb@261
   997
                goto pep_error;
vb@261
   998
            decrypt_status = status;
vb@261
   999
            break;
vb@261
  1000
vb@261
  1001
        case PEP_enc_pieces:
vb@261
  1002
            ctext = src->longmsg;
vb@261
  1003
            csize = strlen(ctext);
vb@261
  1004
vb@265
  1005
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
vb@265
  1006
                    csize, &ptext, &psize, &_keylist);
vb@261
  1007
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
vb@261
  1008
                goto pep_error;
vb@261
  1009
            decrypt_status = status;
vb@261
  1010
            break;
vb@261
  1011
vb@261
  1012
        default:
vb@261
  1013
            NOT_IMPLEMENTED
vb@259
  1014
    }
vb@113
  1015
vb@256
  1016
    *color = decrypt_color(status);
vb@259
  1017
vb@259
  1018
    if (*color != PEP_rating_under_attack) {
vb@259
  1019
        PEP_color kl_color = PEP_rating_undefined;
vb@259
  1020
vb@259
  1021
        if (_keylist)
vb@259
  1022
            kl_color = keylist_color(session, _keylist);
vb@259
  1023
vb@259
  1024
        if (kl_color == PEP_rating_under_attack)
vb@251
  1025
            *color = PEP_rating_under_attack;
vb@259
  1026
vb@260
  1027
        else if (*color >= PEP_rating_reliable &&
vb@259
  1028
                kl_color < PEP_rating_reliable)
vb@259
  1029
            *color = PEP_rating_unreliable;
vb@259
  1030
vb@260
  1031
        else if (*color >= PEP_rating_reliable &&
vb@260
  1032
                kl_color >= PEP_rating_trusted)
vb@260
  1033
            *color = kl_color;
vb@251
  1034
    }
vb@251
  1035
vb@256
  1036
    if (ptext) {
vb@256
  1037
        switch (src->enc_format) {
vb@256
  1038
            case PEP_enc_PGP_MIME:
vb@269
  1039
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  1040
                if (status != PEP_STATUS_OK)
vb@113
  1041
                    goto pep_error;
vb@256
  1042
                break;
vb@256
  1043
vb@256
  1044
            case PEP_enc_pieces:
vb@256
  1045
                msg = clone_to_empty_message(src);
vb@256
  1046
                if (msg == NULL)
vb@113
  1047
                    goto enomem;
vb@113
  1048
vb@256
  1049
                msg->longmsg = strdup(ptext);
vb@256
  1050
                if (msg->longmsg == NULL)
vb@256
  1051
                    goto enomem;
vb@256
  1052
vb@256
  1053
                bloblist_t *_m = msg->attachments;
vb@256
  1054
                bloblist_t *_s;
vb@256
  1055
                for (_s = src->attachments; _s; _s = _s->next) {
vb@256
  1056
                    if (is_encrypted_attachment(_s)) {
vb@256
  1057
                        stringlist_t *_keylist = NULL;
vb@301
  1058
                        ctext = _s->value;
vb@256
  1059
                        csize = _s->size;
vb@256
  1060
vb@256
  1061
                        status = decrypt_and_verify(session, ctext, csize,
vb@256
  1062
                                &ptext, &psize, &_keylist);
vb@256
  1063
                        free_stringlist(_keylist);
vb@256
  1064
vb@289
  1065
                        if (ptext) {
vb@289
  1066
                            if (is_encrypted_html_attachment(_s)) {
vb@290
  1067
                                msg->longmsg_formatted = strdup(ptext);
vb@290
  1068
                                if (msg->longmsg_formatted == NULL)
vb@290
  1069
                                    goto pep_error;
vb@289
  1070
                            }
vb@289
  1071
                            else {
vb@289
  1072
                                char * mime_type = "application/octet-stream";
vb@289
  1073
                                char * filename =
vb@289
  1074
                                    without_double_ending(_s->filename);
vb@289
  1075
                                if (filename == NULL)
vb@289
  1076
                                    goto enomem;
vb@289
  1077
vb@290
  1078
                                char *_ptext = malloc(psize);
vb@290
  1079
                                assert(_ptext);
vb@290
  1080
                                if (_ptext == NULL)
vb@290
  1081
                                    goto enomem;
vb@290
  1082
                                memcpy(_ptext, ptext, psize);
vb@290
  1083
vb@290
  1084
                                _m = bloblist_add(_m, _ptext, psize, mime_type,
vb@289
  1085
                                    filename);
vb@290
  1086
                                if (_m == NULL)
vb@289
  1087
                                    goto enomem;
vb@290
  1088
vb@290
  1089
                                if (msg->attachments == NULL)
vb@290
  1090
                                    msg->attachments = _m;
vb@289
  1091
                            }
vb@256
  1092
                        }
vb@256
  1093
                        else {
vb@289
  1094
                            _m = bloblist_dup(_s);
vb@256
  1095
                            if (_m == NULL)
vb@256
  1096
                                goto enomem;
vb@290
  1097
                            if (msg->attachments == NULL)
vb@256
  1098
                                msg->attachments = _m;
vb@256
  1099
                        }
vb@256
  1100
                    }
vb@256
  1101
                }
vb@256
  1102
vb@256
  1103
                break;
vb@256
  1104
vb@256
  1105
            default:
vb@256
  1106
                // BUG: must implement more
vb@256
  1107
                NOT_IMPLEMENTED
vb@256
  1108
        }
vb@256
  1109
vb@256
  1110
        switch (src->enc_format) {
vb@256
  1111
            case PEP_enc_PGP_MIME:
vb@256
  1112
            case PEP_enc_pieces:
vb@256
  1113
                status = copy_fields(msg, src);
vb@256
  1114
                if (status != PEP_STATUS_OK)
vb@256
  1115
                    goto pep_error;
vb@256
  1116
vb@280
  1117
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1118
                {
vb@256
  1119
                    char * shortmsg;
vb@256
  1120
                    char * longmsg;
vb@256
  1121
vb@256
  1122
                    int r = seperate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1123
                            &longmsg);
vb@256
  1124
                    if (r == -1)
vb@256
  1125
                        goto enomem;
vb@256
  1126
vb@256
  1127
                    free(msg->shortmsg);
vb@256
  1128
                    free(msg->longmsg);
vb@256
  1129
vb@256
  1130
                    msg->shortmsg = shortmsg;
vb@256
  1131
                    msg->longmsg = longmsg;
vb@256
  1132
                }
vb@256
  1133
                else {
vb@256
  1134
                    msg->shortmsg = strdup(src->shortmsg);
vb@256
  1135
                    if (msg->shortmsg == NULL)
vb@256
  1136
                        goto enomem;
vb@256
  1137
                }
vb@256
  1138
                break;
vb@256
  1139
vb@256
  1140
            default:
vb@256
  1141
                // BUG: must implement more
vb@256
  1142
                NOT_IMPLEMENTED
vb@256
  1143
        }
vb@256
  1144
vb@256
  1145
        import_attached_keys(session, msg);
vb@113
  1146
    }
vb@113
  1147
vb@283
  1148
    if (msg)
vb@283
  1149
        decorate_message(msg, _keylist, *color);
vb@235
  1150
vb@74
  1151
    *dst = msg;
vb@241
  1152
    *keylist = _keylist;
vb@241
  1153
vb@267
  1154
    return PEP_STATUS_OK;
vb@73
  1155
vb@73
  1156
enomem:
vb@73
  1157
    status = PEP_OUT_OF_MEMORY;
vb@73
  1158
vb@73
  1159
pep_error:
vb@73
  1160
    free_message(msg);
vb@241
  1161
    free_stringlist(_keylist);
vb@39
  1162
vb@37
  1163
    return status;
vb@37
  1164
}
vb@37
  1165
vb@190
  1166
static PEP_comm_type _get_comm_type(
vb@190
  1167
        PEP_SESSION session,
vb@190
  1168
        PEP_comm_type max_comm_type,
vb@190
  1169
        pEp_identity *ident
vb@190
  1170
    )
vb@190
  1171
{
vb@190
  1172
    PEP_STATUS status = update_identity(session, ident);
vb@190
  1173
vb@190
  1174
    if (max_comm_type == PEP_ct_compromized)
vb@190
  1175
        return PEP_ct_compromized;
vb@190
  1176
vb@190
  1177
    if (status == PEP_STATUS_OK) {
vb@190
  1178
        if (ident->comm_type == PEP_ct_compromized)
vb@190
  1179
            return PEP_ct_compromized;
vb@190
  1180
        else
vb@190
  1181
            return MIN(max_comm_type, ident->comm_type);
vb@190
  1182
    }
vb@190
  1183
    else {
vb@190
  1184
        return PEP_ct_unknown;
vb@190
  1185
    }
vb@190
  1186
}
vb@190
  1187
vb@251
  1188
DYNAMIC_API PEP_STATUS outgoing_message_color(
vb@190
  1189
        PEP_SESSION session,
vb@190
  1190
        message *msg,
vb@232
  1191
        PEP_color *color
vb@190
  1192
    )
vb@190
  1193
{
vb@190
  1194
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
  1195
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  1196
    bool comm_type_determined = false;
vb@190
  1197
    identity_list * il;
vb@190
  1198
vb@190
  1199
    assert(session);
vb@190
  1200
    assert(msg);
vb@251
  1201
    assert(msg->from);
vb@251
  1202
    assert(msg->dir == PEP_dir_outgoing);
vb@190
  1203
    assert(color);
vb@190
  1204
vb@191
  1205
    if (!(session && msg && color))
vb@191
  1206
        return PEP_ILLEGAL_VALUE;
vb@191
  1207
vb@251
  1208
    if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
vb@251
  1209
        return PEP_ILLEGAL_VALUE;
vb@251
  1210
vb@237
  1211
    *color = PEP_rating_undefined;
vb@190
  1212
vb@251
  1213
    status = myself(session, msg->from);
vb@251
  1214
    if (status != PEP_STATUS_OK)
vb@251
  1215
        return status;
vb@251
  1216
vb@251
  1217
    for (il = msg->to; il != NULL; il = il->next) {
vb@251
  1218
        if (il->ident) {
vb@294
  1219
            update_identity(session, il->ident);
vb@251
  1220
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1221
                    il->ident);
vb@190
  1222
            comm_type_determined = true;
vb@251
  1223
        }
vb@251
  1224
    }
vb@190
  1225
vb@251
  1226
    for (il = msg->cc; il != NULL; il = il->next) {
vb@251
  1227
        if (il->ident) {
vb@294
  1228
            update_identity(session, il->ident);
vb@251
  1229
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1230
                    il->ident);
vb@251
  1231
            comm_type_determined = true;
vb@251
  1232
        }
vb@190
  1233
    }
vb@190
  1234
vb@190
  1235
    if (comm_type_determined == false)
vb@237
  1236
        *color = PEP_rating_undefined;
vb@190
  1237
    else
vb@300
  1238
        *color = MAX(_rating(max_comm_type), PEP_rating_unencrypted);
vb@190
  1239
vb@190
  1240
    return PEP_STATUS_OK;
vb@190
  1241
}
vb@190
  1242
vb@240
  1243
DYNAMIC_API PEP_STATUS identity_color(
vb@239
  1244
        PEP_SESSION session,
vb@239
  1245
        pEp_identity *ident,
vb@239
  1246
        PEP_color *color
vb@239
  1247
    )
vb@239
  1248
{
vb@239
  1249
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  1250
vb@239
  1251
    assert(session);
vb@239
  1252
    assert(ident);
vb@239
  1253
    assert(color);
vb@239
  1254
vb@239
  1255
    if (!(session && ident && color))
vb@239
  1256
        return PEP_ILLEGAL_VALUE;
vb@239
  1257
vb@239
  1258
    if (ident->me)
vb@239
  1259
        status = myself(session, ident);
vb@239
  1260
    else
vb@239
  1261
        status = update_identity(session, ident);
vb@239
  1262
vb@239
  1263
    if (status == PEP_STATUS_OK)
vb@239
  1264
        *color = _rating(ident->comm_type);
vb@239
  1265
vb@239
  1266
    return status;
vb@239
  1267
}
vb@239
  1268