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