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