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