src/message_api.c
author Edouard Tisserant <edouard@pep-project.org>
Thu, 26 Jan 2017 16:44:37 +0100
changeset 1557 72aa4570d2c9
parent 1554 a62835de9daf
child 1558 02b4593103fd
permissions -rw-r--r--
Added PEP_enc_format : PEP_enc_PGP_MIME_Outlook1, to detect PGP_MIME messages that have been re-encoded by Outlook with attachments in different order
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
    }
edouard@1557
  1033
    else if (msg->attachments && msg->attachments->next &&
edouard@1557
  1034
            is_mime_type(msg->attachments->next, "application/pgp-encrypted") &&
edouard@1557
  1035
            is_PGP_message_text(msg->attachments->value)
edouard@1557
  1036
        ) {
edouard@1557
  1037
        msg->enc_format = PEP_enc_PGP_MIME_Outlook1;
edouard@1557
  1038
        return PEP_crypt_OpenPGP;
edouard@1557
  1039
    }
vb@311
  1040
    else {
vb@311
  1041
        msg->enc_format = PEP_enc_none;
vb@311
  1042
        return PEP_crypt_none;
vb@311
  1043
    }
vb@311
  1044
}
vb@311
  1045
vb@48
  1046
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
  1047
        PEP_SESSION session,
vb@113
  1048
        message *src,
vb@37
  1049
        stringlist_t * extra,
vb@38
  1050
        message **dst,
vb@939
  1051
        PEP_enc_format enc_format,
vb@939
  1052
        PEP_encrypt_flags_t flags
vb@37
  1053
    )
vb@37
  1054
{
vb@37
  1055
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
  1056
    message * msg = NULL;
vb@63
  1057
    stringlist_t * keys = NULL;
vb@37
  1058
vb@37
  1059
    assert(session);
vb@37
  1060
    assert(src);
vb@37
  1061
    assert(dst);
Edouard@558
  1062
    assert(enc_format != PEP_enc_none);
vb@81
  1063
Edouard@558
  1064
    if (!(session && src && dst && enc_format != PEP_enc_none))
vb@191
  1065
        return PEP_ILLEGAL_VALUE;
vb@191
  1066
Edouard@550
  1067
    if (src->dir == PEP_dir_incoming)
Edouard@550
  1068
        return PEP_ILLEGAL_VALUE;
krista@1427
  1069
vb@259
  1070
    determine_encryption_format(src);
vb@260
  1071
    if (src->enc_format != PEP_enc_none)
vb@260
  1072
        return PEP_ILLEGAL_VALUE;
vb@259
  1073
vb@37
  1074
    *dst = NULL;
vb@67
  1075
vb@236
  1076
    status = myself(session, src->from);
vb@236
  1077
    if (status != PEP_STATUS_OK)
vb@236
  1078
        goto pep_error;
Edouard@558
  1079
vb@80
  1080
    keys = new_stringlist(src->from->fpr);
vb@63
  1081
    if (keys == NULL)
vb@63
  1082
        goto enomem;
vb@37
  1083
vb@39
  1084
    stringlist_t *_k = keys;
vb@39
  1085
vb@39
  1086
    if (extra) {
vb@39
  1087
        _k = stringlist_append(_k, extra);
vb@63
  1088
        if (_k == NULL)
vb@63
  1089
            goto enomem;
vb@37
  1090
    }
vb@39
  1091
vb@299
  1092
    bool dest_keys_found = true;
Edouard@510
  1093
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@299
  1094
vb@37
  1095
    identity_list * _il;
krista@1427
  1096
Edouard@545
  1097
    if ((_il = src->bcc) && _il->ident)
Edouard@545
  1098
    {
Edouard@545
  1099
        // BCC limited support:
Edouard@545
  1100
        //     - App splits mails with BCC in multiple mails.
Edouard@545
  1101
        //     - Each email is encrypted separately
krista@1427
  1102
Edouard@789
  1103
        if(_il->next || (src->to && src->to->ident) || (src->cc && src->cc->ident))
Edouard@545
  1104
        {
Edouard@545
  1105
            // Only one Bcc with no other recipient allowed for now
Edouard@545
  1106
            return PEP_ILLEGAL_VALUE;
Edouard@545
  1107
        }
krista@1427
  1108
vb@299
  1109
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
  1110
        if (_status != PEP_STATUS_OK) {
vb@299
  1111
            status = _status;
vb@63
  1112
            goto pep_error;
vb@299
  1113
        }
krista@1427
  1114
vb@299
  1115
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@39
  1116
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
  1117
            if (_k == NULL)
vb@63
  1118
                goto enomem;
Edouard@510
  1119
            max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@510
  1120
                                           _il->ident);
vb@37
  1121
        }
vb@299
  1122
        else {
vb@299
  1123
            dest_keys_found = false;
vb@37
  1124
            status = PEP_KEY_NOT_FOUND;
krista@1427
  1125
        }
vb@299
  1126
    }
Edouard@545
  1127
    else
Edouard@545
  1128
    {
Edouard@545
  1129
        for (_il = src->to; _il && _il->ident; _il = _il->next) {
Edouard@545
  1130
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1131
            if (_status != PEP_STATUS_OK) {
Edouard@545
  1132
                status = _status;
Edouard@545
  1133
                goto pep_error;
Edouard@545
  1134
            }
vb@299
  1135
Edouard@545
  1136
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1137
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1138
                if (_k == NULL)
Edouard@545
  1139
                    goto enomem;
Edouard@545
  1140
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1141
                                               _il->ident);
Edouard@545
  1142
            }
Edouard@545
  1143
            else {
Edouard@545
  1144
                dest_keys_found = false;
Edouard@545
  1145
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1146
            }
vb@299
  1147
        }
vb@299
  1148
Edouard@545
  1149
        for (_il = src->cc; _il && _il->ident; _il = _il->next) {
Edouard@545
  1150
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1151
            if (_status != PEP_STATUS_OK)
Edouard@545
  1152
            {
Edouard@545
  1153
                status = _status;
Edouard@545
  1154
                goto pep_error;
Edouard@545
  1155
            }
Edouard@545
  1156
Edouard@545
  1157
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1158
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1159
                if (_k == NULL)
Edouard@545
  1160
                    goto enomem;
Edouard@545
  1161
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1162
                                               _il->ident);
Edouard@545
  1163
            }
Edouard@545
  1164
            else {
Edouard@545
  1165
                dest_keys_found = false;
Edouard@545
  1166
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1167
            }
vb@299
  1168
        }
vb@37
  1169
    }
krista@1427
  1170
Edouard@510
  1171
    if (!dest_keys_found ||
Edouard@510
  1172
        stringlist_length(keys) == 0 ||
Edouard@510
  1173
        _rating(max_comm_type,
Edouard@510
  1174
                PEP_rating_undefined) < PEP_rating_reliable)
Edouard@510
  1175
    {
vb@301
  1176
        free_stringlist(keys);
vb@465
  1177
        if (!session->passive_mode)
vb@465
  1178
            attach_own_key(session, src);
vb@301
  1179
        return PEP_UNENCRYPTED;
vb@301
  1180
    }
vb@301
  1181
    else {
vb@299
  1182
        msg = clone_to_empty_message(src);
vb@299
  1183
        if (msg == NULL)
vb@299
  1184
            goto enomem;
vb@299
  1185
Edouard@431
  1186
        attach_own_key(session, src);
Edouard@428
  1187
vb@81
  1188
        switch (enc_format) {
vb@260
  1189
        case PEP_enc_PGP_MIME:
vb@336
  1190
        case PEP_enc_PEP: // BUG: should be implemented extra
vb@260
  1191
            status = encrypt_PGP_MIME(session, src, keys, msg);
vb@260
  1192
            break;
vb@62
  1193
vb@62
  1194
        case PEP_enc_pieces:
vb@260
  1195
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
vb@38
  1196
            break;
vb@38
  1197
vb@336
  1198
        /* case PEP_enc_PEP:
vb@81
  1199
            // TODO: implement
vb@336
  1200
            NOT_IMPLEMENTED */
vb@81
  1201
vb@38
  1202
        default:
vb@38
  1203
            assert(0);
vb@63
  1204
            status = PEP_ILLEGAL_VALUE;
vb@63
  1205
            goto pep_error;
vb@37
  1206
        }
krista@1427
  1207
Edouard@392
  1208
        if (status == PEP_OUT_OF_MEMORY)
Edouard@392
  1209
            goto enomem;
krista@1427
  1210
vb@480
  1211
        if (status != PEP_STATUS_OK)
Edouard@392
  1212
            goto pep_error;
vb@37
  1213
    }
vb@37
  1214
vb@37
  1215
    free_stringlist(keys);
vb@63
  1216
vb@469
  1217
    if (msg && msg->shortmsg == NULL) {
vb@64
  1218
        msg->shortmsg = strdup("pEp");
vb@469
  1219
        assert(msg->shortmsg);
krista@853
  1220
        if (msg->shortmsg == NULL)
krista@853
  1221
            goto enomem;
vb@469
  1222
    }
vb@64
  1223
edouard@1542
  1224
    if (msg) {
vb@311
  1225
        decorate_message(msg, PEP_rating_undefined, NULL);
edouard@1542
  1226
        if (src->id) {
edouard@1542
  1227
            msg->id = strdup(src->id);
edouard@1542
  1228
            assert(msg->id);
edouard@1542
  1229
            if (msg->id == NULL)
edouard@1542
  1230
                goto enomem;
edouard@1542
  1231
        }
edouard@1542
  1232
    }
vb@311
  1233
vb@63
  1234
    *dst = msg;
vb@299
  1235
    return status;
vb@63
  1236
vb@63
  1237
enomem:
vb@63
  1238
    status = PEP_OUT_OF_MEMORY;
vb@63
  1239
vb@63
  1240
