src/message_api.c
author Edouard Tisserant
Sat, 21 Nov 2015 18:47:19 +0100
changeset 392 0680d1c13fd3
parent 388 198198860bda
child 407 f4249c362ab1
permissions -rw-r--r--
Added own key attachement, even when encryption format is PEP_enc_PGP_MIME
vb@125
     1
#include "pEp_internal.h"
vb@37
     2
#include "message_api.h"
vb@37
     3
vb@130
     4
#include "platform.h"
vb@220
     5
#include "mime.h"
vb@85
     6
vb@37
     7
#include <assert.h>
vb@37
     8
#include <string.h>
vb@39
     9
#include <stdlib.h>
vb@39
    10
vb@190
    11
#ifndef MIN
vb@190
    12
#define MIN(A, B) ((B) > (A) ? (A) : (B))
vb@190
    13
#endif
vb@300
    14
#ifndef MAX
vb@300
    15
#define MAX(A, B) ((B) > (A) ? (B) : (A))
vb@300
    16
#endif
vb@190
    17
vb@235
    18
vb@235
    19
static bool string_equality(const char *s1, const char *s2)
vb@235
    20
{
vb@235
    21
    if (s1 == NULL || s2 == NULL)
vb@235
    22
        return false;
vb@235
    23
vb@235
    24
    assert(s1 && s2);
vb@235
    25
vb@235
    26
    return strcmp(s1, s2) == 0;
vb@235
    27
}
vb@235
    28
vb@235
    29
static bool is_mime_type(const bloblist_t *bl, const char *mt)
vb@235
    30
{
vb@235
    31
    assert(mt);
vb@235
    32
vb@235
    33
    return bl && string_equality(bl->mime_type, mt);
vb@235
    34
}
vb@235
    35
vb@235
    36
static bool is_fileending(const bloblist_t *bl, const char *fe)
vb@235
    37
{
vb@235
    38
    assert(fe);
vb@235
    39
vb@235
    40
    if (bl == NULL || bl->filename == NULL)
vb@235
    41
        return false;
vb@235
    42
vb@235
    43
    assert(bl && bl->filename);
vb@235
    44
vb@235
    45
    size_t fe_len = strlen(fe);
vb@235
    46
    size_t fn_len = strlen(bl->filename);
vb@235
    47
vb@235
    48
    if (fn_len <= fe_len)
vb@235
    49
        return false;
vb@235
    50
vb@235
    51
    assert(fn_len > fe_len);
vb@235
    52
vb@235
    53
    return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
vb@235
    54
}
vb@235
    55
vb@284
    56
static void add_opt_field(message *msg, const char *name, const char *value)
vb@284
    57
{
vb@284
    58
    assert(msg);
vb@284
    59
vb@284
    60
    if (msg && name && value) {
vb@284
    61
        stringpair_t *pair = new_stringpair(name, value);
vb@284
    62
        if (pair == NULL)
vb@284
    63
            return;
vb@284
    64
vb@284
    65
        stringpair_list_t *field = stringpair_list_add(msg->opt_fields, pair);
vb@284
    66
        if (field == NULL)
vb@284
    67
            return;
vb@284
    68
vb@284
    69
        if (msg->opt_fields == NULL)
vb@284
    70
            msg->opt_fields = field;
vb@284
    71
    }
vb@284
    72
}
vb@284
    73
vb@83
    74
static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
vb@62
    75
{
vb@62
    76
    char * ptext;
vb@81
    77
vb@83
    78
    assert(shortmsg);
vb@83
    79
    assert(strcmp(shortmsg, "pEp") != 0);
vb@62
    80
vb@83
    81
    if (longmsg == NULL)
vb@63
    82
        longmsg = "";
vb@63
    83
vb@83
    84
    ptext = calloc(1, strlen(shortmsg) + strlen(longmsg) + 12);
vb@109
    85
    assert(ptext);
vb@62
    86
    if (ptext == NULL)
vb@62
    87
        return NULL;
vb@62
    88
vb@85
    89
    strcpy(ptext, "Subject: ");
vb@83
    90
    strcat(ptext, shortmsg);
vb@62
    91
    strcat(ptext, "\n\n");
vb@63
    92
    strcat(ptext, longmsg);
vb@62
    93
vb@62
    94
    return ptext;
vb@62
    95
}
vb@44
    96
vb@82
    97
static int seperate_short_and_long(const char *src, char **shortmsg, char **longmsg)
vb@82
    98
{
vb@82
    99
    char *_shortmsg = NULL;
vb@82
   100
    char *_longmsg = NULL;
vb@82
   101
vb@82
   102
    assert(src);
vb@82
   103
    assert(shortmsg);
vb@82
   104
    assert(longmsg);
vb@82
   105
vb@82
   106
    *shortmsg = NULL;
vb@82
   107
    *longmsg = NULL;
vb@82
   108
vb@85
   109
    if (strncasecmp(src, "subject: ", 9) == 0) {
vb@82
   110
        char *line_end = strchr(src, '\n');
vb@311
   111
vb@82
   112
        if (line_end == NULL) {
vb@82
   113
            _shortmsg = strdup(src + 9);
vb@82
   114
            if (_shortmsg == NULL)
vb@82
   115
                goto enomem;
vb@82
   116
            // _longmsg = NULL;
vb@82
   117
        }
vb@82
   118
        else {
vb@82
   119
            size_t n = line_end - src;
vb@166
   120
vb@82
   121
            if (*(line_end - 1) == '\r')
vb@166
   122
                _shortmsg = strndup(src + 9, n - 10);
vb@82
   123
            else
vb@166
   124
                _shortmsg = strndup(src + 9, n - 9);
vb@166
   125
vb@82
   126
            if (_shortmsg == NULL)
vb@82
   127
                goto enomem;
vb@166
   128
vb@166
   129
            while (*(src + n) && (*(src + n) == '\n' || *(src + n) == '\r'))
vb@166
   130
                ++n;
vb@166
   131
vb@166
   132
            if (*(src + n)) {
vb@166
   133
                _longmsg = strdup(src + n);
vb@166
   134
                if (_longmsg == NULL)
vb@166
   135
                    goto enomem;
vb@166
   136
            }
vb@82
   137
        }
vb@82
   138
    }
vb@82
   139
    else {
vb@113
   140
        _shortmsg = strdup("");
vb@82
   141
        if (_shortmsg == NULL)
vb@82
   142
            goto enomem;
vb@82
   143
        _longmsg = strdup(src);
vb@82
   144
        if (_longmsg == NULL)
vb@82
   145
            goto enomem;
vb@82
   146
    }
vb@311
   147
vb@82
   148
    *shortmsg = _shortmsg;
vb@82
   149
    *longmsg = _longmsg;
vb@82
   150
vb@82
   151
    return 0;
vb@82
   152
vb@82
   153
enomem:
vb@82
   154
    free(_shortmsg);
vb@82
   155
    free(_longmsg);
vb@82
   156
vb@82
   157
    return -1;
vb@82
   158
}
vb@82
   159
vb@113
   160
