src/message_api.c
author Volker Birk <vb@pep-project.org>
Tue, 26 May 2015 15:47:33 +0200
changeset 284 b0c524184969
parent 283 a41aaf9c07f7
child 285 2b2db7f21020
permissions -rw-r--r--
PGP Partitioned Encoding Format encryption
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@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@260
   373
    char *ctext = NULL;
vb@260
   374
    char *mimetext = NULL;
vb@260
   375
    size_t csize;
vb@260
   376
    assert(dst->longmsg == NULL);
vb@260
   377
    dst->enc_format = PEP_enc_PGP_MIME;
vb@260
   378
vb@260
   379
    if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@260
   380
        ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@260
   381
        if (ptext == NULL)
vb@260
   382
            goto enomem;
vb@260
   383
        free_ptext = true;
vb@260
   384
    }
vb@260
   385
    else if (src->longmsg) {
vb@260
   386
        ptext = src->longmsg;
vb@260
   387
    }
vb@260
   388
    else {
vb@260
   389
        ptext = "pEp";
vb@260
   390
    }
vb@260
   391
vb@260
   392
    message *_src = calloc(1, sizeof(message));
vb@260
   393
    assert(_src);
vb@260
   394
    if (_src == NULL)
vb@260
   395
        goto enomem;
vb@260
   396
    _src->longmsg = ptext;
vb@260
   397
    _src->longmsg_formatted = src->longmsg_formatted;
vb@260
   398
    _src->attachments = src->attachments;
vb@260
   399
    _src->enc_format = PEP_enc_none;
vb@260
   400
    status = mime_encode_message(_src, true, &mimetext);
vb@260
   401
    assert(status == PEP_STATUS_OK);
vb@260
   402
    if (free_ptext)
vb@260
   403
        free(ptext);
vb@260
   404
    free(_src);
vb@260
   405
    assert(mimetext);
vb@260
   406
    if (mimetext == NULL)
vb@260
   407
        goto pep_error;
vb@260
   408
vb@260
   409
    status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
vb@260
   410
            &ctext, &csize);
vb@260
   411
    free(mimetext);
vb@260
   412
    if (ctext == NULL)
vb@260
   413
        goto pep_error;
vb@260
   414
vb@260
   415
    dst->longmsg = strdup("this message was encrypted with p≡p "
vb@260
   416
            "http://pEp-project.org");
vb@260
   417
    if (dst->longmsg == NULL)
vb@260
   418
        goto enomem;
vb@260
   419
vb@260
   420
    char *v = strdup("Version: 1");
vb@260
   421
    if (v == NULL)
vb@260
   422
        goto enomem;
vb@260
   423
vb@260
   424
    bloblist_t *_a = new_bloblist(v, 11, "application/pgp-encrypted", NULL);
vb@260
   425
    if (_a == NULL)
vb@260
   426
        goto enomem;
vb@260
   427
    dst->attachments = _a;
vb@260
   428
    _a = bloblist_add(_a, ctext, strlen(ctext) + 1, "application/octet-stream",
vb@260
   429
            "msg.asc");
vb@260
   430
    if (_a == NULL)
vb@260
   431
        goto enomem;
vb@260
   432
vb@260
   433
    return PEP_STATUS_OK;
vb@260
   434
vb@260
   435
enomem:
vb@260
   436
    status = PEP_OUT_OF_MEMORY;
vb@260
   437
vb@260
   438
pep_error:
vb@260
   439
    if (free_ptext)
vb@260
   440
        free(ptext);
vb@260
   441
    free(ctext);
vb@260
   442
    return status;
vb@260
   443
}
vb@260
   444
vb@260
   445
static PEP_STATUS encrypt_PGP_in_pieces(
vb@260
   446
        PEP_SESSION session,
vb@260
   447
        const message *src,
vb@260
   448
        stringlist_t *keys,
vb@260
   449
        message *dst
vb@260
   450
    )
vb@260
   451
{
vb@260
   452
    PEP_STATUS status = PEP_STATUS_OK;
vb@260
   453
    char *ctext = NULL;
vb@260
   454
    size_t csize;
vb@260
   455
vb@260
   456
    assert(dst->longmsg == NULL);
vb@260
   457
    assert(dst->attachments == NULL);
vb@260
   458
vb@260
   459
    dst->enc_format = PEP_enc_pieces;
vb@260
   460
vb@260
   461
    if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@260
   462
        char *ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@260
   463
        if (ptext == NULL)
vb@260
   464
            goto enomem;
vb@260
   465
vb@260
   466
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@260
   467
                &csize);
