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