src/message_api.c
author Roker <roker@pep-project.org>
Tue, 13 Jun 2017 21:15:16 +0200
changeset 1853 a86d6290d493
parent 1852 8b786dad8b60
child 1857 52e46f43ac67
child 1858 600dc0395ec0
permissions -rw-r--r--
rename macro ERROR(x) into ADD_TO_LOG(x) to avoid problems on MSVC
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
vb@125
     4
#include "pEp_internal.h"
vb@37
     5
#include "message_api.h"
vb@37
     6
vb@130
     7
#include "platform.h"
vb@220
     8
#include "mime.h"
vb@952
     9
#include "sync_fsm.h"
vb@85
    10
vb@37
    11
#include <assert.h>
vb@37
    12
#include <string.h>
vb@39
    13
#include <stdlib.h>
vb@39
    14
vb@926
    15
vb@951
    16
#ifndef _MIN
vb@951
    17
#define _MIN(A, B) ((B) > (A) ? (A) : (B))
vb@190
    18
#endif
vb@951
    19
#ifndef _MAX
vb@951
    20
#define _MAX(A, B) ((B) > (A) ? (B) : (A))
vb@300
    21
#endif
vb@190
    22
vb@235
    23
vb@235
    24
static bool string_equality(const char *s1, const char *s2)
vb@235
    25
{
vb@235
    26
    if (s1 == NULL || s2 == NULL)
vb@235
    27
        return false;
vb@235
    28
vb@235
    29
    assert(s1 && s2);
vb@235
    30
vb@235
    31
    return strcmp(s1, s2) == 0;
vb@235
    32
}
vb@235
    33
vb@235
    34
static bool is_mime_type(const bloblist_t *bl, const char *mt)
vb@235
    35
{
vb@235
    36
    assert(mt);
vb@235
    37
vb@235
    38
    return bl && string_equality(bl->mime_type, mt);
vb@235
    39
}
vb@235
    40
krista@854
    41
//
krista@854
    42
// This function presumes the file ending is a proper substring of the
krista@854
    43
// filename (i.e. if bl->filename is "a.pgp" and fe is ".pgp", it will
krista@854
    44
// return true, but if bl->filename is ".pgp" and fe is ".pgp", it will
krista@854
    45
// return false. This is desired behaviour.
krista@854
    46
//
vb@235
    47
static bool is_fileending(const bloblist_t *bl, const char *fe)
vb@235
    48
{
vb@235
    49
    assert(fe);
krista@1427
    50
krista@853
    51
    if (bl == NULL || bl->filename == NULL || fe == NULL)
vb@235
    52
        return false;
vb@235
    53
vb@235
    54
    assert(bl && bl->filename);
vb@235
    55
vb@235
    56
    size_t fe_len = strlen(fe);
vb@235
    57
    size_t fn_len = strlen(bl->filename);
vb@235
    58
vb@235
    59
    if (fn_len <= fe_len)
vb@235
    60
        return false;
vb@235
    61
vb@235
    62
    assert(fn_len > fe_len);
vb@235
    63
vb@235
    64
    return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
vb@235
    65
}
vb@235
    66
vb@952
    67
void add_opt_field(message *msg, const char *name, const char *value)
vb@284
    68
{
vb@952
    69
    assert(msg && name && value);
vb@284
    70
vb@284
    71
    if (msg && name && value) {
vb@284
    72
        stringpair_t *pair = new_stringpair(name, value);
vb@284
    73
        if (pair == NULL)
vb@284
    74
            return;
vb@284
    75
vb@284
    76
        stringpair_list_t *field = stringpair_list_add(msg->opt_fields, pair);
vb@284
    77
        if (field == NULL)
Edouard@833
    78
        {
Edouard@833
    79
            free_stringpair(pair);
vb@284
    80
            return;
Edouard@833
    81
        }
vb@284
    82
vb@284
    83
        if (msg->opt_fields == NULL)
vb@284
    84
            msg->opt_fields = field;
vb@284
    85
    }
vb@284
    86
}
vb@284
    87
vb@83
    88
static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
vb@62
    89
{
vb@83
    90
    assert(shortmsg);
vb@83
    91
    assert(strcmp(shortmsg, "pEp") != 0);
vb@855
    92
vb@857
    93
    if (!shortmsg || strcmp(shortmsg, "pEp") == 0) {
vb@857
    94
        if (!longmsg) {
vb@857
    95
            return NULL;
vb@857
    96
        }
vb@857
    97
        else {
vb@857
    98
            char *result = strdup(longmsg);
vb@857
    99
            assert(result);
vb@857
   100
            return result;
vb@857
   101
        }
vb@857
   102
    }
krista@1427
   103
vb@83
   104
    if (longmsg == NULL)
vb@63
   105
        longmsg = "";
vb@63
   106
krista@935
   107
    const char * const subject = "Subject: ";
krista@935
   108
    const size_t SUBJ_LEN = 9;
krista@935
   109
    const char * const newlines = "\n\n";
krista@935
   110
    const size_t NL_LEN = 2;
krista@935
   111
roker@1559
   112
    const size_t bufsize = SUBJ_LEN + strlen(shortmsg) + NL_LEN + strlen(longmsg) + 1;
roker@1559
   113
    char * ptext = calloc(1, bufsize);
vb@109
   114
    assert(ptext);
vb@62
   115
    if (ptext == NULL)
vb@62
   116
        return NULL;
vb@62
   117
krista@935
   118
    strlcpy(ptext, subject, bufsize);
krista@918
   119
    strlcat(ptext, shortmsg, bufsize);
krista@935
   120
    strlcat(ptext, newlines, bufsize);
krista@918
   121
    strlcat(ptext, longmsg, bufsize);
vb@62
   122
vb@62
   123
    return ptext;
vb@62
   124
}
vb@44
   125
krista@853
   126
static int separate_short_and_long(const char *src, char **shortmsg, char **longmsg)
vb@82
   127
{
vb@82
   128
    char *_shortmsg = NULL;
vb@82
   129
    char *_longmsg = NULL;
vb@82
   130
vb@82
   131
    assert(src);
vb@82
   132
    assert(shortmsg);
vb@82
   133
    assert(longmsg);
krista@1427
   134
krista@853
   135
    if (src == NULL || shortmsg == NULL || longmsg == NULL)
krista@853
   136
        return -1;
vb@82
   137
vb@82
   138
    *shortmsg = NULL;
vb@82
   139
    *longmsg = NULL;
vb@82
   140
vb@85
   141
    if (strncasecmp(src, "subject: ", 9) == 0) {
vb@82
   142
        char *line_end = strchr(src, '\n');
vb@311
   143
vb@82
   144
        if (line_end == NULL) {
vb@82
   145
            _shortmsg = strdup(src + 9);
vb@469
   146
            assert(_shortmsg);
vb@82
   147
            if (_shortmsg == NULL)
vb@82
   148
                goto enomem;
vb@82
   149
            // _longmsg = NULL;
vb@82
   150
        }
vb@82
   151
        else {
vb@82
   152
            size_t n = line_end - src;
vb@166
   153
vb@82
   154
            if (*(line_end - 1) == '\r')
vb@166
   155
                _shortmsg = strndup(src + 9, n - 10);
vb@82
   156
            else
vb@166
   157
                _shortmsg = strndup(src + 9, n - 9);
vb@469
   158
            assert(_shortmsg);
vb@82
   159
            if (_shortmsg == NULL)
vb@82
   160
                goto enomem;
vb@166
   161
vb@166
   162
            while (*(src + n) && (*(src + n) == '\n' || *(src + n) == '\r'))
vb@166
   163
                ++n;
vb@166
   164
vb@166
   165
            if (*(src + n)) {
vb@166
   166
                _longmsg = strdup(src + n);
vb@469
   167
                assert(_longmsg);
vb@166
   168
                if (_longmsg == NULL)
vb@166
   169
                    goto enomem;
vb@166
   170
            }
vb@82
   171
        }
krista@1852
   172
        *shortmsg = _shortmsg;
vb@82
   173
    }
vb@82
   174
    else {
krista@1852
   175
        // If there's no "Subject: " and the shortmsg is
krista@1852
   176
        // pEp (or anything else), then we shouldn't be replacing it.
krista@1852
   177
        // Chances are that the message wasn't encrypted
krista@1852
   178
        // using pEp and that the actually subject IS pEp. In any event,
krista@1852
   179
        // erasing the subject line when we don't have one in the plaintext
krista@1852
   180
        // isn't the right behaviour.
krista@1852
   181
        // _shortmsg = strdup("");
vb@82
   182
        _longmsg = strdup(src);
vb@469
   183
        assert(_longmsg);
vb@82
   184
        if (_longmsg == NULL)
vb@82
   185
            goto enomem;
vb@82
   186
    }
krista@1852
   187
    
vb@82
   188
    *longmsg = _longmsg;
vb@82
   189
vb@82
   190
    return 0;
vb@82
   191
vb@82
   192
enomem:
vb@82
   193
    free(_shortmsg);
vb@82
   194
    free(_longmsg);
vb@82
   195
vb@82
   196
    return -1;
vb@82
   197
}
vb@82
   198
vb@113
   199
static PEP_STATUS copy_fields(message *dst, const message *src)
vb@113
   200
{
vb@164
   201
    assert(dst);
vb@164
   202
    assert(src);
vb@164
   203
Edouard@840
   204
    if(!(dst && src))
Edouard@840
   205
        return PEP_ILLEGAL_VALUE;
Edouard@840
   206
vb@113
   207
    free_timestamp(dst->sent);
vb@113
   208
    dst->sent = NULL;
vb@113
   209
    if (src->sent) {
vb@113
   210
        dst->sent = timestamp_dup(src->sent);
vb@113
   211
        if (dst->sent == NULL)
vb@113
   212
            return PEP_OUT_OF_MEMORY;
vb@113
   213
    }
vb@113
   214
vb@113
   215
    free_timestamp(dst->recv);
vb@113
   216
    dst->recv = NULL;
vb@113
   217
    if (src->recv) {
vb@113
   218
        dst->recv = timestamp_dup(src->recv);
vb@113
   219
        if (dst->recv == NULL)
vb@113
   220
            return PEP_OUT_OF_MEMORY;
vb@113
   221
    }
vb@113
   222
vb@113
   223
    free_identity(dst->from);
vb@113
   224
    dst->from = NULL;
vb@113
   225
    if (src->from) {
vb@113
   226
        dst->from = identity_dup(src->from);
vb@113
   227
        if (dst->from == NULL)
vb@113
   228
            return PEP_OUT_OF_MEMORY;
vb@113
   229
    }
vb@113
   230
vb@113
   231
    free_identity_list(dst->to);
vb@113
   232
    dst->to = NULL;
vb@274
   233
    if (src->to && src->to->ident) {
vb@113
   234
        dst->to = identity_list_dup(src->to);
vb@113
   235
        if (dst->to == NULL)
vb@113
   236
            return PEP_OUT_OF_MEMORY;
vb@113
   237
    }
vb@113
   238
vb@113
   239
    free_identity(dst->recv_by);
vb@113
   240
    dst->recv_by = NULL;
vb@113
   241
    if (src->recv_by) {
vb@113
   242
        dst->recv_by = identity_dup(src->recv_by);
vb@113
   243
        if (dst->recv_by == NULL)
vb@113
   244
            return PEP_OUT_OF_MEMORY;
vb@113
   245
    }
vb@113
   246
vb@113
   247
    free_identity_list(dst->cc);
vb@113
   248
    dst->cc = NULL;
vb@274
   249
    if (src->cc && src->cc->ident) {
vb@113
   250
        dst->cc = identity_list_dup(src->cc);
vb@113
   251
        if (dst->cc == NULL)
vb@113
   252
            return PEP_OUT_OF_MEMORY;
vb@113
   253
    }
vb@113
   254
vb@113
   255
    free_identity_list(dst->bcc);
vb@113
   256
    dst->bcc = NULL;
vb@274
   257
    if (src->bcc && src->bcc->ident) {
vb@113
   258
        dst->bcc = identity_list_dup(src->bcc);
vb@113
   259
        if (dst->bcc == NULL)
vb@113
   260
            return PEP_OUT_OF_MEMORY;
vb@113
   261
    }
vb@113
   262
vb@113
   263
    free_identity_list(dst->reply_to);
vb@113
   264
    dst->reply_to = NULL;
vb@274
   265
    if (src->reply_to && src->reply_to->ident) {
vb@113
   266
        dst->reply_to = identity_list_dup(src->reply_to);
vb@113
   267
        if (dst->reply_to == NULL)
vb@113
   268
            return PEP_OUT_OF_MEMORY;
vb@113
   269
    }
vb@113
   270
vb@113
   271
    free_stringlist(dst->in_reply_to);
vb@113
   272
    dst->in_reply_to = NULL;
vb@274
   273
    if (src->in_reply_to && src->in_reply_to->value) {
vb@113
   274
        dst->in_reply_to = stringlist_dup(src->in_reply_to);
vb@113
   275
        if (dst->in_reply_to == NULL)
vb@113
   276
            return PEP_OUT_OF_MEMORY;
vb@113
   277
    }
vb@113
   278
vb@113
   279
    free_stringlist(dst->references);
vb@113
   280
    dst->references = NULL;
vb@113
   281
    if (src->references) {
vb@113
   282
        dst->references = stringlist_dup(src->references);
vb@113
   283
        if (dst->references == NULL)
vb@113
   284
            return PEP_OUT_OF_MEMORY;
vb@113
   285
    }
vb@113
   286
vb@113
   287
    free_stringlist(dst->keywords);
vb@113
   288
    dst->keywords = NULL;
vb@274
   289
    if (src->keywords && src->keywords->value) {
vb@113
   290
        dst->keywords = stringlist_dup(src->keywords);
vb@113
   291
        if (dst->keywords == NULL)
vb@113
   292
            return PEP_OUT_OF_MEMORY;
vb@113
   293
    }
vb@113
   294
vb@113
   295
    free(dst->comments);
vb@113
   296
    dst->comments = NULL;
vb@113
   297
    if (src->comments) {
vb@113
   298
        dst->comments = strdup(src->comments);
vb@113
   299
        assert(dst->comments);
vb@113
   300
        if (dst->comments == NULL)
vb@113
   301
            return PEP_OUT_OF_MEMORY;
vb@113
   302
    }
vb@113
   303
vb@1092
   304
    free_stringpair_list(dst->opt_fields);
vb@1092
   305
    dst->opt_fields = NULL;
vb@1092
   306
    if (src->opt_fields) {
vb@1092
   307
        dst->opt_fields = stringpair_list_dup(src->opt_fields);
vb@1092
   308
        if (dst->opt_fields == NULL)
vb@1092
   309
            return PEP_OUT_OF_MEMORY;
vb@1092
   310
    }
vb@1092
   311
vb@113
   312
    return PEP_STATUS_OK;
vb@113
   313
}
vb@113
   314
vb@81
   315
static message * clone_to_empty_message(const message * src)
vb@80
   316
{
vb@113
   317
    PEP_STATUS status;
vb@80
   318
    message * msg = NULL;
vb@80
   319
vb@81
   320
    assert(src);
krista@853
   321
    if (src == NULL)
krista@853
   322
        return NULL;
vb@81
   323
vb@113
   324
    msg = calloc(1, sizeof(message));
vb@113
   325
    assert(msg);
vb@80
   326
    if (msg == NULL)
vb@80
   327
        goto enomem;
vb@80
   328
vb@82
   329
    msg->dir = src->dir;
vb@82
   330
vb@113
   331
    status = copy_fields(msg, src);
vb@113
   332
    if (status != PEP_STATUS_OK)
vb@113
   333
        goto enomem;
vb@81
   334
vb@80
   335
    return msg;
vb@80
   336
vb@80
   337
enomem:
vb@113
   338
    free_message(msg);
vb@80
   339
    return NULL;
vb@80
   340
}
vb@80
   341
