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