pep_error:
vb@63
  1241
    free_stringlist(keys);
vb@63
  1242
    free_message(msg);
vb@63
  1243
vb@37
  1244
    return status;
vb@37
  1245
}
vb@37
  1246
krista@995
  1247
DYNAMIC_API PEP_STATUS encrypt_message_for_self(
krista@992
  1248
        PEP_SESSION session,
krista@992
  1249
        pEp_identity* target_id,
krista@992
  1250
        message *src,
krista@992
  1251
        message **dst,
krista@992
  1252
        PEP_enc_format enc_format
krista@992
  1253
    )
krista@992
  1254
{
krista@992
  1255
    PEP_STATUS status = PEP_STATUS_OK;
krista@992
  1256
    message * msg = NULL;
krista@992
  1257
    stringlist_t * keys = NULL;
krista@992
  1258
krista@992
  1259
    assert(session);
krista@992
  1260
    assert(src);
krista@992
  1261
    assert(dst);
krista@992
  1262
    assert(enc_format != PEP_enc_none);
krista@992
  1263
krista@992
  1264
    if (!(session && src && dst && enc_format != PEP_enc_none))
krista@992
  1265
        return PEP_ILLEGAL_VALUE;
krista@992
  1266
krista@992
  1267
    if (src->dir == PEP_dir_incoming)
krista@992
  1268
        return PEP_ILLEGAL_VALUE;
krista@1427
  1269
krista@992
  1270
    determine_encryption_format(src);
krista@992
  1271
    if (src->enc_format != PEP_enc_none)
krista@992
  1272
        return PEP_ILLEGAL_VALUE;
krista@992
  1273
krista@995
  1274
    status = myself(session, target_id);
krista@995
  1275
    if (status != PEP_STATUS_OK)
krista@995
  1276
        goto pep_error;
krista@995
  1277
krista@992
  1278
    *dst = NULL;
krista@992
  1279
krista@1427
  1280
krista@992
  1281
    PEP_STATUS _status = update_identity(session, target_id);
krista@992
  1282
    if (_status != PEP_STATUS_OK) {
krista@992
  1283
        status = _status;
krista@992
  1284
        goto pep_error;
krista@992
  1285
    }
krista@992
  1286
krista@994
  1287
    char* target_fpr = target_id->fpr;
krista@994
  1288
    if (!target_fpr)
krista@994
  1289
        return PEP_KEY_NOT_FOUND; // FIXME: Error condition
krista@1427
  1290
krista@994
  1291
    keys = new_stringlist(target_fpr);
krista@992
  1292
krista@1427
  1293
krista@992
  1294
    msg = clone_to_empty_message(src);
krista@992
  1295
    if (msg == NULL)
krista@992
  1296
        goto enomem;
krista@992
  1297
krista@992
  1298
    switch (enc_format) {
krista@992
  1299
        case PEP_enc_PGP_MIME:
krista@992
  1300
        case PEP_enc_PEP: // BUG: should be implemented extra
krista@992
  1301
            status = encrypt_PGP_MIME(session, src, keys, msg);
krista@992
  1302
            break;
krista@992
  1303
krista@992
  1304
        case PEP_enc_pieces:
krista@992
  1305
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
krista@992
  1306
            break;
krista@992
  1307
krista@992
  1308
        /* case PEP_enc_PEP:
krista@992
  1309
            // TODO: implement
krista@992
  1310
            NOT_IMPLEMENTED */
krista@992
  1311
krista@992
  1312
        default:
krista@992
  1313
            assert(0);
krista@992
  1314
            status = PEP_ILLEGAL_VALUE;
krista@992
  1315
            goto pep_error;
krista@992
  1316
    }
krista@1427
  1317
krista@992
  1318
    if (status == PEP_OUT_OF_MEMORY)
krista@992
  1319
        goto enomem;
krista@1427
  1320
krista@992
  1321
    if (status != PEP_STATUS_OK)
krista@992
  1322
        goto pep_error;
krista@992
  1323
krista@994
  1324
     if (msg && msg->shortmsg == NULL) {
krista@994
  1325
         msg->shortmsg = strdup("pEp");
krista@994
  1326
         assert(msg->shortmsg);
krista@994
  1327
         if (msg->shortmsg == NULL)
krista@994
  1328
             goto enomem;
krista@994
  1329
     }
krista@992
  1330
edouard@1542
  1331
     if (msg) {
edouard@1542
  1332
         if (src->id) {
edouard@1542
  1333
             msg->id = strdup(src->id);
edouard@1542
  1334
             assert(msg->id);
edouard@1542
  1335
             if (msg->id == NULL)
edouard@1542
  1336
                 goto enomem;
edouard@1542
  1337
         }
edouard@1542
  1338
     }
edouard@1542
  1339
krista@992
  1340
    *dst = msg;
krista@992
  1341
    return status;
krista@992
  1342
krista@992
  1343
enomem:
krista@992
  1344
    status = PEP_OUT_OF_MEMORY;
krista@992
  1345
krista@992
  1346
pep_error:
krista@992
  1347
    free_stringlist(keys);
krista@992
  1348
    free_message(msg);
krista@992
  1349
krista@992
  1350
    return status;
krista@992
  1351
}
krista@992
  1352
vb@781
  1353
static bool is_a_pEpmessage(const message *msg)
vb@781
  1354
{
vb@781
  1355
    for (stringpair_list_t *i = msg->opt_fields; i && i->value ; i=i->next) {
vb@781
  1356
        if (strcasecmp(i->value->key, "X-pEp-Version") == 0)
vb@781
  1357
            return true;
vb@781
  1358
    }
vb@781
  1359
    return false;
vb@781
  1360
}
vb@781
  1361
vb@781
  1362
// update comm_type to pEp_ct_pEp if needed
vb@781
  1363
Edouard@858
  1364
static PEP_STATUS _update_identity_for_incoming_message(
vb@781
  1365
        PEP_SESSION session,
vb@781
  1366
        const message *src
vb@781
  1367
    )
vb@781
  1368
{
Edouard@858
  1369
    PEP_STATUS status;
Edouard@858
  1370
    if (src->from && src->from->address) {
Edouard@858
  1371
        status = update_identity(session, src->from);
krista@1192
  1372
        if (status == PEP_STATUS_OK
Edouard@858
  1373
                && is_a_pEpmessage(src)
vb@781
  1374
                && src->from->comm_type >= PEP_ct_OpenPGP_unconfirmed
vb@781
  1375
                && src->from->comm_type != PEP_ct_pEp_unconfirmed
vb@781
  1376
                && src->from->comm_type != PEP_ct_pEp)
vb@781
  1377
        {
vb@781
  1378
            src->from->comm_type |= PEP_ct_pEp_unconfirmed;
Edouard@858
  1379
            status = update_identity(session, src->from);
vb@781
  1380
        }
Edouard@858
  1381
        return status;
vb@781
  1382
    }
Edouard@858
  1383
    return PEP_ILLEGAL_VALUE;
vb@781
  1384
}
vb@781
  1385
krista@1397
  1386
krista@1397
  1387
PEP_STATUS _get_detached_signature(message* msg, bloblist_t** signature_blob) {
krista@1397
  1388
    bloblist_t* attach_curr = msg->attachments;
krista@1427
  1389
krista@1427
  1390
    *signature_blob = NULL;
krista@1427
  1391
krista@1397
  1392
    while (attach_curr) {
krista@1427
  1393
        if (strcasecmp(attach_curr->mime_type, "application/pgp-signature") == 0) {
krista@1397
  1394
            *signature_blob = attach_curr;
krista@1397
  1395
            break;
krista@1397
  1396
        }
krista@1397
  1397
        attach_curr = attach_curr->next;
krista@1397
  1398
    }
krista@1427
  1399
krista@1427
  1400
    return PEP_STATUS_OK;
krista@1427
  1401
}
krista@1427
  1402
krista@1427
  1403
PEP_STATUS _get_signed_text(const char* ptext, const size_t psize,
krista@1427
  1404
                            char** stext, size_t* ssize) {
krista@1427
  1405
krista@1427
  1406
    char* signed_boundary = NULL;
krista@1427
  1407
    char* signpost = strstr(ptext, "Content-Type: multipart/signed");
krista@1427
  1408
krista@1427
  1409
    *ssize = 0;
krista@1427
  1410
    *stext = NULL;
krista@1427
  1411
krista@1427
  1412
    if (!signpost)
krista@1427
  1413
        return PEP_UNKNOWN_ERROR;
krista@1427
  1414
krista@1427
  1415
    char* curr_line = signpost;
krista@1427
  1416
//    const char* end_text = ptext + psize;
krista@1427
  1417
    const char* boundary_key = "boundary=\"";
krista@1427
  1418
    const size_t BOUNDARY_KEY_SIZE = 10;
krista@1427
  1419
krista@1427
  1420
    char* start_boundary = strstr(curr_line, boundary_key);
krista@1427
  1421
    if (!start_boundary)
krista@1427
  1422
        return PEP_UNKNOWN_ERROR;
krista@1427
  1423
krista@1427
  1424
    start_boundary += BOUNDARY_KEY_SIZE;
krista@1427
  1425
krista@1427
  1426
    char* end_boundary = strstr(start_boundary, "\"");
krista@1427
  1427
krista@1427
  1428
    if (!end_boundary)
krista@1427
  1429
        return PEP_UNKNOWN_ERROR;
krista@1427
  1430
krista@1427
  1431
    size_t boundary_strlen = (end_boundary - start_boundary) + 2;
krista@1427
  1432
krista@1427
  1433
    signed_boundary = calloc(1, boundary_strlen + 1);
krista@1427
  1434
    strlcpy(signed_boundary, "--", boundary_strlen + 1);
krista@1427
  1435
    strlcat(signed_boundary, start_boundary, boundary_strlen + 1);
krista@1427
  1436
krista@1427
  1437
    start_boundary = strstr(end_boundary, signed_boundary);
krista@1427
  1438
krista@1427
  1439
    if (!start_boundary)
krista@1427
  1440
        return PEP_UNKNOWN_ERROR;
krista@1427
  1441
krista@1427
  1442
    start_boundary += boundary_strlen;
krista@1427
  1443
krista@1427
  1444
    while (*start_boundary == '\n')
krista@1427
  1445
        start_boundary++;
krista@1427
  1446
krista@1427
  1447
    end_boundary = strstr(start_boundary + boundary_strlen, signed_boundary);
krista@1427
  1448
krista@1427
  1449
    if (!end_boundary)
krista@1427
  1450
        return PEP_UNKNOWN_ERROR;
krista@1427
  1451
krista@1427
  1452
    end_boundary--; // See RFC3156 section 5...
krista@1427
  1453
krista@1427
  1454
    *ssize = end_boundary - start_boundary;
krista@1427
  1455
    *stext = start_boundary;
krista@1427
  1456
    free(signed_boundary);
krista@1427
  1457
krista@1397
  1458
    return PEP_STATUS_OK;
krista@1397
  1459
}
krista@1397
  1460