vb@260
   342
static PEP_STATUS encrypt_PGP_MIME(
vb@311
   343
    PEP_SESSION session,
vb@311
   344
    const message *src,
vb@311
   345
    stringlist_t *keys,
krista@1639
   346
    message *dst,
krista@1639
   347
    PEP_encrypt_flags_t flags
vb@260
   348
    )
vb@260
   349
{
vb@260
   350
    PEP_STATUS status = PEP_STATUS_OK;
vb@260
   351
    bool free_ptext = false;
vb@468
   352
    char *ptext = NULL;
Edouard@882
   353
    char *ctext = NULL;
vb@260
   354
    char *mimetext = NULL;
vb@260
   355
    size_t csize;
vb@260
   356
    assert(dst->longmsg == NULL);
vb@260
   357
    dst->enc_format = PEP_enc_PGP_MIME;
vb@260
   358
vb@260
   359
    if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@466
   360
        if (session->unencrypted_subject) {
vb@466
   361
            dst->shortmsg = strdup(src->shortmsg);
vb@469
   362
            assert(dst->shortmsg);
vb@466
   363
            if (dst->shortmsg == NULL)
vb@466
   364
                goto enomem;
vb@466
   365
            ptext = src->longmsg;
vb@466
   366
        }
vb@466
   367
        else {
vb@466
   368
            ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@466
   369
            if (ptext == NULL)
vb@466
   370
                goto enomem;
vb@466
   371
            free_ptext = true;
vb@466
   372
        }
vb@260
   373
    }
vb@260
   374
    else if (src->longmsg) {
vb@260
   375
        ptext = src->longmsg;
vb@260
   376
    }
vb@260
   377
    else {
vb@260
   378
        ptext = "pEp";
vb@260
   379
    }
vb@260
   380
vb@260
   381
    message *_src = calloc(1, sizeof(message));
vb@260
   382
    assert(_src);
vb@260
   383
    if (_src == NULL)
vb@260
   384
        goto enomem;
vb@260
   385
    _src->longmsg = ptext;
vb@260
   386
    _src->longmsg_formatted = src->longmsg_formatted;
vb@260
   387
    _src->attachments = src->attachments;
vb@260
   388
    _src->enc_format = PEP_enc_none;
vb@260
   389
    status = mime_encode_message(_src, true, &mimetext);
vb@260
   390
    assert(status == PEP_STATUS_OK);
krista@853
   391
    if (status != PEP_STATUS_OK)
krista@853
   392
        goto pep_error;
krista@1427
   393
Edouard@371
   394
    if (free_ptext){
vb@260
   395
        free(ptext);
Edouard@371
   396
        free_ptext=0;
Edouard@371
   397
    }
vb@260
   398
    free(_src);
vb@260
   399
    assert(mimetext);
vb@260
   400
    if (mimetext == NULL)
vb@260
   401
        goto pep_error;
vb@260
   402
krista@1639
   403
    if (flags & PEP_encrypt_flag_force_unsigned)
krista@1639
   404
        status = encrypt_only(session, keys, mimetext, strlen(mimetext),
krista@1639
   405
            &ctext, &csize);
krista@1639
   406
    else
krista@1639
   407
        status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
krista@1639
   408
            &ctext, &csize);
vb@260
   409
    free(mimetext);
vb@260
   410
    if (ctext == NULL)
vb@260
   411
        goto pep_error;
vb@260
   412
vb@260
   413
    dst->longmsg = strdup("this message was encrypted with p≡p "
lix@487
   414
        "https://pEp-project.org");
vb@469
   415
    assert(dst->longmsg);
vb@260
   416
    if (dst->longmsg == NULL)
vb@260
   417
        goto enomem;
vb@260
   418
vb@260
   419
    char *v = strdup("Version: 1");
vb@469
   420
    assert(v);
vb@260
   421
    if (v == NULL)
vb@260
   422
        goto enomem;
vb@260
   423
roker@801
   424
    bloblist_t *_a = new_bloblist(v, strlen(v), "application/pgp-encrypted", NULL);
vb@260
   425
    if (_a == NULL)
vb@260
   426
        goto enomem;
vb@260
   427
    dst->attachments = _a;
vb@288
   428
Edouard@882
   429
    _a = bloblist_add(_a, ctext, csize, "application/octet-stream",
Edouard@427
   430
        "msg.asc");
vb@260
   431
    if (_a == NULL)
vb@260
   432
        goto enomem;
vb@260
   433
vb@260
   434
    return PEP_STATUS_OK;
vb@260
   435
vb@260
   436
enomem:
vb@260
   437
    status = PEP_OUT_OF_MEMORY;
vb@260
   438
vb@260
   439
pep_error:
vb@260
   440
    if (free_ptext)
vb@260
   441
        free(ptext);
Edouard@882
   442
    free(ctext);
vb@260
   443
    return status;
vb@260
   444
}
vb@260
   445
vb@260
   446
static PEP_STATUS encrypt_PGP_in_pieces(
vb@311
   447
    PEP_SESSION session,
vb@311
   448
    const message *src,
vb@311
   449
    stringlist_t *keys,
krista@1639
   450
    message *dst,
krista@1639
   451
    PEP_encrypt_flags_t flags
vb@260
   452
    )
vb@260
   453
{
vb@260
   454
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@882
   455
    char *ctext = NULL;
vb@260
   456
    size_t csize;
vb@468
   457
    char *ptext = NULL;
vb@466
   458
    bool free_ptext = false;
vb@260
   459
vb@260
   460
    assert(dst->longmsg == NULL);
vb@260
   461
    assert(dst->attachments == NULL);
vb@260
   462
vb@260
   463
    dst->enc_format = PEP_enc_pieces;
vb@260
   464
krista@1639
   465
    bool nosign = (flags & PEP_encrypt_flag_force_unsigned);
krista@1639
   466
vb@320
   467
    if (src->shortmsg && src->shortmsg[0] && strcmp(src->shortmsg, "pEp") != 0) {
vb@466
   468
        if (session->unencrypted_subject) {
vb@466
   469
            dst->shortmsg = strdup(src->shortmsg);
vb@469
   470
            assert(dst->shortmsg);
vb@466
   471
            if (dst->shortmsg == NULL)
vb@466
   472
                goto enomem;
vb@466
   473
            ptext = src->longmsg;
vb@466
   474
        }
vb@466
   475
        else {
vb@466
   476
            ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@466
   477
            if (ptext == NULL)
vb@466
   478
                goto enomem;
vb@466
   479
            free_ptext = true;
vb@466
   480
        }
vb@260
   481
krista@1639
   482
        if (nosign)
krista@1639
   483
            status = encrypt_only(session, keys, ptext, strlen(ptext), &ctext,
krista@1639
   484
                &csize);
krista@1639
   485
        else 
krista@1639
   486
            status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
krista@1639
   487
                &csize);
vb@466
   488
        if (free_ptext)
vb@466
   489
            free(ptext);
vb@466
   490
        free_ptext = false;
vb@288
   491
        if (ctext) {
Edouard@882
   492
            dst->longmsg = ctext;
vb@288
   493
        }
vb@288
   494
        else {
vb@260
   495
            goto pep_error;
vb@288
   496
        }
vb@260
   497
    }
vb@320
   498
    else if (src->longmsg && src->longmsg[0]) {
vb@466
   499
        ptext = src->longmsg;
krista@1639
   500
        if (nosign)
krista@1639
   501
            status = encrypt_only(session, keys, ptext, strlen(ptext), &ctext,
krista@1639
   502
                &csize);
krista@1639
   503
        else 
krista@1639
   504
            status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
krista@1639
   505
                &csize);
vb@288
   506
        if (ctext) {
Edouard@882
   507
            dst->longmsg = ctext;
vb@288
   508
        }
vb@288
   509
        else {
vb@260
   510
            goto pep_error;
vb@288
   511
        }
vb@284
   512
    }
vb@284
   513
    else {
vb@284
   514
        dst->longmsg = strdup("");
vb@469
   515
        assert(dst->longmsg);
vb@284
   516
        if (dst->longmsg == NULL)
vb@284
   517
            goto enomem;
vb@260
   518
    }
vb@260
   519
vb@320
   520
    if (src->longmsg_formatted && src->longmsg_formatted[0]) {
vb@466
   521
        ptext = src->longmsg_formatted;
krista@1639
   522
        if (nosign)
krista@1639
   523
            status = encrypt_only(session, keys, ptext, strlen(ptext), &ctext,
krista@1639
   524
                &csize);
krista@1639
   525
        else 
krista@1639
   526
            status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
krista@1639
   527
                &csize);
vb@260
   528
        if (ctext) {
vb@288
   529
Edouard@882
   530
            bloblist_t *_a = bloblist_add(dst->attachments, ctext, csize,
vb@311
   531
                "application/octet-stream", "PGPexch.htm.pgp");
vb@284
   532
            if (_a == NULL)
vb@260
   533
                goto enomem;
vb@284
   534
            if (dst->attachments == NULL)
vb@284
   535
                dst->attachments = _a;
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
    if (src->attachments) {
vb@284
   543
        if (dst->attachments == NULL) {
vb@284
   544
            dst->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@284
   545
            if (dst->attachments == NULL)
vb@284
   546
                goto enomem;
vb@284
   547
        }
vb@260
   548
vb@284
   549
        bloblist_t *_s = src->attachments;
vb@284
   550
        bloblist_t *_d = dst->attachments;
vb@284
   551
Edouard@754
   552
        for (int n = 0; _s; _s = _s->next) {
Edouard@754
   553
            if (_s->value == NULL && _s->size == 0) {
Edouard@754
   554
                _d = bloblist_add(_d, NULL, 0, _s->mime_type, _s->filename);
vb@260
   555
                if (_d == NULL)
vb@260
   556
                    goto enomem;
vb@260
   557
            }
vb@260
   558
            else {
Edouard@754
   559
                size_t psize = _s->size;
Edouard@754
   560
                ptext = _s->value;
krista@1639
   561
                if (nosign)
krista@1639
   562
                    status = encrypt_only(session, keys, ptext, psize, &ctext,
krista@1639
   563
                        &csize);
krista@1639
   564
                else 
krista@1639
   565
                    status = encrypt_and_sign(session, keys, ptext, psize, &ctext,
krista@1639
   566
                        &csize);
Edouard@754
   567
                if (ctext) {
Edouard@754
   568
                    char *filename = NULL;
Edouard@754
   569
Edouard@754
   570
                    if (_s->filename) {
Edouard@754
   571
                        size_t len = strlen(_s->filename);
krista@975
   572
                        size_t bufsize = len + 5; // length of .pgp extension + NUL
krista@975
   573
                        filename = calloc(1, bufsize);
Edouard@754
   574
                        if (filename == NULL)
Edouard@754
   575
                            goto enomem;
Edouard@754
   576
krista@975
   577
                        strlcpy(filename, _s->filename, bufsize);
krista@975
   578
                        strlcat(filename, ".pgp", bufsize);
Edouard@754
   579
                    }
Edouard@754
   580
                    else {
Edouard@754
   581
                        filename = calloc(1, 20);
Edouard@754
   582
                        if (filename == NULL)
Edouard@754
   583
                            goto enomem;
Edouard@754
   584
Edouard@754
   585
                        ++n;
Edouard@754
   586
                        n &= 0xffff;
Edouard@754
   587
                        snprintf(filename, 20, "Attachment%d.pgp", n);
Edouard@754
   588
                    }
Edouard@754
   589
Edouard@882
   590
                    _d = bloblist_add(_d, ctext, csize, "application/octet-stream",
Edouard@754
   591
                        filename);
roker@866
   592
                    free(filename);
Edouard@754
   593
                    if (_d == NULL)
Edouard@754
   594
                        goto enomem;
Edouard@754
   595
                }
Edouard@754
   596
                else {
Edouard@754
   597
                    goto pep_error;
Edouard@754
   598
                }
vb@260
   599
            }
vb@260
   600
        }
vb@260
   601
    }
vb@260
   602
vb@260
   603
    return PEP_STATUS_OK;
vb@260
   604
vb@260
   605
enomem:
vb@260
   606
    status = PEP_OUT_OF_MEMORY;
vb@260
   607
vb@260
   608
pep_error:
vb@466
   609
    if (free_ptext)
vb@466
   610
        free(ptext);
vb@260
   611
    return status;
vb@260
   612
}
vb@260
   613
vb@311
   614
static char * keylist_to_string(const stringlist_t *keylist)
vb@311
   615
{
vb@311
   616
    if (keylist) {
vb@311
   617
        size_t size = stringlist_length(keylist);
vb@311
   618
vb@311
   619
        const stringlist_t *_kl;
vb@311
   620
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   621
            size += strlen(_kl->value);
vb@311
   622
        }
vb@311
   623
vb@311
   624
        char *result = calloc(1, size);
vb@311
   625
        if (result == NULL)
vb@311
   626
            return NULL;
vb@311
   627
vb@311
   628
        char *_r = result;
vb@311
   629
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   630
            _r = stpcpy(_r, _kl->value);
vb@311
   631
            if (_kl->next && _kl->next->value)
vb@311
   632
                _r = stpcpy(_r, ",");
vb@311
   633
        }
vb@311
   634
vb@311
   635
        return result;
vb@311
   636
    }
vb@311
   637
    else {
vb@311
   638
        return NULL;
vb@311
   639
    }
vb@311
   640
}
vb@311
   641
vb@1004
   642
static const char * rating_to_string(PEP_rating rating)
vb@311
   643
{
vb@1004
   644
    switch (rating) {
vb@311
   645
    case PEP_rating_cannot_decrypt:
vb@311
   646
        return "cannot_decrypt";
vb@311
   647
    case PEP_rating_have_no_key:
vb@311
   648
        return "have_no_key";
vb@311
   649
    case PEP_rating_unencrypted:
vb@311
   650
        return "unencrypted";
vb@486
   651
    case PEP_rating_unencrypted_for_some:
vb@486
   652
        return "unencrypted_for_some";
vb@311
   653
    case PEP_rating_unreliable:
vb@311
   654
        return "unreliable";
vb@311
   655
    case PEP_rating_reliable:
vb@311
   656
        return "reliable";
vb@311
   657
    case PEP_rating_trusted:
vb@311
   658
        return "trusted";
vb@311
   659
    case PEP_rating_trusted_and_anonymized:
vb@311
   660
        return "trusted_and_anonymized";
vb@311
   661
    case PEP_rating_fully_anonymous:
vb@311
   662
        return "fully_anonymous";
Edouard@442
   663
    case PEP_rating_mistrust:
Edouard@442
   664
        return "mistrust";
Edouard@442
   665
    case PEP_rating_b0rken:
Edouard@442
   666
        return "b0rken";
vb@311
   667
    case PEP_rating_under_attack:
krista@961
   668
        return "under_attack";
vb@311
   669
    default:
vb@311
   670
        return "undefined";
vb@311
   671
    }
vb@311
   672
}
vb@311
   673
vb@311
   674
static void decorate_message(
vb@311
   675
    message *msg,
vb@1004
   676
    PEP_rating rating,
vb@311
   677
    stringlist_t *keylist
vb@311
   678
    )
vb@311
   679
{
vb@311
   680
    assert(msg);
vb@311
   681
krista@942
   682
    add_opt_field(msg, "X-pEp-Version", PEP_VERSION);
krista@1427
   683
vb@1004
   684
    if (rating != PEP_rating_undefined)
vb@1004
   685
        add_opt_field(msg, "X-EncStatus", rating_to_string(rating));
vb@311
   686
vb@311
   687
    if (keylist) {
vb@311
   688
        char *_keylist = keylist_to_string(keylist);
vb@311
   689
        add_opt_field(msg, "X-KeyList", _keylist);
vb@311
   690
        free(_keylist);
vb@311
   691
    }
vb@311
   692
}
vb@311
   693
vb@1004
   694
