src/message_api.c
author Krista Grothoff <krista@pep-project.org>
Wed, 10 Aug 2016 15:53:55 +0200
branchENGINE-40
changeset 995 6d8b1c28c9a5
parent 994 808f6fc6c5e5
child 1001 42e0841b71f0
child 1126 129b561095a6
child 1186 3b9e4b2666d4
permissions -rw-r--r--
ENGINE-40 #comment Implemented encrypt_message_for_self() to be used in draft-saving, etc. Will comment in bug on usage (or see message_api.h)
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@926
    11
vb@190
    12
#ifndef MIN
vb@190
    13
#define MIN(A, B) ((B) > (A) ? (A) : (B))
vb@190
    14
#endif
vb@300
    15
#ifndef MAX
vb@300
    16
#define MAX(A, B) ((B) > (A) ? (B) : (A))
vb@300
    17
#endif
vb@190
    18
vb@235
    19
vb@235
    20
static bool string_equality(const char *s1, const char *s2)
vb@235
    21
{
vb@235
    22
    if (s1 == NULL || s2 == NULL)
vb@235
    23
        return false;
vb@235
    24
vb@235
    25
    assert(s1 && s2);
vb@235
    26
vb@235
    27
    return strcmp(s1, s2) == 0;
vb@235
    28
}
vb@235
    29
vb@235
    30
static bool is_mime_type(const bloblist_t *bl, const char *mt)
vb@235
    31
{
vb@235
    32
    assert(mt);
vb@235
    33
vb@235
    34
    return bl && string_equality(bl->mime_type, mt);
vb@235
    35
}
vb@235
    36
krista@854
    37
//
krista@854
    38
// This function presumes the file ending is a proper substring of the
krista@854
    39
// filename (i.e. if bl->filename is "a.pgp" and fe is ".pgp", it will
krista@854
    40
// return true, but if bl->filename is ".pgp" and fe is ".pgp", it will
krista@854
    41
// return false. This is desired behaviour.
krista@854
    42
//
vb@235
    43
static bool is_fileending(const bloblist_t *bl, const char *fe)
vb@235
    44
{
vb@235
    45
    assert(fe);
krista@853
    46
    
krista@853
    47
    if (bl == NULL || bl->filename == NULL || fe == NULL)
vb@235
    48
        return false;
vb@235
    49
vb@235
    50
    assert(bl && bl->filename);
vb@235
    51
vb@235
    52
    size_t fe_len = strlen(fe);
vb@235
    53
    size_t fn_len = strlen(bl->filename);
vb@235
    54
vb@235
    55
    if (fn_len <= fe_len)
vb@235
    56
        return false;
vb@235
    57
vb@235
    58
    assert(fn_len > fe_len);
vb@235
    59
vb@235
    60
    return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
vb@235
    61
}
vb@235
    62
vb@284
    63
static void add_opt_field(message *msg, const char *name, const char *value)
vb@284
    64
{
vb@284
    65
    assert(msg);
vb@284
    66
vb@284
    67
    if (msg && name && value) {
vb@284
    68
        stringpair_t *pair = new_stringpair(name, value);
vb@284
    69
        if (pair == NULL)
vb@284
    70
            return;
vb@284
    71
vb@284
    72
        stringpair_list_t *field = stringpair_list_add(msg->opt_fields, pair);
vb@284
    73
        if (field == NULL)
Edouard@833
    74
        {
Edouard@833
    75
            free_stringpair(pair);
vb@284
    76
            return;
Edouard@833
    77
        }
vb@284
    78
vb@284
    79
        if (msg->opt_fields == NULL)
vb@284
    80
            msg->opt_fields = field;
vb@284
    81
    }
vb@284
    82
}
vb@284
    83
vb@83
    84
static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
vb@62
    85
{
vb@62
    86
    char * ptext;
vb@81
    87
vb@83
    88
    assert(shortmsg);
vb@83
    89
    assert(strcmp(shortmsg, "pEp") != 0);
vb@855
    90
vb@857
    91
    if (!shortmsg || strcmp(shortmsg, "pEp") == 0) {
vb@857
    92
        if (!longmsg) {
vb@857
    93
            return NULL;
vb@857
    94
        }
vb@857
    95
        else {
vb@857
    96
            char *result = strdup(longmsg);
vb@857
    97
            assert(result);
vb@857
    98
            return result;
vb@857
    99
        }
vb@857
   100
    }
krista@853
   101
        
vb@83
   102
    if (longmsg == NULL)
vb@63
   103
        longmsg = "";
vb@63
   104
krista@935
   105
    const char * const subject = "Subject: ";
krista@935
   106
    const size_t SUBJ_LEN = 9;
krista@935
   107
    const char * const newlines = "\n\n";
krista@935
   108
    const size_t NL_LEN = 2;
krista@935
   109
krista@935
   110
    size_t bufsize = SUBJ_LEN + strlen(shortmsg) + NL_LEN + strlen(longmsg) + 1;
krista@903
   111
    ptext = calloc(1, bufsize);
vb@109
   112
    assert(ptext);
vb@62
   113
    if (ptext == NULL)
vb@62
   114
        return NULL;
vb@62
   115
krista@935
   116
    strlcpy(ptext, subject, bufsize);
krista@918
   117
    strlcat(ptext, shortmsg, bufsize);
krista@935
   118
    strlcat(ptext, newlines, bufsize);
krista@918
   119
    strlcat(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);
krista@975
   535
                        size_t bufsize = len + 5; // length of .pgp extension + NUL
krista@975
   536
                        filename = calloc(1, bufsize);
Edouard@754
   537
                        if (filename == NULL)
Edouard@754
   538
                            goto enomem;
Edouard@754
   539
krista@975
   540
                        strlcpy(filename, _s->filename, bufsize);
krista@975
   541
                        strlcat(filename, ".pgp", bufsize);
Edouard@754
   542
                    }
Edouard@754
   543
                    else {
Edouard@754
   544
                        filename = calloc(1, 20);
Edouard@754
   545
                        if (filename == NULL)
Edouard@754
   546
                            goto enomem;
Edouard@754
   547
Edouard@754
   548
                        ++n;
Edouard@754
   549
                        n &= 0xffff;
Edouard@754
   550
                        snprintf(filename, 20, "Attachment%d.pgp", n);
Edouard@754
   551
                    }
Edouard@754
   552
Edouard@882
   553
                    _d = bloblist_add(_d, ctext, csize, "application/octet-stream",
Edouard@754
   554
                        filename);
roker@866
   555
                    free(filename);
Edouard@754
   556
                    if (_d == NULL)
Edouard@754
   557
                        goto enomem;
Edouard@754
   558
                }
Edouard@754
   559
                else {
Edouard@754
   560
                    goto pep_error;
Edouard@754
   561
                }
vb@260
   562
            }
vb@260
   563
        }
vb@260
   564
    }
vb@260
   565
vb@260
   566
    return PEP_STATUS_OK;
vb@260
   567
vb@260
   568
enomem:
vb@260
   569
    status = PEP_OUT_OF_MEMORY;
vb@260
   570
vb@260
   571
pep_error:
vb@466
   572
    if (free_ptext)
vb@466
   573
        free(ptext);
vb@260
   574
    return status;
vb@260
   575
}
vb@260
   576
vb@311
   577
static char * keylist_to_string(const stringlist_t *keylist)
vb@311
   578
{
vb@311
   579
    if (keylist) {
vb@311
   580
        size_t size = stringlist_length(keylist);
vb@311
   581
vb@311
   582
        const stringlist_t *_kl;
vb@311
   583
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   584
            size += strlen(_kl->value);
vb@311
   585
        }
vb@311
   586
vb@311
   587
        char *result = calloc(1, size);
vb@311
   588
        if (result == NULL)
vb@311
   589
            return NULL;
vb@311
   590
vb@311
   591
        char *_r = result;
vb@311
   592
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   593
            _r = stpcpy(_r, _kl->value);
vb@311
   594
            if (_kl->next && _kl->next->value)
vb@311
   595
                _r = stpcpy(_r, ",");
vb@311
   596
        }
vb@311
   597
vb@311
   598
        return result;
vb@311
   599
    }
vb@311
   600
    else {
vb@311
   601
        return NULL;
vb@311
   602
    }
vb@311
   603
}
vb@311
   604
