src/message_api.c
author Volker Birk <vb@pep.foundation>
Thu, 11 Aug 2016 21:48:26 +0200
branchkeysync
changeset 1004 9828752c0751
parent 1001 42e0841b71f0
child 1092 222705a14f1a
permissions -rw-r--r--
changing rating and color
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@935
   106
    const char * const subject = "Subject: ";
krista@935
   107
    const size_t SUBJ_LEN = 9;
krista@935
   108
    const char * const newlines = "\n\n";
krista@935
   109
    const size_t NL_LEN = 2;
krista@935
   110
krista@935
   111
    size_t bufsize = SUBJ_LEN + strlen(shortmsg) + NL_LEN + strlen(longmsg) + 1;
krista@903
   112
    ptext = calloc(1, bufsize);
vb@109
   113
    assert(ptext);
vb@62
   114
    if (ptext == NULL)
vb@62
   115
        return NULL;
vb@62
   116
krista@935
   117
    strlcpy(ptext, subject, bufsize);
krista@918
   118
    strlcat(ptext, shortmsg, bufsize);
krista@935
   119
    strlcat(ptext, newlines, bufsize);
krista@918
   120
    strlcat(ptext, longmsg, bufsize);
vb@62
   121
vb@62
   122
    return ptext;
vb@62
   123
}
vb@44
   124
krista@853
   125
static int separate_short_and_long(const char *src, char **shortmsg, char **longmsg)
vb@82
   126
{
vb@82
   127
    char *_shortmsg = NULL;
vb@82
   128
    char *_longmsg = NULL;
vb@82
   129
vb@82
   130
    assert(src);
vb@82
   131
    assert(shortmsg);
vb@82
   132
    assert(longmsg);
krista@853
   133
    
krista@853
   134
    if (src == NULL || shortmsg == NULL || longmsg == NULL)
krista@853
   135
        return -1;
vb@82
   136
vb@82
   137
    *shortmsg = NULL;
vb@82
   138
    *longmsg = NULL;
vb@82
   139
vb@85
   140
    if (strncasecmp(src, "subject: ", 9) == 0) {
vb@82
   141
        char *line_end = strchr(src, '\n');
vb@311
   142
vb@82
   143
        if (line_end == NULL) {
vb@82
   144
            _shortmsg = strdup(src + 9);
vb@469
   145
            assert(_shortmsg);
vb@82
   146
            if (_shortmsg == NULL)
vb@82
   147
                goto enomem;
vb@82
   148
            // _longmsg = NULL;
vb@82
   149
        }
vb@82
   150
        else {
vb@82
   151
            size_t n = line_end - src;
vb@166
   152
vb@82
   153
            if (*(line_end - 1) == '\r')
vb@166
   154
                _shortmsg = strndup(src + 9, n - 10);
vb@82
   155
            else
vb@166
   156
                _shortmsg = strndup(src + 9, n - 9);
vb@469
   157
            assert(_shortmsg);
vb@82
   158
            if (_shortmsg == NULL)
vb@82
   159
                goto enomem;
vb@166
   160
vb@166
   161
            while (*(src + n) && (*(src + n) == '\n' || *(src + n) == '\r'))
vb@166
   162
                ++n;
vb@166
   163
vb@166
   164
            if (*(src + n)) {
vb@166
   165
                _longmsg = strdup(src + n);
vb@469
   166
                assert(_longmsg);
vb@166
   167
                if (_longmsg == NULL)
vb@166
   168
                    goto enomem;
vb@166
   169
            }
vb@82
   170
        }
vb@82
   171
    }
vb@82
   172
    else {
vb@113
   173
        _shortmsg = strdup("");
vb@469
   174
        assert(_shortmsg);
vb@82
   175
        if (_shortmsg == NULL)
vb@82
   176
            goto enomem;
vb@82
   177
        _longmsg = strdup(src);
vb@469
   178
        assert(_longmsg);
vb@82
   179
        if (_longmsg == NULL)
vb@82
   180
            goto enomem;
vb@82
   181
    }
vb@311
   182
vb@82
   183
    *shortmsg = _shortmsg;
vb@82
   184
    *longmsg = _longmsg;
vb@82
   185
vb@82
   186
    return 0;
vb@82
   187
vb@82
   188
enomem:
vb@82
   189
    free(_shortmsg);
vb@82
   190
    free(_longmsg);
vb@82
   191
vb@82
   192
    return -1;
vb@82
   193
}
vb@82
   194
vb@113
   195
