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