src/message_api.c
author Krista Grothoff <krista@pep-project.org>
Tue, 15 Nov 2016 18:16:58 +0100
branchENGINE-74
changeset 1397 0a64816a6e37
parent 1396 26ae3f0cea22
child 1427 72c2539b14e7
permissions -rw-r--r--
stowing changes
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;
krista@1243
   670
    
krista@1243
   671
    else if (ct == PEP_ct_key_not_found)
krista@1243
   672
        return PEP_rating_have_no_key;
krista@1243
   673
        
vb@311
   674
    else if (ct == PEP_ct_compromized)
vb@311
   675
        return PEP_rating_under_attack;
vb@311
   676
Edouard@442
   677
    else if (ct == PEP_ct_mistrusted)
Edouard@442
   678
        return PEP_rating_mistrust;
Edouard@442
   679
    
vb@1004
   680
    if (rating == PEP_rating_unencrypted_for_some)
vb@486
   681
        return PEP_rating_unencrypted_for_some;
vb@486
   682
vb@486
   683
    if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel ||
vb@486
   684
            ct == PEP_ct_my_key_not_included) {
vb@1004
   685
        if (rating > PEP_rating_unencrypted_for_some)
vb@486
   686
            return PEP_rating_unencrypted_for_some;
vb@486
   687
        else
vb@486
   688
            return PEP_rating_unencrypted;
vb@486
   689
    }
vb@486
   690
vb@1004
   691
    if (rating == PEP_rating_unencrypted)
vb@486
   692
        return PEP_rating_unencrypted_for_some;
vb@486
   693
vb@486
   694
    if (ct >= PEP_ct_confirmed_enc_anon)
vb@311
   695
        return PEP_rating_trusted_and_anonymized;
vb@311
   696
vb@311
   697
    else if (ct >= PEP_ct_strong_encryption)
vb@311
   698
        return PEP_rating_trusted;
vb@311
   699
vb@311
   700
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@311
   701
        return PEP_rating_reliable;
vb@311
   702
vb@311
   703
    else
vb@311
   704
        return PEP_rating_unreliable;
vb@311
   705
}
vb@311
   706
vb@311
   707
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@311
   708
{
vb@311
   709
    char *ext;
vb@311
   710
vb@311
   711
    assert(blob);
vb@311
   712
krista@853
   713
    if (blob == NULL || blob->filename == NULL)
vb@311
   714
        return false;
krista@853
   715
    
vb@311
   716
    ext = strrchr(blob->filename, '.');
vb@311
   717
    if (ext == NULL)
vb@311
   718
        return false;
vb@311
   719
vb@320
   720
    if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
vb@311
   721
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@311
   722
            strcmp(ext, ".asc") == 0)
vb@311
   723
            return true;
vb@311
   724
    }
vb@320
   725
    else if (strcmp(blob->mime_type, "text/plain") == 0) {
vb@311
   726
        if (strcmp(ext, ".asc") == 0)
vb@311
   727
            return true;
vb@311
   728
    }
vb@311
   729
vb@311
   730
    return false;
vb@311
   731
}
vb@311
   732
vb@311
   733
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@311
   734
{
vb@311
   735
    assert(blob);
vb@311
   736
    assert(blob->filename);
krista@853
   737
    if (blob == NULL || blob->filename == NULL)
krista@853
   738
        return false;
vb@311
   739
vb@311
   740
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@311
   741
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@311
   742
            strcmp(blob->filename + 11, ".asc") == 0)
vb@311
   743
            return true;
vb@311
   744
    }
vb@311
   745
vb@311
   746
    return false;
vb@311
   747
}
vb@311
   748
vb@311
   749
static char * without_double_ending(const char *filename)
vb@311
   750
{
vb@311
   751
    assert(filename);
krista@853
   752
    if (filename == NULL)
krista@853
   753
        return NULL;
krista@853
   754
    
roker@862
   755
    char *ext = strrchr(filename, '.');
vb@311
   756
    if (ext == NULL)
vb@311
   757
        return NULL;
vb@311
   758
vb@469
   759
    char *result = strndup(filename, ext - filename);
vb@469
   760
    assert(result);
vb@469
   761
    return result;
vb@311
   762
}
vb@311
   763
vb@1004
   764
static PEP_rating decrypt_rating(PEP_STATUS status)
vb@311
   765
{
vb@311
   766
    switch (status) {
vb@311
   767
    case PEP_UNENCRYPTED:
vb@311
   768
    case PEP_VERIFIED:
vb@311
   769
    case PEP_VERIFY_NO_KEY:
vb@311
   770
    case PEP_VERIFIED_AND_TRUSTED:
vb@311
   771
        return PEP_rating_unencrypted;
vb@311
   772
vb@311
   773
    case PEP_DECRYPTED:
vb@311
   774
        return PEP_rating_unreliable;
vb@311
   775
vb@311
   776
    case PEP_DECRYPTED_AND_VERIFIED:
vb@311
   777
        return PEP_rating_reliable;
vb@311
   778
vb@311
   779
    case PEP_DECRYPT_NO_KEY:
vb@311
   780
        return PEP_rating_have_no_key;
vb@311
   781
vb@311
   782
    case PEP_DECRYPT_WRONG_FORMAT:
vb@311
   783
    case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@311
   784
        return PEP_rating_cannot_decrypt;
vb@311
   785
vb@311
   786
    default:
vb@311
   787
        return PEP_rating_undefined;
vb@311
   788
    }
vb@311
   789
}
vb@311
   790
vb@1004
   791
static PEP_rating key_rating(PEP_SESSION session, const char *fpr)
vb@311
   792
{
vb@311
   793
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@311
   794
vb@311
   795
    assert(session);
vb@311
   796
    assert(fpr);
krista@853
   797
    
krista@853
   798
    if (session == NULL || fpr == NULL)
krista@853
   799
        return PEP_rating_undefined;
vb@311
   800
vb@311
   801
    PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
vb@311
   802
    if (status != PEP_STATUS_OK)
vb@311
   803
        return PEP_rating_undefined;
vb@311
   804
vb@486
   805
    return _rating(comm_type, PEP_rating_undefined);
vb@311
   806
}
vb@311
   807
vb@1004
   808
static PEP_rating keylist_rating(PEP_SESSION session, stringlist_t *keylist)
vb@311
   809
{
vb@1004
   810
    PEP_rating rating = PEP_rating_reliable;
vb@311
   811
vb@311
   812
    assert(keylist && keylist->value);
vb@311
   813
    if (keylist == NULL || keylist->value == NULL)
vb@486
   814
        return PEP_rating_undefined;
vb@311
   815
vb@311
   816
    stringlist_t *_kl;
vb@311
   817
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   818
        PEP_comm_type ct;
vb@311
   819
        PEP_STATUS status;
vb@311
   820
vb@1004
   821
        PEP_rating _rating_ = key_rating(session, _kl->value);
vb@1004
   822
        if (_rating_ <= PEP_rating_mistrust)
vb@1004
   823
            return _rating_;
vb@311
   824
vb@1004
   825
        if (rating == PEP_rating_undefined)
vb@1004
   826
            rating = _rating_;
vb@486
   827
vb@1004
   828
        if (_rating_ >= PEP_rating_reliable) {
vb@311
   829
            status = least_trust(session, _kl->value, &ct);
vb@311
   830
            if (status != PEP_STATUS_OK)
vb@311
   831
                return PEP_rating_undefined;
edouard@1156
   832
            if (ct == PEP_ct_unknown){
edouard@1156
   833
                if (rating >= PEP_rating_reliable){
edouard@1154
   834
                    rating = PEP_rating_reliable;
edouard@1156
   835
                }
edouard@1156
   836
            }
edouard@1156
   837
            else{
vb@1004
   838
                rating = _rating(ct, rating);
edouard@1156
   839
            }
vb@486
   840
        }
vb@1004
   841
        else if (_rating_ == PEP_rating_unencrypted) {
vb@1004
   842
            if (rating > PEP_rating_unencrypted_for_some)
vb@1004
   843
                rating = PEP_rating_unencrypted_for_some;
vb@311
   844
        }
vb@311
   845
    }
vb@311
   846
vb@1004
   847
    return rating;
vb@311
   848
}
vb@311
   849
vb@311
   850
static PEP_comm_type _get_comm_type(
vb@311
   851
    PEP_SESSION session,
vb@311
   852
    PEP_comm_type max_comm_type,
vb@311
   853
    pEp_identity *ident
vb@311
   854
    )
vb@311
   855
{
vb@311
   856
    PEP_STATUS status = update_identity(session, ident);
vb@311
   857
vb@311
   858
    if (max_comm_type == PEP_ct_compromized)
vb@311
   859
        return PEP_ct_compromized;
Edouard@510
   860
    
Edouard@510
   861
    if (max_comm_type == PEP_ct_mistrusted)
Edouard@510
   862
        return PEP_ct_mistrusted;
vb@311
   863
vb@311
   864
    if (status == PEP_STATUS_OK) {
vb@311
   865
        if (ident->comm_type == PEP_ct_compromized)
vb@311
   866
            return PEP_ct_compromized;
Edouard@510
   867
        else if (ident->comm_type == PEP_ct_mistrusted)
Edouard@510
   868
            return PEP_ct_mistrusted;
vb@311
   869
        else
vb@951
   870
            return _MIN(max_comm_type, ident->comm_type);
vb@311
   871
    }
vb@311
   872
    else {
vb@311
   873
        return PEP_ct_unknown;
vb@311
   874
    }
vb@311
   875
}
vb@311
   876
vb@731
   877
static void free_bl_entry(bloblist_t *bl)
vb@731
   878
{
vb@731
   879
    if (bl) {
vb@731
   880
        free(bl->value);
vb@731
   881
        free(bl->mime_type);
vb@731
   882
        free(bl->filename);
vb@731
   883
        free(bl);
vb@731
   884
    }
vb@731
   885
}
vb@731
   886
