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