vb@260
   468
        free(ptext);
vb@284
   469
        if (ctext)
vb@284
   470
            dst->longmsg = ctext;
vb@284
   471
        else
vb@260
   472
            goto pep_error;
vb@260
   473
    }
vb@260
   474
    else if (src->longmsg) {
vb@260
   475
        char *ptext = src->longmsg;
vb@260
   476
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@260
   477
                &csize);
vb@284
   478
        if (ctext)
vb@284
   479
            dst->longmsg = ctext;
vb@284
   480
        else
vb@260
   481
            goto pep_error;
vb@284
   482
    }
vb@284
   483
    else {
vb@284
   484
        dst->longmsg = strdup("");
vb@284
   485
        if (dst->longmsg == NULL)
vb@284
   486
            goto enomem;
vb@260
   487
    }
vb@260
   488
vb@260
   489
    if (src->longmsg_formatted) {
vb@260
   490
        char *ptext = src->longmsg_formatted;
vb@260
   491
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
vb@260
   492
                &csize);
vb@260
   493
        if (ctext) {
vb@284
   494
            bloblist_t *_a = bloblist_add(dst->attachments, ctext, csize,
vb@284
   495
                    "application/octet-stream", "PGPexch.htm.pgp");
vb@284
   496
            if (_a == NULL)
vb@260
   497
                goto enomem;
vb@284
   498
            if (dst->attachments == NULL)
vb@284
   499
                dst->attachments = _a;
vb@284
   500
vb@284
   501
            add_opt_field(dst, "X-Content-PGP-Universal-Saved-Content-Type",
vb@284
   502
                    "text/html; charset=US-ASCII");
vb@260
   503
        }
vb@260
   504
        else {
vb@260
   505
            goto pep_error;
vb@260
   506
        }
vb@260
   507
    }
vb@260
   508
vb@260
   509
    if (src->attachments) {
vb@284
   510
        if (dst->attachments == NULL) {
vb@284
   511
            dst->attachments = new_bloblist(NULL, 0, NULL, NULL);
vb@284
   512
            if (dst->attachments == NULL)
vb@284
   513
                goto enomem;
vb@284
   514
        }
vb@260
   515
vb@284
   516
        bloblist_t *_s = src->attachments;
vb@284
   517
        bloblist_t *_d = dst->attachments;
vb@284
   518
vb@284
   519
        for (int n = 0; _s && _s->data; _s = _s->next) {
vb@260
   520
            int psize = _s->size;
vb@260
   521
            char *ptext = _s->data;
vb@260
   522
            status = encrypt_and_sign(session, keys, ptext, psize, &ctext,
vb@260
   523
                    &csize);
vb@260
   524
            if (ctext) {
vb@284
   525
                char *filename = calloc(1, 20);
vb@284
   526
                ++n;
vb@284
   527
                n &= 0xffff;
vb@284
   528
                snprintf(filename, 20, "Attachment%d.pgp", n);
vb@284
   529
                _d = bloblist_add(_d, ctext, csize, "application/octet-stream",
vb@284
   530
                        filename);
vb@260
   531
                if (_d == NULL)
vb@260
   532
                    goto enomem;
vb@260
   533
            }
vb@260
   534
            else {
vb@260
   535
                goto pep_error;
vb@260
   536
            }
vb@260
   537
        }
vb@260
   538
    }
vb@260
   539
vb@260
   540
    return PEP_STATUS_OK;
vb@260
   541
vb@260
   542
enomem:
vb@260
   543
    status = PEP_OUT_OF_MEMORY;
vb@260
   544
vb@260
   545
pep_error:
vb@260
   546
    return status;
vb@260
   547
}
vb@260
   548
vb@48
   549
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
   550
        PEP_SESSION session,
vb@113
   551
        message *src,
vb@37
   552
        stringlist_t * extra,
vb@38
   553
        message **dst,
vb@81
   554
        PEP_enc_format enc_format
vb@37
   555
    )