vb@311
   605
static const char * color_to_string(PEP_color color)
vb@311
   606
{
vb@311
   607
    switch (color) {
vb@311
   608
    case PEP_rating_cannot_decrypt:
vb@311
   609
        return "cannot_decrypt";
vb@311
   610
    case PEP_rating_have_no_key:
vb@311
   611
        return "have_no_key";
vb@311
   612
    case PEP_rating_unencrypted:
vb@311
   613
        return "unencrypted";
vb@486
   614
    case PEP_rating_unencrypted_for_some:
vb@486
   615
        return "unencrypted_for_some";
vb@311
   616
    case PEP_rating_unreliable:
vb@311
   617
        return "unreliable";
vb@311
   618
    case PEP_rating_reliable:
vb@311
   619
        return "reliable";
vb@311
   620
    case PEP_rating_trusted:
vb@311
   621
        return "trusted";
vb@311
   622
    case PEP_rating_trusted_and_anonymized:
vb@311
   623
        return "trusted_and_anonymized";
vb@311
   624
    case PEP_rating_fully_anonymous:
vb@311
   625
        return "fully_anonymous";
Edouard@442
   626
    case PEP_rating_mistrust:
Edouard@442
   627
        return "mistrust";
Edouard@442
   628
    case PEP_rating_b0rken:
Edouard@442
   629
        return "b0rken";
vb@311
   630
    case PEP_rating_under_attack:
krista@961
   631
        return "under_attack";
vb@311
   632
    default:
vb@311
   633
        return "undefined";
vb@311
   634
    }
vb@311
   635
}
vb@311
   636
vb@311
   637
static void decorate_message(
vb@311
   638
    message *msg,
vb@311
   639
    PEP_color color,
vb@311
   640
    stringlist_t *keylist
vb@311
   641
    )
vb@311
   642
{
vb@311
   643
    assert(msg);
vb@311
   644
krista@942
   645
    add_opt_field(msg, "X-pEp-Version", PEP_VERSION);
vb@311
   646
    
vb@311
   647
    if (color != PEP_rating_undefined)
vb@311
   648
        add_opt_field(msg, "X-EncStatus", color_to_string(color));
vb@311
   649
vb@311
   650
    if (keylist) {
vb@311
   651
        char *_keylist = keylist_to_string(keylist);
vb@311
   652
        add_opt_field(msg, "X-KeyList", _keylist);
vb@311
   653
        free(_keylist);
vb@311
   654
    }
vb@311
   655
}
vb@311
   656
vb@486
   657
static PEP_color _rating(PEP_comm_type ct, PEP_color color)
vb@311
   658
{
vb@311
   659
    if (ct == PEP_ct_unknown)
vb@311
   660
        return PEP_rating_undefined;
vb@311
   661
vb@311
   662
    else if (ct == PEP_ct_compromized)
vb@311
   663
        return PEP_rating_under_attack;
vb@311
   664
Edouard@442
   665
    else if (ct == PEP_ct_mistrusted)
Edouard@442
   666
        return PEP_rating_mistrust;
Edouard@442
   667
    
vb@486
   668
    if (color == PEP_rating_unencrypted_for_some)
vb@486
   669
        return PEP_rating_unencrypted_for_some;
vb@486
   670
vb@486
   671
    if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel ||
vb@486
   672
            ct == PEP_ct_my_key_not_included) {
vb@486
   673
        if (color > PEP_rating_unencrypted_for_some)
vb@486
   674
            return PEP_rating_unencrypted_for_some;
vb@486
   675
        else
vb@486
   676
            return PEP_rating_unencrypted;
vb@486
   677
    }
vb@486
   678
vb@486
   679
    if (color == PEP_rating_unencrypted)
vb@486
   680
        return PEP_rating_unencrypted_for_some;
vb@486
   681
vb@486
   682
    if (ct >= PEP_ct_confirmed_enc_anon)
vb@311
   683
        return PEP_rating_trusted_and_anonymized;
vb@311
   684
vb@311
   685
    else if (ct >= PEP_ct_strong_encryption)
vb@311
   686
        return PEP_rating_trusted;
vb@311
   687
vb@311
   688
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@311
   689
        return PEP_rating_reliable;
vb@311
   690
vb@311
   691
    else
vb@311
   692
        return PEP_rating_unreliable;
vb@311
   693
}
vb@311
   694
vb@311
   695
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@311
   696
{
vb@311
   697
    char *ext;
vb@311
   698
vb@311
   699
    assert(blob);
vb@311
   700
krista@853
   701
    if (blob == NULL || blob->filename == NULL)
vb@311
   702
        return false;
krista@853
   703
    
vb@311
   704
    ext = strrchr(blob->filename, '.');
vb@311
   705
    if (ext == NULL)
vb@311
   706
        return false;
vb@311
   707
vb@320
   708
    if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
vb@311
   709
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@311
   710
            strcmp(ext, ".asc") == 0)
vb@311
   711
            return true;
vb@311
   712
    }
vb@320
   713
    else if (strcmp(blob->mime_type, "text/plain") == 0) {
vb@311
   714
        if (strcmp(ext, ".asc") == 0)
vb@311
   715
            return true;
vb@311
   716
    }
vb@311
   717
vb@311
   718
    return false;
vb@311
   719
}
vb@311
   720
vb@311
   721
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@311
   722
{
vb@311
   723
    assert(blob);
vb@311
   724
    assert(blob->filename);
krista@853
   725
    if (blob == NULL || blob->filename == NULL)
krista@853
   726
        return false;
vb@311
   727
vb@311
   728
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@311
   729
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@311
   730
            strcmp(blob->filename + 11, ".asc") == 0)
vb@311
   731
            return true;
vb@311
   732
    }
vb@311
   733
vb@311
   734
    return false;
vb@311
   735
}
vb@311
   736
vb@311
   737
static char * without_double_ending(const char *filename)
vb@311
   738
{
vb@311
   739
    assert(filename);
krista@853
   740
    if (filename == NULL)
krista@853
   741
        return NULL;
krista@853
   742
    
roker@862
   743
    char *ext = strrchr(filename, '.');
vb@311
   744
    if (ext == NULL)
vb@311
   745
        return NULL;
vb@311
   746
vb@469
   747
    char *result = strndup(filename, ext - filename);
vb@469
   748
    assert(result);
vb@469
   749
    return result;
vb@311
   750
}
vb@311
   751
vb@311
   752
static PEP_color decrypt_color(PEP_STATUS status)
vb@311
   753
{
vb@311
   754
    switch (status) {
vb@311
   755
    case PEP_UNENCRYPTED:
vb@311
   756
    case PEP_VERIFIED:
vb@311
   757
    case PEP_VERIFY_NO_KEY:
vb@311
   758
    case PEP_VERIFIED_AND_TRUSTED:
vb@311
   759
        return PEP_rating_unencrypted;
vb@311
   760
vb@311
   761
    case PEP_DECRYPTED:
vb@311
   762
        return PEP_rating_unreliable;
vb@311
   763
vb@311
   764
    case PEP_DECRYPTED_AND_VERIFIED:
vb@311
   765
        return PEP_rating_reliable;
vb@311
   766
vb@311
   767
    case PEP_DECRYPT_NO_KEY:
vb@311
   768
        return PEP_rating_have_no_key;
vb@311
   769
vb@311
   770
    case PEP_DECRYPT_WRONG_FORMAT:
vb@311
   771
    case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@311
   772
        return PEP_rating_cannot_decrypt;
vb@311
   773
vb@311
   774
    default:
vb@311
   775
        return PEP_rating_undefined;
vb@311
   776
    }
vb@311
   777
}
vb@311
   778
vb@311
   779
static PEP_color key_color(PEP_SESSION session, const char *fpr)
vb@311
   780
{
vb@311
   781
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@311
   782
vb@311
   783
    assert(session);
vb@311
   784
    assert(fpr);
krista@853
   785
    
krista@853
   786
    if (session == NULL || fpr == NULL)
krista@853
   787
        return PEP_rating_undefined;
vb@311
   788
vb@311
   789
    PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
vb@311
   790
    if (status != PEP_STATUS_OK)
vb@311
   791
        return PEP_rating_undefined;
vb@311
   792
vb@486
   793
    return _rating(comm_type, PEP_rating_undefined);
vb@311
   794
}
vb@311
   795
