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