src/message_api.c
author Krista Bennett <krista@pep-project.org>
Wed, 25 Oct 2017 12:16:27 +0200
changeset 2204 765c6ff31183
parent 2200 644d22d740f0
child 2205 1cb06c1241bd
child 2207 5a57a595349e
permissions -rw-r--r--
Quick fix for blank Outlook message IDs for inner messages. Will have to be fixed in adapter later.
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@2204
   778
        if (status != PEP_STATUS_OK)
krista@2204
   779
            goto enomem;
krista@2176
   780
        
krista@2152
   781
        attachment->longmsg = encapsulate_message_wrap_info("INNER", attachment->longmsg);
krista@2152
   782
        _envelope->longmsg = encapsulate_message_wrap_info("OUTER", _envelope->longmsg);
krista@2121
   783
    }
krista@2152
   784
    else {
krista@2152
   785
        _envelope->longmsg = encapsulate_message_wrap_info("TRANSPORT", _envelope->longmsg);
krista@2152
   786
    }
krista@2204
   787
    
krista@2204
   788
    if (!attachment->id || attachment->id[0] == "\0") {
krista@2204
   789
        free(attachment->id);
krista@2204
   790
        if (!_envelope->id) {
krista@2204
   791
            status = generate_message_id(_envelope);
krista@2204
   792
        
krista@2204
   793
            if (status != PEP_STATUS_OK)
krista@2204
   794
                goto enomem;
krista@2204
   795
        }
krista@2204
   796
            
krista@2204
   797
        attachment->id = strdup(_envelope->id);
krista@2204
   798
    }
krista@2204
   799
    
krista@2121
   800
    char* message_text = NULL;
krista@2190
   801
krista@2190
   802
    /* prevent introduction of pEp in inner message */
krista@2190
   803
krista@2188
   804
    if (!attachment->shortmsg) {
krista@2188
   805
        attachment->shortmsg = strdup("");
krista@2188
   806
        if (!attachment->shortmsg)
krista@2188
   807
            goto enomem;
krista@2188
   808
    }
krista@2190
   809
            
krista@2121
   810
    /* Turn message into a MIME-blob */
krista@2200
   811
    status = _mime_encode_message_internal(attachment, false, &message_text, false);
krista@2188
   812
        
krista@2188
   813
    if (status != PEP_STATUS_OK)
krista@2188
   814
        goto enomem;
krista@2121
   815
    
krista@2121
   816
    size_t message_len = strlen(message_text);
krista@2121
   817
    
krista@2121
   818
    bloblist_t* message_blob = new_bloblist(message_text, message_len,
krista@2121
   819
                                            "message/rfc822", NULL);
krista@2121
   820
    
krista@2152
   821
    _envelope->attachments = message_blob;
krista@2168
   822
    if (keep_orig_subject && attachment->shortmsg)
krista@2168
   823
        _envelope->shortmsg = strdup(attachment->shortmsg);
krista@2152
   824
    return _envelope;
krista@2188
   825
    
krista@2188
   826
enomem:
krista@2188
   827
    if (!envelope) {
krista@2204
   828
        free_message(_envelope);
krista@2188
   829
    }
krista@2192
   830
    return NULL;    
krista@2121
   831
}
krista@2121
   832
vb@260
   833
static PEP_STATUS encrypt_PGP_MIME(
vb@311
   834
    PEP_SESSION session,
vb@311
   835
    const message *src,
vb@311
   836
    stringlist_t *keys,
krista@1639
   837
    message *dst,
krista@1639
   838
    PEP_encrypt_flags_t flags
vb@260
   839
    )
vb@260
   840
{
vb@260
   841
    PEP_STATUS status = PEP_STATUS_OK;
krista@1987
   842
    bool free_ptext = false;
vb@468
   843
    char *ptext = NULL;
Edouard@882
   844
    char *ctext = NULL;
vb@260
   845
    char *mimetext = NULL;
vb@260
   846
    size_t csize;
vb@260
   847
    assert(dst->longmsg == NULL);
vb@260
   848
    dst->enc_format = PEP_enc_PGP_MIME;
krista@2048
   849
krista@2120
   850
    if (src->shortmsg)
krista@2120
   851
        dst->shortmsg = strdup(src->shortmsg);
krista@2120
   852
        
vb@260
   853
    message *_src = calloc(1, sizeof(message));
vb@260
   854
    assert(_src);
vb@260
   855
    if (_src == NULL)
vb@260
   856
        goto enomem;
krista@2155
   857
//    _src->longmsg = ptext;
krista@2155
   858
    _src->longmsg = src->longmsg;
vb@260
   859
    _src->longmsg_formatted = src->longmsg_formatted;
vb@260
   860
    _src->attachments = src->attachments;
vb@260
   861
    _src->enc_format = PEP_enc_none;
krista@2200
   862
    status = _mime_encode_message_internal(_src, true, &mimetext, false);
vb@260
   863
    assert(status == PEP_STATUS_OK);
krista@853
   864
    if (status != PEP_STATUS_OK)
krista@853
   865
        goto pep_error;
krista@1427
   866
krista@1987
   867
    if (free_ptext){
krista@1987
   868
        free(ptext);
krista@1987
   869
        free_ptext=0;
krista@1987
   870
    }
vb@260
   871
    free(_src);
vb@260
   872
    assert(mimetext);
vb@260
   873
    if (mimetext == NULL)
vb@260
   874
        goto pep_error;
vb@260
   875
krista@1639
   876
    if (flags & PEP_encrypt_flag_force_unsigned)
krista@1639
   877
        status = encrypt_only(session, keys, mimetext, strlen(mimetext),
krista@1639
   878
            &ctext, &csize);
krista@1639
   879
    else
krista@1639
   880
        status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
krista@1639
   881
            &ctext, &csize);
vb@260
   882
    free(mimetext);
vb@260
   883
    if (ctext == NULL)
vb@260
   884
        goto pep_error;
vb@260
   885
vb@260
   886
    dst->longmsg = strdup("this message was encrypted with p≡p "
lix@487
   887
        "https://pEp-project.org");
vb@469
   888
    assert(dst->longmsg);
vb@260
   889
    if (dst->longmsg == NULL)
vb@260
   890
        goto enomem;
vb@260
   891
vb@260
   892
    char *v = strdup("Version: 1");
vb@469
   893
    assert(v);
vb@260
   894
    if (v == NULL)
vb@260
   895
        goto enomem;
vb@260
   896
roker@801
   897
    bloblist_t *_a = new_bloblist(v, strlen(v), "application/pgp-encrypted", NULL);
vb@260
   898
    if (_a == NULL)
vb@260
   899
        goto enomem;
vb@260
   900
    dst->attachments = _a;
vb@288
   901
Edouard@882
   902
    _a = bloblist_add(_a, ctext, csize, "application/octet-stream",
krista@1873
   903
        "file://msg.asc");
vb@260
   904
    if (_a == NULL)
vb@260
   905
        goto enomem;
vb@260
   906
vb@260
   907
    return PEP_STATUS_OK;
vb@260
   908
vb@260
   909
enomem:
vb@260
   910
    status = PEP_OUT_OF_MEMORY;
vb@260
   911
vb@260
   912
pep_error:
vb@260
   913
    if (free_ptext)
vb@260
   914
        free(ptext);
Edouard@882
   915
    free(ctext);
vb@260
   916
    return status;
vb@260
   917
}
vb@260
   918
vb@311
   919
vb@1004
   920
static PEP_rating _rating(PEP_comm_type ct, PEP_rating rating)
vb@311
   921
{
vb@311
   922
    if (ct == PEP_ct_unknown)
vb@311
   923
        return PEP_rating_undefined;
krista@1427
   924
krista@1243
   925
    else if (ct == PEP_ct_key_not_found)
krista@1243
   926
        return PEP_rating_have_no_key;
krista@1427
   927
vb@311
   928
    else if (ct == PEP_ct_compromized)
vb@311
   929
        return PEP_rating_under_attack;
vb@311
   930
Edouard@442
   931
    else if (ct == PEP_ct_mistrusted)
Edouard@442
   932
        return PEP_rating_mistrust;
krista@1427
   933
vb@1004
   934
    if (rating == PEP_rating_unencrypted_for_some)
vb@486
   935
        return PEP_rating_unencrypted_for_some;
vb@486
   936
vb@486
   937
    if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel ||
vb@486
   938
            ct == PEP_ct_my_key_not_included) {
vb@1004
   939
        if (rating > PEP_rating_unencrypted_for_some)
vb@486
   940
            return PEP_rating_unencrypted_for_some;
vb@486
   941
        else
vb@486
   942
            return PEP_rating_unencrypted;
vb@486
   943
    }
vb@486
   944
vb@1004
   945
    if (rating == PEP_rating_unencrypted)
vb@486
   946
        return PEP_rating_unencrypted_for_some;
vb@486
   947
vb@486
   948
    if (ct >= PEP_ct_confirmed_enc_anon)
vb@311
   949
        return PEP_rating_trusted_and_anonymized;
vb@311
   950
vb@311
   951
    else if (ct >= PEP_ct_strong_encryption)
vb@311
   952
        return PEP_rating_trusted;
vb@311
   953
vb@311
   954
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@311
   955
        return PEP_rating_reliable;
vb@311
   956
vb@311
   957
    else
vb@311
   958
        return PEP_rating_unreliable;
vb@311
   959
}
vb@311
   960
vb@311
   961
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@311
   962
{
vb@311
   963
    assert(blob);
vb@311
   964
krista@1873
   965
    if (blob == NULL || blob->filename == NULL || is_cid_uri(blob->filename))
vb@311
   966
        return false;
krista@1427
   967
roker@1559
   968
    char *ext = strrchr(blob->filename, '.');
vb@311
   969
    if (ext == NULL)
vb@311
   970
        return false;
vb@311
   971
vb@320
   972
    if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
vb@311
   973
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@311
   974
            strcmp(ext, ".asc") == 0)
vb@311
   975
            return true;
vb@311
   976
    }
vb@320
   977
    else if (strcmp(blob->mime_type, "text/plain") == 0) {
vb@311
   978
        if (strcmp(ext, ".asc") == 0)
vb@311
   979
            return true;
vb@311
   980
    }
vb@311
   981
vb@311
   982
    return false;
vb@311
   983
}
vb@311
   984