vb@311
   796
static PEP_color keylist_color(PEP_SESSION session, stringlist_t *keylist)
vb@311
   797
{
vb@311
   798
    PEP_color color = PEP_rating_reliable;
vb@311
   799
vb@311
   800
    assert(keylist && keylist->value);
vb@311
   801
    if (keylist == NULL || keylist->value == NULL)
vb@486
   802
        return PEP_rating_undefined;
vb@311
   803
vb@311
   804
    stringlist_t *_kl;
vb@311
   805
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   806
        PEP_comm_type ct;
vb@311
   807
        PEP_STATUS status;
vb@311
   808
vb@486
   809
        PEP_color _color = key_color(session, _kl->value);
vb@486
   810
        if (_color <= PEP_rating_mistrust)
vb@486
   811
            return _color;
vb@311
   812
vb@486
   813
        if (color == PEP_rating_undefined)
vb@486
   814
            color = _color;
vb@486
   815
vb@486
   816
        if (_color >= PEP_rating_reliable) {
vb@311
   817
            status = least_trust(session, _kl->value, &ct);
vb@311
   818
            if (status != PEP_STATUS_OK)
vb@311
   819
                return PEP_rating_undefined;
vb@311
   820
            if (ct == PEP_ct_unknown)
vb@486
   821
                color = PEP_rating_unencrypted_for_some;
vb@311
   822
            else
vb@486
   823
                color = _rating(ct, color);
vb@486
   824
        }
vb@486
   825
        else if (_color == PEP_rating_unencrypted) {
vb@486
   826
            if (color > PEP_rating_unencrypted_for_some)
vb@486
   827
                color = PEP_rating_unencrypted_for_some;
vb@311
   828
        }
vb@311
   829
    }
vb@311
   830
vb@311
   831
    return color;
vb@311
   832
}
vb@311
   833
vb@311
   834
static PEP_comm_type _get_comm_type(
vb@311
   835
    PEP_SESSION session,
vb@311
   836
    PEP_comm_type max_comm_type,
vb@311
   837
    pEp_identity *ident
vb@311
   838
    )
vb@311
   839
{
vb@311
   840
    PEP_STATUS status = update_identity(session, ident);
vb@311
   841
vb@311
   842
    if (max_comm_type == PEP_ct_compromized)
vb@311
   843
        return PEP_ct_compromized;
Edouard@510
   844
    
Edouard@510
   845
    if (max_comm_type == PEP_ct_mistrusted)
Edouard@510
   846
        return PEP_ct_mistrusted;
vb@311
   847
vb@311
   848
    if (status == PEP_STATUS_OK) {
vb@311
   849
        if (ident->comm_type == PEP_ct_compromized)
vb@311
   850
            return PEP_ct_compromized;
Edouard@510
   851
        else if (ident->comm_type == PEP_ct_mistrusted)
Edouard@510
   852
            return PEP_ct_mistrusted;
vb@311
   853
        else
vb@311
   854
            return MIN(max_comm_type, ident->comm_type);
vb@311
   855
    }
vb@311
   856
    else {
vb@311
   857
        return PEP_ct_unknown;
vb@311
   858
    }
vb@311
   859
}
vb@311
   860
vb@731
   861
static void free_bl_entry(bloblist_t *bl)
vb@731
   862
{
vb@731
   863
    if (bl) {
vb@731
   864
        free(bl->value);
vb@731
   865
        free(bl->mime_type);
vb@731
   866
        free(bl->filename);
vb@731
   867
        free(bl);
vb@731
   868
    }
vb@731
   869
}
vb@731
   870
vb@731
   871
static bool is_key(const bloblist_t *bl)
vb@731
   872
{
Edouard@728
   873
    return (// workaround for Apple Mail bugs
Edouard@728
   874
            (is_mime_type(bl, "application/x-apple-msg-attachment") &&
Edouard@728
   875
             is_fileending(bl, ".asc")) ||
Edouard@728
   876
            // as binary, by file name
Edouard@728
   877
            ((bl->mime_type == NULL ||
Edouard@728
   878
              is_mime_type(bl, "application/octet-stream")) &&
Edouard@728
   879
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
   880
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))) ||
Edouard@728
   881
            // explicit mime type 
Edouard@728
   882
            is_mime_type(bl, "application/pgp-keys") ||
Edouard@728
   883
            // as text, by file name
Edouard@728
   884
            (is_mime_type(bl, "text/plain") &&
Edouard@728
   885
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
   886
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc")))
Edouard@728
   887
           );
vb@731
   888
}
vb@731
   889
vb@731
   890
static void remove_attached_keys(message *msg)
vb@731
   891
{
vb@731
   892
    if (msg) {
vb@731
   893
        bloblist_t *last = NULL;
vb@731
   894
        for (bloblist_t *bl = msg->attachments; bl && bl->value; ) {
vb@731
   895
            bloblist_t *next = bl->next;
vb@731
   896
vb@731
   897
            if (is_key(bl)) {
vb@731
   898
                if (last) {
vb@731
   899
                    last->next = next;
vb@731
   900
                }
vb@731
   901
                else {
vb@731
   902
                    msg->attachments = next;
vb@731
   903
                }
vb@731
   904
                free_bl_entry(bl);
vb@731
   905
            }
vb@731
   906
            else {
vb@731
   907
                last = bl;
vb@731
   908
            }
vb@731
   909
            bl = next;
vb@731
   910
        }
vb@731
   911
    }
vb@731
   912
}
vb@731
   913
Edouard@734
   914
bool import_attached_keys(
Edouard@728
   915
        PEP_SESSION session, 
Edouard@728
   916
        const message *msg,
Edouard@728
   917
        identity_list **private_idents
Edouard@728
   918
    )
vb@311
   919
{
vb@311
   920
    assert(session);
vb@311
   921
    assert(msg);
krista@853
   922
    
krista@853
   923
    if (session == NULL || msg == NULL)
krista@853
   924
        return false;
vb@311
   925
vb@731
   926
    bool remove = false;
vb@731
   927
vb@311
   928
    bloblist_t *bl;
vb@908
   929
    int i = 0;
vb@908
   930
    for (bl = msg->attachments; i < MAX_KEYS_TO_IMPORT && bl && bl->value;
vb@908
   931
            bl = bl->next, i++) 
Edouard@747
   932
    {
vb@908
   933
        if (bl && bl->value && bl->size && bl->size < MAX_KEY_SIZE
vb@908
   934
                && is_key(bl)) 
Edouard@728
   935
        {
Edouard@728
   936
            import_key(session, bl->value, bl->size, private_idents);
vb@731
   937
            remove = true;
vb@311
   938
        }
vb@311
   939
    }
vb@731
   940
    return remove;
vb@311
   941
}
vb@311
   942
Edouard@694
   943
Edouard@694
   944
PEP_STATUS _attach_key(PEP_SESSION session, const char* fpr, message *msg)
vb@311
   945
{
vb@311
   946
    char *keydata;
vb@311
   947
    size_t size;
vb@311
   948
    bloblist_t *bl;
vb@311
   949
Edouard@694
   950
    PEP_STATUS status = export_key(session, fpr, &keydata, &size);
Edouard@694
   951
    assert(status == PEP_STATUS_OK);
Edouard@694
   952
    if (status != PEP_STATUS_OK)
Edouard@694
   953
        return status;
Edouard@694
   954
    assert(size);
Edouard@694
   955
    
Edouard@694
   956
    bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
Edouard@694
   957
                      "pEpkey.asc");
Edouard@694
   958
    
Edouard@694
   959
    if (msg->attachments == NULL && bl)
Edouard@694
   960
        msg->attachments = bl;
Edouard@694
   961
Edouard@694
   962
    return PEP_STATUS_OK;
Edouard@694
   963
}
Edouard@694
   964
Edouard@694
   965
#define ONE_WEEK (7*24*3600)
Edouard@694
   966
Edouard@694
   967
