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