krista@1484
  1461
PEP_STATUS combine_keylists(PEP_SESSION session, stringlist_t** verify_in, 
krista@1484
  1462
                            stringlist_t** keylist_in_out, 
krista@1484
  1463
                            pEp_identity* from) {
krista@1484
  1464
    
krista@1484
  1465
    if (!verify_in || !(*verify_in)) // this isn't really a problem.
krista@1484
  1466
        return PEP_STATUS_OK;
krista@1484
  1467
    
krista@1484
  1468
    stringlist_t* orig_verify = *verify_in;
krista@1484
  1469
    
krista@1484
  1470
    stringlist_t* verify_curr;
krista@1484
  1471
    stringlist_t* from_keys;
krista@1484
  1472
    
krista@1484
  1473
    /* FIXME: what to do if head needs to be null */
krista@1484
  1474
    PEP_STATUS status = find_keys(session, from->address, &from_keys);
krista@1484
  1475
    
krista@1484
  1476
    stringlist_t* from_fpr_node = NULL;
krista@1484
  1477
    stringlist_t* from_curr;
krista@1484
  1478
    
krista@1484
  1479
    for (from_curr = from_keys; from_curr; from_curr = from_curr->next) {
krista@1484
  1480
        for (verify_curr = orig_verify; verify_curr; verify_curr = verify_curr->next) {
krista@1484
  1481
            if (from_curr->value && verify_curr->value &&
krista@1484
  1482
                strcasecmp(from_curr->value, verify_curr->value) == 0) {
krista@1484
  1483
                from_fpr_node = from_curr;
krista@1484
  1484
                break;
krista@1484
  1485
            }
krista@1484
  1486
        }
krista@1484
  1487
    }
krista@1484
  1488
    
krista@1484
  1489
    if (!from_fpr_node) {
krista@1484
  1490
        status = PEP_KEY_NOT_FOUND;
krista@1484
  1491
        goto free;
krista@1484
  1492
    }
krista@1484
  1493
krista@1484
  1494
    verify_curr = orig_verify;
krista@1484
  1495
    
krista@1484
  1496
    /* put "from" signer at the beginning of the list */
krista@1484
  1497
    if (strcasecmp(orig_verify->value, from_fpr_node->value) != 0) {
krista@1484
  1498
        orig_verify = stringlist_delete(orig_verify, from_fpr_node->value);
krista@1484
  1499
        verify_curr = new_stringlist(from_fpr_node->value);
krista@1484
  1500
        verify_curr->next = orig_verify;
krista@1484
  1501
    }
krista@1484
  1502
krista@1484
  1503
    /* append keylist to signers */
krista@1484
  1504
    if (keylist_in_out && *keylist_in_out && (*keylist_in_out)->value) {
krista@1484
  1505
        stringlist_t** tail_pp = &verify_curr->next;
krista@1484
  1506
        
krista@1484
  1507
        while (*tail_pp) {
krista@1484
  1508
            tail_pp = &((*tail_pp)->next);
krista@1484
  1509
        }
krista@1484
  1510
        *tail_pp = *keylist_in_out;
krista@1484
  1511
    }
krista@1484
  1512
    
krista@1484
  1513
    *keylist_in_out = verify_curr;
krista@1484
  1514
    
krista@1484
  1515
    status = PEP_STATUS_OK;
krista@1484
  1516
    
krista@1484
  1517
free:
krista@1484
  1518
    free_stringlist(from_keys);
krista@1484
  1519
    return status;    
krista@1484
  1520
}
krista@1484
  1521
krista@1484
  1522
Edouard@741
  1523
DYNAMIC_API PEP_STATUS _decrypt_message(
vb@37
  1524
        PEP_SESSION session,
vb@113
  1525
        message *src,
vb@241
  1526
        message **dst,
vb@251
  1527
        stringlist_t **keylist,
vb@1004
  1528
        PEP_rating *rating,
krista@1427
  1529
        PEP_decrypt_flags_t *flags,
Edouard@741
  1530
        identity_list **private_il
vb@37
  1531
    )
vb@37
  1532
{
vb@37
  1533
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
  1534
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
  1535
    message *msg = NULL;
vb@112
  1536
    char *ctext;
vb@112
  1537
    size_t csize;
vb@269
  1538
    char *ptext = NULL;
vb@112
  1539
    size_t psize;
vb@241
  1540
    stringlist_t *_keylist = NULL;
vb@37
  1541
vb@74
  1542
    assert(session);
vb@74
  1543
    assert(src);
vb@74
  1544
    assert(dst);
vb@241
  1545
    assert(keylist);
vb@1004
  1546
    assert(rating);
Edouard@739
  1547
    assert(flags);
vb@73
  1548
vb@1004
  1549
    if (!(session && src && dst && keylist && rating && flags))
vb@191
  1550
        return PEP_ILLEGAL_VALUE;
vb@191
  1551
Edouard@739
  1552
    *flags = 0;
Edouard@739
  1553
Edouard@734
  1554
    // Private key in unencrypted mail are ignored -> NULL
Edouard@734
  1555
    bool imported_keys = import_attached_keys(session, src, NULL);
Edouard@728
  1556
Edouard@728
  1557
    // Update src->from in case we just imported a key
Edouard@728
  1558
    // we would need to check signature
Edouard@858
  1559
    status = _update_identity_for_incoming_message(session, src);
Edouard@858
  1560
    if(status != PEP_STATUS_OK)
Edouard@858
  1561
        return status;
Edouard@858
  1562
krista@1397
  1563
    // Get detached signature, if any
krista@1397
  1564
    bloblist_t* detached_sig = NULL;
krista@1397
  1565
    char* dsig_text = NULL;
krista@1397
  1566
    size_t dsig_size = 0;
krista@1397
  1567
    status = _get_detached_signature(src, &detached_sig);
krista@1397
  1568
    if (detached_sig) {
krista@1397
  1569
        dsig_text = detached_sig->value;
krista@1397
  1570
        dsig_size = detached_sig->size;
krista@1397
  1571
    }
krista@1427
  1572
vb@259
  1573
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
  1574
vb@74
  1575
    *dst = NULL;
vb@251
  1576
    *keylist = NULL;
vb@1004
  1577
    *rating = PEP_rating_undefined;
krista@1427
  1578
vb@261
  1579
    switch (src->enc_format) {
vb@269
  1580
        case PEP_enc_none:
vb@1004
  1581
            *rating = PEP_rating_unencrypted;
vb@731
  1582
            if (imported_keys)
vb@731
  1583
                remove_attached_keys(src);
edouard@1320
  1584
            if(session->inject_sync_msg){
edouard@1165
  1585
                status = receive_DeviceState_msg(session, src, *rating, *keylist);
krista@1427
  1586
                if (status == PEP_MESSAGE_CONSUME ||
edouard@1369
  1587
                    status == PEP_MESSAGE_IGNORE) {
vb@1134
  1588
                    free_message(msg);
vb@1134
  1589
                    msg = NULL;
edouard@1369
  1590
                    *flags |= (status == PEP_MESSAGE_IGNORE) ?
edouard@1369
  1591
                                PEP_decrypt_flag_ignore :
edouard@1369
  1592
                                PEP_decrypt_flag_consume;
vb@1134
  1593
                }
vb@1134
  1594
                else if (status != PEP_STATUS_OK) {
vb@1134
  1595
                    return status;
vb@1134
  1596
                }
vb@1099
  1597
            }
krista@1484
  1598
            
krista@1484
  1599
            char* slong = src->longmsg;
krista@1484
  1600
            char* sform = src->longmsg_formatted;
krista@1484
  1601
            bloblist_t* satt = src->attachments;
krista@1484
  1602
                                    
krista@1484
  1603
            if ((!slong || slong[0] == '\0')
krista@1484
  1604
                 && (!sform || sform[0] == '\0')) {
krista@1484
  1605
                if (satt) {
krista@1484
  1606
                    const char* inner_mime_type = satt->mime_type;
krista@1484
  1607
                    if (strcasecmp(inner_mime_type, "text/plain") == 0) {
krista@1484
  1608
                        free(slong); /* in case of "" */
krista@1484
  1609
                        src->longmsg = strdup(satt->value);
krista@1484
  1610
                    
krista@1484
  1611
                        bloblist_t* next_node = satt->next;
krista@1484
  1612
                        if (next_node) {
krista@1484
  1613
                            inner_mime_type = next_node->mime_type;
krista@1484
  1614
                            if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@1484
  1615
                                free(sform);
krista@1484
  1616
                                src->longmsg_formatted = strdup(next_node->value);
krista@1484
  1617
                            }
krista@1484
  1618
                        }
krista@1484
  1619
                    }
krista@1484
  1620
                    else if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@1484
  1621
                        free(sform);
krista@1484
  1622
                        src->longmsg_formatted = strdup(satt->value);
krista@1484
  1623
                    }                    
krista@1484
  1624
                }
krista@1484
  1625
            }       