vb@37
   556
{
vb@37
   557
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
   558
    message * msg = NULL;
vb@63
   559
    stringlist_t * keys = NULL;
vb@37
   560
vb@37
   561
    assert(session);
vb@37
   562
    assert(src);
vb@37
   563
    assert(dst);
vb@260
   564
    assert(enc_format != PEP_enc_none);
vb@81
   565
vb@260
   566
    if (!(session && src && dst && enc_format != PEP_enc_none))
vb@191
   567
        return PEP_ILLEGAL_VALUE;
vb@191
   568
vb@259
   569
    determine_encryption_format(src);
vb@260
   570
    if (src->enc_format != PEP_enc_none)
vb@260
   571
        return PEP_ILLEGAL_VALUE;
vb@259
   572
vb@37
   573
    *dst = NULL;
vb@67
   574
vb@236
   575
    status = myself(session, src->from);
vb@236
   576
    if (status != PEP_STATUS_OK)
vb@236
   577
        goto pep_error;
vb@236
   578
vb@81
   579
    msg = clone_to_empty_message(src);
vb@80
   580
    if (msg == NULL)
vb@63
   581
        goto enomem;
vb@40
   582
vb@80
   583
    keys = new_stringlist(src->from->fpr);
vb@63
   584
    if (keys == NULL)
vb@63
   585
        goto enomem;
vb@37
   586
vb@39
   587
    stringlist_t *_k = keys;
vb@39
   588
vb@39
   589
    if (extra) {
vb@39
   590
        _k = stringlist_append(_k, extra);
vb@63
   591
        if (_k == NULL)
vb@63
   592
            goto enomem;
vb@37
   593
    }
vb@39
   594
vb@39
   595
    bool dest_keys_found = false;
vb@37
   596
    identity_list * _il;
vb@80
   597
    for (_il = msg->to; _il && _il->ident; _il = _il->next) {
vb@63
   598
        PEP_STATUS status = update_identity(session, _il->ident);
vb@63
   599
        if (status != PEP_STATUS_OK)
vb@63
   600
            goto pep_error;
vb@63
   601
vb@37
   602
        if (_il->ident->fpr) {
vb@39
   603
            dest_keys_found = true;
vb@39
   604
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
   605
            if (_k == NULL)
vb@63
   606
                goto enomem;
vb@37
   607
        }
vb@37
   608
        else
vb@37
   609
            status = PEP_KEY_NOT_FOUND;
vb@37
   610
    }
vb@37
   611
vb@39
   612
    if (dest_keys_found) {
vb@37
   613
        char *ctext = NULL;
vb@37
   614
        size_t csize = 0;
vb@37
   615
vb@81
   616
        switch (enc_format) {
vb@260
   617
        case PEP_enc_PGP_MIME:
vb@260
   618
            status = encrypt_PGP_MIME(session, src, keys, msg);
vb@260
   619
            if (status != PEP_STATUS_OK)
vb@63
   620
                goto pep_error;
vb@260
   621
            break;
vb@62
   622
vb@62
   623
        case PEP_enc_pieces:
vb@260
   624
            status = encrypt_PGP_in_pieces(session, src, keys, msg);
vb@260
   625
            if (status != PEP_STATUS_OK)
vb@260
   626
                goto pep_error;
vb@260
   627
            attach_own_key(session, msg);
vb@38
   628
            break;
vb@38
   629
vb@81
   630
        case PEP_enc_PEP:
vb@81
   631
            // TODO: implement
vb@81
   632
            NOT_IMPLEMENTED
vb@81
   633
vb@38
   634
        default:
vb@38
   635
            assert(0);
vb@63
   636
            status = PEP_ILLEGAL_VALUE;
vb@63
   637
            goto pep_error;
vb@37
   638
        }
vb@37
   639
    }
vb@260
   640
    else {
vb@260
   641
        attach_own_key(session, msg);
vb@260
   642
    }
vb@37
   643
vb@37
   644
    free_stringlist(keys);
vb@63
   645
vb@64
   646
    if (msg->shortmsg == NULL)
vb@64
   647
        msg->shortmsg = strdup("pEp");
vb@64
   648
vb@63
   649
    *dst = msg;
vb@63
   650
    return PEP_STATUS_OK;
vb@63
   651
vb@63
   652
enomem:
vb@63
   653
    status = PEP_OUT_OF_MEMORY;
vb@63
   654
vb@63
   655
pep_error:
vb@63
   656
    free_stringlist(keys);
vb@63
   657
    free_message(msg);
vb@63
   658
vb@37
   659
    return status;
vb@37
   660
}
vb@37
   661
vb@113
   662
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@113
   663
{
vb@113
   664
    char *ext;
vb@113
   665
 
vb@113
   666
    assert(blob);
vb@113
   667
vb@113
   668
    if (blob->filename == NULL)
vb@113
   669
        return false;
vb@113
   670
vb@113
   671
    ext = strrchr(blob->filename, '.');
vb@113
   672
    if (ext == NULL)
vb@113
   673
        return false;
vb@113
   674
vb@113
   675
    if (strcmp(blob->mime_type, "application/octet-stream")) {
vb@113
   676
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@113
   677
                strcmp(ext, ".asc") == 0)
vb@113
   678
            return true;
vb@113
   679
    }
