src/message_api.c
author Volker Birk <vb@pep-project.org>
Wed, 15 Apr 2015 12:31:08 +0200
changeset 190 b5cdb862fb16
parent 167 04f883e99ed1
child 191 3bb51ee8f90d
permissions -rw-r--r--
compute color
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@37
   248
    *dst = NULL;
vb@67
   249
vb@81
   250
    if (src->enc_format >= PEP_enc_pieces) {
vb@81
   251
        if (src->enc_format == enc_format) {
vb@113
   252
            assert(0); // the message is encrypted this way already
vb@81
   253
            msg = message_dup(src);
vb@81
   254
            if (msg == NULL)
vb@81
   255
                goto enomem;
vb@81
   256
            *dst = msg;
vb@81
   257
            return PEP_STATUS_OK;
vb@81
   258
        }
vb@81
   259
        else {
vb@113
   260
            // decrypt and re-encrypt again
vb@113
   261
            message * _dst = NULL;
vb@113
   262
            PEP_MIME_format mime = (enc_format == PEP_enc_PEP) ? PEP_MIME :
vb@113
   263
                    PEP_MIME_fields_omitted;
vb@113
   264
vb@113
   265
            status = decrypt_message(session, src, mime, &_dst);
vb@113
   266
            if (status != PEP_STATUS_OK)
vb@113
   267
                goto pep_error;
vb@113
   268
vb@113
   269
            src = _dst;
vb@113
   270
            free_src = true;
vb@81
   271
        }
vb@67
   272
    }
vb@37
   273
vb@81
   274
    msg = clone_to_empty_message(src);
vb@80
   275
    if (msg == NULL)
vb@63
   276
        goto enomem;
vb@40
   277
vb@80
   278
    status = myself(session, src->from);
vb@63
   279
    if (status != PEP_STATUS_OK)
vb@63
   280
        goto pep_error;
vb@37
   281
vb@80
   282
    keys = new_stringlist(src->from->fpr);
vb@63
   283
    if (keys == NULL)
vb@63
   284
        goto enomem;
vb@37
   285
vb@39
   286
    stringlist_t *_k = keys;
vb@39
   287
vb@39
   288
    if (extra) {
vb@39
   289
        _k = stringlist_append(_k, extra);
vb@63
   290
        if (_k == NULL)
vb@63
   291
            goto enomem;
vb@37
   292
    }
vb@39
   293
vb@39
   294
    bool dest_keys_found = false;
vb@37
   295
    identity_list * _il;
vb@80
   296
    for (_il = msg->to; _il && _il->ident; _il = _il->next) {
vb@63
   297
        PEP_STATUS status = update_identity(session, _il->ident);
vb@63
   298
        if (status != PEP_STATUS_OK)
vb@63
   299
            goto pep_error;
vb@63
   300
vb@37
   301
        if (_il->ident->fpr) {
vb@39
   302
            dest_keys_found = true;
vb@39
   303
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
   304
            if (_k == NULL)
vb@63
   305
                goto enomem;
vb@37
   306
        }
vb@37
   307
        else
vb@37
   308
            status = PEP_KEY_NOT_FOUND;
vb@37
   309
    }
vb@37
   310
