src/message_api.c
author Krista Grothoff <krista@pep-project.org>
Tue, 12 Jul 2016 19:52:16 +0200
changeset 854 0d068c87fa66
parent 853 2a7d4445fca0
child 855 27364c55fbaa
permissions -rw-r--r--
clarify desired behaviour for #34 (is_fileending()).
vb@125
     1
#include "pEp_internal.h"
vb@37
     2
#include "message_api.h"
vb@37
     3
vb@130
     4
#include "platform.h"
vb@220
     5
#include "mime.h"
vb@85
     6
vb@37
     7
#include <assert.h>
vb@37
     8
#include <string.h>
vb@39
     9
#include <stdlib.h>
vb@39
    10
vb@190
    11
#ifndef MIN
vb@190
    12
#define MIN(A, B) ((B) > (A) ? (A) : (B))
vb@190
    13
#endif
vb@300
    14
#ifndef MAX
vb@300
    15
#define MAX(A, B) ((B) > (A) ? (B) : (A))
vb@300
    16
#endif
vb@190
    17
vb@235
    18
vb@235
    19
static bool string_equality(const char *s1, const char *s2)
vb@235
    20
{
vb@235
    21
    if (s1 == NULL || s2 == NULL)
vb@235
    22
        return false;
vb@235
    23
vb@235
    24
    assert(s1 && s2);
vb@235
    25
vb@235
    26
    return strcmp(s1, s2) == 0;
vb@235
    27
}
vb@235
    28
vb@235
    29
static bool is_mime_type(const bloblist_t *bl, const char *mt)
vb@235
    30
{
vb@235
    31
    assert(mt);
vb@235
    32
vb@235
    33
    return bl && string_equality(bl->mime_type, mt);
vb@235
    34
}
vb@235
    35
krista@854
    36
//
krista@854
    37
// This function presumes the file ending is a proper substring of the
krista@854
    38
// filename (i.e. if bl->filename is "a.pgp" and fe is ".pgp", it will
krista@854
    39
// return true, but if bl->filename is ".pgp" and fe is ".pgp", it will
krista@854
    40
// return false. This is desired behaviour.
krista@854
    41
//
vb@235
    42
static bool is_fileending(const bloblist_t *bl, const char *fe)
vb@235
    43
{
vb@235
    44
    assert(fe);
krista@853
    45
    
krista@853
    46
    if (bl == NULL || bl->filename == NULL || fe == NULL)
vb@235
    47
        return false;
vb@235
    48
vb@235
    49
    assert(bl && bl->filename);
vb@235
    50
vb@235
    51
    size_t fe_len = strlen(fe);
vb@235
    52
    size_t fn_len = strlen(bl->filename);
vb@235
    53
vb@235
    54
    if (fn_len <= fe_len)
vb@235
    55
        return false;
vb@235
    56
vb@235
    57
    assert(fn_len > fe_len);
vb@235
    58
vb@235
    59
    return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
vb@235
    60
}
vb@235
    61
vb@284
    62
static void add_opt_field(message *msg, const char *name, const char *value)
vb@284
    63
{
vb@284
    64
    assert(msg);
vb@284
    65
vb@284
    66
    if (msg && name && value) {
vb@284
    67
        stringpair_t *pair = new_stringpair(name, value);
vb@284
    68
        if (pair == NULL)
vb@284
    69
            return;
vb@284
    70
vb@284
    71
        stringpair_list_t *field = stringpair_list_add(msg->opt_fields, pair);
vb@284
    72
        if (field == NULL)
Edouard@833
    73
        {
Edouard@833
    74
            free_stringpair(pair);
vb@284
    75
            return;
Edouard@833
    76
        }
vb@284
    77
vb@284
    78
        if (msg->opt_fields == NULL)
vb@284
    79
            msg->opt_fields = field;
vb@284
    80
    }
vb@284
    81
}
vb@284
    82
vb@83
    83
static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
vb@62
    84
{
vb@62
    85
    char * ptext;
vb@81
    86
vb@83
    87
    assert(shortmsg);
vb@83
    88
    assert(strcmp(shortmsg, "pEp") != 0);
krista@853
    89
    // Will crash without assert if shortmsg == NULL in strlen
krista@853
    90
    if (shortmsg == NULL)
krista@853
    91
        shortmsg = strdup(""); // Should this be "pEp", as above?
krista@853
    92
        
vb@83
    93
    if (longmsg == NULL)
vb@63
    94
        longmsg = "";
vb@63
    95
vb@83
    96
    ptext = calloc(1, strlen(shortmsg) + strlen(longmsg) + 12);
vb@109
    97
    assert(ptext);
vb@62
    98
    if (ptext == NULL)
vb@62
    99
        return NULL;
vb@62
   100
vb@85
   101
    strcpy(ptext, "Subject: ");
vb@83
   102
    strcat(ptext, shortmsg);
vb@62
   103
    strcat(ptext, "\n\n");
vb@63
   104
    strcat(ptext, longmsg);
vb@62
   105
vb@62
   106
    return ptext;
vb@62
   107
}
vb@44
   108
krista@853
   109
static int separate_short_and_long(const char *src, char **shortmsg, char **longmsg)
vb@82
   110
{
vb@82
   111
    char *_shortmsg = NULL;
vb@82
   112
    char *_longmsg = NULL;
vb@82
   113
vb@82
   114
    assert(src);
vb@82
   115
    assert(shortmsg);
vb@82
   116
    assert(longmsg);
krista@853
   117
    
krista@853
   118
    if (src == NULL || shortmsg == NULL || longmsg == NULL)
krista@853
   119
        return -1;
vb@82
   120
vb@82
   121
    *shortmsg = NULL;
vb@82
   122
    *longmsg = NULL;
vb@82
   123
vb@85
   124
    if (strncasecmp(src, "subject: ", 9) == 0) {
vb@82
   125
        char *line_end = strchr(src, '\n');
vb@311
   126
vb@82
   127
        if (line_end == NULL) {
vb@82
   128
            _shortmsg = strdup(src + 9);
vb@469
   129
            assert(_shortmsg);
vb@82
   130
            if (_shortmsg == NULL)
vb@82
   131
                goto enomem;
vb@82
   132
            // _longmsg = NULL;
vb@82
   133
        }
vb@82
   134
        else {
vb@82
   135
            size_t n = line_end - src;
vb@166
   136
vb@82
   137
            if (*(line_end - 1) == '\r')
vb@166
   138
                _shortmsg = strndup(src + 9, n - 10);
vb@82
   139
            else
vb@166
   140
                _shortmsg = strndup(src + 9, n - 9);
vb@469
   141
            assert(_shortmsg);
vb@82
   142
            if (_shortmsg == NULL)
vb@82
   143
                goto enomem;
vb@166
   144
vb@166
   145
            while (*(src + n) && (*(src + n) == '\n' || *(src + n) == '\r'))
vb@166
   146
                ++n;
vb@166
   147
vb@166
   148
            if (*(src + n)) {
vb@166
   149
                _longmsg = strdup(src + n);
vb@469
   150
                assert(_longmsg);
vb@166
   151
                if (_longmsg == NULL)
vb@166
   152
                    goto enomem;
vb@166
   153
            }
vb@82
   154
        }
vb@82
   155
    }
vb@82
   156
    else {
vb@113
   157
        _shortmsg = strdup("");
vb@469
   158
        assert(_shortmsg);
vb@82
   159
        if (_shortmsg == NULL)
vb@82
   160
            goto enomem;
vb@82
   161
        _longmsg = strdup(src);
vb@469
   162
        assert(_longmsg);
vb@82
   163
        if (_longmsg == NULL)
vb@82
   164
            goto enomem;
vb@82
   165
    }
vb@311
   166
vb@82
   167
    *shortmsg = _shortmsg;
vb@82
   168
    *longmsg = _longmsg;
vb@82
   169
vb@82
   170
    return 0;
vb@82
   171
vb@82
   172
enomem:
vb@82
   173
    free(_shortmsg);
vb@82
   174
    free(_longmsg);
vb@82
   175
vb@82
   176
    return -1;
vb@82
   177
}
vb@82
   178
vb@113
   179