vb@164
   680
    else if (strcmp(blob->mime_type, "text/plain")) {
vb@113
   681
        if (strcmp(ext, ".asc") == 0)
vb@113
   682
            return true;
vb@113
   683
    }
vb@113
   684
vb@113
   685
    return false;
vb@113
   686
}
vb@113
   687
vb@113
   688
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@113
   689
{
vb@113
   690
    assert(blob);
vb@113
   691
    assert(blob->filename);
vb@113
   692
vb@113
   693
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@113
   694
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@113
   695
                strcmp(blob->filename + 11, ".asc") == 0)
vb@113
   696
            return true;
vb@113
   697
    }
vb@113
   698
vb@113
   699
    return false;
vb@113
   700
}
vb@113
   701
vb@159
   702
static char * without_double_ending(const char *filename)
vb@113
   703
{
vb@113
   704
    char *ext;
vb@113
   705
vb@113
   706
    assert(filename);
vb@113
   707
vb@113
   708
    ext = strrchr(filename, '.');
vb@113
   709
    if (ext == NULL)
vb@113
   710
        return NULL;
vb@113
   711
vb@113
   712
    return strndup(filename, ext - filename);
vb@113
   713
}
vb@113
   714
vb@251
   715
static PEP_color decrypt_color(PEP_STATUS status)
vb@251
   716
{
vb@251
   717
    switch (status) {
vb@251
   718
        case PEP_UNENCRYPTED:
vb@251
   719
        case PEP_VERIFIED:
vb@251
   720
        case PEP_VERIFY_NO_KEY:
vb@251
   721
        case PEP_VERIFIED_AND_TRUSTED:
vb@251
   722
            return PEP_rating_unencrypted;
vb@251
   723
vb@251
   724
        case PEP_DECRYPTED:
vb@251
   725
            return PEP_rating_unreliable;
vb@251
   726
vb@251
   727
        case PEP_DECRYPTED_AND_VERIFIED:
vb@251
   728
            return PEP_rating_reliable;
vb@251
   729
vb@267
   730
        case PEP_DECRYPT_NO_KEY:
vb@267
   731
            return PEP_rating_have_no_key;
vb@267
   732
vb@251
   733
        case PEP_DECRYPT_WRONG_FORMAT:
vb@251
   734
        case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@251
   735
            return PEP_rating_cannot_decrypt;
vb@251
   736
vb@251
   737
        default:
vb@251
   738
            return PEP_rating_undefined;
vb@251
   739
    }
vb@251
   740
}
vb@251
   741
vb@251
   742
static PEP_color _rating(PEP_comm_type ct)
vb@251
   743
{
vb@251
   744
    if (ct == PEP_ct_unknown)
vb@251
   745
        return PEP_rating_undefined;
vb@251
   746
vb@251
   747
    else if (ct == PEP_ct_compromized)
vb@251
   748
        return PEP_rating_under_attack;
vb@251
   749
vb@251
   750
    else if (ct >= PEP_ct_confirmed_enc_anon)
vb@251
   751
        return PEP_rating_trusted_and_anonymized;
vb@251
   752
vb@251
   753
    else if (ct >= PEP_ct_strong_encryption)
vb@251
   754
        return PEP_rating_trusted;
vb@251
   755
vb@251
   756
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@251
   757
        return PEP_rating_reliable;
vb@251
   758
    
vb@251
   759
    else if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel)
vb@251
   760
        return PEP_rating_unencrypted;
vb@251
   761
vb@251
   762
    else
vb@251
   763
        return PEP_rating_unreliable;
vb@251
   764
}
vb@251
   765
vb@251
   766
static PEP_color key_color(PEP_SESSION session, const char *fpr) {
vb@251
   767
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@251
   768
vb@251
   769
    assert(session);
vb@251
   770
    assert(fpr);
vb@251
   771
vb@251
   772
    PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
vb@251
   773
    if (status != PEP_STATUS_OK)
vb@251
   774
        return PEP_rating_undefined;
vb@251
   775
vb@251
   776
    return _rating(comm_type);
vb@251
   777
}
vb@251
   778
vb@251
   779
