src/message_api.c
author Volker Birk <vb@pep-project.org>
Mon, 04 May 2015 14:15:06 +0200
changeset 237 62e817a8ea83
parent 236 154a7077dbb7
child 239 8b9080f5ff08
permissions -rw-r--r--
renaming colors
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@190
    14
vb@235
    15
vb@235
    16
static bool string_equality(const char *s1, const char *s2)
vb@235
    17
{
vb@235
    18
    if (s1 == NULL || s2 == NULL)
vb@235
    19
        return false;
vb@235
    20
vb@235
    21
    assert(s1 && s2);
vb@235
    22
vb@235
    23
    return strcmp(s1, s2) == 0;
vb@235
    24
}
vb@235
    25
vb@235
    26
static bool is_mime_type(const bloblist_t *bl, const char *mt)
vb@235
    27
{
vb@235
    28
    assert(mt);
vb@235
    29
vb@235
    30
    return bl && string_equality(bl->mime_type, mt);
vb@235
    31
}
vb@235
    32
vb@235
    33
static bool is_fileending(const bloblist_t *bl, const char *fe)
vb@235
    34
{
vb@235
    35
    assert(fe);
vb@235
    36
vb@235
    37
    if (bl == NULL || bl->filename == NULL)
vb@235
    38
        return false;
vb@235
    39
vb@235
    40
    assert(bl && bl->filename);
vb@235
    41
vb@235
    42
    size_t fe_len = strlen(fe);
vb@235
    43
    size_t fn_len = strlen(bl->filename);
vb@235
    44
vb@235
    45
    if (fn_len <= fe_len)
vb@235
    46
        return false;
vb@235
    47
vb@235
    48
    assert(fn_len > fe_len);
vb@235
    49
vb@235
    50
    return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
vb@235
    51
}
vb@235
    52
vb@235
    53
void import_attached_keys(PEP_SESSION session, const message *msg)
vb@235
    54
{
vb@236
    55
    assert(session);
vb@235
    56
    assert(msg);
vb@235
    57
vb@235
    58
    bloblist_t *bl;
vb@235
    59
    for (bl = msg->attachments; bl && bl->data; bl = bl->next) {
vb@235
    60
        assert(bl && bl->data && bl->size);
vb@235
    61
vb@235
    62
        if (bl->mime_type == NULL ||
vb@235
    63
                    is_mime_type(bl, "application/octet-stream")) {
vb@235
    64
            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
vb@235
    65
                    is_fileending(bl, ".key") ||
vb@235
    66
                    string_equality(bl->filename, "key.asc"))
vb@235
    67
                import_key(session, bl->data, bl->size);
vb@235
    68
        }
vb@235
    69
        else if (is_mime_type(bl, "application/pgp-keys")) {
vb@235
    70
            import_key(session, bl->data, bl->size);
vb@235
    71
        }
vb@235
    72
        else if (is_mime_type(bl, "text/plain")) {
vb@235
    73
            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
vb@235
    74
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
vb@235
    75
                import_key(session, bl->data, bl->size);
vb@235
    76
        }
vb@235
    77
    }
vb@235
    78
}
vb@235
    79
vb@236
    80
void attach_own_key(PEP_SESSION session, message *msg)
vb@236
    81
{
vb@236
    82
    char *keydata;
vb@236
    83
    size_t size;
vb@236
    84
    bloblist_t *bl;
vb@236
    85
vb@236
    86
    assert(session);
vb@236
    87
    assert(msg);
vb@236
    88
vb@236
    89
    if (msg->dir == PEP_dir_incoming)
vb@236
    90
        return;
vb@236
    91
vb@236
    92
    assert(msg->from && msg->from->fpr);
vb@236
    93
    if (msg->from == NULL || msg->from->fpr == NULL)
vb@236
    94
        return;
vb@236
    95
vb@236
    96
    PEP_STATUS status = export_key(session, msg->from->fpr, &keydata, &size);
vb@236
    97
    assert(status == PEP_STATUS_OK);
vb@236
    98
    if (status != PEP_STATUS_OK)
vb@236
    99
        return;
vb@236
   100
    assert(size);
vb@236
   101
vb@236
   102
    bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
vb@236
   103
            "pEp_key.asc");
vb@236
   104
    if (bl)
vb@236
   105
        msg->attachments = bl;
vb@236
   106
}
vb@236
   107
vb@83
   108
static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
vb@62
   109
{
vb@62
   110
    char * ptext;
vb@81
   111
vb@83
   112
    assert(shortmsg);
vb@83
   113
    assert(strcmp(shortmsg, "pEp") != 0);
vb@62
   114
vb@83
   115
    if (longmsg == NULL)
vb@63
   116
        longmsg = "";
vb@63
   117
vb@83
   118
    ptext = calloc(1, strlen(shortmsg) + strlen(longmsg) + 12);
vb@109
   119
    assert(ptext);
vb@62
   120
    if (ptext == NULL)
vb@62
   121
        return NULL;
vb@62
   122
vb@85
   123
    strcpy(ptext, "Subject: ");
vb@83
   124
    strcat(ptext, shortmsg);
vb@62
   125
    strcat(ptext, "\n\n");
vb@63
   126
    strcat(ptext, longmsg);
vb@62
   127
vb@62
   128
    return ptext;
vb@62
   129
}
vb@44
   130
vb@82
   131