static PEP_rating _rating(PEP_comm_type ct, PEP_rating rating)
vb@311
   695
{
vb@311
   696
    if (ct == PEP_ct_unknown)
vb@311
   697
        return PEP_rating_undefined;
krista@1427
   698
krista@1243
   699
    else if (ct == PEP_ct_key_not_found)
krista@1243
   700
        return PEP_rating_have_no_key;
krista@1427
   701
vb@311
   702
    else if (ct == PEP_ct_compromized)
vb@311
   703
        return PEP_rating_under_attack;
vb@311
   704
Edouard@442
   705
    else if (ct == PEP_ct_mistrusted)
Edouard@442
   706
        return PEP_rating_mistrust;
krista@1427
   707
vb@1004
   708
    if (rating == PEP_rating_unencrypted_for_some)
vb@486
   709
        return PEP_rating_unencrypted_for_some;
vb@486
   710
vb@486
   711
    if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel ||
vb@486
   712
            ct == PEP_ct_my_key_not_included) {
vb@1004
   713
        if (rating > PEP_rating_unencrypted_for_some)
vb@486
   714
            return PEP_rating_unencrypted_for_some;
vb@486
   715
        else
vb@486
   716
            return PEP_rating_unencrypted;
vb@486
   717
    }
vb@486
   718
vb@1004
   719
    if (rating == PEP_rating_unencrypted)
vb@486
   720
        return PEP_rating_unencrypted_for_some;
vb@486
   721
vb@486
   722
    if (ct >= PEP_ct_confirmed_enc_anon)
vb@311
   723
        return PEP_rating_trusted_and_anonymized;
vb@311
   724
vb@311
   725
    else if (ct >= PEP_ct_strong_encryption)
vb@311
   726
        return PEP_rating_trusted;
vb@311
   727
vb@311
   728
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@311
   729
        return PEP_rating_reliable;
vb@311
   730
vb@311
   731
    else
vb@311
   732
        return PEP_rating_unreliable;
vb@311
   733
}
vb@311
   734
vb@311
   735
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@311
   736
{
vb@311
   737
    assert(blob);
vb@311
   738
krista@853
   739
    if (blob == NULL || blob->filename == NULL)
vb@311
   740
        return false;
krista@1427
   741
roker@1559
   742
    char *ext = strrchr(blob->filename, '.');
vb@311
   743
    if (ext == NULL)
vb@311
   744
        return false;
vb@311
   745
vb@320
   746
    if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
vb@311
   747
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@311
   748
            strcmp(ext, ".asc") == 0)
vb@311
   749
            return true;
vb@311
   750
    }
vb@320
   751
    else if (strcmp(blob->mime_type, "text/plain") == 0) {
vb@311
   752
        if (strcmp(ext, ".asc") == 0)
vb@311
   753
            return true;
vb@311
   754
    }
vb@311
   755
vb@311
   756
    return false;
vb@311
   757
}
vb@311
   758
vb@311
   759
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@311
   760
{
vb@311
   761
    assert(blob);
vb@311
   762
    assert(blob->filename);
krista@853
   763
    if (blob == NULL || blob->filename == NULL)
krista@853
   764
        return false;
vb@311
   765
vb@311
   766
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@311
   767
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@311
   768
            strcmp(blob->filename + 11, ".asc") == 0)
vb@311
   769
            return true;
vb@311
   770
    }
vb@311
   771
vb@311
   772
    return false;
vb@311
   773
}
vb@311
   774
vb@311
   775
static char * without_double_ending(const char *filename)
vb@311
   776
{
vb@311
   777
    assert(filename);
krista@853
   778
    if (filename == NULL)
krista@853
   779
        return NULL;
krista@1427
   780
roker@862
   781
    char *ext = strrchr(filename, '.');
vb@311
   782
    if (ext == NULL)
vb@311
   783
        return NULL;
vb@311
   784
vb@469
   785
    char *result = strndup(filename, ext - filename);
vb@469
   786
    assert(result);
vb@469
   787
    return result;
vb@311
   788
}
vb@311
   789
vb@1004
   790
static PEP_rating decrypt_rating(PEP_STATUS status)
vb@311
   791
{
vb@311
   792
    switch (status) {
vb@311
   793
    case PEP_UNENCRYPTED:
vb@311
   794
    case PEP_VERIFIED:
vb@311
   795
    case PEP_VERIFY_NO_KEY:
vb@311
   796
    case PEP_VERIFIED_AND_TRUSTED:
vb@311
   797
        return PEP_rating_unencrypted;
vb@311
   798
vb@311
   799
    case PEP_DECRYPTED:
krista@1748
   800
    case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH:
vb@311
   801
        return PEP_rating_unreliable;
vb@311
   802
vb@311
   803
    case PEP_DECRYPTED_AND_VERIFIED:
vb@311
   804
        return PEP_rating_reliable;
vb@311
   805
vb@311
   806
    case PEP_DECRYPT_NO_KEY:
vb@311
   807
        return PEP_rating_have_no_key;
vb@311
   808
vb@311
   809
    case PEP_DECRYPT_WRONG_FORMAT:
vb@311
   810
    case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@311
   811
        return PEP_rating_cannot_decrypt;
vb@311
   812
vb@311
   813
    default:
vb@311
   814
        return PEP_rating_undefined;
vb@311
   815
    }
vb@311
   816
}
vb@311
   817
vb@1004
   818
static PEP_rating key_rating(PEP_SESSION session, const char *fpr)
vb@311
   819
{
vb@311
   820
vb@311
   821
    assert(session);
vb@311
   822
    assert(fpr);
krista@1427
   823
krista@853
   824
    if (session == NULL || fpr == NULL)
krista@853
   825
        return PEP_rating_undefined;
vb@311
   826
edouard@1632
   827
edouard@1632
   828
    PEP_comm_type bare_comm_type = PEP_ct_unknown;
krista@1615
   829
    PEP_STATUS status = get_key_rating(session, fpr, &bare_comm_type);
vb@311
   830
    if (status != PEP_STATUS_OK)
vb@311
   831
        return PEP_rating_undefined;
vb@311
   832
krista@1615
   833
    PEP_comm_type least_trust_type = PEP_ct_unknown;
edouard@1632
   834
    least_trust(session, fpr, &least_trust_type);
edouard@1632
   835
edouard@1632
   836
    if (least_trust_type == PEP_ct_unknown) {
edouard@1632
   837
        return _rating(bare_comm_type, PEP_rating_undefined);
edouard@1632
   838
    } else {
edouard@1632
   839
        return _rating(least_trust_type, PEP_rating_undefined);
edouard@1632
   840
    }
vb@311
   841
}
vb@311
   842
krista@1579
   843
static PEP_rating worst_rating(PEP_rating rating1, PEP_rating rating2) {
krista@1579
   844
    return ((rating1 < rating2) ? rating1 : rating2);
krista@1579
   845
}
krista@1579
   846
vb@1004
   847
static PEP_rating keylist_rating(PEP_SESSION session, stringlist_t *keylist)
vb@311
   848
{
krista@1610
   849
    PEP_rating rating = PEP_rating_undefined;
vb@311
   850
vb@311
   851
    assert(keylist && keylist->value);
vb@311
   852
    if (keylist == NULL || keylist->value == NULL)
vb@486
   853
        return PEP_rating_undefined;
vb@311
   854
vb@311
   855
    stringlist_t *_kl;
krista@1610
   856
    bool first = true;
vb@311
   857
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   858
        PEP_comm_type ct;
vb@311
   859
        PEP_STATUS status;
vb@311
   860
vb@1004
   861
        PEP_rating _rating_ = key_rating(session, _kl->value);
krista@1579
   862
         
vb@1004
   863
        if (_rating_ <= PEP_rating_mistrust)
vb@1004
   864
            return _rating_;
krista@1610
   865
            
krista@1610
   866
        if (first) {
krista@1610
   867
            rating = _rating_;
krista@1610
   868
            first = false;
krista@1610
   869
        }
krista@1610
   870
        else if (rating == PEP_rating_undefined)
krista@1579
   871
            rating = worst_rating(rating, _rating_);
vb@486
   872
vb@1004
   873
        if (_rating_ >= PEP_rating_reliable) {
vb@311
   874
            status = least_trust(session, _kl->value, &ct);
vb@311
   875
            if (status != PEP_STATUS_OK)
vb@311
   876
                return PEP_rating_undefined;
edouard@1156
   877
            if (ct == PEP_ct_unknown){
krista@1615
   878
                /* per edouard, we reduce reliable+ ratings to reliable because
krista@1615
   879
                   ct unknown */
edouard@1156
   880
                if (rating >= PEP_rating_reliable){
krista@1615
   881
                    rating = PEP_rating_reliable; 
edouard@1156
   882
                }
edouard@1156
   883
            }
edouard@1156
   884
            else{
krista@1579
   885
                rating = worst_rating(rating, _rating(ct, rating));
edouard@1156
   886
            }
vb@486
   887
        }
vb@1004
   888
        else if (_rating_ == PEP_rating_unencrypted) {
vb@1004
   889
            if (rating > PEP_rating_unencrypted_for_some)
krista@1579
   890
                rating = worst_rating(rating, PEP_rating_unencrypted_for_some);
vb@311
   891
        }
vb@311
   892
    }
vb@311
   893
vb@1004
   894
    return rating;
vb@311
   895
}
vb@311
   896
vb@311
   897
static PEP_comm_type _get_comm_type(
vb@311
   898
    PEP_SESSION session,
vb@311
   899
    PEP_comm_type max_comm_type,
vb@311
   900
    pEp_identity *ident
vb@311
   901
    )
vb@311
   902
{
vb@311
   903
    PEP_STATUS status = update_identity(session, ident);
vb@311
   904
vb@311
   905
    if (max_comm_type == PEP_ct_compromized)
vb@311
   906
        return PEP_ct_compromized;
krista@1427
   907
Edouard@510
   908
    if (max_comm_type == PEP_ct_mistrusted)
Edouard@510
   909
        return PEP_ct_mistrusted;
vb@311
   910
vb@311
   911
    if (status == PEP_STATUS_OK) {
vb@311
   912
        if (ident->comm_type == PEP_ct_compromized)
vb@311
   913
            return PEP_ct_compromized;
Edouard@510
   914
        else if (ident->comm_type == PEP_ct_mistrusted)
Edouard@510
   915
            return PEP_ct_mistrusted;
vb@311
   916
        else
vb@951
   917
            return _MIN(max_comm_type, ident->comm_type);
vb@311
   918
    }
vb@311
   919
    else {
vb@311
   920
        return PEP_ct_unknown;
vb@311
   921
    }
vb@311
   922
}
vb@311
   923
vb@731
   924
static void free_bl_entry(bloblist_t *bl)
vb@731
   925
{
vb@731
   926
    if (bl) {
vb@731
   927
        free(bl->value);
vb@731
   928
        free(bl->mime_type);
vb@731
   929
        free(bl->filename);
vb@731
   930
        free(bl);
vb@731
   931
    }
vb@731
   932
}
vb@731
   933
vb@731
   934
static bool is_key(const bloblist_t *bl)
vb@731
   935
{
Edouard@728
   936
    return (// workaround for Apple Mail bugs
Edouard@728
   937
            (is_mime_type(bl, "application/x-apple-msg-attachment") &&
Edouard@728
   938
             is_fileending(bl, ".asc")) ||
Edouard@728
   939
            // as binary, by file name
Edouard@728
   940
            ((bl->mime_type == NULL ||
Edouard@728
   941
              is_mime_type(bl, "application/octet-stream")) &&
Edouard@728
   942
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
   943
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))) ||
krista@1427
   944
            // explicit mime type
Edouard@728
   945
            is_mime_type(bl, "application/pgp-keys") ||
Edouard@728
   946
            // as text, by file name
Edouard@728
   947
            (is_mime_type(bl, "text/plain") &&
Edouard@728
   948
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
   949
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc")))
Edouard@728
   950
           );
vb@731
   951
}
vb@731
   952
vb@731
   953
static void remove_attached_keys(message *msg)
vb@731
   954
{
vb@731
   955
    if (msg) {
vb@731
   956
        bloblist_t *last = NULL;
vb@731
   957
        for (bloblist_t *bl = msg->attachments; bl && bl->value; ) {
vb@731
   958
            bloblist_t *next = bl->next;
vb@731
   959
vb@731
   960
            if (is_key(bl)) {
vb@731
   961
                if (last) {
vb@731
   962
                    last->next = next;
vb@731
   963
                }
vb@731
   964
                else {
vb@731
   965
                    msg->attachments = next;
vb@731
   966
                }
vb@731
   967
                free_bl_entry(bl);
vb@731
   968
            }
vb@731
   969
            else {
vb@731
   970
                last = bl;
vb@731
   971
            }
vb@731
   972
            bl = next;
vb@731
   973
        }
vb@731
   974
    }
vb@731
   975
}
vb@731
   976
Edouard@734
   977
bool import_attached_keys(
krista@1427
   978
        PEP_SESSION session,
Edouard@728
   979
        const message *msg,
Edouard@728
   980
        identity_list **private_idents
Edouard@728
   981
    )
vb@311
   982
{
vb@311
   983
    assert(session);
vb@311
   984
    assert(msg);
krista@1427
   985
krista@853
   986
    if (session == NULL || msg == NULL)
krista@853
   987
        return false;
vb@311
   988
vb@731
   989
    bool remove = false;
vb@731
   990
vb@908
   991
    int i = 0;
roker@1559
   992
    for (bloblist_t *bl = msg->attachments; i < MAX_KEYS_TO_IMPORT && bl && bl->value;
krista@1427
   993
            bl = bl->next, i++)
Edouard@747
   994
    {
vb@908
   995
        if (bl && bl->value && bl->size && bl->size < MAX_KEY_SIZE
krista@1427
   996
                && is_key(bl))
Edouard@728
   997
        {
Edouard@728
   998
            import_key(session, bl->value, bl->size, private_idents);
vb@731
   999
            remove = true;
vb@311
  1000
        }
vb@311
  1001
    }
vb@731
  1002
    return remove;
vb@311
  1003
}
vb@311
  1004
Edouard@694
  1005
Edouard@694
  1006
PEP_STATUS _attach_key(PEP_SESSION session, const char* fpr, message *msg)
vb@311
  1007
{
roker@1559
  1008
    char *keydata = NULL;
vb@311
  1009
    size_t size;
vb@311
  1010
Edouard@694
  1011
    PEP_STATUS status = export_key(session, fpr, &keydata, &size);
Edouard@694
  1012
    assert(status == PEP_STATUS_OK);
Edouard@694
  1013
    if (status != PEP_STATUS_OK)
Edouard@694
  1014
        return status;
Edouard@694
  1015
    assert(size);
krista@1427
  1016
roker@1559
  1017
     bloblist_t *bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
Edouard@694
  1018
                      "pEpkey.asc");
krista@1427
  1019
Edouard@694
  1020
    if (msg->attachments == NULL && bl)
Edouard@694
  1021
        msg->attachments = bl;
Edouard@694
  1022
Edouard@694
  1023
    return PEP_STATUS_OK;
Edouard@694
  1024
}
Edouard@694
  1025
Edouard@694
  1026
#define ONE_WEEK (7*24*3600)
Edouard@694
  1027
Edouard@694
  1028
