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