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