vb@731
   887
static bool is_key(const bloblist_t *bl)
vb@731
   888
{
Edouard@728
   889
    return (// workaround for Apple Mail bugs
Edouard@728
   890
            (is_mime_type(bl, "application/x-apple-msg-attachment") &&
Edouard@728
   891
             is_fileending(bl, ".asc")) ||
Edouard@728
   892
            // as binary, by file name
Edouard@728
   893
            ((bl->mime_type == NULL ||
Edouard@728
   894
              is_mime_type(bl, "application/octet-stream")) &&
Edouard@728
   895
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
   896
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))) ||
Edouard@728
   897
            // explicit mime type 
Edouard@728
   898
            is_mime_type(bl, "application/pgp-keys") ||
Edouard@728
   899
            // as text, by file name
Edouard@728
   900
            (is_mime_type(bl, "text/plain") &&
Edouard@728
   901
             (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
Edouard@728
   902
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc")))
Edouard@728
   903
           );
vb@731
   904
}
vb@731
   905
vb@731
   906
static void remove_attached_keys(message *msg)
vb@731
   907
{
vb@731
   908
    if (msg) {
vb@731
   909
        bloblist_t *last = NULL;
vb@731
   910
        for (bloblist_t *bl = msg->attachments; bl && bl->value; ) {
vb@731
   911
            bloblist_t *next = bl->next;
vb@731
   912
vb@731
   913
            if (is_key(bl)) {
vb@731
   914
                if (last) {
vb@731
   915
                    last->next = next;
vb@731
   916
                }
vb@731
   917
                else {
vb@731
   918
                    msg->attachments = next;
vb@731
   919
                }
vb@731
   920
                free_bl_entry(bl);
vb@731
   921
            }
vb@731
   922
            else {
vb@731
   923
                last = bl;
vb@731
   924
            }
vb@731
   925
            bl = next;
vb@731
   926
        }
vb@731
   927
    }
vb@731
   928
}
vb@731
   929
Edouard@734
   930
bool import_attached_keys(
Edouard@728
   931
        PEP_SESSION session, 
Edouard@728
   932
        const message *msg,
Edouard@728
   933
        identity_list **private_idents
Edouard@728
   934
    )
vb@311
   935
{
vb@311
   936
    assert(session);
vb@311
   937
    assert(msg);
krista@853
   938
    
krista@853
   939
    if (session == NULL || msg == NULL)
krista@853
   940
        return false;
vb@311
   941
vb@731
   942
    bool remove = false;
vb@731
   943
vb@311
   944
    bloblist_t *bl;
vb@908
   945
    int i = 0;
vb@908
   946
    for (bl = msg->attachments; i < MAX_KEYS_TO_IMPORT && bl && bl->value;
vb@908
   947
            bl = bl->next, i++) 
Edouard@747
   948
    {
vb@908
   949
        if (bl && bl->value && bl->size && bl->size < MAX_KEY_SIZE
vb@908
   950
                && is_key(bl)) 
Edouard@728
   951
        {
Edouard@728
   952
            import_key(session, bl->value, bl->size, private_idents);
vb@731
   953
            remove = true;
vb@311
   954
        }
vb@311
   955
    }
vb@731
   956
    return remove;
vb@311
   957
}
vb@311
   958
Edouard@694
   959
Edouard@694
   960
PEP_STATUS _attach_key(PEP_SESSION session, const char* fpr, message *msg)
vb@311
   961
{
vb@311
   962
    char *keydata;
vb@311
   963
    size_t size;
vb@311
   964
    bloblist_t *bl;
vb@311
   965
Edouard@694
   966
    PEP_STATUS status = export_key(session, fpr, &keydata, &size);
Edouard@694
   967
    assert(status == PEP_STATUS_OK);
Edouard@694
   968
    if (status != PEP_STATUS_OK)
Edouard@694
   969
        return status;
Edouard@694
   970
    assert(size);
Edouard@694
   971
    
Edouard@694
   972
    bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
Edouard@694
   973
                      "pEpkey.asc");
Edouard@694
   974
    
Edouard@694
   975
    if (msg->attachments == NULL && bl)
Edouard@694
   976
        msg->attachments = bl;
Edouard@694
   977
Edouard@694
   978
    return PEP_STATUS_OK;
Edouard@694
   979
}
Edouard@694
   980
Edouard@694
   981
#define ONE_WEEK (7*24*3600)
Edouard@694
   982
Edouard@694
   983
void attach_own_key(PEP_SESSION session, message *msg)
Edouard@694
   984
{
vb@311
   985
    assert(session);
vb@311
   986
    assert(msg);
krista@853
   987
    
Edouard@558
   988
    if (msg->dir == PEP_dir_incoming)
Edouard@558
   989
        return;
Edouard@558
   990
vb@311
   991
    assert(msg->from && msg->from->fpr);
vb@311
   992
    if (msg->from == NULL || msg->from->fpr == NULL)
vb@311
   993
        return;
vb@311
   994
Edouard@694
   995
    if(_attach_key(session, msg->from->fpr, msg) != PEP_STATUS_OK)
vb@311
   996
        return;
Edouard@694
   997
    
Edouard@694
   998
    char *revoked_fpr = NULL;
Edouard@694
   999
    uint64_t revocation_date = 0;
Edouard@694
  1000
    
Edouard@694
  1001
    if(get_revoked(session, msg->from->fpr,
Edouard@694
  1002
                   &revoked_fpr, &revocation_date) == PEP_STATUS_OK &&
Edouard@694
  1003
       revoked_fpr != NULL)
Edouard@694
  1004
    {
Edouard@694
  1005
        time_t now = time(NULL);
Edouard@694
  1006
        
Edouard@694
  1007
        if (now < (time_t)revocation_date + ONE_WEEK)
Edouard@694
  1008
        {
Edouard@694
  1009
            _attach_key(session, revoked_fpr, msg);
Edouard@694
  1010
        }
Edouard@694
  1011
    }
Edouard@694
  1012
    free(revoked_fpr);
vb@311
  1013
}
vb@311
  1014
vb@311
  1015
PEP_cryptotech determine_encryption_format(message *msg)
vb@311
  1016
{
vb@311
  1017
    assert(msg);
krista@853
  1018
    
vb@311
  1019
    if (is_PGP_message_text(msg->longmsg)) {
vb@311
  1020
        msg->enc_format = PEP_enc_pieces;
vb@311
  1021
        return PEP_crypt_OpenPGP;
vb@311
  1022
    }
vb@311
  1023
    else if (msg->attachments && msg->attachments->next &&
vb@311
  1024
            is_mime_type(msg->attachments, "application/pgp-encrypted") &&
vb@311
  1025
            is_PGP_message_text(msg->attachments->next->value)
vb@311
  1026
        ) {
vb@311
  1027
        msg->enc_format = PEP_enc_PGP_MIME;
vb@311
  1028
        return PEP_crypt_OpenPGP;
vb@311
  1029
    }
vb@311
  1030
    else {
vb@311
  1031
        msg->enc_format = PEP_enc_none;
vb@311
  1032
        return PEP_crypt_none;
vb@311
  1033
    }
vb@311
  1034
}
vb@311
  1035
vb@48
  1036
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
  1037
        PEP_SESSION session,
vb@113
  1038
        message *src,
vb@37
  1039
        stringlist_t * extra,
vb@38
  1040
        message **dst,
vb@939
  1041
        PEP_enc_format enc_format,
vb@939
  1042
        PEP_encrypt_flags_t flags
vb@37
  1043
    )
vb@37
  1044
{
vb@37
  1045
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
  1046
    message * msg = NULL;
vb@63
  1047
    stringlist_t * keys = NULL;
vb@37
  1048
vb@37
  1049
    assert(session);
vb@37
  1050
    assert(src);
vb@37
  1051
    assert(dst);
Edouard@558
  1052
    assert(enc_format != PEP_enc_none);
vb@81
  1053
Edouard@558
  1054
    if (!(session && src && dst && enc_format != PEP_enc_none))
vb@191
  1055
        return PEP_ILLEGAL_VALUE;
vb@191
  1056
Edouard@550
  1057
    if (src->dir == PEP_dir_incoming)
Edouard@550
  1058
        return PEP_ILLEGAL_VALUE;
Edouard@550
  1059
    
vb@259
  1060
    determine_encryption_format(src);
vb@260
  1061
    if (src->enc_format != PEP_enc_none)
vb@260
  1062
        return PEP_ILLEGAL_VALUE;
vb@259
  1063
vb@37
  1064
    *dst = NULL;
vb@67
  1065
vb@236
  1066
    status = myself(session, src->from);
vb@236
  1067
    if (status != PEP_STATUS_OK)
vb@236
  1068
        goto pep_error;
Edouard@558
  1069
vb@80
  1070
    keys = new_stringlist(src->from->fpr);
vb@63
  1071
    if (keys == NULL)
vb@63
  1072
        goto enomem;
vb@37
  1073
vb@39
  1074
    stringlist_t *_k = keys;
vb@39
  1075
vb@39
  1076
    if (extra) {
vb@39
  1077
        _k = stringlist_append(_k, extra);
vb@63
  1078
        if (_k == NULL)
vb@63
  1079
            goto enomem;
vb@37
  1080
    }
vb@39
  1081
vb@299
  1082
    bool dest_keys_found = true;
Edouard@510
  1083
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@299
  1084
vb@37
  1085
    identity_list * _il;
Edouard@545
  1086
    
Edouard@545
  1087
    if ((_il = src->bcc) && _il->ident)
Edouard@545
  1088
    {
Edouard@545
  1089
        // BCC limited support:
Edouard@545
  1090
        //     - App splits mails with BCC in multiple mails.
Edouard@545
  1091
        //     - Each email is encrypted separately
Edouard@545
  1092
        
Edouard@789
  1093
        if(_il->next || (src->to && src->to->ident) || (src->cc && src->cc->ident))
Edouard@545
  1094
        {
Edouard@545
  1095
            // Only one Bcc with no other recipient allowed for now
Edouard@545
  1096
            return PEP_ILLEGAL_VALUE;
Edouard@545
  1097
        }
Edouard@545
  1098
        
vb@299
  1099
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
  1100
        if (_status != PEP_STATUS_OK) {
vb@299
  1101
            status = _status;
vb@63
  1102
            goto pep_error;
vb@299
  1103
        }
Edouard@545
  1104
        
vb@299
  1105
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@39
  1106
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
  1107
            if (_k == NULL)
vb@63
  1108
                goto enomem;
Edouard@510
  1109
            max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@510
  1110
                                           _il->ident);
vb@37
  1111
        }