vb@39
   311
    if (dest_keys_found) {
vb@38
   312
        char *ptext;
vb@37
   313
        char *ctext = NULL;
vb@37
   314
        size_t csize = 0;
vb@37
   315
vb@81
   316
        switch (enc_format) {
vb@112
   317
        case PEP_enc_PGP_MIME: {
vb@62
   318
            bool free_ptext = false;
vb@64
   319
vb@112
   320
            msg->enc_format = PEP_enc_PGP_MIME;
vb@37
   321
vb@113
   322
            if (src->mime == PEP_MIME) {
vb@113
   323
                message *_src = NULL;
vb@113
   324
                assert(src->longmsg);
vb@113
   325
                status = mime_decode_message(src->longmsg, &_src);
vb@113
   326
                if (status != PEP_STATUS_OK)
vb@113
   327
                    goto pep_error;
vb@113
   328
                if (free_src)
vb@113
   329
                    free_message(src);
vb@113
   330
                src = _src;
vb@113
   331
                free_src = true;
vb@62
   332
            }
vb@62
   333
vb@113
   334
            if (src->mime == PEP_MIME_none) {
vb@113
   335
                if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@113
   336
                    ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@113
   337
                    if (ptext == NULL)
vb@113
   338
                        goto enomem;
vb@113
   339
                    free_ptext = true;
vb@113
   340
                }
vb@113
   341
                else if (src->longmsg) {
vb@113
   342
                    ptext = src->longmsg;
vb@113
   343
                }
vb@125
   344
                else {
vb@125
   345
                    ptext = "pEp";
vb@125
   346
                }
vb@113
   347
vb@113
   348
                message *_src = calloc(1, sizeof(message));
vb@89
   349
                assert(_src);
vb@89
   350
                if (_src == NULL)
vb@89
   351
                    goto enomem;
vb@89
   352
                _src->longmsg = ptext;
vb@113
   353
                _src->longmsg_formatted = src->longmsg_formatted;
vb@113
   354
                _src->attachments = src->attachments;
vb@113
   355
                _src->enc_format = PEP_enc_PGP_MIME;
vb@113
   356
                status = mime_encode_message(_src, true, &ptext);
vb@67
   357
                assert(status == PEP_STATUS_OK);
vb@67
   358
                if (free_ptext)
vb@89
   359
                    free(_src->longmsg);
vb@89
   360
                free(_src);
vb@67
   361
                assert(ptext);
vb@67
   362
                if (ptext == NULL)
vb@67
   363
                    goto pep_error;
vb@67
   364
                free_ptext = true;
vb@67
   365
            }
vb@113
   366
            else /* if (src->mime == PEP_MIME_fields_omitted) */ {
vb@67
   367
                ptext = src->longmsg;
vb@67
   368
            }
vb@67
   369
vb@67
   370
            status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@67
   371
                    &ctext, &csize);
vb@62
   372
            if (free_ptext)
vb@62
   373
                free(ptext);
vb@113
   374
            if (ctext == NULL)
vb@63
   375
                goto pep_error;
vb@113
   376
vb@113
   377
            msg->longmsg = strdup(ctext);
vb@113
   378
            if (msg->longmsg == NULL)
vb@113
   379
                goto enomem;
vb@62
   380
        }
vb@63
   381
        break;
vb@62
   382
vb@62
   383
        case PEP_enc_pieces:
vb@64
   384
            msg->enc_format = PEP_enc_pieces;
vb@64
   385
vb@63
   386
            if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@83
   387
                ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@63
   388
                if (ptext == NULL)
vb@63
   389
                    goto enomem;
vb@63
   390
vb@39
   391
                status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@39
   392
                        &ctext, &csize);
vb@40
   393
                free(ptext);
vb@38
   394
                if (ctext) {
vb@40
   395
                    msg->longmsg = strdup(ctext);
vb@64
   396
                    if (msg->longmsg == NULL)
vb@63
   397
                        goto enomem;
vb@38
   398
                }
vb@38
   399
                else {
vb@63
   400
                    goto pep_error;
vb@38
   401
                }
vb@38
   402
            }
vb@38
   403
            else if (src->longmsg) {
vb@38
   404
                ptext = src->longmsg;
vb@39
   405
                status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@39
   406
                        &ctext, &csize);
vb@38
   407
                if (ctext) {
vb@40
   408
                    msg->longmsg = strdup(ctext);
vb@64
   409
                    if (msg->longmsg == NULL)
vb@63
   410
                        goto enomem;
vb@38
   411
                }
vb@38
   412
                else {
vb@63
   413
                    goto pep_error;
vb@38
   414
                }
vb@38
   415
            }
vb@63
   416
