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