vb@299
  1112
        else {
vb@299
  1113
            dest_keys_found = false;
vb@37
  1114
            status = PEP_KEY_NOT_FOUND;
Edouard@545
  1115
        }        
vb@299
  1116
    }
Edouard@545
  1117
    else
Edouard@545
  1118
    {
Edouard@545
  1119
        for (_il = src->to; _il && _il->ident; _il = _il->next) {
Edouard@545
  1120
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1121
            if (_status != PEP_STATUS_OK) {
Edouard@545
  1122
                status = _status;
Edouard@545
  1123
                goto pep_error;
Edouard@545
  1124
            }
vb@299
  1125
Edouard@545
  1126
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1127
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1128
                if (_k == NULL)
Edouard@545
  1129
                    goto enomem;
Edouard@545
  1130
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1131
                                               _il->ident);
Edouard@545
  1132
            }
Edouard@545
  1133
            else {
Edouard@545
  1134
                dest_keys_found = false;
Edouard@545
  1135
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1136
            }
vb@299
  1137
        }
vb@299
  1138
Edouard@545
  1139
        for (_il = src->cc; _il && _il->ident; _il = _il->next) {
Edouard@545
  1140
            PEP_STATUS _status = update_identity(session, _il->ident);
Edouard@545
  1141
            if (_status != PEP_STATUS_OK)
Edouard@545
  1142
            {
Edouard@545
  1143
                status = _status;
Edouard@545
  1144
                goto pep_error;
Edouard@545
  1145
            }
Edouard@545
  1146
Edouard@545
  1147
            if (_il->ident->fpr && _il->ident->fpr[0]) {
Edouard@545
  1148
                _k = stringlist_add(_k, _il->ident->fpr);
Edouard@545
  1149
                if (_k == NULL)
Edouard@545
  1150
                    goto enomem;
Edouard@545
  1151
                max_comm_type = _get_comm_type(session, max_comm_type,
Edouard@545
  1152
                                               _il->ident);
Edouard@545
  1153
            }
Edouard@545
  1154
            else {
Edouard@545
  1155
                dest_keys_found = false;
Edouard@545
  1156
                status = PEP_KEY_NOT_FOUND;
Edouard@545
  1157
            }
vb@299
  1158
        }
vb@37
  1159
    }
Edouard@510
  1160
    
Edouard@510
  1161
    if (!dest_keys_found ||
Edouard@510
  1162
        stringlist_length(keys) == 0 ||
Edouard@510
  1163
        _rating(max_comm_type,
Edouard@510
  1164
                PEP_rating_undefined) < PEP_rating_reliable)
Edouard@510
  1165
    {
vb@301
  1166
        free_stringlist(keys);
vb@465
  1167
        if (!session->passive_mode)
vb@465
  1168
            attach_own_key(session, src);
vb@301
  1169
        return PEP_UNENCRYPTED;
vb@301
  1170
    }
vb@301
  1171
    else {
vb@299
  1172
        msg = clone_to_empty_message(src);
vb@299
  1173
        if (msg == NULL)
vb@299
  1174
            goto enomem;
vb@299
  1175
Edouard@431
  1176
        attach_own_key(session, src);
Edouard@428
  1177
vb@81
  1178
        switch (enc_format) {
vb@260
  1179
        case PEP_enc_PGP_MIME:
vb@336
  1180
        case PEP_enc_PEP: // BUG: should be implemented extra
vb@260
  1181
            status = encrypt_PGP_MIME(session, src, keys, msg);
vb@260
  1182
            break;
vb@62
  1183
vb@62
  1184
        case PEP_enc_pieces:
vb@260
  1185
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
vb@38
  1186
            break;
vb@38
  1187
vb@336
  1188
        /* case PEP_enc_PEP:
vb@81
  1189
            // TODO: implement
vb@336
  1190
            NOT_IMPLEMENTED */
vb@81
  1191
vb@38
  1192
        default:
vb@38
  1193
            assert(0);
vb@63
  1194
            status = PEP_ILLEGAL_VALUE;
vb@63
  1195
            goto pep_error;
vb@37
  1196
        }
Edouard@392
  1197
        
Edouard@392
  1198
        if (status == PEP_OUT_OF_MEMORY)
Edouard@392
  1199
            goto enomem;
Edouard@392
  1200
        
vb@480
  1201
        if (status != PEP_STATUS_OK)
Edouard@392
  1202
            goto pep_error;
vb@37
  1203
    }
vb@37
  1204
vb@37
  1205
    free_stringlist(keys);
vb@63
  1206
vb@469
  1207
    if (msg && msg->shortmsg == NULL) {
vb@64
  1208
        msg->shortmsg = strdup("pEp");
vb@469
  1209
        assert(msg->shortmsg);
krista@853
  1210
        if (msg->shortmsg == NULL)
krista@853
  1211
            goto enomem;
vb@469
  1212
    }
vb@64
  1213
vb@311
  1214
    if (msg)
vb@311
  1215
        decorate_message(msg, PEP_rating_undefined, NULL);
vb@311
  1216
vb@63
  1217
    *dst = msg;
vb@299
  1218
    return status;
vb@63
  1219
vb@63
  1220
enomem:
vb@63
  1221
    status = PEP_OUT_OF_MEMORY;
vb@63
  1222
vb@63
  1223
pep_error:
vb@63
  1224
    free_stringlist(keys);
vb@63
  1225
    free_message(msg);
vb@63
  1226
vb@37
  1227
    return status;
vb@37
  1228
}
vb@37
  1229
krista@995
  1230
DYNAMIC_API PEP_STATUS encrypt_message_for_self(
krista@992
  1231
        PEP_SESSION session,
krista@992
  1232
        pEp_identity* target_id,
krista@992
  1233
        message *src,
krista@992
  1234
        message **dst,
krista@992
  1235
        PEP_enc_format enc_format
krista@992
  1236
    )
krista@992
  1237
{
krista@992
  1238
    PEP_STATUS status = PEP_STATUS_OK;
krista@992
  1239
    message * msg = NULL;
krista@992
  1240
    stringlist_t * keys = NULL;
krista@992
  1241
krista@992
  1242
    assert(session);
krista@992
  1243
    assert(src);
krista@992
  1244
    assert(dst);
krista@992
  1245
    assert(enc_format != PEP_enc_none);
krista@992
  1246
krista@992
  1247
    if (!(session && src && dst && enc_format != PEP_enc_none))
krista@992
  1248
        return PEP_ILLEGAL_VALUE;
krista@992
  1249
krista@992
  1250
    if (src->dir == PEP_dir_incoming)
krista@992
  1251
        return PEP_ILLEGAL_VALUE;
krista@992
  1252
    
krista@992
  1253
    determine_encryption_format(src);
krista@992
  1254
    if (src->enc_format != PEP_enc_none)
krista@992
  1255
        return PEP_ILLEGAL_VALUE;
krista@992
  1256
krista@995
  1257
    status = myself(session, target_id);
krista@995
  1258
    if (status != PEP_STATUS_OK)
krista@995
  1259
        goto pep_error;
krista@995
  1260
krista@992
  1261
    *dst = NULL;
krista@992
  1262
krista@992
  1263
    
krista@992
  1264
    PEP_STATUS _status = update_identity(session, target_id);
krista@992
  1265
    if (_status != PEP_STATUS_OK) {
krista@992
  1266
        status = _status;
krista@992
  1267
        goto pep_error;
krista@992
  1268
    }
krista@992
  1269
krista@994
  1270
    char* target_fpr = target_id->fpr;
krista@994
  1271
    if (!target_fpr)
krista@994
  1272
        return PEP_KEY_NOT_FOUND; // FIXME: Error condition
krista@994
  1273
        
krista@994
  1274
    keys = new_stringlist(target_fpr);
krista@992
  1275
krista@994
  1276
    
krista@992
  1277
    msg = clone_to_empty_message(src);
krista@992
  1278
    if (msg == NULL)
krista@992
  1279
        goto enomem;
krista@992
  1280
krista@992
  1281
    switch (enc_format) {
krista@992
  1282
        case PEP_enc_PGP_MIME:
krista@992
  1283
        case PEP_enc_PEP: // BUG: should be implemented extra
krista@992
  1284
            status = encrypt_PGP_MIME(session, src, keys, msg);
krista@992
  1285
            break;
krista@992
  1286
krista@992
  1287
        case PEP_enc_pieces:
krista@992
  1288
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
krista@992
  1289
            break;
krista@992
  1290
krista@992
  1291
        /* case PEP_enc_PEP:
krista@992
  1292
            // TODO: implement
krista@992
  1293
            NOT_IMPLEMENTED */
krista@992
  1294
krista@992
  1295
        default:
krista@992
  1296
            assert(0);
krista@992
  1297
            status = PEP_ILLEGAL_VALUE;
krista@992
  1298
            goto pep_error;
krista@992
  1299
    }
krista@992
  1300
        
krista@992
  1301
    if (status == PEP_OUT_OF_MEMORY)
krista@992
  1302
        goto enomem;
krista@992
  1303
    
krista@992
  1304
    if (status != PEP_STATUS_OK)
krista@992
  1305
        goto pep_error;
krista@992
  1306
krista@994
  1307
     if (msg && msg->shortmsg == NULL) {
krista@994
  1308
         msg->shortmsg = strdup("pEp");
krista@994
  1309
         assert(msg->shortmsg);
krista@994
  1310
         if (msg->shortmsg == NULL)
krista@994
  1311
             goto enomem;
krista@994
  1312
     }
krista@992
  1313
krista@992
  1314
    *dst = msg;
krista@992
  1315
    return status;
krista@992
  1316
krista@992
  1317
enomem:
krista@992
  1318
    status = PEP_OUT_OF_MEMORY;
krista@992
  1319
krista@992
  1320
pep_error:
krista@992
  1321
    free_stringlist(keys);
krista@992
  1322
    free_message(msg);
krista@992
  1323
krista@992
  1324
    return status;
krista@992
  1325
}
krista@992
  1326
