src/message_api.c
author Volker Birk <vb@pep-project.org>
Fri, 08 May 2015 10:27:54 +0200
changeset 258 8e8eec84418b
parent 256 0232e3d7d77c
child 259 71d6c073fda4
permissions -rw-r--r--
cryptotech
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@250
   117
            is_mime_type(msg->attachments, "multipart/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@113
   246
    if (src->to) {
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@113
   262
    if (src->cc) {
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@113
   270
    if (src->bcc) {
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@113
   278
    if (src->reply_to) {
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@113
   286
    if (src->in_reply_to) {
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@113
   302
    if (src->keywords) {
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@48
   345
DYNAMIC_API PEP_STATUS encrypt_message(
vb@37
   346
        PEP_SESSION session,
vb@113
   347
        message *src,
vb@37
   348
        stringlist_t * extra,
vb@38
   349
        message **dst,
vb@81
   350
        PEP_enc_format enc_format
vb@37
   351
    )
vb@37
   352
{
vb@37
   353
    PEP_STATUS status = PEP_STATUS_OK;
vb@63
   354
    message * msg = NULL;
vb@63
   355
    stringlist_t * keys = NULL;
vb@113
   356
    bool free_src = false;
vb@37
   357
vb@37
   358
    assert(session);
vb@37
   359
    assert(src);
vb@37
   360
    assert(dst);
vb@81
   361
    assert(enc_format >= PEP_enc_pieces);
vb@81
   362
vb@191
   363
    if (!(session && src && dst && (enc_format >= PEP_enc_pieces)))
vb@191
   364
        return PEP_ILLEGAL_VALUE;
vb@191
   365
vb@37
   366
    *dst = NULL;
vb@67
   367
vb@250
   368
    determine_encryption_format(src);
vb@235
   369
    import_attached_keys(session, src);
vb@235
   370
vb@81
   371
    if (src->enc_format >= PEP_enc_pieces) {
vb@81
   372
        if (src->enc_format == enc_format) {
vb@113
   373
            assert(0); // the message is encrypted this way already
vb@81
   374
            msg = message_dup(src);
vb@81
   375
            if (msg == NULL)
vb@81
   376
                goto enomem;
vb@81
   377
            *dst = msg;
vb@81
   378
            return PEP_STATUS_OK;
vb@81
   379
        }
vb@81
   380
        else {
vb@113
   381
            // decrypt and re-encrypt again
vb@113
   382
            message * _dst = NULL;
vb@241
   383
            stringlist_t *_keylist = NULL;
vb@113
   384
            PEP_MIME_format mime = (enc_format == PEP_enc_PEP) ? PEP_MIME :
vb@113
   385
                    PEP_MIME_fields_omitted;
vb@113
   386
vb@251
   387
            PEP_color color;
vb@251
   388
            status = decrypt_message(session, src, mime, &_dst, &_keylist, &color);
vb@113
   389
            if (status != PEP_STATUS_OK)
vb@113
   390
                goto pep_error;
vb@241
   391
            free_stringlist(_keylist);
vb@113
   392
vb@113
   393
            src = _dst;
vb@113
   394
            free_src = true;
vb@81
   395
        }
vb@67
   396
    }
vb@37
   397
vb@236
   398
    status = myself(session, src->from);
vb@236
   399
    if (status != PEP_STATUS_OK)
vb@236
   400
        goto pep_error;
vb@236
   401
vb@81
   402
    msg = clone_to_empty_message(src);
vb@80
   403
    if (msg == NULL)
vb@63
   404
        goto enomem;
vb@40
   405
vb@80
   406
    keys = new_stringlist(src->from->fpr);
vb@63
   407
    if (keys == NULL)
vb@63
   408
        goto enomem;
vb@37
   409
vb@39
   410
    stringlist_t *_k = keys;
vb@39
   411
vb@39
   412
    if (extra) {
vb@39
   413
        _k = stringlist_append(_k, extra);
vb@63
   414
        if (_k == NULL)
vb@63
   415
            goto enomem;
vb@37
   416
    }
vb@39
   417
vb@39
   418
    bool dest_keys_found = false;
vb@37
   419
    identity_list * _il;
vb@80
   420
    for (_il = msg->to; _il && _il->ident; _il = _il->next) {
vb@63
   421
        PEP_STATUS status = update_identity(session, _il->ident);
vb@63
   422
        if (status != PEP_STATUS_OK)
vb@63
   423
            goto pep_error;
vb@63
   424
vb@37
   425
        if (_il->ident->fpr) {
vb@39
   426
            dest_keys_found = true;
vb@39
   427
            _k = stringlist_add(_k, _il->ident->fpr);
vb@63
   428
            if (_k == NULL)
vb@63
   429
                goto enomem;
vb@37
   430
        }
vb@37
   431
        else
vb@37
   432
            status = PEP_KEY_NOT_FOUND;
vb@37
   433
    }
vb@37
   434
vb@39
   435
    if (dest_keys_found) {
vb@38
   436
        char *ptext;
vb@37
   437
        char *ctext = NULL;
vb@37
   438
        size_t csize = 0;
vb@37
   439
vb@81
   440
        switch (enc_format) {
vb@112
   441
        case PEP_enc_PGP_MIME: {
vb@62
   442
            bool free_ptext = false;
vb@64
   443
vb@112
   444
            msg->enc_format = PEP_enc_PGP_MIME;
vb@37
   445
vb@113
   446
            if (src->mime == PEP_MIME) {
vb@113
   447
                message *_src = NULL;
vb@113
   448
                assert(src->longmsg);
vb@113
   449
                status = mime_decode_message(src->longmsg, &_src);
vb@113
   450
                if (status != PEP_STATUS_OK)
vb@113
   451
                    goto pep_error;
vb@113
   452
                if (free_src)
vb@113
   453
                    free_message(src);
vb@113
   454
                src = _src;
vb@113
   455
                free_src = true;
vb@62
   456
            }
vb@62
   457
vb@113
   458
            if (src->mime == PEP_MIME_none) {
vb@113
   459
                if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@113
   460
                    ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@113
   461
                    if (ptext == NULL)
vb@113
   462
                        goto enomem;
vb@113
   463
                    free_ptext = true;
vb@113
   464
                }
vb@113
   465
                else if (src->longmsg) {
vb@113
   466
                    ptext = src->longmsg;
vb@113
   467
                }
vb@125
   468
                else {
vb@125
   469
                    ptext = "pEp";
vb@125
   470
                }
vb@113
   471
vb@113
   472
                message *_src = calloc(1, sizeof(message));
vb@89
   473
                assert(_src);
vb@89
   474
                if (_src == NULL)
vb@89
   475
                    goto enomem;
vb@89
   476
                _src->longmsg = ptext;
vb@113
   477
                _src->longmsg_formatted = src->longmsg_formatted;
vb@113
   478
                _src->attachments = src->attachments;
vb@113
   479
                _src->enc_format = PEP_enc_PGP_MIME;
vb@113
   480
                status = mime_encode_message(_src, true, &ptext);
vb@67
   481
                assert(status == PEP_STATUS_OK);
vb@67
   482
                if (free_ptext)
vb@89
   483
                    free(_src->longmsg);
vb@89
   484
                free(_src);
vb@67
   485
                assert(ptext);
vb@67
   486
                if (ptext == NULL)
vb@67
   487
                    goto pep_error;
vb@67
   488
                free_ptext = true;
vb@67
   489
            }
vb@113
   490
            else /* if (src->mime == PEP_MIME_fields_omitted) */ {
vb@67
   491
                ptext = src->longmsg;
vb@67
   492
            }
vb@67
   493
vb@67
   494
            status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@67
   495
                    &ctext, &csize);
vb@62
   496
            if (free_ptext)
vb@62
   497
                free(ptext);
vb@113
   498
            if (ctext == NULL)
vb@63
   499
                goto pep_error;
vb@113
   500
vb@113
   501
            msg->longmsg = strdup(ctext);
vb@113
   502
            if (msg->longmsg == NULL)
vb@113
   503
                goto enomem;
vb@62
   504
        }
vb@63
   505
        break;
vb@62
   506
vb@62
   507
        case PEP_enc_pieces:
vb@64
   508
            msg->enc_format = PEP_enc_pieces;
vb@64
   509
vb@63
   510
            if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@83
   511
                ptext = combine_short_and_long(src->shortmsg, src->longmsg);
vb@63
   512
                if (ptext == NULL)
vb@63
   513
                    goto enomem;
vb@63
   514
vb@39
   515
                status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@39
   516
                        &ctext, &csize);
vb@40
   517
                free(ptext);
vb@38
   518
                if (ctext) {
vb@40
   519
                    msg->longmsg = strdup(ctext);
vb@64
   520
                    if (msg->longmsg == NULL)
vb@63
   521
                        goto enomem;
vb@38
   522
                }
vb@38
   523
                else {
vb@63
   524
                    goto pep_error;
vb@38
   525
                }
vb@38
   526
            }
vb@38
   527
            else if (src->longmsg) {
vb@38
   528
                ptext = src->longmsg;
vb@39
   529
                status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@39
   530
                        &ctext, &csize);
vb@38
   531
                if (ctext) {
vb@40
   532
                    msg->longmsg = strdup(ctext);
vb@64
   533
                    if (msg->longmsg == NULL)
vb@63
   534
                        goto enomem;
vb@38
   535
                }
vb@38
   536
                else {
vb@63
   537
                    goto pep_error;
vb@38
   538
                }
vb@38
   539
            }
vb@63
   540
vb@63
   541
            if (msg->longmsg_formatted) {
vb@38
   542
                ptext = src->longmsg_formatted;
vb@39
   543
                status = encrypt_and_sign(session, keys, ptext, strlen(ptext),
vb@39
   544
                        &ctext, &csize);
vb@38
   545
                if (ctext) {
vb@40
   546
                    msg->longmsg_formatted = strdup(ctext);
vb@63
   547
                    if (msg->longmsg_formatted == NULL)
vb@63
   548
                        goto enomem;
vb@63
   549
                }
vb@63
   550
                else {
vb@63
   551
                    goto pep_error;
vb@63
   552
                }
vb@63
   553
            }
vb@63
   554
vb@63
   555
            if (src->attachments) {
vb@63
   556
                bloblist_t *_s;
vb@63
   557
                bloblist_t *_d = new_bloblist(NULL, 0, NULL, NULL);
vb@63
   558
                if (_d == NULL)
vb@63
   559
                    goto enomem;
vb@63
   560
vb@63
   561
                msg->attachments = _d;
vb@63
   562
                for (_s = src->attachments; _s && _s->data; _s = _s->next) {
vb@63
   563
                    int psize = _s->size;
vb@63
   564
                    ptext = _s->data;
vb@63
   565
                    status = encrypt_and_sign(session, keys, ptext, psize,
vb@63
   566
                            &ctext, &csize);
vb@63
   567
                    if (ctext) {
vb@63
   568
                        char * _c = strdup(ctext);
vb@63
   569
                        if (_c == NULL)
vb@63
   570
                            goto enomem;
vb@63
   571
vb@63
   572
                        _d = bloblist_add(_d, _c, csize, _s->mime_type,
vb@113
   573
                                _s->filename);
vb@63
   574
                        if (_d == NULL)
vb@63
   575
                            goto enomem;
vb@63
   576
                    }
vb@63
   577
                    else {
vb@63
   578
                        goto pep_error;
vb@40
   579
                    }
vb@38
   580
                }
vb@38
   581
            }
vb@38
   582
            break;
vb@38
   583
vb@81
   584
        case PEP_enc_PEP:
vb@81
   585
            // TODO: implement
vb@81
   586
            NOT_IMPLEMENTED
vb@81
   587
vb@38
   588
        default:
vb@38
   589
            assert(0);
vb@63
   590
            status = PEP_ILLEGAL_VALUE;
vb@63
   591
            goto pep_error;
vb@37
   592
        }
vb@37
   593
    }
vb@37
   594
vb@37
   595
    free_stringlist(keys);
vb@113
   596
    if (free_src)
vb@113
   597
        free_message(src);
vb@63
   598
vb@64
   599
    if (msg->shortmsg == NULL)
vb@64
   600
        msg->shortmsg = strdup("pEp");
vb@64
   601
vb@236
   602
    attach_own_key(session, msg);
vb@235
   603
vb@63
   604
    *dst = msg;
vb@63
   605
    return PEP_STATUS_OK;
vb@63
   606
vb@63
   607
enomem:
vb@63
   608
    status = PEP_OUT_OF_MEMORY;
vb@63
   609
vb@63
   610
pep_error:
vb@63
   611
    free_stringlist(keys);
vb@63
   612
    free_message(msg);
vb@113
   613
    if (free_src)
vb@113
   614
        free_message(src);
vb@63
   615
vb@37
   616
    return status;
vb@37
   617
}
vb@37
   618
vb@113
   619
static bool is_encrypted_attachment(const bloblist_t *blob)
vb@113
   620
{
vb@113
   621
    char *ext;
vb@113
   622
 
vb@113
   623
    assert(blob);
vb@113
   624
vb@113
   625
    if (blob->filename == NULL)
vb@113
   626
        return false;
vb@113
   627
vb@113
   628
    ext = strrchr(blob->filename, '.');
vb@113
   629
    if (ext == NULL)
vb@113
   630
        return false;
vb@113
   631
vb@113
   632
    if (strcmp(blob->mime_type, "application/octet-stream")) {
vb@113
   633
        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
vb@113
   634
                strcmp(ext, ".asc") == 0)
vb@113
   635
            return true;
vb@113
   636
    }
vb@164
   637
    else if (strcmp(blob->mime_type, "text/plain")) {
vb@113
   638
        if (strcmp(ext, ".asc") == 0)
vb@113
   639
            return true;
vb@113
   640
    }
vb@113
   641
vb@113
   642
    return false;
vb@113
   643
}
vb@113
   644
vb@113
   645
static bool is_encrypted_html_attachment(const bloblist_t *blob)
vb@113
   646
{
vb@113
   647
    assert(blob);
vb@113
   648
    assert(blob->filename);
vb@113
   649
vb@113
   650
    if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
vb@113
   651
        if (strcmp(blob->filename + 11, ".pgp") == 0 ||
vb@113
   652
                strcmp(blob->filename + 11, ".asc") == 0)
vb@113
   653
            return true;
vb@113
   654
    }
vb@113
   655
vb@113
   656
    return false;
vb@113
   657
}
vb@113
   658
