src/message_api.c
author Volker Birk <vb@pep-project.org>
Sat, 15 Aug 2015 19:18:09 +0200
changeset 336 e0ef820caa40
parent 334 3036eafdd1eb
child 350 4b9f1fff8a1a
permissions -rw-r--r--
makeing PEP_enc_PEP the default
vb@125
     1
#include "pEp_internal.h"
vb@37
     2
#include "message_api.h"
vb@37
     3
vb@130
     4
#include "platform.h"
vb@220
     5
#include "mime.h"
vb@85
     6
vb@37
     7
#include <assert.h>
vb@37
     8
#include <string.h>
vb@39
     9
#include <stdlib.h>
vb@39
    10
vb@190
    11
#ifndef MIN
vb@190
    12
#define MIN(A, B) ((B) > (A) ? (A) : (B))
vb@190
    13
#endif
vb@300
    14
#ifndef MAX
vb@300
    15
#define MAX(A, B) ((B) > (A) ? (B) : (A))
vb@300
    16
#endif
vb@190
    17
vb@235
    18
vb@235
    19
static bool string_equality(const char *s1, const char *s2)
vb@235
    20
{
vb@235
    21
    if (s1 == NULL || s2 == NULL)
vb@235
    22
        return false;
vb@235
    23
vb@235
    24
    assert(s1 && s2);
vb@235
    25
vb@235
    26
    return strcmp(s1, s2) == 0;
vb@235
    27
}
vb@235
    28
vb@235
    29
static bool is_mime_type(const bloblist_t *bl, const char *mt)
vb@235
    30
{
vb@235
    31
    assert(mt);
vb@235
    32
vb@235
    33
    return bl && string_equality(bl->mime_type, mt);
vb@235
    34
}
vb@235
    35
vb@235
    36
static bool is_fileending(const bloblist_t *bl, const char *fe)
vb@235
    37
{
vb@235
    38
    assert(fe);
vb@235
    39
vb@235
    40
    if (bl == NULL || bl->filename == NULL)
vb@235
    41
        return false;
vb@235
    42
vb@235
    43
    assert(bl && bl->filename);
vb@235
    44
vb@235
    45
    size_t fe_len = strlen(fe);
vb@235
    46
    size_t fn_len = strlen(bl->filename);
vb@235
    47
vb@235
    48
    if (fn_len <= fe_len)
vb@235
    49
        return false;
vb@235
    50
vb@235
    51
    assert(fn_len > fe_len);
vb@235
    52
vb@235
    53
    return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
vb@235
    54
}
vb@235
    55
vb@284
    56
static void add_opt_field(message *msg, const char *name, const char *value)
vb@284
    57
{
vb@284
    58
    assert(msg);
vb@284
    59
vb@284
    60
    if (msg && name && value) {
vb@284
    61
        stringpair_t *pair = new_stringpair(name, value);
vb@284
    62
        if (pair == NULL)
vb@284
    63
            return;
vb@284
    64
vb@284
    65
        stringpair_list_t *field = stringpair_list_add(msg->opt_fields, pair);
vb@284
    66
        if (field == NULL)
vb@284
    67
            return;
vb@284
    68
vb@284
    69
        if (msg->opt_fields == NULL)
vb@284
    70
            msg->opt_fields = field;
vb@284
    71
    }
vb@284
    72
}
vb@284
    73
vb@83
    74
static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
vb@62
    75
{
vb@62
    76
    char * ptext;
vb@81
    77
vb@83
    78
    assert(shortmsg);
vb@83
    79
    assert(strcmp(shortmsg, "pEp") != 0);
vb@62
    80
vb@83
    81
    if (longmsg == NULL)
vb@63
    82
        longmsg = "";
vb@63
    83
vb@83
    84
    ptext = calloc(1, strlen(shortmsg) + strlen(longmsg) + 12);
vb@109
    85
    assert(ptext);
vb@62
    86
    if (ptext == NULL)
vb@62
    87
        return NULL;
vb@62
    88
vb@85
    89
    strcpy(ptext, "Subject: ");
vb@83
    90
    strcat(ptext, shortmsg);
vb@62
    91
    strcat(ptext, "\n\n");
vb@63
    92
    strcat(ptext, longmsg);
vb@62
    93
vb@62
    94
    return ptext;
vb@62
    95
}
vb@44
    96
vb@82
    97
static int seperate_short_and_long(const char *src, char **shortmsg, char **longmsg)
vb@82
    98
{
vb@82
    99
    char *_shortmsg = NULL;
vb@82
   100
    char *_longmsg = NULL;
vb@82
   101
vb@82
   102
    assert(src);
vb@82
   103
    assert(shortmsg);
vb@82
   104
    assert(longmsg);
vb@82
   105
vb@82
   106
    *shortmsg = NULL;
vb@82
   107
    *longmsg = NULL;
vb@82
   108
vb@85
   109
    if (strncasecmp(src, "subject: ", 9) == 0) {
vb@82
   110
        char *line_end = strchr(src, '\n');
vb@311
   111
vb@82
   112
        if (line_end == NULL) {
vb@82
   113
            _shortmsg = strdup(src + 9);
vb@82
   114
            if (_shortmsg == NULL)
vb@82
   115
                goto enomem;
vb@82
   116
            // _longmsg = NULL;
vb@82
   117
        }
vb@82
   118
        else {
vb@82
   119
            size_t n = line_end - src;
vb@166
   120
vb@82
   121
            if (*(line_end - 1) == '\r')
vb@166
   122
                _shortmsg = strndup(src + 9, n - 10);
vb@82
   123
            else
vb@166
   124
                _shortmsg = strndup(src + 9, n - 9);
vb@166
   125
vb@82
   126
            if (_shortmsg == NULL)
vb@82
   127
                goto enomem;
vb@166
   128
vb@166
   129
            while (*(src + n) && (*(src + n) == '\n' || *(src + n) == '\r'))
vb@166
   130
                ++n;
vb@166
   131
vb@166
   132
            if (*(src + n)) {
vb@166
   133
                _longmsg = strdup(src + n);
vb@166
   134
                if (_longmsg == NULL)
vb@166
   135
                    goto enomem;
vb@166
   136
            }
vb@82
   137
        }
vb@82
   138
    }
vb@82
   139
    else {
vb@113
   140
        _shortmsg = strdup("");
vb@82
   141
        if (_shortmsg == NULL)
vb@82
   142
            goto enomem;
vb@82
   143
        _longmsg = strdup(src);
vb@82
   144
        if (_longmsg == NULL)
vb@82
   145
            goto enomem;
vb@82
   146
    }
vb@311
   147
vb@82
   148
    *shortmsg = _shortmsg;
vb@82
   149
    *longmsg = _longmsg;
vb@82
   150
vb@82
   151
    return 0;
vb@82
   152
vb@82
   153
enomem:
vb@82
   154
    free(_shortmsg);
vb@82
   155
    free(_longmsg);
vb@82
   156
vb@82
   157
    return -1;
vb@82
   158
}
vb@82
   159
vb@113
   160
