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