krista@1484
  1626
krista@1484
  1627
            
vb@301
  1628
            return PEP_UNENCRYPTED;
vb@269
  1629
vb@261
  1630
        case PEP_enc_PGP_MIME:
vb@301
  1631
            ctext = src->attachments->next->value;
vb@269
  1632
            csize = src->attachments->next->size;
vb@261
  1633
            break;
vb@261
  1634
edouard@1557
  1635
        case PEP_enc_PGP_MIME_Outlook1:
edouard@1557
  1636
            ctext = src->attachments->value;
edouard@1557
  1637
            csize = src->attachments->size;
edouard@1557
  1638
            break;
edouard@1557
  1639
vb@261
  1640
        case PEP_enc_pieces:
vb@261
  1641
            ctext = src->longmsg;
vb@261
  1642
            csize = strlen(ctext);
vb@261
  1643
            break;
vb@261
  1644
vb@261
  1645
        default:
vb@261
  1646
            NOT_IMPLEMENTED
vb@259
  1647
    }
Edouard@431
  1648
    status = cryptotech[crypto].decrypt_and_verify(session, ctext,
krista@1427
  1649
                                                   csize, dsig_text, dsig_size,
krista@1397
  1650
                                                   &ptext, &psize, &_keylist);
edouard@1195
  1651
    if (status > PEP_CANNOT_DECRYPT_UNKNOWN){
edouard@1271
  1652
        goto pep_error;
edouard@1271
  1653
    }
edouard@1271
  1654
edouard@1273
  1655
    decrypt_status = status;
edouard@1273
  1656
edouard@1271
  1657
    if (status == PEP_DECRYPT_NO_KEY){
edouard@1299
  1658
        PEP_STATUS sync_status = inject_DeviceState_event(session, CannotDecrypt, NULL, NULL);
edouard@1299
  1659
        if (sync_status == PEP_OUT_OF_MEMORY){
edouard@1299
  1660
            status = PEP_OUT_OF_MEMORY;
edouard@1273
  1661
            goto pep_error;
edouard@1299
  1662
        }
edouard@1195
  1663
    }
vb@113
  1664
krista@1427
  1665
    bool imported_private_key_address = false;
Edouard@728
  1666
vb@256
  1667
    if (ptext) {
vb@256
  1668
        switch (src->enc_format) {
vb@256
  1669
            case PEP_enc_PGP_MIME:
edouard@1557
  1670
            case PEP_enc_PGP_MIME_Outlook1:
vb@269
  1671
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  1672
                if (status != PEP_STATUS_OK)
krista@1484
  1673
                    goto pep_error;                
krista@1484
  1674
                
krista@1484
  1675
                char* mlong = msg->longmsg;
krista@1484
  1676
                char* mform = msg->longmsg_formatted;
krista@1484
  1677
                bloblist_t* matt = msg->attachments;
krista@1484
  1678
                                        
krista@1484
  1679
                if ((!mlong || mlong[0] == '\0')
krista@1484
  1680
                     && (!mform || mform[0] == '\0')) {
krista@1484
  1681
                    if (matt) {
krista@1484
  1682
                        const char* inner_mime_type = matt->mime_type;
krista@1484
  1683
                        if (strcasecmp(inner_mime_type, "text/plain") == 0) {
krista@1484
  1684
                            free(mlong); /* in case of "" */
krista@1484
  1685
                            msg->longmsg = strdup(matt->value);
krista@1484
  1686
                        
krista@1484
  1687
                            bloblist_t* next_node = matt->next;
krista@1484
  1688
                            if (next_node) {
krista@1484
  1689
                                inner_mime_type = next_node->mime_type;
krista@1484
  1690
                                if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@1484
  1691
                                    free(mform);
krista@1484
  1692
                                    msg->longmsg_formatted = strdup(next_node->value);
krista@1484
  1693
                                }
krista@1484
  1694
                            }
krista@1484
  1695
                        }
krista@1484
  1696
                        else if (strcasecmp(inner_mime_type, "text/html") == 0) {
krista@1484
  1697
                            free(mform);
krista@1484
  1698
                            msg->longmsg_formatted = strdup(matt->value);
krista@1484
  1699
                        }                    
krista@1484
  1700
                    }
krista@1484
  1701
                    if (msg->shortmsg) {
krista@1484
  1702
                        free(src->shortmsg);
krista@1484
  1703
                        src->shortmsg = strdup(msg->shortmsg);
krista@1484
  1704
                    }
krista@1484
  1705
                }    
krista@1484
  1706
krista@1484
  1707
                if (decrypt_status != PEP_DECRYPTED_AND_VERIFIED) {
krista@1484
  1708
                    status = _get_detached_signature(msg, &detached_sig);
krista@1484
  1709
                    if (decrypt_status == PEP_DECRYPTED && detached_sig) {
krista@1484
  1710
                        dsig_text = detached_sig->value;
krista@1484
  1711
                        dsig_size = detached_sig->size;
krista@1484
  1712
                        size_t ssize = 0;
krista@1484
  1713
                        char* stext = NULL;
krista@1484
  1714
krista@1484
  1715
                        status = _get_signed_text(ptext, psize, &stext, &ssize);
krista@1484
  1716
                        stringlist_t *_verify_keylist = NULL;
krista@1484
  1717
krista@1484
  1718
                        if (ssize > 0 && stext) {
krista@1484
  1719
                            status = cryptotech[crypto].verify_text(session, stext,
krista@1484
  1720
                                                                    ssize, dsig_text, dsig_size,
krista@1484
  1721
                                                                    &_verify_keylist);
krista@1484
  1722
krista@1484
  1723
                            if (status == PEP_VERIFIED || status == PEP_VERIFIED_AND_TRUSTED)
krista@1484
  1724
                                decrypt_status = PEP_DECRYPTED_AND_VERIFIED;
krista@1484
  1725
                            
krista@1484
  1726
                                status = combine_keylists(session, &_verify_keylist, &_keylist, src->from);
krista@1484
  1727
                        }
krista@1427
  1728
                    }
krista@1427
  1729
                }
vb@256
  1730
                break;
vb@256
  1731
vb@256
  1732
            case PEP_enc_pieces:
vb@256
  1733
                msg = clone_to_empty_message(src);
vb@256
  1734
                if (msg == NULL)
vb@113
  1735
                    goto enomem;
vb@113
  1736
Edouard@882
  1737
                msg->longmsg = ptext;
Edouard@882
  1738
                ptext = NULL;
vb@256
  1739
vb@256
  1740
                bloblist_t *_m = msg->attachments;
vb@320
  1741
                if (_m == NULL && src->attachments && src->attachments->value) {
vb@320
  1742
                    msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@320
  1743
                    _m = msg->attachments;
vb@320
  1744
                }
vb@320
  1745
vb@256
  1746
                bloblist_t *_s;
Edouard@754
  1747
                for (_s = src->attachments; _s; _s = _s->next) {
Edouard@754
  1748
                    if (_s->value == NULL && _s->size == 0){
Edouard@754
  1749
                        _m = bloblist_add(_m, NULL, 0, _s->mime_type, _s->filename);
Edouard@754
  1750
                        if (_m == NULL)
Edouard@754
  1751
                            goto enomem;
Edouard@754
  1752
Edouard@754
  1753
                    }
Edouard@754
  1754
                    else if (is_encrypted_attachment(_s)) {
vb@256
  1755
                        stringlist_t *_keylist = NULL;
roker@862
  1756
                        char *attctext  = _s->value;
roker@862
  1757
                        size_t attcsize = _s->size;
Edouard@431
  1758
Edouard@842
  1759
                        free(ptext);
Edouard@842
  1760
                        ptext = NULL;
Edouard@842
  1761
krista@1397
  1762
                        // FIXME: What about attachments with separate sigs???
Edouard@431
  1763
                        status = decrypt_and_verify(session, attctext, attcsize,
krista@1397
  1764
                                                    NULL, 0,
krista@1397
  1765
                                                    &ptext, &psize, &_keylist);
krista@1484
  1766
                        free_stringlist(_keylist); // FIXME: Why do we do this?
vb@256
  1767
vb@289
  1768
                        if (ptext) {
vb@289
  1769
                            if (is_encrypted_html_attachment(_s)) {
Edouard@882
  1770
                                msg->longmsg_formatted = ptext;
Edouard@882
  1771
                                ptext = NULL;
vb@289
  1772
                            }
vb@289
  1773
                            else {
roker@862
  1774
                                static const char * const mime_type = "application/octet-stream";
roker@864
  1775
                                char * const filename =
vb@289
  1776
                                    without_double_ending(_s->filename);
vb@289
  1777
                                if (filename == NULL)
vb@289
  1778
                                    goto enomem;
vb@289
  1779
Edouard@882
  1780
                                _m = bloblist_add(_m, ptext, psize, mime_type,
vb@289
  1781
                                    filename);
roker@862
  1782
                                free(filename);
vb@290
  1783
                                if (_m == NULL)
vb@289
  1784
                                    goto enomem;
vb@290
  1785
Edouard@882
  1786
                                ptext = NULL;
Edouard@882
  1787
vb@290
  1788
                                if (msg->attachments == NULL)
vb@290
  1789
                                    msg->attachments = _m;
vb@289
  1790
                            }
vb@256
  1791
                        }
vb@256
  1792
                        else {
vb@320
  1793
                            char *copy = malloc(_s->size);
vb@350
  1794
                            assert(copy);
vb@350
  1795
                            if (copy == NULL)
vb@350
  1796
                                goto enomem;
vb@320
  1797
                            memcpy(copy, _s->value, _s->size);
vb@320
  1798
                            _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@256
  1799
                            if (_m == NULL)
vb@256
  1800
                                goto enomem;
vb@256
  1801
                        }
vb@256
  1802
                    }
vb@320
  1803
                    else {
vb@320
  1804
                        char *copy = malloc(_s->size);
vb@350
  1805
                        assert(copy);
vb@350
  1806
                        if (copy == NULL)
vb@350
  1807
                            goto enomem;
vb@320
  1808
                        memcpy(copy, _s->value, _s->size);
vb@320
  1809
                        _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@320
  1810
                        if (_m == NULL)
vb@320
  1811
                            goto enomem;
vb@320
  1812
                    }
vb@256
  1813
                }
vb@256
  1814
vb@256
  1815
                break;
vb@256
  1816
vb@256
  1817
            default:
vb@256
  1818
                // BUG: must implement more
vb@256
  1819
                NOT_IMPLEMENTED
vb@256
  1820
        }
