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