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