static PEP_color keylist_color(PEP_SESSION session, stringlist_t *keylist)
vb@251
   780
{
vb@251
   781
    PEP_color color = PEP_rating_reliable;
vb@251
   782
vb@251
   783
    assert(keylist && keylist->value);
vb@251
   784
    if (keylist == NULL || keylist->value == NULL)
vb@251
   785
        return PEP_rating_unencrypted;
vb@251
   786
vb@251
   787
    stringlist_t *_kl;
vb@251
   788
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@251
   789
        PEP_comm_type ct;
vb@251
   790
        PEP_STATUS status;
vb@251
   791
vb@260
   792
        color = key_color(session, _kl->value);
vb@260
   793
        if (color == PEP_rating_under_attack)
vb@251
   794
            return PEP_rating_under_attack;
vb@251
   795
vb@260
   796
        if (color >= PEP_rating_reliable) {
vb@260
   797
            status = least_trust(session, _kl->value, &ct);
vb@260
   798
            if (status != PEP_STATUS_OK)
vb@260
   799
                return PEP_rating_undefined;
vb@260
   800
            if (ct == PEP_ct_unknown)
vb@260
   801
                color = PEP_rating_unreliable;
vb@260
   802
            else
vb@260
   803
                color = _rating(ct);
vb@260
   804
        }
vb@251
   805
    }
vb@251
   806
vb@251
   807
    return color;
vb@251
   808
}
vb@251
   809
vb@281
   810
static char * keylist_to_string(const stringlist_t *keylist)
vb@281
   811
{
vb@281
   812
    if (keylist) {
vb@281
   813
        size_t size = stringlist_length(keylist);
vb@281
   814
vb@281
   815
        const stringlist_t *_kl;
vb@281
   816
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@281
   817
            size += strlen(_kl->value);
vb@281
   818
        }
vb@281
   819
vb@281
   820
        char *result = calloc(1, size);
vb@281
   821
        if (result == NULL)
vb@281
   822
            return NULL;
vb@281
   823
vb@281
   824
        char *_r = result;
vb@281
   825
        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@281
   826
            _r = stpcpy(_r, _kl->value);
vb@281
   827
            if (_kl->next && _kl->next->value)
vb@281
   828
                _r = stpcpy(_r, ",");
vb@281
   829
        }
vb@281
   830
vb@281
   831
        return result;
vb@281
   832
    }
vb@281
   833
    else {
vb@281
   834
        return NULL;
vb@281
   835
    }
vb@281
   836
}
vb@281
   837
vb@281
   838
static const char * color_to_string(PEP_color color)
vb@281
   839
{
vb@281
   840
    switch (color) {
vb@281
   841
        case PEP_rating_cannot_decrypt:
vb@281
   842
            return "cannot_decrypt";
vb@281
   843
        case PEP_rating_have_no_key:
vb@281
   844
            return "have_no_key";
vb@281
   845
        case PEP_rating_unencrypted:
vb@281
   846
            return "unencrypted";
vb@281
   847
        case PEP_rating_unreliable:
vb@281
   848
            return "unreliable";
vb@281
   849
        case PEP_rating_reliable:
vb@281
   850
            return "reliable";
vb@281
   851
        case PEP_rating_trusted:
vb@281
   852
            return "trusted";
vb@281
   853
        case PEP_rating_trusted_and_anonymized:
vb@281
   854
            return "trusted_and_anonymized";
vb@281
   855
        case PEP_rating_fully_anonymous:
vb@281
   856
            return "fully_anonymous";
vb@281
   857
        case PEP_rating_under_attack:
vb@281
   858
            return "unter_attack";
vb@281
   859
        case PEP_rating_b0rken:
vb@281
   860
            return "b0rken";
vb@281
   861
        default:
vb@281
   862
            return "undefined";
vb@281
   863
    }
vb@281
   864
}
vb@281
   865
vb@281
   866
static void decorate_message(
vb@281
   867
        message *msg,
vb@281
   868
        stringlist_t *keylist,
vb@281
   869
        PEP_color color
vb@281
   870
    )
vb@281
   871
{
vb@281
   872
    assert(msg);
vb@281
   873
vb@281
   874
    add_opt_field(msg, "X-pEp-Version", "1.0");
vb@281
   875
    add_opt_field(msg, "X-EncStatus", color_to_string(color));
vb@281
   876
vb@281
   877
    char *_keylist = keylist_to_string(keylist);
vb@281
   878
    add_opt_field(msg, "X-KeyList", _keylist);
vb@281
   879
    free(_keylist);
vb@281
   880
}
vb@281
   881
vb@48
   882
DYNAMIC_API PEP_STATUS decrypt_message(
vb@37
   883
        PEP_SESSION session,
vb@113
   884
        message *src,
vb@241
   885
        message **dst,
vb@251
   886
        stringlist_t **keylist,
vb@251
   887
        PEP_color *color
vb@37
   888
    )