static PEP_STATUS copy_fields(message *dst, const message *src)
vb@113
   161
{
vb@164
   162
    assert(dst);
vb@164
   163
    assert(src);
vb@164
   164
vb@113
   165
    free_timestamp(dst->sent);
vb@113
   166
    dst->sent = NULL;
vb@113
   167
    if (src->sent) {
vb@113
   168
        dst->sent = timestamp_dup(src->sent);
vb@113
   169
        if (dst->sent == NULL)
vb@113
   170
            return PEP_OUT_OF_MEMORY;
vb@113
   171
    }
vb@113
   172
vb@113
   173
    free_timestamp(dst->recv);
vb@113
   174
    dst->recv = NULL;
vb@113
   175
    if (src->recv) {
vb@113
   176
        dst->recv = timestamp_dup(src->recv);
vb@113
   177
        if (dst->recv == NULL)
vb@113
   178
            return PEP_OUT_OF_MEMORY;
vb@113
   179
    }
vb@113
   180
vb@113
   181
    free_identity(dst->from);
vb@113
   182
    dst->from = NULL;
vb@113
   183
    if (src->from) {
vb@113
   184
        dst->from = identity_dup(src->from);
vb@113
   185
        if (dst->from == NULL)
vb@113
   186
            return PEP_OUT_OF_MEMORY;
vb@113
   187
    }
vb@113
   188
vb@113
   189
    free_identity_list(dst->to);
vb@113
   190
    dst->to = NULL;
vb@274
   191
    if (src->to && src->to->ident) {
vb@113
   192
        dst->to = identity_list_dup(src->to);
vb@113
   193
        if (dst->to == NULL)
vb@113
   194
            return PEP_OUT_OF_MEMORY;
vb@113
   195
    }
vb@113
   196
vb@113
   197
    free_identity(dst->recv_by);
vb@113
   198
    dst->recv_by = NULL;
vb@113
   199
    if (src->recv_by) {
vb@113
   200
        dst->recv_by = identity_dup(src->recv_by);
vb@113
   201
        if (dst->recv_by == NULL)
vb@113
   202
            return PEP_OUT_OF_MEMORY;
vb@113
   203
    }
vb@113
   204
vb@113
   205
    free_identity_list(dst->cc);
vb@113
   206
    dst->cc = NULL;
vb@274
   207
    if (src->cc && src->cc->ident) {
vb@113
   208
        dst->cc = identity_list_dup(src->cc);
vb@113
   209
        if (dst->cc == NULL)
vb@113
   210
            return PEP_OUT_OF_MEMORY;
vb@113
   211
    }
vb@113
   212
vb@113
   213
    free_identity_list(dst->bcc);
vb@113
   214
    dst->bcc = NULL;
vb@274
   215
    if (src->bcc && src->bcc->ident) {
vb@113
   216
        dst->bcc = identity_list_dup(src->bcc);
vb@113
   217
        if (dst->bcc == NULL)
vb@113
   218
            return PEP_OUT_OF_MEMORY;
vb@113
   219
    }
vb@113
   220
vb@113
   221
    free_identity_list(dst->reply_to);
vb@113
   222
    dst->reply_to = NULL;
vb@274
   223
    if (src->reply_to && src->reply_to->ident) {
vb@113
   224
        dst->reply_to = identity_list_dup(src->reply_to);
vb@113
   225
        if (dst->reply_to == NULL)
vb@113
   226
            return PEP_OUT_OF_MEMORY;
vb@113
   227
    }
vb@113
   228
vb@113
   229
    free_stringlist(dst->in_reply_to);
vb@113
   230
    dst->in_reply_to = NULL;
vb@274
   231
    if (src->in_reply_to && src->in_reply_to->value) {
vb@113
   232
        dst->in_reply_to = stringlist_dup(src->in_reply_to);
vb@113
   233
        if (dst->in_reply_to == NULL)
vb@113
   234
            return PEP_OUT_OF_MEMORY;
vb@113
   235
    }
vb@113
   236
vb@113
   237
    free_stringlist(dst->references);
vb@113
   238
    dst->references = NULL;
vb@113
   239
    if (src->references) {
vb@113
   240
        dst->references = stringlist_dup(src->references);
vb@113
   241
        if (dst->references == NULL)
vb@113
   242
            return PEP_OUT_OF_MEMORY;
vb@113
   243
    }
vb@113
   244
vb@113
   245
    free_stringlist(dst->keywords);
vb@113
   246
    dst->keywords = NULL;
vb@274
   247
    if (src->keywords && src->keywords->value) {
vb@113
   248
        dst->keywords = stringlist_dup(src->keywords);
vb@113
   249
        if (dst->keywords == NULL)
vb@113
   250
            return PEP_OUT_OF_MEMORY;
vb@113
   251
    }
vb@113
   252
vb@113
   253
    free(dst->comments);
vb@113
   254
    dst->comments = NULL;
vb@113
   255
    if (src->comments) {
vb@113
   256
        dst->comments = strdup(src->comments);
vb@113
   257
        assert(dst->comments);
vb@113
   258
        if (dst->comments == NULL)
vb@113
   259
            return PEP_OUT_OF_MEMORY;
vb@113
   260
    }
vb@113
   261
vb@113
   262
    return PEP_STATUS_OK;
vb@113
   263
}
vb@113
   264
vb@81
   265
static message * clone_to_empty_message(const message * src)
vb@80
   266
{
vb@113
   267
    PEP_STATUS status;
vb@80
   268
    message * msg = NULL;
vb@80
   269
vb@81
   270
    assert(src);
vb@81
   271
vb@113
   272
    msg = calloc(1, sizeof(message));
vb@113
   273
    assert(msg);
vb@80
   274
    if (msg == NULL)
vb@80
   275
        goto enomem;
vb@80
   276
vb@82
   277
    msg->dir = src->dir;
vb@82
   278
vb@113
   279
    status = copy_fields(msg, src);
vb@113
   280
    if (status != PEP_STATUS_OK)
vb@113
   281
        goto enomem;
vb@81
   282
vb@80
   283
    return msg;
vb@80
   284
vb@80
   285
enomem:
vb@113
   286
    free_message(msg);
vb@80
   287
    return NULL;
vb@80
   288
}
vb@80
   289
vb@260
   290
static PEP_STATUS encrypt_PGP_MIME(
vb@311
   291
    PEP_SESSION session,
vb@311
   292
    const message *src,
vb@311
   293
    stringlist_t *keys,
vb@311
   294
    message *dst
vb@260
   295
    )
vb@260
   296
{
vb@260
   297
    PEP_STATUS status = PEP_STATUS_OK;
vb@260
   298
    bool free_ptext = false;
vb@260
   299
    char *ptext;
vb@288
   300
    char *ctext;
vb@288
   301
    char *_ctext = NULL;
vb@260
   302
    char *mimetext = NULL;
vb@260
   303
    size_t csize;
vb@260
   304
    assert(dst->longmsg == NULL);
vb@260
   305
    dst->enc_format = PEP_enc_PGP_MIME;
vb@260
   306
vb@260
   307
    if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@260
   308
        ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@260
   309
        if (ptext == NULL)
vb@260
   310
            goto enomem;
vb@260
   311
        free_ptext = true;
vb@260
   312
    }
