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