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