static PEP_STATUS copy_fields(message *dst, const message *src)
vb@113
   180
{
vb@164
   181
    assert(dst);
vb@164
   182
    assert(src);
vb@164
   183
Edouard@840
   184
    if(!(dst && src))
Edouard@840
   185
        return PEP_ILLEGAL_VALUE;
Edouard@840
   186
vb@113
   187
    free_timestamp(dst->sent);
vb@113
   188
    dst->sent = NULL;
vb@113
   189
    if (src->sent) {
vb@113
   190
        dst->sent = timestamp_dup(src->sent);
vb@113
   191
        if (dst->sent == NULL)
vb@113
   192
            return PEP_OUT_OF_MEMORY;
vb@113
   193
    }
vb@113
   194
vb@113
   195
    free_timestamp(dst->recv);
vb@113
   196
    dst->recv = NULL;
vb@113
   197
    if (src->recv) {
vb@113
   198
        dst->recv = timestamp_dup(src->recv);
vb@113
   199
        if (dst->recv == NULL)
vb@113
   200
            return PEP_OUT_OF_MEMORY;
vb@113
   201
    }
vb@113
   202
vb@113
   203
    free_identity(dst->from);
vb@113
   204
    dst->from = NULL;
vb@113
   205
    if (src->from) {
vb@113
   206
        dst->from = identity_dup(src->from);
vb@113
   207
        if (dst->from == NULL)
vb@113
   208
            return PEP_OUT_OF_MEMORY;
vb@113
   209
    }
vb@113
   210
vb@113
   211
    free_identity_list(dst->to);
vb@113
   212
    dst->to = NULL;
vb@274
   213
    if (src->to && src->to->ident) {
vb@113
   214
        dst->to = identity_list_dup(src->to);
vb@113
   215
        if (dst->to == NULL)
vb@113
   216
            return PEP_OUT_OF_MEMORY;
vb@113
   217
    }
vb@113
   218
vb@113
   219
    free_identity(dst->recv_by);
vb@113
   220
    dst->recv_by = NULL;
vb@113
   221
    if (src->recv_by) {
vb@113
   222
        dst->recv_by = identity_dup(src->recv_by);
vb@113
   223
        if (dst->recv_by == NULL)
vb@113
   224
            return PEP_OUT_OF_MEMORY;
vb@113
   225
    }
vb@113
   226
vb@113
   227
    free_identity_list(dst->cc);
vb@113
   228
    dst->cc = NULL;
vb@274
   229
    if (src->cc && src->cc->ident) {
vb@113
   230
        dst->cc = identity_list_dup(src->cc);
vb@113
   231
        if (dst->cc == NULL)
vb@113
   232
            return PEP_OUT_OF_MEMORY;
vb@113
   233
    }
vb@113
   234
vb@113
   235
    free_identity_list(dst->bcc);
vb@113
   236
    dst->bcc = NULL;
vb@274
   237
    if (src->bcc && src->bcc->ident) {
vb@113
   238
        dst->bcc = identity_list_dup(src->bcc);
vb@113
   239
        if (dst->bcc == NULL)
vb@113
   240
            return PEP_OUT_OF_MEMORY;
vb@113
   241
    }
vb@113
   242
vb@113
   243
    free_identity_list(dst->reply_to);
vb@113
   244
    dst->reply_to = NULL;
vb@274
   245
    if (src->reply_to && src->reply_to->ident) {
vb@113
   246
        dst->reply_to = identity_list_dup(src->reply_to);
vb@113
   247
        if (dst->reply_to == NULL)
vb@113
   248
            return PEP_OUT_OF_MEMORY;
vb@113
   249
    }
vb@113
   250
vb@113
   251
    free_stringlist(dst->in_reply_to);
vb@113
   252
    dst->in_reply_to = NULL;
vb@274
   253
    if (src->in_reply_to && src->in_reply_to->value) {
vb@113
   254
        dst->in_reply_to = stringlist_dup(src->in_reply_to);
vb@113
   255
        if (dst->in_reply_to == NULL)
vb@113
   256
            return PEP_OUT_OF_MEMORY;
vb@113
   257
    }
vb@113
   258
vb@113
   259
    free_stringlist(dst->references);
vb@113
   260
    dst->references = NULL;
vb@113
   261
    if (src->references) {
vb@113
   262
        dst->references = stringlist_dup(src->references);
vb@113
   263
        if (dst->references == NULL)
vb@113
   264
            return PEP_OUT_OF_MEMORY;
vb@113
   265
    }
vb@113
   266
vb@113
   267
    free_stringlist(dst->keywords);
vb@113
   268
    dst->keywords = NULL;
vb@274
   269
    if (src->keywords && src->keywords->value) {
vb@113
   270
        dst->keywords = stringlist_dup(src->keywords);
vb@113
   271
        if (dst->keywords == NULL)
vb@113
   272
            return PEP_OUT_OF_MEMORY;
vb@113
   273
    }
vb@113
   274
vb@113
   275
    free(dst->comments);
vb@113
   276
    dst->comments = NULL;
vb@113
   277
    if (src->comments) {
vb@113
   278
        dst->comments = strdup(src->comments);
vb@113
   279
        assert(dst->comments);
vb@113
   280
        if (dst->comments == NULL)
vb@113
   281
            return PEP_OUT_OF_MEMORY;
vb@113
   282
    }
vb@113
   283
vb@113
   284
    return PEP_STATUS_OK;
vb@113
   285
}
vb@113
   286
vb@81
   287
static message * clone_to_empty_message(const message * src)
vb@80
   288
{
vb@113
   289
    PEP_STATUS status;
vb@80
   290
    message * msg = NULL;
vb@80
   291
vb@81
   292
    assert(src);
krista@853
   293
    if (src == NULL)
krista@853
   294
        return NULL;
vb@81
   295
vb@113
   296
    msg = calloc(1, sizeof(message));
vb@113
   297
    assert(msg);
vb@80
   298
    if (msg == NULL)
vb@80
   299
        goto enomem;
vb@80
   300
vb@82
   301
    msg->dir = src->dir;
vb@82
   302
vb@113
   303
    status = copy_fields(msg, src);
vb@113
   304
    if (status != PEP_STATUS_OK)
vb@113
   305
        goto enomem;
vb@81
   306
vb@80
   307
    return msg;
vb@80
   308
vb@80
   309
enomem:
vb@113
   310
    free_message(msg);
vb@80
   311
    return NULL;
vb@80
   312
}
vb@80
   313
vb@260
   314
static PEP_STATUS encrypt_PGP_MIME(
vb@311
   315
    PEP_SESSION session,
vb@311
   316
    const message *src,
vb@311
   317
    stringlist_t *keys,
vb@311
   318
    message *dst
vb@260
   319
    )
vb@260
   320
{
vb@260
   321
    PEP_STATUS status = PEP_STATUS_OK;
vb@260
   322
    bool free_ptext = false;
vb@468
   323
    char *ptext = NULL;
vb@288
   324
    char *ctext;
vb@288
   325
    char *_ctext = NULL;
vb@260
   326
    char *mimetext = NULL;
vb@260
   327
    size_t csize;
vb@260
   328
    assert(dst->longmsg == NULL);
vb@260
   329
    dst->enc_format = PEP_enc_PGP_MIME;
vb@260
   330
vb@260
   331
    if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@466
   332
        if (session->unencrypted_subject) {
vb@466
   333
            dst->shortmsg = strdup(src->shortmsg);
vb@469
   334
            assert(dst->shortmsg);
vb@466
   335
            if (dst->shortmsg == NULL)
vb@466
   336
                goto enomem;
vb@466
   337
            ptext = src->longmsg;
vb@466
   338
        }
vb@466
   339
        else {
vb@466
   340
            ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@466
   341
            if (ptext == NULL)
vb@466
   342
                goto enomem;
vb@466
   343
            free_ptext = true;
vb@466
   344
        }
vb@260
   345
    }
vb@260
   346
    else if (src->longmsg) {
vb@260
   347
        ptext = src->longmsg;
vb@260
   348
    }
vb@260
   349
    else {
vb@260
   350
        ptext = "pEp";
vb@260
   351
    }
vb@260
   352
vb@260
   353
    message *_src = calloc(1, sizeof(message));
vb@260
   354
    assert(_src);
vb@260
   355
    if (_src == NULL)
vb@260
   356
        goto enomem;
vb@260
   357
    _src->longmsg = ptext;
vb@260
   358
    _src->longmsg_formatted = src->longmsg_formatted;
vb@260
   359
    _src->attachments = src->attachments;
vb@260
   360
    _src->enc_format = PEP_enc_none;
vb@260
   361
    status = mime_encode_message(_src, true, &mimetext);
vb@260
   362
    assert(status == PEP_STATUS_OK);
krista@853
   363
    if (status != PEP_STATUS_OK)
krista@853
   364
        goto pep_error;
krista@853
   365
    
Edouard@371
   366
    if (free_ptext){
vb@260
   367
        free(ptext);
Edouard@371
   368
        free_ptext=0;
Edouard@371
   369
    }
vb@260
   370
    free(_src);
vb@260
   371
    assert(mimetext);
vb@260
   372
    if (mimetext == NULL)
vb@260
   373
        goto pep_error;
vb@260
   374
vb@260
   375
    status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
vb@311
   376
        &ctext, &csize);
vb@260
   377
    free(mimetext);
vb@260
   378
    if (ctext == NULL)
vb@260
   379
        goto pep_error;
vb@260
   380
vb@260
   381
    dst->longmsg = strdup("this message was encrypted with p≡p "
lix@487
   382
        "https://pEp-project.org");
vb@469
   383
    assert(dst->longmsg);
vb@260
   384
    if (dst->longmsg == NULL)
vb@260
   385
        goto enomem;
vb@260
   386
vb@260
   387
    char *v = strdup("Version: 1");
vb@469
   388
    assert(v);
vb@260
   389
    if (v == NULL)
vb@260
   390
        goto enomem;
vb@260
   391
roker@801
   392
    bloblist_t *_a = new_bloblist(v, strlen(v), "application/pgp-encrypted", NULL);
vb@260
   393
    if (_a == NULL)
vb@260
   394
        goto enomem;
vb@260
   395
    dst->attachments = _a;
vb@288
   396
vb@288
   397
    _ctext = malloc(csize);
vb@288
   398
    assert(_ctext);
vb@288
   399
    if (_ctext == NULL)
vb@288
   400
        goto enomem;
vb@288
   401
    memcpy(_ctext, ctext, csize);
vb@288
   402
vb@288
   403
    _a = bloblist_add(_a, _ctext, csize, "application/octet-stream",
Edouard@427
   404
        "msg.asc");
vb@260
   405
    if (_a == NULL)
vb@260
   406
        goto enomem;
vb@260
   407
vb@260
   408
    return PEP_STATUS_OK;
vb@260
   409
vb@260
   410
enomem:
vb@260
   411
    status = PEP_OUT_OF_MEMORY;
vb@260
   412
vb@260
   413
pep_error:
vb@260
   414
    if (free_ptext)
vb@260
   415
        free(ptext);
vb@288
   416
    free(_ctext);
vb@260
   417
    return status;
vb@260
   418
}
vb@260
   419
vb@260
   420
static PEP_STATUS encrypt_PGP_in_pieces(
vb@311
   421
    PEP_SESSION session,
vb@311
   422
    const message *src,
vb@311
   423
    stringlist_t *keys,
vb@311
   424
    message *dst
vb@260
   425
    )
