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