void attach_own_key(PEP_SESSION session, message *msg)
Edouard@694
   968
{
vb@311
   969
    assert(session);
vb@311
   970
    assert(msg);
krista@853
   971
    
Edouard@558
   972
    if (msg->dir == PEP_dir_incoming)
Edouard@558
   973
        return;
Edouard@558
   974
vb@311
   975
    assert(msg->from && msg->from->fpr);
vb@311
   976
    if (msg->from == NULL || msg->from->fpr == NULL)
vb@311
   977
        return;
vb@311
   978
Edouard@694
   979
    if(_attach_key(session, msg->from->fpr, msg) != PEP_STATUS_OK)
vb@311
   980
        return;
Edouard@694
   981
    
Edouard@694
   982
    char *revoked_fpr = NULL;
Edouard@694
   983
    uint64_t revocation_date = 0;
Edouard@694
   984
    
Edouard@694
   985
    if(get_revoked(session, msg->from->fpr,
Edouard@694
   986
                   &revoked_fpr, &revocation_date) == PEP_STATUS_OK &&
Edouard@694
   987
       revoked_fpr != NULL)
Edouard@694
   988
    {
Edouard@694
   989
        time_t now = time(NULL);
Edouard@694
   990
        
Edouard@694
   991
        if (now < (time_t)revocation_date + ONE_WEEK)
Edouard@694
   992
        {
Edouard@694
   993
            _attach_key(session, revoked_fpr, msg);
Edouard@694
   994
        }
Edouard@694
   995
    }
Edouard@694
   996
    free(revoked_fpr);
vb@311
   997
}
vb@311
   998
vb@311
   999
PEP_cryptotech determine_encryption_format(message *msg)
vb@311
  1000
{
vb@311
  1001
    assert(msg);
krista@853
  1002
    
vb@311
  1003
    if (is_PGP_message_text(msg->longmsg)) {
vb@311
  1004
        msg->enc_format = PEP_enc_pieces;
vb@311
  1005
        return PEP_crypt_OpenPGP;
vb@311
  1006
    }
vb@311
  1007
    else if (msg->attachments && msg->attachments->next &&
vb@311
  1008
            is_mime_type(msg->attachments, "application/pgp-encrypted") &&
vb@311
  1009
            is_PGP_message_text(msg->attachments->next->value)
vb@311
  1010
        ) {
vb@311
  1011
        msg->enc_format = PEP_enc_PGP_MIME;
vb@311
  1012
        return PEP_crypt_OpenPGP;
vb@311
  1013
    }
vb@311
  1014
    else {
vb@311
  1015
        msg->enc_format = PEP_enc_none;
vb@311
  1016
        return PEP_crypt_none;
vb@311
  1017
    }
vb@311
  1018
}
vb@311
  1019
vb@48
  1020
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
  1021
        PEP_SESSION session,
vb@113
  1022
        message *src,
vb@37
  1023
        stringlist_t * extra,
vb@38
  1024
        message **dst,
vb@81
  1025
        PEP_enc_format enc_format
vb@37
  1026
    )
vb@37
  1027
{
vb@37
  1028
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
  1029
    message * msg = NULL;
vb@63
  1030
    stringlist_t * keys = NULL;
vb@37
  1031
vb@37
  1032
    assert(session);
vb@37
  1033
    assert(src);
vb@37
  1034
    assert(dst);
Edouard@558
  1035
    assert(enc_format != PEP_enc_none);
vb@81
  1036
Edouard@558
  1037
    if (!(session && src && dst && enc_format != PEP_enc_none))
vb@191
  1038
        return PEP_ILLEGAL_VALUE;
vb@191
  1039
Edouard@550
  1040
    if (src->dir == PEP_dir_incoming)
Edouard@550
  1041
        return PEP_ILLEGAL_VALUE;
Edouard@550
  1042
    
vb@259
  1043
    determine_encryption_format(src);
vb@260
  1044
    if (src->enc_format != PEP_enc_none)
vb@260
  1045
        return PEP_ILLEGAL_VALUE;
vb@259
  1046
vb@37
  1047
    *dst = NULL;
vb@67
  1048
vb@236
  1049
    status = myself(session, src->from);
vb@236
  1050
    if (status != PEP_STATUS_OK)
vb@236
  1051
        goto pep_error;
Edouard@558
  1052
vb@80
  1053
    keys = new_stringlist(src->from->fpr);
vb@63
  1054
    if (keys == NULL)
vb@63
  1055
        goto enomem;
vb@37
  1056
vb@39
  1057
    stringlist_t *_k = keys;
vb@39
  1058
vb@39
  1059
    if (extra) {
vb@39
  1060
        _k = stringlist_append(_k, extra);
vb@63
  1061
        if (_k == NULL)
vb@63
  1062
            goto enomem;
vb@37
  1063
    }
vb@39
  1064
vb@299
  1065
    bool dest_keys_found = true;
Edouard@510
  1066
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@299
  1067
vb@37
  1068
    identity_list * _il;
Edouard@545
  1069
    
Edouard@545
  1070
    if ((_il = src->bcc) && _il->ident)
Edouard@545
  1071
    {
Edouard@545
  1072
        // BCC limited support:
Edouard@545
  1073
        //     - App splits mails with BCC in multiple mails.
Edouard@545
  1074
        //     - Each email is encrypted separately
Edouard@545
  1075
        
Edouard@789
  1076
        if(_il->next || (src->to && src->to->ident) || (src->cc && src->cc->ident))
Edouard@545
  1077
        {
Edouard@545
  1078
            // Only one Bcc with no other recipient allowed for now
Edouard@545
  1079
            return PEP_ILLEGAL_VALUE;
Edouard@545
  1080
        }
Edouard@545
  1081
        
vb@299
  1082
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
  1083
        if (_status != PEP_STATUS_OK) {
vb@299
  1084
            status = _status;
vb@63
  1085
            goto pep_error;
vb@299
  1086
        }
Edouard@545
  1087
        
vb@299
  1088
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@39
  1089
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
  1090
            if (_k == NULL)
vb@63
  1091
                goto enomem;
Edouard@510
  1092
            max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@510
  1093
                                           _il->ident);
vb@37
  1094
        }
vb@299
  1095
        else {
vb@299
  1096
            dest_keys_found = false;
vb@37
  1097
            status = PEP_KEY_NOT_FOUND;
Edouard@545
  1098
        }        
vb@299
  1099
    }
Edouard@545
  1100
    else
Edouard@545
  1101
    {
Edouard@545
  1102
        for (_il = src->to; _il && _il->ident; _il = _il->next) {
Edouard@545
  1103
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1104
            if (_status != PEP_STATUS_OK) {
Edouard@545
  1105
                status = _status;
Edouard@545
  1106
                goto pep_error;
Edouard@545
  1107
            }
vb@299
  1108
Edouard@545
  1109
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1110
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1111
                if (_k == NULL)
Edouard@545
  1112
                    goto enomem;
Edouard@545
  1113
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1114
                                               _il->ident);
Edouard@545
  1115
            }
Edouard@545
  1116
            else {
Edouard@545
  1117
                dest_keys_found = false;
Edouard@545
  1118
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1119
            }
vb@299
  1120
        }
vb@299
  1121
Edouard@545
  1122
        for (_il = src->cc; _il && _il->ident; _il = _il->next) {
Edouard@545
  1123
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1124
            if (_status != PEP_STATUS_OK)
Edouard@545
  1125
            {
Edouard@545
  1126
                status = _status;
Edouard@545
  1127
                goto pep_error;
Edouard@545
  1128
            }
Edouard@545
  1129
Edouard@545
  1130
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1131
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1132
                if (_k == NULL)
Edouard@545
  1133
                    goto enomem;
Edouard@545
  1134
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1135
                                               _il->ident);
Edouard@545
  1136
            }
Edouard@545
  1137
            else {
Edouard@545
  1138
                dest_keys_found = false;
Edouard@545
  1139
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1140
            }
vb@299
  1141
        }
vb@37
  1142
    }
Edouard@510
  1143
    
Edouard@510
  1144
    if (!dest_keys_found ||
Edouard@510
  1145
        stringlist_length(keys) == 0 ||
Edouard@510
  1146
        _rating(max_comm_type,
Edouard@510
  1147
                PEP_rating_undefined) < PEP_rating_reliable)
Edouard@510
  1148
    {
vb@301
  1149
        free_stringlist(keys);
vb@465
  1150
        if (!session->passive_mode)
vb@465
  1151
            attach_own_key(session, src);
vb@301
  1152
        return PEP_UNENCRYPTED;
vb@301
  1153
    }