vb@260
   313
    else if (src->longmsg) {
vb@260
   314
        ptext = src->longmsg;
vb@260
   315
    }
vb@260
   316
    else {
vb@260
   317
        ptext = "pEp";
vb@260
   318
    }
vb@260
   319
vb@260
   320
    message *_src = calloc(1, sizeof(message));
vb@260
   321
    assert(_src);
vb@260
   322
    if (_src == NULL)
vb@260
   323
        goto enomem;
vb@260
   324
    _src->longmsg = ptext;
vb@260
   325
    _src->longmsg_formatted = src->longmsg_formatted;
vb@260
   326
    _src->attachments = src->attachments;
vb@260
   327
    _src->enc_format = PEP_enc_none;
vb@260
   328
    status = mime_encode_message(_src, true, &mimetext);
vb@260
   329
    assert(status == PEP_STATUS_OK);
vb@260
   330
    if (free_ptext)
vb@260
   331
        free(ptext);
vb@260
   332
    free(_src);
vb@260
   333
    assert(mimetext);
vb@260
   334
    if (mimetext == NULL)
vb@260
   335
        goto pep_error;
vb@260
   336
vb@260
   337
    status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
vb@311
   338
        &ctext, &csize);
vb@260
   339
    free(mimetext);
vb@260
   340
    if (ctext == NULL)
vb@260
   341
        goto pep_error;
vb@260
   342
vb@260
   343
    dst->longmsg = strdup("this message was encrypted with p≡p "
vb@311
   344
        "http://pEp-project.org");
vb@260
   345
    if (dst->longmsg == NULL)
vb@260
   346
        goto enomem;
vb@260
   347
vb@260
   348
    char *v = strdup("Version: 1");
vb@260
   349
    if (v == NULL)
vb@260
   350
        goto enomem;
vb@260
   351
vb@260
   352
    bloblist_t *_a = new_bloblist(v, 11, "application/pgp-encrypted", NULL);
vb@260
   353
    if (_a == NULL)
vb@260
   354
        goto enomem;
vb@260
   355
    dst->attachments = _a;
vb@288
   356
vb@288
   357
    _ctext = malloc(csize);
vb@288
   358
    assert(_ctext);
vb@288
   359
    if (_ctext == NULL)
vb@288
   360
        goto enomem;
vb@288
   361
    memcpy(_ctext, ctext, csize);
vb@288
   362
vb@288
   363
    _a = bloblist_add(_a, _ctext, csize, "application/octet-stream",
vb@311
   364
        "msg.asc");
vb@260
   365
    if (_a == NULL)
vb@260
   366
        goto enomem;
vb@260
   367
vb@260
   368
    return PEP_STATUS_OK;
vb@260
   369
vb@260
   370
enomem:
vb@260
   371
    status = PEP_OUT_OF_MEMORY;
vb@260
   372
vb@260
   373
pep_error:
vb@260
   374
    if (free_ptext)
vb@260
   375
        free(ptext);
vb@288
   376
    free(_ctext);
vb@260
   377
    return status;
vb@260
   378
}
vb@260
   379
vb@260
   380
static PEP_STATUS encrypt_PGP_in_pieces(
vb@311
   381
    PEP_SESSION session,
vb@311
   382
    const message *src,
vb@311
   383
    stringlist_t *keys,
vb@311
   384
    message *dst
vb@260
   385
    )
vb@260
   386
{
vb@260
   387
    PEP_STATUS status = PEP_STATUS_OK;
vb@288
   388
    char *ctext;
vb@260
   389
    size_t csize;
vb@260
   390
vb@260
   391
    assert(dst->longmsg == NULL);
vb@260
   392
    assert(dst->attachments == NULL);
vb@260
   393
vb@260
   394
    dst->enc_format = PEP_enc_pieces;
vb@260
   395
vb@320
   396
    if (src->shortmsg && src->shortmsg[0] && strcmp(src->shortmsg, "pEp") != 0) {
vb@260
   397
        char *ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@260
   398
        if (ptext == NULL)
vb@260
   399
            goto enomem;
vb@260
   400
vb@260
   401
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   402
            &csize);
vb@260
   403
        free(ptext);
vb@288
   404
        if (ctext) {
vb@288
   405
            dst->longmsg = strndup(ctext, csize);
vb@288
   406
            assert(dst->longmsg);
vb@288
   407
            if (dst->longmsg == NULL)
vb@288
   408
                goto enomem;
vb@288
   409
        }
vb@288
   410
        else {
vb@260
   411
            goto pep_error;
vb@288
   412
        }
vb@260
   413
    }
vb@320
   414
    else if (src->longmsg && src->longmsg[0]) {
vb@260
   415
        char *ptext = src->longmsg;
vb@260
   416
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   417
            &csize);
vb@288
   418
        if (ctext) {
vb@288
   419
            dst->longmsg = strndup(ctext, csize);
vb@288
   420
            assert(dst->longmsg);
vb@288
   421
            if (dst->longmsg == NULL)
vb@288
   422
                goto enomem;
vb@288
   423
        }
vb@288
   424
        else {
vb@260
   425
            goto pep_error;
vb@288
   426
        }
vb@284
   427
    }
vb@284
   428
    else {
vb@284
   429
        dst->longmsg = strdup("");
vb@284
   430
        if (dst->longmsg == NULL)
vb@284
   431
            goto enomem;
vb@260
   432
    }
vb@260
   433
vb@320
   434
    if (src->longmsg_formatted && src->longmsg_formatted[0]) {
vb@260
   435
        char *ptext = src->longmsg_formatted;
vb@260
   436
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   437
            &csize);
vb@260
   438
        if (ctext) {
vb@288
   439
            char *_ctext = malloc(csize);
vb@288
   440
            assert(_ctext);
vb@288
   441
            if (_ctext == NULL)
vb@288
   442
                goto enomem;
vb@288
   443
            memcpy(_ctext, ctext, csize);
vb@288
   444
vb@288
   445
            bloblist_t *_a = bloblist_add(dst->attachments, _ctext, csize,
vb@311
   446
                "application/octet-stream", "PGPexch.htm.pgp");
vb@284
   447
            if (_a == NULL)
vb@260
   448
                goto enomem;
vb@284
   449
            if (dst->attachments == NULL)
vb@284
   450
                dst->attachments = _a;
vb@260
   451
        }
vb@260
   452
        else {
vb@260
   453
            goto pep_error;
vb@260
   454
        }
vb@260
   455
    }
vb@260
   456