vb@37
   889
{
vb@37
   890
    PEP_STATUS status = PEP_STATUS_OK;
vb@261
   891
    PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
vb@73
   892
    message *msg = NULL;
vb@112
   893
    char *ctext;
vb@112
   894
    size_t csize;
vb@269
   895
    char *ptext = NULL;
vb@112
   896
    size_t psize;
vb@241
   897
    stringlist_t *_keylist = NULL;
vb@37
   898
vb@74
   899
    assert(session);
vb@74
   900
    assert(src);
vb@74
   901
    assert(dst);
vb@241
   902
    assert(keylist);
vb@251
   903
    assert(color);
vb@73
   904
vb@251
   905
    if (!(session && src && dst && keylist && color))
vb@191
   906
        return PEP_ILLEGAL_VALUE;
vb@191
   907
vb@259
   908
    import_attached_keys(session, src);
vb@259
   909
    PEP_cryptotech crypto = determine_encryption_format(src);
vb@259
   910
vb@74
   911
    *dst = NULL;
vb@251
   912
    *keylist = NULL;
vb@251
   913
    *color = PEP_rating_undefined;
vb@81
   914
 
vb@261
   915
    switch (src->enc_format) {
vb@269
   916
        case PEP_enc_none:
vb@269
   917
            status = PEP_UNENCRYPTED;
vb@269
   918
            break;
vb@269
   919
vb@261
   920
        case PEP_enc_PGP_MIME:
vb@261
   921
            ctext = src->attachments->next->data;
vb@269
   922
            csize = src->attachments->next->size;
vb@117
   923
vb@265
   924
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
vb@265
   925
                    csize, &ptext, &psize, &_keylist);
vb@261
   926
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
vb@261
   927
                goto pep_error;
vb@261
   928
            decrypt_status = status;
vb@261
   929
            break;
vb@261
   930
vb@261
   931
        case PEP_enc_pieces:
vb@261
   932
            ctext = src->longmsg;
vb@261
   933
            csize = strlen(ctext);
vb@261
   934
vb@265
   935
            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
vb@265
   936
                    csize, &ptext, &psize, &_keylist);
vb@261
   937
            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
vb@261
   938
                goto pep_error;
vb@261
   939
            decrypt_status = status;
vb@261
   940
            break;
vb@261
   941
vb@261
   942
        default:
vb@261
   943
            NOT_IMPLEMENTED
vb@259
   944
    }
vb@113
   945
vb@256
   946
    *color = decrypt_color(status);
vb@259
   947
vb@259
   948
    if (*color != PEP_rating_under_attack) {
vb@259
   949
        PEP_color kl_color = PEP_rating_undefined;
vb@259
   950
vb@259
   951
        if (_keylist)
vb@259
   952
            kl_color = keylist_color(session, _keylist);
vb@259
   953
vb@259
   954
        if (kl_color == PEP_rating_under_attack)
vb@251
   955
            *color = PEP_rating_under_attack;
vb@259
   956
vb@260
   957
        else if (*color >= PEP_rating_reliable &&
vb@259
   958
                kl_color < PEP_rating_reliable)
vb@259
   959
            *color = PEP_rating_unreliable;
vb@259
   960
vb@260
   961
        else if (*color >= PEP_rating_reliable &&
vb@260
   962
                kl_color >= PEP_rating_trusted)
vb@260
   963
            *color = kl_color;
vb@251
   964
    }
vb@251
   965
