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