static PEP_STATUS copy_fields(message *dst, const message *src)
vb@113
   161
{
vb@164
   162
    assert(dst);
vb@164
   163
    assert(src);
vb@164
   164
vb@113
   165
    free_timestamp(dst->sent);
vb@113
   166
    dst->sent = NULL;
vb@113
   167
    if (src->sent) {
vb@113
   168
        dst->sent = timestamp_dup(src->sent);
vb@113
   169
        if (dst->sent == NULL)
vb@113
   170
            return PEP_OUT_OF_MEMORY;
vb@113
   171
    }
vb@113
   172
vb@113
   173
    free_timestamp(dst->recv);
vb@113
   174
    dst->recv = NULL;
vb@113
   175
    if (src->recv) {
vb@113
   176
        dst->recv = timestamp_dup(src->recv);
vb@113
   177
        if (dst->recv == NULL)
vb@113
   178
            return PEP_OUT_OF_MEMORY;
vb@113
   179
    }
vb@113
   180
vb@113
   181
    free_identity(dst->from);
vb@113
   182
    dst->from = NULL;
vb@113
   183
    if (src->from) {
vb@113
   184
        dst->from = identity_dup(src->from);
vb@113
   185
        if (dst->from == NULL)
vb@113
   186
            return PEP_OUT_OF_MEMORY;
vb@113
   187
    }
vb@113
   188
vb@113
   189
    free_identity_list(dst->to);
vb@113
   190
    dst->to = NULL;
vb@274
   191
    if (src->to && src->to->ident) {
vb@113
   192
        dst->to = identity_list_dup(src->to);
vb@113
   193
        if (dst->to == NULL)
vb@113
   194
            return PEP_OUT_OF_MEMORY;
vb@113
   195
    }
vb@113
   196
vb@113
   197
    free_identity(dst->recv_by);
vb@113
   198
    dst->recv_by = NULL;
vb@113
   199
    if (src->recv_by) {
vb@113
   200
        dst->recv_by = identity_dup(src->recv_by);
vb@113
   201
        if (dst->recv_by == NULL)
vb@113
   202
            return PEP_OUT_OF_MEMORY;
vb@113
   203
    }
vb@113
   204
vb@113
   205
    free_identity_list(dst->cc);
vb@113
   206
    dst->cc = NULL;
vb@274
   207
    if (src->cc && src->cc->ident) {
vb@113
   208
        dst->cc = identity_list_dup(src->cc);
vb@113
   209
        if (dst->cc == NULL)
vb@113
   210
            return PEP_OUT_OF_MEMORY;
vb@113
   211
    }
vb@113
   212
vb@113
   213
    free_identity_list(dst->bcc);
vb@113
   214
    dst->bcc = NULL;
vb@274
   215
    if (src->bcc && src->bcc->ident) {
vb@113
   216
        dst->bcc = identity_list_dup(src->bcc);
vb@113
   217
        if (dst->bcc == NULL)
vb@113
   218
            return PEP_OUT_OF_MEMORY;
vb@113
   219
    }
vb@113
   220
vb@113
   221
    free_identity_list(dst->reply_to);
vb@113
   222
    dst->reply_to = NULL;
vb@274
   223
    if (src->reply_to && src->reply_to->ident) {
vb@113
   224
        dst->reply_to = identity_list_dup(src->reply_to);
vb@113
   225
        if (dst->reply_to == NULL)
vb@113
   226
            return PEP_OUT_OF_MEMORY;
vb@113
   227
    }
vb@113
   228
vb@113
   229
    free_stringlist(dst->in_reply_to);
vb@113
   230
    dst->in_reply_to = NULL;
vb@274
   231
    if (src->in_reply_to && src->in_reply_to->value) {
vb@113
   232
        dst->in_reply_to = stringlist_dup(src->in_reply_to);
vb@113
   233
        if (dst->in_reply_to == NULL)
vb@113
   234
            return PEP_OUT_OF_MEMORY;
vb@113
   235
    }
vb@113
   236
vb@113
   237
    free_stringlist(dst->references);
vb@113
   238
    dst->references = NULL;
vb@113
   239
    if (src->references) {
vb@113
   240
        dst->references = stringlist_dup(src->references);
vb@113
   241
        if (dst->references == NULL)
vb@113
   242
            return PEP_OUT_OF_MEMORY;
vb@113
   243
    }
vb@113
   244
vb@113
   245
    free_stringlist(dst->keywords);
vb@113
   246
    dst->keywords = NULL;
vb@274
   247
    if (src->keywords && src->keywords->value) {
vb@113
   248
        dst->keywords = stringlist_dup(src->keywords);
vb@113
   249
        if (dst->keywords == NULL)
vb@113
   250
            return PEP_OUT_OF_MEMORY;
vb@113
   251
    }
vb@113
   252
vb@113
   253
    free(dst->comments);
vb@113
   254
    dst->comments = NULL;
vb@113
   255
    if (src->comments) {
vb@113
   256
        dst->comments = strdup(src->comments);
vb@113
   257
        assert(dst->comments);
vb@113
   258
        if (dst->comments == NULL)
vb@113
   259
            return PEP_OUT_OF_MEMORY;
vb@113
   260
    }
vb@113
   261
vb@113
   262
    return PEP_STATUS_OK;
vb@113
   263
}
vb@113
   264
vb@81
   265
static message * clone_to_empty_message(const message * src)
vb@80
   266
{
vb@113
   267
    PEP_STATUS status;
vb@80
   268
    message * msg = NULL;
vb@80
   269
vb@81
   270
    assert(src);
vb@81
   271
vb@113
   272
    msg = calloc(1, sizeof(message));
vb@113
   273
    assert(msg);
vb@80
   274
    if (msg == NULL)
vb@80
   275
        goto enomem;
vb@80
   276
vb@82
   277
    msg->dir = src->dir;
vb@82
   278
vb@113
   279
    status = copy_fields(msg, src);
vb@113
   280
    if (status != PEP_STATUS_OK)
vb@113
   281
        goto enomem;
vb@81
   282
vb@80
   283
    return msg;
vb@80
   284
vb@80
   285
enomem:
vb@113
   286
    free_message(msg);
vb@80
   287
    return NULL;
vb@80
   288
}
vb@80
   289
vb@260
   290
static PEP_STATUS encrypt_PGP_MIME(
vb@311
   291
    PEP_SESSION session,
vb@311
   292
    const message *src,
vb@311
   293
    stringlist_t *keys,
vb@311
   294
    message *dst
vb@260
   295
    )
vb@260
   296
{
vb@260
   297
    PEP_STATUS status = PEP_STATUS_OK;
vb@260
   298
    bool free_ptext = false;
vb@260
   299
    char *ptext;
vb@288
   300
    char *ctext;
vb@288
   301
    char *_ctext = NULL;
vb@260
   302
    char *mimetext = NULL;
vb@260
   303
    size_t csize;
vb@260
   304
    assert(dst->longmsg == NULL);
vb@260
   305
    dst->enc_format = PEP_enc_PGP_MIME;
vb@260
   306
vb@260
   307
    if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@260
   308
        ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@260
   309
        if (ptext == NULL)
vb@260
   310
            goto enomem;
vb@260
   311
        free_ptext = true;
vb@260
   312
    }
vb@260
   313
    else if (src->longmsg) {
vb@260
   314
        ptext = src->longmsg;
vb@260
   315
    }
vb@260
   316
    else {
vb@260
   317
        ptext = "pEp";
vb@260
   318
    }
vb@260
   319
vb@260
   320
    message *_src = calloc(1, sizeof(message));
vb@260
   321
    assert(_src);
vb@260
   322
    if (_src == NULL)
vb@260
   323
        goto enomem;
vb@260
   324
    _src->longmsg = ptext;
vb@260
   325
    _src->longmsg_formatted = src->longmsg_formatted;