vb@311
   985
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@311
   986
{
vb@311
   987
    assert(blob);
vb@311
   988
    assert(blob->filename);
krista@1873
   989
    if (blob == NULL || blob->filename == NULL || is_cid_uri(blob->filename))
krista@853
   990
        return false;
vb@311
   991
krista@1873
   992
    const char* bare_filename_ptr = _get_resource_ptr_noown(blob->filename);
krista@1873
   993
    if (strncmp(bare_filename_ptr, "PGPexch.htm.", 12) == 0) {
krista@1873
   994
        if (strcmp(bare_filename_ptr + 11, ".pgp") == 0 ||
krista@1873
   995
            strcmp(bare_filename_ptr + 11, ".asc") == 0)
vb@311
   996
            return true;
vb@311
   997
    }
vb@311
   998
vb@311
   999
    return false;
vb@311
  1000
}
vb@311
  1001
vb@311
  1002
static char * without_double_ending(const char *filename)
vb@311
  1003
{
vb@311
  1004
    assert(filename);
krista@1873
  1005
    if (filename == NULL || is_cid_uri(filename))
krista@853
  1006
        return NULL;
krista@1427
  1007
roker@862
  1008
    char *ext = strrchr(filename, '.');
vb@311
  1009
    if (ext == NULL)
vb@311
  1010
        return NULL;
vb@311
  1011
vb@469
  1012
    char *result = strndup(filename, ext - filename);
vb@469
  1013
    assert(result);
vb@469
  1014
    return result;
vb@311
  1015
}
vb@311
  1016
vb@1004
  1017
static PEP_rating decrypt_rating(PEP_STATUS status)
vb@311
  1018
{
vb@311
  1019
    switch (status) {
vb@311
  1020
    case PEP_UNENCRYPTED:
vb@311
  1021
    case PEP_VERIFIED:
vb@311
  1022
    case PEP_VERIFY_NO_KEY:
vb@311
  1023
    case PEP_VERIFIED_AND_TRUSTED:
vb@311
  1024
        return PEP_rating_unencrypted;
vb@311
  1025
vb@311
  1026
    case PEP_DECRYPTED:
krista@1748
  1027
    case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH:
vb@311
  1028
        return PEP_rating_unreliable;
vb@311
  1029
vb@311
  1030
    case PEP_DECRYPTED_AND_VERIFIED:
vb@311
  1031
        return PEP_rating_reliable;
vb@311
  1032
vb@311
  1033
    case PEP_DECRYPT_NO_KEY:
vb@311
  1034
        return PEP_rating_have_no_key;
vb@311
  1035
vb@311
  1036
    case PEP_DECRYPT_WRONG_FORMAT:
vb@311
  1037
    case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@311
  1038
        return PEP_rating_cannot_decrypt;
vb@311
  1039
vb@311
  1040
    default:
vb@311
  1041
        return PEP_rating_undefined;
vb@311
  1042
    }
vb@311
  1043
}
vb@311
  1044
vb@1004
  1045
static PEP_rating key_rating(PEP_SESSION session, const char *fpr)
vb@311
  1046
{
vb@311
  1047
vb@311
  1048
    assert(session);
vb@311
  1049
    assert(fpr);
krista@1427
  1050
krista@853
  1051
    if (session == NULL || fpr == NULL)
krista@853
  1052
        return PEP_rating_undefined;
vb@311
  1053
edouard@1632
  1054
edouard@1632
  1055
    PEP_comm_type bare_comm_type = PEP_ct_unknown;
edouard@1876
  1056
    PEP_comm_type resulting_comm_type = PEP_ct_unknown;
krista@1615
  1057
    PEP_STATUS status = get_key_rating(session, fpr, &bare_comm_type);
vb@311
  1058
    if (status != PEP_STATUS_OK)
vb@311
  1059
        return PEP_rating_undefined;
vb@311
  1060
edouard@1876
  1061
    PEP_comm_type least_comm_type = PEP_ct_unknown;
edouard@1876
  1062
    least_trust(session, fpr, &least_comm_type);
edouard@1876
  1063
edouard@1876
  1064
    if (least_comm_type == PEP_ct_unknown) {
edouard@1876
  1065
        resulting_comm_type = bare_comm_type;
edouard@1876
  1066
    } else if (least_comm_type < PEP_ct_strong_but_unconfirmed ||
edouard@1876
  1067
               bare_comm_type < PEP_ct_strong_but_unconfirmed) {
edouard@1876
  1068
        // take minimum if anything bad
edouard@1876
  1069
        resulting_comm_type = least_comm_type < bare_comm_type ? 
edouard@1876
  1070
                              least_comm_type : 
edouard@1876
  1071
                              bare_comm_type;
edouard@1632
  1072
    } else {
edouard@1876
  1073
        resulting_comm_type = least_comm_type;
edouard@1632
  1074
    }
edouard@1876
  1075
    return _rating(resulting_comm_type, PEP_rating_undefined);
vb@311
  1076
}
vb@311
  1077
krista@1579
  1078
static PEP_rating worst_rating(PEP_rating rating1, PEP_rating rating2) {
krista@1579
  1079
    return ((rating1 < rating2) ? rating1 : rating2);
krista@1579
  1080
}
krista@1579
  1081
edouard@1876
  1082
static PEP_rating keylist_rating(PEP_SESSION session, stringlist_t *keylist, char* sender_fpr, PEP_rating sender_rating)
vb@311
  1083
{
edouard@1876
  1084
    PEP_rating rating = sender_rating;
vb@311
  1085
vb@311
  1086
    assert(keylist && keylist->value);
vb@311
  1087
    if (keylist == NULL || keylist->value == NULL)
vb@486
  1088
        return PEP_rating_undefined;
vb@311
  1089
vb@311
  1090
    stringlist_t *_kl;
vb@311
  1091
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
edouard@1876
  1092
edouard@1876
  1093
        // Ignore own fpr
edouard@1876
  1094
        if(_same_fpr(sender_fpr, strlen(sender_fpr), _kl->value, strlen(_kl->value)))
edouard@1876
  1095
            continue;
vb@311
  1096
vb@1004
  1097
        PEP_rating _rating_ = key_rating(session, _kl->value);
krista@1579
  1098
         
vb@1004
  1099
        if (_rating_ <= PEP_rating_mistrust)
vb@1004
  1100
            return _rating_;
krista@1610
  1101
            
edouard@1876
  1102
        if (_rating_ == PEP_rating_unencrypted)
edouard@1876
  1103
        {
vb@1004
  1104
            if (rating > PEP_rating_unencrypted_for_some)
krista@1579
  1105
                rating = worst_rating(rating, PEP_rating_unencrypted_for_some);
vb@311
  1106
        }
edouard@1876
  1107
        else
edouard@1876
  1108
        {
edouard@1876
  1109
            rating = worst_rating(rating, _rating_);
edouard@1876
  1110
        }
vb@311
  1111
    }
vb@311
  1112
vb@1004
  1113
    return rating;
vb@311
  1114
}
vb@311
  1115
vb@311
  1116
static PEP_comm_type _get_comm_type(
vb@311
  1117
    PEP_SESSION session,
vb@311
  1118
    PEP_comm_type max_comm_type,
vb@311
  1119
    pEp_identity *ident
vb@311
  1120
    )
vb@311
  1121
{
vb@311
  1122
    PEP_STATUS status = update_identity(session, ident);
vb@311
  1123
vb@311
  1124
    if (max_comm_type == PEP_ct_compromized)
vb@311
  1125
        return PEP_ct_compromized;
krista@1427
  1126
Edouard@510
  1127
    if (max_comm_type == PEP_ct_mistrusted)
Edouard@510
  1128
        return PEP_ct_mistrusted;
vb@311
  1129
vb@311
  1130
    if (status == PEP_STATUS_OK) {
vb@311
  1131
        if (ident->comm_type == PEP_ct_compromized)
vb@311
  1132
            return PEP_ct_compromized;
Edouard@510
  1133
        else if (ident->comm_type == PEP_ct_mistrusted)
Edouard@510
  1134
            return PEP_ct_mistrusted;
vb@311
  1135
        else
vb@951
  1136
            return _MIN(max_comm_type, ident->comm_type);
vb@311
  1137
    }
vb@311
  1138
    else {
vb@311
  1139
        return PEP_ct_unknown;
vb@311
  1140
    }
vb@311
  1141
}
vb@311
  1142
vb@731
  1143
static void free_bl_entry(bloblist_t *bl)
vb@731
  1144
{
vb@731
  1145
    if (bl) {
vb@731
  1146
        free(bl->value);
vb@731
  1147
        free(bl->mime_type);
vb@731
  1148
        free(bl->filename);
vb@731
  1149
        free(bl);
vb@731
  1150
    }
vb@731
  1151
}
vb@731
  1152
vb@731
  1153
static bool is_key(const bloblist_t *bl)
vb@731
  1154
{
Edouard@728
  1155
    return (// workaround for Apple Mail bugs
Edouard@728
  1156
            (is_mime_type(bl, "application/x-apple-msg-attachment") &&
Edouard@728
  1157
             is_fileending(bl, ".asc")) ||
Edouard@728
  1158
            // as binary, by file name
Edouard@728
  1159
            ((bl->mime_type == NULL ||
Edouard@728
  1160
              is_mime_type(bl, "application/octet-stream")) &&
Edouard@728
  1161
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
  1162
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))) ||
krista@1427
  1163
            // explicit mime type
Edouard@728
  1164
            is_mime_type(bl, "application/pgp-keys") ||
Edouard@728
  1165
            // as text, by file name
Edouard@728
  1166
            (is_mime_type(bl, "text/plain") &&
Edouard@728
  1167
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
  1168
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc")))
Edouard@728
  1169
           );
vb@731
  1170
}
vb@731
  1171
vb@731
  1172
static void remove_attached_keys(message *msg)
vb@731
  1173
{
vb@731
  1174
    if (msg) {
vb@731
  1175
        bloblist_t *last = NULL;
vb@731
  1176
        for (bloblist_t *bl = msg->attachments; bl && bl->value; ) {
vb@731
  1177
            bloblist_t *next = bl->next;
vb@731
  1178
vb@731
  1179
            if (is_key(bl)) {
vb@731
  1180
                if (last) {
vb@731
  1181
                    last->next = next;
vb@731
  1182
                }
vb@731
  1183
                else {
vb@731
  1184
                    msg->attachments = next;
vb@731
  1185
                }
vb@731
  1186
                free_bl_entry(bl);
vb@731
  1187
            }
vb@731
  1188
            else {
vb@731
  1189
                last = bl;
vb@731
  1190
            }
vb@731
  1191
            bl = next;
vb@731
  1192
        }
vb@731
  1193
    }
vb@731
  1194
}
vb@731
  1195
Edouard@734
  1196
