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