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