vb@63
   417
            if (msg->longmsg_formatted) {
vb@38
   418
                ptext = src->longmsg_formatted;
vb@39
   419
                status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@39
   420
                        &ctext, &csize);
vb@38
   421
                if (ctext) {
vb@40
   422
                    msg->longmsg_formatted = strdup(ctext);
vb@63
   423
                    if (msg->longmsg_formatted == NULL)
vb@63
   424
                        goto enomem;
vb@63
   425
                }
vb@63
   426
                else {
vb@63
   427
                    goto pep_error;
vb@63
   428
                }
vb@63
   429
            }
vb@63
   430
vb@63
   431
            if (src->attachments) {
vb@63
   432
                bloblist_t *_s;
vb@63
   433
                bloblist_t *_d = new_bloblist(NULL, 0, NULL, NULL);
vb@63
   434
                if (_d == NULL)
vb@63
   435
                    goto enomem;
vb@63
   436
vb@63
   437
                msg->attachments = _d;
vb@63
   438
                for (_s = src->attachments; _s && _s->data; _s = _s->next) {
vb@63
   439
                    int psize = _s->size;
vb@63
   440
                    ptext = _s->data;
vb@63
   441
                    status = encrypt_and_sign(session, keys, ptext, psize,
vb@63
   442
                            &ctext, &csize);
vb@63
   443
                    if (ctext) {
vb@63
   444
                        char * _c = strdup(ctext);
vb@63
   445
                        if (_c == NULL)
vb@63
   446
                            goto enomem;
vb@63
   447
vb@63
   448
                        _d = bloblist_add(_d, _c, csize, _s->mime_type,
vb@113
   449
                                _s->filename);
vb@63
   450
                        if (_d == NULL)
vb@63
   451
                            goto enomem;
vb@63
   452
                    }
vb@63
   453
                    else {
vb@63
   454
                        goto pep_error;
vb@40
   455
                    }
vb@38
   456
                }
vb@38
   457
            }
vb@38
   458
            break;
vb@38
   459
vb@81
   460
        case PEP_enc_PEP:
vb@81
   461
            // TODO: implement
vb@81
   462
            NOT_IMPLEMENTED
vb@81
   463
vb@38
   464
        default:
vb@38
   465
            assert(0);
vb@63
   466
            status = PEP_ILLEGAL_VALUE;
vb@63
   467
            goto pep_error;
vb@37
   468
        }
vb@37
   469
    }
vb@37
   470
vb@37
   471
    free_stringlist(keys);
vb@113
   472
    if (free_src)
vb@113
   473
        free_message(src);
vb@63
   474
vb@64
   475
    if (msg->shortmsg == NULL)
vb@64
   476
        msg->shortmsg = strdup("pEp");
vb@64
   477
vb@63
   478
    *dst = msg;
vb@63
   479
    return PEP_STATUS_OK;
vb@63
   480
vb@63
   481
enomem:
vb@63
   482
    status = PEP_OUT_OF_MEMORY;
vb@63
   483
vb@63
   484
pep_error:
vb@63
   485
    free_stringlist(keys);
vb@63
   486
    free_message(msg);
vb@113
   487
    if (free_src)
vb@113
   488
        free_message(src);
vb@63
   489
vb@37
   490
    return status;
vb@37
   491
}
vb@37
   492
vb@113
   493
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@113
   494
{
vb@113
   495
    char *ext;
vb@113
   496
 
vb@113
   497
    assert(blob);
vb@113
   498
vb@113
   499
    if (blob->filename == NULL)
vb@113
   500
        return false;
vb@113
   501
vb@113
   502
    ext = strrchr(blob->filename, '.');
vb@113
   503
    if (ext == NULL)
vb@113
   504
        return false;
vb@113
   505
vb@113
   506
    if (strcmp(blob->mime_type, "application/octet-stream")) {
vb@113
   507
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@113
   508
                strcmp(ext, ".asc") == 0)
vb@113
   509
            return true;
vb@113
   510
    }