krista@1427
  1821
vb@256
  1822
        switch (src->enc_format) {
vb@256
  1823
            case PEP_enc_PGP_MIME:
vb@256
  1824
            case PEP_enc_pieces:
edouard@1557
  1825
            case PEP_enc_PGP_MIME_Outlook1:
vb@256
  1826
                status = copy_fields(msg, src);
vb@256
  1827
                if (status != PEP_STATUS_OK)
vb@256
  1828
                    goto pep_error;
vb@256
  1829
vb@280
  1830
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1831
                {
vb@256
  1832
                    char * shortmsg;
vb@256
  1833
                    char * longmsg;
vb@256
  1834
krista@853
  1835
                    int r = separate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1836
                            &longmsg);
vb@256
  1837
                    if (r == -1)
vb@256
  1838
                        goto enomem;
vb@256
  1839
vb@256
  1840
                    free(msg->shortmsg);
vb@256
  1841
                    free(msg->longmsg);
vb@256
  1842
vb@256
  1843
                    msg->shortmsg = shortmsg;
vb@256
  1844
                    msg->longmsg = longmsg;
vb@256
  1845
                }
vb@256
  1846
                else {
vb@256
  1847
                    msg->shortmsg = strdup(src->shortmsg);
vb@469
  1848
                    assert(msg->shortmsg);
vb@256
  1849
                    if (msg->shortmsg == NULL)
vb@256
  1850
                        goto enomem;
vb@256
  1851
                }
vb@256
  1852
                break;
vb@256
  1853
            default:
krista@1484
  1854
                    // BUG: must implement more
krista@1484
  1855
                    NOT_IMPLEMENTED
vb@256
  1856
        }
krista@1427
  1857
Edouard@734
  1858
        // check for private key in decrypted message attachement while inporting
Edouard@741
  1859
        identity_list *_private_il = NULL;
Edouard@741
  1860
        imported_keys = import_attached_keys(session, msg, &_private_il);
krista@1427
  1861
        if (_private_il &&
Edouard@741
  1862
            identity_list_length(_private_il) == 1 &&
Edouard@741
  1863
            _private_il->ident->address)
Edouard@728
  1864
        {
Edouard@741
  1865
            imported_private_key_address = true;
Edouard@728
  1866
        }
Edouard@741
  1867
Edouard@741
  1868
        if(private_il && imported_private_key_address){
Edouard@741
  1869
            *private_il = _private_il;
Edouard@741
  1870
        }else{
Edouard@741
  1871
            free_identity_list(_private_il);
Edouard@741
  1872
        }
krista@1427
  1873
Edouard@694
  1874
        if(decrypt_status == PEP_DECRYPTED){
Edouard@728
  1875
Edouard@728
  1876
            // TODO optimize if import_attached_keys didn't import any key
krista@1427
  1877
Edouard@431
  1878
            // In case message did decrypt, but no valid signature could be found
Edouard@431
  1879
            // then retry decrypt+verify after importing key.
Edouard@728
  1880
Edouard@728
  1881
            // Update msg->from in case we just imported a key
Edouard@728
  1882
            // we would need to check signature
vb@781
  1883
Edouard@861
  1884
            status = _update_identity_for_incoming_message(session, src);
Edouard@858
  1885
            if(status != PEP_STATUS_OK)
Edouard@858
  1886
                goto pep_error;
krista@1427
  1887
Edouard@431
  1888
            char *re_ptext = NULL;
Edouard@431
  1889
            size_t re_psize;
krista@1427
  1890
Edouard@431
  1891
            free_stringlist(_keylist);
Edouard@431
  1892
            _keylist = NULL;
vb@256
  1893
Edouard@431
  1894
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
krista@1397
  1895
                csize, dsig_text, dsig_size, &re_ptext, &re_psize, &_keylist);
krista@1427
  1896
Edouard@882
  1897
            free(re_ptext);
krista@1427
  1898
Edouard@431
  1899
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
Edouard@431
  1900
                goto pep_error;
krista@1427
  1901
Edouard@431
  1902
            decrypt_status = status;
Edouard@431
  1903
        }
krista@1427
  1904
vb@1004
  1905
        *rating = decrypt_rating(decrypt_status);
krista@1427
  1906
vb@1004
  1907
        if (*rating > PEP_rating_mistrust) {
vb@1004
  1908
            PEP_rating kl_rating = PEP_rating_undefined;
krista@1427
  1909
Edouard@431
  1910
            if (_keylist)
vb@1004
  1911
                kl_rating = keylist_rating(session, _keylist);
krista@1427
  1912
vb@1004
  1913
            if (kl_rating <= PEP_rating_mistrust) {
vb@1004
  1914
                *rating = kl_rating;
Edouard@445
  1915
            }
vb@1004
  1916
            else if (*rating >= PEP_rating_reliable &&
vb@1004
  1917
                     kl_rating < PEP_rating_reliable) {
vb@1004
  1918
                *rating = PEP_rating_unreliable;
Edouard@431
  1919
            }
vb@1004
  1920
            else if (*rating >= PEP_rating_reliable &&
vb@1004
  1921
                     kl_rating >= PEP_rating_reliable) {
Edouard@431
  1922
                if (!(src->from && src->from->user_id && src->from->user_id[0])) {
vb@1004
  1923
                    *rating = PEP_rating_unreliable;
Edouard@431
  1924
                }
Edouard@431
  1925
                else {
Edouard@431
  1926
                    char *fpr = _keylist->value;
Edouard@431
  1927
                    pEp_identity *_from = new_identity(src->from->address, fpr,
Edouard@431
  1928
                                                       src->from->user_id, src->from->username);
Edouard@431
  1929
                    if (_from == NULL)
Edouard@431
  1930
                        goto enomem;
edouard@1156
  1931
                    status = get_trust(session, _from);
Edouard@431
  1932
                    if (_from->comm_type != PEP_ct_unknown)
vb@1004
  1933
                        *rating = _rating(_from->comm_type, PEP_rating_undefined);
Edouard@431
  1934
                    free_identity(_from);
edouard@1156
  1935
                    if (status == PEP_CANNOT_FIND_IDENTITY)
edouard@1156
  1936
                       status = PEP_STATUS_OK;
Edouard@431
  1937
                    if (status != PEP_STATUS_OK)
Edouard@431
  1938
                        goto pep_error;
Edouard@431
  1939
                }
Edouard@431
  1940
            }
Edouard@431
  1941
        }
vb@113
  1942
    }
Edouard@745
  1943
    else
Edouard@745
  1944
    {
vb@1004
  1945
        *rating = decrypt_rating(decrypt_status);
Edouard@745
  1946
        goto pep_error;
Edouard@745
  1947
    }
vb@731
  1948
Edouard@728
  1949
    // Case of own key imported from own trusted message
krista@1427
  1950
    if (// Message have been reliably decrypted
Edouard@728
  1951
        msg &&
vb@1004
  1952
        *rating >= PEP_rating_trusted &&
Edouard@728
  1953
        imported_private_key_address &&
Edouard@728
  1954
        // to is [own]
Edouard@728
  1955
        msg->to->ident->user_id &&
krista@1427
  1956
        strcmp(msg->to->ident->user_id, PEP_OWN_USERID) == 0
Edouard@729
  1957
        )
Edouard@728
  1958
    {
Edouard@738
  1959
        *flags |= PEP_decrypt_flag_own_private_key;
Edouard@728
  1960
    }
vb@113
  1961
vb@731
  1962
    if (msg) {
vb@1004
  1963
        decorate_message(msg, *rating, _keylist);
vb@731
  1964
        if (imported_keys)
vb@731
  1965
            remove_attached_keys(msg);
edouard@1329
  1966
        if (*rating >= PEP_rating_reliable && session->inject_sync_msg) {
edouard@1165
  1967
            status = receive_DeviceState_msg(session, msg, *rating, _keylist);
krista@1427
  1968
            if (status == PEP_MESSAGE_CONSUME ||
edouard@1369
  1969
                status == PEP_MESSAGE_IGNORE) {
vb@1099
  1970
                free_message(msg);
vb@1099
  1971
                msg = NULL;
edouard@1369
  1972
                *flags |= (status == PEP_MESSAGE_IGNORE) ?
edouard@1369
  1973
                            PEP_decrypt_flag_ignore :
edouard@1369
  1974
                            PEP_decrypt_flag_consume;
edouard@1355
  1975
edouard@1355
  1976
                status = decrypt_status;
vb@1099
  1977
            }
vb@1099
  1978
            else if (status != PEP_STATUS_OK){
vb@1099
  1979
                goto pep_error;
vb@1099
  1980
            }
vb@1099
  1981
        }
edouard@1542
  1982
        if (src->id) {
edouard@1542
  1983
            msg->id = strdup(src->id);
edouard@1542
  1984
            assert(msg->id);
edouard@1542
  1985
            if (msg->id == NULL)
edouard@1542
  1986
                goto enomem;
edouard@1542
  1987
        }
vb@731
  1988
    }
