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