static PEP_STATUS copy_fields(message *dst, const message *src)
vb@113
   196
{
vb@164
   197
    assert(dst);
vb@164
   198
    assert(src);
vb@164
   199
Edouard@840
   200
    if(!(dst && src))
Edouard@840
   201
        return PEP_ILLEGAL_VALUE;
Edouard@840
   202
vb@113
   203
    free_timestamp(dst->sent);
vb@113
   204
    dst->sent = NULL;
vb@113
   205
    if (src->sent) {
vb@113
   206
        dst->sent = timestamp_dup(src->sent);
vb@113
   207
        if (dst->sent == NULL)
vb@113
   208
            return PEP_OUT_OF_MEMORY;
vb@113
   209
    }
vb@113
   210
vb@113
   211
    free_timestamp(dst->recv);
vb@113
   212
    dst->recv = NULL;
vb@113
   213
    if (src->recv) {
vb@113
   214
        dst->recv = timestamp_dup(src->recv);
vb@113
   215
        if (dst->recv == NULL)
vb@113
   216
            return PEP_OUT_OF_MEMORY;
vb@113
   217
    }
vb@113
   218
vb@113
   219
    free_identity(dst->from);
vb@113
   220
    dst->from = NULL;
vb@113
   221
    if (src->from) {
vb@113
   222
        dst->from = identity_dup(src->from);
vb@113
   223
        if (dst->from == NULL)
vb@113
   224
            return PEP_OUT_OF_MEMORY;
vb@113
   225
    }
vb@113
   226
vb@113
   227
    free_identity_list(dst->to);
vb@113
   228
    dst->to = NULL;
vb@274
   229
    if (src->to && src->to->ident) {
vb@113
   230
        dst->to = identity_list_dup(src->to);
vb@113
   231
        if (dst->to == NULL)
vb@113
   232
            return PEP_OUT_OF_MEMORY;
vb@113
   233
    }
vb@113
   234
vb@113
   235
    free_identity(dst->recv_by);
vb@113
   236
    dst->recv_by = NULL;
vb@113
   237
    if (src->recv_by) {
vb@113
   238
        dst->recv_by = identity_dup(src->recv_by);
vb@113
   239
        if (dst->recv_by == NULL)
vb@113
   240
            return PEP_OUT_OF_MEMORY;
vb@113
   241
    }
vb@113
   242
vb@113
   243
    free_identity_list(dst->cc);
vb@113
   244
    dst->cc = NULL;
vb@274
   245
    if (src->cc && src->cc->ident) {
vb@113
   246
        dst->cc = identity_list_dup(src->cc);
vb@113
   247
        if (dst->cc == NULL)
vb@113
   248
            return PEP_OUT_OF_MEMORY;
vb@113
   249
    }
vb@113
   250
vb@113
   251
    free_identity_list(dst->bcc);
vb@113
   252
    dst->bcc = NULL;
vb@274
   253
    if (src->bcc && src->bcc->ident) {
vb@113
   254
        dst->bcc = identity_list_dup(src->bcc);
vb@113
   255
        if (dst->bcc == NULL)
vb@113
   256
            return PEP_OUT_OF_MEMORY;
vb@113
   257
    }
vb@113
   258
vb@113
   259
    free_identity_list(dst->reply_to);
vb@113
   260
    dst->reply_to = NULL;
vb@274
   261
    if (src->reply_to && src->reply_to->ident) {
vb@113
   262
        dst->reply_to = identity_list_dup(src->reply_to);
vb@113
   263
        if (dst->reply_to == NULL)
vb@113
   264
            return PEP_OUT_OF_MEMORY;
vb@113
   265
    }
vb@113
   266
vb@113
   267
    free_stringlist(dst->in_reply_to);
vb@113
   268
    dst->in_reply_to = NULL;
vb@274
   269
    if (src->in_reply_to && src->in_reply_to->value) {
vb@113
   270
        dst->in_reply_to = stringlist_dup(src->in_reply_to);
vb@113
   271
        if (dst->in_reply_to == NULL)
vb@113
   272
            return PEP_OUT_OF_MEMORY;
vb@113
   273
    }
vb@113
   274
vb@113
   275
    free_stringlist(dst->references);
vb@113
   276
    dst->references = NULL;
vb@113
   277
    if (src->references) {
vb@113
   278
        dst->references = stringlist_dup(src->references);
vb@113
   279
        if (dst->references == NULL)
vb@113
   280
            return PEP_OUT_OF_MEMORY;
vb@113
   281
    }
vb@113
   282
vb@113
   283
    free_stringlist(dst->keywords);
vb@113
   284
    dst->keywords = NULL;
vb@274
   285
    if (src->keywords && src->keywords->value) {
vb@113
   286
        dst->keywords = stringlist_dup(src->keywords);
vb@113
   287
        if (dst->keywords == NULL)
vb@113
   288
            return PEP_OUT_OF_MEMORY;
vb@113
   289
    }
vb@113
   290
vb@113
   291
    free(dst->comments);
vb@113
   292
    dst->comments = NULL;
vb@113
   293
    if (src->comments) {
vb@113
   294
        dst->comments = strdup(src->comments);
vb@113
   295
        assert(dst->comments);
vb@113
   296
        if (dst->comments == NULL)
vb@113
   297
            return PEP_OUT_OF_MEMORY;
vb@113
   298
    }
vb@113
   299
vb@113
   300
    return PEP_STATUS_OK;
vb@113
   301
}
vb@113
   302
vb@81
   303
static message * clone_to_empty_message(const message * src)
vb@80
   304
{
vb@113
   305
    PEP_STATUS status;
vb@80
   306
    message * msg = NULL;
vb@80
   307
vb@81
   308
    assert(src);
krista@853
   309
    if (src == NULL)
krista@853
   310
        return NULL;
vb@81
   311
vb@113
   312
    msg = calloc(1, sizeof(message));
vb@113
   313
    assert(msg);
vb@80
   314
    if (msg == NULL)
vb@80
   315
        goto enomem;
vb@80
   316
vb@82
   317
    msg->dir = src->dir;
vb@82
   318
vb@113
   319
    status = copy_fields(msg, src);
vb@113
   320
    if (status != PEP_STATUS_OK)
vb@113
   321
        goto enomem;
vb@81
   322
vb@80
   323
    return msg;
vb@80
   324
vb@80
   325
enomem:
vb@113
   326
    free_message(msg);
vb@80
   327
    return NULL;
vb@80
   328
}
vb@80
   329
vb@260
   330
static PEP_STATUS encrypt_PGP_MIME(
vb@311
   331
    PEP_SESSION session,
vb@311
   332
    const message *src,
vb@311
   333
    stringlist_t *keys,
vb@311
   334
    message *dst
vb@260
   335
    )
vb@260
   336
{
vb@260
   337
    PEP_STATUS status = PEP_STATUS_OK;
vb@260
   338
    bool free_ptext = false;
vb@468
   339
    char *ptext = NULL;
Edouard@882
   340
    char *ctext = NULL;
vb@260
   341
    char *mimetext = NULL;
vb@260
   342
    size_t csize;
vb@260
   343
    assert(dst->longmsg == NULL);
vb@260
   344
    dst->enc_format = PEP_enc_PGP_MIME;
vb@260
   345
vb@260
   346
    if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@466
   347
        if (session->unencrypted_subject) {
vb@466
   348
            dst->shortmsg = strdup(src->shortmsg);
vb@469
   349
            assert(dst->shortmsg);
vb@466
   350
            if (dst->shortmsg == NULL)
vb@466
   351
                goto enomem;
vb@466
   352
            ptext = src->longmsg;
vb@466
   353
        }
vb@466
   354
        else {
vb@466
   355
            ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@466
   356
            if (ptext == NULL)
vb@466
   357
                goto enomem;
vb@466
   358
            free_ptext = true;
vb@466
   359
        }
vb@260
   360
    }
vb@260
   361
    else if (src->longmsg) {
vb@260
   362
        ptext = src->longmsg;
vb@260
   363
    }
vb@260
   364
    else {
vb@260
   365
        ptext = "pEp";
vb@260
   366
    }
vb@260
   367
vb@260
   368
    message *_src = calloc(1, sizeof(message));
vb@260
   369
    assert(_src);
vb@260
   370
    if (_src == NULL)
vb@260
   371
        goto enomem;
vb@260
   372
    _src->longmsg = ptext;
vb@260
   373
    _src->longmsg_formatted = src->longmsg_formatted;
vb@260
   374
    _src->attachments = src->attachments;
vb@260
   375
    _src->enc_format = PEP_enc_none;
vb@260
   376
    status = mime_encode_message(_src, true, &mimetext);
vb@260
   377
    assert(status == PEP_STATUS_OK);
krista@853
   378
    if (status != PEP_STATUS_OK)
krista@853
   379
        goto pep_error;
krista@853
   380
    
Edouard@371
   381
    if (free_ptext){
vb@260
   382
        free(ptext);
Edouard@371
   383
        free_ptext=0;
Edouard@371
   384
    }
vb@260
   385
    free(_src);
vb@260
   386
    assert(mimetext);
vb@260
   387
    if (mimetext == NULL)
vb@260
   388
        goto pep_error;
vb@260
   389
vb@260
   390
    status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
vb@311
   391
        &ctext, &csize);
vb@260
   392
    free(mimetext);