vb@235
  1989
vb@74
  1990
    *dst = msg;
vb@241
  1991
    *keylist = _keylist;
vb@241
  1992
vb@953
  1993
    return status;
vb@73
  1994
vb@73
  1995
enomem:
vb@73
  1996
    status = PEP_OUT_OF_MEMORY;
vb@73
  1997
vb@73
  1998
pep_error:
Edouard@882
  1999
    free(ptext);
vb@73
  2000
    free_message(msg);
vb@241
  2001
    free_stringlist(_keylist);
vb@39
  2002
vb@37
  2003
    return status;
vb@37
  2004
}
vb@37
  2005
Edouard@741
  2006
DYNAMIC_API PEP_STATUS decrypt_message(
Edouard@741
  2007
        PEP_SESSION session,
Edouard@741
  2008
        message *src,
Edouard@741
  2009
        message **dst,
Edouard@741
  2010
        stringlist_t **keylist,
vb@1004
  2011
        PEP_rating *rating,
krista@1427
  2012
        PEP_decrypt_flags_t *flags
Edouard@741
  2013
    )
Edouard@741
  2014
{
vb@1004
  2015
    return _decrypt_message( session, src, dst, keylist, rating, flags, NULL );
Edouard@741
  2016
}
Edouard@741
  2017
Edouard@728
  2018
DYNAMIC_API PEP_STATUS own_message_private_key_details(
Edouard@728
  2019
        PEP_SESSION session,
Edouard@728
  2020
        message *msg,
krista@1427
  2021
        pEp_identity **ident
Edouard@728
  2022
    )
Edouard@728
  2023
{
Edouard@728
  2024
    assert(session);
Edouard@728
  2025
    assert(msg);
Edouard@728
  2026
    assert(ident);
Edouard@728
  2027
Edouard@736
  2028
    if (!(session && msg && ident))
Edouard@728
  2029
        return PEP_ILLEGAL_VALUE;
Edouard@728
  2030
krista@1427
  2031
    message *dst = NULL;
krista@956
  2032
    stringlist_t *keylist = NULL;
vb@1004
  2033
    PEP_rating rating;
krista@1427
  2034
    PEP_decrypt_flags_t flags;
Edouard@728
  2035
Edouard@729
  2036
    *ident = NULL;
Edouard@729
  2037
Edouard@740
  2038
    identity_list *private_il = NULL;
vb@1004
  2039
    PEP_STATUS status = _decrypt_message(session, msg,  &dst, &keylist, &rating, &flags, &private_il);
vb@953
  2040
    free_message(dst);
vb@953
  2041
    free_stringlist(keylist);
Edouard@728
  2042
Edouard@729
  2043
    if (status == PEP_STATUS_OK &&
Edouard@741
  2044
        flags & PEP_decrypt_flag_own_private_key &&
Edouard@741
  2045
        private_il)
Edouard@729
  2046
    {
Edouard@740
  2047
        *ident = identity_dup(private_il->ident);
Edouard@729
  2048
    }
Edouard@729
  2049
Edouard@741
  2050
    free_identity_list(private_il);
Edouard@741
  2051
Edouard@728
  2052
    return status;
Edouard@741
  2053
Edouard@728
  2054
}
Edouard@728
  2055
Edouard@858
  2056
static void _max_comm_type_from_identity_list(
krista@1427
  2057
        identity_list *identities,
Edouard@858
  2058
        PEP_SESSION session,
Edouard@858
  2059
        PEP_comm_type *max_comm_type,
Edouard@858
  2060
        bool *comm_type_determined
Edouard@858
  2061
    )
Edouard@858
  2062
{
Edouard@858
  2063
    identity_list * il;
Edouard@858
  2064
    for (il = identities; il != NULL; il = il->next)
Edouard@858
  2065
    {
Edouard@858
  2066
        if (il->ident)
Edouard@858
  2067
        {
edouard@1385
  2068
            PEP_STATUS status = update_identity(session, il->ident);
Edouard@858
  2069
            if (status == PEP_STATUS_OK)
Edouard@858
  2070
            {
Edouard@858
  2071
                *max_comm_type = _get_comm_type(session, *max_comm_type,
Edouard@858
  2072
                        il->ident);
Edouard@858
  2073
                *comm_type_determined = true;
Edouard@858
  2074
            }
Edouard@858
  2075
        }
Edouard@858
  2076
    }
Edouard@858
  2077
}
Edouard@858
  2078
vb@1004
  2079
DYNAMIC_API PEP_STATUS outgoing_message_rating(
vb@190
  2080
        PEP_SESSION session,
vb@190
  2081
        message *msg,
vb@1004
  2082
        PEP_rating *rating
vb@190
  2083
    )
vb@190
  2084
{
vb@190
  2085
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  2086
    bool comm_type_determined = false;
vb@190
  2087
vb@190
  2088
    assert(session);
vb@190
  2089
    assert(msg);
vb@251
  2090
    assert(msg->dir == PEP_dir_outgoing);
vb@1004
  2091
    assert(rating);
vb@190
  2092
vb@1004
  2093
    if (!(session && msg && rating))
vb@191
  2094
        return PEP_ILLEGAL_VALUE;
vb@191
  2095
vb@1360
  2096
    if (msg->dir != PEP_dir_outgoing)
vb@251
  2097
        return PEP_ILLEGAL_VALUE;
vb@251
  2098
vb@1004
  2099
    *rating = PEP_rating_undefined;
vb@190
  2100
Edouard@858
  2101
    _max_comm_type_from_identity_list(msg->to, session,
Edouard@858
  2102
                                      &max_comm_type, &comm_type_determined);
vb@190
  2103
Edouard@858
  2104
    _max_comm_type_from_identity_list(msg->cc, session,
Edouard@858
  2105
                                      &max_comm_type, &comm_type_determined);
krista@1427
  2106
Edouard@858
  2107
    _max_comm_type_from_identity_list(msg->bcc, session,
Edouard@858
  2108
                                      &max_comm_type, &comm_type_determined);
vb@190
  2109
vb@190
  2110
    if (comm_type_determined == false)
vb@1004
  2111
        *rating = PEP_rating_undefined;
vb@190
  2112
    else
vb@1004
  2113
        *rating = _MAX(_rating(max_comm_type, PEP_rating_undefined),
vb@486
  2114
                PEP_rating_unencrypted);
vb@190
  2115
vb@190
  2116
    return PEP_STATUS_OK;
vb@190
  2117
}
vb@190
  2118
vb@1004
  2119
DYNAMIC_API PEP_STATUS identity_rating(
vb@239
  2120
        PEP_SESSION session,
vb@239
  2121
        pEp_identity *ident,
vb@1004
  2122
        PEP_rating *rating
vb@239
  2123
    )
vb@239
  2124
{
vb@239
  2125
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  2126
vb@239
  2127
    assert(session);
vb@239
  2128
    assert(ident);
vb@1004
  2129
    assert(rating);
vb@239
  2130
vb@1004
  2131
    if (!(session && ident && rating))
vb@239
  2132
        return PEP_ILLEGAL_VALUE;
vb@239
  2133
vb@239
  2134
    if (ident->me)
edouard@1406
  2135
        status = _myself(session, ident, false, true);
vb@239
  2136
    else
vb@239
  2137
        status = update_identity(session, ident);
vb@239
  2138
vb@239
  2139
    if (status == PEP_STATUS_OK)
vb@1004
  2140
        *rating = _rating(ident->comm_type, PEP_rating_undefined);
vb@239
  2141
vb@239
  2142
    return status;
vb@239
  2143
}
vb@239
  2144
vb@507
  2145
DYNAMIC_API PEP_STATUS get_binary_path(PEP_cryptotech tech, const char **path)
vb@507
  2146
{
vb@507
  2147
    PEP_STATUS status = PEP_STATUS_OK;
vb@507
  2148
vb@507
  2149
    assert(path);
vb@507
  2150
    if (path == NULL)
vb@507
  2151
        return PEP_ILLEGAL_VALUE;
vb@507
  2152
vb@507
  2153
    if (cryptotech[tech].binary_path == NULL)
vb@507
  2154
        *path = NULL;
vb@507
  2155
    else
vb@507
  2156
        status = cryptotech[tech].binary_path(path);
vb@507
  2157
vb@507
  2158
    return status;
vb@507
  2159
}
vb@507
  2160
vb@1004
  2161
vb@1004
  2162
DYNAMIC_API PEP_color color_from_rating(PEP_rating rating)
vb@1004
  2163
{
krista@1243
  2164
    if (rating == PEP_rating_b0rken || rating == PEP_rating_have_no_key)
vb@1004
  2165
        return PEP_color_no_color;
vb@1004
  2166
vb@1004
  2167
    if (rating < PEP_rating_undefined)
vb@1004
  2168
        return PEP_color_red;
vb@1004
  2169
vb@1004
  2170
    if (rating < PEP_rating_reliable)
vb@1004
  2171
        return PEP_color_no_color;
vb@1004
  2172
vb@1004
  2173
    if (rating < PEP_rating_trusted)
vb@1004
  2174
        return PEP_color_yellow;
vb@1004
  2175
vb@1004
  2176
    if (rating >= PEP_rating_trusted)
vb@1004
  2177
        return PEP_color_green;
vb@1004
  2178
vb@1004
  2179
    // this should never happen
vb@1004
  2180
    assert(false);
vb@1475
  2181
	return PEP_color_no_color;
vb@1004
  2182
}
vb@1004
  2183
krista@1315
  2184