static int seperate_short_and_long(const char *src, char **shortmsg, char **longmsg)
vb@82
   132
{
vb@82
   133
    char *_shortmsg = NULL;
vb@82
   134
    char *_longmsg = NULL;
vb@82
   135
vb@82
   136
    assert(src);
vb@82
   137
    assert(shortmsg);
vb@82
   138
    assert(longmsg);
vb@82
   139
vb@82
   140
    *shortmsg = NULL;
vb@82
   141
    *longmsg = NULL;
vb@82
   142
vb@85
   143
    if (strncasecmp(src, "subject: ", 9) == 0) {
vb@82
   144
        char *line_end = strchr(src, '\n');
vb@82
   145
        
vb@82
   146
        if (line_end == NULL) {
vb@82
   147
            _shortmsg = strdup(src + 9);
vb@82
   148
            if (_shortmsg == NULL)
vb@82
   149
                goto enomem;
vb@82
   150
            // _longmsg = NULL;
vb@82
   151
        }
vb@82
   152
        else {
vb@82
   153
            size_t n = line_end - src;
vb@166
   154
vb@82
   155
            if (*(line_end - 1) == '\r')
vb@166
   156
                _shortmsg = strndup(src + 9, n - 10);
vb@82
   157
            else
vb@166
   158
                _shortmsg = strndup(src + 9, n - 9);
vb@166
   159
vb@82
   160
            if (_shortmsg == NULL)
vb@82
   161
                goto enomem;
vb@166
   162
vb@166
   163
            while (*(src + n) && (*(src + n) == '\n' || *(src + n) == '\r'))
vb@166
   164
                ++n;
vb@166
   165
vb@166
   166
            if (*(src + n)) {
vb@166
   167
                _longmsg = strdup(src + n);
vb@166
   168
                if (_longmsg == NULL)
vb@166
   169
                    goto enomem;
vb@166
   170
            }
vb@82
   171
        }
vb@82
   172
    }
vb@82
   173
    else {
vb@113
   174
        _shortmsg = strdup("");
vb@82
   175
        if (_shortmsg == NULL)
vb@82
   176
            goto enomem;
vb@82
   177
        _longmsg = strdup(src);
vb@82
   178
        if (_longmsg == NULL)
vb@82
   179
            goto enomem;
vb@82
   180
    }
vb@82
   181
    
vb@82
   182
    *shortmsg = _shortmsg;
vb@82
   183
    *longmsg = _longmsg;
vb@82
   184
vb@82
   185
    return 0;
vb@82
   186
vb@82
   187
enomem:
vb@82
   188
    free(_shortmsg);
vb@82
   189
    free(_longmsg);
vb@82
   190
vb@82
   191
    return -1;
vb@82
   192
}
vb@82
   193
vb@113
   194
static PEP_STATUS copy_fields(message *dst, const message *src)
vb@113
   195
{
vb@164
   196
    assert(dst);
vb@164
   197
    assert(src);
vb@164
   198
vb@113
   199
    free_timestamp(dst->sent);
vb@113
   200
    dst->sent = NULL;
vb@113
   201
    if (src->sent) {
vb@113
   202
        dst->sent = timestamp_dup(src->sent);
vb@113
   203
        if (dst->sent == NULL)
vb@113
   204
            return PEP_OUT_OF_MEMORY;
vb@113
   205
    }
vb@113
   206
vb@113
   207
    free_timestamp(dst->recv);
vb@113
   208
    dst->recv = NULL;
vb@113
   209
    if (src->recv) {
vb@113
   210
        dst->recv = timestamp_dup(src->recv);
vb@113
   211
        if (dst->recv == NULL)
vb@113
   212
            return PEP_OUT_OF_MEMORY;
vb@113
   213
    }
vb@113
   214
vb@113
   215
    free_identity(dst->from);
vb@113
   216
    dst->from = NULL;
vb@113
   217
    if (src->from) {
vb@113
   218
        dst->from = identity_dup(src->from);
vb@113
   219
        if (dst->from == NULL)
vb@113
   220
            return PEP_OUT_OF_MEMORY;
vb@113
   221
    }
vb@113
   222
vb@113
   223
    free_identity_list(dst->to);
vb@113
   224
    dst->to = NULL;
vb@113
   225
    if (src->to) {
vb@113
   226
        dst->to = identity_list_dup(src->to);
vb@113
   227
        if (dst->to == NULL)
vb@113
   228
            return PEP_OUT_OF_MEMORY;
vb@113
   229
    }
vb@113
   230
vb@113
   231
    free_identity(dst->recv_by);
vb@113
   232
    dst->recv_by = NULL;
vb@113
   233
    if (src->recv_by) {
vb@113
   234
        dst->recv_by = identity_dup(src->recv_by);
vb@113
   235
        if (dst->recv_by == NULL)
vb@113
   236
            return PEP_OUT_OF_MEMORY;
vb@113
   237
    }
vb@113
   238
vb@113
   239
    free_identity_list(dst->cc);
vb@113
   240
    dst->cc = NULL;
vb@113
   241
    if (src->cc) {
vb@113
   242
        dst->cc = identity_list_dup(src->cc);
vb@113
   243
        if (dst->cc == NULL)
vb@113
   244
            return PEP_OUT_OF_MEMORY;
vb@113
   245
    }
vb@113
   246
vb@113
   247
    free_identity_list(dst->bcc);
vb@113
   248
    dst->bcc = NULL;
vb@113
   249
    if (src->bcc) {
vb@113
   250
        dst->bcc = identity_list_dup(src->bcc);
vb@113
   251
        if (dst->bcc == NULL)
vb@113
   252
            return PEP_OUT_OF_MEMORY;
vb@113
   253
    }
vb@113
   254
vb@113
   255
    free_identity_list(dst->reply_to);
vb@113
   256
    dst->reply_to = NULL;
vb@113
   257
    if (src->reply_to) {
vb@113
   258
        dst->reply_to = identity_list_dup(src->reply_to);
vb@113
   259
        if (dst->reply_to == NULL)
vb@113
   260
            return PEP_OUT_OF_MEMORY;
vb@113
   261
    }
vb@113
   262
vb@113
   263
    free_stringlist(dst->in_reply_to);
vb@113
   264
    dst->in_reply_to = NULL;
vb@113
   265
    if (src->in_reply_to) {
vb@113
   266
        dst->in_reply_to = stringlist_dup(src->in_reply_to);
vb@113
   267
        if (dst->in_reply_to == NULL)
vb@113
   268
            return PEP_OUT_OF_MEMORY;
vb@113
   269
    }
vb@113
   270
vb@113
   271
    free_stringlist(dst->references);
vb@113
   272
    dst->references = NULL;
vb@113
   273
    if (src->references) {
vb@113
   274
        dst->references = stringlist_dup(src->references);
vb@113
   275
        if (dst->references == NULL)
vb@113
   276
            return PEP_OUT_OF_MEMORY;
vb@113
   277
    }
vb@113
   278
vb@113
   279
    free_stringlist(dst->keywords);
vb@113
   280
    dst->keywords = NULL;
vb@113
   281
    if (src->keywords) {
vb@113
   282
        dst->keywords = stringlist_dup(src->keywords);
vb@113
   283
        if (dst->keywords == NULL)
vb@113
   284
            return PEP_OUT_OF_MEMORY;
vb@113
   285
    }
vb@113
   286
vb@113
   287
    free(dst->comments);
vb@113
   288
    dst->comments = NULL;
vb@113
   289
    if (src->comments) {
vb@113
   290
        dst->comments = strdup(src->comments);
vb@113
   291
        assert(dst->comments);
vb@113
   292
        if (dst->comments == NULL)
vb@113
   293
            return PEP_OUT_OF_MEMORY;
vb@113
   294
    }
vb@113
   295
vb@113
   296
    return PEP_STATUS_OK;
vb@113
   297
}
vb@113
   298
