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