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