vb@81
   299
static message * clone_to_empty_message(const message * src)
vb@80
   300
{
vb@113
   301
    PEP_STATUS status;
vb@80
   302
    message * msg = NULL;
vb@80
   303
vb@81
   304
    assert(src);
vb@81
   305
vb@113
   306
    msg = calloc(1, sizeof(message));
vb@113
   307
    assert(msg);
vb@80
   308
    if (msg == NULL)
vb@80
   309
        goto enomem;
vb@80
   310
vb@82
   311
    msg->dir = src->dir;
vb@82
   312
vb@113
   313
    status = copy_fields(msg, src);
vb@113
   314
    if (status != PEP_STATUS_OK)
vb@113
   315
        goto enomem;
vb@81
   316
vb@80
   317
    return msg;
vb@80
   318
vb@80
   319
enomem:
vb@113
   320
    free_message(msg);
vb@80
   321
    return NULL;
vb@80
   322
}
vb@80
   323
vb@48
   324
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
   325
        PEP_SESSION session,
vb@113
   326
        message *src,
vb@37
   327
        stringlist_t * extra,
vb@38
   328
        message **dst,
vb@81
   329
        PEP_enc_format enc_format
vb@37
   330
    )
vb@37
   331
{
vb@37
   332
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
   333
    message * msg = NULL;
vb@63
   334
    stringlist_t * keys = NULL;
vb@113
   335
    bool free_src = false;
vb@37
   336
vb@37
   337
    assert(session);
vb@37
   338
    assert(src);
vb@37
   339
    assert(dst);
vb@81
   340
    assert(enc_format >= PEP_enc_pieces);
vb@81
   341
vb@191
   342
    if (!(session && src && dst && (enc_format >= PEP_enc_pieces)))
vb@191
   343
        return PEP_ILLEGAL_VALUE;
vb@191
   344
vb@37
   345
    *dst = NULL;
vb@67
   346
vb@235
   347
    import_attached_keys(session, src);
vb@235
   348
vb@81
   349
    if (src->enc_format >= PEP_enc_pieces) {
vb@81
   350
        if (src->enc_format == enc_format) {
vb@113
   351
            assert(0); // the message is encrypted this way already
vb@81
   352
            msg = message_dup(src);
vb@81
   353
            if (msg == NULL)
vb@81
   354
                goto enomem;
vb@81
   355
            *dst = msg;
vb@81
   356
            return PEP_STATUS_OK;
vb@81
   357
        }
vb@81
   358
        else {
vb@113
   359
            // decrypt and re-encrypt again
vb@113
   360
            message * _dst = NULL;
vb@113
   361
            PEP_MIME_format mime = (enc_format == PEP_enc_PEP) ? PEP_MIME :
vb@113
   362
                    PEP_MIME_fields_omitted;
vb@113
   363
vb@113
   364
            status = decrypt_message(session, src, mime, &_dst);
vb@113
   365
            if (status != PEP_STATUS_OK)
vb@113
   366
                goto pep_error;
vb@113
   367
vb@113
   368
            src = _dst;
vb@113
   369
            free_src = true;
vb@81
   370
        }
vb@67
   371
    }
vb@37
   372
vb@236
   373
    status = myself(session, src->from);
vb@236
   374
    if (status != PEP_STATUS_OK)
vb@236
   375
        goto pep_error;
vb@236
   376
vb@81
   377
    msg = clone_to_empty_message(src);
vb@80
   378
    if (msg == NULL)
vb@63
   379
        goto enomem;
vb@40
   380
vb@80
   381
    keys = new_stringlist(src->from->fpr);
vb@63
   382
    if (keys == NULL)
vb@63
   383
        goto enomem;
vb@37
   384
vb@39
   385
    stringlist_t *_k = keys;
vb@39
   386
vb@39
   387
    if (extra) {
vb@39
   388
        _k = stringlist_append(_k, extra);
vb@63
   389
        if (_k == NULL)
vb@63
   390
            goto enomem;
vb@37
   391
    }
vb@39
   392
vb@39
   393
    bool dest_keys_found = false;
vb@37
   394
    identity_list * _il;
vb@80
   395
    for (_il = msg->to; _il && _il->ident; _il = _il->next) {
vb@63
   396
        PEP_STATUS status = update_identity(session, _il->ident);
vb@63
   397
        if (status != PEP_STATUS_OK)
vb@63
   398
            goto pep_error;
vb@63
   399
vb@37
   400
        if (_il->ident->fpr) {
vb@39
   401
            dest_keys_found = true;
vb@39
   402
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
   403
            if (_k == NULL)
vb@63
   404
                goto enomem;
vb@37
   405
        }
vb@37
   406
        else
vb@37
   407
            status = PEP_KEY_NOT_FOUND;
vb@37
   408
    }
vb@37
   409
vb@39
   410
    if (dest_keys_found) {
vb@38
   411
        char *ptext;
vb@37
   412
        char *ctext = NULL;
vb@37
   413
        size_t csize = 0;
vb@37
   414
vb@81
   415
        switch (enc_format) {
vb@112
   416
        case PEP_enc_PGP_MIME: {
vb@62
   417
            bool free_ptext = false;
vb@64
   418
vb@112
   419
            msg->enc_format = PEP_enc_PGP_MIME;
vb@37
   420
vb@113
   421
            if (src->mime == PEP_MIME) {
vb@113
   422
                message *_src = NULL;
vb@113
   423
                assert(src->longmsg);
vb@113
   424
                status = mime_decode_message(src->longmsg, &_src);
vb@113
   425
                if (status != PEP_STATUS_OK)
vb@113
   426
                    goto pep_error;
vb@113
   427
                if (free_src)
vb@113
   428
                    free_message(src);
vb@113
   429
                src = _src;
vb@113
   430
                free_src = true;
vb@62
   431
            }
vb@62
   432
vb@113
   433
            if (src->mime == PEP_MIME_none) {
vb@113
   434
                if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@113
   435
                    ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@113
   436
                    if (ptext == NULL)
vb@113
   437
                        goto enomem;
vb@113
   438
                    free_ptext = true;
vb@113
   439
                }
vb@113
   440
                else if (src->longmsg) {
vb@113
   441
                    ptext = src->longmsg;
vb@113
   442
                }
vb@125
   443
                else {
vb@125
   444
                    ptext = "pEp";
vb@125
   445
                }
vb@113
   446
vb@113
   447
                message *_src = calloc(1, sizeof(message));
vb@89
   448
                assert(_src);
vb@89
   449
                if (_src == NULL)
vb@89
   450
                    goto enomem;
vb@89
   451
                _src->longmsg = ptext;
vb@113
   452
                _src->longmsg_formatted = src->longmsg_formatted;
vb@113
   453
                _src->attachments = src->attachments;
vb@113
   454
                _src->enc_format = PEP_enc_PGP_MIME;
vb@113
   455
                status = mime_encode_message(_src, true, &ptext);
vb@67
   456
                assert(status == PEP_STATUS_OK);
vb@67
   457
                if (free_ptext)
vb@89
   458
                    free(_src->longmsg);
vb@89
   459
                free(_src);
vb@67
   460
                assert(ptext);
vb@67
   461
                if (ptext == NULL)
vb@67
   462
                    goto pep_error;
vb@67
   463
                free_ptext = true;
vb@67
   464
            }
vb@113
   465
            else /* if (src->mime == PEP_MIME_fields_omitted) */ {
vb@67
   466
                ptext = src->longmsg;
vb@67
   467
            }
vb@67
   468
vb@67
   469
            status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@67
   470
                    &ctext, &csize);
vb@62
   471
            if (free_ptext)
vb@62
   472
                free(ptext);
vb@113
   473
            if (ctext == NULL)
vb@63
   474
                goto pep_error;
vb@113
   475
vb@113
   476
            msg->longmsg = strdup(ctext);
vb@113
   477
            if (msg->longmsg == NULL)
vb@113
   478
                goto enomem;
vb@62
   479
        }
vb@63
   480
        break;
vb@62
   481
vb@62
   482
        case PEP_enc_pieces:
vb@64
   483
            msg->enc_format = PEP_enc_pieces;
vb@64
   484
vb@63
   485
            if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@83
   486
                ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@63
   487
                if (ptext == NULL)
vb@63
   488
                    goto enomem;
vb@63
   489
vb@39
   490
                status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@39
   491
                        &ctext, &csize);
