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