void attach_own_key(PEP_SESSION session, message *msg)
Edouard@694
  1029
{
vb@311
  1030
    assert(session);
vb@311
  1031
    assert(msg);
krista@1427
  1032
Edouard@558
  1033
    if (msg->dir == PEP_dir_incoming)
Edouard@558
  1034
        return;
Edouard@558
  1035
vb@311
  1036
    assert(msg->from && msg->from->fpr);
vb@311
  1037
    if (msg->from == NULL || msg->from->fpr == NULL)
vb@311
  1038
        return;
vb@311
  1039
Edouard@694
  1040
    if(_attach_key(session, msg->from->fpr, msg) != PEP_STATUS_OK)
vb@311
  1041
        return;
krista@1427
  1042
Edouard@694
  1043
    char *revoked_fpr = NULL;
Edouard@694
  1044
    uint64_t revocation_date = 0;
krista@1427
  1045
Edouard@694
  1046
    if(get_revoked(session, msg->from->fpr,
Edouard@694
  1047
                   &revoked_fpr, &revocation_date) == PEP_STATUS_OK &&
Edouard@694
  1048
       revoked_fpr != NULL)
Edouard@694
  1049
    {
Edouard@694
  1050
        time_t now = time(NULL);
krista@1427
  1051
Edouard@694
  1052
        if (now < (time_t)revocation_date + ONE_WEEK)
Edouard@694
  1053
        {
Edouard@694
  1054
            _attach_key(session, revoked_fpr, msg);
Edouard@694
  1055
        }
Edouard@694
  1056
    }
Edouard@694
  1057
    free(revoked_fpr);
vb@311
  1058
}
vb@311
  1059
vb@311
  1060
PEP_cryptotech determine_encryption_format(message *msg)
vb@311
  1061
{
vb@311
  1062
    assert(msg);
krista@1427
  1063
vb@311
  1064
    if (is_PGP_message_text(msg->longmsg)) {
vb@311
  1065
        msg->enc_format = PEP_enc_pieces;
vb@311
  1066
        return PEP_crypt_OpenPGP;
vb@311
  1067
    }
vb@311
  1068
    else if (msg->attachments && msg->attachments->next &&
vb@311
  1069
            is_mime_type(msg->attachments, "application/pgp-encrypted") &&
vb@311
  1070
            is_PGP_message_text(msg->attachments->next->value)
vb@311
  1071
        ) {
vb@311
  1072
        msg->enc_format = PEP_enc_PGP_MIME;
vb@311
  1073
        return PEP_crypt_OpenPGP;
vb@311
  1074
    }
edouard@1557
  1075
    else if (msg->attachments && msg->attachments->next &&
edouard@1557
  1076
            is_mime_type(msg->attachments->next, "application/pgp-encrypted") &&
edouard@1557
  1077
            is_PGP_message_text(msg->attachments->value)
edouard@1557
  1078
        ) {
edouard@1557
  1079
        msg->enc_format = PEP_enc_PGP_MIME_Outlook1;
edouard@1557
  1080
        return PEP_crypt_OpenPGP;
edouard@1557
  1081
    }
vb@311
  1082
    else {
vb@311
  1083
        msg->enc_format = PEP_enc_none;
vb@311
  1084
        return PEP_crypt_none;
vb@311
  1085
    }
vb@311
  1086
}
vb@311
  1087
vb@48
  1088
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
  1089
        PEP_SESSION session,
vb@113
  1090
        message *src,
vb@37
  1091
        stringlist_t * extra,
vb@38
  1092
        message **dst,
vb@939
  1093
        PEP_enc_format enc_format,
vb@939
  1094
        PEP_encrypt_flags_t flags
vb@37
  1095
    )
vb@37
  1096
{
vb@37
  1097
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
  1098
    message * msg = NULL;
vb@63
  1099
    stringlist_t * keys = NULL;
vb@37
  1100
vb@37
  1101
    assert(session);
vb@37
  1102
    assert(src);
vb@37
  1103
    assert(dst);
Edouard@558
  1104
    assert(enc_format != PEP_enc_none);
vb@81
  1105
Edouard@558
  1106
    if (!(session && src && dst && enc_format != PEP_enc_none))
roker@1853
  1107
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
  1108
Edouard@550
  1109
    if (src->dir == PEP_dir_incoming)
roker@1853
  1110
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
krista@1427
  1111
vb@259
  1112
    determine_encryption_format(src);
vb@260
  1113
    if (src->enc_format != PEP_enc_none)
roker@1853
  1114
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@259
  1115
vb@37
  1116
    *dst = NULL;
vb@67
  1117
vb@236
  1118
    status = myself(session, src->from);
vb@236
  1119
    if (status != PEP_STATUS_OK)
roker@1734
  1120
        GOTO(pep_error);
Edouard@558
  1121
vb@80
  1122
    keys = new_stringlist(src->from->fpr);
vb@63
  1123
    if (keys == NULL)
vb@63
  1124
        goto enomem;
vb@37
  1125
vb@39
  1126
    stringlist_t *_k = keys;
vb@39
  1127
vb@39
  1128
    if (extra) {
vb@39
  1129
        _k = stringlist_append(_k, extra);
vb@63
  1130
        if (_k == NULL)
vb@63
  1131
            goto enomem;
vb@37
  1132
    }
vb@39
  1133
vb@299
  1134
    bool dest_keys_found = true;
Edouard@510
  1135
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@299
  1136
vb@37
  1137
    identity_list * _il;
krista@1427
  1138
Edouard@545
  1139
    if ((_il = src->bcc) && _il->ident)
Edouard@545
  1140
    {
Edouard@545
  1141
        // BCC limited support:
Edouard@545
  1142
        //     - App splits mails with BCC in multiple mails.
Edouard@545
  1143
        //     - Each email is encrypted separately
krista@1427
  1144
Edouard@789
  1145
        if(_il->next || (src->to && src->to->ident) || (src->cc && src->cc->ident))
Edouard@545
  1146
        {
Edouard@545
  1147
            // Only one Bcc with no other recipient allowed for now
Edouard@545
  1148
            return PEP_ILLEGAL_VALUE;
Edouard@545
  1149
        }
krista@1427
  1150
vb@299
  1151
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
  1152
        if (_status != PEP_STATUS_OK) {
vb@299
  1153
            status = _status;
roker@1734
  1154
            GOTO(pep_error);
vb@299
  1155
        }
krista@1427
  1156
vb@299
  1157
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@39
  1158
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
  1159
            if (_k == NULL)
vb@63
  1160
                goto enomem;
Edouard@510
  1161
            max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@510
  1162
                                           _il->ident);
vb@37
  1163
        }
vb@299
  1164
        else {
vb@299
  1165
            dest_keys_found = false;
vb@37
  1166
            status = PEP_KEY_NOT_FOUND;
krista@1427
  1167
        }
vb@299
  1168
    }
Edouard@545
  1169
    else
Edouard@545
  1170
    {
Edouard@545
  1171
        for (_il = src->to; _il && _il->ident; _il = _il->next) {
Edouard@545
  1172
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1173
            if (_status != PEP_STATUS_OK) {
Edouard@545
  1174
                status = _status;
roker@1734
  1175
                GOTO(pep_error);
Edouard@545
  1176
            }
vb@299
  1177
Edouard@545
  1178
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1179
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1180
                if (_k == NULL)
Edouard@545
  1181
                    goto enomem;
Edouard@545
  1182
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1183
                                               _il->ident);
Edouard@545
  1184
            }
Edouard@545
  1185
            else {
Edouard@545
  1186
                dest_keys_found = false;
Edouard@545
  1187
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1188
            }
vb@299
  1189
        }
vb@299
  1190
Edouard@545
  1191
        for (_il = src->cc; _il && _il->ident; _il = _il->next) {
Edouard@545
  1192
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1193
            if (_status != PEP_STATUS_OK)
Edouard@545
  1194
            {
Edouard@545
  1195
                status = _status;
roker@1734
  1196
                GOTO(pep_error);
Edouard@545
  1197
            }
Edouard@545
  1198
Edouard@545
  1199
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1200
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1201
                if (_k == NULL)
Edouard@545
  1202
                    goto enomem;
Edouard@545
  1203
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1204
                                               _il->ident);
Edouard@545
  1205
            }
Edouard@545
  1206
            else {
Edouard@545
  1207
                dest_keys_found = false;
Edouard@545
  1208
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1209
            }
vb@299
  1210
        }
vb@37
  1211
    }
krista@1427
  1212
Edouard@510
  1213
    if (!dest_keys_found ||
krista@1640
  1214
        stringlist_length(keys)  == 0 ||
Edouard@510
  1215
        _rating(max_comm_type,
Edouard@510
  1216
                PEP_rating_undefined) < PEP_rating_reliable)
Edouard@510
  1217
    {
vb@301
  1218
        free_stringlist(keys);
krista@1640
  1219
        if (!session->passive_mode && !(flags & PEP_encrypt_flag_force_no_attached_key))
vb@465
  1220
            attach_own_key(session, src);
roker@1853
  1221
        return ADD_TO_LOG(PEP_UNENCRYPTED);
vb@301
  1222
    }
vb@301
  1223
    else {
vb@299
  1224
        msg = clone_to_empty_message(src);
vb@299
  1225
        if (msg == NULL)
vb@299
  1226
            goto enomem;
vb@299
  1227
krista@1640
  1228
        if (!(flags & PEP_encrypt_flag_force_no_attached_key))
krista@1640
  1229
            attach_own_key(session, src);
Edouard@428
  1230
vb@81
  1231
        switch (enc_format) {
vb@260
  1232
        case PEP_enc_PGP_MIME:
vb@336
  1233
        case PEP_enc_PEP: // BUG: should be implemented extra
krista@1639
  1234
            status = encrypt_PGP_MIME(session, src, keys, msg, flags);
vb@260
  1235
            break;
vb@62
  1236
vb@62
  1237
        case PEP_enc_pieces:
krista@1639
  1238
            status = encrypt_PGP_in_pieces(session, src, keys, msg, flags);
vb@38
  1239
            break;
vb@38
  1240
vb@336
  1241
        /* case PEP_enc_PEP:
vb@81
  1242
            // TODO: implement
vb@336
  1243
            NOT_IMPLEMENTED */
vb@81
  1244
vb@38
  1245
        default:
vb@38
  1246
            assert(0);
vb@63
  1247
            status = PEP_ILLEGAL_VALUE;
roker@1734
  1248
            GOTO(pep_error);
vb@37
  1249
        }
krista@1427
  1250
Edouard@392
  1251
        if (status == PEP_OUT_OF_MEMORY)
Edouard@392
  1252
            goto enomem;
krista@1427
  1253
vb@480
  1254
        if (status != PEP_STATUS_OK)
roker@1734
  1255
            GOTO(pep_error);
vb@37
  1256
    }
vb@37
  1257
vb@37
  1258
    free_stringlist(keys);
vb@63
  1259
vb@469
  1260
    if (msg && msg->shortmsg == NULL) {
vb@64
  1261
        msg->shortmsg = strdup("pEp");
vb@469
  1262
        assert(msg->shortmsg);
krista@853
  1263
        if (msg->shortmsg == NULL)
krista@853
  1264
            goto enomem;
vb@469
  1265
    }
vb@64
  1266
edouard@1542
  1267
    if (msg) {
vb@311
  1268
        decorate_message(msg, PEP_rating_undefined, NULL);
edouard@1542
  1269
        if (src->id) {
edouard@1542
  1270
            msg->id = strdup(src->id);
edouard@1542
  1271
            assert(msg->id);
edouard@1542
  1272
            if (msg->id == NULL)
edouard@1542
  1273
                goto enomem;
edouard@1542
  1274
        }
edouard@1542
  1275
    }
vb@311
  1276
vb@63
  1277
    *dst = msg;
roker@1853
  1278
    return ADD_TO_LOG(status);
vb@63
  1279
vb@63
  1280
enomem:
vb@63
  1281
    status = PEP_OUT_OF_MEMORY;
vb@63
  1282
vb@63
  1283
pep_error:
vb@63
  1284
    free_stringlist(keys);
vb@63
  1285
    free_message(msg);
vb@63
  1286
roker@1853
  1287
    return ADD_TO_LOG(status);
vb@37
  1288
}
vb@37
  1289
krista@995
  1290
DYNAMIC_API PEP_STATUS encrypt_message_for_self(
krista@992
  1291
        PEP_SESSION session,
krista@992
  1292
        pEp_identity* target_id,
krista@992
  1293
        message *src,
krista@992
  1294
        message **dst,
markus@1633
  1295
        PEP_enc_format enc_format,
markus@1633
  1296
        PEP_encrypt_flags_t flags
krista@992
  1297
    )
krista@992
  1298
{
krista@992
  1299
    PEP_STATUS status = PEP_STATUS_OK;
krista@992
  1300
    message * msg = NULL;
krista@992
  1301
    stringlist_t * keys = NULL;
krista@992
  1302
krista@992
  1303
    assert(session);
krista@992
  1304
    assert(src);
krista@992
  1305
    assert(dst);
krista@992
  1306
    assert(enc_format != PEP_enc_none);
krista@992
  1307
krista@992
  1308
    if (!(session && src && dst && enc_format != PEP_enc_none))
roker@1853
  1309
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
krista@992
  1310
krista@992
  1311
    if (src->dir == PEP_dir_incoming)
roker@1853
  1312
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
krista@1427
  1313
krista@992
  1314
    determine_encryption_format(src);
krista@992
  1315
    if (src->enc_format != PEP_enc_none)
roker@1853
  1316
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
krista@992
  1317
krista@995
  1318
    status = myself(session, target_id);
krista@995
  1319
    if (status != PEP_STATUS_OK)
roker@1734
  1320
        GOTO(pep_error);
krista@995
  1321
krista@992
  1322
    *dst = NULL;
krista@992
  1323
krista@1427
  1324
krista@992
  1325
    PEP_STATUS _status = update_identity(session, target_id);
krista@992
  1326
    if (_status != PEP_STATUS_OK) {
krista@992
  1327
        status = _status;
krista@992
  1328
        goto pep_error;
krista@992
  1329
    }
krista@992
  1330
krista@994
  1331
    char* target_fpr = target_id->fpr;
krista@994
  1332
    if (!target_fpr)
krista@994
  1333
        return PEP_KEY_NOT_FOUND; // FIXME: Error condition
krista@1685
  1334
 
krista@994
  1335
    keys = new_stringlist(target_fpr);
krista@1685
  1336
    
krista@1640
  1337
    /* KG: did we ever do this??? */
krista@1640
  1338
    if (!(flags & PEP_encrypt_flag_force_no_attached_key))
krista@1685
  1339
        _attach_key(session, target_fpr, src);
krista@1427
  1340
krista@992
  1341
    msg = clone_to_empty_message(src);
krista@992
  1342
    if (msg == NULL)
krista@992
  1343
        goto enomem;
krista@992
  1344
krista@992
  1345
    switch (enc_format) {
krista@992
  1346
        case PEP_enc_PGP_MIME:
krista@992
  1347
        case PEP_enc_PEP: // BUG: should be implemented extra
krista@1639
  1348
            status = encrypt_PGP_MIME(session, src, keys, msg, flags);
krista@992
  1349
            break;
krista@992
  1350
krista@992
  1351
        case PEP_enc_pieces:
krista@1639
  1352
            status = encrypt_PGP_in_pieces(session, src, keys, msg, flags);
krista@992
  1353
            break;
krista@992
  1354
krista@992
  1355
        /* case PEP_enc_PEP:
krista@1641
  1356
            NOT_IMPLEMENTED */
krista@992
  1357
            // TODO: implement
krista@992
  1358
krista@992
  1359
        default:
krista@992
  1360
            assert(0);
krista@992
  1361
            status = PEP_ILLEGAL_VALUE;
krista@992
  1362
            goto pep_error;
krista@992
  1363
    }
krista@1427
  1364
krista@992
  1365
    if (status == PEP_OUT_OF_MEMORY)
krista@992
  1366
        goto enomem;
krista@1427
  1367
krista@992
  1368
    if (status != PEP_STATUS_OK)
krista@992
  1369
        goto pep_error;
krista@992
  1370
krista@994
  1371
     if (msg && msg->shortmsg == NULL) {
krista@994
  1372
         msg->shortmsg = strdup("pEp");
krista@994
  1373
         assert(msg->shortmsg);
krista@994
  1374
         if (msg->shortmsg == NULL)
krista@994
  1375
             goto enomem;
krista@994
  1376
     }
krista@992
  1377
edouard@1542
  1378
     if (msg) {
edouard@1542
  1379
         if (src->id) {
edouard@1542
  1380
             msg->id = strdup(src->id);
edouard@1542
  1381
             assert(msg->id);
edouard@1542
  1382
             if (msg->id == NULL)
edouard@1542
  1383
                 goto enomem;
edouard@1542
  1384
         }
edouard@1542
  1385
     }
edouard@1542
  1386
krista@992
  1387
    *dst = msg;
krista@992
  1388
    return status;
krista@992
  1389
krista@992
  1390
enomem:
krista@992
  1391
    status = PEP_OUT_OF_MEMORY;
krista@992
  1392
krista@992
  1393
pep_error:
krista@992
  1394
    free_stringlist(keys);
krista@992
  1395
    free_message(msg);
krista@992
  1396
roker@1853
  1397
    return ADD_TO_LOG(status);
krista@992
  1398
}
krista@992
  1399