bool import_attached_keys(
krista@1427
  1197
        PEP_SESSION session,
Edouard@728
  1198
        const message *msg,
Edouard@728
  1199
        identity_list **private_idents
Edouard@728
  1200
    )
vb@311
  1201
{
vb@311
  1202
    assert(session);
vb@311
  1203
    assert(msg);
krista@1427
  1204
krista@853
  1205
    if (session == NULL || msg == NULL)
krista@853
  1206
        return false;
vb@311
  1207
vb@731
  1208
    bool remove = false;
vb@731
  1209
vb@908
  1210
    int i = 0;
roker@1559
  1211
    for (bloblist_t *bl = msg->attachments; i < MAX_KEYS_TO_IMPORT && bl && bl->value;
krista@1427
  1212
            bl = bl->next, i++)
Edouard@747
  1213
    {
vb@908
  1214
        if (bl && bl->value && bl->size && bl->size < MAX_KEY_SIZE
krista@1427
  1215
                && is_key(bl))
Edouard@728
  1216
        {
edouard@1745
  1217
            identity_list *local_private_idents = NULL;
edouard@1745
  1218
            import_key(session, bl->value, bl->size, &local_private_idents);
vb@731
  1219
            remove = true;
edouard@1863
  1220
            if (private_idents && *private_idents == NULL && local_private_idents != NULL)
edouard@1745
  1221
                *private_idents = local_private_idents;
edouard@1745
  1222
            else
edouard@1745
  1223
                free_identity_list(local_private_idents);
vb@311
  1224
        }
vb@311
  1225
    }
vb@731
  1226
    return remove;
vb@311
  1227
}
vb@311
  1228
Edouard@694
  1229
Edouard@694
  1230
PEP_STATUS _attach_key(PEP_SESSION session, const char* fpr, message *msg)
vb@311
  1231
{
roker@1559
  1232
    char *keydata = NULL;
vb@311
  1233
    size_t size;
vb@311
  1234
Edouard@694
  1235
    PEP_STATUS status = export_key(session, fpr, &keydata, &size);
Edouard@694
  1236
    assert(status == PEP_STATUS_OK);
Edouard@694
  1237
    if (status != PEP_STATUS_OK)
Edouard@694
  1238
        return status;
Edouard@694
  1239
    assert(size);
krista@1427
  1240
roker@1559
  1241
     bloblist_t *bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
krista@1873
  1242
                      "file://pEpkey.asc");
krista@1427
  1243
Edouard@694
  1244
    if (msg->attachments == NULL && bl)
Edouard@694
  1245
        msg->attachments = bl;
Edouard@694
  1246
Edouard@694
  1247
    return PEP_STATUS_OK;
Edouard@694
  1248
}
Edouard@694
  1249
Edouard@694
  1250
#define ONE_WEEK (7*24*3600)
Edouard@694
  1251
Edouard@694
  1252
void attach_own_key(PEP_SESSION session, message *msg)
Edouard@694
  1253
{
vb@311
  1254
    assert(session);
vb@311
  1255
    assert(msg);
krista@1427
  1256
Edouard@558
  1257
    if (msg->dir == PEP_dir_incoming)
Edouard@558
  1258
        return;
Edouard@558
  1259
vb@311
  1260
    assert(msg->from && msg->from->fpr);
vb@311
  1261
    if (msg->from == NULL || msg->from->fpr == NULL)
vb@311
  1262
        return;
vb@311
  1263
Edouard@694
  1264
    if(_attach_key(session, msg->from->fpr, msg) != PEP_STATUS_OK)
vb@311
  1265
        return;
krista@1427
  1266
Edouard@694
  1267
    char *revoked_fpr = NULL;
Edouard@694
  1268
    uint64_t revocation_date = 0;
krista@1427
  1269
Edouard@694
  1270
    if(get_revoked(session, msg->from->fpr,
Edouard@694
  1271
                   &revoked_fpr, &revocation_date) == PEP_STATUS_OK &&
Edouard@694
  1272
       revoked_fpr != NULL)
Edouard@694
  1273
    {
Edouard@694
  1274
        time_t now = time(NULL);
krista@1427
  1275
Edouard@694
  1276
        if (now < (time_t)revocation_date + ONE_WEEK)
Edouard@694
  1277
        {
Edouard@694
  1278
            _attach_key(session, revoked_fpr, msg);
Edouard@694
  1279
        }
Edouard@694
  1280
    }
Edouard@694
  1281
    free(revoked_fpr);
vb@311
  1282
}
vb@311
  1283
vb@311
  1284
PEP_cryptotech determine_encryption_format(message *msg)
vb@311
  1285
{
vb@311
  1286
    assert(msg);
krista@1427
  1287
vb@311
  1288
    if (is_PGP_message_text(msg->longmsg)) {
vb@311
  1289
        msg->enc_format = PEP_enc_pieces;
vb@311
  1290
        return PEP_crypt_OpenPGP;
vb@311
  1291
    }
vb@311
  1292
    else if (msg->attachments && msg->attachments->next &&
vb@311
  1293
            is_mime_type(msg->attachments, "application/pgp-encrypted") &&
vb@311
  1294
            is_PGP_message_text(msg->attachments->next->value)
vb@311
  1295
        ) {
vb@311
  1296
        msg->enc_format = PEP_enc_PGP_MIME;
vb@311
  1297
        return PEP_crypt_OpenPGP;
vb@311
  1298
    }
edouard@1557
  1299
    else if (msg->attachments && msg->attachments->next &&
edouard@1557
  1300
            is_mime_type(msg->attachments->next, "application/pgp-encrypted") &&
edouard@1557
  1301
            is_PGP_message_text(msg->attachments->value)
edouard@1557
  1302
        ) {
edouard@1557
  1303
        msg->enc_format = PEP_enc_PGP_MIME_Outlook1;
edouard@1557
  1304
        return PEP_crypt_OpenPGP;
edouard@1557
  1305
    }
vb@311
  1306
    else {
vb@311
  1307
        msg->enc_format = PEP_enc_none;
vb@311
  1308
        return PEP_crypt_none;
vb@311
  1309
    }
vb@311
  1310
}
vb@311
  1311
vb@48
  1312
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
  1313
        PEP_SESSION session,
vb@113
  1314
        message *src,
vb@37
  1315
        stringlist_t * extra,
vb@38
  1316
        message **dst,
vb@939
  1317
        PEP_enc_format enc_format,
vb@939
  1318
        PEP_encrypt_flags_t flags
vb@37
  1319
    )
vb@37
  1320
{
vb@37
  1321
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
  1322
    message * msg = NULL;
vb@63
  1323
    stringlist_t * keys = NULL;
krista@2121
  1324
    message* _src = src;
krista@2121
  1325
    
vb@37
  1326
    assert(session);
vb@37
  1327
    assert(src);
vb@37
  1328
    assert(dst);
Edouard@558
  1329
    assert(enc_format != PEP_enc_none);
vb@81
  1330
Edouard@558
  1331
    if (!(session && src && dst && enc_format != PEP_enc_none))
roker@1853
  1332
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
  1333
Edouard@550
  1334
    if (src->dir == PEP_dir_incoming)
roker@1853
  1335
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
krista@1427
  1336
vb@259
  1337
    determine_encryption_format(src);
vb@260
  1338
    if (src->enc_format != PEP_enc_none)
roker@1853
  1339
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@259
  1340
vb@37
  1341
    *dst = NULL;
vb@67
  1342
vb@236
  1343
    status = myself(session, src->from);
vb@236
  1344
    if (status != PEP_STATUS_OK)
roker@1734
  1345
        GOTO(pep_error);
Edouard@558
  1346
vb@80
  1347
    keys = new_stringlist(src->from->fpr);
vb@63
  1348
    if (keys == NULL)
vb@63
  1349
        goto enomem;
vb@37
  1350
vb@39
  1351
    stringlist_t *_k = keys;
vb@39
  1352
vb@39
  1353
    if (extra) {
vb@39
  1354
        _k = stringlist_append(_k, extra);
vb@63
  1355
        if (_k == NULL)
vb@63
  1356
            goto enomem;
vb@37
  1357
    }
vb@39
  1358
vb@299
  1359
    bool dest_keys_found = true;
Edouard@510
  1360
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@299
  1361
vb@37
  1362
    identity_list * _il;
krista@1427
  1363
Edouard@545
  1364
    if ((_il = src->bcc) && _il->ident)
Edouard@545
  1365
    {
Edouard@545
  1366
        // BCC limited support:
Edouard@545
  1367
        //     - App splits mails with BCC in multiple mails.
Edouard@545
  1368
        //     - Each email is encrypted separately
krista@1427
  1369
Edouard@789
  1370
        if(_il->next || (src->to && src->to->ident) || (src->cc && src->cc->ident))
Edouard@545
  1371
        {
Edouard@545
  1372
            // Only one Bcc with no other recipient allowed for now
Edouard@545
  1373
            return PEP_ILLEGAL_VALUE;
Edouard@545
  1374
        }
krista@1427
  1375
vb@299
  1376
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
  1377
        if (_status != PEP_STATUS_OK) {
vb@299
  1378
            status = _status;
roker@1734
  1379
            GOTO(pep_error);
vb@299
  1380
        }
krista@1427
  1381
vb@299
  1382
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@39
  1383
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
  1384
            if (_k == NULL)
vb@63
  1385
                goto enomem;
Edouard@510
  1386
            max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@510
  1387
                                           _il->ident);
vb@37
  1388
        }
vb@299
  1389
        else {
vb@299
  1390
            dest_keys_found = false;
vb@37
  1391
            status = PEP_KEY_NOT_FOUND;
krista@1427
  1392
        }
vb@299
  1393
    }
Edouard@545
  1394
    else
Edouard@545
  1395
    {
Edouard@545
  1396
        for (_il = src->to; _il && _il->ident; _il = _il->next) {
Edouard@545
  1397
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1398
            if (_status != PEP_STATUS_OK) {
Edouard@545
  1399
                status = _status;
roker@1734
  1400
                GOTO(pep_error);
Edouard@545
  1401
            }
vb@299
  1402
Edouard@545
  1403
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1404
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1405
                if (_k == NULL)
Edouard@545
  1406
                    goto enomem;
Edouard@545
  1407
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1408
                                               _il->ident);
Edouard@545
  1409
            }
Edouard@545
  1410
            else {
Edouard@545
  1411
                dest_keys_found = false;
Edouard@545
  1412
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1413
            }
vb@299
  1414
        }
vb@299
  1415
