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