vb@40
   492
                free(ptext);
vb@38
   493
                if (ctext) {
vb@40
   494
                    msg->longmsg = strdup(ctext);
vb@64
   495
                    if (msg->longmsg == NULL)
vb@63
   496
                        goto enomem;
vb@38
   497
                }
vb@38
   498
                else {
vb@63
   499
                    goto pep_error;
vb@38
   500
                }
vb@38
   501
            }
vb@38
   502
            else if (src->longmsg) {
vb@38
   503
                ptext = src->longmsg;
vb@39
   504
                status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@39
   505
                        &ctext, &csize);
vb@38
   506
                if (ctext) {
vb@40
   507
                    msg->longmsg = strdup(ctext);
vb@64
   508
                    if (msg->longmsg == NULL)
vb@63
   509
                        goto enomem;
vb@38
   510
                }
vb@38
   511
                else {
vb@63
   512
                    goto pep_error;
vb@38
   513
                }
vb@38
   514
            }
vb@63
   515
vb@63
   516
            if (msg->longmsg_formatted) {
vb@38
   517
                ptext = src->longmsg_formatted;
vb@39
   518
                status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@39
   519
                        &ctext, &csize);
vb@38
   520
                if (ctext) {
vb@40
   521
                    msg->longmsg_formatted = strdup(ctext);
vb@63
   522
                    if (msg->longmsg_formatted == NULL)
vb@63
   523
                        goto enomem;
vb@63
   524
                }
