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