vb@260
   457
    if (src->attachments) {
vb@284
   458
        if (dst->attachments == NULL) {
vb@284
   459
            dst->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@284
   460
            if (dst->attachments == NULL)
vb@284
   461
                goto enomem;
vb@284
   462
        }
vb@260
   463
vb@284
   464
        bloblist_t *_s = src->attachments;
vb@284
   465
        bloblist_t *_d = dst->attachments;
vb@284
   466
vb@301
   467
        for (int n = 0; _s && _s->value; _s = _s->next) {
vb@334
   468
            size_t psize = _s->size;
vb@301
   469
            char *ptext = _s->value;
vb@260
   470
            status = encrypt_and_sign(session, keys, ptext, psize, &ctext,
vb@311
   471
                &csize);
vb@260
   472
            if (ctext) {
vb@285
   473
                char *filename = NULL;
vb@285
   474
vb@285
   475
                if (_s->filename) {
vb@285
   476
                    size_t len = strlen(_s->filename);
vb@285
   477
                    filename = calloc(1, len + 5);
vb@285
   478
                    if (filename == NULL)
vb@285
   479
                        goto enomem;
vb@285
   480
vb@285
   481
                    strcpy(filename, _s->filename);
vb@285
   482
                    strcpy(filename + len, ".pgp");
vb@285
   483
                }
vb@285
   484
                else {
vb@285
   485
                    filename = calloc(1, 20);
vb@285
   486
                    if (filename == NULL)
vb@285
   487
                        goto enomem;
vb@285
   488
vb@285
   489
                    ++n;
vb@285
   490
                    n &= 0xffff;
vb@285
   491
                    snprintf(filename, 20, "Attachment%d.pgp", n);
vb@285
   492
                }
vb@285
   493
vb@288
   494
                char *_ctext = malloc(csize);
vb@288
   495
                assert(_ctext);
vb@288
   496
                if (_ctext == NULL)
vb@288
   497
                    goto enomem;
vb@288
   498
                memcpy(_ctext, ctext, csize);
vb@288
   499
vb@288
   500
                _d = bloblist_add(_d, _ctext, csize, "application/octet-stream",
vb@311
   501
                    filename);
vb@260
   502
                if (_d == NULL)
vb@260
   503
                    goto enomem;
vb@260
   504
            }
vb@260
   505
            else {
vb@260
   506
                goto pep_error;
vb@260
   507
            }
vb@260
   508
        }
vb@260
   509
    }
vb@260
   510
vb@260
   511
    return PEP_STATUS_OK;
vb@260
   512
vb@260
   513
enomem:
vb@260
   514
    status = PEP_OUT_OF_MEMORY;
vb@260
   515
vb@260
   516
pep_error:
vb@260
   517
    return status;
vb@260
   518
}
vb@260
   519
vb@311
   520
static char * keylist_to_string(const stringlist_t *keylist)
vb@311
   521
{
vb@311
   522
    if (keylist) {
vb@311
   523
        size_t size = stringlist_length(keylist);
vb@311
   524
vb@311
   525
        const stringlist_t *_kl;
vb@311
   526
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   527
            size += strlen(_kl->value);
vb@311
   528
        }
vb@311
   529
vb@311
   530
        char *result = calloc(1, size);
vb@311
   531
        if (result == NULL)
vb@311
   532
            return NULL;
vb@311
   533
vb@311
   534
        char *_r = result;
vb@311
   535
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   536
            _r = stpcpy(_r, _kl->value);
vb@311
   537
            if (_kl->next && _kl->next->value)
vb@311
   538
                _r = stpcpy(_r, ",");
vb@311
   539
        }
vb@311
   540
vb@311
   541
        return result;
vb@311
   542
    }
vb@311
   543
    else {
vb@311
   544
        return NULL;
vb@311
   545
    }
vb@311
   546
}
vb@311
   547
vb@311
   548
static const char * color_to_string(PEP_color color)
vb@311
   549
{
vb@311
   550
    switch (color) {
vb@311
   551
    case PEP_rating_cannot_decrypt:
vb@311
   552
        return "cannot_decrypt";
vb@311
   553
    case PEP_rating_have_no_key:
vb@311
   554
        return "have_no_key";
vb@311
   555
    case PEP_rating_unencrypted:
vb@311
   556
        return "unencrypted";
vb@311
   557
    case PEP_rating_unreliable:
vb@311
   558
        return "unreliable";
vb@311
   559
    case PEP_rating_reliable:
vb@311
   560
        return "reliable";
vb@311
   561
    case PEP_rating_trusted:
vb@311
   562
        return "trusted";
vb@311
   563
    case PEP_rating_trusted_and_anonymized:
vb@311
   564
        return "trusted_and_anonymized";
vb@311
   565
    case PEP_rating_fully_anonymous:
vb@311
   566
        return "fully_anonymous";
vb@311
   567
    case PEP_rating_under_attack:
vb@311
   568
        return "unter_attack";
vb@311
   569
    case PEP_rating_b0rken:
vb@311
   570
        return "b0rken";
vb@311
   571
    default:
vb@311
   572
        return "undefined";
vb@311
   573
    }
vb@311
   574
}
vb@311
   575
vb@311
   576
static void decorate_message(
vb@311
   577
    message *msg,
vb@311
   578
    PEP_color color,
vb@311
   579
    stringlist_t *keylist
vb@311
   580
    )
vb@311
   581
{
vb@311
   582
    assert(msg);
vb@311
   583
vb@311
   584
    add_opt_field(msg, "X-pEp-Version", "1.0");
vb@311
   585
    
vb@311
   586
    if (color != PEP_rating_undefined)
vb@311
   587
        add_opt_field(msg, "X-EncStatus", color_to_string(color));
vb@311
   588
vb@311
   589
    if (keylist) {
vb@311
   590
        char *_keylist = keylist_to_string(keylist);
vb@311
   591
        add_opt_field(msg, "X-KeyList", _keylist);
vb@311
   592
        free(_keylist);
vb@311
   593
    }
vb@311
   594
}
vb@311
   595
vb@311
   596
static PEP_color _rating(PEP_comm_type ct)
vb@311
   597
{
vb@311
   598
    if (ct == PEP_ct_unknown)
vb@311
   599
        return PEP_rating_undefined;
vb@311
   600
vb@311
   601
    else if (ct == PEP_ct_compromized)
vb@311
   602
        return PEP_rating_under_attack;
vb@311
   603
vb@311
   604
    else if (ct >= PEP_ct_confirmed_enc_anon)
vb@311
   605
        return PEP_rating_trusted_and_anonymized;
vb@311
   606
vb@311
   607
    else if (ct >= PEP_ct_strong_encryption)
vb@311
   608
        return PEP_rating_trusted;
vb@311
   609
vb@311
   610
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@311
   611
        return PEP_rating_reliable;
vb@311
   612
vb@311
   613
    else if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel)
vb@311
   614
        return PEP_rating_unencrypted;
vb@311
   615
vb@311
   616
    else
vb@311
   617
        return PEP_rating_unreliable;
vb@311
   618
}
vb@311
   619
vb@311
   620
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@311
   621
{
vb@311
   622
    char *ext;
vb@311
   623
vb@311
   624
    assert(blob);
vb@311
   625
vb@311
   626
    if (blob->filename == NULL)
vb@311
   627
        return false;
vb@311
   628
vb@311
   629
    ext = strrchr(blob->filename, '.');
vb@311
   630
    if (ext == NULL)
vb@311
   631
        return false;
vb@311
   632
vb@320
   633
    if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
vb@311
   634
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@311
   635
            strcmp(ext, ".asc") == 0)
vb@311
   636
            return true;
vb@311
   637
    }
vb@320
   638
    else if (strcmp(blob->mime_type, "text/plain") == 0) {
vb@311
   639
        if (strcmp(ext, ".asc") == 0)
vb@311
   640
            return true;
vb@311
   641
    }