vb@159
   659
static char * without_double_ending(const char *filename)
vb@113
   660
{
vb@113
   661
    char *ext;
vb@113
   662
vb@113
   663
    assert(filename);
vb@113
   664
vb@113
   665
    ext = strrchr(filename, '.');
vb@113
   666
    if (ext == NULL)
vb@113
   667
        return NULL;
vb@113
   668
vb@113
   669
    return strndup(filename, ext - filename);
vb@113
   670
}
vb@113
   671
vb@251
   672
static PEP_color decrypt_color(PEP_STATUS status)
vb@251
   673
{
vb@251
   674
    switch (status) {
vb@251
   675
        case PEP_UNENCRYPTED:
vb@251
   676
        case PEP_VERIFIED:
vb@251
   677
        case PEP_VERIFY_NO_KEY:
vb@251
   678
        case PEP_VERIFIED_AND_TRUSTED:
vb@251
   679
            return PEP_rating_unencrypted;
vb@251
   680
vb@251
   681
        case PEP_DECRYPTED:
vb@251
   682
            return PEP_rating_unreliable;
vb@251
   683
vb@251
   684
        case PEP_DECRYPTED_AND_VERIFIED:
vb@251
   685
            return PEP_rating_reliable;
vb@251
   686
vb@251
   687
        case PEP_DECRYPT_WRONG_FORMAT:
vb@251
   688
        case PEP_DECRYPT_NO_KEY:
vb@251
   689
        case PEP_CANNOT_DECRYPT_UNKNOWN:
vb@251
   690
            return PEP_rating_cannot_decrypt;
vb@251
   691
vb@251
   692
        default:
vb@251
   693
            return PEP_rating_undefined;
vb@251
   694
    }
vb@251
   695
}
vb@251
   696