vb@301
  1154
    else {
vb@299
  1155
        msg = clone_to_empty_message(src);
vb@299
  1156
        if (msg == NULL)
vb@299
  1157
            goto enomem;
vb@299
  1158
Edouard@431
  1159
        attach_own_key(session, src);
Edouard@428
  1160
vb@81
  1161
        switch (enc_format) {
vb@260
  1162
        case PEP_enc_PGP_MIME:
vb@336
  1163
        case PEP_enc_PEP: // BUG: should be implemented extra
vb@260
  1164
            status = encrypt_PGP_MIME(session, src, keys, msg);
vb@260
  1165
            break;
vb@62
  1166
vb@62
  1167
        case PEP_enc_pieces:
vb@260
  1168
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
vb@38
  1169
            break;
vb@38
  1170
vb@336
  1171
        /* case PEP_enc_PEP:
vb@81
  1172
            // TODO: implement
vb@336
  1173
            NOT_IMPLEMENTED */
vb@81
  1174
vb@38
  1175
        default:
vb@38
  1176
            assert(0);
vb@63
  1177
            status = PEP_ILLEGAL_VALUE;
vb@63
  1178
            goto pep_error;
vb@37
  1179
        }
Edouard@392
  1180
        
Edouard@392
  1181
        if (status == PEP_OUT_OF_MEMORY)
Edouard@392
  1182
            goto enomem;
Edouard@392
  1183
        
vb@480
  1184
        if (status != PEP_STATUS_OK)
Edouard@392
  1185
            goto pep_error;
vb@37
  1186
    }
vb@37
  1187
vb@37
  1188
    free_stringlist(keys);
vb@63
  1189
vb@469
  1190
    if (msg && msg->shortmsg == NULL) {
vb@64
  1191
        msg->shortmsg = strdup("pEp");
vb@469
  1192
        assert(msg->shortmsg);
krista@853
  1193
        if (msg->shortmsg == NULL)
krista@853
  1194
            goto enomem;
vb@469
  1195
    }
vb@64
  1196
vb@311
  1197
    if (msg)
vb@311
  1198
        decorate_message(msg, PEP_rating_undefined, NULL);
vb@311
  1199
vb@63
  1200
    *dst = msg;
vb@299
  1201
    return status;
vb@63
  1202
vb@63
  1203
enomem:
vb@63
  1204
    status = PEP_OUT_OF_MEMORY;
vb@63
  1205
vb@63
  1206
pep_error:
vb@63
  1207
    free_stringlist(keys);
vb@63
  1208
    free_message(msg);
vb@63
  1209
vb@37
  1210
    return status;
vb@37
  1211
}
vb@37
  1212
krista@995
  1213
DYNAMIC_API PEP_STATUS encrypt_message_for_self(
krista@992
  1214
        PEP_SESSION session,
krista@992
  1215
        pEp_identity* target_id,
krista@992
  1216
        message *src,
krista@992
  1217
        message **dst,
krista@992
  1218
        PEP_enc_format enc_format
krista@992
  1219
    )
krista@992
  1220
{
krista@992
  1221
    PEP_STATUS status = PEP_STATUS_OK;
krista@992
  1222
    message * msg = NULL;
krista@992
  1223
    stringlist_t * keys = NULL;
krista@992
  1224
krista@992
  1225
    assert(session);
krista@992
  1226
    assert(src);
krista@992
  1227
    assert(dst);
krista@992
  1228
    assert(enc_format != PEP_enc_none);
krista@992
  1229
krista@992
  1230
    if (!(session && src && dst && enc_format != PEP_enc_none))
krista@992
  1231
        return PEP_ILLEGAL_VALUE;
krista@992
  1232
krista@992
  1233
    if (src->dir == PEP_dir_incoming)
krista@992
  1234
        return PEP_ILLEGAL_VALUE;
krista@992
  1235
    
krista@992
  1236
    determine_encryption_format(src);
krista@992
  1237
    if (src->enc_format != PEP_enc_none)
krista@992
  1238
        return PEP_ILLEGAL_VALUE;
krista@992
  1239
krista@995
  1240
    status = myself(session, target_id);
krista@995
  1241
    if (status != PEP_STATUS_OK)
krista@995
  1242
        goto pep_error;
krista@995
  1243
krista@992
  1244
    *dst = NULL;
krista@992
  1245
krista@992
  1246
    
krista@992
  1247
    PEP_STATUS _status = update_identity(session, target_id);
krista@992
  1248
    if (_status != PEP_STATUS_OK) {
krista@992
  1249
        status = _status;
krista@992
  1250
        goto pep_error;
krista@992
  1251
    }
krista@992
  1252
krista@994
  1253
    char* target_fpr = target_id->fpr;
krista@994
  1254
    if (!target_fpr)
krista@994
  1255
        return PEP_KEY_NOT_FOUND; // FIXME: Error condition
krista@994
  1256
        
krista@994
  1257
    keys = new_stringlist(target_fpr);
krista@992
  1258
krista@994
  1259
    
krista@992
  1260
    msg = clone_to_empty_message(src);
krista@992
  1261
    if (msg == NULL)
krista@992
  1262
        goto enomem;
krista@992
  1263
krista@992
  1264
    switch (enc_format) {
krista@992
  1265
        case PEP_enc_PGP_MIME:
krista@992
  1266
        case PEP_enc_PEP: // BUG: should be implemented extra
krista@992
  1267
            status = encrypt_PGP_MIME(session, src, keys, msg);
krista@992
  1268
            break;
krista@992
  1269
krista@992
  1270
        case PEP_enc_pieces:
krista@992
  1271
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
krista@992
  1272
            break;
krista@992
  1273
krista@992
  1274
        /* case PEP_enc_PEP:
krista@992
  1275
            // TODO: implement
krista@992
  1276
            NOT_IMPLEMENTED */
krista@992
  1277
krista@992
  1278
        default:
krista@992
  1279
            assert(0);
krista@992
  1280
            status = PEP_ILLEGAL_VALUE;
krista@992
  1281
            goto pep_error;
krista@992
  1282
    }
krista@992
  1283
        
krista@992
  1284
    if (status == PEP_OUT_OF_MEMORY)
krista@992
  1285
        goto enomem;
krista@992
  1286
    
krista@992
  1287
    if (status != PEP_STATUS_OK)
krista@992
  1288
        goto pep_error;
krista@992
  1289
krista@994
  1290
     if (msg && msg->shortmsg == NULL) {
krista@994
  1291
         msg->shortmsg = strdup("pEp");
krista@994
  1292
         assert(msg->shortmsg);
krista@994
  1293
         if (msg->shortmsg == NULL)
krista@994
  1294
             goto enomem;
krista@994
  1295
     }
krista@992
  1296
krista@992
  1297
    *dst = msg;
krista@992
  1298
    return status;
krista@992
  1299
krista@992
  1300
enomem:
krista@992
  1301
    status = PEP_OUT_OF_MEMORY;
krista@992
  1302
krista@992
  1303
pep_error:
krista@992
  1304
    free_stringlist(keys);
krista@992
  1305
    free_message(msg);
krista@992
  1306
krista@992
  1307
    return status;
krista@992
  1308
}
krista@992
  1309
vb@781
  1310
static bool is_a_pEpmessage(const message *msg)
vb@781
  1311
{
vb@781
  1312
    for (stringpair_list_t *i = msg->opt_fields; i && i->value ; i=i->next) {
vb@781
  1313
        if (strcasecmp(i->value->key, "X-pEp-Version") == 0)
vb@781
  1314
            return true;
vb@781
  1315
    }
vb@781
  1316
    return false;
vb@781
  1317
}
vb@781
  1318
vb@781
  1319
// update comm_type to pEp_ct_pEp if needed
vb@781
  1320
Edouard@858
  1321
static PEP_STATUS _update_identity_for_incoming_message(
vb@781
  1322
        PEP_SESSION session,
vb@781
  1323
        const message *src
vb@781
  1324
    )
