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