vb@251
   697
static PEP_color _rating(PEP_comm_type ct)
vb@251
   698
{
vb@251
   699
    if (ct == PEP_ct_unknown)
vb@251
   700
        return PEP_rating_undefined;
vb@251
   701
vb@251
   702
    else if (ct == PEP_ct_compromized)
vb@251
   703
        return PEP_rating_under_attack;
vb@251
   704
vb@251
   705
    else if (ct >= PEP_ct_confirmed_enc_anon)
vb@251
   706
        return PEP_rating_trusted_and_anonymized;
vb@251
   707
vb@251
   708
    else if (ct >= PEP_ct_strong_encryption)
vb@251
   709
        return PEP_rating_trusted;
vb@251
   710
vb@251
   711
    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
vb@251
   712
        return PEP_rating_reliable;
vb@251
   713
    
vb@251
   714
    else if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel)
vb@251
   715
        return PEP_rating_unencrypted;
vb@251
   716
vb@251
   717
    else
vb@251
   718
        return PEP_rating_unreliable;
vb@251
   719
}
vb@251
   720
vb@251
   721
static PEP_color key_color(PEP_SESSION session, const char *fpr) {
vb@251
   722
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@251
   723
vb@251
   724
    assert(session);
vb@251
   725
    assert(fpr);
vb@251
   726
vb@251
   727
    PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
vb@251
   728
    if (status != PEP_STATUS_OK)
vb@251
   729
        return PEP_rating_undefined;
vb@251
   730
vb@251
   731
    return _rating(comm_type);
vb@251
   732
}
vb@251
   733