vb@260
   326
    _src->attachments = src->attachments;
vb@260
   327
    _src->enc_format = PEP_enc_none;
vb@260
   328
    status = mime_encode_message(_src, true, &mimetext);
vb@260
   329
    assert(status == PEP_STATUS_OK);
Edouard@371
   330
    if (free_ptext){
vb@260
   331
        free(ptext);
Edouard@371
   332
        free_ptext=0;
Edouard@371
   333
    }
vb@260
   334
    free(_src);
vb@260
   335
    assert(mimetext);
vb@260
   336
    if (mimetext == NULL)
vb@260
   337
        goto pep_error;
vb@260
   338
vb@260
   339
    status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
vb@311
   340
        &ctext, &csize);
vb@260
   341
    free(mimetext);
vb@260
   342
    if (ctext == NULL)
vb@260
   343
        goto pep_error;
vb@260
   344
vb@260
   345
    dst->longmsg = strdup("this message was encrypted with p≡p "
vb@311
   346
        "http://pEp-project.org");
vb@260
   347
    if (dst->longmsg == NULL)
vb@260
   348
        goto enomem;
vb@260
   349
vb@260
   350
    char *v = strdup("Version: 1");
vb@260
   351
    if (v == NULL)
vb@260
   352
        goto enomem;
vb@260
   353
vb@260
   354
    bloblist_t *_a = new_bloblist(v, 11, "application/pgp-encrypted", NULL);
vb@260
   355
    if (_a == NULL)
vb@260
   356
        goto enomem;
vb@260
   357
    dst->attachments = _a;
vb@288
   358
vb@288
   359
    _ctext = malloc(csize);
vb@288
   360
    assert(_ctext);
vb@288
   361
    if (_ctext == NULL)
vb@288
   362
        goto enomem;
vb@288
   363
    memcpy(_ctext, ctext, csize);
vb@288
   364
vb@288
   365
    _a = bloblist_add(_a, _ctext, csize, "application/octet-stream",
vb@311
   366
        "msg.asc");
vb@260
   367
    if (_a == NULL)
vb@260
   368
        goto enomem;
vb@260
   369
vb@260
   370
    return PEP_STATUS_OK;
vb@260
   371
vb@260
   372
enomem:
vb@260
   373
    status = PEP_OUT_OF_MEMORY;
vb@260
   374
vb@260
   375
pep_error:
vb@260
   376
    if (free_ptext)
vb@260
   377
        free(ptext);
vb@288
   378
    free(_ctext);
vb@260
   379
    return status;
vb@260
   380
}
vb@260
   381
vb@260
   382
static PEP_STATUS encrypt_PGP_in_pieces(
vb@311
   383
    PEP_SESSION session,
vb@311
   384
    const message *src,
vb@311
   385
    stringlist_t *keys,
vb@311
   386
    message *dst
vb@260
   387
    )
vb@260
   388
{
vb@260
   389
    PEP_STATUS status = PEP_STATUS_OK;
vb@288
   390
    char *ctext;
vb@260
   391
    size_t csize;
vb@260
   392
vb@260
   393
    assert(dst->longmsg == NULL);
vb@260
   394
    assert(dst->attachments == NULL);
vb@260
   395
vb@260
   396
    dst->enc_format = PEP_enc_pieces;
vb@260
   397
vb@320
   398
    if (src->shortmsg && src->shortmsg[0] && strcmp(src->shortmsg, "pEp") != 0) {
vb@260
   399
        char *ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@260
   400
        if (ptext == NULL)
vb@260
   401
            goto enomem;
vb@260
   402
vb@260
   403
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   404
            &csize);
vb@260
   405
        free(ptext);
vb@288
   406
        if (ctext) {
vb@288
   407
            dst->longmsg = strndup(ctext, csize);
vb@288
   408
            assert(dst->longmsg);
vb@288
   409
            if (dst->longmsg == NULL)
vb@288
   410
                goto enomem;
vb@288
   411
        }
vb@288
   412
        else {
vb@260
   413
            goto pep_error;
vb@288
   414
        }
vb@260
   415
    }
vb@320
   416
    else if (src->longmsg && src->longmsg[0]) {
vb@260
   417
        char *ptext = src->longmsg;
vb@260
   418
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   419
            &csize);
vb@288
   420
        if (ctext) {
vb@288
   421
            dst->longmsg = strndup(ctext, csize);
vb@288
   422
            assert(dst->longmsg);
vb@288
   423
            if (dst->longmsg == NULL)
vb@288
   424
                goto enomem;
vb@288
   425
        }
vb@288
   426
        else {
vb@260
   427
            goto pep_error;
vb@288
   428
        }
vb@284
   429
    }
vb@284
   430
    else {
vb@284
   431
        dst->longmsg = strdup("");
vb@284
   432
        if (dst->longmsg == NULL)
vb@284
   433
            goto enomem;
vb@260
   434
    }
vb@260
   435
vb@320
   436
    if (src->longmsg_formatted && src->longmsg_formatted[0]) {
vb@260
   437
        char *ptext = src->longmsg_formatted;
vb@260
   438
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@311
   439
            &csize);
vb@260
   440
        if (ctext) {
vb@288
   441
            char *_ctext = malloc(csize);
vb@288
   442
            assert(_ctext);
vb@288
   443
            if (_ctext == NULL)
vb@288
   444
                goto enomem;
vb@288
   445
            memcpy(_ctext, ctext, csize);
vb@288
   446
vb@288
   447
            bloblist_t *_a = bloblist_add(dst->attachments, _ctext, csize,
vb@311
   448
                "application/octet-stream", "PGPexch.htm.pgp");
vb@284
   449
            if (_a == NULL)
vb@260
   450
                goto enomem;
vb@284
   451
            if (dst->attachments == NULL)
vb@284
   452
                dst->attachments = _a;
vb@260
   453
        }
vb@260
   454
        else {
vb@260
   455
            goto pep_error;
vb@260
   456
        }
vb@260
   457
    }
vb@260
   458
vb@260
   459
    if (src->attachments) {
vb@284
   460
        if (dst->attachments == NULL) {
vb@284
   461
            dst->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@284
   462
            if (dst->attachments == NULL)
vb@284
   463
                goto enomem;
vb@284
   464
        }
vb@260
   465
vb@284
   466
        bloblist_t *_s = src->attachments;
vb@284
   467
        bloblist_t *_d = dst->attachments;
vb@284
   468
vb@301
   469
        for (int n = 0; _s && _s->value; _s = _s->next) {
vb@334
   470
            size_t psize = _s->size;
vb@301
   471
            char *ptext = _s->value;
vb@260
   472
            status = encrypt_and_sign(session, keys, ptext, psize, &ctext,
vb@311
   473
                &csize);
vb@260
   474
            if (ctext) {
vb@285
   475
                char *filename = NULL;
vb@285
   476
vb@285
   477
                if (_s->filename) {
vb@285
   478
                    size_t len = strlen(_s->filename);
vb@285
   479
                    filename = calloc(1, len + 5);
vb@285
   480
                    if (filename == NULL)
vb@285
   481
                        goto enomem;
vb@285
   482
vb@285
   483
                    strcpy(filename, _s->filename);
vb@285
   484
                    strcpy(filename + len, ".pgp");
vb@285
   485
                }
vb@285
   486
                else {
vb@285
   487
                    filename = calloc(1, 20);
vb@285
   488
                    if (filename == NULL)
vb@285
   489
                        goto enomem;
vb@285
   490
vb@285
   491
                    ++n;
vb@285
   492
                    n &= 0xffff;
vb@285
   493
                    snprintf(filename, 20, "Attachment%d.pgp", n);
vb@285
   494
                }
vb@285
   495
vb@288
   496
                char *_ctext = malloc(csize);
vb@288
   497
                assert(_ctext);
vb@288
   498
                if (_ctext == NULL)
vb@288
   499
                    goto enomem;
vb@288
   500
                memcpy(_ctext, ctext, csize);
vb@288
   501
vb@288
   502
                _d = bloblist_add(_d, _ctext, csize, "application/octet-stream",
vb@311
   503
                    filename);
vb@260
   504
                if (_d == NULL)
vb@260
   505
                    goto enomem;
vb@260
   506
            }
vb@260
   507
            else {
vb@260
   508
                goto pep_error;
vb@260
   509
            }
vb@260
   510
        }
vb@260
   511
    }