vb@260
   426
{
vb@260
   427
    PEP_STATUS status = PEP_STATUS_OK;
vb@288
   428
    char *ctext;
vb@260
   429
    size_t csize;
vb@468
   430
    char *ptext = NULL;
vb@466
   431
    bool free_ptext = false;
vb@260
   432
vb@260
   433
    assert(dst->longmsg == NULL);
vb@260
   434
    assert(dst->attachments == NULL);
vb@260
   435
vb@260
   436
    dst->enc_format = PEP_enc_pieces;
vb@260
   437
vb@320
   438
    if (src->shortmsg && src->shortmsg[0] && strcmp(src->shortmsg, "pEp") != 0) {
vb@466
   439
        if (session->unencrypted_subject) {
vb@466
   440
            dst->shortmsg = strdup(src->shortmsg);
vb@469
   441
            assert(dst->shortmsg);
vb@466
   442
            if (dst->shortmsg == NULL)
vb@466
   443
                goto enomem;
vb@466
   444
            ptext = src->longmsg;
vb@466
   445
        }
vb@466
   446
        else {
vb@466
   447
            ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@466
   448
            if (ptext == NULL)
vb@466
   449
                goto enomem;
vb@466
   450
            free_ptext = true;
vb@466
   451
        }
vb@260
   452
vb@260
   453
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   454
            &csize);
vb@466
   455
        if (free_ptext)
vb@466
   456
            free(ptext);
vb@466
   457
        free_ptext = false;
vb@288
   458
        if (ctext) {
vb@288
   459
            dst->longmsg = strndup(ctext, csize);
vb@288
   460
            assert(dst->longmsg);
vb@288
   461
            if (dst->longmsg == NULL)
vb@288
   462
                goto enomem;
vb@288
   463
        }
vb@288
   464
        else {
vb@260
   465
            goto pep_error;
vb@288
   466
        }
vb@260
   467
    }
vb@320
   468
    else if (src->longmsg && src->longmsg[0]) {
vb@466
   469
        ptext = src->longmsg;
vb@260
   470
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   471
            &csize);
vb@288
   472
        if (ctext) {
vb@288
   473
            dst->longmsg = strndup(ctext, csize);
vb@288
   474
            assert(dst->longmsg);
vb@288
   475
            if (dst->longmsg == NULL)
vb@288
   476
                goto enomem;
vb@288
   477
        }
vb@288
   478
        else {
vb@260
   479
            goto pep_error;
vb@288
   480
        }
vb@284
   481
    }
vb@284
   482
    else {
vb@284
   483
        dst->longmsg = strdup("");
vb@469
   484
        assert(dst->longmsg);
vb@284
   485
        if (dst->longmsg == NULL)
vb@284
   486
            goto enomem;
vb@260
   487
    }
vb@260
   488
vb@320
   489
    if (src->longmsg_formatted && src->longmsg_formatted[0]) {
vb@466
   490
        ptext = src->longmsg_formatted;
vb@260
   491
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   492
            &csize);
vb@260
   493
        if (ctext) {
vb@288
   494
            char *_ctext = malloc(csize);
vb@288
   495
            assert(_ctext);
vb@288
   496
            if (_ctext == NULL)
vb@288
   497
                goto enomem;
vb@288
   498
            memcpy(_ctext, ctext, csize);
vb@288
   499
vb@288
   500
            bloblist_t *_a = bloblist_add(dst->attachments, _ctext, csize,
vb@311
   501
                "application/octet-stream", "PGPexch.htm.pgp");
vb@284
   502
            if (_a == NULL)
vb@260
   503
                goto enomem;
vb@284
   504
            if (dst->attachments == NULL)
vb@284
   505
                dst->attachments = _a;
vb@260
   506
        }
vb@260
   507
        else {
vb@260
   508
            goto pep_error;
vb@260
   509
        }
vb@260
   510
    }
vb@260
   511
vb@260
   512
    if (src->attachments) {
vb@284
   513
        if (dst->attachments == NULL) {
vb@284
   514
            dst->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@284
   515
            if (dst->attachments == NULL)
vb@284
   516
                goto enomem;
vb@284
   517
        }
vb@260
   518
vb@284
   519
        bloblist_t *_s = src->attachments;
vb@284
   520
        bloblist_t *_d = dst->attachments;
vb@284
   521
Edouard@754
   522
        for (int n = 0; _s; _s = _s->next) {
Edouard@754
   523
            if (_s->value == NULL && _s->size == 0) {
Edouard@754
   524
                _d = bloblist_add(_d, NULL, 0, _s->mime_type, _s->filename);
vb@260
   525
                if (_d == NULL)
vb@260
   526
                    goto enomem;
vb@260
   527
            }
vb@260
   528
            else {
Edouard@754
   529
                size_t psize = _s->size;
Edouard@754
   530
                ptext = _s->value;
Edouard@754
   531
                status = encrypt_and_sign(session, keys, ptext, psize, &ctext,
Edouard@754
   532
                    &csize);
Edouard@754
   533
                if (ctext) {
Edouard@754
   534
                    char *filename = NULL;
Edouard@754
   535
Edouard@754
   536
                    if (_s->filename) {
Edouard@754
   537
                        size_t len = strlen(_s->filename);
Edouard@754
   538
                        filename = calloc(1, len + 5);
Edouard@754
   539
                        if (filename == NULL)
Edouard@754
   540
                            goto enomem;
Edouard@754
   541
Edouard@754
   542
                        strcpy(filename, _s->filename);
Edouard@754
   543
                        strcpy(filename + len, ".pgp");
Edouard@754
   544
                    }
Edouard@754
   545
                    else {
Edouard@754
   546
                        filename = calloc(1, 20);
Edouard@754
   547
                        if (filename == NULL)
Edouard@754
   548
                            goto enomem;
Edouard@754
   549
Edouard@754
   550
                        ++n;
Edouard@754
   551
                        n &= 0xffff;
Edouard@754
   552
                        snprintf(filename, 20, "Attachment%d.pgp", n);
Edouard@754
   553
                    }
Edouard@754
   554
Edouard@754
   555
                    char *_ctext = malloc(csize);
Edouard@754
   556
                    assert(_ctext);
Edouard@754
   557
                    if (_ctext == NULL)
Edouard@754
   558
                        goto enomem;
Edouard@754
   559
                    memcpy(_ctext, ctext, csize);
Edouard@754
   560
Edouard@754
   561
                    _d = bloblist_add(_d, _ctext, csize, "application/octet-stream",
Edouard@754
   562
                        filename);
Edouard@754
   563
                    if (_d == NULL)
Edouard@754
   564
                        goto enomem;
Edouard@754
   565
                }
Edouard@754
   566
                else {
Edouard@754
   567
                    goto pep_error;
Edouard@754
   568
                }
vb@260
   569
            }
vb@260
   570
        }
vb@260
   571
    }
vb@260
   572
vb@260
   573
    return PEP_STATUS_OK;
vb@260
   574
vb@260
   575
enomem:
vb@260
   576
    status = PEP_OUT_OF_MEMORY;
vb@260
   577
vb@260
   578
pep_error:
vb@466
   579
    if (free_ptext)
vb@466
   580
        free(ptext);
vb@260
   581
    return status;
vb@260
   582
}
vb@260
   583
vb@311
   584
static char * keylist_to_string(const stringlist_t *keylist)
vb@311
   585
{
vb@311
   586
    if (keylist) {
vb@311
   587
        size_t size = stringlist_length(keylist);
vb@311
   588
vb@311
   589
        const stringlist_t *_kl;
vb@311
   590
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   591
            size += strlen(_kl->value);
vb@311
   592
        }
vb@311
   593
vb@311
   594
        char *result = calloc(1, size);
vb@311
   595
        if (result == NULL)
vb@311
   596
            return NULL;
vb@311
   597
vb@311
   598
        char *_r = result;
vb@311
   599
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   600
            _r = stpcpy(_r, _kl->value);
vb@311
   601
            if (_kl->next && _kl->next->value)
vb@311
   602
                _r = stpcpy(_r, ",");
vb@311
   603
        }
vb@311
   604
vb@311
   605
        return result;
vb@311
   606
    }
vb@311
   607
    else {
vb@311
   608
        return NULL;
vb@311
   609
    }
vb@311
   610
}
vb@311
   611
vb@311
   612
static const char * color_to_string(PEP_color color)
vb@311
   613
{
vb@311
   614
    switch (color) {
vb@311
   615
    case PEP_rating_cannot_decrypt:
vb@311
   616
        return "cannot_decrypt";
vb@311
   617
    case PEP_rating_have_no_key:
vb@311
   618
        return "have_no_key";
vb@311
   619
    case PEP_rating_unencrypted:
vb@311
   620
        return "unencrypted";
vb@486
   621
    case PEP_rating_unencrypted_for_some:
vb@486
   622
        return "unencrypted_for_some";
vb@311
   623
    case PEP_rating_unreliable:
vb@311
   624
        return "unreliable";
vb@311
   625
    case PEP_rating_reliable:
vb@311
   626
        return "reliable";
vb@311
   627
    case PEP_rating_trusted:
vb@311
   628
        return "trusted";
vb@311
   629
    case PEP_rating_trusted_and_anonymized:
vb@311
   630
        return "trusted_and_anonymized";
vb@311
   631
    case PEP_rating_fully_anonymous:
vb@311
   632
        return "fully_anonymous";
Edouard@442
   633
    case PEP_rating_mistrust:
Edouard@442
   634
        return "mistrust";
Edouard@442
   635
    case PEP_rating_b0rken:
Edouard@442
   636
        return "b0rken";
vb@311
   637
    case PEP_rating_under_attack:
vb@311
   638
        return "unter_attack";
vb@311
   639
    default:
vb@311
   640
        return "undefined";
vb@311
   641
    }
vb@311
   642
}
vb@311
   643
vb@311
   644
static void decorate_message(
vb@311
   645
    message *msg,
vb@311
   646
    PEP_color color,
vb@311
   647
    stringlist_t *keylist
vb@311
   648
    )
vb@311
   649
{
vb@311
   650
    assert(msg);
vb@311
   651
vb@311
   652
    add_opt_field(msg, "X-pEp-Version", "1.0");
vb@311
   653
    
vb@311
   654
    if (color != PEP_rating_undefined)
vb@311
   655
        add_opt_field(msg, "X-EncStatus", color_to_string(color));
vb@311
   656
vb@311
   657
    if (keylist) {
vb@311
   658
        char *_keylist = keylist_to_string(keylist);
vb@311
   659
        add_opt_field(msg, "X-KeyList", _keylist);
vb@311
   660
        free(_keylist);
vb@311
   661
    }
vb@311
   662
}
vb@311
   663