Edouard@545
  1416
        for (_il = src->cc; _il && _il->ident; _il = _il->next) {
Edouard@545
  1417
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1418
            if (_status != PEP_STATUS_OK)
Edouard@545
  1419
            {
Edouard@545
  1420
                status = _status;
roker@1734
  1421
                GOTO(pep_error);
Edouard@545
  1422
            }
Edouard@545
  1423
Edouard@545
  1424
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1425
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1426
                if (_k == NULL)
Edouard@545
  1427
                    goto enomem;
Edouard@545
  1428
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1429
                                               _il->ident);
Edouard@545
  1430
            }
Edouard@545
  1431
            else {
Edouard@545
  1432
                dest_keys_found = false;
Edouard@545
  1433
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1434
            }
vb@299
  1435
        }
vb@37
  1436
    }
krista@1427
  1437
Edouard@510
  1438
    if (!dest_keys_found ||
krista@1640
  1439
        stringlist_length(keys)  == 0 ||
Edouard@510
  1440
        _rating(max_comm_type,
Edouard@510
  1441
                PEP_rating_undefined) < PEP_rating_reliable)
Edouard@510
  1442
    {
vb@301
  1443
        free_stringlist(keys);
edouard@1899
  1444
        if (!session->passive_mode && 
edouard@1899
  1445
            !(flags & PEP_encrypt_flag_force_no_attached_key)) {
vb@465
  1446
            attach_own_key(session, src);
edouard@1899
  1447
            decorate_message(src, PEP_rating_undefined, NULL);
edouard@1899
  1448
        }
roker@1853
  1449
        return ADD_TO_LOG(PEP_UNENCRYPTED);
vb@301
  1450
    }
vb@301
  1451
    else {
krista@2152
  1452
        // FIXME - we need to deal with transport types (via flag)
krista@2152
  1453
        if ((max_comm_type | PEP_ct_confirmed) == PEP_ct_pEp) {
krista@2168
  1454
            _src = wrap_message_as_attachment(NULL, src, session->unencrypted_subject);
krista@2176
  1455
            if (!_src)
krista@2176
  1456
                goto pep_error;
krista@2121
  1457
        }
krista@2157
  1458
        else {
krista@2157
  1459
            // hide subject
krista@2168
  1460
            if (!session->unencrypted_subject) {
krista@2168
  1461
                status = replace_subject(_src);
krista@2168
  1462
                if (status == PEP_OUT_OF_MEMORY)
krista@2168
  1463
                    goto enomem;
krista@2168
  1464
            }
krista@2157
  1465
        }
krista@2152
  1466
        if (!(flags & PEP_encrypt_flag_force_no_attached_key))
krista@2152
  1467
            attach_own_key(session, _src);
krista@2152
  1468
krista@2121
  1469
        msg = clone_to_empty_message(_src);
vb@299
  1470
        if (msg == NULL)
vb@299
  1471
            goto enomem;
vb@299
  1472
vb@81
  1473
        switch (enc_format) {
krista@2121
  1474
            case PEP_enc_PGP_MIME:
krista@2121
  1475
            case PEP_enc_PEP: // BUG: should be implemented extra
krista@2121
  1476
                status = encrypt_PGP_MIME(session, _src, keys, msg, flags);
krista@2121
  1477
                break;
krista@2121
  1478
krista@2121
  1479
            // case PEP_enc_pieces:
krista@2121
  1480
            //     status = encrypt_PGP_in_pieces(session, src, keys, msg, flags);
krista@2121
  1481
            //     break;
krista@2121
  1482
krista@2121
  1483
            /* case PEP_enc_PEP:
krista@2121
  1484
                // TODO: implement
krista@2121
  1485
                NOT_IMPLEMENTED */
krista@2121
  1486
krista@2121
  1487
            default:
krista@2121
  1488
                assert(0);
krista@2121
  1489
                status = PEP_ILLEGAL_VALUE;
krista@2121
  1490
                GOTO(pep_error);
vb@37
  1491
        }
krista@1427
  1492
Edouard@392
  1493
        if (status == PEP_OUT_OF_MEMORY)
Edouard@392
  1494
            goto enomem;
krista@1427
  1495
vb@480
  1496
        if (status != PEP_STATUS_OK)
roker@1734
  1497
            GOTO(pep_error);
vb@37
  1498
    }
vb@37
  1499
vb@37
  1500
    free_stringlist(keys);
vb@63
  1501
vb@469
  1502
    if (msg && msg->shortmsg == NULL) {
krista@2121
  1503
        msg->shortmsg = strdup("");
vb@469
  1504
        assert(msg->shortmsg);
krista@853
  1505
        if (msg->shortmsg == NULL)
krista@853
  1506
            goto enomem;
vb@469
  1507
    }
vb@64
  1508
edouard@1542
  1509
    if (msg) {
vb@311
  1510
        decorate_message(msg, PEP_rating_undefined, NULL);
krista@2176
  1511
        if (_src->id) {
krista@2176
  1512
            msg->id = strdup(_src->id);
edouard@1542
  1513
            assert(msg->id);
edouard@1542
  1514
            if (msg->id == NULL)
edouard@1542
  1515
                goto enomem;
edouard@1542
  1516
        }
edouard@1542
  1517
    }
vb@311
  1518
vb@63
  1519
    *dst = msg;
krista@2121
  1520
    
krista@2121
  1521
    // ??? FIXME: Check to be sure we don't have references btw _src and msg. 
krista@2121
  1522
    // I don't think we do.
krista@2121
  1523
    if (_src && _src != src)
krista@2121
  1524
        free_message(_src);
krista@2121
  1525
        
roker@1853
  1526
    return ADD_TO_LOG(status);
vb@63
  1527
vb@63
  1528
enomem:
vb@63
  1529
    status = PEP_OUT_OF_MEMORY;
vb@63
  1530
vb@63
  1531
pep_error:
vb@63
  1532
    free_stringlist(keys);
vb@63
  1533
    free_message(msg);
krista@2121
  1534
    if (_src && _src != src)
krista@2121
  1535
        free_message(_src);
krista@2035
  1536
roker@1853
  1537
    return ADD_TO_LOG(status);
vb@37
  1538
}
vb@37
  1539
krista@995
  1540
DYNAMIC_API PEP_STATUS encrypt_message_for_self(
krista@992
  1541
        PEP_SESSION session,
krista@992
  1542
        pEp_identity* target_id,
krista@992
  1543
        message *src,
krista@992
  1544
        message **dst,
markus@1633
  1545
        PEP_enc_format enc_format,
markus@1633
  1546
        PEP_encrypt_flags_t flags
krista@992
  1547
    )
krista@992
  1548
{
krista@992
  1549
    PEP_STATUS status = PEP_STATUS_OK;
krista@992
  1550
    message * msg = NULL;
krista@992
  1551
    stringlist_t * keys = NULL;
krista@2185
  1552
    message* _src = src;
krista@992
  1553
krista@992
  1554
    assert(session);
krista@992
  1555
    assert(src);
krista@992
  1556
    assert(dst);
krista@992
  1557
    assert(enc_format != PEP_enc_none);
krista@992
  1558
krista@992
  1559
    if (!(session && src && dst && enc_format != PEP_enc_none))
roker@1853
  1560
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
krista@992
  1561
krista@992
  1562
    if (src->dir == PEP_dir_incoming)
roker@1853
  1563
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
krista@1427
  1564
krista@992
  1565
    determine_encryption_format(src);
krista@992
  1566
    if (src->enc_format != PEP_enc_none)
roker@1853
  1567
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
krista@992
  1568
krista@995
  1569
    status = myself(session, target_id);
krista@995
  1570
    if (status != PEP_STATUS_OK)
roker@1734
  1571
        GOTO(pep_error);
krista@995
  1572
krista@992
  1573
    *dst = NULL;
krista@992
  1574
krista@1427
  1575
krista@992
  1576
    PEP_STATUS _status = update_identity(session, target_id);
krista@992
  1577
    if (_status != PEP_STATUS_OK) {
krista@992
  1578
        status = _status;
krista@992
  1579
        goto pep_error;
krista@992
  1580
    }
krista@992
  1581
krista@994
  1582
    char* target_fpr = target_id->fpr;
krista@994
  1583
    if (!target_fpr)
krista@994
  1584
        return PEP_KEY_NOT_FOUND; // FIXME: Error condition
krista@1685
  1585
 
krista@994
  1586
    keys = new_stringlist(target_fpr);
krista@1685
  1587
    
krista@1640
  1588
    /* KG: did we ever do this??? */
krista@1640
  1589
    if (!(flags & PEP_encrypt_flag_force_no_attached_key))
krista@1685
  1590
        _attach_key(session, target_fpr, src);
krista@1427
  1591
krista@2185
  1592
    _src = wrap_message_as_attachment(NULL, src, session->unencrypted_subject);
krista@2185
  1593
    if (!_src)
krista@2185
  1594
        goto pep_error;
krista@2185
  1595
krista@2185
  1596
    msg = clone_to_empty_message(_src);
krista@992
  1597
    if (msg == NULL)
krista@992
  1598
        goto enomem;
krista@992
  1599
krista@992
  1600
    switch (enc_format) {
krista@992
  1601
        case PEP_enc_PGP_MIME:
krista@992
  1602
        case PEP_enc_PEP: // BUG: should be implemented extra
krista@2185
  1603
            status = encrypt_PGP_MIME(session, _src, keys, msg, flags);
krista@992
  1604
            break;
krista@992
  1605
krista@992
  1606
        default:
krista@992
  1607
            assert(0);
krista@992
  1608
            status = PEP_ILLEGAL_VALUE;
krista@992
  1609
            goto pep_error;
krista@992
  1610
    }
krista@1427
  1611
krista@992
  1612
    if (status == PEP_OUT_OF_MEMORY)
krista@992
  1613
        goto enomem;
krista@1427
  1614
krista@992
  1615
    if (status != PEP_STATUS_OK)
krista@992
  1616
        goto pep_error;
krista@992
  1617
krista@994
  1618
     if (msg && msg->shortmsg == NULL) {
krista@2045
  1619
         msg->shortmsg = _pep_subj_copy();
krista@994
  1620
         assert(msg->shortmsg);
krista@994
  1621
         if (msg->shortmsg == NULL)
krista@994
  1622
             goto enomem;
krista@994
  1623
     }
krista@992
  1624
edouard@1542
  1625
     if (msg) {
krista@2185
  1626
         if (_src->id) {
krista@2185
  1627
             msg->id = strdup(_src->id);
edouard@1542
  1628
             assert(msg->id);
edouard@1542
  1629
             if (msg->id == NULL)
edouard@1542
  1630
                 goto enomem;
edouard@1542
  1631
         }
edouard@1542
  1632
     }
edouard@1542
  1633
krista@992
  1634
    *dst = msg;
krista@2185
  1635
    
krista@2185
  1636
    if (src != _src)
krista@2185
  1637
        free_message(_src);
krista@2185
  1638
krista@992
  1639
    return status;
krista@992
  1640
krista@992
  1641
enomem:
krista@992
  1642
    status = PEP_OUT_OF_MEMORY;
krista@992
  1643
krista@992
  1644
pep_error:
krista@992
  1645
    free_stringlist(keys);
krista@992
  1646
    free_message(msg);
krista@2185
  1647
    if (src != _src)
krista@2185
  1648
        free_message(_src);
krista@992
  1649
roker@1853
  1650
    return ADD_TO_LOG(status);
krista@992
  1651
}
krista@992
  1652