vb@311
   642
vb@311
   643
    return false;
vb@311
   644
}
vb@311
   645
vb@311
   646
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@311
   647
{
vb@311
   648
    assert(blob);
vb@311
   649
    assert(blob->filename);
vb@311
   650
vb@311
   651
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@311
   652
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@311
   653
            strcmp(blob->filename + 11, ".asc") == 0)
vb@311
   654
            return true;
vb@311
   655
    }
vb@311
   656
vb@311
   657
    return false;
vb@311
   658
}
vb@311
   659
vb@311
   660
static char * without_double_ending(const char *filename)
vb@311
   661
{
vb@311
   662
    char *ext;
vb@311
   663
vb@311
   664
    assert(filename);
vb@311
   665
vb@311
   666
    ext = strrchr(filename, '.');
vb@311
   667
    if (ext == NULL)
vb@311
   668
        return NULL;
vb@311
   669
vb@311
   670
    return strndup(filename, ext - filename);
vb@311
   671
}
vb@311
   672
vb@311
   673
static PEP_color decrypt_color(PEP_STATUS status)
vb@311
   674
{
vb@311
   675
    switch (status) {
vb@311
   676
    case PEP_UNENCRYPTED:
vb@311
   677
    case PEP_VERIFIED:
vb@311
   678
    case PEP_VERIFY_NO_KEY:
vb@311
   679
    case PEP_VERIFIED_AND_TRUSTED:
vb@311
   680
        return PEP_rating_unencrypted;
vb@311
   681
vb@311
   682
    case PEP_DECRYPTED:
vb@311
   683
        return PEP_rating_unreliable;
vb@311
   684
vb@311
   685
    case PEP_DECRYPTED_AND_VERIFIED:
vb@311
   686
        return PEP_rating_reliable;
vb@311
   687
vb@311
   688
    case PEP_DECRYPT_NO_KEY:
vb@311
   689
        return PEP_rating_have_no_key;
vb@311
   690
vb@311
   691
    case PEP_DECRYPT_WRONG_FORMAT:
vb@311
   692
    case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@311
   693
        return PEP_rating_cannot_decrypt;
vb@311
   694
vb@311
   695
    default:
vb@311
   696
        return PEP_rating_undefined;
vb@311
   697
    }
vb@311
   698
}
vb@311
   699
vb@311
   700
static PEP_color key_color(PEP_SESSION session, const char *fpr)
vb@311
   701
{
vb@311
   702
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@311
   703
vb@311
   704
    assert(session);
vb@311
   705
    assert(fpr);
vb@311
   706
vb@311
   707
    PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
vb@311
   708
    if (status != PEP_STATUS_OK)
vb@311
   709
        return PEP_rating_undefined;
vb@311
   710
vb@311
   711
    return _rating(comm_type);
vb@311
   712
}
vb@311
   713
vb@311
   714
static PEP_color keylist_color(PEP_SESSION session, stringlist_t *keylist)
vb@311
   715
{
vb@311
   716
    PEP_color color = PEP_rating_reliable;
vb@311
   717
vb@311
   718
    assert(keylist && keylist->value);
vb@311
   719
    if (keylist == NULL || keylist->value == NULL)
vb@311
   720
        return PEP_rating_unencrypted;
vb@311
   721
vb@311
   722
    stringlist_t *_kl;
vb@311
   723
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   724
        PEP_comm_type ct;
vb@311
   725
        PEP_STATUS status;
vb@311
   726
vb@311
   727
        color = key_color(session, _kl->value);
vb@311
   728
        if (color == PEP_rating_under_attack)
vb@311
   729
            return PEP_rating_under_attack;
vb@311
   730
vb@311
   731
        if (color >= PEP_rating_reliable) {
vb@311
   732
            status = least_trust(session, _kl->value, &ct);
vb@311
   733
            if (status != PEP_STATUS_OK)
vb@311
   734
                return PEP_rating_undefined;
vb@311
   735
            if (ct == PEP_ct_unknown)
vb@311
   736
                color = PEP_rating_unreliable;
vb@311
   737
            else
vb@311
   738
                color = _rating(ct);
vb@311
   739
        }
vb@311
   740
    }
vb@311
   741
vb@311
   742
    return color;
vb@311
   743
}
vb@311
   744
vb@311
   745
static PEP_comm_type _get_comm_type(
vb@311
   746
    PEP_SESSION session,
vb@311
   747
    PEP_comm_type max_comm_type,
vb@311
   748
    pEp_identity *ident
vb@311
   749
    )
vb@311
   750
{
vb@311
   751
    PEP_STATUS status = update_identity(session, ident);
vb@311
   752
vb@311
   753
    if (max_comm_type == PEP_ct_compromized)
vb@311
   754
        return PEP_ct_compromized;
vb@311
   755
vb@311
   756
    if (status == PEP_STATUS_OK) {
vb@311
   757
        if (ident->comm_type == PEP_ct_compromized)
vb@311
   758
            return PEP_ct_compromized;
vb@311
   759
        else
vb@311
   760
            return MIN(max_comm_type, ident->comm_type);
vb@311
   761
    }
vb@311
   762
    else {
vb@311
   763
        return PEP_ct_unknown;
vb@311
   764
    }
vb@311
   765
}
vb@311
   766
vb@311
   767
void import_attached_keys(PEP_SESSION session, const message *msg)
vb@311
   768
{
vb@311
   769
    assert(session);
vb@311
   770
    assert(msg);
vb@311
   771
vb@311
   772
    bloblist_t *bl;
vb@311
   773
    for (bl = msg->attachments; bl && bl->value; bl = bl->next) {
vb@311
   774
        assert(bl && bl->value && bl->size);
vb@311
   775
vb@311
   776
        if (bl->mime_type == NULL ||
vb@311
   777
                    is_mime_type(bl, "application/octet-stream")) {
vb@311
   778
            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
vb@311
   779
                    is_fileending(bl, ".key") ||
vb@311
   780
                    string_equality(bl->filename, "key.asc"))
vb@311
   781
                import_key(session, bl->value, bl->size);
vb@311
   782
        }
vb@311
   783
        else if (is_mime_type(bl, "application/pgp-keys")) {
vb@311
   784
            import_key(session, bl->value, bl->size);
vb@311
   785
        }
vb@311
   786
        else if (is_mime_type(bl, "text/plain")) {
vb@311
   787
            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
vb@311
   788
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
vb@311
   789
                import_key(session, bl->value, bl->size);
vb@311
   790
        }
vb@311
   791
    }
vb@311
   792
}
vb@311
   793
vb@311
   794
void attach_own_key(PEP_SESSION session, message *msg)
vb@311
   795
{
vb@311
   796
    char *keydata;
vb@311
   797
    size_t size;
vb@311
   798
    bloblist_t *bl;
vb@311
   799
vb@311
   800
    assert(session);
vb@311
   801
    assert(msg);
vb@311
   802
vb@311
   803
    if (msg->dir == PEP_dir_incoming)
vb@311
   804
        return;
vb@311
   805
vb@311
   806
    assert(msg->from && msg->from->fpr);
vb@311
   807
    if (msg->from == NULL || msg->from->fpr == NULL)
vb@311
   808
        return;
vb@311
   809
vb@311
   810
    PEP_STATUS status = export_key(session, msg->from->fpr, &keydata, &size);
vb@311
   811
    assert(status == PEP_STATUS_OK);
vb@311
   812
    if (status != PEP_STATUS_OK)
vb@311
   813
        return;
vb@311
   814
    assert(size);
vb@311
   815
vb@311
   816
    bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
vb@311
   817
            "pEp_key.asc");