vb@486
   664
static PEP_color _rating(PEP_comm_type ct, PEP_color color)
vb@311
   665
{
vb@311
   666
    if (ct == PEP_ct_unknown)
vb@311
   667
        return PEP_rating_undefined;
vb@311
   668
vb@311
   669
    else if (ct == PEP_ct_compromized)
vb@311
   670
        return PEP_rating_under_attack;
vb@311
   671
Edouard@442
   672
    else if (ct == PEP_ct_mistrusted)
Edouard@442
   673
        return PEP_rating_mistrust;
Edouard@442
   674
    
vb@486
   675
    if (color == PEP_rating_unencrypted_for_some)
vb@486
   676
        return PEP_rating_unencrypted_for_some;
vb@486
   677
vb@486
   678
    if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel ||
vb@486
   679
            ct == PEP_ct_my_key_not_included) {
vb@486
   680
        if (color > PEP_rating_unencrypted_for_some)
vb@486
   681
            return PEP_rating_unencrypted_for_some;
vb@486
   682
        else
vb@486
   683
            return PEP_rating_unencrypted;
vb@486
   684
    }
vb@486
   685
vb@486
   686
    if (color == PEP_rating_unencrypted)
vb@486
   687
        return PEP_rating_unencrypted_for_some;
vb@486
   688
vb@486
   689
    if (ct >= PEP_ct_confirmed_enc_anon)
vb@311
   690
        return PEP_rating_trusted_and_anonymized;
vb@311
   691
vb@311
   692
    else if (ct >= PEP_ct_strong_encryption)
vb@311
   693
        return PEP_rating_trusted;
vb@311
   694
vb@311
   695
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@311
   696
        return PEP_rating_reliable;
vb@311
   697
vb@311
   698
    else
vb@311
   699
        return PEP_rating_unreliable;
vb@311
   700
}
vb@311
   701
vb@311
   702
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@311
   703
{
vb@311
   704
    char *ext;
vb@311
   705
vb@311
   706
    assert(blob);
vb@311
   707
krista@853
   708
    if (blob == NULL || blob->filename == NULL)
vb@311
   709
        return false;
krista@853
   710
    
vb@311
   711
    ext = strrchr(blob->filename, '.');
vb@311
   712
    if (ext == NULL)
vb@311
   713
        return false;
vb@311
   714
vb@320
   715
    if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
vb@311
   716
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@311
   717
            strcmp(ext, ".asc") == 0)
vb@311
   718
            return true;
vb@311
   719
    }
vb@320
   720
    else if (strcmp(blob->mime_type, "text/plain") == 0) {
vb@311
   721
        if (strcmp(ext, ".asc") == 0)
vb@311
   722
            return true;
vb@311
   723
    }
vb@311
   724
vb@311
   725
    return false;
vb@311
   726
}
vb@311
   727
vb@311
   728
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@311
   729
{
vb@311
   730
    assert(blob);
vb@311
   731
    assert(blob->filename);
krista@853
   732
    if (blob == NULL || blob->filename == NULL)
krista@853
   733
        return false;
vb@311
   734
vb@311
   735
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@311
   736
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@311
   737
            strcmp(blob->filename + 11, ".asc") == 0)
vb@311
   738
            return true;
vb@311
   739
    }
vb@311
   740
vb@311
   741
    return false;
vb@311
   742
}
vb@311
   743
vb@311
   744
static char * without_double_ending(const char *filename)
vb@311
   745
{
vb@311
   746
    char *ext;
vb@311
   747
vb@311
   748
    assert(filename);
krista@853
   749
    if (filename == NULL)
krista@853
   750
        return NULL;
krista@853
   751
    
vb@311
   752
    ext = strrchr(filename, '.');
vb@311
   753
    if (ext == NULL)
vb@311
   754
        return NULL;
vb@311
   755
vb@469
   756
    char *result = strndup(filename, ext - filename);
vb@469
   757
    assert(result);
vb@469
   758
    return result;
vb@311
   759
}
vb@311
   760
vb@311
   761
static PEP_color decrypt_color(PEP_STATUS status)
vb@311
   762
{
vb@311
   763
    switch (status) {
vb@311
   764
    case PEP_UNENCRYPTED:
vb@311
   765
    case PEP_VERIFIED:
vb@311
   766
    case PEP_VERIFY_NO_KEY:
vb@311
   767
    case PEP_VERIFIED_AND_TRUSTED:
vb@311
   768
        return PEP_rating_unencrypted;
vb@311
   769
vb@311
   770
    case PEP_DECRYPTED:
vb@311
   771
        return PEP_rating_unreliable;
vb@311
   772
vb@311
   773
    case PEP_DECRYPTED_AND_VERIFIED:
vb@311
   774
        return PEP_rating_reliable;
vb@311
   775
vb@311
   776
    case PEP_DECRYPT_NO_KEY:
vb@311
   777
        return PEP_rating_have_no_key;
vb@311
   778
vb@311
   779
    case PEP_DECRYPT_WRONG_FORMAT:
vb@311
   780
    case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@311
   781
        return PEP_rating_cannot_decrypt;
vb@311
   782
vb@311
   783
    default:
vb@311
   784
        return PEP_rating_undefined;
vb@311
   785
    }
vb@311
   786
}
vb@311
   787
vb@311
   788
static PEP_color key_color(PEP_SESSION session, const char *fpr)
vb@311
   789
{
vb@311
   790
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@311
   791
vb@311
   792
    assert(session);
vb@311
   793
    assert(fpr);
krista@853
   794
    
krista@853
   795
    if (session == NULL || fpr == NULL)
krista@853
   796
        return PEP_rating_undefined;
vb@311
   797
vb@311
   798
    PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
vb@311
   799
    if (status != PEP_STATUS_OK)
vb@311
   800
        return PEP_rating_undefined;
vb@311
   801
vb@486
   802
    return _rating(comm_type, PEP_rating_undefined);
vb@311
   803
}
vb@311
   804
vb@311
   805
static PEP_color keylist_color(PEP_SESSION session, stringlist_t *keylist)
vb@311
   806
{
vb@311
   807
    PEP_color color = PEP_rating_reliable;
vb@311
   808
vb@311
   809
    assert(keylist && keylist->value);
vb@311
   810
    if (keylist == NULL || keylist->value == NULL)
vb@486
   811
        return PEP_rating_undefined;
vb@311
   812
vb@311
   813
    stringlist_t *_kl;
vb@311
   814
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   815
        PEP_comm_type ct;
vb@311
   816
        PEP_STATUS status;
vb@311
   817
vb@486
   818
        PEP_color _color = key_color(session, _kl->value);
vb@486
   819
        if (_color <= PEP_rating_mistrust)
vb@486
   820
            return _color;
vb@311
   821
vb@486
   822
        if (color == PEP_rating_undefined)
vb@486
   823
            color = _color;
vb@486
   824
vb@486
   825
        if (_color >= PEP_rating_reliable) {
vb@311
   826
            status = least_trust(session, _kl->value, &ct);
vb@311
   827
            if (status != PEP_STATUS_OK)
vb@311
   828
                return PEP_rating_undefined;
vb@311
   829
            if (ct == PEP_ct_unknown)
vb@486
   830
                color = PEP_rating_unencrypted_for_some;
vb@311
   831
            else
vb@486
   832
                color = _rating(ct, color);
vb@486
   833
        }
vb@486
   834
        else if (_color == PEP_rating_unencrypted) {
vb@486
   835
            if (color > PEP_rating_unencrypted_for_some)
vb@486
   836
                color = PEP_rating_unencrypted_for_some;
vb@311
   837
        }
vb@311
   838
    }
vb@311
   839
vb@311
   840
    return color;
vb@311
   841
}
vb@311
   842
vb@311
   843
static PEP_comm_type _get_comm_type(
vb@311
   844
    PEP_SESSION session,
vb@311
   845
    PEP_comm_type max_comm_type,
vb@311
   846
    pEp_identity *ident
vb@311
   847
    )
vb@311
   848
{
vb@311
   849
    PEP_STATUS status = update_identity(session, ident);
vb@311
   850
vb@311
   851
    if (max_comm_type == PEP_ct_compromized)
vb@311
   852
        return PEP_ct_compromized;
Edouard@510
   853
    
Edouard@510
   854
    if (max_comm_type == PEP_ct_mistrusted)
Edouard@510
   855
        return PEP_ct_mistrusted;
vb@311
   856
vb@311
   857
    if (status == PEP_STATUS_OK) {
vb@311
   858
        if (ident->comm_type == PEP_ct_compromized)
vb@311
   859
            return PEP_ct_compromized;
Edouard@510
   860
        else if (ident->comm_type == PEP_ct_mistrusted)
Edouard@510
   861
            return PEP_ct_mistrusted;
vb@311
   862
        else
vb@311
   863
            return MIN(max_comm_type, ident->comm_type);
vb@311
   864
    }
vb@311
   865
    else {
vb@311
   866
        return PEP_ct_unknown;
vb@311
   867
    }
vb@311
   868
}
vb@311
   869
vb@731
   870
static void free_bl_entry(bloblist_t *bl)
vb@731
   871
{
vb@731
   872
    if (bl) {
vb@731
   873
        free(bl->value);
vb@731
   874
        free(bl->mime_type);
vb@731
   875
        free(bl->filename);
vb@731
   876
        free(bl);
vb@731
   877
    }
vb@731
   878
}
vb@731
   879
vb@731
   880
static bool is_key(const bloblist_t *bl)
vb@731
   881
{
Edouard@728
   882
    return (// workaround for Apple Mail bugs
Edouard@728
   883
            (is_mime_type(bl, "application/x-apple-msg-attachment") &&
Edouard@728
   884
             is_fileending(bl, ".asc")) ||
Edouard@728
   885
            // as binary, by file name
Edouard@728
   886
            ((bl->mime_type == NULL ||
Edouard@728
   887
              is_mime_type(bl, "application/octet-stream")) &&
Edouard@728
   888
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
   889
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))) ||
Edouard@728
   890
            // explicit mime type 
