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