vb@311
   818
    if (msg->attachments == NULL && bl)
vb@311
   819
        msg->attachments = bl;
vb@311
   820
}
vb@311
   821
vb@311
   822
PEP_cryptotech determine_encryption_format(message *msg)
vb@311
   823
{
vb@311
   824
    assert(msg);
vb@311
   825
vb@311
   826
    if (is_PGP_message_text(msg->longmsg)) {
vb@311
   827
        msg->enc_format = PEP_enc_pieces;
vb@311
   828
        return PEP_crypt_OpenPGP;
vb@311
   829
    }
vb@311
   830
    else if (msg->attachments && msg->attachments->next &&
vb@311
   831
            is_mime_type(msg->attachments, "application/pgp-encrypted") &&
vb@311
   832
            is_PGP_message_text(msg->attachments->next->value)
vb@311
   833
        ) {
vb@311
   834
        msg->enc_format = PEP_enc_PGP_MIME;
vb@311
   835
        return PEP_crypt_OpenPGP;
vb@311
   836
    }
vb@311
   837
    else {
vb@311
   838
        msg->enc_format = PEP_enc_none;
vb@311
   839
        return PEP_crypt_none;
vb@311
   840
    }
vb@311
   841
}
vb@311
   842
vb@48
   843
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
   844
        PEP_SESSION session,
vb@113
   845
        message *src,
vb@37
   846
        stringlist_t * extra,
vb@38
   847
        message **dst,
vb@81
   848
        PEP_enc_format enc_format
vb@37
   849
    )
vb@37
   850
{
vb@37
   851
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
   852
    message * msg = NULL;
vb@63
   853
    stringlist_t * keys = NULL;
vb@37
   854
vb@37
   855
    assert(session);
vb@37
   856
    assert(src);
vb@37
   857
    assert(dst);
vb@260
   858
    assert(enc_format != PEP_enc_none);
vb@81
   859
vb@260
   860
    if (!(session && src && dst && enc_format != PEP_enc_none))
vb@191
   861
        return PEP_ILLEGAL_VALUE;
vb@191
   862
vb@259
   863
    determine_encryption_format(src);
vb@260
   864
    if (src->enc_format != PEP_enc_none)
vb@260
   865
        return PEP_ILLEGAL_VALUE;
vb@259
   866
vb@37
   867
    *dst = NULL;
vb@67
   868
vb@236
   869
    status = myself(session, src->from);
vb@236
   870
    if (status != PEP_STATUS_OK)
vb@236
   871
        goto pep_error;
vb@236
   872
vb@80
   873
    keys = new_stringlist(src->from->fpr);
vb@63
   874
    if (keys == NULL)
vb@63
   875
        goto enomem;
vb@37
   876
vb@39
   877
    stringlist_t *_k = keys;
vb@39
   878
vb@39
   879
    if (extra) {
vb@39
   880
        _k = stringlist_append(_k, extra);
vb@63
   881
        if (_k == NULL)
vb@63
   882
            goto enomem;
vb@37
   883
    }
vb@39
   884
vb@299
   885
    bool dest_keys_found = true;
vb@299
   886
vb@37
   887
    identity_list * _il;
vb@299
   888
    for (_il = src->to; _il && _il->ident; _il = _il->next) {
vb@299
   889
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
   890
        if (_status != PEP_STATUS_OK) {
vb@299
   891
            status = _status;
vb@63
   892
            goto pep_error;
vb@299
   893
        }
vb@63
   894
vb@299
   895
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@39
   896
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
   897
            if (_k == NULL)
vb@63
   898
                goto enomem;
vb@37
   899
        }
vb@299
   900
        else {
vb@299
   901
            dest_keys_found = false;
vb@37
   902
            status = PEP_KEY_NOT_FOUND;
vb@299
   903
        }
vb@299
   904
    }
vb@299
   905
vb@299
   906
    for (_il = src->cc; _il && _il->ident; _il = _il->next) {
vb@299
   907
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
   908
        if (_status != PEP_STATUS_OK)
vb@299
   909
        {
vb@299
   910
            status = _status;
vb@299
   911
            goto pep_error;
vb@299
   912
        }
vb@299
   913
vb@299
   914
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@299
   915
            _k = stringlist_add(_k, _il->ident->fpr);
vb@299
   916
            if (_k == NULL)
vb@299
   917
                goto enomem;
vb@299
   918
        }
vb@299
   919
        else {
vb@299
   920
            dest_keys_found = false;
vb@299
   921
            status = PEP_KEY_NOT_FOUND;
vb@299
   922
        }
vb@37
   923
    }
vb@37
   924
vb@301
   925
    if (!dest_keys_found) {
vb@301
   926
        free_stringlist(keys);
vb@301
   927
        attach_own_key(session, src);
vb@301
   928
        return PEP_UNENCRYPTED;
vb@301
   929
    }
vb@301
   930
    else {
vb@299
   931
        msg = clone_to_empty_message(src);
vb@299
   932
        if (msg == NULL)
vb@299
   933
            goto enomem;
vb@299
   934
vb@81
   935
        switch (enc_format) {
vb@260
   936
        case PEP_enc_PGP_MIME:
vb@336
   937
        case PEP_enc_PEP: // BUG: should be implemented extra
vb@260
   938
            status = encrypt_PGP_MIME(session, src, keys, msg);
vb@260
   939
            if (status != PEP_STATUS_OK)
vb@63
   940
                goto pep_error;
vb@260
   941
            break;
vb@62
   942
vb@62
   943
        case PEP_enc_pieces:
vb@260
   944
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
vb@319
   945
            if (status == PEP_OUT_OF_MEMORY)
vb@319
   946
                goto enomem;
vb@319
   947
            if (status != PEP_STATUS_OK) {
vb@319
   948
                attach_own_key(session, src);
vb@260
   949
                goto pep_error;
vb@319
   950
            }
vb@319
   951
            else {
vb@319
   952
                attach_own_key(session, msg);
vb@319
   953
            }
vb@38
   954
            break;
vb@38
   955
vb@336
   956
vb@336
   957
        /* case PEP_enc_PEP:
vb@81
   958
            // TODO: implement
vb@336
   959
            NOT_IMPLEMENTED */
vb@81
   960
vb@38
   961
        default:
vb@38
   962
            assert(0);
vb@63
   963
            status = PEP_ILLEGAL_VALUE;
vb@63
   964
            goto pep_error;
vb@37
   965
        }
vb@37
   966
    }
vb@37
   967
vb@37
   968
    free_stringlist(keys);
vb@63
   969
vb@299
   970
    if (msg && msg->shortmsg == NULL)
vb@64
   971
        msg->shortmsg = strdup("pEp");
vb@64
   972
