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