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