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