vb@781
  1653
static bool is_a_pEpmessage(const message *msg)
vb@781
  1654
{
vb@781
  1655
    for (stringpair_list_t *i = msg->opt_fields; i && i->value ; i=i->next) {
vb@781
  1656
        if (strcasecmp(i->value->key, "X-pEp-Version") == 0)
vb@781
  1657
            return true;
vb@781
  1658
    }
vb@781
  1659
    return false;
vb@781
  1660
}
vb@781
  1661
vb@781
  1662
// update comm_type to pEp_ct_pEp if needed
krista@2035
  1663
Edouard@858
  1664
static PEP_STATUS _update_identity_for_incoming_message(
vb@781
  1665
        PEP_SESSION session,
vb@781
  1666
        const message *src
vb@781
  1667
    )
vb@781
  1668
{
Edouard@858
  1669
    PEP_STATUS status;
Edouard@858
  1670
    if (src->from && src->from->address) {
Edouard@858
  1671
        status = update_identity(session, src->from);
krista@1192
  1672
        if (status == PEP_STATUS_OK
Edouard@858
  1673
                && is_a_pEpmessage(src)
vb@781
  1674
                && src->from->comm_type >= PEP_ct_OpenPGP_unconfirmed
vb@781
  1675
                && src->from->comm_type != PEP_ct_pEp_unconfirmed
vb@781
  1676
                && src->from->comm_type != PEP_ct_pEp)
vb@781
  1677
        {
vb@781
  1678
            src->from->comm_type |= PEP_ct_pEp_unconfirmed;
krista@1823
  1679
            status = set_identity(session, src->from);
vb@781
  1680
        }
Edouard@858
  1681
        return status;
vb@781
  1682
    }
Edouard@858
  1683
    return PEP_ILLEGAL_VALUE;
vb@781
  1684
}
vb@781
  1685
krista@1397
  1686
krista@2106
  1687
static PEP_STATUS _get_detached_signature(message* msg, 
krista@2106
  1688
                                          bloblist_t** signature_blob) {
krista@1397
  1689
    bloblist_t* attach_curr = msg->attachments;
krista@1427
  1690
krista@1427
  1691
    *signature_blob = NULL;
krista@1427
  1692
krista@1397
  1693
    while (attach_curr) {
krista@1427
  1694
        if (strcasecmp(attach_curr->mime_type, "application/pgp-signature") == 0) {
krista@1397
  1695
            *signature_blob = attach_curr;
krista@1397
  1696
            break;
krista@1397
  1697
        }
krista@1397
  1698
        attach_curr = attach_curr->next;
krista@1397
  1699
    }
krista@1427
  1700
krista@1427
  1701
    return PEP_STATUS_OK;
krista@1427
  1702
}
krista@1427
  1703
krista@2106
  1704
static PEP_STATUS _get_signed_text(const char* ptext, const size_t psize,
krista@2106
  1705
                                   char** stext, size_t* ssize) {
krista@1427
  1706
krista@1427
  1707
    char* signed_boundary = NULL;
krista@1427
  1708
    char* signpost = strstr(ptext, "Content-Type: multipart/signed");
krista@1427
  1709
krista@1427
  1710
    *ssize = 0;
krista@1427
  1711
    *stext = NULL;
krista@1427
  1712
krista@1427
  1713
    if (!signpost)
krista@1427
  1714
        return PEP_UNKNOWN_ERROR;
krista@1427
  1715
krista@1427
  1716
    char* curr_line = signpost;
krista@1427
  1717
//    const char* end_text = ptext + psize;
krista@1755
  1718
    const char* boundary_key = "boundary=";
krista@1755
  1719
    const size_t BOUNDARY_KEY_SIZE = 9;
krista@1427
  1720
krista@1427
  1721
    char* start_boundary = strstr(curr_line, boundary_key);
krista@1427
  1722
    if (!start_boundary)
krista@1427
  1723
        return PEP_UNKNOWN_ERROR;
krista@1427
  1724
krista@1427
  1725
    start_boundary += BOUNDARY_KEY_SIZE;
krista@1427
  1726
krista@1755
  1727
    bool quoted = (*start_boundary == '"');
krista@1755
  1728
krista@1755
  1729
    if (quoted)
krista@1755
  1730
        start_boundary++;
krista@1755
  1731
        
krista@1755
  1732
    char* end_boundary = (quoted ? strstr(start_boundary, "\"") : strstr(start_boundary, ";")); // FIXME: third possiblity is CRLF, or?
krista@1427
  1733
krista@1427
  1734
    if (!end_boundary)
krista@1427
  1735
        return PEP_UNKNOWN_ERROR;
krista@1427
  1736
krista@1755
  1737
    // Add space for the "--"
krista@1427
  1738
    size_t boundary_strlen = (end_boundary - start_boundary) + 2;
krista@1427
  1739
krista@2176
  1740
    signed_boundary = calloc(boundary_strlen + 1, 1);
krista@1427
  1741
    strlcpy(signed_boundary, "--", boundary_strlen + 1);
krista@1427
  1742
    strlcat(signed_boundary, start_boundary, boundary_strlen + 1);
krista@1427
  1743
krista@1427
  1744
    start_boundary = strstr(end_boundary, signed_boundary);
krista@1427
  1745
krista@1427
  1746
    if (!start_boundary)
krista@1427
  1747
        return PEP_UNKNOWN_ERROR;
krista@1427
  1748
krista@1427
  1749
    start_boundary += boundary_strlen;
krista@1427
  1750
krista@1756
  1751
    if (*start_boundary == '\r') {
krista@1756
  1752
        if (*(start_boundary + 1) == '\n')
krista@1756
  1753
            start_boundary += 2;
krista@1756
  1754
    }
krista@1756
  1755
    else if (*start_boundary == '\n')
krista@1427
  1756
        start_boundary++;
krista@1427
  1757
krista@1427
  1758
    end_boundary = strstr(start_boundary + boundary_strlen, signed_boundary);
krista@1427
  1759
krista@1427
  1760
    if (!end_boundary)
krista@1427
  1761
        return PEP_UNKNOWN_ERROR;
krista@1427
  1762
edouard@1809
  1763
    // See RFC3156 section 5...
edouard@1809
  1764
    end_boundary--; 
edouard@1809
  1765
    if (*(end_boundary - 1) == '\r')
edouard@1809
  1766
        end_boundary--; 
krista@1427
  1767
krista@1427
  1768
    *ssize = end_boundary - start_boundary;
krista@1427
  1769
    *stext = start_boundary;
krista@1427
  1770
    free(signed_boundary);
krista@1427
  1771
krista@1397
  1772
    return PEP_STATUS_OK;
krista@1397
  1773
}
krista@1397
  1774
krista@2106
  1775
static PEP_STATUS combine_keylists(PEP_SESSION session, stringlist_t** verify_in, 
krista@2106
  1776
                                   stringlist_t** keylist_in_out, 
krista@2106
  1777
                                   pEp_identity* from) {
krista@1484
  1778
    
krista@1484
  1779
    if (!verify_in || !(*verify_in)) // this isn't really a problem.
krista@1484
  1780
        return PEP_STATUS_OK;
krista@1484
  1781
    
krista@1484
  1782
    stringlist_t* orig_verify = *verify_in;
krista@1484
  1783
    
roker@1559
  1784
    stringlist_t* verify_curr = NULL;
roker@1559
  1785
    stringlist_t* from_keys = NULL;
krista@1484
  1786
    
krista@1484
  1787
    /* FIXME: what to do if head needs to be null */
krista@1484
  1788
    PEP_STATUS status = find_keys(session, from->address, &from_keys);
krista@1484
  1789
    
krista@1484
  1790
    stringlist_t* from_fpr_node = NULL;
krista@1484
  1791
    stringlist_t* from_curr;
krista@1484
  1792
    
krista@1484
  1793
    for (from_curr = from_keys; from_curr; from_curr = from_curr->next) {
krista@1484
  1794
        for (verify_curr = orig_verify; verify_curr; verify_curr = verify_curr->next) {
krista@1484
  1795
            if (from_curr->value && verify_curr->value &&
edouard@1573
  1796
                _same_fpr(from_curr->value, strlen(from_curr->value),
edouard@1573
  1797
                          verify_curr->value, strlen(verify_curr->value))) {
krista@1484
  1798
                from_fpr_node = from_curr;
krista@1484
  1799
                break;
krista@1484
  1800
            }
krista@1484
  1801
        }
krista@1484
  1802
    }
krista@1484
  1803
    
krista@1484
  1804
    if (!from_fpr_node) {
krista@1484
  1805
        status = PEP_KEY_NOT_FOUND;
krista@1484
  1806
        goto free;
krista@1484
  1807
    }
krista@1484
  1808
krista@1484
  1809
    verify_curr = orig_verify;
krista@1484
  1810
    
krista@1484
  1811
    /* put "from" signer at the beginning of the list */
edouard@1573
  1812
    if (!_same_fpr(orig_verify->value, strlen(orig_verify->value),
edouard@1573
  1813
                   from_fpr_node->value, strlen(from_fpr_node->value))) {
krista@1484
  1814
        orig_verify = stringlist_delete(orig_verify, from_fpr_node->value);
krista@1484
  1815
        verify_curr = new_stringlist(from_fpr_node->value);
krista@1484
  1816
        verify_curr->next = orig_verify;
krista@1484
  1817
    }
krista@1484
  1818
krista@1484
  1819
    /* append keylist to signers */
krista@1484
  1820
    if (keylist_in_out && *keylist_in_out && (*keylist_in_out)->value) {
krista@1484
  1821
        stringlist_t** tail_pp = &verify_curr->next;
krista@1484
  1822
        
krista@1484
  1823
        while (*tail_pp) {
krista@1484
  1824
            tail_pp = &((*tail_pp)->next);
krista@1484
  1825
        }
krista@1615
  1826
        stringlist_t* second_list = *keylist_in_out;
krista@1615
  1827
        if (second_list) {
krista@1615
  1828
            char* listhead_val = second_list->value;
krista@1615
  1829
            if (!listhead_val || listhead_val[0] == '\0') {
krista@1615
  1830
                /* remove head, basically. This can happen when,
krista@1615
  1831
                   for example, the signature is detached and
krista@1615
  1832
                   verification is not seen directly after
krista@1615
  1833
                   decryption, so no signer is presumed in
krista@1615
  1834
                   the first construction of the keylist */
krista@1615
  1835
                *keylist_in_out = (*keylist_in_out)->next;
krista@1615
  1836
                second_list->next = NULL;
krista@1615
  1837
                free_stringlist(second_list);
krista@1615
  1838
            }
krista@1615
  1839
        }
krista@1484
  1840
        *tail_pp = *keylist_in_out;
krista@1484
  1841
    }
krista@1484
  1842
    
krista@1484
  1843
    *keylist_in_out = verify_curr;
krista@1484
  1844
    
krista@1484
  1845
    status = PEP_STATUS_OK;
krista@1484
  1846
    
krista@1484
  1847
free:
krista@1484
  1848
    free_stringlist(from_keys);
roker@1559
  1849
    return status;
krista@1484
  1850
}
krista@1484
  1851
