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