vb@251
   734
static PEP_color keylist_color(PEP_SESSION session, stringlist_t *keylist)
vb@251
   735
{
vb@251
   736
    PEP_color color = PEP_rating_reliable;
vb@251
   737
vb@251
   738
    assert(keylist && keylist->value);
vb@251
   739
    if (keylist == NULL || keylist->value == NULL)
vb@251
   740
        return PEP_rating_unencrypted;
vb@251
   741
vb@251
   742
    stringlist_t *_kl;
vb@251
   743
    for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
vb@251
   744
        PEP_comm_type ct;
vb@251
   745
        PEP_STATUS status;
vb@251
   746
        PEP_color _color;
vb@251
   747
vb@251
   748
        _color = key_color(session, _kl->value);
vb@251
   749
        if (_color == PEP_rating_under_attack)
vb@251
   750
            return PEP_rating_under_attack;
vb@251
   751
vb@251
   752
        color = MIN(color, _color);
vb@251
   753
vb@251
   754
        status = least_trust(session, _kl->value, &ct);
vb@251
   755
        if (status != PEP_STATUS_OK)
vb@251
   756
            return PEP_rating_undefined;
vb@251
   757
vb@251
   758
        _color = _rating(ct);
vb@251
   759
        if (_color == PEP_rating_under_attack)
vb@251
   760
            return PEP_rating_under_attack;
vb@251
   761
vb@251
   762
        color = MIN(color, _color);
vb@251
   763
    }