vb@164
   511
    else if (strcmp(blob->mime_type, "text/plain")) {
vb@113
   512
        if (strcmp(ext, ".asc") == 0)
vb@113
   513
            return true;
vb@113
   514
    }
vb@113
   515
vb@113
   516
    return false;
vb@113
   517
}
vb@113
   518
vb@113
   519
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@113
   520
{
vb@113
   521
    assert(blob);
vb@113
   522
    assert(blob->filename);
vb@113
   523
vb@113
   524
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@113
   525
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@113
   526
                strcmp(blob->filename + 11, ".asc") == 0)
vb@113
   527
            return true;
vb@113
   528
    }
vb@113
   529
vb@113
   530
    return false;
vb@113
   531
}
vb@113
   532
vb@159
   533
static char * without_double_ending(const char *filename)
vb@113
   534
{
vb@113
   535
    char *ext;
vb@113
   536
vb@113
   537
    assert(filename);
vb@113
   538
vb@113
   539
    ext = strrchr(filename, '.');
vb@113
   540
    if (ext == NULL)
vb@113
   541
        return NULL;
vb@113
   542
vb@113
   543
    return strndup(filename, ext - filename);
vb@113
   544
}
vb@113
   545
vb@48
   546
DYNAMIC_API PEP_STATUS decrypt_message(
vb@37
   547
        PEP_SESSION session,
vb@113
   548
        message *src,
vb@113
   549
        PEP_MIME_format mime,
vb@113
   550
        message **dst
vb@37
   551
    )