vb@260
   512
vb@260
   513
    return PEP_STATUS_OK;
vb@260
   514
vb@260
   515
enomem:
vb@260
   516
    status = PEP_OUT_OF_MEMORY;
vb@260
   517
vb@260
   518
pep_error:
vb@260
   519
    return status;
vb@260
   520
}
vb@260
   521
vb@311
   522
static char * keylist_to_string(const stringlist_t *keylist)
vb@311
   523
{
vb@311
   524
    if (keylist) {
vb@311
   525
        size_t size = stringlist_length(keylist);
vb@311
   526
vb@311
   527
        const stringlist_t *_kl;
vb@311
   528
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   529
            size += strlen(_kl->value);
vb@311
   530
        }
vb@311
   531
vb@311
   532
        char *result = calloc(1, size);
vb@311
   533
        if (result == NULL)
vb@311
   534
            return NULL;
vb@311
   535
vb@311
   536
        char *_r = result;
vb@311
   537
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   538
            _r = stpcpy(_r, _kl->value);
vb@311
   539
            if (_kl->next && _kl->next->value)
vb@311
   540
                _r = stpcpy(_r, ",");
vb@311
   541
        }
vb@311
   542
vb@311
   543
        return result;
vb@311
   544
    }
vb@311
   545
    else {
vb@311
   546
        return NULL;
vb@311
   547
    }
vb@311
   548
}
vb@311
   549
vb@311
   550
static const char * color_to_string(PEP_color color)
vb@311
   551
{
vb@311
   552
    switch (color) {
vb@311
   553
    case PEP_rating_cannot_decrypt:
vb@311
   554
        return "cannot_decrypt";
vb@311
   555
    case PEP_rating_have_no_key:
vb@311
   556
        return "have_no_key";
vb@311
   557
    case PEP_rating_unencrypted:
vb@311
   558
        return "unencrypted";
vb@311
   559
    case PEP_rating_unreliable:
vb@311
   560
        return "unreliable";
vb@311
   561
    case PEP_rating_reliable:
vb@311
   562
        return "reliable";
vb@311
   563
    case PEP_rating_trusted:
vb@311
   564
        return "trusted";
vb@311
   565
    case PEP_rating_trusted_and_anonymized:
vb@311
   566
        return "trusted_and_anonymized";
vb@311
   567
    case PEP_rating_fully_anonymous:
vb@311
   568
        return "fully_anonymous";
vb@311
   569
    case PEP_rating_under_attack:
vb@311
   570
        return "unter_attack";
vb@311
   571
    case PEP_rating_b0rken:
vb@311
   572
        return "b0rken";
vb@311
   573
    default:
vb@311
   574
        return "undefined";
vb@311
   575
    }
vb@311
   576
}
vb@311
   577
vb@311
   578
static void decorate_message(
vb@311
   579
    message *msg,
vb@311
   580
    PEP_color color,
vb@311
   581
    stringlist_t *keylist
vb@311
   582
    )
vb@311
   583
{
vb@311
   584
    assert(msg);
vb@311
   585
vb@311
   586
    add_opt_field(msg, "X-pEp-Version", "1.0");
vb@311
   587
    
vb@311
   588
    if (color != PEP_rating_undefined)
vb@311
   589
        add_opt_field(msg, "X-EncStatus", color_to_string(color));
vb@311
   590
vb@311
   591
    if (keylist) {
vb@311
   592
        char *_keylist = keylist_to_string(keylist);
vb@311
   593
        add_opt_field(msg, "X-KeyList", _keylist);
vb@311
   594
        free(_keylist);
vb@311
   595
    }
vb@311
   596
}
vb@311
   597
vb@311
   598
static PEP_color _rating(PEP_comm_type ct)
vb@311
   599
{
vb@311
   600
    if (ct == PEP_ct_unknown)
vb@311
   601
        return PEP_rating_undefined;
vb@311
   602
vb@311
   603
    else if (ct == PEP_ct_compromized)
vb@311
   604
        return PEP_rating_under_attack;
vb@311
   605
vb@311
   606
    else if (ct >= PEP_ct_confirmed_enc_anon)
vb@311
   607
        return PEP_rating_trusted_and_anonymized;
vb@311
   608
vb@311
   609
    else if (ct >= PEP_ct_strong_encryption)
vb@311
   610
        return PEP_rating_trusted;
vb@311
   611
vb@311
   612
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@311
   613
        return PEP_rating_reliable;
vb@311
   614
vb@311
   615
    else if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel)
vb@311
   616
        return PEP_rating_unencrypted;
vb@311
   617
vb@311
   618
    else
vb@311
   619
        return PEP_rating_unreliable;
vb@311
   620
}
vb@311
   621
vb@311
   622
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@311
   623
{
vb@311
   624
    char *ext;
vb@311
   625
vb@311
   626
    assert(blob);
vb@311
   627
vb@311
   628
    if (blob->filename == NULL)
vb@311
   629
        return false;
vb@311
   630
vb@311
   631
    ext = strrchr(blob->filename, '.');
vb@311
   632
    if (ext == NULL)
vb@311
   633
        return false;
vb@311
   634
vb@320
   635
    if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
vb@311
   636
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@311
   637
            strcmp(ext, ".asc") == 0)
vb@311
   638
            return true;
vb@311
   639
    }
vb@320
   640
    else if (strcmp(blob->mime_type, "text/plain") == 0) {
vb@311
   641
        if (strcmp(ext, ".asc") == 0)
vb@311
   642
            return true;
vb@311
   643
    }
vb@311
   644
vb@311
   645
    return false;
vb@311
   646
}
vb@311
   647
vb@311
   648
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@311
   649
{
vb@311
   650
    assert(blob);
vb@311
   651
    assert(blob->filename);
vb@311
   652
vb@311
   653
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@311
   654
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@311
   655
            strcmp(blob->filename + 11, ".asc") == 0)
vb@311
   656
            return true;
vb@311
   657
    }
vb@311
   658
vb@311
   659
    return false;
vb@311
   660
}
vb@311
   661
vb@311
   662
static char * without_double_ending(const char *filename)
vb@311
   663
{
vb@311
   664
    char *ext;
vb@311
   665
vb@311
   666
    assert(filename);
vb@311
   667
vb@311
   668
    ext = strrchr(filename, '.');
vb@311
   669
    if (ext == NULL)
vb@311
   670
        return NULL;
vb@311
   671
vb@311
   672
    return strndup(filename, ext - filename);
vb@311
   673
}
vb@311
   674
