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