vb@311
   973
    if (msg)
vb@311
   974
        decorate_message(msg, PEP_rating_undefined, NULL);
vb@311
   975
vb@63
   976
    *dst = msg;
vb@299
   977
    return status;
vb@63
   978
vb@63
   979
enomem:
vb@63
   980
    status = PEP_OUT_OF_MEMORY;
vb@63
   981
vb@63
   982
pep_error:
vb@63
   983
    free_stringlist(keys);
vb@63
   984
    free_message(msg);
vb@63
   985
vb@37
   986
    return status;
vb@37
   987
}
vb@37
   988
vb@48
   989
DYNAMIC_API PEP_STATUS decrypt_message(
vb@37
   990
        PEP_SESSION session,
vb@113
   991
        message *src,
vb@241
   992
        message **dst,
vb@251
   993
        stringlist_t **keylist,
vb@251
   994
        PEP_color *color
vb@37
   995
    )
vb@37
   996
{
vb@37
   997
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
   998
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
   999
    message *msg = NULL;
vb@112
  1000
    char *ctext;
vb@112
  1001
    size_t csize;
vb@269
  1002
    char *ptext = NULL;
vb@112
  1003
    size_t psize;
vb@241
  1004
    stringlist_t *_keylist = NULL;
vb@37
  1005
vb@74
  1006
    assert(session);
vb@74
  1007
    assert(src);
vb@74
  1008
    assert(dst);
vb@241
  1009
    assert(keylist);
vb@251
  1010
    assert(color);
vb@73
  1011
vb@251
  1012
    if (!(session && src && dst && keylist && color))
vb@191
  1013
        return PEP_ILLEGAL_VALUE;
vb@191
  1014
vb@259
  1015
    import_attached_keys(session, src);
vb@259
  1016
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
  1017
vb@74
  1018
    *dst = NULL;
vb@251
  1019
    *keylist = NULL;
vb@251
  1020
    *color = PEP_rating_undefined;
vb@81
  1021
 
vb@261
  1022
    switch (src->enc_format) {
vb@269
  1023
        case PEP_enc_none:
vb@301
  1024
            *color = PEP_rating_unencrypted;
vb@301
  1025
            return PEP_UNENCRYPTED;
vb@269
  1026
vb@261
  1027
        case PEP_enc_PGP_MIME:
vb@301
  1028
            ctext = src->attachments->next->value;
vb@269
  1029
            csize = src->attachments->next->size;
vb@117
  1030
vb@265
  1031
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
vb@265
  1032
                    csize, &ptext, &psize, &_keylist);
vb@261
  1033
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
vb@261
  1034
                goto pep_error;
vb@261
  1035
            decrypt_status = status;
vb@261
  1036
            break;
vb@261
  1037
vb@261
  1038
        case PEP_enc_pieces:
vb@261
  1039
            ctext = src->longmsg;
vb@261
  1040
            csize = strlen(ctext);
vb@261
  1041
vb@265
  1042
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
vb@265
  1043
                    csize, &ptext, &psize, &_keylist);
vb@261
  1044
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
vb@261
  1045
                goto pep_error;
vb@261
  1046
            decrypt_status = status;
vb@261
  1047
            break;
vb@261
  1048
vb@261
  1049
        default:
vb@261
  1050
            NOT_IMPLEMENTED
vb@259
  1051
    }
vb@113
  1052
vb@256
  1053
    *color = decrypt_color(status);
vb@259
  1054
vb@259
  1055
    if (*color != PEP_rating_under_attack) {
vb@259
  1056
        PEP_color kl_color = PEP_rating_undefined;
vb@259
  1057
vb@259
  1058
        if (_keylist)
vb@259
  1059
            kl_color = keylist_color(session, _keylist);
vb@259
  1060
vb@259
  1061
        if (kl_color == PEP_rating_under_attack)
vb@251
  1062
            *color = PEP_rating_under_attack;
vb@259
  1063
vb@260
  1064
        else if (*color >= PEP_rating_reliable &&
vb@259
  1065
                kl_color < PEP_rating_reliable)
vb@259
  1066
            *color = PEP_rating_unreliable;
vb@259
  1067
vb@260
  1068
        else if (*color >= PEP_rating_reliable &&
vb@260
  1069
                kl_color >= PEP_rating_trusted)
vb@260
  1070
            *color = kl_color;
vb@251
  1071
    }
vb@251
  1072