vb@311
   675
static PEP_color decrypt_color(PEP_STATUS status)
vb@311
   676
{
vb@311
   677
    switch (status) {
vb@311
   678
    case PEP_UNENCRYPTED:
vb@311
   679
    case PEP_VERIFIED:
vb@311
   680
    case PEP_VERIFY_NO_KEY:
vb@311
   681
    case PEP_VERIFIED_AND_TRUSTED:
vb@311
   682
        return PEP_rating_unencrypted;
vb@311
   683
vb@311
   684
    case PEP_DECRYPTED:
vb@311
   685
        return PEP_rating_unreliable;
vb@311
   686
vb@311
   687
    case PEP_DECRYPTED_AND_VERIFIED:
vb@311
   688
        return PEP_rating_reliable;
vb@311
   689
vb@311
   690
    case PEP_DECRYPT_NO_KEY:
vb@311
   691
        return PEP_rating_have_no_key;
vb@311
   692
vb@311
   693
    case PEP_DECRYPT_WRONG_FORMAT:
vb@311
   694
    case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@311
   695
        return PEP_rating_cannot_decrypt;
vb@311
   696
vb@311
   697
    default:
vb@311
   698
        return PEP_rating_undefined;
vb@311
   699
    }
vb@311
   700
}
vb@311
   701
vb@311
   702
static PEP_color key_color(PEP_SESSION session, const char *fpr)
vb@311
   703
{
vb@311
   704
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@311
   705
vb@311
   706
    assert(session);
vb@311
   707
    assert(fpr);
vb@311
   708
vb@311
   709
    PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
vb@311
   710
    if (status != PEP_STATUS_OK)
vb@311
   711
        return PEP_rating_undefined;
vb@311
   712
vb@311
   713
    return _rating(comm_type);
vb@311
   714
}
vb@311
   715
vb@311
   716
static PEP_color keylist_color(PEP_SESSION session, stringlist_t *keylist)
vb@311
   717
{
vb@311
   718
    PEP_color color = PEP_rating_reliable;
vb@311
   719
vb@311
   720
    assert(keylist && keylist->value);
vb@311
   721
    if (keylist == NULL || keylist->value == NULL)
vb@311
   722
        return PEP_rating_unencrypted;
vb@311
   723
vb@311
   724
    stringlist_t *_kl;
vb@311
   725
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@311
   726
        PEP_comm_type ct;
vb@311
   727
        PEP_STATUS status;
vb@311
   728
vb@311
   729
        color = key_color(session, _kl->value);
vb@311
   730
        if (color == PEP_rating_under_attack)
vb@311
   731
            return PEP_rating_under_attack;
vb@311
   732
vb@311
   733
        if (color >= PEP_rating_reliable) {
vb@311
   734
            status = least_trust(session, _kl->value, &ct);
vb@311
   735
            if (status != PEP_STATUS_OK)
vb@311
   736
                return PEP_rating_undefined;
vb@311
   737
            if (ct == PEP_ct_unknown)
vb@311
   738
                color = PEP_rating_unreliable;
vb@311
   739
            else
vb@311
   740
                color = _rating(ct);
vb@311
   741
        }
vb@311
   742
    }
vb@311
   743
vb@311
   744
    return color;
vb@311
   745
}
vb@311
   746
vb@311
   747
static PEP_comm_type _get_comm_type(
vb@311
   748
    PEP_SESSION session,
vb@311
   749
    PEP_comm_type max_comm_type,
vb@311
   750
    pEp_identity *ident
vb@311
   751
    )
vb@311
   752
{
vb@311
   753
    PEP_STATUS status = update_identity(session, ident);
vb@311
   754
vb@311
   755
    if (max_comm_type == PEP_ct_compromized)
vb@311
   756
        return PEP_ct_compromized;
vb@311
   757
vb@311
   758
    if (status == PEP_STATUS_OK) {
vb@311
   759
        if (ident->comm_type == PEP_ct_compromized)
vb@311
   760
            return PEP_ct_compromized;
vb@311
   761
        else
vb@311
   762
            return MIN(max_comm_type, ident->comm_type);
vb@311
   763
    }
vb@311
   764
    else {
vb@311
   765
        return PEP_ct_unknown;
vb@311
   766
    }
vb@311
   767
}
vb@311
   768
vb@311
   769
void import_attached_keys(PEP_SESSION session, const message *msg)
vb@311
   770
{
vb@311
   771
    assert(session);
vb@311
   772
    assert(msg);
vb@311
   773
vb@311
   774
    bloblist_t *bl;
vb@311
   775
    for (bl = msg->attachments; bl && bl->value; bl = bl->next) {
vb@311
   776
        assert(bl && bl->value && bl->size);
vb@311
   777
vb@377
   778
        // workaround for Apple Mail bugs
vb@377
   779
        if (is_mime_type(bl, "application/x-apple-msg-attachment")) {
vb@377
   780
            if (is_fileending(bl, ".asc")) {
vb@377
   781
                if (strlen(bl->filename) == 14 &&
vb@377
   782
                        bl->filename[0] == '0' && bl->filename[1] == 'x')
vb@377
   783
                    import_key(session, bl->value, bl->size);
vb@377
   784
                else if (strlen(bl->filename) == 12)
vb@377
   785
                    import_key(session, bl->value, bl->size);
vb@377
   786
            }
vb@377
   787
        }
vb@377
   788
        else if (bl->mime_type == NULL ||
vb@311
   789
                    is_mime_type(bl, "application/octet-stream")) {
vb@311
   790
            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
vb@375
   791
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
vb@311
   792
                import_key(session, bl->value, bl->size);
vb@311
   793
        }
vb@311
   794
        else if (is_mime_type(bl, "application/pgp-keys")) {
vb@311
   795
            import_key(session, bl->value, bl->size);
vb@311
   796
        }
vb@311
   797
        else if (is_mime_type(bl, "text/plain")) {
vb@311
   798
            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
vb@311
   799
                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
vb@311
   800
                import_key(session, bl->value, bl->size);
vb@311
   801
        }
vb@311
   802
    }
vb@311
   803
}
vb@311
   804
vb@311
   805
void attach_own_key(PEP_SESSION session, message *msg)
vb@311
   806
{
vb@311
   807
    char *keydata;
vb@311
   808
    size_t size;
vb@311
   809
    bloblist_t *bl;
vb@311
   810
vb@311
   811
    assert(session);
vb@311
   812
    assert(msg);
vb@311
   813
vb@311
   814
    if (msg->dir == PEP_dir_incoming)
vb@311
   815
        return;
vb@311
   816
vb@311
   817
    assert(msg->from && msg->from->fpr);
vb@311
   818
    if (msg->from == NULL || msg->from->fpr == NULL)
vb@311
   819
        return;
vb@311
   820
vb@311
   821
    PEP_STATUS status = export_key(session, msg->from->fpr, &keydata, &size);
vb@311
   822
    assert(status == PEP_STATUS_OK);
vb@311
   823
    if (status != PEP_STATUS_OK)
vb@311
   824
        return;
vb@311
   825
    assert(size);
vb@311
   826
vb@311
   827
    bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
vb@311
   828
            "pEp_key.asc");
vb@311
   829
    if (msg->attachments == NULL && bl)
vb@311
   830
        msg->attachments = bl;
vb@311
   831
}
vb@311
   832
vb@311
   833