static bool _is_valid_hex(const char* hexstr) {
krista@1315
  2185
    if (!hexstr)
krista@1315
  2186
        return false;
krista@1427
  2187
krista@1315
  2188
    const char* curr = hexstr;
krista@1315
  2189
    char currchar;
krista@1427
  2190
krista@1315
  2191
    for (currchar = *curr; currchar != '\0'; currchar = *(++curr)) {
krista@1315
  2192
        if ((currchar >= '0' && currchar <= '9') ||
krista@1315
  2193
            (currchar >= 'a' && currchar <= 'f') ||
krista@1427
  2194
            (currchar >= 'A' && currchar <= 'F'))
krista@1315
  2195
        {
krista@1315
  2196
            continue;
krista@1315
  2197
        }
krista@1315
  2198
        return false;
krista@1315
  2199
    }
krista@1427
  2200
    return true;
krista@1315
  2201
}
krista@1315
  2202
krista@1312
  2203
// Returns, in comparison: 1 if fpr1 > fpr2, 0 if equal, -1 if fpr1 < fpr2
krista@1312
  2204
static PEP_STATUS _compare_fprs(const char* fpr1, const char* fpr2, int* comparison) {
krista@1427
  2205
krista@1312
  2206
    const int _FULL_FINGERPRINT_LENGTH = 40;
krista@1312
  2207
    const int _ASCII_LOWERCASE_OFFSET = 32;
krista@1312
  2208
krista@1312
  2209
    size_t fpr1_len = strlen(fpr1);
krista@1312
  2210
    size_t fpr2_len = strlen(fpr2);
krista@1427
  2211
krista@1312
  2212
    if (fpr1_len != _FULL_FINGERPRINT_LENGTH || fpr2_len != _FULL_FINGERPRINT_LENGTH)
krista@1312
  2213
        return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
krista@1427
  2214
krista@1315
  2215
    if (!_is_valid_hex(fpr1) || !_is_valid_hex(fpr2))
krista@1315
  2216
        return PEP_ILLEGAL_VALUE;
krista@1427
  2217
krista@1312
  2218
    const char* fpr1_curr = fpr1;
krista@1312
  2219
    const char* fpr2_curr = fpr2;
krista@1427
  2220
krista@1312
  2221
    char current;
krista@1312
  2222
krista@1315
  2223
    // Advance past leading zeros.
krista@1313
  2224
    for (current = *fpr1_curr; current != '0' && current != '\0'; current = *(++fpr1_curr), fpr1_len--);
krista@1313
  2225
    for (current = *fpr2_curr; current != '0' && current != '\0'; current = *(++fpr2_curr), fpr2_len--);
krista@1427
  2226
krista@1312
  2227
    if (fpr1_len == fpr2_len) {
krista@1312
  2228
        char digit1;
krista@1312
  2229
        char digit2;
krista@1312
  2230
krista@1315
  2231
        while (fpr1_curr && *fpr1_curr != '\0') {
krista@1312
  2232
            digit1 = *fpr1_curr++;
krista@1312
  2233
            digit2 = *fpr2_curr++;
krista@1312
  2234
krista@1312
  2235
            // Adjust for case-insensitive compare
krista@1312
  2236
            if (digit1 >= 'a' && digit1 <= 'f')
krista@1312
  2237
                digit1 -= _ASCII_LOWERCASE_OFFSET;
krista@1312
  2238
            if (digit2 >= 'a' && digit2 <= 'f')
krista@1312
  2239
                digit2 -= _ASCII_LOWERCASE_OFFSET;
krista@1427
  2240
krista@1312
  2241
            // We take advantage of the fact that 'a'-'f' are larger
krista@1312
  2242
            // integer values in the ASCII table than '0'-'9'.
krista@1312
  2243
            // This allows us to compare digits directly.
krista@1312
  2244
            if (digit1 > digit2) {
krista@1312
  2245
                *comparison = 1;
krista@1312
  2246
                return PEP_STATUS_OK;
krista@1312
  2247
            } else if (digit1 < digit2) {
krista@1312
  2248
                *comparison = -1;
krista@1312
  2249
                return PEP_STATUS_OK;
krista@1312
  2250
            }
krista@1427
  2251
krista@1312
  2252
            // pointers already advanced above. Keep going.
krista@1312
  2253
        }
krista@1312
  2254
        *comparison = 0;
krista@1312
  2255
        return PEP_STATUS_OK;
krista@1312
  2256
    }
krista@1312
  2257
    else if (fpr1_len > fpr2_len) {
krista@1312
  2258
        *comparison = 1;
krista@1312
  2259
        return PEP_STATUS_OK;
krista@1312
  2260
    }
krista@1312
  2261
    // Otherwise, fpr1_len < fpr2_len
krista@1312
  2262
    *comparison = -1;
krista@1312
  2263
    return PEP_STATUS_OK;
krista@1312
  2264
}
krista@1312
  2265
krista@1307
  2266
DYNAMIC_API PEP_STATUS get_trustwords(
roker@1509
  2267
    PEP_SESSION session, const pEp_identity* id1, const pEp_identity* id2,
krista@1307
  2268
    const char* lang, char **words, size_t *wsize, bool full
krista@1307
  2269
)
krista@1307
  2270
{
krista@1307
  2271
    assert(session);
krista@1307
  2272
    assert(id1);
krista@1307
  2273
    assert(id2);
krista@1307
  2274
    assert(id1->fpr);
krista@1307
  2275
    assert(id2->fpr);
krista@1307
  2276
    assert(words);
krista@1307
  2277
    assert(wsize);
krista@1427
  2278
krista@1307
  2279
    if (!(session && id1 && id2 && words && wsize) ||
krista@1307
  2280
        !(id1->fpr) || (!id2->fpr))
krista@1307
  2281
        return PEP_ILLEGAL_VALUE;
krista@1427
  2282
krista@1307
  2283
    const char *source1 = id1->fpr;
krista@1307
  2284
    const char *source2 = id2->fpr;
krista@1427
  2285
krista@1307
  2286
    *words = NULL;
krista@1307
  2287
    *wsize = 0;
krista@1307
  2288
krista@1307
  2289
    const size_t SHORT_NUM_TWORDS = 5;
krista@1427
  2290
krista@1307
  2291
    // N.B. THIS will have to be changed once we start checking trustword entropy.
krista@1307
  2292
    // For now, full is ALL, and otherwise it's 5-per-id.
krista@1307
  2293
    size_t max_words_per_id = (full ? 0 : SHORT_NUM_TWORDS);
krista@1427
  2294
krista@1307
  2295
    char* first_set = NULL;
krista@1307
  2296
    char* second_set = NULL;
krista@1307
  2297
    size_t first_wsize = 0;
krista@1307
  2298
    size_t second_wsize = 0;
krista@1427
  2299
krista@1312
  2300
    int fpr_comparison = -255;
krista@1312
  2301
    PEP_STATUS status = _compare_fprs(source1, source2, &fpr_comparison);
krista@1312
  2302
    if (status != PEP_STATUS_OK)
krista@1312
  2303
        return status;
krista@1312
  2304
krista@1307
  2305
    char* _retstr = NULL;
krista@1312
  2306
krista@1312
  2307
    switch (fpr_comparison) {
krista@1312
  2308
        case 1: // source1 > source2
krista@1312
  2309
            status = trustwords(session, source2, lang, &first_set, &first_wsize, max_words_per_id);
krista@1312
  2310
            if (status != PEP_STATUS_OK)
krista@1312
  2311
                goto error_release;
krista@1427
  2312
            status = trustwords(session, source1, lang, &second_set, &second_wsize, max_words_per_id);
krista@1312
  2313
            if (status != PEP_STATUS_OK)
krista@1312
  2314
                goto error_release;
krista@1312
  2315
            break;
krista@1427
  2316
        case 0:
krista@1312
  2317
        case -1: // source1 <= source2
krista@1312
  2318
            status = trustwords(session, source1, lang, &first_set, &first_wsize, max_words_per_id);
krista@1312
  2319
            if (status != PEP_STATUS_OK)
krista@1312
  2320
                goto error_release;
krista@1427
  2321
            status = trustwords(session, source2, lang, &second_set, &second_wsize, max_words_per_id);
krista@1312
  2322
            if (status != PEP_STATUS_OK)
krista@1312
  2323
                goto error_release;
krista@1315
  2324
            break;
krista@1312
  2325
        default:
krista@1312
  2326
            return PEP_UNKNOWN_ERROR; // shouldn't be possible
krista@1312
  2327
    }
krista@1427
  2328
krista@1307
  2329
    size_t _wsize = first_wsize + second_wsize;
krista@1427
  2330
krista@1315
  2331
    bool needs_space = (first_set[first_wsize - 1] != ' ');
krista@1427
  2332
krista@1315
  2333
    if (needs_space)
krista@1315
  2334
        _wsize++;
krista@1427
  2335
krista@1307
  2336
    _retstr = calloc(1, _wsize + 1);
krista@1427
  2337
krista@1307
  2338
    size_t len = strlcpy(_retstr, first_set, _wsize);
krista@1307
  2339
    if (len >= _wsize) {
krista@1307
  2340
        status = PEP_UNKNOWN_ERROR;
krista@1307
  2341
        goto error_release;
krista@1307
  2342
    }
krista@1315
  2343
    if (needs_space) {
krista@1315
  2344
        strlcat(_retstr, " ", _wsize);
krista@1315
  2345
        if (len >= _wsize) {
krista@1315
  2346
            status = PEP_UNKNOWN_ERROR;
krista@1315
  2347
            goto error_release;
krista@1315
  2348
        }
krista@1315
  2349
    }
krista@1307
  2350
    strlcat(_retstr, second_set, _wsize);
krista@1307
  2351
    if (len >= _wsize){
krista@1307
  2352
        status = PEP_UNKNOWN_ERROR;
krista@1307
  2353
        goto error_release;
krista@1307
  2354
    }
krista@1427
  2355
krista@1307
  2356
    *words = _retstr;
krista@1307
  2357
    *wsize = _wsize;
krista@1307
  2358
    status = PEP_STATUS_OK;
krista@1427
  2359
krista@1307
  2360
    goto the_end;
krista@1427
  2361
krista@1307
  2362
    error_release:
krista@1307
  2363
    free(_retstr);
krista@1427
  2364
krista@1307
  2365
    the_end:
krista@1307
  2366
    free(first_set);
krista@1307
  2367
    free(second_set);
krista@1307
  2368
    return status;
vb@1309
  2369
}
vb@1309
  2370
