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