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