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