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