vb@63
   525
                else {
vb@63
   526
                    goto pep_error;
vb@63
   527
                }
vb@63
   528
            }
vb@63
   529
vb@63
   530
            if (src->attachments) {
vb@63
   531
                bloblist_t *_s;
vb@63
   532
                bloblist_t *_d = new_bloblist(NULL, 0, NULL, NULL);
vb@63
   533
                if (_d == NULL)
vb@63
   534
                    goto enomem;
vb@63
   535
vb@63
   536
                msg->attachments = _d;
vb@63
   537
                for (_s = src->attachments; _s && _s->data; _s = _s->next) {
vb@63
   538
                    int psize = _s->size;
vb@63
   539
                    ptext = _s->data;
vb@63
   540
                    status = encrypt_and_sign(session, keys, ptext, psize,
vb@63
   541
                            &ctext, &csize);
vb@63
   542
                    if (ctext) {
vb@63
   543
                        char * _c = strdup(ctext);
vb@63
   544
                        if (_c == NULL)
vb@63
   545
                            goto enomem;
vb@63
   546
vb@63
   547
                        _d = bloblist_add(_d, _c, csize, _s->mime_type,
vb@113
   548
                                _s->filename);
vb@63
   549
                        if (_d == NULL)
vb@63
   550
                            goto enomem;
vb@63
   551
                    }
vb@63
   552
                    else {
vb@63
   553
                        goto pep_error;
vb@40
   554
                    }
vb@38
   555
                }
vb@38
   556
            }
vb@38
   557
            break;
vb@38
   558
vb@81
   559
        case PEP_enc_PEP:
vb@81
   560
            // TODO: implement
vb@81
   561
            NOT_IMPLEMENTED
vb@81
   562
vb@38
   563
        default:
vb@38
   564
            assert(0);
vb@63
   565
            status = PEP_ILLEGAL_VALUE;
vb@63
   566
            goto pep_error;
vb@37
   567
        }
vb@37
   568
    }
vb@37
   569
vb@37
   570
    free_stringlist(keys);
vb@113
   571
    if (free_src)
vb@113
   572
        free_message(src);
vb@63
   573
vb@64
   574
    if (msg->shortmsg == NULL)
vb@64
   575
        msg->shortmsg = strdup("pEp");
vb@64
   576
vb@236
   577
    attach_own_key(session, msg);
vb@235
   578
vb@63
   579
    *dst = msg;
vb@63
   580
    return PEP_STATUS_OK;
vb@63
   581
vb@63
   582
enomem:
vb@63
   583
    status = PEP_OUT_OF_MEMORY;
vb@63
   584
vb@63
   585
pep_error:
vb@63
   586
    free_stringlist(keys);
vb@63
   587
    free_message(msg);
vb@113
   588
    if (free_src)
vb@113
   589
        free_message(src);
vb@63
   590
vb@37
   591
    return status;
vb@37
   592
}
vb@37
   593
vb@113
   594
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@113
   595
{
vb@113
   596
    char *ext;
vb@113
   597
 
vb@113
   598
    assert(blob);
vb@113
   599
vb@113
   600
    if (blob->filename == NULL)
vb@113
   601
        return false;
vb@113
   602
vb@113
   603
    ext = strrchr(blob->filename, '.');
vb@113
   604
    if (ext == NULL)
vb@113
   605
        return false;
vb@113
   606
vb@113
   607
    if (strcmp(blob->mime_type, "application/octet-stream")) {
vb@113
   608
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@113
   609
                strcmp(ext, ".asc") == 0)
vb@113
   610
            return true;
vb@113
   611
    }
