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