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