vb@781
  1400
static bool is_a_pEpmessage(const message *msg)
vb@781
  1401
{
vb@781
  1402
    for (stringpair_list_t *i = msg->opt_fields; i && i->value ; i=i->next) {
vb@781
  1403
        if (strcasecmp(i->value->key, "X-pEp-Version") == 0)
vb@781
  1404
            return true;
vb@781
  1405
    }
vb@781
  1406
    return false;
vb@781
  1407
}
vb@781
  1408
vb@781
  1409
// update comm_type to pEp_ct_pEp if needed
vb@781
  1410
Edouard@858
  1411
static PEP_STATUS _update_identity_for_incoming_message(
vb@781
  1412
        PEP_SESSION session,
vb@781
  1413
        const message *src
vb@781
  1414
    )
vb@781
  1415
{
Edouard@858
  1416
    PEP_STATUS status;
Edouard@858
  1417
    if (src->from && src->from->address) {
Edouard@858
  1418
        status = update_identity(session, src->from);
krista@1192
  1419
        if (status == PEP_STATUS_OK
Edouard@858
  1420
                && is_a_pEpmessage(src)
vb@781
  1421
                && src->from->comm_type >= PEP_ct_OpenPGP_unconfirmed
vb@781
  1422
                && src->from->comm_type != PEP_ct_pEp_unconfirmed
vb@781
  1423
                && src->from->comm_type != PEP_ct_pEp)
vb@781
  1424
        {
vb@781
  1425
            src->from->comm_type |= PEP_ct_pEp_unconfirmed;
krista@1823
  1426
            status = set_identity(session, src->from);
vb@781
  1427
        }
Edouard@858
  1428
        return status;
vb@781
  1429
    }
Edouard@858
  1430
    return PEP_ILLEGAL_VALUE;
vb@781
  1431
}
vb@781
  1432
krista@1397
  1433
krista@1397
  1434
PEP_STATUS _get_detached_signature(message* msg, bloblist_t** signature_blob) {
krista@1397
  1435
    bloblist_t* attach_curr = msg->attachments;
krista@1427
  1436
krista@1427
  1437
    *signature_blob = NULL;
krista@1427
  1438
krista@1397
  1439
    while (attach_curr) {
krista@1427
  1440
        if (strcasecmp(attach_curr->mime_type, "application/pgp-signature") == 0) {
krista@1397
  1441
            *signature_blob = attach_curr;
krista@1397
  1442
            break;
krista@1397
  1443
        }
krista@1397
  1444
        attach_curr = attach_curr->next;
krista@1397
  1445
    }
krista@1427
  1446
krista@1427
  1447
    return PEP_STATUS_OK;
krista@1427
  1448
}
krista@1427
  1449
krista@1427
  1450
PEP_STATUS _get_signed_text(const char* ptext, const size_t psize,
krista@1427
  1451
                            char** stext, size_t* ssize) {
krista@1427
  1452
krista@1427
  1453
    char* signed_boundary = NULL;
krista@1427
  1454
    char* signpost = strstr(ptext, "Content-Type: multipart/signed");
krista@1427
  1455
krista@1427
  1456
    *ssize = 0;
krista@1427
  1457
    *stext = NULL;
krista@1427
  1458
krista@1427
  1459
    if (!signpost)
krista@1427
  1460
        return PEP_UNKNOWN_ERROR;
krista@1427
  1461
krista@1427
  1462
    char* curr_line = signpost;
krista@1427
  1463
//    const char* end_text = ptext + psize;
krista@1755
  1464
    const char* boundary_key = "boundary=";
krista@1755
  1465
    const size_t BOUNDARY_KEY_SIZE = 9;
krista@1427
  1466
krista@1427
  1467
    char* start_boundary = strstr(curr_line, boundary_key);
krista@1427
  1468
    if (!start_boundary)
krista@1427
  1469
        return PEP_UNKNOWN_ERROR;
krista@1427
  1470
krista@1427
  1471
    start_boundary += BOUNDARY_KEY_SIZE;
krista@1427
  1472
krista@1755
  1473
    bool quoted = (*start_boundary == '"');
krista@1755
  1474
krista@1755
  1475
    if (quoted)
krista@1755
  1476
        start_boundary++;
krista@1755
  1477
        
krista@1755
  1478
    char* end_boundary = (quoted ? strstr(start_boundary, "\"") : strstr(start_boundary, ";")); // FIXME: third possiblity is CRLF, or?
krista@1427
  1479
krista@1427
  1480
    if (!end_boundary)
krista@1427
  1481
        return PEP_UNKNOWN_ERROR;
krista@1427
  1482
krista@1755
  1483
    // Add space for the "--"
krista@1427
  1484
    size_t boundary_strlen = (end_boundary - start_boundary) + 2;
krista@1427
  1485
krista@1427
  1486
    signed_boundary = calloc(1, boundary_strlen + 1);
krista@1427
  1487
    strlcpy(signed_boundary, "--", boundary_strlen + 1);
krista@1427
  1488
    strlcat(signed_boundary, start_boundary, boundary_strlen + 1);
krista@1427
  1489
krista@1427
  1490
    start_boundary = strstr(end_boundary, signed_boundary);
krista@1427
  1491
krista@1427
  1492
    if (!start_boundary)
krista@1427
  1493
        return PEP_UNKNOWN_ERROR;
krista@1427
  1494
krista@1427
  1495
    start_boundary += boundary_strlen;
krista@1427
  1496
krista@1756
  1497
    if (*start_boundary == '\r') {
krista@1756
  1498
        if (*(start_boundary + 1) == '\n')
krista@1756
  1499
            start_boundary += 2;
krista@1756
  1500
    }
krista@1756
  1501
    else if (*start_boundary == '\n')
krista@1427
  1502
        start_boundary++;
krista@1427
  1503
krista@1427
  1504
    end_boundary = strstr(start_boundary + boundary_strlen, signed_boundary);
krista@1427
  1505
krista@1427
  1506
    if (!end_boundary)
krista@1427
  1507
        return PEP_UNKNOWN_ERROR;
krista@1427
  1508
edouard@1809
  1509
    // See RFC3156 section 5...
edouard@1809
  1510
    end_boundary--; 
edouard@1809
  1511
    if (*(end_boundary - 1) == '\r')
edouard@1809
  1512
        end_boundary--; 
krista@1427
  1513
krista@1427
  1514
    *ssize = end_boundary - start_boundary;
krista@1427
  1515
    *stext = start_boundary;
krista@1427
  1516
    free(signed_boundary);
krista@1427
  1517
krista@1397
  1518
    return PEP_STATUS_OK;
krista@1397
  1519
}
krista@1397
  1520
krista@1484
  1521
PEP_STATUS combine_keylists(PEP_SESSION session, stringlist_t** verify_in, 
krista@1484
  1522
                            stringlist_t** keylist_in_out, 
krista@1484
  1523
                            pEp_identity* from) {
krista@1484
  1524
    
krista@1484
  1525
    if (!verify_in || !(*verify_in)) // this isn't really a problem.
krista@1484
  1526
        return PEP_STATUS_OK;
krista@1484
  1527
    
krista@1484
  1528
    stringlist_t* orig_verify = *verify_in;
krista@1484
  1529
    
roker@1559
  1530
    stringlist_t* verify_curr = NULL;
roker@1559
  1531
    stringlist_t* from_keys = NULL;
krista@1484
  1532
    
krista@1484
  1533
    /* FIXME: what to do if head needs to be null */
krista@1484
  1534
    PEP_STATUS status = find_keys(session, from->address, &from_keys);
krista@1484
  1535
    
krista@1484
  1536
    stringlist_t* from_fpr_node = NULL;
krista@1484
  1537
    stringlist_t* from_curr;
krista@1484
  1538
    
krista@1484
  1539
    for (from_curr = from_keys; from_curr; from_curr = from_curr->next) {
krista@1484
  1540
        for (verify_curr = orig_verify; verify_curr; verify_curr = verify_curr->next) {
krista@1484
  1541
            if (from_curr->value && verify_curr->value &&
edouard@1573
  1542
                _same_fpr(from_curr->value, strlen(from_curr->value),
edouard@1573
  1543
                          verify_curr->value, strlen(verify_curr->value))) {
krista@1484
  1544
                from_fpr_node = from_curr;
krista@1484
  1545
                break;
krista@1484
  1546
            }
krista@1484
  1547
        }
krista@1484
  1548
    }
krista@1484
  1549
    
krista@1484
  1550
    if (!from_fpr_node) {
krista@1484
  1551
        status = PEP_KEY_NOT_FOUND;
krista@1484
  1552
        goto free;
krista@1484
  1553
    }
krista@1484
  1554
krista@1484
  1555
    verify_curr = orig_verify;
krista@1484
  1556
    
krista@1484
  1557
    /* put "from" signer at the beginning of the list */
edouard@1573
  1558
    if (!_same_fpr(orig_verify->value, strlen(orig_verify->value),
edouard@1573
  1559
                   from_fpr_node->value, strlen(from_fpr_node->value))) {
krista@1484
  1560
        orig_verify = stringlist_delete(orig_verify, from_fpr_node->value);
krista@1484
  1561
        verify_curr = new_stringlist(from_fpr_node->value);
krista@1484
  1562
        verify_curr->next = orig_verify;
krista@1484
  1563
    }
krista@1484
  1564
krista@1484
  1565
    /* append keylist to signers */
krista@1484
  1566
    if (keylist_in_out && *keylist_in_out && (*keylist_in_out)->value) {
krista@1484
  1567
        stringlist_t** tail_pp = &verify_curr->next;
krista@1484
  1568
        
krista@1484
  1569
        while (*tail_pp) {
krista@1484
  1570
            tail_pp = &((*tail_pp)->next);
krista@1484
  1571
        }
krista@1615
  1572
        stringlist_t* second_list = *keylist_in_out;
krista@1615
  1573
        if (second_list) {
krista@1615
  1574
            char* listhead_val = second_list->value;
krista@1615
  1575
            if (!listhead_val || listhead_val[0] == '\0') {
krista@1615
  1576
                /* remove head, basically. This can happen when,
krista@1615
  1577
                   for example, the signature is detached and
krista@1615
  1578
                   verification is not seen directly after
krista@1615
  1579
                   decryption, so no signer is presumed in
krista@1615
  1580
                   the first construction of the keylist */
krista@1615
  1581
                *keylist_in_out = (*keylist_in_out)->next;
krista@1615
  1582
                second_list->next = NULL;
krista@1615
  1583
                free_stringlist(second_list);
krista@1615
  1584
            }
krista@1615
  1585
        }
krista@1484
  1586
        *tail_pp = *keylist_in_out;
krista@1484
  1587
    }
krista@1484
  1588
    
krista@1484
  1589
    *keylist_in_out = verify_curr;
krista@1484
  1590
    
krista@1484
  1591
    status = PEP_STATUS_OK;
krista@1484
  1592
    
krista@1484
  1593
free:
krista@1484
  1594
    free_stringlist(from_keys);
roker@1559
  1595
    return status;
krista@1484
  1596
}
krista@1484
  1597
edouard@1815
  1598
PEP_STATUS amend_rating_according_to_sender_and_recipients(
edouard@1815
  1599
    PEP_SESSION session,
edouard@1815
  1600
    PEP_rating *rating,
edouard@1815
  1601
    pEp_identity *sender,
edouard@1815
  1602
    stringlist_t *recipients) {
edouard@1815
  1603
    
edouard@1815
  1604
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1815
  1605
edouard@1815
  1606
    if (*rating > PEP_rating_mistrust) {
edouard@1815
  1607
        PEP_rating kl_rating = PEP_rating_undefined;
edouard@1815
  1608
edouard@1815
  1609
        if (recipients)
edouard@1815
  1610
            kl_rating = keylist_rating(session, recipients);
edouard@1815
  1611
edouard@1815
  1612
        if (kl_rating <= PEP_rating_mistrust) {
edouard@1815
  1613
            *rating = kl_rating;
edouard@1815
  1614
        }
edouard@1815
  1615
        else if (*rating >= PEP_rating_reliable &&
edouard@1815
  1616
                 kl_rating < PEP_rating_reliable) {
edouard@1815
  1617
            *rating = PEP_rating_unreliable;
edouard@1815
  1618
        }
edouard@1815
  1619
        else if (*rating >= PEP_rating_reliable &&
edouard@1815
  1620
                 kl_rating >= PEP_rating_reliable) {
edouard@1815
  1621
            if (!(sender && sender->user_id && sender->user_id[0])) {
edouard@1815
  1622
                *rating = PEP_rating_unreliable;
edouard@1815
  1623
            }
edouard@1815
  1624
            else {
edouard@1815
  1625
                char *fpr = recipients->value;
edouard@1815
  1626
                pEp_identity *_sender = new_identity(sender->address, fpr,
edouard@1815
  1627
                                                   sender->user_id, sender->username);
edouard@1815
  1628
                if (_sender == NULL)
edouard@1815
  1629
                    return PEP_OUT_OF_MEMORY;
edouard@1815
  1630
                status = get_trust(session, _sender);
edouard@1815
  1631
                if (_sender->comm_type != PEP_ct_unknown) {
edouard@1815
  1632
                    *rating = worst_rating(_rating(_sender->comm_type, PEP_rating_undefined),
edouard@1815
  1633
                              kl_rating);
edouard@1815
  1634
                }
edouard@1815
  1635
                free_identity(_sender);
edouard@1815
  1636
                if (status == PEP_CANNOT_FIND_IDENTITY)
edouard@1815
  1637
                   status = PEP_STATUS_OK;
edouard@1815
  1638
            }
edouard@1815
  1639
        }
edouard@1815
  1640
    }
edouard@1815
  1641
    return status;
edouard@1815
  1642
}
edouard@1815
  1643
krista@1484
  1644
Edouard@741
  1645
DYNAMIC_API PEP_STATUS _decrypt_message(
vb@37
  1646
        PEP_SESSION session,
vb@113
  1647
        message *src,
vb@241
  1648
        message **dst,
vb@251
  1649
        stringlist_t **keylist,
vb@1004
  1650
        PEP_rating *rating,
krista@1427
  1651
        PEP_decrypt_flags_t *flags,
Edouard@741
  1652
        identity_list **private_il
vb@37
  1653
    )
