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