vb@781
  1327
static bool is_a_pEpmessage(const message *msg)
vb@781
  1328
{
vb@781
  1329
    for (stringpair_list_t *i = msg->opt_fields; i && i->value ; i=i->next) {
vb@781
  1330
        if (strcasecmp(i->value->key, "X-pEp-Version") == 0)
vb@781
  1331
            return true;
vb@781
  1332
    }
vb@781
  1333
    return false;
vb@781
  1334
}
vb@781
  1335
vb@781
  1336
// update comm_type to pEp_ct_pEp if needed
vb@781
  1337
Edouard@858
  1338
static PEP_STATUS _update_identity_for_incoming_message(
vb@781
  1339
        PEP_SESSION session,
vb@781
  1340
        const message *src
vb@781
  1341
    )
vb@781
  1342
{
Edouard@858
  1343
    PEP_STATUS status;
Edouard@858
  1344
    if (src->from && src->from->address) {
Edouard@858
  1345
        status = update_identity(session, src->from);
krista@1192
  1346
        if (status == PEP_STATUS_OK
Edouard@858
  1347
                && is_a_pEpmessage(src)
vb@781
  1348
                && src->from->comm_type >= PEP_ct_OpenPGP_unconfirmed
vb@781
  1349
                && src->from->comm_type != PEP_ct_pEp_unconfirmed
vb@781
  1350
                && src->from->comm_type != PEP_ct_pEp)
vb@781
  1351
        {
vb@781
  1352
            src->from->comm_type |= PEP_ct_pEp_unconfirmed;
Edouard@858
  1353
            status = update_identity(session, src->from);
vb@781
  1354
        }
Edouard@858
  1355
        return status;
vb@781
  1356
    }
Edouard@858
  1357
    return PEP_ILLEGAL_VALUE;
vb@781
  1358
}
vb@781
  1359
krista@1397
  1360
krista@1397
  1361
PEP_STATUS _get_detached_signature(message* msg, bloblist_t** signature_blob) {
krista@1397
  1362
    bloblist_t* attach_curr = msg->attachments;
krista@1397
  1363
    
krista@1397
  1364
    while (attach_curr) {
krista@1397
  1365
        if (strcasecmp(attach_curr->mime_type, "application/pgp-signature")) {
krista@1397
  1366
            // TODO: deal with filenames. Is that sending-client-specific?
krista@1397
  1367
            // In any event, presume the first one signs the plaintext. Hopefully
krista@1397
  1368
            // that's sufficient?
krista@1397
  1369
            *signature_blob = attach_curr;
krista@1397
  1370
            break;
krista@1397
  1371
        }
krista@1397
  1372
        attach_curr = attach_curr->next;
krista@1397
  1373
    }
krista@1397
  1374
    
krista@1397
  1375
    return PEP_STATUS_OK;
krista@1397
  1376
}
krista@1397
  1377
Edouard@741
  1378
DYNAMIC_API PEP_STATUS _decrypt_message(
vb@37
  1379
        PEP_SESSION session,
vb@113
  1380
        message *src,
vb@241
  1381
        message **dst,
vb@251
  1382
        stringlist_t **keylist,
vb@1004
  1383
        PEP_rating *rating,
Edouard@741
  1384
        PEP_decrypt_flags_t *flags, 
Edouard@741
  1385
        identity_list **private_il
vb@37
  1386
    )