vb@164
   612
    else if (strcmp(blob->mime_type, "text/plain")) {
vb@113
   613
        if (strcmp(ext, ".asc") == 0)
vb@113
   614
            return true;
vb@113
   615
    }
vb@113
   616
vb@113
   617
    return false;
vb@113
   618
}
vb@113
   619
vb@113
   620
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@113
   621
{
vb@113
   622
    assert(blob);
vb@113
   623
    assert(blob->filename);
vb@113
   624
vb@113
   625
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@113
   626
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@113
   627
                strcmp(blob->filename + 11, ".asc") == 0)
vb@113
   628
            return true;
vb@113
   629
    }
vb@113
   630
vb@113
   631
    return false;
vb@113
   632
}
vb@113
   633
vb@159
   634
static char * without_double_ending(const char *filename)
vb@113
   635
{
vb@113
   636
    char *ext;
vb@113
   637
vb@113
   638
    assert(filename);
vb@113
   639
vb@113
   640
    ext = strrchr(filename, '.');
vb@113
   641
    if (ext == NULL)
vb@113
   642
        return NULL;
vb@113
   643
vb@113
   644
    return strndup(filename, ext - filename);
vb@113
   645
}
vb@113
   646
vb@48
   647
DYNAMIC_API PEP_STATUS decrypt_message(
vb@37
   648
        PEP_SESSION session,
vb@113
   649
        message *src,
vb@113
   650
        PEP_MIME_format mime,
vb@113
   651
        message **dst
vb@37
   652
    )
vb@37
   653
{
vb@37
   654
    PEP_STATUS status = PEP_STATUS_OK;
vb@73
   655
    message *msg = NULL;
vb@112
   656
    char *ctext;
vb@112
   657
    size_t csize;
vb@112
   658
    char *ptext;
vb@112
   659
    size_t psize;
vb@112
   660
    stringlist_t *keylist;
vb@113
   661
    bool free_src = false;
vb@37
   662
vb@74
   663
    assert(session);
vb@74
   664
    assert(src);
vb@74
   665
    assert(dst);
vb@73
   666
vb@191
   667
    if (!(session && src && dst))
vb@191
   668
        return PEP_ILLEGAL_VALUE;
vb@191
   669
vb@74
   670
    *dst = NULL;
vb@81
   671
 
vb@235
   672
    import_attached_keys(session, src);
vb@235
   673
vb@113
   674
    if (src->mime == PEP_MIME_fields_omitted || src->mime == PEP_MIME) {
vb@113
   675
        message *_src = NULL;
vb@113
   676
        status = mime_decode_message(src->longmsg, &_src);
vb@113
   677
        if (status != PEP_STATUS_OK)
vb@113
   678
            goto pep_error;
vb@81
   679
vb@113
   680
        if ( src->mime == PEP_MIME_fields_omitted) {
vb@113
   681
            status = copy_fields(_src, src);
vb@113
   682
            if (status != PEP_STATUS_OK) {
vb@113
   683
                free_message(_src);
vb@113
   684
                goto pep_error;
vb@113
   685
            }
vb@113
   686
        }
vb@113
   687
vb@113
   688
        src = _src;
vb@113
   689
        free_src = true;
vb@113
   690
    }
vb@113
   691
vb@119
   692
    // src message is not MIME encoded (any more)
vb@113
   693
    assert(src->mime == PEP_MIME_none);
vb@113
   694
vb@117
   695
    if (!is_PGP_message_text(src->longmsg)) {
vb@117
   696
        status = PEP_UNENCRYPTED;
vb@117
   697
        goto pep_error;
vb@117
   698
    }
vb@117
   699
vb@113
   700
    ctext = src->longmsg;
vb@113
   701
    csize = strlen(src->longmsg);
vb@113
   702
vb@113
   703
    status = decrypt_and_verify(session, ctext, csize, &ptext, &psize,
vb@113
   704
            &keylist);
vb@113
   705
    if (ptext == NULL)
vb@113
   706
        goto pep_error;
vb@113
   707
vb@113
   708
    switch (src->enc_format) {
vb@113
   709
        case PEP_enc_PGP_MIME:
vb@113
   710
            status = mime_decode_message(ptext, &msg);
vb@113
   711
            if (status != PEP_STATUS_OK)
vb@113
   712
                goto pep_error;
vb@113
   713
vb@113
   714
            break;
vb@113
   715
vb@113
   716
        case PEP_enc_pieces:
vb@113
   717
            msg = clone_to_empty_message(src);
vb@113
   718
            if (msg == NULL)
vb@113
   719
                goto enomem;
vb@113
   720
vb@113
   721
            msg->longmsg = strdup(ptext);
vb@113
   722
            if (msg->longmsg == NULL)
vb@113
   723
                goto enomem;
vb@113
   724
vb@113
   725
            bloblist_t *_m = msg->attachments;
vb@113
   726
            bloblist_t *_s;
vb@113
   727
            for (_s = src->attachments; _s; _s = _s->next) {
vb@113
   728
                if (is_encrypted_attachment(_s)) {
vb@113
   729
                    ctext = _s->data;
vb@113
   730
                    csize = _s->size;
vb@113
   731
vb@113
   732
                    status = decrypt_and_verify(session, ctext, csize, &ptext,
vb@113
   733
                            &psize, &keylist);
vb@113
   734
                    if (ptext == NULL)
vb@113
   735
                        goto pep_error;
vb@113
   736
                    
vb@113
   737
                    if (is_encrypted_html_attachment(_s)) {
vb@113
   738
                        msg->longmsg_formatted = strdup(ptext);
vb@113
   739
                        if (msg->longmsg_formatted == NULL)
vb@113
   740
                            goto pep_error;
vb@113
   741
                    }
vb@113
   742
                    else {
vb@113
   743
                        char * mime_type = "application/octet-stream";
vb@113
   744
                        char * filename = without_double_ending(_s->filename);
vb@113
   745
                        if (filename == NULL)
vb@113
   746
                            goto enomem;
vb@113
   747
vb@113
   748
                        _m = bloblist_add(_m, ptext, psize, mime_type, filename);
vb@113
   749
                        if (_m == NULL)
vb@113
   750
                            goto enomem;
vb@113
   751
vb@113
   752
                       if (msg->attachments == NULL)
vb@113
   753
                            msg->attachments = _m;
vb@113
   754
                    }
vb@113
   755
                }
vb@82
   756
            }
vb@82
   757
vb@81
   758
            break;
vb@81
   759
vb@81
   760
        default:
vb@113
   761
            // BUG: must implement more
vb@113
   762
            NOT_IMPLEMENTED
vb@81
   763
    }
vb@74
   764
vb@113
   765
    switch (src->enc_format) {
vb@113
   766
        case PEP_enc_PGP_MIME:
vb@113
   767
        case PEP_enc_pieces:
vb@113
   768
            status = copy_fields(msg, src);
vb@113
   769
            if (status != PEP_STATUS_OK)
vb@113
   770
                goto pep_error;
vb@113
   771
vb@166
   772
            if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@113
   773
                free(msg->shortmsg);
vb@113
   774
                msg->shortmsg = strdup(src->shortmsg);
vb@113
   775
                if (msg->shortmsg == NULL)
vb@113
   776
                    goto enomem;
vb@113
   777
            }
vb@113
   778
vb@113
   779
            if (msg->shortmsg == NULL || strcmp(msg->shortmsg, "pEp") == 0)
vb@113
   780
            {
vb@113
   781
                char * shortmsg;
vb@113
   782
                char * longmsg;
vb@113
   783
vb@113
   784
                int r = seperate_short_and_long(msg->longmsg, &shortmsg,
vb@113
   785
                        &longmsg);
vb@113
   786
                if (r == -1)
vb@113
   787
                    goto enomem;
vb@113
   788
vb@113
   789
                free(msg->shortmsg);
vb@113
   790
                free(msg->longmsg);
vb@113
   791
vb@113
   792
                msg->shortmsg = shortmsg;
vb@113
   793
                msg->longmsg = longmsg;
vb@113
   794
            }
vb@113
   795
            else {
vb@113
   796
                msg->shortmsg = strdup(src->shortmsg);
vb@113
   797
                if (msg->shortmsg == NULL)
vb@113
   798
                    goto enomem;
vb@113
   799
                msg->longmsg = ptext;
vb@113
   800
            }
vb@166
   801
            break;
vb@113
   802
vb@113
   803
        default:
vb@113
   804
            // BUG: must implement more
vb@113
   805
            NOT_IMPLEMENTED
vb@113
   806
    }