vb@37
   552
{
vb@37
   553
    PEP_STATUS status = PEP_STATUS_OK;
vb@73
   554
    message *msg = NULL;
vb@112
   555
    char *ctext;
vb@112
   556
    size_t csize;
vb@112
   557
    char *ptext;
vb@112
   558
    size_t psize;
vb@112
   559
    stringlist_t *keylist;
vb@113
   560
    bool free_src = false;
vb@37
   561
vb@74
   562
    assert(session);
vb@74
   563
    assert(src);
vb@74
   564
    assert(dst);
vb@73
   565
vb@74
   566
    *dst = NULL;
vb@81
   567
 
vb@113
   568
    if (src->mime == PEP_MIME_fields_omitted || src->mime == PEP_MIME) {
vb@113
   569
        message *_src = NULL;
vb@113
   570
        status = mime_decode_message(src->longmsg, &_src);
vb@113
   571
        if (status != PEP_STATUS_OK)
vb@113
   572
            goto pep_error;
vb@81
   573
vb@113
   574
        if ( src->mime == PEP_MIME_fields_omitted) {
vb@113
   575
            status = copy_fields(_src, src);
vb@113
   576
            if (status != PEP_STATUS_OK) {
vb@113
   577
                free_message(_src);
vb@113
   578
                goto pep_error;
vb@113
   579
            }
vb@113
   580
        }
vb@113
   581
vb@113
   582
        src = _src;
vb@113
   583
        free_src = true;
vb@113
   584
    }
vb@113
   585
vb@119
   586
    // src message is not MIME encoded (any more)
vb@113
   587
    assert(src->mime == PEP_MIME_none);
vb@113
   588
vb@117
   589
    if (!is_PGP_message_text(src->longmsg)) {
vb@117
   590
        status = PEP_UNENCRYPTED;
vb@117
   591
        goto pep_error;
vb@117
   592
    }
vb@117
   593
vb@113
   594
    ctext = src->longmsg;
vb@113
   595
    csize = strlen(src->longmsg);
vb@113
   596
vb@113
   597
    status = decrypt_and_verify(session, ctext, csize, &ptext, &psize,
vb@113
   598
            &keylist);
vb@113
   599
    if (ptext == NULL)
vb@113
   600
        goto pep_error;
vb@113
   601
vb@113
   602
    switch (src->enc_format) {
vb@113
   603
        case PEP_enc_PGP_MIME:
vb@113
   604
            status = mime_decode_message(ptext, &msg);
vb@113
   605
            if (status != PEP_STATUS_OK)
vb@113
   606
                goto pep_error;
vb@113
   607
vb@113
   608
            break;
vb@113
   609
vb@113
   610
        case PEP_enc_pieces:
vb@113
   611
            msg = clone_to_empty_message(src);
vb@113
   612
            if (msg == NULL)
vb@113
   613
                goto enomem;
vb@113
   614
vb@113
   615
            msg->longmsg = strdup(ptext);
vb@113
   616
            if (msg->longmsg == NULL)
vb@113
   617
                goto enomem;
vb@113
   618
vb@113
   619
            bloblist_t *_m = msg->attachments;
vb@113
   620
            bloblist_t *_s;
vb@113
   621
            for (_s = src->attachments; _s; _s = _s->next) {
vb@113
   622
                if (is_encrypted_attachment(_s)) {
vb@113
   623
                    ctext = _s->data;
vb@113
   624
                    csize = _s->size;
vb@113
   625
vb@113
   626
                    status = decrypt_and_verify(session, ctext, csize, &ptext,
vb@113
   627
                            &psize, &keylist);
vb@113
   628
                    if (ptext == NULL)
vb@113
   629
                        goto pep_error;
vb@113
   630
                    
vb@113
   631
                    if (is_encrypted_html_attachment(_s)) {
vb@113
   632
                        msg->longmsg_formatted = strdup(ptext);
vb@113
   633
                        if (msg->longmsg_formatted == NULL)
vb@113
   634
                            goto pep_error;
vb@113
   635
                    }
vb@113
   636
                    else {
vb@113
   637
                        char * mime_type = "application/octet-stream";
vb@113
   638
                        char * filename = without_double_ending(_s->filename);
vb@113
   639
                        if (filename == NULL)
vb@113
   640
                            goto enomem;
vb@113
   641
vb@113
   642
                        _m = bloblist_add(_m, ptext, psize, mime_type, filename);
vb@113
   643
                        if (_m == NULL)
vb@113
   644
                            goto enomem;
vb@113
   645
vb@113
   646
                       if (msg->attachments == NULL)
vb@113
   647
                            msg->attachments = _m;
vb@113
   648
                    }
vb@113
   649
                }
vb@82
   650
            }
vb@82
   651
vb@81
   652
            break;
vb@81
   653
vb@81
   654
        default:
vb@113
   655
            // BUG: must implement more
vb@113
   656
            NOT_IMPLEMENTED
vb@81
   657
    }
vb@74
   658
vb@113
   659
    switch (src->enc_format) {
vb@113
   660
        case PEP_enc_PGP_MIME:
vb@113
   661
        case PEP_enc_pieces:
vb@113
   662
            status = copy_fields(msg, src);
vb@113
   663
            if (status != PEP_STATUS_OK)
vb@113
   664
                goto pep_error;
vb@113
   665
vb@166
   666
            if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@113
   667
                free(msg->shortmsg);
vb@113
   668
                msg->shortmsg = strdup(src->shortmsg);
vb@113
   669
                if (msg->shortmsg == NULL)
vb@113
   670
                    goto enomem;
vb@113
   671
            }
vb@113
   672
vb@113
   673
            if (msg->shortmsg == NULL || strcmp(msg->shortmsg, "pEp") == 0)
vb@113
   674
            {
vb@113
   675
                char * shortmsg;
vb@113
   676
                char * longmsg;
vb@113
   677
vb@113
   678
                int r = seperate_short_and_long(msg->longmsg, &shortmsg,
vb@113
   679
                        &longmsg);
vb@113
   680
                if (r == -1)
vb@113
   681
                    goto enomem;
vb@113
   682
vb@113
   683
                free(msg->shortmsg);
vb@113
   684
                free(msg->longmsg);
vb@113
   685
vb@113
   686
                msg->shortmsg = shortmsg;
vb@113
   687
                msg->longmsg = longmsg;
vb@113
   688
            }
vb@113
   689
            else {
vb@113
   690
                msg->shortmsg = strdup(src->shortmsg);
vb@113
   691
                if (msg->shortmsg == NULL)
vb@113
   692
                    goto enomem;
vb@113
   693
                msg->longmsg = ptext;
vb@113
   694
            }
vb@166
   695
            break;
vb@113
   696
vb@113
   697
        default:
vb@113
   698
            // BUG: must implement more
vb@113
   699
            NOT_IMPLEMENTED
vb@113
   700
    }
