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