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