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