edouard@1553
  2371
DYNAMIC_API PEP_STATUS get_message_trustwords(
edouard@1553
  2372
    PEP_SESSION session, 
edouard@1553
  2373
    message *msg,
edouard@1553
  2374
    stringlist_t *keylist,
edouard@1553
  2375
    pEp_identity* received_by,
edouard@1553
  2376
    const char* lang, char **words, bool full
edouard@1553
  2377
)
edouard@1553
  2378
{
edouard@1553
  2379
    assert(session);
edouard@1553
  2380
    assert(msg);
edouard@1553
  2381
    assert(received_by);
edouard@1553
  2382
    assert(received_by->address);
edouard@1553
  2383
    assert(lang);
edouard@1553
  2384
    assert(words);
edouard@1553
  2385
edouard@1553
  2386
    if (!(session && 
edouard@1553
  2387
          msg &&
edouard@1553
  2388
          received_by && 
edouard@1553
  2389
          received_by->address && 
edouard@1553
  2390
          lang && 
edouard@1553
  2391
          words))
edouard@1553
  2392
        return PEP_ILLEGAL_VALUE;
edouard@1553
  2393
    
edouard@1557
  2394
    pEp_identity* partner = NULL;
edouard@1553
  2395
     
edouard@1553
  2396
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1553
  2397
    
edouard@1553
  2398
    *words = NULL;
edouard@1553
  2399
edouard@1553
  2400
    // We want fingerprint of key that did sign the message
edouard@1553
  2401
edouard@1553
  2402
    if (keylist == NULL) {
edouard@1553
  2403
edouard@1553
  2404
        // Message is to be decrypted
roker@1554
  2405
        message *dst = NULL;
edouard@1553
  2406
        stringlist_t *_keylist = keylist;
edouard@1553
  2407
        PEP_rating rating;
edouard@1553
  2408
        PEP_decrypt_flags_t flags;
edouard@1553
  2409
        status = decrypt_message( session, msg, &dst, &_keylist, &rating, &flags);
edouard@1553
  2410
edouard@1553
  2411
        if (status != PEP_STATUS_OK) {
edouard@1553
  2412
            free_message(dst);
edouard@1553
  2413
            free_stringlist(_keylist);
edouard@1553
  2414
            return status;
edouard@1553
  2415
        }
edouard@1553
  2416
edouard@1553
  2417
        if (dst && dst->from && _keylist) {
edouard@1553
  2418
            partner = identity_dup(dst->from); 
edouard@1553
  2419
            if(partner){
edouard@1553
  2420
                free(partner->fpr);
edouard@1553
  2421
                partner->fpr = strdup(_keylist->value);
edouard@1553
  2422
                if (partner->fpr == NULL)
edouard@1553
  2423
                    status = PEP_OUT_OF_MEMORY;
edouard@1553
  2424
            } else {
edouard@1553
  2425
                status = PEP_OUT_OF_MEMORY;
edouard@1553
  2426
            }
edouard@1553
  2427
        } else {
edouard@1553
  2428
            status = PEP_UNKNOWN_ERROR;
edouard@1553
  2429
        }
edouard@1553
  2430
edouard@1553
  2431
        free_message(dst);
edouard@1553
  2432
        free_stringlist(_keylist);
edouard@1553
  2433
edouard@1553
  2434
    } else {
edouard@1553
  2435
edouard@1553
  2436
        // Message already decrypted
edouard@1553
  2437
        if (keylist->value) {
edouard@1553
  2438
            partner = identity_dup(msg->from); 
edouard@1553
  2439
            if(partner){
edouard@1553
  2440
                free(partner->fpr);
edouard@1553
  2441
                partner->fpr = strdup(keylist->value);
edouard@1553
  2442
                if (partner->fpr == NULL)
edouard@1553
  2443
                    status = PEP_OUT_OF_MEMORY;
edouard@1553
  2444
            } else {
edouard@1553
  2445
                status = PEP_OUT_OF_MEMORY;
edouard@1553
  2446
            }
edouard@1553
  2447
        } else {
edouard@1553
  2448
            status = PEP_ILLEGAL_VALUE;
edouard@1553
  2449
        }
edouard@1553
  2450
    }
edouard@1553
  2451
edouard@1553
  2452
    if (status != PEP_STATUS_OK) {
edouard@1553
  2453
        free_identity(partner);
edouard@1553
  2454
        return status;
edouard@1553
  2455
    }
edouard@1553
  2456
   
edouard@1553
  2457
    // Find own identity corresponding to given account address.
edouard@1553
  2458
    // In that case we want default key attached to own identity
edouard@1553
  2459
    pEp_identity *stored_identity = NULL;
edouard@1553
  2460
    status = get_identity(session,
edouard@1553
  2461
                          received_by->address,
edouard@1553
  2462
                          PEP_OWN_USERID,
edouard@1553
  2463
                          &stored_identity);
edouard@1553
  2464
edouard@1553
  2465
    if (status != PEP_STATUS_OK) {
edouard@1553
  2466
        free_identity(stored_identity);
edouard@1553
  2467
        return status;
edouard@1553
  2468
    }
edouard@1553
  2469
edouard@1553
  2470
    // get the trustwords
edouard@1553
  2471
    size_t wsize;
edouard@1553
  2472
    status = get_trustwords(session, 
edouard@1553
  2473
                            partner, received_by, 
edouard@1553
  2474
                            lang, words, &wsize, full);
edouard@1553
  2475
edouard@1553
  2476
    return status;
edouard@1553
  2477
}
krista@1325
  2478
krista@1325
  2479
DYNAMIC_API PEP_STATUS MIME_decrypt_message(
krista@1325
  2480
    PEP_SESSION session,
krista@1325
  2481
    const char *mimetext,
krista@1325
  2482
    size_t size,
krista@1325
  2483
    char** mime_plaintext,
krista@1325
  2484
    stringlist_t **keylist,
krista@1325
  2485
    PEP_rating *rating,
krista@1325
  2486
    PEP_decrypt_flags_t *flags
krista@1427
  2487
)
krista@1325
  2488
{
krista@1325
  2489
    PEP_STATUS status = PEP_STATUS_OK;
krista@1325
  2490
    message* tmp_msg = NULL;
krista@1325
  2491
    message* dec_msg = NULL;
krista@1427
  2492
krista@1325
  2493
    status = mime_decode_message(mimetext, size, &tmp_msg);
krista@1325
  2494
    if (status != PEP_STATUS_OK)
krista@1325
  2495
        goto pep_error;
krista@1427
  2496
krista@1325
  2497
    status = decrypt_message(session,
krista@1325
  2498
                             tmp_msg,
krista@1325
  2499
                             &dec_msg,
krista@1325
  2500
                             keylist,
krista@1325
  2501
                             rating,
krista@1325
  2502
                             flags);
krista@1325
  2503
    if (status != PEP_STATUS_OK)
krista@1325
  2504
        goto pep_error;
krista@1427
  2505
krista@1325
  2506
    status = mime_encode_message(dec_msg, false, mime_plaintext);
krista@1427
  2507
krista@1325
  2508
pep_error:
krista@1325
  2509
    free_message(tmp_msg);
krista@1325
  2510
    free_message(dec_msg);
krista@1427
  2511
krista@1325
  2512
    return status;
krista@1325
  2513
}
krista@1325
  2514
krista@1325
  2515
DYNAMIC_API PEP_STATUS MIME_encrypt_message(
krista@1325
  2516
    PEP_SESSION session,
krista@1325
  2517
    const char *mimetext,
krista@1325
  2518
    size_t size,
krista@1325
  2519
    stringlist_t* extra,
krista@1325
  2520
    char** mime_ciphertext,
krista@1325
  2521
    PEP_enc_format enc_format,
krista@1325
  2522
    PEP_encrypt_flags_t flags
krista@1427
  2523
)
krista@1325
  2524
{
krista@1325
  2525
    PEP_STATUS status = PEP_STATUS_OK;
krista@1325
  2526
    message* tmp_msg = NULL;
krista@1325
  2527
    message* enc_msg = NULL;
krista@1427
  2528
krista@1325
  2529
    status = mime_decode_message(mimetext, size, &tmp_msg);
krista@1325
  2530
    if (status != PEP_STATUS_OK)
krista@1325
  2531
        goto pep_error;
krista@1427
  2532
krista@1325
  2533
    // This isn't incoming, though... so we need to reverse the direction
krista@1325
  2534
    tmp_msg->dir = PEP_dir_outgoing;
krista@1325
  2535
    status = encrypt_message(session,
krista@1325
  2536
                             tmp_msg,
krista@1325
  2537
                             extra,
krista@1325
  2538
                             &enc_msg,
krista@1325
  2539
                             enc_format,
krista@1325
  2540
                             flags);
krista@1325
  2541
    if (status != PEP_STATUS_OK)
krista@1325
  2542
        goto pep_error;
krista@1427
  2543
krista@1325
  2544
    status = mime_encode_message(enc_msg, false, mime_ciphertext);
krista@1427
  2545
krista@1325
  2546
pep_error:
krista@1325
  2547
    free_message(tmp_msg);
krista@1325
  2548
    free_message(enc_msg);
krista@1325
  2549
krista@1325
  2550
    return status;
krista@1325
  2551
krista@1325
  2552
}