Edouard@728
   891
            is_mime_type(bl, "application/pgp-keys") ||
Edouard@728
   892
            // as text, by file name
Edouard@728
   893
            (is_mime_type(bl, "text/plain") &&
Edouard@728
   894
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
   895
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc")))
Edouard@728
   896
           );
vb@731
   897
}
vb@731
   898
vb@731
   899
static void remove_attached_keys(message *msg)
vb@731
   900
{
vb@731
   901
    if (msg) {
vb@731
   902
        bloblist_t *last = NULL;
vb@731
   903
        for (bloblist_t *bl = msg->attachments; bl && bl->value; ) {
vb@731
   904
            bloblist_t *next = bl->next;
vb@731
   905
vb@731
   906
            if (is_key(bl)) {
vb@731
   907
                if (last) {
vb@731
   908
                    last->next = next;
vb@731
   909
                }
vb@731
   910
                else {
vb@731
   911
                    msg->attachments = next;
vb@731
   912
                }
vb@731
   913
                free_bl_entry(bl);
vb@731
   914
            }
vb@731
   915
            else {
vb@731
   916
                last = bl;
vb@731
   917
            }
vb@731
   918
            bl = next;
vb@731
   919
        }
vb@731
   920
    }
vb@731
   921
}
vb@731
   922
Edouard@734
   923
bool import_attached_keys(
Edouard@728
   924
        PEP_SESSION session, 
Edouard@728
   925
        const message *msg,
Edouard@728
   926
        identity_list **private_idents
Edouard@728
   927
    )
vb@311
   928
{
vb@311
   929
    assert(session);
vb@311
   930
    assert(msg);
krista@853
   931
    
krista@853
   932
    if (session == NULL || msg == NULL)
krista@853
   933
        return false;
vb@311
   934
vb@731
   935
    bool remove = false;
vb@731
   936
vb@311
   937
    bloblist_t *bl;
Edouard@747
   938
    for (bl = msg->attachments; bl && bl->value; bl = bl->next) 
Edouard@747
   939
    {
Edouard@747
   940
        if (bl && bl->value && bl->size && is_key(bl)) 
Edouard@728
   941
        {
Edouard@728
   942
            import_key(session, bl->value, bl->size, private_idents);
vb@731
   943
            remove = true;
vb@311
   944
        }
vb@311
   945
    }
vb@731
   946
    return remove;
vb@311
   947
}
vb@311
   948
Edouard@694
   949
Edouard@694
   950
PEP_STATUS _attach_key(PEP_SESSION session, const char* fpr, message *msg)
vb@311
   951
{
vb@311
   952
    char *keydata;
vb@311
   953
    size_t size;
vb@311
   954
    bloblist_t *bl;
vb@311
   955
Edouard@694
   956
    PEP_STATUS status = export_key(session, fpr, &keydata, &size);
Edouard@694
   957
    assert(status == PEP_STATUS_OK);
Edouard@694
   958
    if (status != PEP_STATUS_OK)
Edouard@694
   959
        return status;
Edouard@694
   960
    assert(size);
Edouard@694
   961
    
Edouard@694
   962
    bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
Edouard@694
   963
                      "pEpkey.asc");
Edouard@694
   964
    
Edouard@694
   965
    if (msg->attachments == NULL && bl)
Edouard@694
   966
        msg->attachments = bl;
Edouard@694
   967
Edouard@694
   968
    return PEP_STATUS_OK;
Edouard@694
   969
}
Edouard@694
   970
Edouard@694
   971
#define ONE_WEEK (7*24*3600)
Edouard@694
   972
Edouard@694
   973
void attach_own_key(PEP_SESSION session, message *msg)
Edouard@694
   974
{
vb@311
   975
    assert(session);
vb@311
   976
    assert(msg);
krista@853
   977
    
Edouard@558
   978
    if (msg->dir == PEP_dir_incoming)
Edouard@558
   979
        return;
Edouard@558
   980
vb@311
   981
    assert(msg->from && msg->from->fpr);
vb@311
   982
    if (msg->from == NULL || msg->from->fpr == NULL)
vb@311
   983
        return;
vb@311
   984
Edouard@694
   985
    if(_attach_key(session, msg->from->fpr, msg) != PEP_STATUS_OK)
vb@311
   986
        return;
Edouard@694
   987
    
Edouard@694
   988
    char *revoked_fpr = NULL;
Edouard@694
   989
    uint64_t revocation_date = 0;
Edouard@694
   990
    
Edouard@694
   991
    if(get_revoked(session, msg->from->fpr,
Edouard@694
   992
                   &revoked_fpr, &revocation_date) == PEP_STATUS_OK &&
Edouard@694
   993
       revoked_fpr != NULL)
Edouard@694
   994
    {
Edouard@694
   995
        time_t now = time(NULL);
Edouard@694
   996
        
Edouard@694
   997
        if (now < (time_t)revocation_date + ONE_WEEK)
Edouard@694
   998
        {
Edouard@694
   999
            _attach_key(session, revoked_fpr, msg);
Edouard@694
  1000
        }
Edouard@694
  1001
    }
Edouard@694
  1002
    free(revoked_fpr);
vb@311
  1003
}
vb@311
  1004
vb@311
  1005
PEP_cryptotech determine_encryption_format(message *msg)
vb@311
  1006
{
vb@311
  1007
    assert(msg);
krista@853
  1008
    
vb@311
  1009
    if (is_PGP_message_text(msg->longmsg)) {
vb@311
  1010
        msg->enc_format = PEP_enc_pieces;
vb@311
  1011
        return PEP_crypt_OpenPGP;
vb@311
  1012
    }
vb@311
  1013
    else if (msg->attachments && msg->attachments->next &&
vb@311
  1014
            is_mime_type(msg->attachments, "application/pgp-encrypted") &&
vb@311
  1015
            is_PGP_message_text(msg->attachments->next->value)
vb@311
  1016
        ) {
vb@311
  1017
        msg->enc_format = PEP_enc_PGP_MIME;
vb@311
  1018
        return PEP_crypt_OpenPGP;
vb@311
  1019
    }
vb@311
  1020
    else {
vb@311
  1021
        msg->enc_format = PEP_enc_none;
vb@311
  1022
        return PEP_crypt_none;
vb@311
  1023
    }
vb@311
  1024
}
vb@311
  1025
vb@48
  1026
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
  1027
        PEP_SESSION session,
vb@113
  1028
        message *src,
vb@37
  1029
        stringlist_t * extra,
vb@38
  1030
        message **dst,
vb@81
  1031
        PEP_enc_format enc_format
vb@37
  1032
    )
vb@37
  1033
{
vb@37
  1034
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
  1035
    message * msg = NULL;
vb@63
  1036
    stringlist_t * keys = NULL;
vb@37
  1037
vb@37
  1038
    assert(session);
vb@37
  1039
    assert(src);
vb@37
  1040
    assert(dst);
Edouard@558
  1041
    assert(enc_format != PEP_enc_none);
vb@81
  1042
Edouard@558
  1043
    if (!(session && src && dst && enc_format != PEP_enc_none))
vb@191
  1044
        return PEP_ILLEGAL_VALUE;
vb@191
  1045
Edouard@550
  1046
    if (src->dir == PEP_dir_incoming)
Edouard@550
  1047
        return PEP_ILLEGAL_VALUE;
Edouard@550
  1048
    
vb@259
  1049
    determine_encryption_format(src);
vb@260
  1050
    if (src->enc_format != PEP_enc_none)
vb@260
  1051
        return PEP_ILLEGAL_VALUE;
vb@259
  1052
vb@37
  1053
    *dst = NULL;
vb@67
  1054
vb@236
  1055
    status = myself(session, src->from);
vb@236
  1056
    if (status != PEP_STATUS_OK)
vb@236
  1057
        goto pep_error;
Edouard@558
  1058
vb@80
  1059
    keys = new_stringlist(src->from->fpr);
vb@63
  1060
    if (keys == NULL)
vb@63
  1061
        goto enomem;
vb@37
  1062
vb@39
  1063
    stringlist_t *_k = keys;
vb@39
  1064
vb@39
  1065
    if (extra) {
vb@39
  1066
        _k = stringlist_append(_k, extra);
vb@63
  1067
        if (_k == NULL)
vb@63
  1068
            goto enomem;
vb@37
  1069
    }
vb@39
  1070
vb@299
  1071
    bool dest_keys_found = true;
Edouard@510
  1072
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@299
  1073
vb@37
  1074
    identity_list * _il;
Edouard@545
  1075
    
Edouard@545
  1076
    if ((_il = src->bcc) && _il->ident)
Edouard@545
  1077
    {
Edouard@545
  1078
        // BCC limited support:
Edouard@545
  1079
        //     - App splits mails with BCC in multiple mails.
Edouard@545
  1080
        //     - Each email is encrypted separately
Edouard@545
  1081
        
Edouard@789
  1082
        if(_il->next || (src->to && src->to->ident) || (src->cc && src->cc->ident))
Edouard@545
  1083
        {
Edouard@545
  1084
            // Only one Bcc with no other recipient allowed for now
Edouard@545
  1085
            return PEP_ILLEGAL_VALUE;
Edouard@545
  1086
        }
Edouard@545
  1087
        
vb@299
  1088
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
  1089
        if (_status != PEP_STATUS_OK) {
vb@299
  1090
            status = _status;
vb@63
  1091
            goto pep_error;
vb@299
  1092
        }
Edouard@545
  1093
        
vb@299
  1094
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@39
  1095
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
  1096
            if (_k == NULL)
vb@63
  1097
                goto enomem;
Edouard@510
  1098
            max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@510
  1099
                                           _il->ident);
vb@37
  1100
        }