vb@37
  1387
{
vb@37
  1388
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
  1389
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
  1390
    message *msg = NULL;
vb@112
  1391
    char *ctext;
vb@112
  1392
    size_t csize;
vb@269
  1393
    char *ptext = NULL;
vb@112
  1394
    size_t psize;
vb@241
  1395
    stringlist_t *_keylist = NULL;
vb@37
  1396
vb@74
  1397
    assert(session);
vb@74
  1398
    assert(src);
vb@74
  1399
    assert(dst);
vb@241
  1400
    assert(keylist);
vb@1004
  1401
    assert(rating);
Edouard@739
  1402
    assert(flags);
vb@73
  1403
vb@1004
  1404
    if (!(session && src && dst && keylist && rating && flags))
vb@191
  1405
        return PEP_ILLEGAL_VALUE;
vb@191
  1406
Edouard@739
  1407
    *flags = 0;
Edouard@739
  1408
Edouard@734
  1409
    // Private key in unencrypted mail are ignored -> NULL
Edouard@734
  1410
    bool imported_keys = import_attached_keys(session, src, NULL);
Edouard@728
  1411
Edouard@728
  1412
    // Update src->from in case we just imported a key
Edouard@728
  1413
    // we would need to check signature
Edouard@858
  1414
    status = _update_identity_for_incoming_message(session, src);
Edouard@858
  1415
    if(status != PEP_STATUS_OK)
Edouard@858
  1416
        return status;
Edouard@858
  1417
krista@1397
  1418
    // IF longmsg and longmsg_formatted are empty, we MAY have an encrypted body
krista@1397
  1419
    // that's an attachment instead.
krista@1396
  1420
    // Check for encryption stuck in the first 2 attachments instead of the body
krista@1396
  1421
    // (This is currently based on AppleMail and a couple of other things
krista@1397
  1422
    //  which were broken for us - we may find a more general case to deal with)
krista@1396
  1423
    if (!src->longmsg && !src->longmsg_formatted) {
krista@1396
  1424
        bloblist_t* attached_head = src->attachments;
krista@1396
  1425
        if (attached_head && strcasecmp(attached_head->mime_type, "application/pgp-encrypted")) {
krista@1396
  1426
            bloblist_t* enc_att_txt = attached_head->next;
krista@1396
  1427
            if (enc_att_txt && strcasecmp(enc_att_txt->mime_type, "application/octet-stream")) {
krista@1396
  1428
                size_t enc_att_len = enc_att_txt->size;
krista@1396
  1429
                char* newlongmsg = calloc(1, enc_att_len + 1);
krista@1396
  1430
                if (newlongmsg == NULL)
krista@1396
  1431
                    goto enomem;
krista@1396
  1432
                
krista@1396
  1433
                memcpy(newlongmsg, enc_att_txt, enc_att_len);
krista@1396
  1434
                newlongmsg[enc_att_len] = '\0';
krista@1396
  1435
                
krista@1396
  1436
                src->longmsg = newlongmsg;
krista@1396
  1437
                
krista@1397
  1438
                // delete attachments here
krista@1396
  1439
                src->attachments = enc_att_txt->next;
krista@1396
  1440
                consume_bloblist_head(attached_head);
krista@1396
  1441
                consume_bloblist_head(attached_head);
krista@1396
  1442
            }
krista@1396
  1443
        }
krista@1396
  1444
    }
krista@1396
  1445
    
krista@1397
  1446
    // Get detached signature, if any
krista@1397
  1447
    bloblist_t* detached_sig = NULL;
krista@1397
  1448
    char* dsig_text = NULL;
krista@1397
  1449
    size_t dsig_size = 0;
krista@1397
  1450
    status = _get_detached_signature(src, &detached_sig);
krista@1397
  1451
    if (detached_sig) {
krista@1397
  1452
        dsig_text = detached_sig->value;
krista@1397
  1453
        dsig_size = detached_sig->size;
krista@1397
  1454
    }
krista@1397
  1455
    
vb@259
  1456
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
  1457
vb@74
  1458
    *dst = NULL;
vb@251
  1459
    *keylist = NULL;
vb@1004
  1460
    *rating = PEP_rating_undefined;
vb@81
  1461
 
vb@261
  1462
    switch (src->enc_format) {
vb@269
  1463
        case PEP_enc_none:
vb@1004
  1464
            *rating = PEP_rating_unencrypted;
vb@731
  1465
            if (imported_keys)
vb@731
  1466
                remove_attached_keys(src);
edouard@1320
  1467
            if(session->inject_sync_msg){
edouard@1165
  1468
                status = receive_DeviceState_msg(session, src, *rating, *keylist);
edouard@1369
  1469
                if (status == PEP_MESSAGE_CONSUME || 
edouard@1369
  1470
                    status == PEP_MESSAGE_IGNORE) {
vb@1134
  1471
                    free_message(msg);
vb@1134
  1472
                    msg = NULL;
edouard@1369
  1473
                    *flags |= (status == PEP_MESSAGE_IGNORE) ?
edouard@1369
  1474
                                PEP_decrypt_flag_ignore :
edouard@1369
  1475
                                PEP_decrypt_flag_consume;
vb@1134
  1476
                }
vb@1134
  1477
                else if (status != PEP_STATUS_OK) {
vb@1134
  1478
                    return status;
vb@1134
  1479
                }
vb@1099
  1480
            }
vb@301
  1481
            return PEP_UNENCRYPTED;
vb@269
  1482
vb@261
  1483
        case PEP_enc_PGP_MIME:
vb@301
  1484
            ctext = src->attachments->next->value;
vb@269
  1485
            csize = src->attachments->next->size;
vb@261
  1486
            break;
vb@261
  1487
vb@261
  1488
        case PEP_enc_pieces:
vb@261
  1489
            ctext = src->longmsg;
vb@261
  1490
            csize = strlen(ctext);
vb@261
  1491
            break;
vb@261
  1492
vb@261
  1493
        default:
vb@261
  1494
            NOT_IMPLEMENTED
vb@259
  1495
    }
Edouard@431
  1496
    status = cryptotech[crypto].decrypt_and_verify(session, ctext,
krista@1397
  1497
                                                   csize, dsig_text, dsig_size, 
krista@1397
  1498
                                                   &ptext, &psize, &_keylist);
edouard@1195
  1499
    if (status > PEP_CANNOT_DECRYPT_UNKNOWN){
edouard@1271
  1500
        goto pep_error;
edouard@1271
  1501
    }
edouard@1271
  1502
edouard@1273
  1503
    decrypt_status = status;
edouard@1273
  1504
edouard@1271
  1505
    if (status == PEP_DECRYPT_NO_KEY){
edouard@1299
  1506
        PEP_STATUS sync_status = inject_DeviceState_event(session, CannotDecrypt, NULL, NULL);
edouard@1299
  1507
        if (sync_status == PEP_OUT_OF_MEMORY){
edouard@1299
  1508
            status = PEP_OUT_OF_MEMORY;
edouard@1273
  1509
            goto pep_error;
edouard@1299
  1510
        }
edouard@1195
  1511
    }
vb@113
  1512
Edouard@741
  1513
    bool imported_private_key_address = false; 
Edouard@728
  1514
vb@256
  1515
    if (ptext) {
vb@256
  1516
        switch (src->enc_format) {
vb@256
  1517
            case PEP_enc_PGP_MIME:
vb@269
  1518
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  1519
                if (status != PEP_STATUS_OK)
vb@113
  1520
                    goto pep_error;
vb@256
  1521
                break;
vb@256
  1522
vb@256
  1523
            case PEP_enc_pieces:
vb@256
  1524
                msg = clone_to_empty_message(src);
vb@256
  1525
                if (msg == NULL)
vb@113
  1526
                    goto enomem;
vb@113
  1527
Edouard@882
  1528
                msg->longmsg = ptext;
Edouard@882
  1529
                ptext = NULL;
vb@256
  1530
vb@256
  1531
                bloblist_t *_m = msg->attachments;
vb@320
  1532
                if (_m == NULL && src->attachments && src->attachments->value) {
vb@320
  1533
                    msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@320
  1534
                    _m = msg->attachments;
vb@320
  1535
                }
vb@320
  1536
vb@256
  1537
                bloblist_t *_s;
Edouard@754
  1538
                for (_s = src->attachments; _s; _s = _s->next) {
Edouard@754
  1539
                    if (_s->value == NULL && _s->size == 0){
Edouard@754
  1540
                        _m = bloblist_add(_m, NULL, 0, _s->mime_type, _s->filename);
Edouard@754
  1541
                        if (_m == NULL)
Edouard@754
  1542
                            goto enomem;
Edouard@754
  1543
Edouard@754
  1544
                    }
Edouard@754
  1545
                    else if (is_encrypted_attachment(_s)) {
vb@256
  1546
                        stringlist_t *_keylist = NULL;
roker@862
  1547
                        char *attctext  = _s->value;
roker@862
  1548
                        size_t attcsize = _s->size;
Edouard@431
  1549
Edouard@842
  1550
                        free(ptext);
Edouard@842
  1551
                        ptext = NULL;
Edouard@842
  1552
krista@1397
  1553
                        // FIXME: What about attachments with separate sigs???
Edouard@431
  1554
                        status = decrypt_and_verify(session, attctext, attcsize,
krista@1397
  1555
                                                    NULL, 0,
krista@1397
  1556
                                                    &ptext, &psize, &_keylist);
vb@256
  1557
                        free_stringlist(_keylist);
vb@256
  1558
vb@289
  1559
                        if (ptext) {
vb@289
  1560
                            if (is_encrypted_html_attachment(_s)) {
Edouard@882
  1561
                                msg->longmsg_formatted = ptext;
Edouard@882
  1562
                                ptext = NULL;
vb@289
  1563
                            }
vb@289
  1564
                            else {
roker@862
  1565
                                static const char * const mime_type = "application/octet-stream";
roker@864
  1566
                                char * const filename =
vb@289
  1567
                                    without_double_ending(_s->filename);
vb@289
  1568
                                if (filename == NULL)
vb@289
  1569
                                    goto enomem;
vb@289
  1570
Edouard@882
  1571
                                _m = bloblist_add(_m, ptext, psize, mime_type,
vb@289
  1572
                                    filename);
roker@862
  1573
                                free(filename);
vb@290
  1574
                                if (_m == NULL)
vb@289
  1575
                                    goto enomem;
vb@290
  1576
Edouard@882
  1577
                                ptext = NULL;
Edouard@882
  1578
vb@290
  1579
                                if (msg->attachments == NULL)
vb@290
  1580
                                    msg->attachments = _m;
vb@289
  1581
                            }
vb@256
  1582
                        }
vb@256
  1583
                        else {
vb@320
  1584
                            char *copy = malloc(_s->size);
vb@350
  1585
                            assert(copy);
vb@350
  1586
                            if (copy == NULL)
vb@350
  1587
                                goto enomem;
vb@320
  1588
                            memcpy(copy, _s->value, _s->size);
vb@320
  1589
                            _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@256
  1590
                            if (_m == NULL)
vb@256
  1591
                                goto enomem;
vb@256
  1592
                        }
vb@256
  1593
                    }
vb@320
  1594
                    else {
vb@320
  1595
                        char *copy = malloc(_s->size);
vb@350
  1596
                        assert(copy);
vb@350
  1597
                        if (copy == NULL)
vb@350
  1598
                            goto enomem;
vb@320
  1599
                        memcpy(copy, _s->value, _s->size);
vb@320
  1600
                        _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@320
  1601
                        if (_m == NULL)
vb@320
  1602
                            goto enomem;
vb@320
  1603
                    }
vb@256
  1604
                }
vb@256
  1605
vb@256
  1606
                break;
vb@256
  1607
vb@256
  1608
            default:
vb@256
  1609
                // BUG: must implement more
vb@256
  1610
                NOT_IMPLEMENTED
vb@256
  1611
        }
Edouard@431
  1612
        
vb@256
  1613
        switch (src->enc_format) {
vb@256
  1614
            case PEP_enc_PGP_MIME:
vb@256
  1615
            case PEP_enc_pieces:
vb@256
  1616
                status = copy_fields(msg, src);
vb@256
  1617
                if (status != PEP_STATUS_OK)
vb@256
  1618
                    goto pep_error;
vb@256
  1619
vb@280
  1620
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1621
                {
vb@256
  1622
                    char * shortmsg;
vb@256
  1623
                    char * longmsg;
vb@256
  1624
krista@853
  1625
                    int r = separate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1626
                            &longmsg);
vb@256
  1627
                    if (r == -1)
vb@256
  1628
                        goto enomem;
vb@256
  1629
vb@256
  1630
                    free(msg->shortmsg);
vb@256
  1631
                    free(msg->longmsg);
vb@256
  1632
vb@256
  1633
                    msg->shortmsg = shortmsg;
vb@256
  1634
                    msg->longmsg = longmsg;
vb@256
  1635
                }
vb@256
  1636
                else {
vb@256
  1637
                    msg->shortmsg = strdup(src->shortmsg);
vb@469
  1638
                    assert(msg->shortmsg);
vb@256
  1639
                    if (msg->shortmsg == NULL)
vb@256
  1640
                        goto enomem;
vb@256
  1641
                }
vb@256
  1642
                break;
vb@256
  1643
vb@256
  1644
            default:
vb@256
  1645
                // BUG: must implement more
vb@256
  1646
                NOT_IMPLEMENTED
vb@256
  1647
        }
Edouard@728
  1648
       
Edouard@734
  1649
        // check for private key in decrypted message attachement while inporting
Edouard@741
  1650
        identity_list *_private_il = NULL;
Edouard@741
  1651
        imported_keys = import_attached_keys(session, msg, &_private_il);
Edouard@741
  1652
        if (_private_il && 
Edouard@741
  1653
            identity_list_length(_private_il) == 1 &&
Edouard@741
  1654
            _private_il->ident->address)
Edouard@728
  1655
        {
Edouard@741
  1656
            imported_private_key_address = true;
Edouard@728
  1657
        }
Edouard@741
  1658
Edouard@741
  1659
        if(private_il && imported_private_key_address){
Edouard@741
  1660
            *private_il = _private_il;
Edouard@741
  1661
        }else{
Edouard@741
  1662
            free_identity_list(_private_il);
Edouard@741
  1663
        }
Edouard@728
  1664
         
Edouard@694
  1665
        if(decrypt_status == PEP_DECRYPTED){
Edouard@728
  1666
Edouard@728
  1667
            // TODO optimize if import_attached_keys didn't import any key
Edouard@431
  1668
            
Edouard@431
  1669
            // In case message did decrypt, but no valid signature could be found
Edouard@431
  1670
            // then retry decrypt+verify after importing key.
Edouard@728
  1671
Edouard@728
  1672
            // Update msg->from in case we just imported a key
Edouard@728
  1673
            // we would need to check signature
vb@781
  1674
Edouard@861
  1675
            status = _update_identity_for_incoming_message(session, src);
Edouard@858
  1676
            if(status != PEP_STATUS_OK)
Edouard@858
  1677
                goto pep_error;
Edouard@431
  1678
            
Edouard@431
  1679
            char *re_ptext = NULL;
Edouard@431
  1680
            size_t re_psize;
Edouard@441
  1681
            
Edouard@431
  1682
            free_stringlist(_keylist);
Edouard@431
  1683
            _keylist = NULL;
vb@256
  1684
Edouard@431
  1685
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
krista@1397
  1686
                csize, dsig_text, dsig_size, &re_ptext, &re_psize, &_keylist);
Edouard@433
  1687
            
Edouard@882
  1688
            free(re_ptext);
Edouard@433
  1689
            
Edouard@431
  1690
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
Edouard@431
  1691
                goto pep_error;
Edouard@431
  1692
            
Edouard@431
  1693
            decrypt_status = status;
Edouard@431
  1694
        }
