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