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