vb@251
   764
vb@251
   765
    return color;
vb@251
   766
}
vb@251
   767
vb@48
   768
DYNAMIC_API PEP_STATUS decrypt_message(
vb@37
   769
        PEP_SESSION session,
vb@113
   770
        message *src,
vb@113
   771
        PEP_MIME_format mime,
vb@241
   772
        message **dst,
vb@251
   773
        stringlist_t **keylist,
vb@251
   774
        PEP_color *color
vb@37
   775
    )
vb@37
   776
{
vb@37
   777
    PEP_STATUS status = PEP_STATUS_OK;
vb@73
   778
    message *msg = NULL;
vb@112
   779
    char *ctext;
vb@112
   780
    size_t csize;
vb@112
   781
    char *ptext;
vb@112
   782
    size_t psize;
vb@241
   783
    stringlist_t *_keylist = NULL;
vb@113
   784
    bool free_src = false;
vb@37
   785
vb@74
   786
    assert(session);
vb@74
   787
    assert(src);
vb@74
   788
    assert(dst);
vb@241
   789
    assert(keylist);
vb@251
   790
    assert(color);
vb@73
   791
vb@251
   792
    if (!(session && src && dst && keylist && color))
vb@191
   793
        return PEP_ILLEGAL_VALUE;
vb@191
   794
vb@74
   795
    *dst = NULL;
vb@251
   796
    *keylist = NULL;
vb@251
   797
    *color = PEP_rating_undefined;
vb@81
   798
 
vb@250
   799
    determine_encryption_format(src);
vb@235
   800
    import_attached_keys(session, src);
vb@235
   801
vb@113
   802
    if (src->mime == PEP_MIME_fields_omitted || src->mime == PEP_MIME) {
vb@113
   803
        message *_src = NULL;
vb@113
   804
        status = mime_decode_message(src->longmsg, &_src);
vb@113
   805
        if (status != PEP_STATUS_OK)
vb@113
   806
            goto pep_error;
vb@81
   807
vb@113
   808
        if ( src->mime == PEP_MIME_fields_omitted) {
vb@113
   809
            status = copy_fields(_src, src);
vb@113
   810
            if (status != PEP_STATUS_OK) {
vb@113
   811
                free_message(_src);
vb@113
   812
                goto pep_error;
vb@113
   813
            }
vb@113
   814
        }
vb@113
   815
vb@113
   816
        src = _src;
vb@113
   817
        free_src = true;
vb@113
   818
    }
vb@113
   819
vb@119
   820
    // src message is not MIME encoded (any more)
vb@113
   821
    assert(src->mime == PEP_MIME_none);
vb@113
   822
vb@117
   823
    if (!is_PGP_message_text(src->longmsg)) {
vb@117
   824
        status = PEP_UNENCRYPTED;
vb@117
   825
    }
vb@258
   826
    else {
vb@258
   827
        ctext = src->longmsg;
vb@258
   828
        csize = strlen(src->longmsg);
vb@117
   829
vb@258
   830
        status = decrypt_and_verify(session, ctext, csize, &ptext, &psize,
vb@258
   831
                &_keylist);
vb@258
   832
        if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
vb@258
   833
            goto pep_error;
vb@258
   834
    }
vb@113
   835
vb@256
   836
    *color = decrypt_color(status);
vb@256
   837
    if (*color != PEP_rating_under_attack && _keylist) {
vb@251
   838
        PEP_color _color = keylist_color(session, _keylist);
vb@251
   839
        if (_color == PEP_rating_under_attack)
vb@251
   840
            *color = PEP_rating_under_attack;
vb@251
   841
        else
vb@251
   842
            *color = MIN(*color, _color);
vb@251
   843
    }
vb@251
   844
vb@256
   845
    if (ptext) {
vb@256
   846
        switch (src->enc_format) {
vb@256
   847
            case PEP_enc_PGP_MIME:
vb@256
   848
                status = mime_decode_message(ptext, &msg);
vb@113
   849
                if (status != PEP_STATUS_OK)
vb@113
   850
                    goto pep_error;
vb@113
   851
vb@256
   852
                break;
vb@256
   853
vb@256
   854
            case PEP_enc_pieces:
vb@256
   855
                msg = clone_to_empty_message(src);
vb@256
   856
                if (msg == NULL)
vb@113
   857
                    goto enomem;
vb@113
   858
vb@256
   859
                msg->longmsg = strdup(ptext);
vb@256
   860
                if (msg->longmsg == NULL)
vb@256
   861
                    goto enomem;
vb@256
   862
vb@256
   863
                bloblist_t *_m = msg->attachments;
vb@256
   864
                bloblist_t *_s;
vb@256
   865
                for (_s = src->attachments; _s; _s = _s->next) {
vb@256
   866
                    if (is_encrypted_attachment(_s)) {
vb@256
   867
                        stringlist_t *_keylist = NULL;
vb@256
   868
                        ctext = _s->data;
vb@256
   869
                        csize = _s->size;
vb@256
   870
vb@256
   871
                        status = decrypt_and_verify(session, ctext, csize,
vb@256
   872
                                &ptext, &psize, &_keylist);
vb@256
   873
                        if (ptext == NULL)
vb@256
   874
                            goto pep_error;
vb@256
   875
                        free_stringlist(_keylist);
vb@256
   876
vb@256
   877
                        if (is_encrypted_html_attachment(_s)) {
vb@256
   878
                            msg->longmsg_formatted = strdup(ptext);
vb@256
   879
                            if (msg->longmsg_formatted == NULL)
vb@256
   880
                                goto pep_error;
vb@256
   881
                        }
vb@256
   882
                        else {
vb@256
   883
                            char * mime_type = "application/octet-stream";
vb@256
   884
                            char * filename =
vb@256
   885
                                    without_double_ending(_s->filename);
vb@256
   886
                            if (filename == NULL)
vb@256
   887
                                goto enomem;
vb@256
   888
vb@256
   889
                            _m = bloblist_add(_m, ptext, psize, mime_type,
vb@256
   890
                                    filename);
vb@256
   891
                            if (_m == NULL)
vb@256
   892
                                goto enomem;
vb@256
   893
vb@256
   894
                           if (msg->attachments == NULL)
vb@256
   895
                                msg->attachments = _m;
vb@256
   896
                        }
vb@256
   897
                    }
vb@256
   898
                }
vb@256
   899
vb@256
   900
                break;
vb@256
   901
vb@256
   902
            default:
vb@256
   903
                // BUG: must implement more
vb@256
   904
                NOT_IMPLEMENTED
vb@256
   905
        }
vb@256
   906
vb@256
   907
        switch (src->enc_format) {
vb@256
   908
            case PEP_enc_PGP_MIME:
vb@256
   909
            case PEP_enc_pieces:
vb@256
   910
                status = copy_fields(msg, src);
vb@256
   911
                if (status != PEP_STATUS_OK)
vb@256
   912
                    goto pep_error;
vb@256
   913
vb@256
   914
                if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
vb@256
   915
                    free(msg->shortmsg);
vb@256
   916
                    msg->shortmsg = strdup(src->shortmsg);
vb@256
   917
                    if (msg->shortmsg == NULL)
vb@256
   918
                        goto enomem;
vb@256
   919
                }
vb@256
   920
vb@256
   921
                if (msg->shortmsg == NULL || strcmp(msg->shortmsg, "pEp") == 0)
vb@256
   922
                {
vb@256
   923
                    char * shortmsg;
vb@256
   924
                    char * longmsg;
vb@256
   925
vb@256
   926
                    int r = seperate_short_and_long(msg->longmsg, &shortmsg,
vb@256
   927
                            &longmsg);
vb@256
   928
                    if (r == -1)
vb@256
   929
                        goto enomem;
vb@256
   930
vb@256
   931
                    free(msg->shortmsg);
vb@256
   932
                    free(msg->longmsg);
vb@256
   933
vb@256
   934
                    msg->shortmsg = shortmsg;
vb@256
   935
                    msg->longmsg = longmsg;
vb@256
   936
                }
vb@256
   937
                else {
vb@256
   938
                    msg->shortmsg = strdup(src->shortmsg);
vb@256
   939
                    if (msg->shortmsg == NULL)
vb@256
   940
                        goto enomem;
vb@256
   941
                    msg->longmsg = ptext;
vb@256
   942
                }
vb@256
   943
                break;
vb@256
   944
vb@256
   945
            default:
vb@256
   946
                // BUG: must implement more
vb@256
   947
                NOT_IMPLEMENTED
vb@256
   948
        }
vb@256
   949
vb@256
   950
        switch (mime) {
vb@256
   951
            case PEP_MIME_none:
vb@256
   952
                break;
vb@256
   953
vb@256
   954
            case PEP_MIME:
vb@256
   955
            case PEP_MIME_fields_omitted:
vb@256
   956
                {
vb@256
   957
                    char *text = NULL;
vb@256
   958
                    status = mime_encode_message(msg,
vb@256
   959
                            mime == PEP_MIME_fields_omitted, &text);
vb@256
   960
                    if (status != PEP_STATUS_OK)
vb@256
   961
                        goto pep_error;
vb@256
   962
vb@256
   963
                    message *_msg = clone_to_empty_message(msg);
vb@256
   964
                    if (_msg == NULL) {
vb@256
   965
                        free(text);
vb@256
   966
                        goto enomem;
vb@256
   967
                    }
vb@256
   968
                    _msg->longmsg = text;
vb@256
   969
                    _msg->shortmsg = strdup(msg->shortmsg);
vb@256
   970
                    if (msg->shortmsg == NULL)
vb@256
   971
                        goto enomem;
vb@256
   972
vb@256
   973
                    free_message(msg);
vb@256
   974
                    msg = _msg;
vb@256
   975
                }
vb@256
   976
                break;
vb@256
   977
        }
vb@256
   978
vb@256
   979
        import_attached_keys(session, msg);
vb@113
   980
    }