vb@299
  1101
        else {
vb@299
  1102
            dest_keys_found = false;
vb@37
  1103
            status = PEP_KEY_NOT_FOUND;
Edouard@545
  1104
        }        
vb@299
  1105
    }
Edouard@545
  1106
    else
Edouard@545
  1107
    {
Edouard@545
  1108
        for (_il = src->to; _il && _il->ident; _il = _il->next) {
Edouard@545
  1109
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1110
            if (_status != PEP_STATUS_OK) {
Edouard@545
  1111
                status = _status;
Edouard@545
  1112
                goto pep_error;
Edouard@545
  1113
            }
vb@299
  1114
Edouard@545
  1115
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1116
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1117
                if (_k == NULL)
Edouard@545
  1118
                    goto enomem;
Edouard@545
  1119
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1120
                                               _il->ident);
Edouard@545
  1121
            }
Edouard@545
  1122
            else {
Edouard@545
  1123
                dest_keys_found = false;
Edouard@545
  1124
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1125
            }
vb@299
  1126
        }
vb@299
  1127
Edouard@545
  1128
        for (_il = src->cc; _il && _il->ident; _il = _il->next) {
Edouard@545
  1129
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1130
            if (_status != PEP_STATUS_OK)
Edouard@545
  1131
            {
Edouard@545
  1132
                status = _status;
Edouard@545
  1133
                goto pep_error;
Edouard@545
  1134
            }
Edouard@545
  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@37
  1148
    }
Edouard@510
  1149
    
Edouard@510
  1150
    if (!dest_keys_found ||
Edouard@510
  1151
        stringlist_length(keys) == 0 ||
Edouard@510
  1152
        _rating(max_comm_type,
Edouard@510
  1153
                PEP_rating_undefined) < PEP_rating_reliable)
Edouard@510
  1154
    {
vb@301
  1155
        free_stringlist(keys);
vb@465
  1156
        if (!session->passive_mode)
vb@465
  1157
            attach_own_key(session, src);
vb@301
  1158
        return PEP_UNENCRYPTED;
vb@301
  1159
    }
vb@301
  1160
    else {
vb@299
  1161
        msg = clone_to_empty_message(src);
vb@299
  1162
        if (msg == NULL)
vb@299
  1163
            goto enomem;
vb@299
  1164
Edouard@431
  1165
        attach_own_key(session, src);
Edouard@428
  1166
vb@81
  1167
        switch (enc_format) {
vb@260
  1168
        case PEP_enc_PGP_MIME:
vb@336
  1169
        case PEP_enc_PEP: // BUG: should be implemented extra
vb@260
  1170
            status = encrypt_PGP_MIME(session, src, keys, msg);
vb@260
  1171
            break;
vb@62
  1172
vb@62
  1173
        case PEP_enc_pieces:
vb@260
  1174
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
vb@38
  1175
            break;
vb@38
  1176
vb@336
  1177
        /* case PEP_enc_PEP:
vb@81
  1178
            // TODO: implement
vb@336
  1179
            NOT_IMPLEMENTED */
vb@81
  1180
vb@38
  1181
        default:
vb@38
  1182
            assert(0);
vb@63
  1183
            status = PEP_ILLEGAL_VALUE;
vb@63
  1184
            goto pep_error;
vb@37
  1185
        }
Edouard@392
  1186
        
Edouard@392
  1187
        if (status == PEP_OUT_OF_MEMORY)
Edouard@392
  1188
            goto enomem;
Edouard@392
  1189
        
vb@480
  1190
        if (status != PEP_STATUS_OK)
Edouard@392
  1191
            goto pep_error;
vb@37
  1192
    }
vb@37
  1193
vb@37
  1194
    free_stringlist(keys);
vb@63
  1195
vb@469
  1196
    if (msg && msg->shortmsg == NULL) {
vb@64
  1197
        msg->shortmsg = strdup("pEp");
vb@469
  1198
        assert(msg->shortmsg);
krista@853
  1199
        if (msg->shortmsg == NULL)
krista@853
  1200
            goto enomem;
vb@469
  1201
    }
vb@64
  1202
vb@311
  1203
    if (msg)
vb@311
  1204
        decorate_message(msg, PEP_rating_undefined, NULL);
vb@311
  1205
vb@63
  1206
    *dst = msg;
vb@299
  1207
    return status;
vb@63
  1208
vb@63
  1209
enomem:
vb@63
  1210
    status = PEP_OUT_OF_MEMORY;
vb@63
  1211
vb@63
  1212
pep_error:
vb@63
  1213
    free_stringlist(keys);
vb@63
  1214
    free_message(msg);
vb@63
  1215
vb@37
  1216
    return status;
vb@37
  1217
}
vb@37
  1218
vb@781
  1219
static bool is_a_pEpmessage(const message *msg)
vb@781
  1220
{
vb@781
  1221
    for (stringpair_list_t *i = msg->opt_fields; i && i->value ; i=i->next) {
vb@781
  1222
        if (strcasecmp(i->value->key, "X-pEp-Version") == 0)
vb@781
  1223
            return true;
vb@781
  1224
    }
vb@781
  1225
    return false;
vb@781
  1226
}
vb@781
  1227
vb@781
  1228
// update comm_type to pEp_ct_pEp if needed
vb@781
  1229
vb@781
  1230
static void _update_identity_for_incoming_message(
vb@781
  1231
        PEP_SESSION session,
vb@781
  1232
        const message *src
vb@781
  1233
    )
vb@781
  1234
{
vb@781
  1235
    if (src->from && src->from->user_id && src->from->address) {
vb@781
  1236
        update_identity(session, src->from);
vb@781
  1237
        if (is_a_pEpmessage(src)
vb@781
  1238
                && src->from->comm_type >= PEP_ct_OpenPGP_unconfirmed
vb@781
  1239
                && src->from->comm_type != PEP_ct_pEp_unconfirmed
vb@781
  1240
                && src->from->comm_type != PEP_ct_pEp)
vb@781
  1241
        {
vb@781
  1242
            src->from->comm_type |= PEP_ct_pEp_unconfirmed;
vb@781
  1243
            update_identity(session, src->from);
vb@781
  1244
        }
vb@781
  1245
    }
vb@781
  1246
}
vb@781
  1247
Edouard@741
  1248
DYNAMIC_API PEP_STATUS _decrypt_message(
vb@37
  1249
        PEP_SESSION session,
vb@113
  1250
        message *src,
vb@241
  1251
        message **dst,
vb@251
  1252
        stringlist_t **keylist,
Edouard@728
  1253
        PEP_color *color,
Edouard@741
  1254
        PEP_decrypt_flags_t *flags, 
Edouard@741
  1255
        identity_list **private_il
vb@37
  1256
    )