Edouard@431
  1695
        
vb@1004
  1696
        *rating = decrypt_rating(decrypt_status);
Edouard@431
  1697
        
vb@1004
  1698
        if (*rating > PEP_rating_mistrust) {
vb@1004
  1699
            PEP_rating kl_rating = PEP_rating_undefined;
Edouard@431
  1700
            
Edouard@431
  1701
            if (_keylist)
vb@1004
  1702
                kl_rating = keylist_rating(session, _keylist);
Edouard@431
  1703
            
vb@1004
  1704
            if (kl_rating <= PEP_rating_mistrust) {
vb@1004
  1705
                *rating = kl_rating;
Edouard@445
  1706
            }
vb@1004
  1707
            else if (*rating >= PEP_rating_reliable &&
vb@1004
  1708
                     kl_rating < PEP_rating_reliable) {
vb@1004
  1709
                *rating = PEP_rating_unreliable;
Edouard@431
  1710
            }
vb@1004
  1711
            else if (*rating >= PEP_rating_reliable &&
vb@1004
  1712
                     kl_rating >= PEP_rating_reliable) {
Edouard@431
  1713
                if (!(src->from && src->from->user_id && src->from->user_id[0])) {
vb@1004
  1714
                    *rating = PEP_rating_unreliable;
Edouard@431
  1715
                }
Edouard@431
  1716
                else {
Edouard@431
  1717
                    char *fpr = _keylist->value;
Edouard@431
  1718
                    pEp_identity *_from = new_identity(src->from->address, fpr,
Edouard@431
  1719
                                                       src->from->user_id, src->from->username);
Edouard@431
  1720
                    if (_from == NULL)
Edouard@431
  1721
                        goto enomem;
edouard@1156
  1722
                    status = get_trust(session, _from);
Edouard@431
  1723
                    if (_from->comm_type != PEP_ct_unknown)
vb@1004
  1724
                        *rating = _rating(_from->comm_type, PEP_rating_undefined);
Edouard@431
  1725
                    free_identity(_from);
edouard@1156
  1726
                    if (status == PEP_CANNOT_FIND_IDENTITY)
edouard@1156
  1727
                       status = PEP_STATUS_OK;
Edouard@431
  1728
                    if (status != PEP_STATUS_OK)
Edouard@431
  1729
                        goto pep_error;
Edouard@431
  1730
                }
Edouard@431
  1731
            }
Edouard@431
  1732
        }
vb@113
  1733
    }
Edouard@745
  1734
    else
Edouard@745
  1735
    {
vb@1004
  1736
        *rating = decrypt_rating(decrypt_status);
Edouard@745
  1737
        goto pep_error;
Edouard@745
  1738
    }
vb@731
  1739
Edouard@728
  1740
    // Case of own key imported from own trusted message
Edouard@728
  1741
    if (// Message have been reliably decrypted 
Edouard@728
  1742
        msg &&
vb@1004
  1743
        *rating >= PEP_rating_trusted &&
Edouard@728
  1744
        imported_private_key_address &&
Edouard@728
  1745
        // to is [own]
Edouard@728
  1746
        msg->to->ident->user_id &&
Edouard@729
  1747
        strcmp(msg->to->ident->user_id, PEP_OWN_USERID) == 0 
Edouard@729
  1748
        )
Edouard@728
  1749
    {
Edouard@738
  1750
        *flags |= PEP_decrypt_flag_own_private_key;
Edouard@728
  1751
    }
vb@113
  1752
vb@731
  1753
    if (msg) {
vb@1004
  1754
        decorate_message(msg, *rating, _keylist);
vb@731
  1755
        if (imported_keys)
vb@731
  1756
            remove_attached_keys(msg);
edouard@1329
  1757
        if (*rating >= PEP_rating_reliable && session->inject_sync_msg) {
edouard@1165
  1758
            status = receive_DeviceState_msg(session, msg, *rating, _keylist);
edouard@1369
  1759
            if (status == PEP_MESSAGE_CONSUME || 
edouard@1369
  1760
                status == PEP_MESSAGE_IGNORE) {
vb@1099
  1761
                free_message(msg);
vb@1099
  1762
                msg = NULL;
edouard@1369
  1763
                *flags |= (status == PEP_MESSAGE_IGNORE) ?
edouard@1369
  1764
                            PEP_decrypt_flag_ignore :
edouard@1369
  1765
                            PEP_decrypt_flag_consume;
edouard@1355
  1766
edouard@1355
  1767
                status = decrypt_status;
vb@1099
  1768
            }
vb@1099
  1769
            else if (status != PEP_STATUS_OK){
vb@1099
  1770
                goto pep_error;
vb@1099
  1771
            }
vb@1099
  1772
        }
vb@731
  1773
    }
vb@235
  1774
vb@74
  1775
    *dst = msg;
vb@241
  1776
    *keylist = _keylist;
vb@241
  1777
vb@953
  1778
    return status;
vb@73
  1779
vb@73
  1780
enomem:
vb@73
  1781
    status = PEP_OUT_OF_MEMORY;
vb@73
  1782
vb@73
  1783
pep_error:
Edouard@882
  1784
    free(ptext);
vb@73
  1785
    free_message(msg);
vb@241
  1786
    free_stringlist(_keylist);
vb@39
  1787
vb@37
  1788
    return status;
vb@37
  1789
}
vb@37
  1790
Edouard@741
  1791
DYNAMIC_API PEP_STATUS decrypt_message(
Edouard@741
  1792
        PEP_SESSION session,
Edouard@741
  1793
        message *src,
Edouard@741
  1794
        message **dst,
Edouard@741
  1795
        stringlist_t **keylist,
vb@1004
  1796
        PEP_rating *rating,
Edouard@741
  1797
        PEP_decrypt_flags_t *flags 
Edouard@741
  1798
    )
Edouard@741
  1799
{
vb@1004
  1800
    return _decrypt_message( session, src, dst, keylist, rating, flags, NULL );
Edouard@741
  1801
}
Edouard@741
  1802
Edouard@728
  1803
DYNAMIC_API PEP_STATUS own_message_private_key_details(
Edouard@728
  1804
        PEP_SESSION session,
Edouard@728
  1805
        message *msg,
Edouard@728
  1806
        pEp_identity **ident 
Edouard@728
  1807
    )
Edouard@728
  1808
{
Edouard@728
  1809
    assert(session);
Edouard@728
  1810
    assert(msg);
Edouard@728
  1811
    assert(ident);
Edouard@728
  1812
Edouard@736
  1813
    if (!(session && msg && ident))
Edouard@728
  1814
        return PEP_ILLEGAL_VALUE;
Edouard@728
  1815
krista@956
  1816
    message *dst = NULL; 
krista@956
  1817
    stringlist_t *keylist = NULL;
vb@1004
  1818
    PEP_rating rating;
Edouard@728
  1819
    PEP_decrypt_flags_t flags; 
Edouard@728
  1820
Edouard@729
  1821
    *ident = NULL;
Edouard@729
  1822
Edouard@740
  1823
    identity_list *private_il = NULL;
vb@1004
  1824
    PEP_STATUS status = _decrypt_message(session, msg,  &dst, &keylist, &rating, &flags, &private_il);
vb@953
  1825
    free_message(dst);
vb@953
  1826
    free_stringlist(keylist);
Edouard@728
  1827
Edouard@729
  1828
    if (status == PEP_STATUS_OK &&
Edouard@741
  1829
        flags & PEP_decrypt_flag_own_private_key &&
Edouard@741
  1830
        private_il)
Edouard@729
  1831
    {
Edouard@740
  1832
        *ident = identity_dup(private_il->ident);
Edouard@729
  1833
    }
Edouard@729
  1834
Edouard@741
  1835
    free_identity_list(private_il);
Edouard@741
  1836
Edouard@728
  1837
    return status;
Edouard@741
  1838
Edouard@728
  1839
}
Edouard@728
  1840
Edouard@858
  1841
static void _max_comm_type_from_identity_list(
Edouard@858
  1842
        identity_list *identities, 
Edouard@858
  1843
        PEP_SESSION session,
Edouard@858
  1844
        PEP_comm_type *max_comm_type,
Edouard@858
  1845
        bool *comm_type_determined
Edouard@858
  1846
    )
Edouard@858
  1847
{
Edouard@858
  1848
    identity_list * il;
Edouard@858
  1849
    for (il = identities; il != NULL; il = il->next)
Edouard@858
  1850
    {
Edouard@858
  1851
        if (il->ident)
Edouard@858
  1852
        {
edouard@1385
  1853
            PEP_STATUS status = update_identity(session, il->ident);
Edouard@858
  1854
            if (status == PEP_STATUS_OK)
Edouard@858
  1855
            {
Edouard@858
  1856
                *max_comm_type = _get_comm_type(session, *max_comm_type,
Edouard@858
  1857
                        il->ident);
Edouard@858
  1858
                *comm_type_determined = true;
Edouard@858
  1859
            }
Edouard@858
  1860
        }
Edouard@858
  1861
    }
Edouard@858
  1862
}
Edouard@858
  1863
vb@1004
  1864