vb@113
   981
vb@113
   982
    if (free_src)
vb@113
   983
        free_message(src);
vb@235
   984
vb@74
   985
    *dst = msg;
vb@241
   986
    *keylist = _keylist;
vb@241
   987
vb@74
   988
    return PEP_STATUS_OK;
vb@73
   989
vb@73
   990
enomem:
vb@73
   991
    status = PEP_OUT_OF_MEMORY;
vb@73
   992
vb@73
   993
pep_error:
vb@73
   994
    free_message(msg);
vb@241
   995
    free_stringlist(_keylist);
vb@113
   996
    if (free_src)
vb@113
   997
        free_message(src);
vb@39
   998
vb@37
   999
    return status;
vb@37
  1000
}
vb@37
  1001
vb@190
  1002
static PEP_comm_type _get_comm_type(
vb@190
  1003
        PEP_SESSION session,
vb@190
  1004
        PEP_comm_type max_comm_type,
vb@190
  1005
        pEp_identity *ident
vb@190
  1006
    )
vb@190
  1007
{
vb@190
  1008
    PEP_STATUS status = update_identity(session, ident);
vb@190
  1009
vb@190
  1010
    if (max_comm_type == PEP_ct_compromized)
vb@190
  1011
        return PEP_ct_compromized;
vb@190
  1012
vb@190
  1013
    if (status == PEP_STATUS_OK) {
vb@190
  1014
        if (ident->comm_type == PEP_ct_compromized)
vb@190
  1015
            return PEP_ct_compromized;
vb@190
  1016
        else
vb@190
  1017
            return MIN(max_comm_type, ident->comm_type);
vb@190
  1018
    }
vb@190
  1019
    else {
vb@190
  1020
        return PEP_ct_unknown;
vb@190
  1021
    }
vb@190
  1022
}
vb@190
  1023