vb@37
  1257
{
vb@37
  1258
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
  1259
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
  1260
    message *msg = NULL;
vb@112
  1261
    char *ctext;
vb@112
  1262
    size_t csize;
vb@269
  1263
    char *ptext = NULL;
vb@112
  1264
    size_t psize;
vb@241
  1265
    stringlist_t *_keylist = NULL;
vb@37
  1266
vb@74
  1267
    assert(session);
vb@74
  1268
    assert(src);
vb@74
  1269
    assert(dst);
vb@241
  1270
    assert(keylist);
vb@251
  1271
    assert(color);
Edouard@739
  1272
    assert(flags);
vb@73
  1273
Edouard@739
  1274
    if (!(session && src && dst && keylist && color && flags))
vb@191
  1275
        return PEP_ILLEGAL_VALUE;
vb@191
  1276
Edouard@739
  1277
    *flags = 0;
Edouard@739
  1278
Edouard@734
  1279
    // Private key in unencrypted mail are ignored -> NULL
Edouard@734
  1280
    bool imported_keys = import_attached_keys(session, src, NULL);
Edouard@728
  1281
Edouard@728
  1282
    // Update src->from in case we just imported a key
Edouard@728
  1283
    // we would need to check signature
vb@781
  1284
    _update_identity_for_incoming_message(session, src);
vb@259
  1285
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
  1286
vb@74
  1287
    *dst = NULL;
vb@251
  1288
    *keylist = NULL;
vb@251
  1289
    *color = PEP_rating_undefined;
vb@81
  1290
 
vb@261
  1291
    switch (src->enc_format) {
vb@269
  1292
        case PEP_enc_none:
vb@301
  1293
            *color = PEP_rating_unencrypted;
vb@731
  1294
            if (imported_keys)
vb@731
  1295
                remove_attached_keys(src);
vb@301
  1296
            return PEP_UNENCRYPTED;
vb@269
  1297
vb@261
  1298
        case PEP_enc_PGP_MIME:
vb@301
  1299
            ctext = src->attachments->next->value;
vb@269
  1300
            csize = src->attachments->next->size;
vb@261
  1301
            break;
vb@261
  1302
vb@261
  1303
        case PEP_enc_pieces:
vb@261
  1304
            ctext = src->longmsg;
vb@261
  1305
            csize = strlen(ctext);
vb@261
  1306
            break;
vb@261
  1307
vb@261
  1308
        default:
vb@261
  1309
            NOT_IMPLEMENTED
vb@259
  1310
    }
Edouard@431
  1311
    status = cryptotech[crypto].decrypt_and_verify(session, ctext,
Edouard@431
  1312
                                                   csize, &ptext, &psize, &_keylist);
Edouard@431
  1313
    if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
Edouard@431
  1314
        goto pep_error;
vb@113
  1315
Edouard@431
  1316
    decrypt_status = status;
vb@251
  1317
Edouard@741
  1318
    bool imported_private_key_address = false; 
Edouard@728
  1319
vb@256
  1320
    if (ptext) {
vb@256
  1321
        switch (src->enc_format) {
vb@256
  1322
            case PEP_enc_PGP_MIME:
vb@269
  1323
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  1324
                if (status != PEP_STATUS_OK)
vb@113
  1325
                    goto pep_error;
vb@256
  1326
                break;
vb@256
  1327
vb@256
  1328
            case PEP_enc_pieces:
vb@256
  1329
                msg = clone_to_empty_message(src);
vb@256
  1330
                if (msg == NULL)
vb@113
  1331
                    goto enomem;
vb@113
  1332
vb@256
  1333
                msg->longmsg = strdup(ptext);
vb@469
  1334
                assert(msg->longmsg);
vb@256
  1335
                if (msg->longmsg == NULL)
vb@256
  1336
                    goto enomem;
vb@256
  1337
vb@256
  1338
                bloblist_t *_m = msg->attachments;
vb@320
  1339
                if (_m == NULL && src->attachments && src->attachments->value) {
vb@320
  1340
                    msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@320
  1341
                    _m = msg->attachments;
vb@320
  1342
                }
vb@320
  1343
vb@256
  1344
                bloblist_t *_s;
Edouard@754
  1345
                for (_s = src->attachments; _s; _s = _s->next) {
Edouard@754
  1346
                    if (_s->value == NULL && _s->size == 0){
Edouard@754
  1347
                        _m = bloblist_add(_m, NULL, 0, _s->mime_type, _s->filename);
Edouard@754
  1348
                        if (_m == NULL)
Edouard@754
  1349
                            goto enomem;
Edouard@754
  1350
Edouard@754
  1351
                    }
Edouard@754
  1352
                    else if (is_encrypted_attachment(_s)) {
vb@256
  1353
                        stringlist_t *_keylist = NULL;
Edouard@431
  1354
                        char *attctext;
Edouard@431
  1355
                        size_t attcsize;
vb@256
  1356
Edouard@431
  1357
                        attctext = _s->value;
Edouard@431
  1358
                        attcsize = _s->size;
Edouard@431
  1359
Edouard@842
  1360
                        free(ptext);
Edouard@842
  1361
                        ptext = NULL;
Edouard@842
  1362
Edouard@431
  1363
                        status = decrypt_and_verify(session, attctext, attcsize,
vb@256
  1364
                                &ptext, &psize, &_keylist);
vb@256
  1365
                        free_stringlist(_keylist);
vb@256
  1366
vb@289
  1367
                        if (ptext) {
vb@289
  1368
                            if (is_encrypted_html_attachment(_s)) {
vb@290
  1369
                                msg->longmsg_formatted = strdup(ptext);
vb@469
  1370
                                assert(msg->longmsg_formatted);
vb@290
  1371
                                if (msg->longmsg_formatted == NULL)
vb@290
  1372
                                    goto pep_error;
vb@289
  1373
                            }
vb@289
  1374
                            else {
vb@289
  1375
                                char * mime_type = "application/octet-stream";
vb@289
  1376
                                char * filename =
vb@289
  1377
                                    without_double_ending(_s->filename);
vb@289
  1378
                                if (filename == NULL)
vb@289
  1379
                                    goto enomem;
vb@289
  1380
vb@290
  1381
                                char *_ptext = malloc(psize);
vb@290
  1382
                                assert(_ptext);
vb@290
  1383
                                if (_ptext == NULL)
vb@290
  1384
                                    goto enomem;
vb@290
  1385
                                memcpy(_ptext, ptext, psize);
vb@290
  1386
vb@290
  1387
                                _m = bloblist_add(_m, _ptext, psize, mime_type,
vb@289
  1388
                                    filename);
vb@290
  1389
                                if (_m == NULL)
vb@289
  1390
                                    goto enomem;
vb@290
  1391
vb@290
  1392
                                if (msg->attachments == NULL)
vb@290
  1393
                                    msg->attachments = _m;
vb@289
  1394
                            }
vb@256
  1395
                        }
vb@256
  1396
                        else {
vb@320
  1397
                            char *copy = malloc(_s->size);
vb@350
  1398
                            assert(copy);
vb@350
  1399
                            if (copy == NULL)
vb@350
  1400
                                goto enomem;
vb@320
  1401
                            memcpy(copy, _s->value, _s->size);
vb@320
  1402
                            _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@256
  1403
                            if (_m == NULL)
vb@256
  1404
                                goto enomem;
vb@256
  1405
                        }
vb@256
  1406
                    }
vb@320
  1407
                    else {
vb@320
  1408
                        char *copy = malloc(_s->size);
vb@350
  1409
                        assert(copy);
vb@350
  1410
                        if (copy == NULL)
vb@350
  1411
                            goto enomem;
vb@320
  1412
                        memcpy(copy, _s->value, _s->size);
vb@320
  1413
                        _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@320
  1414
                        if (_m == NULL)
vb@320
  1415
                            goto enomem;
vb@320
  1416
                    }
vb@256
  1417
                }
vb@256
  1418
vb@256
  1419
                break;
vb@256
  1420
vb@256
  1421
            default:
vb@256
  1422
                // BUG: must implement more
vb@256
  1423
                NOT_IMPLEMENTED
vb@256
  1424
        }
Edouard@431
  1425
        
vb@256
  1426
        switch (src->enc_format) {
vb@256
  1427
            case PEP_enc_PGP_MIME:
vb@256
  1428
            case PEP_enc_pieces:
vb@256
  1429
                status = copy_fields(msg, src);
vb@256
  1430
                if (status != PEP_STATUS_OK)
vb@256
  1431
                    goto pep_error;
vb@256
  1432
vb@280
  1433
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1434
                {
vb@256
  1435
                    char * shortmsg;
vb@256
  1436
                    char * longmsg;
vb@256
  1437
krista@853
  1438
                    int r = separate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1439
                            &longmsg);
vb@256
  1440
                    if (r == -1)
vb@256
  1441
                        goto enomem;
vb@256
  1442
vb@256
  1443
                    free(msg->shortmsg);
vb@256
  1444
                    free(msg->longmsg);
vb@256
  1445
vb@256
  1446
                    msg->shortmsg = shortmsg;
vb@256
  1447
                    msg->longmsg = longmsg;
vb@256
  1448
                }
vb@256
  1449
                else {
vb@256
  1450
                    msg->shortmsg = strdup(src->shortmsg);
vb@469
  1451
                    assert(msg->shortmsg);
vb@256
  1452
                    if (msg->shortmsg == NULL)
vb@256
  1453
                        goto enomem;
vb@256
  1454
                }
vb@256
  1455
                break;
vb@256
  1456
vb@256
  1457
            default:
vb@256
  1458
                // BUG: must implement more
vb@256
  1459
                NOT_IMPLEMENTED
vb@256
  1460
        }
Edouard@728
  1461
       
Edouard@734
  1462
        // check for private key in decrypted message attachement while inporting
Edouard@741
  1463
        identity_list *_private_il = NULL;
Edouard@741
  1464
        imported_keys = import_attached_keys(session, msg, &_private_il);
Edouard@741
  1465
        if (_private_il && 
Edouard@741
  1466
            identity_list_length(_private_il) == 1 &&
Edouard@741
  1467
            _private_il->ident->address)
Edouard@728
  1468
        {
Edouard@741
  1469
            imported_private_key_address = true;
Edouard@728
  1470
        }
Edouard@741
  1471
Edouard@741
  1472
        if(private_il && imported_private_key_address){
Edouard@741
  1473
            *private_il = _private_il;
Edouard@741
  1474
        }else{
Edouard@741
  1475
            free_identity_list(_private_il);
Edouard@741
  1476
        }
Edouard@728
  1477
         
Edouard@694
  1478
        if(decrypt_status == PEP_DECRYPTED){
Edouard@728
  1479
Edouard@728
  1480
            // TODO optimize if import_attached_keys didn't import any key
Edouard@431
  1481
            
Edouard@431
  1482
            // In case message did decrypt, but no valid signature could be found
Edouard@431
  1483
            // then retry decrypt+verify after importing key.
Edouard@728
  1484
Edouard@728
  1485
            // Update msg->from in case we just imported a key
Edouard@728
  1486
            // we would need to check signature
vb@781
  1487
vb@781
  1488
            _update_identity_for_incoming_message(session, src);
Edouard@431
  1489
            
Edouard@431
  1490
            char *re_ptext = NULL;
Edouard@431
  1491
            size_t re_psize;
Edouard@441
  1492
            
Edouard@431
  1493
            free_stringlist(_keylist);
Edouard@431
  1494
            _keylist = NULL;
vb@256
  1495
Edouard@431
  1496
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
Edouard@431
  1497
                csize, &re_ptext, &re_psize, &_keylist);
Edouard@433
  1498
            
vb@731
  1499
            if (re_ptext)
Edouard@433
  1500
                free(re_ptext);
Edouard@433
  1501
            
Edouard@431
  1502
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
Edouard@431
  1503
                goto pep_error;
Edouard@431
  1504
            
Edouard@431
  1505
            decrypt_status = status;
Edouard@431
  1506
        }
Edouard@431
  1507
        
Edouard@431
  1508
        *color = decrypt_color(decrypt_status);
Edouard@431
  1509
        
vb@485
  1510
        if (*color > PEP_rating_mistrust) {
Edouard@431
  1511
            PEP_color kl_color = PEP_rating_undefined;
Edouard@431
  1512
            
Edouard@431
  1513
            if (_keylist)
Edouard@431
  1514
                kl_color = keylist_color(session, _keylist);
Edouard@431
  1515
            
vb@485
  1516
            if (kl_color <= PEP_rating_mistrust) {
vb@485
  1517
                *color = kl_color;
Edouard@445
  1518
            }
Edouard@431
  1519
            else if (*color >= PEP_rating_reliable &&
Edouard@431
  1520
                     kl_color < PEP_rating_reliable) {
Edouard@431
  1521
                *color = PEP_rating_unreliable;
Edouard@431
  1522
            }
Edouard@431
  1523
            else if (*color >= PEP_rating_reliable &&
Edouard@431
  1524
                     kl_color >= PEP_rating_reliable) {
Edouard@431
  1525
                if (!(src->from && src->from->user_id && src->from->user_id[0])) {
Edouard@431
  1526
                    *color = PEP_rating_unreliable;
Edouard@431
  1527
                }
Edouard@431
  1528
                else {
Edouard@431
  1529
                    char *fpr = _keylist->value;
Edouard@431
  1530
                    pEp_identity *_from = new_identity(src->from->address, fpr,
Edouard@431
  1531
                                                       src->from->user_id, src->from->username);
Edouard@431
  1532
                    if (_from == NULL)
Edouard@431
  1533
                        goto enomem;
Edouard@431
  1534
                    status = update_identity(session, _from);
Edouard@431
  1535
                    if (_from->comm_type != PEP_ct_unknown)
vb@486
  1536
                        *color = _rating(_from->comm_type, PEP_rating_undefined);
Edouard@431
  1537
                    free_identity(_from);
Edouard@431
  1538
                    if (status != PEP_STATUS_OK)
Edouard@431
  1539
                        goto pep_error;
Edouard@431
  1540
                }
Edouard@431
  1541
            }
Edouard@431
  1542
        }