vb@781
  1325
{
Edouard@858
  1326
    PEP_STATUS status;
Edouard@858
  1327
    if (src->from && src->from->address) {
Edouard@858
  1328
        status = update_identity(session, src->from);
Edouard@858
  1329
        if (status == PEP_STATUS_OK
Edouard@858
  1330
                && is_a_pEpmessage(src)
vb@781
  1331
                && src->from->comm_type >= PEP_ct_OpenPGP_unconfirmed
vb@781
  1332
                && src->from->comm_type != PEP_ct_pEp_unconfirmed
vb@781
  1333
                && src->from->comm_type != PEP_ct_pEp)
vb@781
  1334
        {
vb@781
  1335
            src->from->comm_type |= PEP_ct_pEp_unconfirmed;
Edouard@858
  1336
            status = update_identity(session, src->from);
vb@781
  1337
        }
Edouard@858
  1338
        return status;
vb@781
  1339
    }
Edouard@858
  1340
    return PEP_ILLEGAL_VALUE;
vb@781
  1341
}
vb@781
  1342
Edouard@741
  1343
DYNAMIC_API PEP_STATUS _decrypt_message(
vb@37
  1344
        PEP_SESSION session,
vb@113
  1345
        message *src,
vb@241
  1346
        message **dst,
vb@251
  1347
        stringlist_t **keylist,
Edouard@728
  1348
        PEP_color *color,
Edouard@741
  1349
        PEP_decrypt_flags_t *flags, 
Edouard@741
  1350
        identity_list **private_il
vb@37
  1351
    )
vb@37
  1352
{
vb@37
  1353
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
  1354
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
  1355
    message *msg = NULL;
vb@112
  1356
    char *ctext;
vb@112
  1357
    size_t csize;
vb@269
  1358
    char *ptext = NULL;
vb@112
  1359
    size_t psize;
vb@241
  1360
    stringlist_t *_keylist = NULL;
vb@37
  1361
vb@74
  1362
    assert(session);
vb@74
  1363
    assert(src);
vb@74
  1364
    assert(dst);
vb@241
  1365
    assert(keylist);
vb@251
  1366
    assert(color);
Edouard@739
  1367
    assert(flags);
vb@73
  1368
Edouard@739
  1369
    if (!(session && src && dst && keylist && color && flags))
vb@191
  1370
        return PEP_ILLEGAL_VALUE;
vb@191
  1371
Edouard@739
  1372
    *flags = 0;
Edouard@739
  1373
Edouard@734
  1374
    // Private key in unencrypted mail are ignored -> NULL
Edouard@734
  1375
    bool imported_keys = import_attached_keys(session, src, NULL);
Edouard@728
  1376
Edouard@728
  1377
    // Update src->from in case we just imported a key
Edouard@728
  1378
    // we would need to check signature
Edouard@858
  1379
    status = _update_identity_for_incoming_message(session, src);
Edouard@858
  1380
    if(status != PEP_STATUS_OK)
Edouard@858
  1381
        return status;
Edouard@858
  1382
vb@259
  1383
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
  1384
vb@74
  1385
    *dst = NULL;
vb@251
  1386
    *keylist = NULL;
vb@251
  1387
    *color = PEP_rating_undefined;
vb@81
  1388
 
vb@261
  1389
    switch (src->enc_format) {
vb@269
  1390
        case PEP_enc_none:
vb@301
  1391
            *color = PEP_rating_unencrypted;
vb@731
  1392
            if (imported_keys)
vb@731
  1393
                remove_attached_keys(src);
vb@301
  1394
            return PEP_UNENCRYPTED;
vb@269
  1395
vb@261
  1396
        case PEP_enc_PGP_MIME:
vb@301
  1397
            ctext = src->attachments->next->value;
vb@269
  1398
            csize = src->attachments->next->size;
vb@261
  1399
            break;
vb@261
  1400
vb@261
  1401
        case PEP_enc_pieces:
vb@261
  1402
            ctext = src->longmsg;
vb@261
  1403
            csize = strlen(ctext);
vb@261
  1404
            break;
vb@261
  1405
vb@261
  1406
        default:
vb@261
  1407
            NOT_IMPLEMENTED
vb@259
  1408
    }
Edouard@431
  1409
    status = cryptotech[crypto].decrypt_and_verify(session, ctext,
Edouard@431
  1410
                                                   csize, &ptext, &psize, &_keylist);
Edouard@431
  1411
    if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
Edouard@431
  1412
        goto pep_error;
vb@113
  1413
Edouard@431
  1414
    decrypt_status = status;
vb@251
  1415
Edouard@741
  1416
    bool imported_private_key_address = false; 
Edouard@728
  1417
vb@256
  1418
    if (ptext) {
vb@256
  1419
        switch (src->enc_format) {
vb@256
  1420
            case PEP_enc_PGP_MIME:
vb@269
  1421
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  1422
                if (status != PEP_STATUS_OK)
vb@113
  1423
                    goto pep_error;
vb@256
  1424
                break;
vb@256
  1425
vb@256
  1426
            case PEP_enc_pieces:
vb@256
  1427
                msg = clone_to_empty_message(src);
vb@256
  1428
                if (msg == NULL)
vb@113
  1429
                    goto enomem;
vb@113
  1430
Edouard@882
  1431
                msg->longmsg = ptext;
Edouard@882
  1432
                ptext = NULL;
vb@256
  1433
vb@256
  1434
                bloblist_t *_m = msg->attachments;
vb@320
  1435
                if (_m == NULL && src->attachments && src->attachments->value) {
vb@320
  1436
                    msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@320
  1437
                    _m = msg->attachments;
vb@320
  1438
                }
vb@320
  1439
vb@256
  1440
                bloblist_t *_s;
Edouard@754
  1441
                for (_s = src->attachments; _s; _s = _s->next) {
Edouard@754
  1442
                    if (_s->value == NULL && _s->size == 0){
Edouard@754
  1443
                        _m = bloblist_add(_m, NULL, 0, _s->mime_type, _s->filename);
Edouard@754
  1444
                        if (_m == NULL)
Edouard@754
  1445
                            goto enomem;
Edouard@754
  1446
Edouard@754
  1447
                    }
Edouard@754
  1448
                    else if (is_encrypted_attachment(_s)) {
vb@256
  1449
                        stringlist_t *_keylist = NULL;
roker@862
  1450
                        char *attctext  = _s->value;
roker@862
  1451
                        size_t attcsize = _s->size;
Edouard@431
  1452
Edouard@842
  1453
                        free(ptext);
Edouard@842
  1454
                        ptext = NULL;
Edouard@842
  1455
Edouard@431
  1456
                        status = decrypt_and_verify(session, attctext, attcsize,
vb@256
  1457
                                &ptext, &psize, &_keylist);
vb@256
  1458
                        free_stringlist(_keylist);
vb@256
  1459
vb@289
  1460
                        if (ptext) {
vb@289
  1461
                            if (is_encrypted_html_attachment(_s)) {
Edouard@882
  1462
                                msg->longmsg_formatted = ptext;
Edouard@882
  1463
                                ptext = NULL;
vb@289
  1464
                            }
vb@289
  1465
                            else {
roker@862
  1466
                                static const char * const mime_type = "application/octet-stream";
roker@864
  1467
                                char * const filename =
vb@289
  1468
                                    without_double_ending(_s->filename);
vb@289
  1469
                                if (filename == NULL)
vb@289
  1470
                                    goto enomem;
vb@289
  1471
Edouard@882
  1472
                                _m = bloblist_add(_m, ptext, psize, mime_type,
vb@289
  1473
                                    filename);
roker@862
  1474
                                free(filename);
vb@290
  1475
                                if (_m == NULL)
vb@289
  1476
                                    goto enomem;
vb@290
  1477
Edouard@882
  1478
                                ptext = NULL;
Edouard@882
  1479
vb@290
  1480
                                if (msg->attachments == NULL)
vb@290
  1481
                                    msg->attachments = _m;
vb@289
  1482
                            }
vb@256
  1483
                        }
vb@256
  1484
                        else {
vb@320
  1485
                            char *copy = malloc(_s->size);
vb@350
  1486
                            assert(copy);
vb@350
  1487
                            if (copy == NULL)
vb@350
  1488
                                goto enomem;
vb@320
  1489
                            memcpy(copy, _s->value, _s->size);
vb@320
  1490
                            _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@256
  1491
                            if (_m == NULL)
vb@256
  1492
                                goto enomem;
vb@256
  1493
                        }
vb@256
  1494
                    }
vb@320
  1495
                    else {
vb@320
  1496
                        char *copy = malloc(_s->size);
vb@350
  1497
                        assert(copy);
vb@350
  1498
                        if (copy == NULL)
vb@350
  1499
                            goto enomem;
vb@320
  1500
                        memcpy(copy, _s->value, _s->size);
vb@320
  1501
                        _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@320
  1502
                        if (_m == NULL)
vb@320
  1503
                            goto enomem;
vb@320
  1504
                    }
vb@256
  1505
                }
vb@256
  1506
vb@256
  1507
                break;
vb@256
  1508
vb@256
  1509
            default:
vb@256
  1510
                // BUG: must implement more
vb@256
  1511
                NOT_IMPLEMENTED
vb@256
  1512
        }
Edouard@431
  1513
        
vb@256
  1514
        switch (src->enc_format) {
vb@256
  1515
            case PEP_enc_PGP_MIME:
vb@256
  1516
            case PEP_enc_pieces:
vb@256
  1517
                status = copy_fields(msg, src);
vb@256
  1518
                if (status != PEP_STATUS_OK)
vb@256
  1519
                    goto pep_error;
vb@256
  1520
vb@280
  1521
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1522
                {
vb@256
  1523
                    char * shortmsg;
vb@256
  1524
                    char * longmsg;
vb@256
  1525
krista@853
  1526
                    int r = separate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1527
                            &longmsg);
vb@256
  1528
                    if (r == -1)
vb@256
  1529
                        goto enomem;
vb@256
  1530
vb@256
  1531
                    free(msg->shortmsg);
vb@256
  1532
                    free(msg->longmsg);
vb@256
  1533
vb@256
  1534
                    msg->shortmsg = shortmsg;
vb@256
  1535
                    msg->longmsg = longmsg;
vb@256
  1536
                }
vb@256
  1537
                else {
vb@256
  1538
                    msg->shortmsg = strdup(src->shortmsg);
vb@469
  1539
                    assert(msg->shortmsg);
vb@256
  1540
                    if (msg->shortmsg == NULL)
vb@256
  1541
                        goto enomem;
vb@256
  1542
                }
vb@256
  1543
                break;
vb@256
  1544
vb@256
  1545
            default:
vb@256
  1546
                // BUG: must implement more
vb@256
  1547
                NOT_IMPLEMENTED
vb@256
  1548
        }
Edouard@728
  1549
       
Edouard@734
  1550
        // check for private key in decrypted message attachement while inporting
Edouard@741
  1551
        identity_list *_private_il = NULL;
Edouard@741
  1552
        imported_keys = import_attached_keys(session, msg, &_private_il);
Edouard@741
  1553
        if (_private_il && 
Edouard@741
  1554
            identity_list_length(_private_il) == 1 &&
Edouard@741
  1555
            _private_il->ident->address)
Edouard@728
  1556
        {
Edouard@741
  1557
            imported_private_key_address = true;
Edouard@728
  1558
        }
Edouard@741
  1559
Edouard@741
  1560
        if(private_il && imported_private_key_address){
Edouard@741
  1561
            *private_il = _private_il;
Edouard@741
  1562
        }else{
Edouard@741
  1563
            free_identity_list(_private_il);
Edouard@741
  1564
        }
Edouard@728
  1565
         
Edouard@694
  1566
        if(decrypt_status == PEP_DECRYPTED){
Edouard@728
  1567
Edouard@728
  1568
            // TODO optimize if import_attached_keys didn't import any key
Edouard@431
  1569
            
Edouard@431
  1570
            // In case message did decrypt, but no valid signature could be found
Edouard@431
  1571
            // then retry decrypt+verify after importing key.
Edouard@728
  1572
Edouard@728
  1573
            // Update msg->from in case we just imported a key
Edouard@728
  1574
            // we would need to check signature
vb@781
  1575
Edouard@861
  1576
            status = _update_identity_for_incoming_message(session, src);
Edouard@858
  1577
            if(status != PEP_STATUS_OK)
Edouard@858
  1578
                goto pep_error;
Edouard@431
  1579
            
Edouard@431
  1580
            char *re_ptext = NULL;
Edouard@431
  1581
            size_t re_psize;
Edouard@441
  1582
            
Edouard@431
  1583
            free_stringlist(_keylist);
Edouard@431
  1584
            _keylist = NULL;
vb@256
  1585
Edouard@431
  1586
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
Edouard@431
  1587
                csize, &re_ptext, &re_psize, &_keylist);
Edouard@433
  1588
            
Edouard@882
  1589
            free(re_ptext);
Edouard@433
  1590
            
Edouard@431
  1591
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
Edouard@431
  1592
                goto pep_error;
Edouard@431
  1593
            
Edouard@431
  1594
            decrypt_status = status;
Edouard@431
  1595
        }