vb@113
   807
vb@113
   808
    switch (mime) {
vb@113
   809
        case PEP_MIME_none:
vb@113
   810
            break;
vb@113
   811
vb@113
   812
        case PEP_MIME:
vb@113
   813
        case PEP_MIME_fields_omitted:
vb@113
   814
            {
vb@113
   815
                char *text = NULL;
vb@113
   816
                status = mime_encode_message(msg,
vb@113
   817
                        mime == PEP_MIME_fields_omitted, &text);
vb@113
   818
                if (status != PEP_STATUS_OK)
vb@113
   819
                    goto pep_error;
vb@113
   820
vb@113
   821
                message *_msg = clone_to_empty_message(msg);
vb@113
   822
                if (_msg == NULL) {
vb@113
   823
                    free(text);
vb@113
   824
                    goto enomem;
vb@113
   825
                }
vb@113
   826
                _msg->longmsg = text;
vb@113
   827
                _msg->shortmsg = strdup(msg->shortmsg);
vb@113
   828
                if (msg->shortmsg == NULL)
vb@113
   829
                    goto enomem;
vb@113
   830
vb@113
   831
                free_message(msg);
vb@113
   832
                msg = _msg;
vb@113
   833
            }
vb@167
   834
            break;
vb@113
   835
    }
vb@113
   836
vb@113
   837
    if (free_src)
vb@113
   838
        free_message(src);
vb@235
   839
vb@235
   840
    import_attached_keys(session, msg);
vb@235
   841
vb@74
   842
    *dst = msg;
vb@74
   843
    return PEP_STATUS_OK;
vb@73
   844
vb@73
   845
enomem:
vb@73
   846
    status = PEP_OUT_OF_MEMORY;
vb@73
   847
vb@73
   848
pep_error:
vb@73
   849
    free_message(msg);
vb@113
   850
    if (free_src)
vb@113
   851
        free_message(src);
vb@39
   852
vb@37
   853
    return status;
vb@37
   854
}
vb@37
   855
vb@190
   856
static PEP_comm_type _get_comm_type(
vb@190
   857
        PEP_SESSION session,
vb@190
   858
        PEP_comm_type max_comm_type,
vb@190
   859
        pEp_identity *ident
vb@190
   860
    )