vb@260
   393
    if (ctext == NULL)
vb@260
   394
        goto pep_error;
vb@260
   395
vb@260
   396
    dst->longmsg = strdup("this message was encrypted with p≡p "
lix@487
   397
        "https://pEp-project.org");
vb@469
   398
    assert(dst->longmsg);
vb@260
   399
    if (dst->longmsg == NULL)
vb@260
   400
        goto enomem;
vb@260
   401
vb@260
   402
    char *v = strdup("Version: 1");
vb@469
   403
    assert(v);
vb@260
   404
    if (v == NULL)
vb@260
   405
        goto enomem;
vb@260
   406
roker@801
   407
    bloblist_t *_a = new_bloblist(v, strlen(v), "application/pgp-encrypted", NULL);
vb@260
   408
    if (_a == NULL)
vb@260
   409
        goto enomem;
vb@260
   410
    dst->attachments = _a;
vb@288
   411
Edouard@882
   412
    _a = bloblist_add(_a, ctext, csize, "application/octet-stream",
Edouard@427
   413
        "msg.asc");
vb@260
   414
    if (_a == NULL)
vb@260
   415
        goto enomem;
vb@260
   416
vb@260
   417
    return PEP_STATUS_OK;
vb@260
   418
vb@260
   419
enomem:
vb@260
   420
    status = PEP_OUT_OF_MEMORY;
vb@260
   421
vb@260
   422
pep_error:
vb@260
   423
    if (free_ptext)
vb@260
   424
        free(ptext);
Edouard@882
   425
    free(ctext);
vb@260
   426
    return status;
vb@260
   427
}
vb@260
   428
vb@260
   429
static PEP_STATUS encrypt_PGP_in_pieces(
vb@311
   430
    PEP_SESSION session,
vb@311
   431
    const message *src,
vb@311
   432
    stringlist_t *keys,
vb@311
   433
    message *dst
vb@260
   434
    )
vb@260
   435
{
vb@260
   436
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@882
   437
    char *ctext = NULL;
vb@260
   438
    size_t csize;
vb@468
   439
    char *ptext = NULL;
vb@466
   440
    bool free_ptext = false;
vb@260
   441
vb@260
   442
    assert(dst->longmsg == NULL);
vb@260
   443
    assert(dst->attachments == NULL);
vb@260
   444
vb@260
   445
    dst->enc_format = PEP_enc_pieces;
vb@260
   446
vb@320
   447
    if (src->shortmsg && src->shortmsg[0] && strcmp(src->shortmsg, "pEp") != 0) {
vb@466
   448
        if (session->unencrypted_subject) {
vb@466
   449
            dst->shortmsg = strdup(src->shortmsg);
vb@469
   450
            assert(dst->shortmsg);
vb@466
   451
            if (dst->shortmsg == NULL)
vb@466
   452
                goto enomem;
vb@466
   453
            ptext = src->longmsg;
vb@466
   454
        }
vb@466
   455
        else {
vb@466
   456
            ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@466
   457
            if (ptext == NULL)
vb@466
   458
                goto enomem;
vb@466
   459
            free_ptext = true;
vb@466
   460
        }
vb@260
   461
vb@260
   462
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   463
            &csize);
vb@466
   464
        if (free_ptext)
vb@466
   465
            free(ptext);
vb@466
   466
        free_ptext = false;
vb@288
   467
        if (ctext) {
Edouard@882
   468
            dst->longmsg = ctext;
vb@288
   469
        }
vb@288
   470
        else {
vb@260
   471
            goto pep_error;
vb@288
   472
        }
vb@260
   473
    }
vb@320
   474
    else if (src->longmsg && src->longmsg[0]) {
vb@466
   475
        ptext = src->longmsg;
vb@260
   476
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   477
            &csize);
vb@288
   478
        if (ctext) {
Edouard@882
   479
            dst->longmsg = ctext;
vb@288
   480
        }
vb@288
   481
        else {
vb@260
   482
            goto pep_error;
vb@288
   483
        }
vb@284
   484
    }
vb@284
   485
    else {
vb@284
   486
        dst->longmsg = strdup("");
vb@469
   487
        assert(dst->longmsg);
vb@284
   488
        if (dst->longmsg == NULL)
vb@284
   489
            goto enomem;
vb@260
   490
    }
vb@260
   491
vb@320
   492
    if (src->longmsg_formatted && src->longmsg_formatted[0]) {
vb@466
   493
        ptext = src->longmsg_formatted;
vb@260
   494
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   495
            &csize);
vb@260
   496
        if (ctext) {
vb@288
   497
Edouard@882
   498
            bloblist_t *_a = bloblist_add(dst->attachments, ctext, csize,
vb@311
   499
                "application/octet-stream", "PGPexch.htm.pgp");
vb@284
   500
            if (_a == NULL)
vb@260
   501
                goto enomem;
vb@284
   502
            if (dst->attachments == NULL)
vb@284
   503
                dst->attachments = _a;
vb@260
   504
        }
vb@260
   505
        else {
vb@260
   506
            goto pep_error;
vb@260
   507
        }
vb@260
   508
    }
vb@260
   509