Edouard@431
  1596
        
Edouard@431
  1597
        *color = decrypt_color(decrypt_status);
Edouard@431
  1598
        
vb@485
  1599
        if (*color > PEP_rating_mistrust) {
Edouard@431
  1600
            PEP_color kl_color = PEP_rating_undefined;
Edouard@431
  1601
            
Edouard@431
  1602
            if (_keylist)
Edouard@431
  1603
                kl_color = keylist_color(session, _keylist);
Edouard@431
  1604
            
vb@485
  1605
            if (kl_color <= PEP_rating_mistrust) {
vb@485
  1606
                *color = kl_color;
Edouard@445
  1607
            }
Edouard@431
  1608
            else if (*color >= PEP_rating_reliable &&
Edouard@431
  1609
                     kl_color < PEP_rating_reliable) {
Edouard@431
  1610
                *color = PEP_rating_unreliable;
Edouard@431
  1611
            }
Edouard@431
  1612
            else if (*color >= PEP_rating_reliable &&
Edouard@431
  1613
                     kl_color >= PEP_rating_reliable) {
Edouard@431
  1614
                if (!(src->from && src->from->user_id && src->from->user_id[0])) {
Edouard@431
  1615
                    *color = PEP_rating_unreliable;
Edouard@431
  1616
                }
Edouard@431
  1617
                else {
Edouard@431
  1618
                    char *fpr = _keylist->value;
Edouard@431
  1619
                    pEp_identity *_from = new_identity(src->from->address, fpr,
Edouard@431
  1620
                                                       src->from->user_id, src->from->username);
Edouard@431
  1621
                    if (_from == NULL)
Edouard@431
  1622
                        goto enomem;
Edouard@431
  1623
                    status = update_identity(session, _from);
Edouard@431
  1624
                    if (_from->comm_type != PEP_ct_unknown)
vb@486
  1625
                        *color = _rating(_from->comm_type, PEP_rating_undefined);
Edouard@431
  1626
                    free_identity(_from);
Edouard@431
  1627
                    if (status != PEP_STATUS_OK)
Edouard@431
  1628
                        goto pep_error;
Edouard@431
  1629
                }
Edouard@431
  1630
            }
Edouard@431
  1631
        }
vb@113
  1632
    }
Edouard@745
  1633
    else
Edouard@745
  1634
    {
Edouard@745
  1635
        *color = decrypt_color(decrypt_status);
Edouard@745
  1636
        goto pep_error;
Edouard@745
  1637
    }
vb@731
  1638
Edouard@728
  1639
    // Case of own key imported from own trusted message
Edouard@728
  1640
    if (// Message have been reliably decrypted 
Edouard@728
  1641
        msg &&
Edouard@728
  1642
        *color >= PEP_rating_green &&
Edouard@728
  1643
        imported_private_key_address &&
Edouard@728
  1644
        // to is [own]
Edouard@728
  1645
        msg->to->ident->user_id &&
Edouard@729
  1646
        strcmp(msg->to->ident->user_id, PEP_OWN_USERID) == 0 
Edouard@729
  1647
        )
Edouard@728
  1648
    {
Edouard@738
  1649
        *flags |= PEP_decrypt_flag_own_private_key;
Edouard@728
  1650
    }
vb@113
  1651
vb@731
  1652
    if (msg) {
vb@311
  1653
        decorate_message(msg, *color, _keylist);
vb@731
  1654
        if (imported_keys)
vb@731
  1655
            remove_attached_keys(msg);
vb@731
  1656
    }
vb@235
  1657
vb@74
  1658
    *dst = msg;
vb@241
  1659
    *keylist = _keylist;
vb@241
  1660
vb@267
  1661
    return PEP_STATUS_OK;