vb@190
   861
{
vb@190
   862
    PEP_STATUS status = update_identity(session, ident);
vb@190
   863
vb@190
   864
    if (max_comm_type == PEP_ct_compromized)
vb@190
   865
        return PEP_ct_compromized;
vb@190
   866
vb@190
   867
    if (status == PEP_STATUS_OK) {
vb@190
   868
        if (ident->comm_type == PEP_ct_compromized)
vb@190
   869
            return PEP_ct_compromized;
vb@190
   870
        else
vb@190
   871
            return MIN(max_comm_type, ident->comm_type);
vb@190
   872
    }
vb@190
   873
    else {
vb@190
   874
        return PEP_ct_unknown;
vb@190
   875
    }
vb@190
   876
}
vb@190
   877
vb@193
   878
DYNAMIC_API PEP_STATUS get_message_color(
vb@190
   879
        PEP_SESSION session,
vb@190
   880
        message *msg,
vb@232
   881
        PEP_color *color
vb@190
   882
    )
vb@190
   883
{
vb@190
   884
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
   885
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
   886
    bool comm_type_determined = false;
vb@190
   887
    identity_list * il;
vb@190
   888
vb@190
   889
    assert(session);
vb@190
   890
    assert(msg);
vb@190
   891
    assert(color);
vb@190
   892
vb@191
   893
    if (!(session && msg && color))
vb@191
   894
        return PEP_ILLEGAL_VALUE;
vb@191
   895
vb@237
   896
    *color = PEP_rating_undefined;
vb@190
   897
vb@190
   898
    assert(msg->from);
vb@190
   899
    if (msg->from == NULL)
vb@190
   900
        return PEP_ILLEGAL_VALUE;
vb@190
   901
vb@190
   902
    switch (msg->dir) {
vb@190
   903
        case PEP_dir_incoming:
vb@190
   904
            status = update_identity(session, msg->from);
vb@190
   905
            if (status != PEP_STATUS_OK)
vb@190
   906
                return status;
vb@190
   907
            max_comm_type = msg->from->comm_type;
vb@190
   908
            comm_type_determined = true;
vb@190
   909
            break;
vb@190
   910
        
vb@190
   911
        case PEP_dir_outgoing:
vb@190
   912
            status = myself(session, msg->from);
vb@190
   913
            if (status != PEP_STATUS_OK)
vb@190
   914
                return status;
vb@190
   915
vb@195
   916
            for (il = msg->to; il != NULL; il = il->next) {
vb@195
   917
                if (il->ident) {
vb@195
   918
                    max_comm_type = _get_comm_type(session, max_comm_type,
vb@195
   919
                            il->ident);
vb@195
   920
                    comm_type_determined = true;
vb@190
   921
                }
vb@190
   922
            }
vb@190
   923
vb@195
   924
            for (il = msg->cc; il != NULL; il = il->next) {
vb@195
   925
                if (il->ident) {
vb@195
   926
                    max_comm_type = _get_comm_type(session, max_comm_type,
vb@195
   927
                            il->ident);
vb@195
   928
                    comm_type_determined = true;
vb@190
   929
                }
vb@190
   930
            }
vb@190
   931
vb@195
   932
            for (il = msg->bcc; il != NULL; il = il->next) {
vb@195
   933
                if (il->ident) {
vb@195
   934
                    max_comm_type = _get_comm_type(session, max_comm_type,
vb@195
   935
                            il->ident);
vb@195
   936
                    comm_type_determined = true;
vb@190
   937
                }
vb@190
   938
            }
vb@190
   939
            break;
vb@190
   940
vb@190
   941
        default:
vb@190
   942
            return PEP_ILLEGAL_VALUE;
vb@190
   943
    }
vb@190
   944
vb@190
   945
    if (comm_type_determined == false)
vb@237
   946
        *color = PEP_rating_undefined;
vb@190
   947
vb@190
   948
    else if (max_comm_type == PEP_ct_compromized)
vb@237
   949
        *color = PEP_rating_under_attack;
vb@190
   950
vb@190
   951
    else if (max_comm_type >= PEP_ct_confirmed_enc_anon)
vb@237
   952
        *color = PEP_rating_trusted_and_anonymized;
vb@190
   953
vb@190
   954
    else if (max_comm_type >= PEP_ct_strong_encryption)
vb@237
   955
        *color = PEP_rating_trusted;
vb@190
   956
vb@190
   957
    else if (max_comm_type >= PEP_ct_strong_but_unconfirmed &&
vb@190
   958
            max_comm_type < PEP_ct_confirmed)
vb@237
   959
        *color = PEP_rating_reliable;
vb@190
   960
    
vb@190
   961
    else if (max_comm_type == PEP_ct_no_encryption ||
vb@190
   962
            max_comm_type == PEP_ct_no_encrypted_channel)
vb@237
   963
        *color = PEP_rating_unencrypted;
vb@190
   964
vb@190
   965
    else if (max_comm_type == PEP_ct_unknown)
vb@237
   966
        *color = PEP_rating_undefined;
vb@190
   967
vb@190
   968
    else
vb@237
   969
        *color = PEP_rating_unreliable;
vb@190
   970
vb@190
   971
    return PEP_STATUS_OK;
vb@190
   972
}
vb@190
   973