vb@37
  1654
{
vb@37
  1655
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
  1656
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
  1657
    message *msg = NULL;
vb@112
  1658
    char *ctext;
vb@112
  1659
    size_t csize;
vb@269
  1660
    char *ptext = NULL;
vb@112
  1661
    size_t psize;
vb@241
  1662
    stringlist_t *_keylist = NULL;
vb@37
  1663
vb@74
  1664
    assert(session);
vb@74
  1665
    assert(src);
vb@74
  1666
    assert(dst);
vb@241
  1667
    assert(keylist);
vb@1004
  1668
    assert(rating);
Edouard@739
  1669
    assert(flags);
vb@73
  1670
vb@1004
  1671
    if (!(session && src && dst && keylist && rating && flags))
roker@1853
  1672
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
  1673
Edouard@739
  1674
    *flags = 0;
Edouard@739
  1675
Edouard@734
  1676
    // Private key in unencrypted mail are ignored -> NULL
Edouard@734
  1677
    bool imported_keys = import_attached_keys(session, src, NULL);
Edouard@728
  1678
Edouard@728
  1679
    // Update src->from in case we just imported a key
Edouard@728
  1680
    // we would need to check signature
Edouard@858
  1681
    status = _update_identity_for_incoming_message(session, src);
Edouard@858
  1682
    if(status != PEP_STATUS_OK)
roker@1853
  1683
        return ADD_TO_LOG(status);
Edouard@858
  1684
krista@1397
  1685
    // Get detached signature, if any
krista@1397
  1686
    bloblist_t* detached_sig = NULL;
krista@1397
  1687
    char* dsig_text = NULL;
krista@1397
  1688
    size_t dsig_size = 0;
krista@1397
  1689
    status = _get_detached_signature(src, &detached_sig);
krista@1397
  1690
    if (detached_sig) {
krista@1397
  1691
        dsig_text = detached_sig->value;
krista@1397
  1692
        dsig_size = detached_sig->size;
krista@1397
  1693
    }
krista@1427
  1694
vb@259
  1695
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
  1696
vb@74
  1697
    *dst = NULL;
vb@251
  1698
    *keylist = NULL;
vb@1004
  1699
    *rating = PEP_rating_undefined;
krista@1427
  1700
vb@261
  1701
    switch (src->enc_format) {
vb@269
  1702
        case PEP_enc_none:
vb@1004
  1703
            *rating = PEP_rating_unencrypted;
vb@731
  1704
            if (imported_keys)
vb@731
  1705
                remove_attached_keys(src);
edouard@1603
  1706
            if(session->sync_session->inject_sync_msg){
edouard@1165
  1707
                status = receive_DeviceState_msg(session, src, *rating, *keylist);
krista@1427
  1708
                if (status == PEP_MESSAGE_CONSUME ||
edouard@1369
  1709
                    status == PEP_MESSAGE_IGNORE) {
vb@1134
  1710
                    free_message(msg);
vb@1134
  1711
                    msg = NULL;
edouard@1369
  1712
                    *flags |= (status == PEP_MESSAGE_IGNORE) ?
edouard@1369
  1713
                                PEP_decrypt_flag_ignore :
edouard@1369
  1714
                                PEP_decrypt_flag_consume;
vb@1134
  1715
                }
vb@1134
  1716
                else if (status != PEP_STATUS_OK) {
roker@1853
  1717
                    return ADD_TO_LOG(status);
vb@1134
  1718
                }
vb@1099
  1719
            }
krista@1484
  1720
            
krista@1484
  1721
            char* slong = src->longmsg;
krista@1484
  1722
            char* sform = src->longmsg_formatted;
krista@1484
  1723
            bloblist_t* satt = src->attachments;
roker@1563
  1724
            
krista@1484
  1725
            if ((!slong || slong[0] == '\0')
krista@1484
  1726
                 && (!sform || sform[0] == '\0')) {
krista@1484
  1727
                if (satt) {
krista@1484
  1728
                    const char* inner_mime_type = satt->mime_type;
krista@1484
  1729
                    if (strcasecmp(inner_mime_type, "text/plain") == 0) {
krista@1484
  1730
                        free(slong); /* in case of "" */
roker@1563
  1731
                        src->longmsg = strndup(satt->value, satt->size); // N.B.: longmsg might be shorter, if attachment contains NUL bytes which are not allowed in text/plain!
roker@1563
  1732
                        
krista@1484
  1733
                        bloblist_t* next_node = satt->next;
krista@1484
  1734
                        if (next_node) {
krista@1484
  1735
                            inner_mime_type = next_node->mime_type;
krista@1484
  1736
                            if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@1484
  1737
                                free(sform);
roker@1563
  1738
                                src->longmsg_formatted = strndup(next_node->value, next_node->size);  // N.B.: longmsg might be shorter, if attachment contains NUL bytes which are not allowed in text/plain!
krista@1484
  1739
                            }
krista@1484
  1740
                        }
krista@1484
  1741
                    }
krista@1484
  1742
                    else if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@1484
  1743
                        free(sform);
roker@1563
  1744
                        src->longmsg_formatted = strndup(satt->value, satt->size);  // N.B.: longmsg might be shorter, if attachment contains NUL bytes which are not allowed in text/plain!
roker@1563
  1745
                    }
krista@1484
  1746
                }
roker@1563
  1747
            }
krista@1484
  1748
            
roker@1853
  1749
            return ADD_TO_LOG(PEP_UNENCRYPTED);
vb@269
  1750
vb@261
  1751
        case PEP_enc_PGP_MIME:
vb@301
  1752
            ctext = src->attachments->next->value;
vb@269
  1753
            csize = src->attachments->next->size;
vb@261
  1754
            break;
vb@261
  1755
edouard@1557
  1756
        case PEP_enc_PGP_MIME_Outlook1:
edouard@1557
  1757
            ctext = src->attachments->value;
edouard@1557
  1758
            csize = src->attachments->size;
edouard@1557
  1759
            break;
edouard@1557
  1760
vb@261
  1761
        case PEP_enc_pieces:
vb@261
  1762
            ctext = src->longmsg;
vb@261
  1763
            csize = strlen(ctext);
vb@261
  1764
            break;
vb@261
  1765
vb@261
  1766
        default:
vb@261
  1767
            NOT_IMPLEMENTED
vb@259
  1768
    }
Edouard@431
  1769
    status = cryptotech[crypto].decrypt_and_verify(session, ctext,
krista@1427
  1770
                                                   csize, dsig_text, dsig_size,
krista@1397
  1771
                                                   &ptext, &psize, &_keylist);
edouard@1195
  1772
    if (status > PEP_CANNOT_DECRYPT_UNKNOWN){
roker@1729
  1773
        GOTO(pep_error);
edouard@1271
  1774
    }
edouard@1271
  1775
edouard@1273
  1776
    decrypt_status = status;
edouard@1273
  1777
edouard@1271
  1778
    if (status == PEP_DECRYPT_NO_KEY){
edouard@1299
  1779
        PEP_STATUS sync_status = inject_DeviceState_event(session, CannotDecrypt, NULL, NULL);
edouard@1299
  1780
        if (sync_status == PEP_OUT_OF_MEMORY){
edouard@1299
  1781
            status = PEP_OUT_OF_MEMORY;
edouard@1273
  1782
            goto pep_error;
edouard@1299
  1783
        }
edouard@1195
  1784
    }
vb@113
  1785
krista@1427
  1786
    bool imported_private_key_address = false;
Edouard@728
  1787
vb@256
  1788
    if (ptext) {
vb@256
  1789
        switch (src->enc_format) {
vb@256
  1790
            case PEP_enc_PGP_MIME:
edouard@1557
  1791
            case PEP_enc_PGP_MIME_Outlook1:
vb@269
  1792
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  1793
                if (status != PEP_STATUS_OK)
roker@1563
  1794
                    goto pep_error;
krista@1484
  1795
                
krista@1484
  1796
                char* mlong = msg->longmsg;
krista@1484
  1797
                char* mform = msg->longmsg_formatted;
krista@1484
  1798
                bloblist_t* matt = msg->attachments;
roker@1563
  1799
                
krista@1484
  1800
                if ((!mlong || mlong[0] == '\0')
krista@1484
  1801
                     && (!mform || mform[0] == '\0')) {
krista@1484
  1802
                    if (matt) {
krista@1484
  1803
                        const char* inner_mime_type = matt->mime_type;
krista@1484
  1804
                        if (strcasecmp(inner_mime_type, "text/plain") == 0) {
krista@1484
  1805
                            free(mlong); /* in case of "" */
roker@1563
  1806
                            msg->longmsg = strndup(matt->value, matt->size);
roker@1563
  1807
                            
krista@1484
  1808
                            bloblist_t* next_node = matt->next;
krista@1484
  1809
                            if (next_node) {
krista@1484
  1810
                                inner_mime_type = next_node->mime_type;
krista@1484
  1811
                                if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@1484
  1812
                                    free(mform);
roker@1563
  1813
                                    msg->longmsg_formatted = strndup(next_node->value, next_node->size);
krista@1484
  1814
                                }
krista@1484
  1815
                            }
krista@1484
  1816
                        }
krista@1484
  1817
                        else if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@1484
  1818
                            free(mform);
roker@1563
  1819
                            msg->longmsg_formatted = strndup(matt->value, matt->size);
roker@1563
  1820
                        }
krista@1484
  1821
                    }
krista@1484
  1822
                    if (msg->shortmsg) {
krista@1484
  1823
                        free(src->shortmsg);
krista@1484
  1824
                        src->shortmsg = strdup(msg->shortmsg);
krista@1484
  1825
                    }
roker@1563
  1826
                }
krista@1484
  1827
krista@1484
  1828
                if (decrypt_status != PEP_DECRYPTED_AND_VERIFIED) {
krista@1484
  1829
                    status = _get_detached_signature(msg, &detached_sig);
krista@1484
  1830
                    if (decrypt_status == PEP_DECRYPTED && detached_sig) {
krista@1484
  1831
                        dsig_text = detached_sig->value;
krista@1484
  1832
                        dsig_size = detached_sig->size;
krista@1484
  1833
                        size_t ssize = 0;
krista@1484
  1834
                        char* stext = NULL;
krista@1484
  1835
krista@1484
  1836
                        status = _get_signed_text(ptext, psize, &stext, &ssize);
krista@1484
  1837
                        stringlist_t *_verify_keylist = NULL;
krista@1484
  1838
krista@1484
  1839
                        if (ssize > 0 && stext) {
krista@1484
  1840
                            status = cryptotech[crypto].verify_text(session, stext,
krista@1484
  1841
                                                                    ssize, dsig_text, dsig_size,
krista@1484
  1842
                                                                    &_verify_keylist);
krista@1484
  1843
krista@1484
  1844
                            if (status == PEP_VERIFIED || status == PEP_VERIFIED_AND_TRUSTED)
krista@1484
  1845
                                decrypt_status = PEP_DECRYPTED_AND_VERIFIED;
krista@1484
  1846
                            
krista@1484
  1847
                                status = combine_keylists(session, &_verify_keylist, &_keylist, src->from);
krista@1484
  1848
                        }
krista@1427
  1849
                    }
krista@1427
  1850
                }
vb@256
  1851
                break;
vb@256
  1852
vb@256
  1853
            case PEP_enc_pieces:
vb@256
  1854
                msg = clone_to_empty_message(src);
vb@256
  1855
                if (msg == NULL)
vb@113
  1856
                    goto enomem;
vb@113
  1857
Edouard@882
  1858
                msg->longmsg = ptext;
Edouard@882
  1859
                ptext = NULL;
vb@256
  1860
vb@256
  1861
                bloblist_t *_m = msg->attachments;
vb@320
  1862
                if (_m == NULL && src->attachments && src->attachments->value) {
vb@320
  1863
                    msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@320
  1864
                    _m = msg->attachments;
vb@320
  1865
                }
vb@320
  1866
vb@256
  1867
                bloblist_t *_s;
Edouard@754
  1868
                for (_s = src->attachments; _s; _s = _s->next) {
Edouard@754
  1869
                    if (_s->value == NULL && _s->size == 0){
Edouard@754
  1870
                        _m = bloblist_add(_m, NULL, 0, _s->mime_type, _s->filename);
Edouard@754
  1871
                        if (_m == NULL)
Edouard@754
  1872
                            goto enomem;
Edouard@754
  1873
Edouard@754
  1874
                    }
Edouard@754
  1875
                    else if (is_encrypted_attachment(_s)) {
vb@256
  1876
                        stringlist_t *_keylist = NULL;
roker@862
  1877
                        char *attctext  = _s->value;
roker@862
  1878
                        size_t attcsize = _s->size;
Edouard@431
  1879
Edouard@842
  1880
                        free(ptext);
Edouard@842
  1881
                        ptext = NULL;
Edouard@842
  1882
krista@1397
  1883
                        // FIXME: What about attachments with separate sigs???
Edouard@431
  1884
                        status = decrypt_and_verify(session, attctext, attcsize,
krista@1397
  1885
                                                    NULL, 0,
krista@1397
  1886
                                                    &ptext, &psize, &_keylist);
krista@1484
  1887
                        free_stringlist(_keylist); // FIXME: Why do we do this?
vb@256
  1888
vb@289
  1889
                        if (ptext) {
vb@289
  1890
                            if (is_encrypted_html_attachment(_s)) {
Edouard@882
  1891
                                msg->longmsg_formatted = ptext;
Edouard@882
  1892
                                ptext = NULL;
vb@289
  1893
                            }
vb@289
  1894
                            else {
roker@862
  1895
                                static const char * const mime_type = "application/octet-stream";
roker@864
  1896
                                char * const filename =
vb@289
  1897
                                    without_double_ending(_s->filename);
vb@289
  1898
                                if (filename == NULL)
vb@289
  1899
                                    goto enomem;
vb@289
  1900
Edouard@882
  1901
                                _m = bloblist_add(_m, ptext, psize, mime_type,
vb@289
  1902
                                    filename);
roker@862
  1903
                                free(filename);
vb@290
  1904
                                if (_m == NULL)
vb@289
  1905
                                    goto enomem;
vb@290
  1906
Edouard@882
  1907
                                ptext = NULL;
Edouard@882
  1908
vb@290
  1909
                                if (msg->attachments == NULL)
vb@290
  1910
                                    msg->attachments = _m;
vb@289
  1911
                            }
vb@256
  1912
                        }
vb@256
  1913
                        else {
vb@320
  1914
                            char *copy = malloc(_s->size);
vb@350
  1915
                            assert(copy);
vb@350
  1916
                            if (copy == NULL)
vb@350
  1917
                                goto enomem;
vb@320
  1918
                            memcpy(copy, _s->value, _s->size);
vb@320
  1919
                            _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@256
  1920
                            if (_m == NULL)
vb@256
  1921
                                goto enomem;
vb@256
  1922
                        }
vb@256
  1923
                    }
vb@320
  1924
                    else {
vb@320
  1925
                        char *copy = malloc(_s->size);
vb@350
  1926
                        assert(copy);
vb@350
  1927
                        if (copy == NULL)
vb@350
  1928
                            goto enomem;
vb@320
  1929
                        memcpy(copy, _s->value, _s->size);
vb@320
  1930
                        _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@320
  1931
                        if (_m == NULL)
vb@320
  1932
                            goto enomem;
vb@320
  1933
                    }
vb@256
  1934
                }
vb@256
  1935
vb@256
  1936
                break;
vb@256
  1937
vb@256
  1938
            default:
vb@256
  1939
                // BUG: must implement more
vb@256
  1940
                NOT_IMPLEMENTED
vb@256
  1941
        }
krista@1427
  1942
vb@256
  1943
        switch (src->enc_format) {
vb@256
  1944
            case PEP_enc_PGP_MIME:
vb@256
  1945
            case PEP_enc_pieces:
edouard@1557
  1946
            case PEP_enc_PGP_MIME_Outlook1:
vb@256
  1947
                status = copy_fields(msg, src);
vb@256
  1948
                if (status != PEP_STATUS_OK)
roker@1729
  1949
                {
roker@1729
  1950
                    GOTO(pep_error);
roker@1729
  1951
                }
vb@256
  1952
vb@280
  1953
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1954
                {
vb@256
  1955
                    char * shortmsg;
vb@256
  1956
                    char * longmsg;
vb@256
  1957
krista@853
  1958
                    int r = separate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1959
                            &longmsg);
krista@1852
  1960
                    
vb@256
  1961
                    if (r == -1)
vb@256
  1962
                        goto enomem;
vb@256
  1963
krista@1852
  1964
                    if (shortmsg == NULL) {
krista@1852
  1965
                        if (src->shortmsg == NULL)
krista@1852
  1966
                            shortmsg = strdup("");
krista@1852
  1967
                        else {
krista@1852
  1968
                            // FIXME: is msg->shortmsg always a copy of
krista@1852
  1969
                            // src->shortmsg already?
krista@1852
  1970
                            // if so, we need to change the logic so
krista@1852
  1971
                            // that in this case, we don't free msg->shortmsg
krista@1852
  1972
                            // and do this strdup, etc.
krista@1852
  1973
                            shortmsg = strdup(src->shortmsg);
krista@1852
  1974
                        }
krista@1852
  1975
                    }
krista@1852
  1976
krista@1852
  1977
vb@256
  1978
                    free(msg->shortmsg);
vb@256
  1979
                    free(msg->longmsg);
vb@256
  1980
vb@256
  1981
                    msg->shortmsg = shortmsg;
vb@256
  1982
                    msg->longmsg = longmsg;
vb@256
  1983
                }