vb@73
  1662
vb@73
  1663
enomem:
vb@73
  1664
    status = PEP_OUT_OF_MEMORY;
vb@73
  1665
vb@73
  1666
pep_error:
Edouard@882
  1667
    free(ptext);
vb@73
  1668
    free_message(msg);
vb@241
  1669
    free_stringlist(_keylist);
vb@39
  1670
vb@37
  1671
    return status;
vb@37
  1672
}
vb@37
  1673
Edouard@741
  1674
DYNAMIC_API PEP_STATUS decrypt_message(
Edouard@741
  1675
        PEP_SESSION session,
Edouard@741
  1676
        message *src,
Edouard@741
  1677
        message **dst,
Edouard@741
  1678
        stringlist_t **keylist,
Edouard@741
  1679
        PEP_color *color,
Edouard@741
  1680
        PEP_decrypt_flags_t *flags 
Edouard@741
  1681
    )
Edouard@741
  1682
{
Edouard@741
  1683
    return _decrypt_message( session, src, dst, keylist, color, flags, NULL );
Edouard@741
  1684
}
Edouard@741
  1685
Edouard@728
  1686
DYNAMIC_API PEP_STATUS own_message_private_key_details(
Edouard@728
  1687
        PEP_SESSION session,
Edouard@728
  1688
        message *msg,
Edouard@728
  1689
        pEp_identity **ident 
Edouard@728
  1690
    )
Edouard@728
  1691
{
Edouard@728
  1692
    assert(session);
Edouard@728
  1693
    assert(msg);
Edouard@728
  1694
    assert(ident);
Edouard@728
  1695
Edouard@736
  1696
    if (!(session && msg && ident))
Edouard@728
  1697
        return PEP_ILLEGAL_VALUE;
Edouard@728
  1698
krista@956
  1699
    message *dst = NULL; 
krista@956
  1700
    stringlist_t *keylist = NULL;
Edouard@728
  1701
    PEP_color color;
Edouard@728
  1702
    PEP_decrypt_flags_t flags; 
Edouard@728
  1703
Edouard@729
  1704
    *ident = NULL;
Edouard@729
  1705
Edouard@740
  1706
    identity_list *private_il = NULL;
Edouard@741
  1707
    PEP_STATUS status = _decrypt_message(session, msg,  &dst, &keylist, &color, &flags, &private_il);
Edouard@728
  1708
Edouard@729
  1709
    if (status == PEP_STATUS_OK &&
Edouard@741
  1710
        flags & PEP_decrypt_flag_own_private_key &&
Edouard@741
  1711
        private_il)
Edouard@729
  1712
    {
Edouard@740
  1713
        *ident = identity_dup(private_il->ident);
Edouard@729
  1714
    }
Edouard@729
  1715
Edouard@741
  1716
    free_identity_list(private_il);
krista@956
  1717
    free_stringlist(keylist);
krista@956
  1718
    free_message(dst);
Edouard@741
  1719
Edouard@728
  1720
    return status;
Edouard@741
  1721
Edouard@728
  1722
}
Edouard@728
  1723
Edouard@858
  1724
static void _max_comm_type_from_identity_list(
Edouard@858
  1725
        identity_list *identities, 
Edouard@858
  1726
        PEP_SESSION session,
Edouard@858
  1727
        PEP_comm_type *max_comm_type,
Edouard@858
  1728
        bool *comm_type_determined
Edouard@858
  1729
    )
Edouard@858
  1730
{
Edouard@858
  1731
    identity_list * il;
Edouard@858
  1732
    for (il = identities; il != NULL; il = il->next)
Edouard@858
  1733
    {
Edouard@858
  1734
        if (il->ident)
Edouard@858
  1735
        {
Edouard@858
  1736
            PEP_STATUS status = update_identity(session, il->ident);
Edouard@858
  1737
            if (status == PEP_STATUS_OK)
Edouard@858
  1738
            {
Edouard@858
  1739
                *max_comm_type = _get_comm_type(session, *max_comm_type,
Edouard@858
  1740
                        il->ident);
Edouard@858
  1741
                *comm_type_determined = true;
Edouard@858
  1742
            }
Edouard@858
  1743
        }
Edouard@858
  1744
    }
Edouard@858
  1745
}
Edouard@858
  1746
vb@251
  1747
DYNAMIC_API PEP_STATUS outgoing_message_color(
vb@190
  1748
        PEP_SESSION session,
vb@190
  1749
        message *msg,
vb@232
  1750
        PEP_color *color
vb@190
  1751
    )
vb@190
  1752
{
vb@190
  1753
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
  1754
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  1755
    bool comm_type_determined = false;
vb@190
  1756
vb@190
  1757
    assert(session);
vb@190
  1758
    assert(msg);
vb@251
  1759
    assert(msg->from);
vb@251
  1760
    assert(msg->dir == PEP_dir_outgoing);
vb@190
  1761
    assert(color);
vb@190
  1762
vb@191
  1763
    if (!(session && msg && color))
vb@191
  1764
        return PEP_ILLEGAL_VALUE;
vb@191
  1765
vb@251
  1766
    if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
vb@251
  1767
        return PEP_ILLEGAL_VALUE;
vb@251
  1768
vb@237
  1769
    *color = PEP_rating_undefined;
vb@190
  1770
vb@251
  1771
    status = myself(session, msg->from);
vb@251
  1772
    if (status != PEP_STATUS_OK)
vb@251
  1773
        return status;
vb@251
  1774
Edouard@858
  1775
    _max_comm_type_from_identity_list(msg->to, session,
Edouard@858
  1776
                                      &max_comm_type, &comm_type_determined);
vb@190
  1777
Edouard@858
  1778
    _max_comm_type_from_identity_list(msg->cc, session,
Edouard@858
  1779
                                      &max_comm_type, &comm_type_determined);
Edouard@545
  1780
        
Edouard@858
  1781
    _max_comm_type_from_identity_list(msg->bcc, session,
Edouard@858
  1782
                                      &max_comm_type, &comm_type_determined);
vb@190
  1783
vb@190
  1784
    if (comm_type_determined == false)
vb@237
  1785
        *color = PEP_rating_undefined;
vb@190
  1786
    else
vb@486
  1787
        *color = MAX(_rating(max_comm_type, PEP_rating_undefined),
vb@486
  1788
                PEP_rating_unencrypted);
vb@190
  1789
vb@190
  1790
    return PEP_STATUS_OK;
vb@190
  1791
}
vb@190
  1792
vb@240
  1793
DYNAMIC_API PEP_STATUS identity_color(
vb@239
  1794
        PEP_SESSION session,
vb@239
  1795
        pEp_identity *ident,
vb@239
  1796
        PEP_color *color
vb@239
  1797
    )
vb@239
  1798
{
vb@239
  1799
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  1800
vb@239
  1801
    assert(session);
vb@239
  1802
    assert(ident);
vb@239
  1803
    assert(color);
vb@239
  1804
vb@239
  1805
    if (!(session && ident && color))
vb@239
  1806
        return PEP_ILLEGAL_VALUE;
vb@239
  1807
vb@239
  1808
    if (ident->me)
vb@239
  1809
        status = myself(session, ident);
vb@239
  1810
    else
vb@239
  1811
        status = update_identity(session, ident);
vb@239
  1812
vb@239
  1813
    if (status == PEP_STATUS_OK)
vb@486
  1814
        *color = _rating(ident->comm_type, PEP_rating_undefined);
vb@239
  1815
vb@239
  1816
    return status;
vb@239
  1817
}
vb@239
  1818
vb@507
  1819
DYNAMIC_API PEP_STATUS get_binary_path(PEP_cryptotech tech, const char **path)
vb@507
  1820
{
vb@507
  1821
    PEP_STATUS status = PEP_STATUS_OK;
vb@507
  1822
vb@507
  1823
    assert(path);
vb@507
  1824
    if (path == NULL)
vb@507
  1825
        return PEP_ILLEGAL_VALUE;
vb@507
  1826
vb@507
  1827
    if (cryptotech[tech].binary_path == NULL)
vb@507
  1828
        *path = NULL;
vb@507
  1829
    else
vb@507
  1830
        status = cryptotech[tech].binary_path(path);
vb@507
  1831
vb@507
  1832
    return status;
vb@507
  1833
}
vb@507
  1834