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