vb@256
   966
    if (ptext) {
vb@256
   967
        switch (src->enc_format) {
vb@256
   968
            case PEP_enc_PGP_MIME:
vb@269
   969
                status = mime_decode_message(ptext, psize, &msg);
vb@113
   970
                if (status != PEP_STATUS_OK)
vb@113
   971
                    goto pep_error;
vb@256
   972
                break;
vb@256
   973
vb@256
   974
            case PEP_enc_pieces:
vb@256
   975
                msg = clone_to_empty_message(src);
vb@256
   976
                if (msg == NULL)
vb@113
   977
                    goto enomem;
vb@113
   978
vb@256
   979
                msg->longmsg = strdup(ptext);
vb@256
   980
                if (msg->longmsg == NULL)
vb@256
   981
                    goto enomem;
vb@256
   982
vb@256
   983
                bloblist_t *_m = msg->attachments;
vb@256
   984
                bloblist_t *_s;
vb@256
   985
                for (_s = src->attachments; _s; _s = _s->next) {
vb@256
   986
                    if (is_encrypted_attachment(_s)) {
vb@256
   987
                        stringlist_t *_keylist = NULL;
vb@256
   988
                        ctext = _s->data;
vb@256
   989
                        csize = _s->size;
vb@256
   990
vb@256
   991
                        status = decrypt_and_verify(session, ctext, csize,
vb@256
   992
                                &ptext, &psize, &_keylist);
vb@256
   993
                        if (ptext == NULL)
vb@256
   994
                            goto pep_error;
vb@256
   995
                        free_stringlist(_keylist);
vb@256
   996
vb@256
   997
                        if (is_encrypted_html_attachment(_s)) {
vb@256
   998
                            msg->longmsg_formatted = strdup(ptext);
vb@256
   999
                            if (msg->longmsg_formatted == NULL)
vb@256
  1000
                                goto pep_error;
vb@256
  1001
                        }
vb@256
  1002
                        else {
vb@256
  1003
                            char * mime_type = "application/octet-stream";
vb@256
  1004
                            char * filename =
vb@256
  1005
                                    without_double_ending(_s->filename);
vb@256
  1006
                            if (filename == NULL)
vb@256
  1007
                                goto enomem;
vb@256
  1008
vb@256
  1009
                            _m = bloblist_add(_m, ptext, psize, mime_type,
vb@256
  1010
                                    filename);
vb@256
  1011
                            if (_m == NULL)
vb@256
  1012
                                goto enomem;
vb@256
  1013
vb@256
  1014
                           if (msg->attachments == NULL)
vb@256
  1015
                                msg->attachments = _m;
vb@256
  1016
                        }
vb@256
  1017
                    }
vb@256
  1018
                }
vb@256
  1019
vb@256
  1020
                break;
vb@256
  1021
vb@256
  1022
            default:
vb@256
  1023
                // BUG: must implement more
vb@256
  1024
                NOT_IMPLEMENTED
vb@256
  1025
        }
vb@256
  1026
vb@256
  1027
        switch (src->enc_format) {
vb@256
  1028
            case PEP_enc_PGP_MIME:
vb@256
  1029
            case PEP_enc_pieces:
vb@256
  1030
                status = copy_fields(msg, src);
vb@256
  1031
                if (status != PEP_STATUS_OK)
vb@256
  1032
                    goto pep_error;
vb@256
  1033
vb@280
  1034
                if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
vb@256
  1035
                {
vb@256
  1036
                    char * shortmsg;
vb@256
  1037
                    char * longmsg;
vb@256
  1038
vb@256
  1039
                    int r = seperate_short_and_long(msg->longmsg, &shortmsg,
vb@256
  1040
                            &longmsg);
vb@256
  1041
                    if (r == -1)
vb@256
  1042
                        goto enomem;
vb@256
  1043
vb@256
  1044
                    free(msg->shortmsg);
vb@256
  1045
                    free(msg->longmsg);
vb@256
  1046
vb@256
  1047
                    msg->shortmsg = shortmsg;
vb@256
  1048
                    msg->longmsg = longmsg;
vb@256
  1049
                }
vb@256
  1050
                else {
vb@256
  1051
                    msg->shortmsg = strdup(src->shortmsg);
vb@256
  1052
                    if (msg->shortmsg == NULL)
vb@256
  1053
                        goto enomem;
vb@256
  1054
                }
vb@256
  1055
                break;
vb@256
  1056
vb@256
  1057
            default:
vb@256
  1058
                // BUG: must implement more
vb@256
  1059
                NOT_IMPLEMENTED
vb@256
  1060
        }
vb@256
  1061
vb@256
  1062
        import_attached_keys(session, msg);
vb@113
  1063
    }
vb@113
  1064
vb@283
  1065
    if (msg)
vb@283
  1066
        decorate_message(msg, _keylist, *color);
vb@235
  1067
vb@74
  1068
    *dst = msg;
vb@241
  1069
    *keylist = _keylist;
vb@241
  1070
vb@267
  1071
    return PEP_STATUS_OK;
vb@73
  1072
vb@73
  1073
enomem:
vb@73
  1074
    status = PEP_OUT_OF_MEMORY;
vb@73
  1075
vb@73
  1076
pep_error:
vb@73
  1077
    free_message(msg);
vb@241
  1078
    free_stringlist(_keylist);
vb@39
  1079
vb@37
  1080
    return status;
vb@37
  1081
}
vb@37
  1082
vb@190
  1083