vb@113
  1543
    }
Edouard@745
  1544
    else
Edouard@745
  1545
    {
Edouard@745
  1546
        *color = decrypt_color(decrypt_status);
Edouard@745
  1547
        goto pep_error;
Edouard@745
  1548
    }
vb@731
  1549
Edouard@728
  1550
    // Case of own key imported from own trusted message
Edouard@728
  1551
    if (// Message have been reliably decrypted 
Edouard@728
  1552
        msg &&
Edouard@728
  1553
        *color >= PEP_rating_green &&
Edouard@728
  1554
        imported_private_key_address &&
Edouard@728
  1555
        // to is [own]
Edouard@728
  1556
        msg->to->ident->user_id &&
Edouard@729
  1557
        strcmp(msg->to->ident->user_id, PEP_OWN_USERID) == 0 
Edouard@729
  1558
        )
Edouard@728
  1559
    {
Edouard@738
  1560
        *flags |= PEP_decrypt_flag_own_private_key;
Edouard@728
  1561
    }
vb@113
  1562
vb@731
  1563
    if (msg) {
vb@311
  1564
        decorate_message(msg, *color, _keylist);
vb@731
  1565
        if (imported_keys)
vb@731
  1566
            remove_attached_keys(msg);
vb@731
  1567
    }
vb@235
  1568
vb@74
  1569
    *dst = msg;
vb@241
  1570
    *keylist = _keylist;
vb@241
  1571
vb@267
  1572
    return PEP_STATUS_OK;
vb@73
  1573
vb@73
  1574
enomem:
vb@73
  1575
    status = PEP_OUT_OF_MEMORY;
vb@73
  1576
vb@73
  1577
pep_error:
vb@73
  1578
    free_message(msg);
vb@241
  1579
    free_stringlist(_keylist);
vb@39
  1580
vb@37
  1581
    return status;
vb@37
  1582
}
vb@37
  1583
Edouard@741
  1584
DYNAMIC_API PEP_STATUS decrypt_message(
Edouard@741
  1585
        PEP_SESSION session,
Edouard@741
  1586
        message *src,
Edouard@741
  1587
        message **dst,
Edouard@741
  1588
        stringlist_t **keylist,
Edouard@741
  1589
        PEP_color *color,
Edouard@741
  1590
        PEP_decrypt_flags_t *flags 
Edouard@741
  1591
    )
Edouard@741
  1592
{
Edouard@741
  1593
    return _decrypt_message( session, src, dst, keylist, color, flags, NULL );
Edouard@741
  1594
}
Edouard@741
  1595
Edouard@728
  1596
DYNAMIC_API PEP_STATUS own_message_private_key_details(
Edouard@728
  1597
        PEP_SESSION session,
Edouard@728
  1598
        message *msg,
Edouard@728
  1599
        pEp_identity **ident 
Edouard@728
  1600
    )
Edouard@728
  1601
{
Edouard@728
  1602
    assert(session);
Edouard@728
  1603
    assert(msg);
Edouard@728
  1604
    assert(ident);
Edouard@728
  1605
Edouard@736
  1606
    if (!(session && msg && ident))
Edouard@728
  1607
        return PEP_ILLEGAL_VALUE;
Edouard@728
  1608
Edouard@728
  1609
    message *dst; 
Edouard@728
  1610
    stringlist_t *keylist;
Edouard@728
  1611
    PEP_color color;
Edouard@728
  1612
    PEP_decrypt_flags_t flags; 
Edouard@728
  1613
Edouard@729
  1614
    *ident = NULL;
Edouard@729
  1615
Edouard@740
  1616
    identity_list *private_il = NULL;
Edouard@741
  1617
    PEP_STATUS status = _decrypt_message(session, msg,  &dst, &keylist, &color, &flags, &private_il);
Edouard@728
  1618
Edouard@729
  1619
    if (status == PEP_STATUS_OK &&
Edouard@741
  1620
        flags & PEP_decrypt_flag_own_private_key &&
Edouard@741
  1621
        private_il)
Edouard@729
  1622
    {
Edouard@740
  1623
        *ident = identity_dup(private_il->ident);
Edouard@729
  1624
    }
Edouard@729
  1625
Edouard@741
  1626
    free_identity_list(private_il);
Edouard@741
  1627
Edouard@728
  1628
    return status;
Edouard@741
  1629
Edouard@728
  1630
}
Edouard@728
  1631
vb@251
  1632
DYNAMIC_API PEP_STATUS outgoing_message_color(
vb@190
  1633
        PEP_SESSION session,
vb@190
  1634
        message *msg,
vb@232
  1635
        PEP_color *color
vb@190
  1636
    )
vb@190
  1637
{
vb@190
  1638
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
  1639
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  1640
    bool comm_type_determined = false;
vb@190
  1641
    identity_list * il;
vb@190
  1642
vb@190
  1643
    assert(session);
vb@190
  1644
    assert(msg);
vb@251
  1645
    assert(msg->from);
vb@251
  1646
    assert(msg->dir == PEP_dir_outgoing);
vb@190
  1647
    assert(color);
vb@190
  1648
vb@191
  1649
    if (!(session && msg && color))
vb@191
  1650
        return PEP_ILLEGAL_VALUE;
vb@191
  1651
vb@251
  1652
    if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
vb@251
  1653
        return PEP_ILLEGAL_VALUE;
vb@251
  1654
vb@237
  1655
    *color = PEP_rating_undefined;
vb@190
  1656
vb@251
  1657
    status = myself(session, msg->from);
vb@251
  1658
    if (status != PEP_STATUS_OK)
vb@251
  1659
        return status;
vb@251
  1660
Edouard@545
  1661
    for (il = msg->to; il != NULL; il = il->next)
Edouard@545
  1662
    {
Edouard@545
  1663
        if (il->ident)
Edouard@545
  1664
        {
vb@294
  1665
            update_identity(session, il->ident);
vb@251
  1666
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1667
                    il->ident);
vb@190
  1668
            comm_type_determined = true;
vb@251
  1669
        }
vb@251
  1670
    }
vb@190
  1671
Edouard@545
  1672
    for (il = msg->cc; il != NULL; il = il->next)
Edouard@545
  1673
    {
Edouard@545
  1674
        if (il->ident)
Edouard@545
  1675
        {
vb@294
  1676
            update_identity(session, il->ident);
vb@251
  1677
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1678
                    il->ident);
vb@251
  1679
            comm_type_determined = true;
vb@251
  1680
        }
vb@190
  1681
    }
Edouard@545
  1682
        
Edouard@545
  1683
    for (il = msg->bcc; il != NULL; il = il->next)
Edouard@545
  1684
    {
Edouard@545
  1685
        if (il->ident)
Edouard@545
  1686
        {
Edouard@545
  1687
            update_identity(session, il->ident);
Edouard@545
  1688
            max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1689
                                           il->ident);
Edouard@545
  1690
            comm_type_determined = true;
Edouard@545
  1691
        }
Edouard@545
  1692
    }
vb@190
  1693
vb@190
  1694
    if (comm_type_determined == false)
vb@237
  1695
        *color = PEP_rating_undefined;
vb@190
  1696
    else
vb@486
  1697
        *color = MAX(_rating(max_comm_type, PEP_rating_undefined),
vb@486
  1698
                PEP_rating_unencrypted);
vb@190
  1699
vb@190
  1700
    return PEP_STATUS_OK;
vb@190
  1701
}
vb@190
  1702
vb@240
  1703
DYNAMIC_API PEP_STATUS identity_color(
vb@239
  1704
        PEP_SESSION session,
vb@239
  1705
        pEp_identity *ident,
vb@239
  1706
        PEP_color *color
vb@239
  1707
    )
vb@239
  1708
{
vb@239
  1709
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  1710
vb@239
  1711
    assert(session);
vb@239
  1712
    assert(ident);
vb@239
  1713
    assert(color);
vb@239
  1714
vb@239
  1715
    if (!(session && ident && color))
vb@239
  1716
        return PEP_ILLEGAL_VALUE;
vb@239
  1717
vb@239
  1718
    if (ident->me)
vb@239
  1719
        status = myself(session, ident);
vb@239
  1720
    else
vb@239
  1721
        status = update_identity(session, ident);
vb@239
  1722
vb@239
  1723
    if (status == PEP_STATUS_OK)
vb@486
  1724
        *color = _rating(ident->comm_type, PEP_rating_undefined);
vb@239
  1725
vb@239
  1726
    return status;
vb@239
  1727
}
vb@239
  1728
vb@507
  1729
DYNAMIC_API PEP_STATUS get_binary_path(PEP_cryptotech tech, const char **path)
vb@507
  1730
{
vb@507
  1731
    PEP_STATUS status = PEP_STATUS_OK;
vb@507
  1732
vb@507
  1733
    assert(path);
vb@507
  1734
    if (path == NULL)
vb@507
  1735
        return PEP_ILLEGAL_VALUE;
vb@507
  1736
vb@507
  1737
    if (cryptotech[tech].binary_path == NULL)
vb@507
  1738
        *path = NULL;
vb@507
  1739
    else
vb@507
  1740
        status = cryptotech[tech].binary_path(path);
vb@507
  1741
vb@507
  1742
    return status;
vb@507
  1743
}
vb@507
  1744