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