krista@2106
  1852
static PEP_STATUS amend_rating_according_to_sender_and_recipients(
krista@2106
  1853
       PEP_SESSION session,
krista@2106
  1854
       PEP_rating *rating,
krista@2106
  1855
       pEp_identity *sender,
krista@2106
  1856
       stringlist_t *recipients) {
edouard@1815
  1857
    
edouard@1815
  1858
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1815
  1859
edouard@1815
  1860
    if (*rating > PEP_rating_mistrust) {
edouard@1876
  1861
edouard@1876
  1862
        if (recipients == NULL) {
edouard@1876
  1863
            *rating = PEP_rating_undefined;
edouard@1876
  1864
            return PEP_STATUS_OK;
edouard@1815
  1865
        }
edouard@1876
  1866
edouard@1876
  1867
        char *fpr = recipients->value;
edouard@1876
  1868
edouard@1876
  1869
        if (!(sender && sender->user_id && sender->user_id[0] && fpr && fpr[0])) {
edouard@1815
  1870
            *rating = PEP_rating_unreliable;
edouard@1815
  1871
        }
edouard@1876
  1872
        else {
edouard@1876
  1873
            pEp_identity *_sender = new_identity(sender->address, fpr,
edouard@1876
  1874
                                               sender->user_id, sender->username);
edouard@1876
  1875
            if (_sender == NULL)
edouard@1876
  1876
                return PEP_OUT_OF_MEMORY;
edouard@1876
  1877
edouard@1876
  1878
            status = get_trust(session, _sender);
edouard@1876
  1879
            if (_sender->comm_type != PEP_ct_unknown) {
edouard@1876
  1880
                *rating = keylist_rating(session, recipients, 
edouard@1876
  1881
                            fpr, _rating(_sender->comm_type, 
edouard@1876
  1882
                                          PEP_rating_undefined));
edouard@1815
  1883
            }
edouard@1876
  1884
            free_identity(_sender);
edouard@1876
  1885
            if (status == PEP_CANNOT_FIND_IDENTITY)
edouard@1876
  1886
               status = PEP_STATUS_OK;
edouard@1815
  1887
        }
edouard@1815
  1888
    }
edouard@1815
  1889
    return status;
edouard@1815
  1890
}
edouard@1815
  1891
krista@2097
  1892
static PEP_STATUS check_for_sync_msg(PEP_SESSION session, 
krista@2092
  1893
                                     message* src,
krista@2092
  1894
                                     PEP_rating* rating, 
krista@2097
  1895
                                     PEP_decrypt_flags_t* flags,
krista@2092
  1896
                                     stringlist_t** keylist) {
krista@2092
  1897
    assert(session);
krista@2092
  1898
    assert(src);
krista@2092
  1899
    assert(rating);
krista@2092
  1900
    assert(keylist);
krista@2092
  1901
    
krista@2092
  1902
    if (session->sync_session->inject_sync_msg){
krista@2097
  1903
        PEP_STATUS status = receive_DeviceState_msg(session, src, *rating, *keylist);
krista@2092
  1904
        if (status == PEP_MESSAGE_CONSUME ||
krista@2092
  1905
            status == PEP_MESSAGE_IGNORE) {
krista@2092
  1906
            *flags |= (status == PEP_MESSAGE_IGNORE) ?
krista@2092
  1907
                        PEP_decrypt_flag_ignore :
krista@2092
  1908
                        PEP_decrypt_flag_consume;
krista@2092
  1909
        }
krista@2092
  1910
        else if (status != PEP_STATUS_OK) {
krista@2092
  1911
            return ADD_TO_LOG(status);
krista@2092
  1912
        }
krista@2092
  1913
    }
krista@2092
  1914
    return PEP_STATUS_OK;
krista@2092
  1915
}
krista@2092
  1916
krista@2092
  1917
static PEP_STATUS sync_if_no_key(PEP_STATUS decrypt_status, PEP_SESSION session) {
krista@2092
  1918
    if (decrypt_status == PEP_DECRYPT_NO_KEY) {
krista@2092
  1919
        PEP_STATUS sync_status = inject_DeviceState_event(session, CannotDecrypt, NULL, NULL);
krista@2092
  1920
        if (sync_status == PEP_OUT_OF_MEMORY){
krista@2092
  1921
            return PEP_OUT_OF_MEMORY;
krista@2092
  1922
        }
krista@2092
  1923
    }
krista@2092
  1924
    return PEP_STATUS_OK;
krista@2092
  1925
}
krista@2092
  1926
krista@2092
  1927
// FIXME: Do we need to remove the attachment? I think we do...
krista@2092
  1928
static bool pull_up_attached_main_msg(message* src) {
krista@2092
  1929
    char* slong = src->longmsg;
krista@2092
  1930
    char* sform = src->longmsg_formatted;
krista@2092
  1931
    bloblist_t* satt = src->attachments;
krista@2092
  1932
    
krista@2092
  1933
    if ((!slong || slong[0] == '\0')
krista@2092
  1934
         && (!sform || sform[0] == '\0')) {
krista@2092
  1935
        if (satt) {
krista@2092
  1936
            const char* inner_mime_type = satt->mime_type;
krista@2092
  1937
            if (strcasecmp(inner_mime_type, "text/plain") == 0) {
krista@2092
  1938
                free(slong); /* in case of "" */
krista@2092
  1939
                src->longmsg = strndup(satt->value, satt->size); 
krista@2092
  1940
                
krista@2092
  1941
                bloblist_t* next_node = satt->next;
krista@2092
  1942
                if (next_node) {
krista@2092
  1943
                    inner_mime_type = next_node->mime_type;
krista@2092
  1944
                    if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@2092
  1945
                        free(sform);
krista@2092
  1946
                        src->longmsg_formatted = strndup(next_node->value, next_node->size);
krista@2092
  1947
                    }
krista@2092
  1948
                }
krista@2092
  1949
            }
krista@2092
  1950
            else if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@2092
  1951
                free(sform);
krista@2092
  1952
                src->longmsg_formatted = strndup(satt->value, satt->size);
krista@2092
  1953
            }
krista@2092
  1954
        }
krista@2092
  1955
        return true;
krista@2092
  1956
    }
krista@2092
  1957
    return false;
krista@2092
  1958
}
krista@2092
  1959
krista@2135
  1960
static PEP_STATUS unencapsulate_hidden_fields(message* src, message* msg,
krista@2122
  1961
                                              char** msg_wrap_info) {
krista@2155
  1962
    if (!src)
krista@2155
  1963
        return PEP_ILLEGAL_VALUE;
krista@2093
  1964
    unsigned char pepstr[] = PEP_SUBJ_STRING;
krista@2097
  1965
    PEP_STATUS status = PEP_STATUS_OK;
krista@2155
  1966
krista@2155
  1967
    bool change_source_in_place = (msg ? false : true);
krista@2155
  1968
    
krista@2155
  1969
    if (change_source_in_place)
krista@2155
  1970
        msg = src;
krista@2155
  1971
        
krista@2155
  1972
//    const char* version_string = get_message_version_string(src);
krista@2093
  1973
    
krista@2092
  1974
    switch (src->enc_format) {
krista@2092
  1975
        case PEP_enc_PGP_MIME:
krista@2092
  1976
        case PEP_enc_pieces:
krista@2092
  1977
        case PEP_enc_PGP_MIME_Outlook1:
krista@2155
  1978
//        case PEP_enc_none: // FIXME - this is wrong
krista@2155
  1979
krista@2155
  1980
            if (!change_source_in_place)
krista@2155
  1981
                status = copy_fields(msg, src);
krista@2155
  1982
                
krista@2092
  1983
            if (status != PEP_STATUS_OK)
krista@2092
  1984
                return status;
krista@2155
  1985
                
krista@2137
  1986
            // FIXME: This is a mess. Talk with VB about how far we go to identify
krista@2137
  1987
            if (is_a_pEpmessage(src) || (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0 ||
krista@2137
  1988
                _unsigned_signed_strcmp(pepstr, src->shortmsg, PEP_SUBJ_BYTELEN) == 0))
krista@2092
  1989
            {
krista@2135
  1990
                char * shortmsg = NULL;
krista@2135
  1991
                char * longmsg = NULL;
krista@2155
  1992
        
krista@2135
  1993
                if (msg->longmsg) {
krista@2135
  1994
                    int r = separate_short_and_long(msg->longmsg, 
krista@2135
  1995
                                                    &shortmsg, 
krista@2135
  1996
                                                    msg_wrap_info,
krista@2135
  1997
                                                    &longmsg);
krista@2135
  1998
                
krista@2135
  1999
                    if (r == -1)
krista@2135
  2000
                        return PEP_OUT_OF_MEMORY;
krista@2135
  2001
                }
krista@2092
  2002
krista@2122
  2003
                // We only use the shortmsg in version 1.0 messages; if it occurs where we
krista@2122
  2004
                // didn't replace the subject, we ignore this all
krista@2155
  2005
                if (!(*msg_wrap_info || change_source_in_place)) {
krista@2122
  2006
                    if (!shortmsg || 
krista@2122
  2007
                        (src->shortmsg != NULL && strcmp(src->shortmsg, "pEp") != 0 &&
krista@2122
  2008
                         _unsigned_signed_strcmp(pepstr, src->shortmsg, PEP_SUBJ_BYTELEN) != 0)) {
krista@2122
  2009
                             
krista@2122
  2010
                        if (shortmsg != NULL)
krista@2122
  2011
                            free(shortmsg);                        
krista@2155
  2012
                            
krista@2122
  2013
                        if (src->shortmsg == NULL) {
krista@2122
  2014
                            shortmsg = strdup("");
krista@2122
  2015
                        }
krista@2122
  2016
                        else {
krista@2122
  2017
                            // FIXME: is msg->shortmsg always a copy of
krista@2122
  2018
                            // src->shortmsg already?
krista@2122
  2019
                            // if so, we need to change the logic so
krista@2122
  2020
                            // that in this case, we don't free msg->shortmsg
krista@2155
  2021
                            // and do this strdup, etc
krista@2155
  2022
                            shortmsg = strdup(src->shortmsg);
krista@2122
  2023
                        }        
krista@2092
  2024
                    }
krista@2155
  2025
                    free(msg->shortmsg);
krista@2155
  2026
                    msg->shortmsg = shortmsg;
krista@2092
  2027
                }
krista@2122
  2028
                
krista@2092
  2029
                free(msg->longmsg);
krista@2092
  2030
krista@2092
  2031
                msg->longmsg = longmsg;
krista@2092
  2032
            }
krista@2092
  2033
            else {
krista@2155
  2034
                if (!change_source_in_place) {
krista@2155
  2035
                    msg->shortmsg = strdup(src->shortmsg);
krista@2155
  2036
                    assert(msg->shortmsg);
krista@2155
  2037
                    if (msg->shortmsg == NULL)
krista@2155
  2038
                        return PEP_OUT_OF_MEMORY;
krista@2155
  2039
                }
krista@2092
  2040
            }
krista@2092
  2041
            break;
krista@2092
  2042
        default:
krista@2092
  2043
                // BUG: must implement more
krista@2092
  2044
                NOT_IMPLEMENTED
krista@2092
  2045
    }
krista@2092
  2046
    return PEP_STATUS_OK;
krista@2092
  2047
krista@2092
  2048
}
krista@2092
  2049
