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