PEP_cryptotech determine_encryption_format(message *msg)
vb@311
   834
{
vb@311
   835
    assert(msg);
vb@311
   836
vb@311
   837
    if (is_PGP_message_text(msg->longmsg)) {
vb@311
   838
        msg->enc_format = PEP_enc_pieces;
vb@311
   839
        return PEP_crypt_OpenPGP;
vb@311
   840
    }
vb@311
   841
    else if (msg->attachments && msg->attachments->next &&
vb@311
   842
            is_mime_type(msg->attachments, "application/pgp-encrypted") &&
vb@311
   843
            is_PGP_message_text(msg->attachments->next->value)
vb@311
   844
        ) {
vb@311
   845
        msg->enc_format = PEP_enc_PGP_MIME;
vb@311
   846
        return PEP_crypt_OpenPGP;
vb@311
   847
    }
vb@311
   848
    else {
vb@311
   849
        msg->enc_format = PEP_enc_none;
vb@311
   850
        return PEP_crypt_none;
vb@311
   851
    }
vb@311
   852
}
vb@311
   853
vb@48
   854
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
   855
        PEP_SESSION session,
vb@113
   856
        message *src,
vb@37
   857
        stringlist_t * extra,
vb@38
   858
        message **dst,
vb@81
   859
        PEP_enc_format enc_format
vb@37
   860
    )
vb@37
   861
{
vb@37
   862
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
   863
    message * msg = NULL;
vb@63
   864
    stringlist_t * keys = NULL;
vb@37
   865
vb@37
   866
    assert(session);
vb@37
   867
    assert(src);
vb@37
   868
    assert(dst);
vb@260
   869
    assert(enc_format != PEP_enc_none);
vb@81
   870
vb@260
   871
    if (!(session && src && dst && enc_format != PEP_enc_none))
vb@191
   872
        return PEP_ILLEGAL_VALUE;
vb@191
   873
vb@259
   874
    determine_encryption_format(src);
vb@260
   875
    if (src->enc_format != PEP_enc_none)
vb@260
   876
        return PEP_ILLEGAL_VALUE;
vb@259
   877
vb@37
   878
    *dst = NULL;
vb@67
   879
vb@236
   880
    status = myself(session, src->from);
vb@236
   881
    if (status != PEP_STATUS_OK)
vb@236
   882
        goto pep_error;
vb@236
   883
vb@80
   884
    keys = new_stringlist(src->from->fpr);
vb@63
   885
    if (keys == NULL)
vb@63
   886
        goto enomem;
vb@37
   887
vb@39
   888
    stringlist_t *_k = keys;
vb@39
   889
vb@39
   890
    if (extra) {
vb@39
   891
        _k = stringlist_append(_k, extra);
vb@63
   892
        if (_k == NULL)
vb@63
   893
            goto enomem;
vb@37
   894
    }
vb@39
   895
vb@299
   896
    bool dest_keys_found = true;
vb@299
   897
vb@37
   898
    identity_list * _il;
vb@299
   899
    for (_il = src->to; _il && _il->ident; _il = _il->next) {
vb@299
   900
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
   901
        if (_status != PEP_STATUS_OK) {
vb@299
   902
            status = _status;
vb@63
   903
            goto pep_error;
vb@299
   904
        }
vb@63
   905
vb@299
   906
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@39
   907
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
   908
            if (_k == NULL)
vb@63
   909
                goto enomem;
vb@37
   910
        }
vb@299
   911
        else {
vb@299
   912
            dest_keys_found = false;
vb@37
   913
            status = PEP_KEY_NOT_FOUND;
vb@299
   914
        }
vb@299
   915
    }
vb@299
   916
vb@299
   917
    for (_il = src->cc; _il && _il->ident; _il = _il->next) {
vb@299
   918
        PEP_STATUS _status = update_identity(session, _il->ident);
vb@299
   919
        if (_status != PEP_STATUS_OK)
vb@299
   920
        {
vb@299
   921
            status = _status;
vb@299
   922
            goto pep_error;
vb@299
   923
        }
vb@299
   924
vb@299
   925
        if (_il->ident->fpr && _il->ident->fpr[0]) {
vb@299
   926
            _k = stringlist_add(_k, _il->ident->fpr);
vb@299
   927
            if (_k == NULL)
vb@299
   928
                goto enomem;
vb@299
   929
        }
vb@299
   930
        else {
vb@299
   931
            dest_keys_found = false;
vb@299
   932
            status = PEP_KEY_NOT_FOUND;
vb@299
   933
        }
vb@37
   934
    }
vb@37
   935
vb@301
   936
    if (!dest_keys_found) {
vb@301
   937
        free_stringlist(keys);
vb@301
   938
        attach_own_key(session, src);
vb@301
   939
        return PEP_UNENCRYPTED;
vb@301
   940
    }
vb@301
   941
    else {
vb@299
   942
        msg = clone_to_empty_message(src);
vb@299
   943
        if (msg == NULL)
vb@299
   944
            goto enomem;
vb@299
   945
vb@81
   946
        switch (enc_format) {
vb@260
   947
        case PEP_enc_PGP_MIME:
vb@336
   948
        case PEP_enc_PEP: // BUG: should be implemented extra
vb@260
   949
            status = encrypt_PGP_MIME(session, src, keys, msg);
vb@260
   950
            break;
vb@62
   951
vb@62
   952
        case PEP_enc_pieces:
vb@260
   953
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
vb@38
   954
            break;
vb@38
   955
vb@336
   956
        /* case PEP_enc_PEP:
vb@81
   957
            // TODO: implement
vb@336
   958
            NOT_IMPLEMENTED */
vb@81
   959
vb@38
   960
        default:
vb@38
   961
            assert(0);
vb@63
   962
            status = PEP_ILLEGAL_VALUE;
vb@63
   963
            goto pep_error;
vb@37
   964
        }
Edouard@392
   965
        
Edouard@392
   966
        if (status == PEP_OUT_OF_MEMORY)
Edouard@392
   967
            goto enomem;
Edouard@392
   968
        
Edouard@392
   969
        if (status != PEP_STATUS_OK) {
Edouard@392
   970
            attach_own_key(session, src);
Edouard@392
   971
            goto pep_error;
Edouard@392
   972
        }
Edouard@392
   973
        else {
Edouard@392
   974
            attach_own_key(session, msg);
Edouard@392
   975
        }
vb@37
   976
    }
vb@37
   977
vb@37
   978
    free_stringlist(keys);
vb@63
   979
vb@299
   980
    if (msg && msg->shortmsg == NULL)
vb@64
   981
        msg->shortmsg = strdup("pEp");
vb@64
   982
vb@311
   983
    if (msg)
vb@311
   984
        decorate_message(msg, PEP_rating_undefined, NULL);
vb@311
   985
vb@63
   986
    *dst = msg;
vb@299
   987
    return status;
vb@63
   988
vb@63
   989
enomem:
vb@63
   990
    status = PEP_OUT_OF_MEMORY;
vb@63
   991
vb@63
   992
pep_error:
vb@63
   993
    free_stringlist(keys);
vb@63
   994
    free_message(msg);
vb@63
   995
vb@37
   996
    return status;
vb@37
   997
}
vb@37
   998
vb@48
   999
DYNAMIC_API PEP_STATUS decrypt_message(
vb@37
  1000
        PEP_SESSION session,
vb@113
  1001
        message *src,
vb@241
  1002
        message **dst,
vb@251
  1003
        stringlist_t **keylist,
vb@251
  1004
        PEP_color *color
vb@37
  1005
    )
