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