static PEP_comm_type _get_comm_type(
vb@190
  1084
        PEP_SESSION session,
vb@190
  1085
        PEP_comm_type max_comm_type,
vb@190
  1086
        pEp_identity *ident
vb@190
  1087
    )
vb@190
  1088
{
vb@190
  1089
    PEP_STATUS status = update_identity(session, ident);
vb@190
  1090
vb@190
  1091
    if (max_comm_type == PEP_ct_compromized)
vb@190
  1092
        return PEP_ct_compromized;
vb@190
  1093
vb@190
  1094
    if (status == PEP_STATUS_OK) {
vb@190
  1095
        if (ident->comm_type == PEP_ct_compromized)
vb@190
  1096
            return PEP_ct_compromized;
vb@190
  1097
        else
vb@190
  1098
            return MIN(max_comm_type, ident->comm_type);
vb@190
  1099
    }
vb@190
  1100
    else {
vb@190
  1101
        return PEP_ct_unknown;
vb@190
  1102
    }
vb@190
  1103
}
vb@190
  1104
vb@251
  1105
DYNAMIC_API PEP_STATUS outgoing_message_color(
vb@190
  1106
        PEP_SESSION session,
vb@190
  1107
        message *msg,
vb@232
  1108
        PEP_color *color
vb@190
  1109
    )
vb@190
  1110
{
vb@190
  1111
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
  1112
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  1113
    bool comm_type_determined = false;
vb@190
  1114
    identity_list * il;
vb@190
  1115
vb@190
  1116
    assert(session);
vb@190
  1117
    assert(msg);
vb@251
  1118
    assert(msg->from);
vb@251
  1119
    assert(msg->dir == PEP_dir_outgoing);
vb@190
  1120
    assert(color);
vb@190
  1121
vb@191
  1122
    if (!(session && msg && color))
vb@191
  1123
        return PEP_ILLEGAL_VALUE;
vb@191
  1124
vb@251
  1125
    if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
vb@251
  1126
        return PEP_ILLEGAL_VALUE;
vb@251
  1127
vb@237
  1128
    *color = PEP_rating_undefined;
vb@190
  1129
vb@190
  1130
    assert(msg->from);
vb@190
  1131
    if (msg->from == NULL)
vb@190
  1132
        return PEP_ILLEGAL_VALUE;
vb@190
  1133
vb@251
  1134
    status = myself(session, msg->from);
vb@251
  1135
    if (status != PEP_STATUS_OK)
vb@251
  1136
        return status;
vb@251
  1137
vb@251
  1138
    for (il = msg->to; il != NULL; il = il->next) {
vb@251
  1139
        if (il->ident) {
vb@251
  1140
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1141
                    il->ident);
vb@190
  1142
            comm_type_determined = true;
vb@251
  1143
        }
vb@251
  1144
    }
vb@190
  1145
vb@251
  1146
    for (il = msg->cc; il != NULL; il = il->next) {
vb@251
  1147
        if (il->ident) {
vb@251
  1148
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1149
                    il->ident);
vb@251
  1150
            comm_type_determined = true;
vb@251
  1151
        }
vb@190
  1152
    }
vb@190
  1153
vb@190
  1154
    if (comm_type_determined == false)
vb@237
  1155
        *color = PEP_rating_undefined;
vb@190
  1156
    else
vb@239
  1157
        *color = _rating(max_comm_type);
vb@190
  1158
vb@190
  1159
    return PEP_STATUS_OK;
vb@190
  1160
}
vb@190
  1161
vb@240
  1162
DYNAMIC_API PEP_STATUS identity_color(
vb@239
  1163
        PEP_SESSION session,
vb@239
  1164
        pEp_identity *ident,
vb@239
  1165
        PEP_color *color
vb@239
  1166
    )
vb@239
  1167
{
vb@239
  1168
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  1169
vb@239
  1170
    assert(session);
vb@239
  1171
    assert(ident);
vb@239
  1172
    assert(color);
vb@239
  1173
vb@239
  1174
    if (!(session && ident && color))
vb@239
  1175
        return PEP_ILLEGAL_VALUE;
vb@239
  1176
vb@239
  1177
    if (ident->me)
vb@239
  1178
        status = myself(session, ident);
vb@239
  1179
    else
vb@239
  1180
        status = update_identity(session, ident);
vb@239
  1181
vb@239
  1182
    if (status == PEP_STATUS_OK)
vb@239
  1183
        *color = _rating(ident->comm_type);
vb@239
  1184
vb@239
  1185
    return status;
vb@239
  1186
}
vb@239
  1187