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