vb@260
   510
    if (src->attachments) {
vb@284
   511
        if (dst->attachments == NULL) {
vb@284
   512
            dst->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@284
   513
            if (dst->attachments == NULL)
vb@284
   514
                goto enomem;
vb@284
   515
        }
vb@260
   516
vb@284
   517
        bloblist_t *_s = src->attachments;
vb@284
   518
        bloblist_t *_d = dst->attachments;
vb@284
   519
Edouard@754
   520
        for (int n = 0; _s; _s = _s->next) {
Edouard@754
   521
            if (_s->value == NULL && _s->size == 0) {
Edouard@754
   522
                _d = bloblist_add(_d, NULL, 0, _s->mime_type, _s->filename);
vb@260
   523
                if (_d == NULL)
vb@260
   524
                    goto enomem;
vb@260
   525
            }
vb@260
   526
            else {
Edouard@754
   527
                size_t psize = _s->size;
Edouard@754
   528
                ptext = _s->value;
Edouard@754
   529
                status = encrypt_and_sign(session, keys, ptext, psize, &ctext,
Edouard@754
   530
                    &csize);
Edouard@754
   531
                if (ctext) {
Edouard@754
   532
                    char *filename = NULL;
Edouard@754
   533
Edouard@754
   534
                    if (_s->filename) {
Edouard@754
   535
                        size_t len = strlen(_s->filename);
krista@975
   536
                        size_t bufsize = len + 5; // length of .pgp extension + NUL
krista@975
   537
                        filename = calloc(1, bufsize);
Edouard@754
   538
                        if (filename == NULL)
Edouard@754
   539
                            goto enomem;
Edouard@754
   540
krista@975
   541
                        strlcpy(filename, _s->filename, bufsize);
krista@975
   542
                        strlcat(filename, ".pgp", bufsize);
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@1004
   606
static const char * rating_to_string(PEP_rating rating)
vb@311
   607
{
vb@1004
   608
    switch (rating) {
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:
krista@961
   632
        return "under_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@1004
   640
    PEP_rating rating,
vb@311
   641
    stringlist_t *keylist
vb@311
   642
    )
vb@311
   643
{
vb@311
   644
    assert(msg);
vb@311
   645
krista@942
   646
    add_opt_field(msg, "X-pEp-Version", PEP_VERSION);
vb@311
   647
    
vb@1004
   648
    if (rating != PEP_rating_undefined)
vb@1004
   649
        add_opt_field(msg, "X-EncStatus", rating_to_string(rating));
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@1004
   658
static PEP_rating _rating(PEP_comm_type ct, PEP_rating rating)
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@1004
   669
    if (rating == 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@1004
   674
        if (rating > 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@1004
   680
    if (rating == 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@1004
   753
static PEP_rating decrypt_rating(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@1004
   780
static PEP_rating key_rating(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@1004
   797
static PEP_rating keylist_rating(PEP_SESSION session, stringlist_t *keylist)
vb@311
   798
{
vb@1004
   799
    PEP_rating rating = 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@1004
   810
        PEP_rating _rating_ = key_rating(session, _kl->value);
vb@1004
   811
        if (_rating_ <= PEP_rating_mistrust)
vb@1004
   812
            return _rating_;
vb@311
   813
vb@1004
   814
        if (rating == PEP_rating_undefined)
vb@1004
   815
            rating = _rating_;
vb@486
   816
vb@1004
   817
        if (_rating_ >= 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@1004
   822
                rating = PEP_rating_unencrypted_for_some;
vb@311
   823
            else
vb@1004
   824
                rating = _rating(ct, rating);
vb@486
   825
        }
vb@1004
   826
        else if (_rating_ == PEP_rating_unencrypted) {
vb@1004
   827
            if (rating > PEP_rating_unencrypted_for_some)
vb@1004
   828
                rating = PEP_rating_unencrypted_for_some;
vb@311
   829
        }
vb@311
   830
    }
vb@311
   831
vb@1004
   832
    return rating;
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
krista@995
  1215
DYNAMIC_API PEP_STATUS encrypt_message_for_self(
krista@992
  1216
        PEP_SESSION session,
krista@992
  1217
        pEp_identity* target_id,
krista@992
  1218
        message *src,
krista@992
  1219
        message **dst,
krista@992
  1220
        PEP_enc_format enc_format
krista@992
  1221
    )
krista@992
  1222
{
krista@992
  1223
    PEP_STATUS status = PEP_STATUS_OK;
krista@992
  1224
    message * msg = NULL;
krista@992
  1225
    stringlist_t * keys = NULL;
krista@992
  1226
krista@992
  1227
    assert(session);
krista@992
  1228
    assert(src);
krista@992
  1229
    assert(dst);
krista@992
  1230
    assert(enc_format != PEP_enc_none);
krista@992
  1231
krista@992
  1232
    if (!(session && src && dst && enc_format != PEP_enc_none))
krista@992
  1233
        return PEP_ILLEGAL_VALUE;
krista@992
  1234
krista@992
  1235
    if (src->dir == PEP_dir_incoming)
krista@992
  1236
        return PEP_ILLEGAL_VALUE;
krista@992
  1237
    
krista@992
  1238
    determine_encryption_format(src);
krista@992
  1239
    if (src->enc_format != PEP_enc_none)
krista@992
  1240
        return PEP_ILLEGAL_VALUE;
krista@992
  1241
krista@995
  1242
    status = myself(session, target_id);
krista@995
  1243
    if (status != PEP_STATUS_OK)
krista@995
  1244
        goto pep_error;
krista@995
  1245
krista@992
  1246
    *dst = NULL;
krista@992
  1247
krista@992
  1248
    
krista@992
  1249
    PEP_STATUS _status = update_identity(session, target_id);
krista@992
  1250
    if (_status != PEP_STATUS_OK) {
krista@992
  1251
        status = _status;
krista@992
  1252
        goto pep_error;
krista@992
  1253
    }
krista@992
  1254
krista@994
  1255
    char* target_fpr = target_id->fpr;
krista@994
  1256
    if (!target_fpr)
krista@994
  1257
        return PEP_KEY_NOT_FOUND; // FIXME: Error condition
krista@994
  1258
        
krista@994
  1259
    keys = new_stringlist(target_fpr);
krista@992
  1260
krista@994
  1261
    
krista@992
  1262
    msg = clone_to_empty_message(src);
krista@992
  1263
    if (msg == NULL)
krista@992
  1264
        goto enomem;
krista@992
  1265
krista@992
  1266
    switch (enc_format) {
krista@992
  1267
        case PEP_enc_PGP_MIME:
krista@992
  1268
        case PEP_enc_PEP: // BUG: should be implemented extra
krista@992
  1269
            status = encrypt_PGP_MIME(session, src, keys, msg);
krista@992
  1270
            break;
krista@992
  1271
krista@992
  1272
        case PEP_enc_pieces:
krista@992
  1273
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
krista@992
  1274
            break;
krista@992
  1275
krista@992
  1276
        /* case PEP_enc_PEP:
krista@992
  1277
            // TODO: implement
krista@992
  1278
            NOT_IMPLEMENTED */
krista@992
  1279
krista@992
  1280
        default:
krista@992
  1281
            assert(0);
krista@992
  1282
            status = PEP_ILLEGAL_VALUE;
krista@992
  1283
            goto pep_error;
krista@992
  1284
    }
krista@992
  1285
        
krista@992
  1286
    if (status == PEP_OUT_OF_MEMORY)
krista@992
  1287
        goto enomem;
krista@992
  1288
    
krista@992
  1289
    if (status != PEP_STATUS_OK)
krista@992
  1290
        goto pep_error;
krista@992
  1291
krista@994
  1292
     if (msg && msg->shortmsg == NULL) {
krista@994
  1293
         msg->shortmsg = strdup("pEp");
krista@994
  1294
         assert(msg->shortmsg);
krista@994
  1295
         if (msg->shortmsg == NULL)
krista@994
  1296
             goto enomem;
krista@994
  1297
     }
krista@992
  1298
krista@992
  1299
    *dst = msg;
krista@992
  1300
    return status;
krista@992
  1301
krista@992
  1302
enomem:
krista@992
  1303
    status = PEP_OUT_OF_MEMORY;
krista@992
  1304
krista@992
  1305
pep_error:
krista@992
  1306
    free_stringlist(keys);
krista@992
  1307
    free_message(msg);
krista@992
  1308
krista@992
  1309
    return status;
krista@992
  1310
}
krista@992
  1311
vb@781
  1312
static bool is_a_pEpmessage(const message *msg)
vb@781
  1313
{
vb@781
  1314
    for (stringpair_list_t *i = msg->opt_fields; i && i->value ; i=i->next) {
vb@781
  1315
        if (strcasecmp(i->value->key, "X-pEp-Version") == 0)
vb@781
  1316
            return true;
vb@781
  1317
    }
vb@781
  1318
    return false;
vb@781
  1319
}
vb@781
  1320
vb@781
  1321
// update comm_type to pEp_ct_pEp if needed
vb@781
  1322
Edouard@858
  1323
static PEP_STATUS _update_identity_for_incoming_message(
vb@781
  1324
        PEP_SESSION session,
vb@781
  1325
        const message *src
vb@781
  1326
    )
vb@781
  1327
{
Edouard@858
  1328
    PEP_STATUS status;
Edouard@858
  1329
    if (src->from && src->from->address) {
Edouard@858
  1330
        status = update_identity(session, src->from);
Edouard@858
  1331
        if (status == PEP_STATUS_OK
Edouard@858
  1332
                && is_a_pEpmessage(src)
vb@781
  1333
                && src->from->comm_type >= PEP_ct_OpenPGP_unconfirmed
vb@781
  1334
                && src->from->comm_type != PEP_ct_pEp_unconfirmed
vb@781
  1335
                && src->from->comm_type != PEP_ct_pEp)
vb@781
  1336
        {
vb@781
  1337
            src->from->comm_type |= PEP_ct_pEp_unconfirmed;
Edouard@858
  1338
            status = update_identity(session, src->from);
vb@781
  1339
        }
Edouard@858
  1340
        return status;
vb@781
  1341
    }
Edouard@858
  1342
    return PEP_ILLEGAL_VALUE;
vb@781
  1343
}
vb@781
  1344
Edouard@741
  1345
DYNAMIC_API PEP_STATUS _decrypt_message(
vb@37
  1346
        PEP_SESSION session,
vb@113
  1347
        message *src,
vb@241
  1348
        message **dst,
vb@251
  1349
        stringlist_t **keylist,
vb@1004
  1350
        PEP_rating *rating,
Edouard@741
  1351
        PEP_decrypt_flags_t *flags, 
Edouard@741
  1352
        identity_list **private_il
vb@37
  1353
    )
vb@37
  1354
{
vb@37
  1355
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
  1356
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
  1357
    message *msg = NULL;
vb@112
  1358
    char *ctext;
vb@112
  1359
    size_t csize;
vb@269
  1360
    char *ptext = NULL;
vb@112
  1361
    size_t psize;
vb@241
  1362
    stringlist_t *_keylist = NULL;
vb@37
  1363
vb@74
  1364
    assert(session);
vb@74
  1365
    assert(src);
vb@74
  1366
    assert(dst);
vb@241
  1367
    assert(keylist);
vb@1004
  1368
    assert(rating);
Edouard@739
  1369
    assert(flags);
vb@73
  1370
vb@1004
  1371
    if (!(session && src && dst && keylist && rating && flags))
vb@191
  1372
        return PEP_ILLEGAL_VALUE;
vb@191
  1373
Edouard@739
  1374
    *flags = 0;
Edouard@739
  1375
Edouard@734
  1376
    // Private key in unencrypted mail are ignored -> NULL
Edouard@734
  1377
    bool imported_keys = import_attached_keys(session, src, NULL);
Edouard@728
  1378
Edouard@728
  1379
    // Update src->from in case we just imported a key
Edouard@728
  1380
    // we would need to check signature
Edouard@858
  1381
    status = _update_identity_for_incoming_message(session, src);
Edouard@858
  1382
    if(status != PEP_STATUS_OK)
Edouard@858
  1383
        return status;
Edouard@858
  1384
vb@259
  1385
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
  1386
vb@74
  1387
    *dst = NULL;
vb@251
  1388
    *keylist = NULL;
vb@1004
  1389
    *rating = PEP_rating_undefined;
vb@81
  1390
 
vb@261
  1391
    switch (src->enc_format) {
vb@269
  1392
        case PEP_enc_none:
vb@1004
  1393
            *rating = PEP_rating_unencrypted;
vb@731
  1394
            if (imported_keys)
vb@731
  1395
                remove_attached_keys(src);
vb@301
  1396
            return PEP_UNENCRYPTED;
vb@269
  1397
vb@261
  1398
        case PEP_enc_PGP_MIME:
vb@301
  1399
            ctext = src->attachments->next->value;
vb@269
  1400
            csize = src->attachments->next->size;
vb@261
  1401
            break;
vb@261
  1402
vb@261
  1403
        case PEP_enc_pieces:
vb@261
  1404
            ctext = src->longmsg;
vb@261
  1405
            csize = strlen(ctext);
vb@261
  1406
            break;
vb@261
  1407
vb@261
  1408
        default:
vb@261
  1409
            NOT_IMPLEMENTED
vb@259
  1410
    }
Edouard@431
  1411
    status = cryptotech[crypto].decrypt_and_verify(session, ctext,
Edouard@431
  1412
                                                   csize, &ptext, &psize, &_keylist);
Edouard@431
  1413
    if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
Edouard@431
  1414
        goto pep_error;
vb@113
  1415
Edouard@431
  1416
    decrypt_status = status;
vb@251
  1417
Edouard@741
  1418
    bool imported_private_key_address = false; 
Edouard@728
  1419
vb@256
  1420
    if (ptext) {
vb@256
  1421
        switch (src->enc_format) {
vb@256
  1422
            case PEP_enc_PGP_MIME:
vb@269
  1423
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  1424
                if (status != PEP_STATUS_OK)
vb@113
  1425
                    goto pep_error;
vb@256
  1426
                break;
vb@256
  1427
vb@256
  1428
            case PEP_enc_pieces:
vb@256
  1429
                msg = clone_to_empty_message(src);
vb@256
  1430
                if (msg == NULL)
vb@113
  1431
                    goto enomem;
vb@113
  1432
Edouard@882
  1433
                msg->longmsg = ptext;
Edouard@882
  1434
                ptext = NULL;
vb@256
  1435
vb@256
  1436
                bloblist_t *_m = msg->attachments;
vb@320
  1437
                if (_m == NULL && src->attachments && src->attachments->value) {
vb@320
  1438
                    msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@320
  1439
                    _m = msg->attachments;
vb@320
  1440
                }
vb@320
  1441
vb@256
  1442
                bloblist_t *_s;
Edouard@754
  1443
                for (_s = src->attachments; _s; _s = _s->next) {
Edouard@754
  1444
                    if (_s->value == NULL && _s->size == 0){
Edouard@754
  1445
                        _m = bloblist_add(_m, NULL, 0, _s->mime_type, _s->filename);
Edouard@754
  1446
                        if (_m == NULL)
Edouard@754
  1447
                            goto enomem;
Edouard@754
  1448
Edouard@754
  1449
                    }
Edouard@754
  1450
                    else if (is_encrypted_attachment(_s)) {
vb@256
  1451
                        stringlist_t *_keylist = NULL;
roker@862
  1452
                        char *attctext  = _s->value;
roker@862
  1453
                        size_t attcsize = _s->size;
Edouard@431
  1454
Edouard@842
  1455
                        free(ptext);
Edouard@842
  1456
                        ptext = NULL;
Edouard@842
  1457
Edouard@431
  1458
                        status = decrypt_and_verify(session, attctext, attcsize,
vb@256
  1459
                                &ptext, &psize, &_keylist);
vb@256
  1460
                        free_stringlist(_keylist);
vb@256
  1461
vb@289
  1462
                        if (ptext) {
vb@289
  1463
                            if (is_encrypted_html_attachment(_s)) {
Edouard@882
  1464
                                msg->longmsg_formatted = ptext;
Edouard@882
  1465
                                ptext = NULL;
vb@289
  1466
                            }
vb@289
  1467
                            else {
roker@862
  1468
                                static const char * const mime_type = "application/octet-stream";
roker@864
  1469
                                char * const filename =
vb@289
  1470
                                    without_double_ending(_s->filename);
vb@289
  1471
                                if (filename == NULL)
vb@289
  1472
                                    goto enomem;
vb@289
  1473
Edouard@882
  1474
                                _m = bloblist_add(_m, ptext, psize, mime_type,
vb@289
  1475
                                    filename);
roker@862
  1476
                                free(filename);
vb@290
  1477
                                if (_m == NULL)
vb@289
  1478
                                    goto enomem;
vb@290
  1479
Edouard@882
  1480
                                ptext = NULL;
Edouard@882
  1481
vb@290
  1482
                                if (msg->attachments == NULL)
vb@290
  1483
                                    msg->attachments = _m;
vb@289
  1484
                            }
vb@256
  1485
                        }
vb@256
  1486
                        else {
vb@320
  1487
                            char *copy = malloc(_s->size);
vb@350
  1488
                            assert(copy);
vb@350
  1489
                            if (copy == NULL)
vb@350
  1490
                                goto enomem;
vb@320
  1491
                            memcpy(copy, _s->value, _s->size);
vb@320
  1492
                            _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@256
  1493
                            if (_m == NULL)
vb@256
  1494
                                goto enomem;
vb@256
  1495
                        }
vb@256
  1496
                    }
vb@320
  1497
                    else {
vb@320
  1498
                        char *copy = malloc(_s->size);
vb@350
  1499
                        assert(copy);
vb@350
  1500
                        if (copy == NULL)
vb@350
  1501
                            goto enomem;
vb@320
  1502
                        memcpy(copy, _s->value, _s->size);
vb@320
  1503
                        _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@320
  1504
                        if (_m == NULL)
vb@320
  1505
                            goto enomem;
vb@320
  1506
                    }
vb@256
  1507
                }
vb@256
  1508
vb@256
  1509
                break;
vb@256
  1510
vb@256
  1511
            default:
vb@256
  1512
                // BUG: must implement more
vb@256
  1513
                NOT_IMPLEMENTED
vb@256
  1514
        }
Edouard@431
  1515
        
vb@256
  1516
        switch (src->enc_format) {
vb@256
  1517
            case PEP_enc_PGP_MIME:
vb@256
  1518
            case PEP_enc_pieces:
vb@256
  1519
                status = copy_fields(msg, src);
vb@256
  1520
                if (status != PEP_STATUS_OK)
vb@256
  1521
                    goto pep_error;
vb@256
  1522
vb@280
  1523
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1524
                {
vb@256
  1525
                    char * shortmsg;
vb@256
  1526
                    char * longmsg;
vb@256
  1527
krista@853
  1528
                    int r = separate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1529
                            &longmsg);
vb@256
  1530
                    if (r == -1)
vb@256
  1531
                        goto enomem;
vb@256
  1532
vb@256
  1533
                    free(msg->shortmsg);
vb@256
  1534
                    free(msg->longmsg);
vb@256
  1535
vb@256
  1536
                    msg->shortmsg = shortmsg;
vb@256
  1537
                    msg->longmsg = longmsg;
vb@256
  1538
                }
vb@256
  1539
                else {
vb@256
  1540
                    msg->shortmsg = strdup(src->shortmsg);
vb@469
  1541
                    assert(msg->shortmsg);
vb@256
  1542
                    if (msg->shortmsg == NULL)
vb@256
  1543
                        goto enomem;
vb@256
  1544
                }
vb@256
  1545
                break;
vb@256
  1546
vb@256
  1547
            default:
vb@256
  1548
                // BUG: must implement more
vb@256
  1549
                NOT_IMPLEMENTED
vb@256
  1550
        }
Edouard@728
  1551
       
Edouard@734
  1552
        // check for private key in decrypted message attachement while inporting
Edouard@741
  1553
        identity_list *_private_il = NULL;
Edouard@741
  1554
        imported_keys = import_attached_keys(session, msg, &_private_il);
Edouard@741
  1555
        if (_private_il && 
Edouard@741
  1556
            identity_list_length(_private_il) == 1 &&
Edouard@741
  1557
            _private_il->ident->address)
Edouard@728
  1558
        {
Edouard@741
  1559
            imported_private_key_address = true;
Edouard@728
  1560
        }
Edouard@741
  1561
Edouard@741
  1562
        if(private_il && imported_private_key_address){
Edouard@741
  1563
            *private_il = _private_il;
Edouard@741
  1564
        }else{
Edouard@741
  1565
            free_identity_list(_private_il);
Edouard@741
  1566
        }
Edouard@728
  1567
         
Edouard@694
  1568
        if(decrypt_status == PEP_DECRYPTED){
Edouard@728
  1569
Edouard@728
  1570
            // TODO optimize if import_attached_keys didn't import any key
Edouard@431
  1571
            
Edouard@431
  1572
            // In case message did decrypt, but no valid signature could be found
Edouard@431
  1573
            // then retry decrypt+verify after importing key.
Edouard@728
  1574
Edouard@728
  1575
            // Update msg->from in case we just imported a key
Edouard@728
  1576
            // we would need to check signature
vb@781
  1577
Edouard@861
  1578
            status = _update_identity_for_incoming_message(session, src);
Edouard@858
  1579
            if(status != PEP_STATUS_OK)
Edouard@858
  1580
                goto pep_error;
Edouard@431
  1581
            
Edouard@431
  1582
            char *re_ptext = NULL;
Edouard@431
  1583
            size_t re_psize;
Edouard@441
  1584
            
Edouard@431
  1585
            free_stringlist(_keylist);
Edouard@431
  1586
            _keylist = NULL;
vb@256
  1587
Edouard@431
  1588
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
Edouard@431
  1589
                csize, &re_ptext, &re_psize, &_keylist);
Edouard@433
  1590
            
Edouard@882
  1591
            free(re_ptext);
Edouard@433
  1592
            
Edouard@431
  1593
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
Edouard@431
  1594
                goto pep_error;
Edouard@431
  1595
            
Edouard@431
  1596
            decrypt_status = status;
Edouard@431
  1597
        }
Edouard@431
  1598
        
vb@1004
  1599
        *rating = decrypt_rating(decrypt_status);
Edouard@431
  1600
        
vb@1004
  1601
        if (*rating > PEP_rating_mistrust) {
vb@1004
  1602
            PEP_rating kl_rating = PEP_rating_undefined;
Edouard@431
  1603
            
Edouard@431
  1604
            if (_keylist)
vb@1004
  1605
                kl_rating = keylist_rating(session, _keylist);
Edouard@431
  1606
            
vb@1004
  1607
            if (kl_rating <= PEP_rating_mistrust) {
vb@1004
  1608
                *rating = kl_rating;
Edouard@445
  1609
            }
vb@1004
  1610
            else if (*rating >= PEP_rating_reliable &&
vb@1004
  1611
                     kl_rating < PEP_rating_reliable) {
vb@1004
  1612
                *rating = PEP_rating_unreliable;
Edouard@431
  1613
            }
vb@1004
  1614
            else if (*rating >= PEP_rating_reliable &&
vb@1004
  1615
                     kl_rating >= PEP_rating_reliable) {
Edouard@431
  1616
                if (!(src->from && src->from->user_id && src->from->user_id[0])) {
vb@1004
  1617
                    *rating = PEP_rating_unreliable;
Edouard@431
  1618
                }
Edouard@431
  1619
                else {
Edouard@431
  1620
                    char *fpr = _keylist->value;
Edouard@431
  1621
                    pEp_identity *_from = new_identity(src->from->address, fpr,
Edouard@431
  1622
                                                       src->from->user_id, src->from->username);
Edouard@431
  1623
                    if (_from == NULL)
Edouard@431
  1624
                        goto enomem;
Edouard@431
  1625
                    status = update_identity(session, _from);
Edouard@431
  1626
                    if (_from->comm_type != PEP_ct_unknown)
vb@1004
  1627
                        *rating = _rating(_from->comm_type, PEP_rating_undefined);
Edouard@431
  1628
                    free_identity(_from);
Edouard@431
  1629
                    if (status != PEP_STATUS_OK)
Edouard@431
  1630
                        goto pep_error;
Edouard@431
  1631
                }
Edouard@431
  1632
            }
Edouard@431
  1633
        }
vb@113
  1634
    }
Edouard@745
  1635
    else
Edouard@745
  1636
    {
vb@1004
  1637
        *rating = decrypt_rating(decrypt_status);
Edouard@745
  1638
        goto pep_error;
Edouard@745
  1639
    }
vb@731
  1640
Edouard@728
  1641
    // Case of own key imported from own trusted message
Edouard@728
  1642
    if (// Message have been reliably decrypted 
Edouard@728
  1643
        msg &&
vb@1004
  1644
        *rating >= PEP_rating_trusted &&
Edouard@728
  1645
        imported_private_key_address &&
Edouard@728
  1646
        // to is [own]
Edouard@728
  1647
        msg->to->ident->user_id &&
Edouard@729
  1648
        strcmp(msg->to->ident->user_id, PEP_OWN_USERID) == 0 
Edouard@729
  1649
        )
Edouard@728
  1650
    {
Edouard@738
  1651
        *flags |= PEP_decrypt_flag_own_private_key;
Edouard@728
  1652
    }
vb@113
  1653
vb@731
  1654
    if (msg) {
vb@1004
  1655
        decorate_message(msg, *rating, _keylist);
vb@731
  1656
        if (imported_keys)
vb@731
  1657
            remove_attached_keys(msg);
vb@731
  1658
    }
vb@235
  1659
vb@952
  1660
    status = receive_DeviceState_msg(session, msg);
vb@952
  1661
    if (status == PEP_MESSAGE_CONSUMED) {
vb@952
  1662
        free_message(msg);
vb@952
  1663
        msg = NULL;
vb@952
  1664
    }
vb@953
  1665
    else if (status != PEP_STATUS_OK){
vb@953
  1666
        goto pep_error;
vb@953
  1667
    }
vb@952
  1668
vb@74
  1669
    *dst = msg;
vb@241
  1670
    *keylist = _keylist;
vb@241
  1671
vb@953
  1672
    return status;
vb@73
  1673
vb@73
  1674
enomem:
vb@73
  1675
    status = PEP_OUT_OF_MEMORY;
vb@73
  1676
vb@73
  1677
pep_error:
Edouard@882
  1678
    free(ptext);
vb@73
  1679
    free_message(msg);
vb@241
  1680
    free_stringlist(_keylist);
vb@39
  1681
vb@37
  1682
    return status;
vb@37
  1683
}
vb@37
  1684
Edouard@741
  1685
DYNAMIC_API PEP_STATUS decrypt_message(
Edouard@741
  1686
        PEP_SESSION session,
Edouard@741
  1687
        message *src,
Edouard@741
  1688
        message **dst,
Edouard@741
  1689
        stringlist_t **keylist,
vb@1004
  1690
        PEP_rating *rating,
Edouard@741
  1691
        PEP_decrypt_flags_t *flags 
Edouard@741
  1692
    )
Edouard@741
  1693
{
vb@1004
  1694
    return _decrypt_message( session, src, dst, keylist, rating, flags, NULL );
Edouard@741
  1695
}
Edouard@741
  1696
Edouard@728
  1697
DYNAMIC_API PEP_STATUS own_message_private_key_details(
Edouard@728
  1698
        PEP_SESSION session,
Edouard@728
  1699
        message *msg,
Edouard@728
  1700
        pEp_identity **ident 
Edouard@728
  1701
    )
Edouard@728
  1702
{
Edouard@728
  1703
    assert(session);
Edouard@728
  1704
    assert(msg);
Edouard@728
  1705
    assert(ident);
Edouard@728
  1706
Edouard@736
  1707
    if (!(session && msg && ident))
Edouard@728
  1708
        return PEP_ILLEGAL_VALUE;
Edouard@728
  1709
krista@956
  1710
    message *dst = NULL; 
krista@956
  1711
    stringlist_t *keylist = NULL;
vb@1004
  1712
    PEP_rating rating;
Edouard@728
  1713
    PEP_decrypt_flags_t flags; 
Edouard@728
  1714
Edouard@729
  1715
    *ident = NULL;
Edouard@729
  1716
Edouard@740
  1717
    identity_list *private_il = NULL;
vb@1004
  1718
    PEP_STATUS status = _decrypt_message(session, msg,  &dst, &keylist, &rating, &flags, &private_il);
vb@953
  1719
    free_message(dst);
vb@953
  1720
    free_stringlist(keylist);
Edouard@728
  1721
Edouard@729
  1722
    if (status == PEP_STATUS_OK &&
Edouard@741
  1723
        flags & PEP_decrypt_flag_own_private_key &&
Edouard@741
  1724
        private_il)
Edouard@729
  1725
    {
Edouard@740
  1726
        *ident = identity_dup(private_il->ident);
Edouard@729
  1727
    }
Edouard@729
  1728
Edouard@741
  1729
    free_identity_list(private_il);
krista@956
  1730
    free_stringlist(keylist);
krista@956
  1731
    free_message(dst);
Edouard@741
  1732
Edouard@728
  1733
    return status;
Edouard@741
  1734
Edouard@728
  1735
}
Edouard@728
  1736
Edouard@858
  1737
static void _max_comm_type_from_identity_list(
Edouard@858
  1738
        identity_list *identities, 
Edouard@858
  1739
        PEP_SESSION session,
Edouard@858
  1740
        PEP_comm_type *max_comm_type,
Edouard@858
  1741
        bool *comm_type_determined
Edouard@858
  1742
    )
Edouard@858
  1743
{
Edouard@858
  1744
    identity_list * il;
Edouard@858
  1745
    for (il = identities; il != NULL; il = il->next)
Edouard@858
  1746
    {
Edouard@858
  1747
        if (il->ident)
Edouard@858
  1748
        {
Edouard@858
  1749
            PEP_STATUS status = update_identity(session, il->ident);
Edouard@858
  1750
            if (status == PEP_STATUS_OK)
Edouard@858
  1751
            {
Edouard@858
  1752
                *max_comm_type = _get_comm_type(session, *max_comm_type,
Edouard@858
  1753
                        il->ident);
Edouard@858
  1754
                *comm_type_determined = true;
Edouard@858
  1755
            }
Edouard@858
  1756
        }
Edouard@858
  1757
    }
Edouard@858
  1758
}
Edouard@858
  1759
vb@1004
  1760
DYNAMIC_API PEP_STATUS outgoing_message_rating(
vb@190
  1761
        PEP_SESSION session,
vb@190
  1762
        message *msg,
vb@1004
  1763
        PEP_rating *rating
vb@190
  1764
    )
vb@190
  1765
{
vb@190
  1766
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
  1767
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  1768
    bool comm_type_determined = false;
vb@190
  1769
vb@190
  1770
    assert(session);
vb@190
  1771
    assert(msg);
vb@251
  1772
    assert(msg->from);
vb@251
  1773
    assert(msg->dir == PEP_dir_outgoing);
vb@1004
  1774
    assert(rating);
vb@190
  1775
vb@1004
  1776
    if (!(session && msg && rating))
vb@191
  1777
        return PEP_ILLEGAL_VALUE;
vb@191
  1778
vb@251
  1779
    if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
vb@251
  1780
        return PEP_ILLEGAL_VALUE;
vb@251
  1781
vb@1004
  1782
    *rating = PEP_rating_undefined;
vb@190
  1783
vb@251
  1784
    status = myself(session, msg->from);
vb@251
  1785
    if (status != PEP_STATUS_OK)
vb@251
  1786
        return status;
vb@251
  1787
Edouard@858
  1788
    _max_comm_type_from_identity_list(msg->to, session,
Edouard@858
  1789
                                      &max_comm_type, &comm_type_determined);
vb@190
  1790
Edouard@858
  1791
    _max_comm_type_from_identity_list(msg->cc, session,
Edouard@858
  1792
                                      &max_comm_type, &comm_type_determined);
Edouard@545
  1793
        
Edouard@858
  1794
    _max_comm_type_from_identity_list(msg->bcc, session,
Edouard@858
  1795
                                      &max_comm_type, &comm_type_determined);
vb@190
  1796
vb@190
  1797
    if (comm_type_determined == false)
vb@1004
  1798
        *rating = PEP_rating_undefined;
vb@190
  1799
    else
vb@1004
  1800
        *rating = _MAX(_rating(max_comm_type, PEP_rating_undefined),
vb@486
  1801
                PEP_rating_unencrypted);
vb@190
  1802
vb@190
  1803
    return PEP_STATUS_OK;
vb@190
  1804
}
vb@190
  1805
vb@1004
  1806
DYNAMIC_API PEP_STATUS identity_rating(
vb@239
  1807
        PEP_SESSION session,
vb@239
  1808
        pEp_identity *ident,
vb@1004
  1809
        PEP_rating *rating
vb@239
  1810
    )
vb@239
  1811
{
vb@239
  1812
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  1813
vb@239
  1814
    assert(session);
vb@239
  1815
    assert(ident);
vb@1004
  1816
    assert(rating);
vb@239
  1817
vb@1004
  1818
    if (!(session && ident && rating))
vb@239
  1819
        return PEP_ILLEGAL_VALUE;
vb@239
  1820
vb@239
  1821
    if (ident->me)
vb@239
  1822
        status = myself(session, ident);
vb@239
  1823
    else
vb@239
  1824
        status = update_identity(session, ident);
vb@239
  1825
vb@239
  1826
    if (status == PEP_STATUS_OK)
vb@1004
  1827
        *rating = _rating(ident->comm_type, PEP_rating_undefined);
vb@239
  1828
vb@239
  1829
    return status;
vb@239
  1830
}
vb@239
  1831
vb@507
  1832
DYNAMIC_API PEP_STATUS get_binary_path(PEP_cryptotech tech, const char **path)
vb@507
  1833
{
vb@507
  1834
    PEP_STATUS status = PEP_STATUS_OK;
vb@507
  1835
vb@507
  1836
    assert(path);
vb@507
  1837
    if (path == NULL)
vb@507
  1838
        return PEP_ILLEGAL_VALUE;
vb@507
  1839
vb@507
  1840
    if (cryptotech[tech].binary_path == NULL)
vb@507
  1841
        *path = NULL;
vb@507
  1842
    else
vb@507
  1843
        status = cryptotech[tech].binary_path(path);
vb@507
  1844
vb@507
  1845
    return status;
vb@507
  1846
}
vb@507
  1847
vb@1004
  1848
vb@1004
  1849
DYNAMIC_API PEP_color color_from_rating(PEP_rating rating)
vb@1004
  1850
{
vb@1004
  1851
    if (rating == PEP_rating_b0rken)
vb@1004
  1852
        return PEP_color_no_color;
vb@1004
  1853
vb@1004
  1854
    if (rating < PEP_rating_undefined)
vb@1004
  1855
        return PEP_color_red;
vb@1004
  1856
vb@1004
  1857
    if (rating < PEP_rating_reliable)
vb@1004
  1858
        return PEP_color_no_color;
vb@1004
  1859
vb@1004
  1860
    if (rating < PEP_rating_trusted)
vb@1004
  1861
        return PEP_color_yellow;
vb@1004
  1862
vb@1004
  1863
    if (rating >= PEP_rating_trusted)
vb@1004
  1864
        return PEP_color_green;
vb@1004
  1865
vb@1004
  1866
    // this should never happen
vb@1004
  1867
    assert(false);
vb@1004
  1868
}
vb@1004
  1869