DYNAMIC_API PEP_STATUS outgoing_message_rating(
vb@190
  1865
        PEP_SESSION session,
vb@190
  1866
        message *msg,
vb@1004
  1867
        PEP_rating *rating
vb@190
  1868
    )
vb@190
  1869
{
vb@190
  1870
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  1871
    bool comm_type_determined = false;
vb@190
  1872
vb@190
  1873
    assert(session);
vb@190
  1874
    assert(msg);
vb@251
  1875
    assert(msg->dir == PEP_dir_outgoing);
vb@1004
  1876
    assert(rating);
vb@190
  1877
vb@1004
  1878
    if (!(session && msg && rating))
vb@191
  1879
        return PEP_ILLEGAL_VALUE;
vb@191
  1880
vb@1360
  1881
    if (msg->dir != PEP_dir_outgoing)
vb@251
  1882
        return PEP_ILLEGAL_VALUE;
vb@251
  1883
vb@1004
  1884
    *rating = PEP_rating_undefined;
vb@190
  1885
Edouard@858
  1886
    _max_comm_type_from_identity_list(msg->to, session,
Edouard@858
  1887
                                      &max_comm_type, &comm_type_determined);
vb@190
  1888
Edouard@858
  1889
    _max_comm_type_from_identity_list(msg->cc, session,
Edouard@858
  1890
                                      &max_comm_type, &comm_type_determined);
Edouard@545
  1891
        
Edouard@858
  1892
    _max_comm_type_from_identity_list(msg->bcc, session,
Edouard@858
  1893
                                      &max_comm_type, &comm_type_determined);
vb@190
  1894
vb@190
  1895
    if (comm_type_determined == false)
vb@1004
  1896
        *rating = PEP_rating_undefined;
vb@190
  1897
    else
vb@1004
  1898
        *rating = _MAX(_rating(max_comm_type, PEP_rating_undefined),
vb@486
  1899
                PEP_rating_unencrypted);
vb@190
  1900
vb@190
  1901
    return PEP_STATUS_OK;
vb@190
  1902
}
vb@190
  1903
vb@1004
  1904
DYNAMIC_API PEP_STATUS identity_rating(
vb@239
  1905
        PEP_SESSION session,
vb@239
  1906
        pEp_identity *ident,
vb@1004
  1907
        PEP_rating *rating
vb@239
  1908
    )
vb@239
  1909
{
vb@239
  1910
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  1911
vb@239
  1912
    assert(session);
vb@239
  1913
    assert(ident);
vb@1004
  1914
    assert(rating);
vb@239
  1915
vb@1004
  1916
    if (!(session && ident && rating))
vb@239
  1917
        return PEP_ILLEGAL_VALUE;
vb@239
  1918
vb@239
  1919
    if (ident->me)
edouard@1385
  1920
        status = _myself(session, ident, false);
vb@239
  1921
    else
vb@239
  1922
        status = update_identity(session, ident);
vb@239
  1923
vb@239
  1924
    if (status == PEP_STATUS_OK)
vb@1004
  1925
        *rating = _rating(ident->comm_type, PEP_rating_undefined);
vb@239
  1926
vb@239
  1927
    return status;
vb@239
  1928
}
vb@239
  1929
vb@507
  1930
DYNAMIC_API PEP_STATUS get_binary_path(PEP_cryptotech tech, const char **path)
vb@507
  1931
{
vb@507
  1932
    PEP_STATUS status = PEP_STATUS_OK;
vb@507
  1933
vb@507
  1934
    assert(path);
vb@507
  1935
    if (path == NULL)
vb@507
  1936
        return PEP_ILLEGAL_VALUE;
vb@507
  1937
vb@507
  1938
    if (cryptotech[tech].binary_path == NULL)
vb@507
  1939
        *path = NULL;
vb@507
  1940
    else
vb@507
  1941
        status = cryptotech[tech].binary_path(path);
vb@507
  1942
vb@507
  1943
    return status;
vb@507
  1944
}
vb@507
  1945
vb@1004
  1946
vb@1004
  1947
DYNAMIC_API PEP_color color_from_rating(PEP_rating rating)
vb@1004
  1948
{
krista@1243
  1949
    if (rating == PEP_rating_b0rken || rating == PEP_rating_have_no_key)
vb@1004
  1950
        return PEP_color_no_color;
vb@1004
  1951
vb@1004
  1952
    if (rating < PEP_rating_undefined)
vb@1004
  1953
        return PEP_color_red;
vb@1004
  1954
vb@1004
  1955
    if (rating < PEP_rating_reliable)
vb@1004
  1956
        return PEP_color_no_color;
vb@1004
  1957
vb@1004
  1958
    if (rating < PEP_rating_trusted)
vb@1004
  1959
        return PEP_color_yellow;
vb@1004
  1960
vb@1004
  1961
    if (rating >= PEP_rating_trusted)
vb@1004
  1962
        return PEP_color_green;
vb@1004
  1963
vb@1004
  1964
    // this should never happen
vb@1004
  1965
    assert(false);
vb@1004
  1966
}
vb@1004
  1967
krista@1315
  1968
static bool _is_valid_hex(const char* hexstr) {
krista@1315
  1969
    if (!hexstr)
krista@1315
  1970
        return false;
krista@1315
  1971
    
krista@1315
  1972
    const char* curr = hexstr;
krista@1315
  1973
    char currchar;
krista@1315
  1974
    
krista@1315
  1975
    for (currchar = *curr; currchar != '\0'; currchar = *(++curr)) {
krista@1315
  1976
        if ((currchar >= '0' && currchar <= '9') ||
krista@1315
  1977
            (currchar >= 'a' && currchar <= 'f') ||
krista@1315
  1978
            (currchar >= 'A' && currchar <= 'F')) 
krista@1315
  1979
        {
krista@1315
  1980
            continue;
krista@1315
  1981
        }
krista@1315
  1982
        return false;
krista@1315
  1983
    }
krista@1315
  1984
    return true;        
krista@1315
  1985
}
krista@1315
  1986
krista@1312
  1987
// Returns, in comparison: 1 if fpr1 > fpr2, 0 if equal, -1 if fpr1 < fpr2
krista@1312
  1988
static PEP_STATUS _compare_fprs(const char* fpr1, const char* fpr2, int* comparison) {
krista@1312
  1989
    
krista@1312
  1990
    const int _FULL_FINGERPRINT_LENGTH = 40;
krista@1312
  1991
    const int _ASCII_LOWERCASE_OFFSET = 32;
krista@1312
  1992
krista@1312
  1993
    size_t fpr1_len = strlen(fpr1);
krista@1312
  1994
    size_t fpr2_len = strlen(fpr2);
krista@1312
  1995
    
krista@1312
  1996
    if (fpr1_len != _FULL_FINGERPRINT_LENGTH || fpr2_len != _FULL_FINGERPRINT_LENGTH)
krista@1312
  1997
        return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
krista@1312
  1998
    
krista@1315
  1999
    if (!_is_valid_hex(fpr1) || !_is_valid_hex(fpr2))
krista@1315
  2000
        return PEP_ILLEGAL_VALUE;
krista@1315
  2001
    
krista@1312
  2002
    const char* fpr1_curr = fpr1;
krista@1312
  2003
    const char* fpr2_curr = fpr2;
krista@1312
  2004
    
krista@1312
  2005
    char current;
krista@1312
  2006
krista@1315
  2007
    // Advance past leading zeros.
krista@1313
  2008
    for (current = *fpr1_curr; current != '0' && current != '\0'; current = *(++fpr1_curr), fpr1_len--);
krista@1313
  2009
    for (current = *fpr2_curr; current != '0' && current != '\0'; current = *(++fpr2_curr), fpr2_len--);
krista@1312
  2010
    
krista@1312
  2011
    if (fpr1_len == fpr2_len) {
krista@1312
  2012
        char digit1;
krista@1312
  2013
        char digit2;
krista@1312
  2014
krista@1315
  2015
        while (fpr1_curr && *fpr1_curr != '\0') {
krista@1312
  2016
            digit1 = *fpr1_curr++;
krista@1312
  2017
            digit2 = *fpr2_curr++;
krista@1312
  2018
krista@1312
  2019
            // Adjust for case-insensitive compare
krista@1312
  2020
            if (digit1 >= 'a' && digit1 <= 'f')
krista@1312
  2021
                digit1 -= _ASCII_LOWERCASE_OFFSET;
krista@1312
  2022
            if (digit2 >= 'a' && digit2 <= 'f')
krista@1312
  2023
                digit2 -= _ASCII_LOWERCASE_OFFSET;
krista@1315
  2024
                        
krista@1312
  2025
            // We take advantage of the fact that 'a'-'f' are larger
krista@1312
  2026
            // integer values in the ASCII table than '0'-'9'.
krista@1312
  2027
            // This allows us to compare digits directly.
krista@1312
  2028
            if (digit1 > digit2) {
krista@1312
  2029
                *comparison = 1;
krista@1312
  2030
                return PEP_STATUS_OK;
krista@1312
  2031
            } else if (digit1 < digit2) {
krista@1312
  2032
                *comparison = -1;
krista@1312
  2033
                return PEP_STATUS_OK;
krista@1312
  2034
            }
krista@1312
  2035
            
krista@1312
  2036
            // pointers already advanced above. Keep going.
krista@1312
  2037
        }
krista@1312
  2038
        *comparison = 0;
krista@1312
  2039
        return PEP_STATUS_OK;
krista@1312
  2040
    }
krista@1312
  2041
    else if (fpr1_len > fpr2_len) {
krista@1312
  2042
        *comparison = 1;
krista@1312
  2043
        return PEP_STATUS_OK;
krista@1312
  2044
    }
krista@1312
  2045
    // Otherwise, fpr1_len < fpr2_len
krista@1312
  2046
    *comparison = -1;
krista@1312
  2047
    return PEP_STATUS_OK;
krista@1312
  2048
}
krista@1312
  2049