vb@113
   701
vb@113
   702
    switch (mime) {
vb@113
   703
        case PEP_MIME_none:
vb@113
   704
            break;
vb@113
   705
vb@113
   706
        case PEP_MIME:
vb@113
   707
        case PEP_MIME_fields_omitted:
vb@113
   708
            {
vb@113
   709
                char *text = NULL;
vb@113
   710
                status = mime_encode_message(msg,
vb@113
   711
                        mime == PEP_MIME_fields_omitted, &text);
vb@113
   712
                if (status != PEP_STATUS_OK)
vb@113
   713
                    goto pep_error;
vb@113
   714
vb@113
   715
                message *_msg = clone_to_empty_message(msg);
vb@113
   716
                if (_msg == NULL) {
vb@113
   717
                    free(text);
vb@113
   718
                    goto enomem;
vb@113
   719
                }
vb@113
   720
                _msg->longmsg = text;
vb@113
   721
                _msg->shortmsg = strdup(msg->shortmsg);
vb@113
   722
                if (msg->shortmsg == NULL)
vb@113
   723
                    goto enomem;
vb@113
   724
vb@113
   725
                free_message(msg);
vb@113
   726
                msg = _msg;
vb@113
   727
            }
vb@167
   728
            break;
vb@113
   729
    }
vb@113
   730
vb@113
   731
    if (free_src)
vb@113
   732
        free_message(src);
vb@74
   733
    *dst = msg;
vb@74
   734
    return PEP_STATUS_OK;
vb@73
   735
vb@73
   736
enomem:
vb@73
   737
    status = PEP_OUT_OF_MEMORY;
vb@73
   738
vb@73
   739
pep_error:
vb@73
   740
    free_message(msg);
vb@113
   741
    if (free_src)
vb@113
   742
        free_message(src);
vb@39
   743
vb@37
   744
    return status;
vb@37
   745
}
vb@37
   746
vb@190
   747
static PEP_comm_type _get_comm_type(
vb@190
   748
        PEP_SESSION session,
vb@190
   749
        PEP_comm_type max_comm_type,
vb@190
   750
        pEp_identity *ident
vb@190
   751
    )
vb@190
   752
{
vb@190
   753
    PEP_STATUS status = update_identity(session, ident);
vb@190
   754
vb@190
   755
    if (max_comm_type == PEP_ct_compromized)
vb@190
   756
        return PEP_ct_compromized;
vb@190
   757
vb@190
   758
    if (status == PEP_STATUS_OK) {
vb@190
   759
        if (ident->comm_type == PEP_ct_compromized)
vb@190
   760
            return PEP_ct_compromized;
vb@190
   761
        else
vb@190
   762
            return MIN(max_comm_type, ident->comm_type);
vb@190
   763
    }
vb@190
   764
    else {
vb@190
   765
        return PEP_ct_unknown;
vb@190
   766
    }
vb@190
   767
}
vb@190
   768
vb@190
   769
DYNAMIC_API PEP_STATUS get_color(
vb@190
   770
        PEP_SESSION session,
vb@190
   771
        message *msg,
vb@190
   772
        pEp_color *color
vb@190
   773
    )