vb@256
  1073
    if (ptext) {
vb@256
  1074
        switch (src->enc_format) {
vb@256
  1075
            case PEP_enc_PGP_MIME:
vb@269
  1076
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  1077
                if (status != PEP_STATUS_OK)
vb@113
  1078
                    goto pep_error;
vb@256
  1079
                break;
vb@256
  1080
vb@256
  1081
            case PEP_enc_pieces:
vb@256
  1082
                msg = clone_to_empty_message(src);
vb@256
  1083
                if (msg == NULL)
vb@113
  1084
                    goto enomem;
vb@113
  1085
vb@256
  1086
                msg->longmsg = strdup(ptext);
vb@256
  1087
                if (msg->longmsg == NULL)
vb@256
  1088
                    goto enomem;
vb@256
  1089
vb@256
  1090
                bloblist_t *_m = msg->attachments;
vb@320
  1091
                if (_m == NULL && src->attachments && src->attachments->value) {
vb@320
  1092
                    msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@320
  1093
                    _m = msg->attachments;
vb@320
  1094
                }
vb@320
  1095
vb@256
  1096
                bloblist_t *_s;
vb@320
  1097
                for (_s = src->attachments; _s && _s->value; _s = _s->next) {
vb@256
  1098
                    if (is_encrypted_attachment(_s)) {
vb@256
  1099
                        stringlist_t *_keylist = NULL;
vb@301
  1100
                        ctext = _s->value;
vb@256
  1101
                        csize = _s->size;
vb@256
  1102
vb@256
  1103
                        status = decrypt_and_verify(session, ctext, csize,
vb@256
  1104
                                &ptext, &psize, &_keylist);
vb@256
  1105
                        free_stringlist(_keylist);
vb@256
  1106
vb@289
  1107
                        if (ptext) {
vb@289
  1108
                            if (is_encrypted_html_attachment(_s)) {
vb@290
  1109
                                msg->longmsg_formatted = strdup(ptext);
vb@290
  1110
                                if (msg->longmsg_formatted == NULL)
vb@290
  1111
                                    goto pep_error;
vb@289
  1112
                            }
vb@289
  1113
                            else {
vb@289
  1114
                                char * mime_type = "application/octet-stream";
vb@289
  1115
                                char * filename =
vb@289
  1116
                                    without_double_ending(_s->filename);
vb@289
  1117
                                if (filename == NULL)
vb@289
  1118
                                    goto enomem;
vb@289
  1119
vb@290
  1120
                                char *_ptext = malloc(psize);
vb@290
  1121
                                assert(_ptext);
vb@290
  1122
                                if (_ptext == NULL)
vb@290
  1123
                                    goto enomem;
vb@290
  1124
                                memcpy(_ptext, ptext, psize);
vb@290
  1125
vb@290
  1126
                                _m = bloblist_add(_m, _ptext, psize, mime_type,
vb@289
  1127
                                    filename);
vb@290
  1128
                                if (_m == NULL)
vb@289
  1129
                                    goto enomem;
vb@290
  1130
vb@290
  1131
                                if (msg->attachments == NULL)
vb@290
  1132
                                    msg->attachments = _m;
vb@289
  1133
                            }
vb@256
  1134
                        }
vb@256
  1135
                        else {
vb@320
  1136
                            char *copy = malloc(_s->size);
vb@320
  1137
                            memcpy(copy, _s->value, _s->size);
vb@320
  1138
                            _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@256
  1139
                            if (_m == NULL)
vb@256
  1140
                                goto enomem;
vb@256
  1141
                        }
vb@256
  1142
                    }
vb@320
  1143
                    else {
vb@320
  1144
                        char *copy = malloc(_s->size);
vb@320
  1145
                        memcpy(copy, _s->value, _s->size);
vb@320
  1146
                        _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@320
  1147
                        if (_m == NULL)
vb@320
  1148
                            goto enomem;
vb@320
  1149
                    }
vb@256
  1150
                }
vb@256
  1151
vb@256
  1152
                break;
vb@256
  1153
vb@256
  1154
            default:
vb@256
  1155
                // BUG: must implement more
vb@256
  1156
                NOT_IMPLEMENTED
vb@256
  1157
        }
vb@256
  1158
vb@256
  1159
        switch (src->enc_format) {
vb@256
  1160
            case PEP_enc_PGP_MIME:
vb@256
  1161
            case PEP_enc_pieces:
vb@256
  1162
                status = copy_fields(msg, src);
vb@256
  1163
                if (status != PEP_STATUS_OK)
vb@256
  1164
                    goto pep_error;
vb@256
  1165
vb@280
  1166
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1167
                {
vb@256
  1168
                    char * shortmsg;
vb@256
  1169
                    char * longmsg;
vb@256
  1170
vb@256
  1171
                    int r = seperate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1172
                            &longmsg);
vb@256
  1173
                    if (r == -1)
vb@256
  1174
                        goto enomem;
vb@256
  1175
vb@256
  1176
                    free(msg->shortmsg);
vb@256
  1177
                    free(msg->longmsg);
vb@256
  1178
vb@256
  1179
                    msg->shortmsg = shortmsg;
vb@256
  1180
                    msg->longmsg = longmsg;
vb@256
  1181
                }
vb@256
  1182
                else {
vb@256
  1183
                    msg->shortmsg = strdup(src->shortmsg);
vb@256
  1184
                    if (msg->shortmsg == NULL)
vb@256
  1185
                        goto enomem;
vb@256
  1186
                }
vb@256
  1187
                break;
vb@256
  1188
vb@256
  1189
            default:
vb@256
  1190
                // BUG: must implement more
vb@256
  1191
                NOT_IMPLEMENTED
vb@256
  1192
        }
vb@256
  1193
vb@256
  1194
        import_attached_keys(session, msg);
vb@113
  1195
    }
vb@113
  1196
vb@283
  1197
    if (msg)
vb@311
  1198
        decorate_message(msg, *color, _keylist);
vb@235
  1199
vb@74
  1200
    *dst = msg;
vb@241
  1201
    *keylist = _keylist;
vb@241
  1202
vb@267
  1203
    return PEP_STATUS_OK;
vb@73
  1204
vb@73
  1205
enomem:
vb@73
  1206
    status = PEP_OUT_OF_MEMORY;
vb@73
  1207
vb@73
  1208
pep_error:
vb@73
  1209
    free_message(msg);
vb@241
  1210
    free_stringlist(_keylist);
vb@39
  1211
vb@37
  1212
    return status;
vb@37
  1213
}
vb@37
  1214
vb@251
  1215
DYNAMIC_API PEP_STATUS outgoing_message_color(
vb@190
  1216
        PEP_SESSION session,
vb@190
  1217
        message *msg,
vb@232
  1218
        PEP_color *color
vb@190
  1219
    )
vb@190
  1220
{
vb@190
  1221
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
  1222
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  1223
    bool comm_type_determined = false;
vb@190
  1224
    identity_list * il;
vb@190
  1225
vb@190
  1226
    assert(session);
vb@190
  1227
    assert(msg);
vb@251
  1228
    assert(msg->from);
vb@251
  1229
    assert(msg->dir == PEP_dir_outgoing);
vb@190
  1230
    assert(color);
vb@190
  1231
vb@191
  1232
    if (!(session && msg && color))
vb@191
  1233
        return PEP_ILLEGAL_VALUE;
vb@191
  1234
vb@251
  1235
    if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
vb@251
  1236
        return PEP_ILLEGAL_VALUE;
vb@251
  1237
vb@237
  1238
    *color = PEP_rating_undefined;
vb@190
  1239
vb@251
  1240
    status = myself(session, msg->from);
vb@251
  1241
    if (status != PEP_STATUS_OK)
vb@251
  1242
        return status;
vb@251
  1243
vb@251
  1244
    for (il = msg->to; il != NULL; il = il->next) {
vb@251
  1245
        if (il->ident) {
vb@294
  1246
            update_identity(session, il->ident);
vb@251
  1247
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1248
                    il->ident);
vb@190
  1249
            comm_type_determined = true;
vb@251
  1250
        }
vb@251
  1251
    }
vb@190
  1252
vb@251
  1253
    for (il = msg->cc; il != NULL; il = il->next) {
vb@251
  1254
        if (il->ident) {
vb@294
  1255
            update_identity(session, il->ident);
vb@251
  1256
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1257
                    il->ident);
vb@251
  1258
            comm_type_determined = true;
vb@251
  1259
        }
vb@190
  1260
    }
vb@190
  1261
vb@190
  1262
    if (comm_type_determined == false)
vb@237
  1263
        *color = PEP_rating_undefined;
vb@190
  1264
    else
vb@300
  1265
        *color = MAX(_rating(max_comm_type), PEP_rating_unencrypted);
vb@190
  1266
vb@190
  1267
    return PEP_STATUS_OK;
vb@190
  1268
}
vb@190
  1269
vb@240
  1270
DYNAMIC_API PEP_STATUS identity_color(
vb@239
  1271
        PEP_SESSION session,
vb@239
  1272
        pEp_identity *ident,
vb@239
  1273
        PEP_color *color
vb@239
  1274
    )
vb@239
  1275
{
vb@239
  1276
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  1277
vb@239
  1278
    assert(session);
vb@239
  1279
    assert(ident);
vb@239
  1280
    assert(color);
vb@239
  1281
vb@239
  1282
    if (!(session && ident && color))
vb@239
  1283
        return PEP_ILLEGAL_VALUE;
vb@239
  1284
vb@239
  1285
    if (ident->me)
vb@239
  1286
        status = myself(session, ident);
vb@239
  1287
    else
vb@239
  1288
        status = update_identity(session, ident);
vb@239
  1289
vb@239
  1290
    if (status == PEP_STATUS_OK)
vb@239
  1291
        *color = _rating(ident->comm_type);
vb@239
  1292
vb@239
  1293
    return status;
vb@239
  1294
}
vb@239
  1295