vb@256
  1984
                else {
vb@256
  1985
                    msg->shortmsg = strdup(src->shortmsg);
vb@469
  1986
                    assert(msg->shortmsg);
vb@256
  1987
                    if (msg->shortmsg == NULL)
vb@256
  1988
                        goto enomem;
vb@256
  1989
                }
vb@256
  1990
                break;
vb@256
  1991
            default:
krista@1484
  1992
                    // BUG: must implement more
krista@1484
  1993
                    NOT_IMPLEMENTED
vb@256
  1994
        }
krista@1427
  1995
Edouard@734
  1996
        // check for private key in decrypted message attachement while inporting
Edouard@741
  1997
        identity_list *_private_il = NULL;
Edouard@741
  1998
        imported_keys = import_attached_keys(session, msg, &_private_il);
krista@1427
  1999
        if (_private_il &&
Edouard@741
  2000
            identity_list_length(_private_il) == 1 &&
Edouard@741
  2001
            _private_il->ident->address)
Edouard@728
  2002
        {
Edouard@741
  2003
            imported_private_key_address = true;
Edouard@728
  2004
        }
Edouard@741
  2005
Edouard@741
  2006
        if(private_il && imported_private_key_address){
Edouard@741
  2007
            *private_il = _private_il;
Edouard@741
  2008
        }else{
Edouard@741
  2009
            free_identity_list(_private_il);
Edouard@741
  2010
        }
krista@1427
  2011
Edouard@694
  2012
        if(decrypt_status == PEP_DECRYPTED){
Edouard@728
  2013
Edouard@728
  2014
            // TODO optimize if import_attached_keys didn't import any key
krista@1427
  2015
Edouard@431
  2016
            // In case message did decrypt, but no valid signature could be found
Edouard@431
  2017
            // then retry decrypt+verify after importing key.
Edouard@728
  2018
Edouard@728
  2019
            // Update msg->from in case we just imported a key
Edouard@728
  2020
            // we would need to check signature
vb@781
  2021
Edouard@861
  2022
            status = _update_identity_for_incoming_message(session, src);
Edouard@858
  2023
            if(status != PEP_STATUS_OK)
roker@1729
  2024
            {
roker@1729
  2025
                GOTO(pep_error);
roker@1729
  2026
            }
krista@1427
  2027
Edouard@431
  2028
            char *re_ptext = NULL;
Edouard@431
  2029
            size_t re_psize;
krista@1427
  2030
Edouard@431
  2031
            free_stringlist(_keylist);
Edouard@431
  2032
            _keylist = NULL;
vb@256
  2033
Edouard@431
  2034
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
krista@1397
  2035
                csize, dsig_text, dsig_size, &re_ptext, &re_psize, &_keylist);
krista@1427
  2036
Edouard@882
  2037
            free(re_ptext);
krista@1427
  2038
Edouard@431
  2039
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
roker@1729
  2040
            {
roker@1729
  2041
                GOTO(pep_error);
roker@1729
  2042
            }
krista@1427
  2043
Edouard@431
  2044
            decrypt_status = status;
Edouard@431
  2045
        }
krista@1427
  2046
vb@1004
  2047
        *rating = decrypt_rating(decrypt_status);
krista@1427
  2048
edouard@1815
  2049
        status = amend_rating_according_to_sender_and_recipients(session,
edouard@1815
  2050
                                                                 rating,
edouard@1815
  2051
                                                                 src->from,
edouard@1815
  2052
                                                                 _keylist);
edouard@1815
  2053
edouard@1815
  2054
        if (status != PEP_STATUS_OK)
edouard@1815
  2055
            GOTO(pep_error);
vb@113
  2056
    }
Edouard@745
  2057
    else
Edouard@745
  2058
    {
vb@1004
  2059
        *rating = decrypt_rating(decrypt_status);
Edouard@745
  2060
        goto pep_error;
Edouard@745
  2061
    }
vb@731
  2062
Edouard@728
  2063
    // Case of own key imported from own trusted message
krista@1427
  2064
    if (// Message have been reliably decrypted
Edouard@728
  2065
        msg &&
vb@1004
  2066
        *rating >= PEP_rating_trusted &&
Edouard@728
  2067
        imported_private_key_address &&
Edouard@728
  2068
        // to is [own]
Edouard@728
  2069
        msg->to->ident->user_id &&
krista@1427
  2070
        strcmp(msg->to->ident->user_id, PEP_OWN_USERID) == 0
Edouard@729
  2071
        )
Edouard@728
  2072
    {
Edouard@738
  2073
        *flags |= PEP_decrypt_flag_own_private_key;
Edouard@728
  2074
    }
vb@113
  2075
vb@731
  2076
    if (msg) {
vb@1004
  2077
        decorate_message(msg, *rating, _keylist);
vb@731
  2078
        if (imported_keys)
vb@731
  2079
            remove_attached_keys(msg);
edouard@1603
  2080
        if (*rating >= PEP_rating_reliable &&
edouard@1603
  2081
            session->sync_session->inject_sync_msg) {
edouard@1165
  2082
            status = receive_DeviceState_msg(session, msg, *rating, _keylist);
krista@1427
  2083
            if (status == PEP_MESSAGE_CONSUME ||
edouard@1369
  2084
                status == PEP_MESSAGE_IGNORE) {
vb@1099
  2085
                free_message(msg);
vb@1099
  2086
                msg = NULL;
edouard@1369
  2087
                *flags |= (status == PEP_MESSAGE_IGNORE) ?
edouard@1369
  2088
                            PEP_decrypt_flag_ignore :
edouard@1369
  2089
                            PEP_decrypt_flag_consume;
edouard@1355
  2090
edouard@1355
  2091
                status = decrypt_status;
vb@1099
  2092
            }
vb@1099
  2093
            else if (status != PEP_STATUS_OK){
vb@1099
  2094
                goto pep_error;
vb@1099
  2095
            }
vb@1099
  2096
        }
edouard@1558
  2097
    }
edouard@1558
  2098
    if (msg) {
edouard@1542
  2099
        if (src->id) {
edouard@1542
  2100
            msg->id = strdup(src->id);
edouard@1542
  2101
            assert(msg->id);
edouard@1542
  2102
            if (msg->id == NULL)
edouard@1542
  2103
                goto enomem;
edouard@1542
  2104
        }
vb@731
  2105
    }
vb@235
  2106
vb@74
  2107
    *dst = msg;
vb@241
  2108
    *keylist = _keylist;
vb@241
  2109
edouard@1832
  2110
    return PEP_STATUS_OK;
vb@73
  2111
vb@73
  2112
enomem:
vb@73
  2113
    status = PEP_OUT_OF_MEMORY;
vb@73
  2114
vb@73
  2115
pep_error:
Edouard@882
  2116
    free(ptext);
vb@73
  2117
    free_message(msg);
vb@241
  2118
    free_stringlist(_keylist);
vb@39
  2119
roker@1853
  2120
    return ADD_TO_LOG(status);
vb@37
  2121
}
vb@37
  2122
Edouard@741
  2123
DYNAMIC_API PEP_STATUS decrypt_message(
Edouard@741
  2124
        PEP_SESSION session,
Edouard@741
  2125
        message *src,
Edouard@741
  2126
        message **dst,
Edouard@741
  2127
        stringlist_t **keylist,
vb@1004
  2128
        PEP_rating *rating,
krista@1427
  2129
        PEP_decrypt_flags_t *flags
Edouard@741
  2130
    )
Edouard@741
  2131
{
vb@1004
  2132
    return _decrypt_message( session, src, dst, keylist, rating, flags, NULL );
Edouard@741
  2133
}
Edouard@741
  2134
Edouard@728
  2135
DYNAMIC_API PEP_STATUS own_message_private_key_details(
Edouard@728
  2136
        PEP_SESSION session,
Edouard@728
  2137
        message *msg,
krista@1427
  2138
        pEp_identity **ident
Edouard@728
  2139
    )
Edouard@728
  2140
{
Edouard@728
  2141
    assert(session);
Edouard@728
  2142
    assert(msg);
Edouard@728
  2143
    assert(ident);
Edouard@728
  2144
Edouard@736
  2145
    if (!(session && msg && ident))
Edouard@728
  2146
        return PEP_ILLEGAL_VALUE;
Edouard@728
  2147
krista@1427
  2148
    message *dst = NULL;
krista@956
  2149
    stringlist_t *keylist = NULL;
vb@1004
  2150
    PEP_rating rating;
krista@1427
  2151
    PEP_decrypt_flags_t flags;
Edouard@728
  2152
Edouard@729
  2153
    *ident = NULL;
Edouard@729
  2154
Edouard@740
  2155
    identity_list *private_il = NULL;
vb@1004
  2156
    PEP_STATUS status = _decrypt_message(session, msg,  &dst, &keylist, &rating, &flags, &private_il);
vb@953
  2157
    free_message(dst);
vb@953
  2158
    free_stringlist(keylist);
Edouard@728
  2159
Edouard@729
  2160
    if (status == PEP_STATUS_OK &&
Edouard@741
  2161
        flags & PEP_decrypt_flag_own_private_key &&
Edouard@741
  2162
        private_il)
Edouard@729
  2163
    {
Edouard@740
  2164
        *ident = identity_dup(private_il->ident);
Edouard@729
  2165
    }
Edouard@729
  2166
Edouard@741
  2167
    free_identity_list(private_il);
Edouard@741
  2168
roker@1853
  2169
    return ADD_TO_LOG(status);
Edouard@728
  2170
}
Edouard@728
  2171
Edouard@858
  2172
static void _max_comm_type_from_identity_list(
krista@1427
  2173
        identity_list *identities,
Edouard@858
  2174
        PEP_SESSION session,
Edouard@858
  2175
        PEP_comm_type *max_comm_type,
Edouard@858
  2176
        bool *comm_type_determined
Edouard@858
  2177
    )
Edouard@858
  2178
{
Edouard@858
  2179
    identity_list * il;
Edouard@858
  2180
    for (il = identities; il != NULL; il = il->next)
Edouard@858
  2181
    {
Edouard@858
  2182
        if (il->ident)
Edouard@858
  2183
        {
edouard@1385
  2184
            PEP_STATUS status = update_identity(session, il->ident);
Edouard@858
  2185
            if (status == PEP_STATUS_OK)
Edouard@858
  2186
            {
Edouard@858
  2187
                *max_comm_type = _get_comm_type(session, *max_comm_type,
Edouard@858
  2188
                        il->ident);
Edouard@858
  2189
                *comm_type_determined = true;
Edouard@858
  2190
            }
Edouard@858
  2191
        }
Edouard@858
  2192
    }
Edouard@858
  2193
}
Edouard@858
  2194
vb@1004
  2195
DYNAMIC_API PEP_STATUS outgoing_message_rating(
vb@190
  2196
        PEP_SESSION session,
vb@190
  2197
        message *msg,
vb@1004
  2198
        PEP_rating *rating
vb@190
  2199
    )
vb@190
  2200
{
vb@190
  2201
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  2202
    bool comm_type_determined = false;
vb@190
  2203
vb@190
  2204
    assert(session);
vb@190
  2205
    assert(msg);
vb@251
  2206
    assert(msg->dir == PEP_dir_outgoing);
vb@1004
  2207
    assert(rating);
vb@190
  2208
vb@1004
  2209
    if (!(session && msg && rating))
roker@1853
  2210
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
  2211
vb@1360
  2212
    if (msg->dir != PEP_dir_outgoing)
roker@1853
  2213
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@251
  2214
vb@1004
  2215
    *rating = PEP_rating_undefined;
vb@190
  2216
Edouard@858
  2217
    _max_comm_type_from_identity_list(msg->to, session,
Edouard@858
  2218
                                      &max_comm_type, &comm_type_determined);
vb@190
  2219
Edouard@858
  2220
    _max_comm_type_from_identity_list(msg->cc, session,
Edouard@858
  2221
                                      &max_comm_type, &comm_type_determined);
krista@1427
  2222
Edouard@858
  2223
    _max_comm_type_from_identity_list(msg->bcc, session,
Edouard@858
  2224
                                      &max_comm_type, &comm_type_determined);
vb@190
  2225
vb@190
  2226
    if (comm_type_determined == false)
vb@1004
  2227
        *rating = PEP_rating_undefined;
vb@190
  2228
    else
vb@1004
  2229
        *rating = _MAX(_rating(max_comm_type, PEP_rating_undefined),
vb@486
  2230
                PEP_rating_unencrypted);
vb@190
  2231
vb@190
  2232
    return PEP_STATUS_OK;
vb@190
  2233
}
vb@190
  2234
vb@1004
  2235
DYNAMIC_API PEP_STATUS identity_rating(
vb@239
  2236
        PEP_SESSION session,
vb@239
  2237
        pEp_identity *ident,
vb@1004
  2238
        PEP_rating *rating
vb@239
  2239
    )
vb@239
  2240
{
vb@239
  2241
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  2242
vb@239
  2243
    assert(session);
vb@239
  2244
    assert(ident);
vb@1004
  2245
    assert(rating);
vb@239
  2246
vb@1004
  2247
    if (!(session && ident && rating))
vb@239
  2248
        return PEP_ILLEGAL_VALUE;
vb@239
  2249
vb@239
  2250
    if (ident->me)
edouard@1406
  2251
        status = _myself(session, ident, false, true);
vb@239
  2252
    else
vb@239
  2253
        status = update_identity(session, ident);
vb@239
  2254
vb@239
  2255
    if (status == PEP_STATUS_OK)
vb@1004
  2256
        *rating = _rating(ident->comm_type, PEP_rating_undefined);
vb@239
  2257
vb@239
  2258
    return status;
vb@239
  2259
}
vb@239
  2260
vb@507
  2261
DYNAMIC_API PEP_STATUS get_binary_path(PEP_cryptotech tech, const char **path)
vb@507
  2262
{
vb@507
  2263
    PEP_STATUS status = PEP_STATUS_OK;
vb@507
  2264
vb@507
  2265
    assert(path);
vb@507
  2266
    if (path == NULL)
vb@507
  2267
        return PEP_ILLEGAL_VALUE;
vb@507
  2268
vb@507
  2269
    if (cryptotech[tech].binary_path == NULL)
vb@507
  2270
        *path = NULL;
vb@507
  2271
    else
vb@507
  2272
        status = cryptotech[tech].binary_path(path);
vb@507
  2273
vb@507
  2274
    return status;
vb@507
  2275
}
vb@507
  2276
vb@1004
  2277
vb@1004
  2278