krista@2106
  2050
static PEP_STATUS verify_decrypted(PEP_SESSION session,
krista@2106
  2051
                                   message* msg, 
krista@2106
  2052
                                   pEp_identity* sender,
krista@2106
  2053
                                   char* plaintext, 
krista@2106
  2054
                                   size_t plaintext_size,
krista@2106
  2055
                                   stringlist_t** keylist,
krista@2106
  2056
                                   PEP_STATUS* decrypt_status,
krista@2106
  2057
                                   PEP_cryptotech crypto) {
krista@2092
  2058
    bloblist_t* detached_sig = NULL;
krista@2092
  2059
    PEP_STATUS status = _get_detached_signature(msg, &detached_sig);
krista@2092
  2060
    
krista@2092
  2061
    if (detached_sig) {
krista@2092
  2062
        char* dsig_text = detached_sig->value;
krista@2092
  2063
        size_t dsig_size = detached_sig->size;
krista@2092
  2064
        size_t ssize = 0;
krista@2092
  2065
        char* stext = NULL;
krista@2092
  2066
krista@2097
  2067
        status = _get_signed_text(plaintext, plaintext_size, &stext, &ssize);
krista@2097
  2068
        stringlist_t *verify_keylist = NULL;
krista@2092
  2069
krista@2092
  2070
        if (ssize > 0 && stext) {
krista@2092
  2071
            status = cryptotech[crypto].verify_text(session, stext,
krista@2092
  2072
                                                    ssize, dsig_text, dsig_size,
krista@2097
  2073
                                                    &verify_keylist);
krista@2092
  2074
krista@2092
  2075
            if (status == PEP_VERIFIED || status == PEP_VERIFIED_AND_TRUSTED)
krista@2092
  2076
            {
krista@2092
  2077
                *decrypt_status = PEP_DECRYPTED_AND_VERIFIED;
krista@2092
  2078
            
krista@2105
  2079
                status = combine_keylists(session, &verify_keylist, keylist, sender);
krista@2092
  2080
            }
krista@2092
  2081
        }
krista@2092
  2082
    }
krista@2092
  2083
    return status;
krista@2092
  2084
}
krista@2092
  2085
krista@2097
  2086