vb@251
  1024
DYNAMIC_API PEP_STATUS outgoing_message_color(
vb@190
  1025
        PEP_SESSION session,
vb@190
  1026
        message *msg,
vb@232
  1027
        PEP_color *color
vb@190
  1028
    )
vb@190
  1029
{
vb@190
  1030
    PEP_STATUS status = PEP_STATUS_OK;
vb@190
  1031
    PEP_comm_type max_comm_type = PEP_ct_pEp;
vb@190
  1032
    bool comm_type_determined = false;
vb@190
  1033
    identity_list * il;
vb@190
  1034
vb@190
  1035
    assert(session);
vb@190
  1036
    assert(msg);
vb@251
  1037
    assert(msg->from);
vb@251
  1038
    assert(msg->dir == PEP_dir_outgoing);
vb@190
  1039
    assert(color);
vb@190
  1040
vb@191
  1041
    if (!(session && msg && color))
vb@191
  1042
        return PEP_ILLEGAL_VALUE;
vb@191
  1043
vb@251
  1044
    if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
vb@251
  1045
        return PEP_ILLEGAL_VALUE;
vb@251
  1046
vb@237
  1047
    *color = PEP_rating_undefined;
vb@190
  1048
vb@190
  1049
    assert(msg->from);
vb@190
  1050
    if (msg->from == NULL)
vb@190
  1051
        return PEP_ILLEGAL_VALUE;
vb@190
  1052
vb@251
  1053
    status = myself(session, msg->from);
vb@251
  1054
    if (status != PEP_STATUS_OK)
vb@251
  1055
        return status;
vb@251
  1056
vb@251
  1057
    for (il = msg->to; il != NULL; il = il->next) {
vb@251
  1058
        if (il->ident) {
vb@251
  1059
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1060
                    il->ident);
vb@190
  1061
            comm_type_determined = true;
vb@251
  1062
        }
vb@251
  1063
    }