krista@1307
  2050
DYNAMIC_API PEP_STATUS get_trustwords(
krista@1307
  2051
    PEP_SESSION session, pEp_identity* id1, pEp_identity* id2,
krista@1307
  2052
    const char* lang, char **words, size_t *wsize, bool full
krista@1307
  2053
)
krista@1307
  2054
{
krista@1307
  2055
    assert(session);
krista@1307
  2056
    assert(id1);
krista@1307
  2057
    assert(id2);
krista@1307
  2058
    assert(id1->fpr);
krista@1307
  2059
    assert(id2->fpr);
krista@1307
  2060
    assert(words);
krista@1307
  2061
    assert(wsize);
krista@1307
  2062
    
krista@1307
  2063
    if (!(session && id1 && id2 && words && wsize) ||
krista@1307
  2064
        !(id1->fpr) || (!id2->fpr))
krista@1307
  2065
        return PEP_ILLEGAL_VALUE;
krista@1307
  2066
    
krista@1307
  2067
    const char *source1 = id1->fpr;
krista@1307
  2068
    const char *source2 = id2->fpr;
krista@1307
  2069
    
krista@1307
  2070
    *words = NULL;
krista@1307
  2071
    *wsize = 0;
krista@1307
  2072
krista@1307
  2073
    const size_t SHORT_NUM_TWORDS = 5;
krista@1307
  2074
    
krista@1307
  2075
    // N.B. THIS will have to be changed once we start checking trustword entropy.
krista@1307
  2076
    // For now, full is ALL, and otherwise it's 5-per-id.
krista@1307
  2077
    size_t max_words_per_id = (full ? 0 : SHORT_NUM_TWORDS);
krista@1307
  2078
    
krista@1307
  2079
    char* first_set = NULL;
krista@1307
  2080
    char* second_set = NULL;
krista@1307
  2081
    size_t first_wsize = 0;
krista@1307
  2082
    size_t second_wsize = 0;
krista@1307
  2083
    
krista@1312
  2084
    int fpr_comparison = -255;
krista@1312
  2085
    PEP_STATUS status = _compare_fprs(source1, source2, &fpr_comparison);
krista@1312
  2086
    if (status != PEP_STATUS_OK)
krista@1312
  2087
        return status;
krista@1312
  2088
krista@1307
  2089
    char* _retstr = NULL;
krista@1312
  2090
krista@1312
  2091
    switch (fpr_comparison) {
krista@1312
  2092
        case 1: // source1 > source2
krista@1312
  2093
            status = trustwords(session, source2, lang, &first_set, &first_wsize, max_words_per_id);
krista@1312
  2094
            if (status != PEP_STATUS_OK)
krista@1312
  2095
                goto error_release;
krista@1312
  2096
            status = trustwords(session, source1, lang, &second_set, &second_wsize, max_words_per_id); 
krista@1312
  2097
            if (status != PEP_STATUS_OK)
krista@1312
  2098
                goto error_release;
krista@1312
  2099
            break;
krista@1312
  2100
        case 0: 
krista@1312
  2101
        case -1: // source1 <= source2
krista@1312
  2102
            status = trustwords(session, source1, lang, &first_set, &first_wsize, max_words_per_id);
krista@1312
  2103
            if (status != PEP_STATUS_OK)
krista@1312
  2104
                goto error_release;
krista@1312
  2105
            status = trustwords(session, source2, lang, &second_set, &second_wsize, max_words_per_id); 
krista@1312
  2106
            if (status != PEP_STATUS_OK)
krista@1312
  2107
                goto error_release;
krista@1315
  2108
            break;
krista@1312
  2109
        default:
krista@1312
  2110
            return PEP_UNKNOWN_ERROR; // shouldn't be possible
krista@1312
  2111
    }
krista@1307
  2112
    
krista@1307
  2113
    size_t _wsize = first_wsize + second_wsize;
krista@1307
  2114
    
krista@1315
  2115
    bool needs_space = (first_set[first_wsize - 1] != ' ');
krista@1315
  2116
    
krista@1315
  2117
    if (needs_space)
krista@1315
  2118
        _wsize++;
krista@1315
  2119
    
krista@1307
  2120
    _retstr = calloc(1, _wsize + 1);
krista@1307
  2121
    
krista@1307
  2122
    size_t len = strlcpy(_retstr, first_set, _wsize);
krista@1307
  2123
    if (len >= _wsize) {
krista@1307
  2124
        status = PEP_UNKNOWN_ERROR;
krista@1307
  2125
        goto error_release;
krista@1307
  2126
    }
krista@1315
  2127
    if (needs_space) {
krista@1315
  2128
        strlcat(_retstr, " ", _wsize);
krista@1315
  2129
        if (len >= _wsize) {
krista@1315
  2130
            status = PEP_UNKNOWN_ERROR;
krista@1315
  2131
            goto error_release;
krista@1315
  2132
        }
krista@1315
  2133
    }
krista@1307
  2134
    strlcat(_retstr, second_set, _wsize);
krista@1307
  2135
    if (len >= _wsize){
krista@1307
  2136
        status = PEP_UNKNOWN_ERROR;
krista@1307
  2137
        goto error_release;
krista@1307
  2138
    }
krista@1307
  2139
    
krista@1307
  2140
    *words = _retstr;
krista@1307
  2141
    *wsize = _wsize;
krista@1307
  2142
    status = PEP_STATUS_OK;
krista@1307
  2143
    
krista@1307
  2144
    goto the_end;
krista@1307
  2145
    
krista@1307
  2146
    error_release:
krista@1307
  2147
    free(_retstr);
krista@1307
  2148
    
krista@1307
  2149
    the_end:
krista@1307
  2150
    free(first_set);
krista@1307
  2151
    free(second_set);
krista@1307
  2152
    return status;
vb@1309
  2153
}
vb@1309
  2154
krista@1325
  2155
krista@1325
  2156
DYNAMIC_API PEP_STATUS MIME_decrypt_message(
krista@1325
  2157
    PEP_SESSION session,
krista@1325
  2158
    const char *mimetext,
krista@1325
  2159
    size_t size,
krista@1325
  2160
    char** mime_plaintext,
krista@1325
  2161
    stringlist_t **keylist,
krista@1325
  2162
    PEP_rating *rating,
krista@1325
  2163
    PEP_decrypt_flags_t *flags
krista@1325
  2164
) 
krista@1325
  2165
{
krista@1325
  2166
    PEP_STATUS status = PEP_STATUS_OK;
krista@1325
  2167
    message* tmp_msg = NULL;
krista@1325
  2168
    message* dec_msg = NULL;
krista@1325
  2169
    
krista@1325
  2170
    status = mime_decode_message(mimetext, size, &tmp_msg);
krista@1325
  2171
    if (status != PEP_STATUS_OK)
krista@1325
  2172
        goto pep_error;
krista@1325
  2173
    
krista@1325
  2174
    status = decrypt_message(session,
krista@1325
  2175
                             tmp_msg,
krista@1325
  2176
                             &dec_msg,
krista@1325
  2177
                             keylist,
krista@1325
  2178
                             rating,
krista@1325
  2179
                             flags);
krista@1325
  2180
    if (status != PEP_STATUS_OK)
krista@1325
  2181
        goto pep_error;
krista@1325
  2182
    
krista@1325
  2183
    status = mime_encode_message(dec_msg, false, mime_plaintext);
krista@1325
  2184
    
krista@1325
  2185
pep_error:
krista@1325
  2186
    free_message(tmp_msg);
krista@1325
  2187
    free_message(dec_msg);
krista@1325
  2188
    
krista@1325
  2189
    return status;
krista@1325
  2190
}
krista@1325
  2191
krista@1325
  2192
DYNAMIC_API PEP_STATUS MIME_encrypt_message(
krista@1325
  2193
    PEP_SESSION session,
krista@1325
  2194
    const char *mimetext,
krista@1325
  2195
    size_t size,
krista@1325
  2196
    stringlist_t* extra,
krista@1325
  2197
    char** mime_ciphertext,
krista@1325
  2198
    PEP_enc_format enc_format,
krista@1325
  2199
    PEP_encrypt_flags_t flags
krista@1325
  2200
) 
krista@1325
  2201
{
krista@1325
  2202
    PEP_STATUS status = PEP_STATUS_OK;
krista@1325
  2203
    message* tmp_msg = NULL;
krista@1325
  2204
    message* enc_msg = NULL;
krista@1325
  2205
    
krista@1325
  2206
    status = mime_decode_message(mimetext, size, &tmp_msg);
krista@1325
  2207
    if (status != PEP_STATUS_OK)
krista@1325
  2208
        goto pep_error;
krista@1325
  2209
    
krista@1325
  2210
    // This isn't incoming, though... so we need to reverse the direction
krista@1325
  2211
    tmp_msg->dir = PEP_dir_outgoing;
krista@1325
  2212
    status = encrypt_message(session,
krista@1325
  2213
                             tmp_msg,
krista@1325
  2214
                             extra,
krista@1325
  2215
                             &enc_msg,
krista@1325
  2216
                             enc_format,
krista@1325
  2217
                             flags);
krista@1325
  2218
    if (status != PEP_STATUS_OK)
krista@1325
  2219
        goto pep_error;
krista@1325
  2220
        
krista@1325
  2221
    status = mime_encode_message(enc_msg, false, mime_ciphertext);
krista@1325
  2222
    
krista@1325
  2223
pep_error:
krista@1325
  2224
    free_message(tmp_msg);
krista@1325
  2225
    free_message(enc_msg);
krista@1325
  2226
krista@1325
  2227
    return status;
krista@1325
  2228
krista@1325
  2229
}
krista@1325
  2230
krista@1325
  2231