DYNAMIC_API PEP_color color_from_rating(PEP_rating rating)
vb@1004
  2279
{
krista@1243
  2280
    if (rating == PEP_rating_b0rken || rating == PEP_rating_have_no_key)
vb@1004
  2281
        return PEP_color_no_color;
vb@1004
  2282
vb@1004
  2283
    if (rating < PEP_rating_undefined)
vb@1004
  2284
        return PEP_color_red;
vb@1004
  2285
vb@1004
  2286
    if (rating < PEP_rating_reliable)
vb@1004
  2287
        return PEP_color_no_color;
vb@1004
  2288
vb@1004
  2289
    if (rating < PEP_rating_trusted)
vb@1004
  2290
        return PEP_color_yellow;
vb@1004
  2291
vb@1004
  2292
    if (rating >= PEP_rating_trusted)
vb@1004
  2293
        return PEP_color_green;
vb@1004
  2294
vb@1004
  2295
    // this should never happen
vb@1004
  2296
    assert(false);
roker@1729
  2297
    return PEP_color_no_color;
vb@1004
  2298
}
vb@1004
  2299
krista@1307
  2300
DYNAMIC_API PEP_STATUS get_trustwords(
roker@1509
  2301
    PEP_SESSION session, const pEp_identity* id1, const pEp_identity* id2,
krista@1307
  2302
    const char* lang, char **words, size_t *wsize, bool full
krista@1307
  2303
)
krista@1307
  2304
{
krista@1307
  2305
    assert(session);
krista@1307
  2306
    assert(id1);
krista@1307
  2307
    assert(id2);
krista@1307
  2308
    assert(id1->fpr);
krista@1307
  2309
    assert(id2->fpr);
krista@1307
  2310
    assert(words);
krista@1307
  2311
    assert(wsize);
krista@1427
  2312
krista@1307
  2313
    if (!(session && id1 && id2 && words && wsize) ||
krista@1307
  2314
        !(id1->fpr) || (!id2->fpr))
krista@1307
  2315
        return PEP_ILLEGAL_VALUE;
krista@1427
  2316
krista@1307
  2317
    const char *source1 = id1->fpr;
krista@1307
  2318
    const char *source2 = id2->fpr;
krista@1427
  2319
krista@1307
  2320
    *words = NULL;
krista@1307
  2321
    *wsize = 0;
krista@1307
  2322
krista@1307
  2323
    const size_t SHORT_NUM_TWORDS = 5;
krista@1427
  2324
krista@1307
  2325
    // N.B. THIS will have to be changed once we start checking trustword entropy.
krista@1307
  2326
    // For now, full is ALL, and otherwise it's 5-per-id.
krista@1307
  2327
    size_t max_words_per_id = (full ? 0 : SHORT_NUM_TWORDS);
krista@1427
  2328
krista@1307
  2329
    char* first_set = NULL;
krista@1307
  2330
    char* second_set = NULL;
krista@1307
  2331
    size_t first_wsize = 0;
krista@1307
  2332
    size_t second_wsize = 0;
krista@1427
  2333
krista@1312
  2334
    int fpr_comparison = -255;
edouard@1746
  2335
    PEP_STATUS status = _compare_fprs(source1, strlen(source1), source2, strlen(source2), &fpr_comparison);
krista@1312
  2336
    if (status != PEP_STATUS_OK)
krista@1312
  2337
        return status;
krista@1312
  2338
krista@1307
  2339
    char* _retstr = NULL;
krista@1312
  2340
krista@1312
  2341
    switch (fpr_comparison) {
krista@1312
  2342
        case 1: // source1 > source2
krista@1312
  2343
            status = trustwords(session, source2, lang, &first_set, &first_wsize, max_words_per_id);
krista@1312
  2344
            if (status != PEP_STATUS_OK)
krista@1312
  2345
                goto error_release;
krista@1427
  2346
            status = trustwords(session, source1, lang, &second_set, &second_wsize, max_words_per_id);
krista@1312
  2347
            if (status != PEP_STATUS_OK)
krista@1312
  2348
                goto error_release;
krista@1312
  2349
            break;
krista@1427
  2350
        case 0:
krista@1312
  2351
        case -1: // source1 <= source2
krista@1312
  2352
            status = trustwords(session, source1, lang, &first_set, &first_wsize, max_words_per_id);
krista@1312
  2353
            if (status != PEP_STATUS_OK)
krista@1312
  2354
                goto error_release;
krista@1427
  2355
            status = trustwords(session, source2, lang, &second_set, &second_wsize, max_words_per_id);
krista@1312
  2356
            if (status != PEP_STATUS_OK)
krista@1312
  2357
                goto error_release;
krista@1315
  2358
            break;
krista@1312
  2359
        default:
roker@1853
  2360
            return ADD_TO_LOG(PEP_UNKNOWN_ERROR); // shouldn't be possible
krista@1312
  2361
    }
krista@1427
  2362
krista@1307
  2363
    size_t _wsize = first_wsize + second_wsize;
krista@1427
  2364
krista@1315
  2365
    bool needs_space = (first_set[first_wsize - 1] != ' ');
krista@1427
  2366
krista@1315
  2367
    if (needs_space)
krista@1315
  2368
        _wsize++;
krista@1427
  2369
krista@1307
  2370
    _retstr = calloc(1, _wsize + 1);
krista@1427
  2371
krista@1307
  2372
    size_t len = strlcpy(_retstr, first_set, _wsize);
krista@1307
  2373
    if (len >= _wsize) {
krista@1307
  2374
        status = PEP_UNKNOWN_ERROR;
krista@1307
  2375
        goto error_release;
krista@1307
  2376
    }
krista@1315
  2377
    if (needs_space) {
krista@1315
  2378
        strlcat(_retstr, " ", _wsize);
krista@1315
  2379
        if (len >= _wsize) {
krista@1315
  2380
            status = PEP_UNKNOWN_ERROR;
krista@1315
  2381
            goto error_release;
krista@1315
  2382
        }
krista@1315
  2383
    }
krista@1307
  2384
    strlcat(_retstr, second_set, _wsize);
krista@1307
  2385
    if (len >= _wsize){
krista@1307
  2386
        status = PEP_UNKNOWN_ERROR;
krista@1307
  2387
        goto error_release;
krista@1307
  2388
    }
krista@1427
  2389
krista@1307
  2390
    *words = _retstr;
krista@1307
  2391
    *wsize = _wsize;
krista@1307
  2392
    status = PEP_STATUS_OK;
krista@1427
  2393
krista@1307
  2394
    goto the_end;
krista@1427
  2395
krista@1307
  2396
    error_release:
krista@1307
  2397
    free(_retstr);
krista@1427
  2398
krista@1307
  2399
    the_end:
krista@1307
  2400
    free(first_set);
krista@1307
  2401
    free(second_set);
roker@1853
  2402
    return ADD_TO_LOG(status);
vb@1309
  2403
}
vb@1309
  2404
edouard@1553
  2405
DYNAMIC_API PEP_STATUS get_message_trustwords(
edouard@1553
  2406
    PEP_SESSION session, 
edouard@1553
  2407
    message *msg,
edouard@1553
  2408
    stringlist_t *keylist,
edouard@1553
  2409
    pEp_identity* received_by,
edouard@1553
  2410
    const char* lang, char **words, bool full
edouard@1553
  2411
)
edouard@1553
  2412
{
edouard@1553
  2413
    assert(session);
edouard@1553
  2414
    assert(msg);
edouard@1553
  2415
    assert(received_by);
edouard@1553
  2416
    assert(received_by->address);
edouard@1553
  2417
    assert(lang);
edouard@1553
  2418
    assert(words);
edouard@1553
  2419
edouard@1553
  2420
    if (!(session && 
edouard@1553
  2421
          msg &&
edouard@1553
  2422
          received_by && 
edouard@1553
  2423
          received_by->address && 
edouard@1553
  2424
          lang && 
edouard@1553
  2425
          words))
edouard@1553
  2426
        return PEP_ILLEGAL_VALUE;
edouard@1553
  2427
    
edouard@1557
  2428
    pEp_identity* partner = NULL;
edouard@1553
  2429
     
edouard@1553
  2430
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1553
  2431
    
edouard@1553
  2432
    *words = NULL;
edouard@1553
  2433
edouard@1553
  2434
    // We want fingerprint of key that did sign the message
edouard@1553
  2435
edouard@1553
  2436
    if (keylist == NULL) {
edouard@1553
  2437
edouard@1553
  2438
        // Message is to be decrypted
roker@1554
  2439
        message *dst = NULL;
edouard@1553
  2440
        stringlist_t *_keylist = keylist;
edouard@1553
  2441
        PEP_rating rating;
edouard@1553
  2442
        PEP_decrypt_flags_t flags;
edouard@1553
  2443
        status = decrypt_message( session, msg, &dst, &_keylist, &rating, &flags);
edouard@1553
  2444
edouard@1553
  2445
        if (status != PEP_STATUS_OK) {
edouard@1553
  2446
            free_message(dst);
edouard@1553
  2447
            free_stringlist(_keylist);
edouard@1553
  2448
            return status;
edouard@1553
  2449
        }
edouard@1553
  2450
edouard@1553
  2451
        if (dst && dst->from && _keylist) {
edouard@1553
  2452
            partner = identity_dup(dst->from); 
edouard@1553
  2453
            if(partner){
edouard@1553
  2454
                free(partner->fpr);
edouard@1553
  2455
                partner->fpr = strdup(_keylist->value);
edouard@1553
  2456
                if (partner->fpr == NULL)
edouard@1553
  2457
                    status = PEP_OUT_OF_MEMORY;
edouard@1553
  2458
            } else {
edouard@1553
  2459
                status = PEP_OUT_OF_MEMORY;
edouard@1553
  2460
            }
edouard@1553
  2461
        } else {
edouard@1553
  2462
            status = PEP_UNKNOWN_ERROR;
edouard@1553
  2463
        }
edouard@1553
  2464
edouard@1553
  2465
        free_message(dst);
edouard@1553
  2466
        free_stringlist(_keylist);
edouard@1553
  2467
edouard@1553
  2468
    } else {
edouard@1553
  2469
edouard@1553
  2470
        // Message already decrypted
edouard@1553
  2471
        if (keylist->value) {
edouard@1553
  2472
            partner = identity_dup(msg->from); 
edouard@1553
  2473
            if(partner){
edouard@1553
  2474
                free(partner->fpr);
edouard@1553
  2475
                partner->fpr = strdup(keylist->value);
edouard@1553
  2476
                if (partner->fpr == NULL)
edouard@1553
  2477
                    status = PEP_OUT_OF_MEMORY;
edouard@1553
  2478
            } else {
edouard@1553
  2479
                status = PEP_OUT_OF_MEMORY;
edouard@1553
  2480
            }
edouard@1553
  2481
        } else {
edouard@1553
  2482
            status = PEP_ILLEGAL_VALUE;
edouard@1553
  2483
        }
edouard@1553
  2484
    }
edouard@1553
  2485
edouard@1553
  2486
    if (status != PEP_STATUS_OK) {
edouard@1553
  2487
        free_identity(partner);
roker@1853
  2488
        return ADD_TO_LOG(status);
edouard@1553
  2489
    }
edouard@1553
  2490
   
edouard@1553
  2491
    // Find own identity corresponding to given account address.
edouard@1553
  2492
    // In that case we want default key attached to own identity
edouard@1553
  2493
    pEp_identity *stored_identity = NULL;
edouard@1553
  2494
    status = get_identity(session,
edouard@1553
  2495
                          received_by->address,
edouard@1553
  2496
                          PEP_OWN_USERID,
edouard@1553
  2497
                          &stored_identity);
edouard@1553
  2498
edouard@1553
  2499
    if (status != PEP_STATUS_OK) {
edouard@1553
  2500
        free_identity(stored_identity);
roker@1853
  2501
        return ADD_TO_LOG(status);
edouard@1553
  2502
    }
edouard@1553
  2503
edouard@1553
  2504
    // get the trustwords
edouard@1553
  2505
    size_t wsize;
edouard@1553
  2506
    status = get_trustwords(session, 
edouard@1553
  2507
                            partner, received_by, 
edouard@1553
  2508
                            lang, words, &wsize, full);
edouard@1553
  2509
roker@1853
  2510
    return ADD_TO_LOG(status);
edouard@1553
  2511
}
krista@1325
  2512
krista@1325
  2513
DYNAMIC_API PEP_STATUS MIME_decrypt_message(
krista@1325
  2514
    PEP_SESSION session,
krista@1325
  2515
    const char *mimetext,
krista@1325
  2516
    size_t size,
krista@1325
  2517
    char** mime_plaintext,
krista@1325
  2518
    stringlist_t **keylist,
krista@1325
  2519
    PEP_rating *rating,
krista@1325
  2520
    PEP_decrypt_flags_t *flags
krista@1427
  2521
)
krista@1325
  2522
{
roker@1741
  2523
    assert(mimetext);
roker@1741
  2524
    assert(mime_plaintext);
roker@1741
  2525
    assert(keylist);
roker@1741
  2526
    assert(rating);
roker@1741
  2527
    assert(flags);
roker@1741
  2528
krista@1325
  2529
    PEP_STATUS status = PEP_STATUS_OK;
krista@1325
  2530
    message* tmp_msg = NULL;
krista@1325
  2531
    message* dec_msg = NULL;
krista@1774
  2532
    *mime_plaintext = NULL;
krista@1427
  2533
krista@1325
  2534
    status = mime_decode_message(mimetext, size, &tmp_msg);
krista@1325
  2535
    if (status != PEP_STATUS_OK)
roker@1754
  2536
        GOTO(pep_error);
krista@1427
  2537
krista@1644
  2538
    PEP_STATUS decrypt_status = decrypt_message(session,
krista@1644
  2539
                                                tmp_msg,
krista@1644
  2540
                                                &dec_msg,
krista@1644
  2541
                                                keylist,
krista@1644
  2542
                                                rating,
krista@1644
  2543
                                                flags);
krista@1743
  2544
                                                
krista@1773
  2545
    if (!dec_msg && (decrypt_status == PEP_UNENCRYPTED || decrypt_status == PEP_VERIFIED)) {
krista@1773
  2546
        dec_msg = message_dup(tmp_msg);
krista@1743
  2547
    }
krista@1743
  2548
        
krista@1783
  2549
    if (decrypt_status > PEP_CANNOT_DECRYPT_UNKNOWN || !dec_msg)
roker@1708
  2550
    {
roker@1708
  2551
        status = decrypt_status;
roker@1754
  2552
        GOTO(pep_error);
roker@1708
  2553
    }
roker@1708
  2554
krista@1325
  2555
    status = mime_encode_message(dec_msg, false, mime_plaintext);
krista@1427
  2556
krista@1644
  2557
    if (status == PEP_STATUS_OK)
roker@1708
  2558
    {
roker@1708
  2559
        free(tmp_msg);
krista@1773
  2560
        free(dec_msg);
roker@1853
  2561
        return ADD_TO_LOG(decrypt_status);
roker@1708
  2562
    }
roker@1708
  2563
    
krista@1325
  2564
pep_error:
krista@1325
  2565
    free_message(tmp_msg);
krista@1773
  2566
    free_message(dec_msg);
krista@1427
  2567
roker@1853
  2568
    return ADD_TO_LOG(status);
krista@1325
  2569
}
krista@1325
  2570
krista@1641
  2571
krista@1325
  2572
DYNAMIC_API PEP_STATUS MIME_encrypt_message(
krista@1325
  2573
    PEP_SESSION session,
krista@1325
  2574
    const char *mimetext,
krista@1325
  2575
    size_t size,
krista@1325
  2576
    stringlist_t* extra,
krista@1325
  2577
    char** mime_ciphertext,
krista@1325
  2578
    PEP_enc_format enc_format,
krista@1325
  2579
    PEP_encrypt_flags_t flags
krista@1427
  2580
)
krista@1325
  2581
{
krista@1325
  2582
    PEP_STATUS status = PEP_STATUS_OK;
krista@1325
  2583
    message* tmp_msg = NULL;
krista@1325
  2584
    message* enc_msg = NULL;
krista@1427
  2585
krista@1325
  2586
    status = mime_decode_message(mimetext, size, &tmp_msg);
krista@1325
  2587
    if (status != PEP_STATUS_OK)
roker@1734
  2588
        GOTO(pep_error);