vb@190
  1064
vb@251
  1065
    for (il = msg->cc; il != NULL; il = il->next) {
vb@251
  1066
        if (il->ident) {
vb@251
  1067
            max_comm_type = _get_comm_type(session, max_comm_type,
vb@251
  1068
                    il->ident);
vb@251
  1069
            comm_type_determined = true;
vb@251
  1070
        }
vb@190
  1071
    }
vb@190
  1072
vb@190
  1073
    if (comm_type_determined == false)
vb@237
  1074
        *color = PEP_rating_undefined;
vb@190
  1075
    else
vb@239
  1076
        *color = _rating(max_comm_type);
vb@190
  1077
vb@190
  1078
    return PEP_STATUS_OK;
vb@190
  1079
}
vb@190
  1080
vb@240
  1081
DYNAMIC_API PEP_STATUS identity_color(
vb@239
  1082
        PEP_SESSION session,
vb@239
  1083
        pEp_identity *ident,
vb@239
  1084
        PEP_color *color
vb@239
  1085
    )
vb@239
  1086
{
vb@239
  1087
    PEP_STATUS status = PEP_STATUS_OK;
vb@239
  1088
vb@239
  1089
    assert(session);
vb@239
  1090
    assert(ident);
vb@239
  1091
    assert(color);
vb@239
  1092
vb@239
  1093
    if (!(session && ident && color))
vb@239
  1094
        return PEP_ILLEGAL_VALUE;
vb@239
  1095
vb@239
  1096
    if (ident->me)
vb@239
  1097
        status = myself(session, ident);
vb@239
  1098
    else
vb@239
  1099
        status = update_identity(session, ident);
vb@239
  1100
vb@239
  1101
    if (status == PEP_STATUS_OK)
vb@239
  1102
        *color = _rating(ident->comm_type);
vb@239
  1103
vb@239
  1104
    return status;
vb@239
  1105
}
vb@239
  1106