static PEP_STATUS _decrypt_in_pieces(PEP_SESSION session, 
krista@2097
  2087
                                     message* src, 
krista@2097
  2088
                                     message** msg_ptr, 
krista@2097
  2089
                                     char* ptext,
krista@2092
  2090
                                     size_t psize) {
krista@2092
  2091
                            
krista@2092
  2092
    PEP_STATUS status = PEP_UNKNOWN_ERROR;
krista@2092
  2093
    
krista@2097
  2094
    *msg_ptr = clone_to_empty_message(src);
krista@2092
  2095
krista@2092
  2096
    if (*msg_ptr == NULL)
krista@2092
  2097
        return PEP_OUT_OF_MEMORY;
krista@2092
  2098
krista@2092
  2099
    message* msg = *msg_ptr;
krista@2092
  2100
krista@2092
  2101
    msg->longmsg = ptext;
krista@2092
  2102
    ptext = NULL;
krista@2092
  2103
krista@2092
  2104
    bloblist_t *_m = msg->attachments;
krista@2092
  2105
    if (_m == NULL && src->attachments && src->attachments->value) {
krista@2092
  2106
        msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
krista@2092
  2107
        _m = msg->attachments;
krista@2092
  2108
    }
krista@2092
  2109
krista@2092
  2110
    bloblist_t *_s;
krista@2092
  2111
    for (_s = src->attachments; _s; _s = _s->next) {
krista@2092
  2112
        if (_s->value == NULL && _s->size == 0){
krista@2092
  2113
            _m = bloblist_add(_m, NULL, 0, _s->mime_type, _s->filename);
krista@2092
  2114
            if (_m == NULL)
krista@2092
  2115
                return PEP_OUT_OF_MEMORY;
krista@2092
  2116
krista@2092
  2117
        }
krista@2092
  2118
        else if (is_encrypted_attachment(_s)) {
krista@2092
  2119
            stringlist_t *_keylist = NULL;
krista@2092
  2120
            char *attctext  = _s->value;
krista@2092
  2121
            size_t attcsize = _s->size;
krista@2092
  2122
krista@2092
  2123
            free(ptext);
krista@2092
  2124
            ptext = NULL;
krista@2092
  2125
krista@2092
  2126
            // FIXME: What about attachments with separate sigs???
krista@2092
  2127
            status = decrypt_and_verify(session, attctext, attcsize,
krista@2092
  2128
                                        NULL, 0,
krista@2092
  2129
                                        &ptext, &psize, &_keylist);
krista@2092
  2130
            free_stringlist(_keylist); // FIXME: Why do we do this?
krista@2092
  2131
krista@2092
  2132
            if (ptext) {
krista@2092
  2133
                if (is_encrypted_html_attachment(_s)) {
krista@2092
  2134
                    msg->longmsg_formatted = ptext;
krista@2092
  2135
                    ptext = NULL;
krista@2092
  2136
                }
krista@2092
  2137
                else {
krista@2092
  2138
                    static const char * const mime_type = "application/octet-stream";
krista@2092
  2139
                    char * const filename =
krista@2092
  2140
                        without_double_ending(_s->filename);
krista@2092
  2141
                    if (filename == NULL)
krista@2092
  2142
                        return PEP_OUT_OF_MEMORY;
krista@2092
  2143
krista@2092
  2144
                    _m = bloblist_add(_m, ptext, psize, mime_type,
krista@2092
  2145
                        filename);
krista@2092
  2146
                    free(filename);
krista@2092
  2147
                    if (_m == NULL)
krista@2092
  2148
                        return PEP_OUT_OF_MEMORY;
krista@2092
  2149
krista@2092
  2150
                    ptext = NULL;
krista@2092
  2151
krista@2092
  2152
                    if (msg->attachments == NULL)
krista@2092
  2153
                        msg->attachments = _m;
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
        else {
krista@2092
  2168
            char *copy = malloc(_s->size);
krista@2092
  2169
            assert(copy);
krista@2092
  2170
            if (copy == NULL)
krista@2092
  2171
                return PEP_OUT_OF_MEMORY;
krista@2092
  2172
            memcpy(copy, _s->value, _s->size);
krista@2092
  2173
            _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
krista@2092
  2174
            if (_m == NULL)
krista@2092
  2175
                return PEP_OUT_OF_MEMORY;
krista@2092
  2176
        }
krista@2092
  2177
    }
krista@2092
  2178
    return status;
krista@2092
  2179
}
krista@2048
  2180
krista@2097
  2181
static PEP_STATUS get_crypto_text(message* src, char** crypto_text, size_t* text_size) {
krista@2097
  2182
                
krista@2097
  2183
    // this is only here because of how NOT_IMPLEMENTED works            
krista@2097
  2184
    PEP_STATUS status = PEP_STATUS_OK;
krista@2097
  2185
                    
krista@2093
  2186
    switch (src->enc_format) {
krista@2093
  2187
        case PEP_enc_PGP_MIME:
krista@2093
  2188
            *crypto_text = src->attachments->next->value;
krista@2093
  2189
            *text_size = src->attachments->next->size;
krista@2093
  2190
            break;
krista@2093
  2191
krista@2093
  2192
        case PEP_enc_PGP_MIME_Outlook1:
krista@2093
  2193
            *crypto_text = src->attachments->value;
krista@2093
  2194
            *text_size = src->attachments->size;
krista@2093
  2195
            break;
krista@2093
  2196
krista@2093
  2197
        case PEP_enc_pieces:
krista@2093
  2198
            *crypto_text = src->longmsg;
krista@2097
  2199
            *text_size = strlen(*crypto_text);
krista@2093
  2200
            break;
krista@2093
  2201
krista@2093
  2202
        default:
krista@2093
  2203
            NOT_IMPLEMENTED
krista@2093
  2204
    }
krista@2097
  2205
    
krista@2097
  2206
    return status;
krista@2093
  2207
}
krista@2093
  2208
krista@2097
  2209
static PEP_STATUS import_priv_keys_from_decrypted_msg(PEP_SESSION session,
krista@2096
  2210
                                                      message* src, 
krista@2096
  2211
                                                      message* msg,
krista@2097
  2212
                                                      bool* imported_keys,
krista@2096
  2213
                                                      bool* imported_private,
krista@2096
  2214
                                                      identity_list** private_il) {
krista@2096
  2215
                                                          
krista@2096
  2216
    PEP_STATUS status = PEP_STATUS_OK;
krista@2096
  2217
    
krista@2096
  2218
    // check for private key in decrypted message attachment while importing
krista@2096
  2219
    identity_list *_private_il = NULL;
krista@2097
  2220
    *imported_keys = import_attached_keys(session, msg, &_private_il);
krista@2096
  2221
    
krista@2096
  2222
    if (_private_il && identity_list_length(_private_il) == 1 &&
krista@2096
  2223
        _private_il->ident->address)
krista@2096
  2224
        *imported_private = true;
krista@2096
  2225
krista@2096
  2226
    if (private_il && imported_private)
krista@2096
  2227
        *private_il = _private_il;
krista@2096
  2228
    else
krista@2096
  2229
        free_identity_list(_private_il);
krista@2096
  2230
krista@2096
  2231
    if (imported_keys)
krista@2096
  2232
        status = _update_identity_for_incoming_message(session, src);
krista@2096
  2233
        
krista@2096
  2234
    return status;
krista@2096
  2235
}
krista@1484
  2236
Edouard@741
  2237
DYNAMIC_API PEP_STATUS _decrypt_message(
vb@37
  2238
        PEP_SESSION session,
vb@113
  2239
        message *src,
vb@241
  2240
        message **dst,
vb@251
  2241
        stringlist_t **keylist,
vb@1004
  2242
        PEP_rating *rating,
krista@1427
  2243
        PEP_decrypt_flags_t *flags,
Edouard@741
  2244
        identity_list **private_il
vb@37
  2245
    )
vb@37
  2246
{
krista@2092
  2247
    
krista@2093
  2248
    assert(session);
krista@2093
  2249
    assert(src);
krista@2093
  2250
    assert(dst);
krista@2093
  2251
    assert(keylist);
krista@2093
  2252
    assert(rating);
krista@2093
  2253
    assert(flags);
krista@2093
  2254
krista@2093
  2255
    if (!(session && src && dst && keylist && rating && flags))
krista@2093
  2256
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
krista@2093
  2257
krista@2092
  2258
    /*** Begin init ***/
vb@37
  2259
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
  2260
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
  2261
    message *msg = NULL;
vb@112
  2262
    char *ctext;
vb@112
  2263
    size_t csize;
vb@269
  2264
    char *ptext = NULL;
vb@112
  2265
    size_t psize;
vb@241
  2266
    stringlist_t *_keylist = NULL;
krista@2093
  2267
krista@2093
  2268
    *dst = NULL;
krista@2093
  2269
    *keylist = NULL;
krista@2093
  2270
    *rating = PEP_rating_undefined;
vb@191
  2271
Edouard@739
  2272
    *flags = 0;
krista@2092
  2273
    /*** End init ***/
krista@2092
  2274
krista@2092
  2275
    /*** Begin Import any attached public keys and update identities accordingly ***/
Edouard@739
  2276
Edouard@734
  2277
    // Private key in unencrypted mail are ignored -> NULL
Edouard@734
  2278
    bool imported_keys = import_attached_keys(session, src, NULL);
Edouard@728
  2279
Edouard@728
  2280
    // Update src->from in case we just imported a key
Edouard@728
  2281
    // we would need to check signature
Edouard@858
  2282
    status = _update_identity_for_incoming_message(session, src);
Edouard@858
  2283
    if(status != PEP_STATUS_OK)
roker@1853
  2284
        return ADD_TO_LOG(status);
Edouard@858
  2285
krista@2092
  2286
    /*** End Import any attached public keys and update identities accordingly ***/
krista@2092
  2287
    
krista@2093
  2288
    /*** Begin get detached signatures that are attached to the encrypted message ***/
krista@1397
  2289
    // Get detached signature, if any
krista@1397
  2290
    bloblist_t* detached_sig = NULL;
krista@1397
  2291
    char* dsig_text = NULL;
krista@1397
  2292
    size_t dsig_size = 0;
krista@1397
  2293
    status = _get_detached_signature(src, &detached_sig);
krista@1397
  2294
    if (detached_sig) {
krista@1397
  2295
        dsig_text = detached_sig->value;
krista@1397
  2296
        dsig_size = detached_sig->size;
krista@1397
  2297
    }
krista@2093
  2298
    /*** End get detached signatures that are attached to the encrypted message ***/
krista@2092
  2299
krista@2092
  2300
    /*** Determine encryption format ***/
vb@259
  2301
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
  2302
krista@2093
  2303
    // Check for and deal with unencrypted messages
krista@2093
  2304
    if (src->enc_format == PEP_enc_none) {
krista@2093
  2305
krista@2093
  2306
        *rating = PEP_rating_unencrypted;
krista@2093
  2307
krista@2093
  2308
        if (imported_keys)
krista@2093
  2309
            remove_attached_keys(src);
krista@2093
  2310
krista@2097
  2311
        status = check_for_sync_msg(session, src, rating, flags, keylist);
krista@2093
  2312
        
krista@2093
  2313
        if (status != PEP_STATUS_OK)
krista@2093
  2314
            return ADD_TO_LOG(status);
krista@2093
  2315
                                    
krista@2093
  2316
        pull_up_attached_main_msg(src);
krista@2093
  2317
        
krista@2093
  2318
        return ADD_TO_LOG(PEP_UNENCRYPTED);
vb@259
  2319
    }
krista@2093
  2320
krista@2097
  2321
    status = get_crypto_text(src, &ctext, &csize);
krista@2097
  2322
    if (status != PEP_STATUS_OK)
krista@2097
  2323
        return status;
krista@2092
  2324
    
krista@2092
  2325
    /** Ok, we should be ready to decrypt. Try decrypt and verify first! **/
Edouard@431
  2326
    status = cryptotech[crypto].decrypt_and_verify(session, ctext,
krista@1427
  2327
                                                   csize, dsig_text, dsig_size,
krista@1397
  2328
                                                   &ptext, &psize, &_keylist);
krista@2092
  2329
krista@2092
  2330
    if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
roker@1729
  2331
        GOTO(pep_error);
edouard@1271
  2332
edouard@1273
  2333
    decrypt_status = status;
krista@2096
  2334
    
krista@2092
  2335
    /* inject appropriate sync message if we couldn't decrypt due to no key */
krista@2092
  2336
    if (sync_if_no_key(decrypt_status, session) == PEP_OUT_OF_MEMORY)
krista@2092
  2337
        goto pep_error;
vb@113
  2338
krista@1427
  2339
    bool imported_private_key_address = false;
Edouard@728
  2340
krista@2106
  2341
    if (ptext) { 
krista@2106
  2342
        /* we got a plaintext from decryption */
vb@256
  2343
        switch (src->enc_format) {
krista@2092
  2344
            
vb@256
  2345
            case PEP_enc_PGP_MIME:
edouard@1557
  2346
            case PEP_enc_PGP_MIME_Outlook1:
krista@2092
  2347
            
vb@269
  2348
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  2349
                if (status != PEP_STATUS_OK)
roker@1563
  2350
                    goto pep_error;
krista@1484
  2351
                
krista@2092
  2352
                /* Ensure messages whose maintext is in the attachments
krista@2092
  2353
                   move main text into message struct longmsg et al */
krista@2092
  2354
                if (pull_up_attached_main_msg(msg) && msg->shortmsg) {
krista@2092
  2355
                    free(src->shortmsg);
krista@2092
  2356
                    src->shortmsg = strdup(msg->shortmsg);
roker@1563
  2357
                }
krista@1484
  2358
krista@2092
  2359
                /* if decrypted, but not verified... */
krista@2092
  2360
                if (decrypt_status == PEP_DECRYPTED) {
krista@2096
  2361
                    
krista@2096
  2362
                    // check for private key in decrypted message attachment while importing
krista@2096
  2363
                    status = import_priv_keys_from_decrypted_msg(session, src, msg,
krista@2097
  2364
                                                                 &imported_keys,
krista@2096
  2365
                                                                 &imported_private_key_address,
krista@2096
  2366
                                                                 private_il);
krista@2096
  2367
                    if (status != PEP_STATUS_OK)
krista@2096
  2368
                        GOTO(pep_error);            
krista@2096
  2369
                                                                 
krista@2106
  2370
                    status = verify_decrypted(session,
krista@2106
  2371
                                              msg, src->from,
krista@2106
  2372
                                              ptext, psize,
krista@2106
  2373
                                              &_keylist,
krista@2106
  2374
                                              &decrypt_status,
krista@2106
  2375
                                              crypto);
krista@1427
  2376
                }
vb@256
  2377
                break;
vb@256
  2378
vb@256
  2379
            case PEP_enc_pieces:
krista@2097
  2380
                status = _decrypt_in_pieces(session, src, &msg, ptext, psize);
krista@2092
  2381
            
krista@2092
  2382
                if (status == PEP_OUT_OF_MEMORY)
vb@113
  2383
                    goto enomem;
vb@113
  2384
vb@256
  2385
                break;
vb@256
  2386
vb@256
  2387
            default:
vb@256
  2388
                // BUG: must implement more
vb@256
  2389
                NOT_IMPLEMENTED
vb@256
  2390
        }
krista@1427
  2391
krista@2092
  2392
        if (status == PEP_OUT_OF_MEMORY)
krista@2092
  2393
            goto enomem;
krista@2092
  2394
            
krista@2092
  2395
        if (status != PEP_STATUS_OK)
krista@2092
  2396
            goto pep_error;
krista@2048
  2397
krista@2135
  2398
        if (decrypt_status == PEP_DECRYPTED || decrypt_status == PEP_DECRYPTED_AND_VERIFIED) {
krista@2135
  2399
            char* wrap_info = NULL;
krista@2135
  2400
            status = unencapsulate_hidden_fields(src, msg, &wrap_info);
krista@2152
  2401
krista@2168
  2402
//            bool is_transport_wrapper = false;
krista@2135
  2403
            
krista@2135
  2404
            // FIXME: replace with enums, check status
krista@2135
  2405
            if (wrap_info) {
krista@2135
  2406
                if (strcmp(wrap_info, "OUTER") == 0) {
krista@2135
  2407
                    // this only occurs in with a direct outer wrapper
krista@2135
  2408
                    // where the actual content is in the inner wrapper
krista@2152
  2409
                    message* inner_message = NULL;                    
krista@2135
  2410
                    bloblist_t* actual_message = msg->attachments;
krista@2135
  2411