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