vb@37
  1006
{
vb@37
  1007
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
  1008
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
  1009
    message *msg = NULL;
vb@112
  1010
    char *ctext;
vb@112
  1011
    size_t csize;
vb@269
  1012
    char *ptext = NULL;
vb@112
  1013
    size_t psize;
vb@241
  1014
    stringlist_t *_keylist = NULL;
vb@37
  1015
vb@74
  1016
    assert(session);
vb@74
  1017
    assert(src);
vb@74
  1018
    assert(dst);
vb@241
  1019
    assert(keylist);
vb@251
  1020
    assert(color);
vb@73
  1021
vb@251
  1022
    if (!(session && src && dst && keylist && color))
vb@191
  1023
        return PEP_ILLEGAL_VALUE;
vb@191
  1024
vb@259
  1025
    import_attached_keys(session, src);
vb@259
  1026
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
  1027
vb@74
  1028
    *dst = NULL;
vb@251
  1029
    *keylist = NULL;
vb@251
  1030
    *color = PEP_rating_undefined;
vb@81
  1031
 
vb@261
  1032
    switch (src->enc_format) {
vb@269
  1033
        case PEP_enc_none:
vb@301
  1034
            *color = PEP_rating_unencrypted;
vb@301
  1035
            return PEP_UNENCRYPTED;
vb@269
  1036
vb@261
  1037
        case PEP_enc_PGP_MIME:
vb@301
  1038
            ctext = src->attachments->next->value;
vb@269
  1039
            csize = src->attachments->next->size;
vb@117
  1040
vb@265
  1041
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
vb@265
  1042
                    csize, &ptext, &psize, &_keylist);
vb@261
  1043
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
vb@261
  1044
                goto pep_error;
vb@261
  1045
            decrypt_status = status;
vb@261
  1046
            break;
vb@261
  1047
vb@261
  1048
        case PEP_enc_pieces:
vb@261
  1049
            ctext = src->longmsg;
vb@261
  1050
            csize = strlen(ctext);
vb@261
  1051
vb@265
  1052
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
vb@265
  1053
                    csize, &ptext, &psize, &_keylist);
vb@261
  1054
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
vb@261
  1055
                goto pep_error;
vb@261
  1056
            decrypt_status = status;
vb@261
  1057
            break;
vb@261
  1058
vb@261
  1059
        default:
vb@261
  1060
            NOT_IMPLEMENTED
vb@259
  1061
    }
vb@113
  1062
vb@256
  1063
    *color = decrypt_color(status);
vb@259
  1064
vb@259
  1065
    if (*color != PEP_rating_under_attack) {
vb@259
  1066
        PEP_color kl_color = PEP_rating_undefined;
vb@259
  1067
vb@259
  1068
        if (_keylist)
vb@259
  1069
            kl_color = keylist_color(session, _keylist);
vb@259
  1070
vb@358
  1071
        if (kl_color == PEP_rating_under_attack) {
vb@251
  1072
            *color = PEP_rating_under_attack;
vb@358
  1073
        }
vb@260
  1074
        else if (*color >= PEP_rating_reliable &&
vb@358
  1075
               kl_color < PEP_rating_reliable) {
vb@259
  1076
            *color = PEP_rating_unreliable;
vb@358
  1077
        }
vb@260
  1078
        else if (*color >= PEP_rating_reliable &&
vb@358
  1079
               kl_color == PEP_rating_reliable) {
vb@358
  1080
            if (!(src->from && src->from->user_id && src->from->user_id[0])) {
vb@358
  1081
                *color = PEP_rating_unreliable;
vb@358
  1082
            }
vb@358
  1083
            else {
vb@358
  1084
                char *fpr = _keylist->value;
vb@358
  1085
                pEp_identity *_from = new_identity(src->from->address, fpr,
vb@358
  1086
                        src->from->user_id, src->from->username);
vb@358
  1087
                if (_from == NULL)
vb@358
  1088
                    goto enomem;
vb@358
  1089
                status = update_identity(session, _from);
vb@358
  1090
                if (_from->comm_type != PEP_ct_unknown)
vb@358
  1091
                    *color = _rating(_from->comm_type);
vb@358
  1092
                free_identity(_from);
vb@358
  1093
                if (status != PEP_STATUS_OK)
vb@358
  1094
                    goto pep_error;
vb@358
  1095
            }
vb@358
  1096
        }
vb@251
  1097
    }
vb@251
  1098