vb@190
   774
{
vb@190
   775
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
   776
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
   777
    bool comm_type_determined = false;
vb@190
   778
    identity_list * il;
vb@190
   779
vb@190
   780
    assert(session);
vb@190
   781
    assert(msg);
vb@190
   782
    assert(color);
vb@190
   783
vb@190
   784
    *color = pEp_undefined;
vb@190
   785
vb@190
   786
    assert(msg->from);
vb@190
   787
    if (msg->from == NULL)
vb@190
   788
        return PEP_ILLEGAL_VALUE;
vb@190
   789
vb@190
   790
    switch (msg->dir) {
vb@190
   791
        case PEP_dir_incoming:
vb@190
   792
            status = update_identity(session, msg->from);
vb@190
   793
            if (status != PEP_STATUS_OK)
vb@190
   794
                return status;
vb@190
   795
            max_comm_type = msg->from->comm_type;
vb@190
   796
            comm_type_determined = true;
vb@190
   797
            break;
vb@190
   798
        
vb@190
   799
        case PEP_dir_outgoing:
vb@190
   800
            status = myself(session, msg->from);
vb@190
   801
            if (status != PEP_STATUS_OK)
vb@190
   802
                return status;
vb@190
   803
vb@190
   804
            if (msg->to) {
vb@190
   805
                for (il = msg->to; il != NULL; il = il->next) {
vb@190
   806
                    if (il->ident) {
vb@190
   807
                        max_comm_type = _get_comm_type(session, max_comm_type, il->ident);
vb@190
   808
                        comm_type_determined = true;
vb@190
   809
                    }
vb@190
   810
                }
vb@190
   811
            }
vb@190
   812
vb@190
   813
            if (msg->cc) {
vb@190
   814
                for (il = msg->cc; il != NULL; il = il->next) {
vb@190
   815
                    if (il->ident) {
vb@190
   816
                        max_comm_type = _get_comm_type(session, max_comm_type, il->ident);
vb@190
   817
                        comm_type_determined = true;
vb@190
   818
                    }
vb@190
   819
                }
vb@190
   820
            }
vb@190
   821
vb@190
   822
            if (msg->bcc) {
vb@190
   823
                for (il = msg->bcc; il != NULL; il = il->next) {
vb@190
   824
                    if (il->ident) {
vb@190
   825
                        max_comm_type = _get_comm_type(session, max_comm_type, il->ident);
vb@190
   826
                        comm_type_determined = true;
vb@190
   827
                    }
vb@190
   828
                }
vb@190
   829
            }
vb@190
   830
            break;
vb@190
   831
vb@190
   832
        default:
vb@190
   833
            return PEP_ILLEGAL_VALUE;
vb@190
   834
    }
vb@190
   835
vb@190
   836
    if (comm_type_determined == false)
vb@190
   837
        *color = pEp_undefined;
vb@190
   838
vb@190
   839
    else if (max_comm_type == PEP_ct_compromized)
vb@190
   840
        *color = pEp_under_attack;
vb@190
   841
vb@190
   842
    else if (max_comm_type >= PEP_ct_confirmed_enc_anon)
vb@190
   843
        *color = pEp_trusted_and_anonymized;
vb@190
   844
vb@190
   845
    else if (max_comm_type >= PEP_ct_strong_encryption)
vb@190
   846
        *color = pEp_trusted;
vb@190
   847
vb@190
   848
    else if (max_comm_type >= PEP_ct_strong_but_unconfirmed &&
vb@190
   849
            max_comm_type < PEP_ct_confirmed)
vb@190
   850
        *color = pEp_reliable;
vb@190
   851
    
vb@190
   852
    else if (max_comm_type == PEP_ct_no_encryption ||
vb@190
   853
            max_comm_type == PEP_ct_no_encrypted_channel)
vb@190
   854
        *color = pEp_unencrypted;
vb@190
   855
vb@190
   856
    else if (max_comm_type == PEP_ct_unknown)
vb@190
   857
        *color = pEp_undefined;
vb@190
   858
vb@190
   859
    else
vb@190
   860
        *color = pEp_unreliable;
vb@190
   861
vb@190
   862
    return PEP_STATUS_OK;
vb@190
   863
}
vb@190
   864