vb@256
  1099
    if (ptext) {
vb@256
  1100
        switch (src->enc_format) {
vb@256
  1101
            case PEP_enc_PGP_MIME:
vb@269
  1102
                status = mime_decode_message(ptext, psize, &msg);
vb@113
  1103
                if (status != PEP_STATUS_OK)
vb@113
  1104
                    goto pep_error;
vb@256
  1105
                break;
vb@256
  1106
vb@256
  1107
            case PEP_enc_pieces:
vb@256
  1108
                msg = clone_to_empty_message(src);
vb@256
  1109
                if (msg == NULL)
vb@113
  1110
                    goto enomem;
vb@113
  1111
vb@256
  1112
                msg->longmsg = strdup(ptext);
vb@256
  1113
                if (msg->longmsg == NULL)
vb@256
  1114
                    goto enomem;
vb@256
  1115
vb@256
  1116
                bloblist_t *_m = msg->attachments;
vb@320
  1117
                if (_m == NULL && src->attachments && src->attachments->value) {
vb@320
  1118
                    msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@320
  1119
                    _m = msg->attachments;
vb@320
  1120
                }
vb@320
  1121
vb@256
  1122
                bloblist_t *_s;
vb@320
  1123
                for (_s = src->attachments; _s && _s->value; _s = _s->next) {
vb@256
  1124
                    if (is_encrypted_attachment(_s)) {
vb@256
  1125
                        stringlist_t *_keylist = NULL;
vb@301
  1126
                        ctext = _s->value;
vb@256
  1127
                        csize = _s->size;
vb@256
  1128
vb@256
  1129
                        status = decrypt_and_verify(session, ctext, csize,
vb@256
  1130
                                &ptext, &psize, &_keylist);
vb@256
  1131
                        free_stringlist(_keylist);
vb@256
  1132
vb@289
  1133
                        if (ptext) {
vb@289
  1134
                            if (is_encrypted_html_attachment(_s)) {
vb@290
  1135
                                msg->longmsg_formatted = strdup(ptext);
vb@290
  1136
                                if (msg->longmsg_formatted == NULL)
vb@290
  1137
                                    goto pep_error;
vb@289
  1138
                            }
vb@289
  1139
                            else {
vb@289
  1140
                                char * mime_type = "application/octet-stream";
vb@289
  1141
                                char * filename =
vb@289
  1142
                                    without_double_ending(_s->filename);
vb@289
  1143
                                if (filename == NULL)
vb@289
  1144
                                    goto enomem;
vb@289
  1145
vb@290
  1146
                                char *_ptext = malloc(psize);
vb@290
  1147
                                assert(_ptext);
vb@290
  1148
                                if (_ptext == NULL)
vb@290
  1149
                                    goto enomem;
vb@290
  1150
                                memcpy(_ptext, ptext, psize);
vb@290
  1151
vb@290
  1152
                                _m = bloblist_add(_m, _ptext, psize, mime_type,
vb@289
  1153
                                    filename);
vb@290
  1154
                                if (_m == NULL)
vb@289
  1155
                                    goto enomem;
vb@290
  1156
vb@290
  1157
                                if (msg->attachments == NULL)
vb@290
  1158
                                    msg->attachments = _m;
vb@289
  1159
                            }
vb@256
  1160
                        }
vb@256
  1161
                        else {
vb@320
  1162
                            char *copy = malloc(_s->size);
vb@350
  1163
                            assert(copy);
vb@350
  1164
                            if (copy == NULL)
vb@350
  1165
                                goto enomem;
vb@320
  1166
                            memcpy(copy, _s->value, _s->size);
vb@320
  1167
                            _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@256
  1168
                            if (_m == NULL)
vb@256
  1169
                                goto enomem;
vb@256
  1170
                        }
vb@256
  1171
                    }
vb@320
  1172
                    else {
vb@320
  1173
                        char *copy = malloc(_s->size);
vb@350
  1174
                        assert(copy);
vb@350
  1175
                        if (copy == NULL)
vb@350
  1176
                            goto enomem;
vb@320
  1177
                        memcpy(copy, _s->value, _s->size);
vb@320
  1178
                        _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
vb@320
  1179
                        if (_m == NULL)
vb@320
  1180
                            goto enomem;
vb@320
  1181
                    }
vb@256
  1182
                }
vb@256
  1183
vb@256
  1184
                break;
vb@256
  1185
vb@256
  1186
            default:
vb@256
  1187
                // BUG: must implement more
vb@256
  1188
                NOT_IMPLEMENTED
vb@256
  1189
        }
vb@256
  1190
vb@256
  1191
        switch (src->enc_format) {
vb@256
  1192
            case PEP_enc_PGP_MIME:
vb@256
  1193
            case PEP_enc_pieces:
vb@256
  1194
                status = copy_fields(msg, src);
vb@256
  1195
                if (status != PEP_STATUS_OK)
vb@256
  1196
                    goto pep_error;
vb@256
  1197
vb@280
  1198
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1199
                {
vb@256
  1200
                    char * shortmsg;
vb@256
  1201
                    char * longmsg;
vb@256
  1202
vb@256
  1203
                    int r = seperate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1204
                            &longmsg);
vb@256
  1205
                    if (r == -1)
vb@256
  1206
                        goto enomem;
vb@256
  1207
vb@256
  1208
                    free(msg->shortmsg);
vb@256
  1209
                    free(msg->longmsg);
vb@256
  1210
vb@256
  1211
                    msg->shortmsg = shortmsg;
vb@256
  1212
                    msg->longmsg = longmsg;
vb@256
  1213
                }
vb@256
  1214
                else {
vb@256
  1215
                    msg->shortmsg = strdup(src->shortmsg);
vb@256
  1216
                    if (msg->shortmsg == NULL)
vb@256
  1217
                        goto enomem;
vb@256
  1218
                }
vb@256
  1219
                break;
vb@256
  1220
vb@256
  1221
            default:
vb@256
  1222
                // BUG: must implement more
vb@256
  1223
                NOT_IMPLEMENTED
vb@256
  1224
        }
vb@256
  1225
vb@256
  1226
        import_attached_keys(session, msg);
vb@113
  1227
    }
vb@113
  1228
vb@283
  1229
    if (msg)
vb@311
  1230
        decorate_message(msg, *color, _keylist);
vb@235
  1231
vb@74
  1232
    *dst = msg;
vb@241
  1233
    *keylist = _keylist;
vb@241
  1234
vb@267
  1235
    return PEP_STATUS_OK;
vb@73
  1236
vb@73
  1237
enomem:
vb@73
  1238
    status = PEP_OUT_OF_MEMORY;
vb@73
  1239
vb@73
  1240
pep_error:
vb@73
  1241
    free_message(msg);
vb@241
  1242
    free_stringlist(_keylist);
vb@39
  1243
vb@37
  1244
    return status;
vb@37
  1245
}
vb@37
  1246
vb@251
  1247
DYNAMIC_API PEP_STATUS outgoing_message_color(
vb@190
  1248
        PEP_SESSION session,
vb@190
  1249
        message *msg,
vb@232
  1250
        PEP_color *color
vb@190
  1251
    )
vb@190
  1252
{
vb@190
  1253
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
  1254
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  1255
    bool comm_type_determined = false;
vb@190
  1256
    identity_list * il;
vb@190
  1257
vb@190
  1258
    assert(session);
vb@190
  1259
    assert(msg);
vb@251
  1260
    assert(msg->from);
vb@251
  1261
    assert(msg->dir == PEP_dir_outgoing);
vb@190
  1262
    assert(color);
vb@190
  1263
vb@191
  1264
    if (!(session && msg && color))
vb@191
  1265
        return PEP_ILLEGAL_VALUE;
vb@191
  1266
vb@251
  1267
    if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
vb@251
  1268
        return PEP_ILLEGAL_VALUE;
vb@251
  1269
vb@237
  1270
    *color = PEP_rating_undefined;
vb@190
  1271
vb@251
  1272
    status = myself(session, msg->from);
vb@251
  1273
    if (status != PEP_STATUS_OK)
vb@251
  1274
        return status;
vb@251
  1275
vb@251
  1276
    for (il = msg->to; il != NULL; il = il->next) {
vb@251
  1277
        if (il->ident) {
vb@294
  1278
            update_identity(session, il->ident);
vb@251
  1279
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1280
                    il->ident);
vb@190
  1281
            comm_type_determined = true;
vb@251
  1282
        }
vb@251
  1283
    }
vb@190
  1284
vb@251
  1285
    for (il = msg->cc; il != NULL; il = il->next) {
vb@251
  1286
        if (il->ident) {
vb@294
  1287
            update_identity(session, il->ident);
vb@251
  1288
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1289
                    il->ident);
vb@251
  1290
            comm_type_determined = true;
vb@251
  1291
        }
vb@190
  1292
    }
vb@190
  1293
vb@190
  1294
    if (comm_type_determined == false)
vb@237
  1295
        *color = PEP_rating_undefined;
vb@190
  1296
    else
vb@300
  1297
        *color = MAX(_rating(max_comm_type), PEP_rating_unencrypted);
vb@190
  1298
vb@190
  1299
    return PEP_STATUS_OK;
vb@190
  1300
}
vb@190
  1301
vb@240
  1302
DYNAMIC_API PEP_STATUS identity_color(
vb@239
  1303
        PEP_SESSION session,
vb@239
  1304
        pEp_identity *ident,
vb@239
  1305
        PEP_color *color
vb@239
  1306
    )
vb@239
  1307
{
vb@239
  1308
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  1309
vb@239
  1310
    assert(session);
vb@239
  1311
    assert(ident);
vb@239
  1312
    assert(color);
vb@239
  1313
vb@239
  1314
    if (!(session && ident && color))
vb@239
  1315
        return PEP_ILLEGAL_VALUE;
vb@239
  1316
vb@239
  1317
    if (ident->me)
vb@239
  1318
        status = myself(session, ident);
vb@239
  1319
    else
vb@239
  1320
        status = update_identity(session, ident);
vb@239
  1321
vb@239
  1322
    if (status == PEP_STATUS_OK)
vb@239
  1323
        *color = _rating(ident->comm_type);
vb@239
  1324
vb@239
  1325
    return status;
vb@239
  1326
}
vb@239
  1327