src/keymanagement.c
author Edouard Tisserant
Wed, 09 Mar 2016 12:52:54 +0100
changeset 499 a6c031512a72
parent 498 8d5b72408060
child 500 a0baa88ec3e3
permissions -rw-r--r--
Fixed #32 : assert(keylist) without if in keymanagement.c:238
vb@130
     1
#include "platform.h"
vb@0
     2
vb@0
     3
#include <string.h>
vb@0
     4
#include <stdio.h>
vb@0
     5
#include <stdlib.h>
vb@0
     6
#include <assert.h>
vb@0
     7
vb@217
     8
#include "pEp_internal.h"
vb@0
     9
#include "keymanagement.h"
vb@0
    10
vb@0
    11
#ifndef MIN
vb@0
    12
#define MIN(A, B) ((B) > (A) ? (A) : (B))
vb@0
    13
#endif
vb@0
    14
Edouard@439
    15
#ifndef EMPTYSTR
Edouard@439
    16
#define EMPTYSTR(STR) ((STR == NULL) || (STR)[0] == 0)
vb@8
    17
#endif
vb@8
    18
vb@214
    19
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
vb@214
    20
vb@0
    21
DYNAMIC_API PEP_STATUS update_identity(
vb@0
    22
        PEP_SESSION session, pEp_identity * identity
vb@0
    23
    )
vb@0
    24
{
vb@0
    25
    pEp_identity *stored_identity;
vb@0
    26
    PEP_STATUS status;
vb@0
    27
vb@0
    28
    assert(session);
vb@0
    29
    assert(identity);
Edouard@439
    30
    assert(!EMPTYSTR(identity->address));
vb@0
    31
Edouard@439
    32
    if (!(session && identity && !EMPTYSTR(identity->address)))
vb@191
    33
        return PEP_ILLEGAL_VALUE;
vb@191
    34
vb@0
    35
    status = get_identity(session, identity->address, &stored_identity);
vb@0
    36
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
    37
    if (status == PEP_OUT_OF_MEMORY)
vb@0
    38
        return PEP_OUT_OF_MEMORY;
vb@0
    39
vb@14
    40
    if (stored_identity) {
vb@14
    41
        PEP_comm_type _comm_type_key;
vb@14
    42
        status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
vb@14
    43
        assert(status != PEP_OUT_OF_MEMORY);
vb@14
    44
        if (status == PEP_OUT_OF_MEMORY)
vb@14
    45
            return PEP_OUT_OF_MEMORY;
vb@10
    46
Edouard@439
    47
        if (EMPTYSTR(identity->user_id)) {
vb@22
    48
            free(identity->user_id);
Edouard@498
    49
            identity->user_id = strndup(stored_identity->user_id, stored_identity->user_id_size);
Edouard@498
    50
            assert(identity->user_id);
vb@22
    51
            if (identity->user_id == NULL)
vb@22
    52
                return PEP_OUT_OF_MEMORY;
vb@22
    53
            identity->user_id_size = stored_identity->user_id_size;
vb@22
    54
        }
vb@22
    55
Edouard@439
    56
        if (EMPTYSTR(identity->username)) {
vb@22
    57
            free(identity->username);
Edouard@498
    58
            identity->username = strndup(stored_identity->username, stored_identity->username_size);
Edouard@498
    59
            assert(identity->username);
vb@22
    60
            if (identity->username == NULL)
vb@22
    61
                return PEP_OUT_OF_MEMORY;
vb@22
    62
            identity->username_size = stored_identity->username_size;
vb@22
    63
        }
vb@22
    64
Edouard@439
    65
        if (EMPTYSTR(identity->fpr)) {
Edouard@498
    66
            identity->fpr = strndup(stored_identity->fpr, stored_identity->fpr_size);
vb@8
    67
            assert(identity->fpr);
vb@8
    68
            if (identity->fpr == NULL)
vb@8
    69
                return PEP_OUT_OF_MEMORY;
Edouard@406
    70
            identity->fpr_size = stored_identity->fpr_size;
vb@14
    71
            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
vb@14
    72
                identity->comm_type = _comm_type_key;
vb@14
    73
            }
vb@14
    74
            else {
vb@14
    75
                identity->comm_type = stored_identity->comm_type;
vb@14
    76
            }
vb@8
    77
        }
Edouard@439
    78
        else /* !EMPTYSTR(identity->fpr) */ {
Edouard@498
    79
            if (strncmp(identity->fpr, stored_identity->fpr, stored_identity->fpr_size) == 0) {
vb@14
    80
                if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
vb@14
    81
                    identity->comm_type = _comm_type_key;
Edouard@488
    82
                }else{
Edouard@488
    83
                    identity->comm_type = stored_identity->comm_type;
Edouard@488
    84
                    if (identity->comm_type == PEP_ct_unknown) {
Edouard@488
    85
                        identity->comm_type = _comm_type_key;
vb@8
    86
                    }
vb@8
    87
                }
vb@496
    88
            } else {
Edouard@488
    89
                status = get_trust(session, identity);
Edouard@488
    90
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@488
    91
                if (status == PEP_OUT_OF_MEMORY)
Edouard@488
    92
                    return PEP_OUT_OF_MEMORY;
vb@496
    93
                if (identity->comm_type < stored_identity->comm_type)
vb@496
    94
                    identity->comm_type = PEP_ct_unknown;
vb@8
    95
            }
vb@8
    96
        }
vb@8
    97
vb@21
    98
        if (identity->lang[0] == 0) {
vb@21
    99
            identity->lang[0] = stored_identity->lang[0];
vb@21
   100
            identity->lang[1] = stored_identity->lang[1];
vb@21
   101
            identity->lang[2] = 0;
vb@21
   102
        }
vb@21
   103
    }
vb@21
   104
    else /* stored_identity == NULL */ {
Edouard@439
   105
        if (!EMPTYSTR(identity->fpr)) {
vb@21
   106
            PEP_comm_type _comm_type_key;
vb@21
   107
vb@21
   108
            status = get_key_rating(session, identity->fpr, &_comm_type_key);
vb@21
   109
            assert(status != PEP_OUT_OF_MEMORY);
vb@21
   110
            if (status == PEP_OUT_OF_MEMORY)
vb@21
   111
                return PEP_OUT_OF_MEMORY;
vb@21
   112
vb@21
   113
            identity->comm_type = _comm_type_key;
vb@21
   114
        }
Edouard@439
   115
        else /* EMPTYSTR(identity->fpr) */ {
vb@21
   116
            PEP_STATUS status;
vb@21
   117
            stringlist_t *keylist;
vb@21
   118
            char *_fpr = NULL;
vb@21
   119
            identity->comm_type = PEP_ct_unknown;
vb@21
   120
vb@21
   121
            status = find_keys(session, identity->address, &keylist);
vb@21
   122
            assert(status != PEP_OUT_OF_MEMORY);
vb@21
   123
            if (status == PEP_OUT_OF_MEMORY)
vb@21
   124
                return PEP_OUT_OF_MEMORY;
vb@21
   125
vb@21
   126
            stringlist_t *_keylist;
vb@21
   127
            for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
vb@21
   128
                PEP_comm_type _comm_type_key;
vb@21
   129
vb@21
   130
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
vb@21
   131
                assert(status != PEP_OUT_OF_MEMORY);
vb@21
   132
                if (status == PEP_OUT_OF_MEMORY) {
vb@21
   133
                    free_stringlist(keylist);
vb@21
   134
                    return PEP_OUT_OF_MEMORY;
vb@21
   135
                }
vb@21
   136
vb@21
   137
                if (identity->comm_type == PEP_ct_unknown) {
vb@21
   138
                    if (_comm_type_key != PEP_ct_compromized && _comm_type_key != PEP_ct_unknown) {
vb@21
   139
                        identity->comm_type = _comm_type_key;
vb@21
   140
                        _fpr = _keylist->value;
vb@21
   141
                    }
vb@21
   142
                }
vb@21
   143
                else {
vb@21
   144
                    if (_comm_type_key != PEP_ct_compromized && _comm_type_key != PEP_ct_unknown) {
vb@21
   145
                        if (_comm_type_key > identity->comm_type) {
vb@21
   146
                            identity->comm_type = _comm_type_key;
vb@21
   147
                            _fpr = _keylist->value;
vb@21
   148
                        }
vb@21
   149
                    }
vb@21
   150
                }
vb@21
   151
            }
vb@21
   152
vb@21
   153
            if (_fpr) {
vb@21
   154
                free(identity->fpr);
vb@21
   155
vb@21
   156
                identity->fpr = strdup(_fpr);
vb@21
   157
                if (identity->fpr == NULL) {
vb@21
   158
                    free_stringlist(keylist);
vb@21
   159
                    return PEP_OUT_OF_MEMORY;
vb@21
   160
                }
vb@21
   161
                identity->fpr_size = strlen(identity->fpr);
vb@21
   162
            }
vb@21
   163
            free_stringlist(keylist);
vb@21
   164
        }
vb@21
   165
    }
vb@21
   166
vb@21
   167
    status = PEP_STATUS_OK;
vb@21
   168
Edouard@439
   169
    if (identity->comm_type != PEP_ct_unknown && !EMPTYSTR(identity->user_id)) {
Edouard@439
   170
        assert(!EMPTYSTR(identity->username)); // this should not happen
vb@22
   171
Edouard@439
   172
        if (EMPTYSTR(identity->username)) { // mitigate
vb@0
   173
            free(identity->username);
vb@22
   174
            identity->username = strdup("anonymous");
vb@8
   175
            if (identity->username == NULL)
vb@8
   176
                return PEP_OUT_OF_MEMORY;
vb@22
   177
            identity->username_size = 9;
vb@0
   178
        }
vb@8
   179
vb@8
   180
        status = set_identity(session, identity);
vb@8
   181
        assert(status == PEP_STATUS_OK);
Edouard@499
   182
        if (status != PEP_STATUS_OK) {
Edouard@499
   183
            return status;
Edouard@499
   184
        }
vb@8
   185
    }
vb@8
   186
vb@297
   187
    if (identity->comm_type != PEP_ct_compromized &&
vb@297
   188
            identity->comm_type < PEP_ct_strong_but_unconfirmed)
vb@297
   189
        if (session->examine_identity)
vb@297
   190
            session->examine_identity(identity, session->examine_management);
vb@297
   191
vb@8
   192
    return status;
vb@0
   193
}
vb@0
   194
vb@0
   195
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
vb@0
   196
{
vb@0
   197
    PEP_STATUS status;
vb@46
   198
    stringlist_t *keylist = NULL;
vb@0
   199
vb@0
   200
    assert(session);
vb@0
   201
    assert(identity);
vb@0
   202
    assert(identity->address);
vb@0
   203
    assert(identity->username);
vb@0
   204
    assert(identity->user_id);
vb@0
   205
vb@191
   206
    if (!(session && identity && identity->address && identity->username &&
vb@191
   207
                identity->user_id))
vb@191
   208
        return PEP_ILLEGAL_VALUE;
vb@191
   209
vb@0
   210
    identity->comm_type = PEP_ct_pEp;
vb@0
   211
    identity->me = true;
vb@0
   212
vb@215
   213
    DEBUG_LOG("myself", "debug", identity->address);
vb@0
   214
vb@0
   215
    status = find_keys(session, identity->address, &keylist);
vb@0
   216
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   217
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   218
        return PEP_OUT_OF_MEMORY;
vb@0
   219
vb@0
   220
    if (keylist == NULL || keylist->value == NULL) {
vb@215
   221
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   222
        status = generate_keypair(session, identity);
vb@0
   223
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   224
        if (status != PEP_STATUS_OK) {
vb@0
   225
            char buf[11];
vb@0
   226
            snprintf(buf, 11, "%d", status);
vb@215
   227
            DEBUG_LOG("generating key pair failed", "debug", buf);
vb@0
   228
            return status;
vb@0
   229
        }
vb@0
   230
vb@0
   231
        status = find_keys(session, identity->address, &keylist);
vb@0
   232
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   233
        if (status == PEP_OUT_OF_MEMORY)
vb@0
   234
            return PEP_OUT_OF_MEMORY;
vb@0
   235
vb@372
   236
        assert(keylist && keylist->value);
Edouard@371
   237
        if (keylist == NULL || keylist->value == NULL) {
Edouard@371
   238
            return PEP_UNKNOWN_ERROR;
Edouard@371
   239
        }
vb@0
   240
    }
vb@214
   241
    else {
vb@214
   242
        bool expired;
vb@214
   243
        status = key_expired(session, keylist->value, &expired);
vb@214
   244
        assert(status == PEP_STATUS_OK);
Edouard@499
   245
        if (status != PEP_STATUS_OK) {
Edouard@499
   246
            goto free_keylist;
Edouard@499
   247
        }
vb@214
   248
vb@214
   249
        if (status == PEP_STATUS_OK && expired) {
vb@214
   250
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
vb@214
   251
            renew_key(session, keylist->value, ts);
vb@214
   252
            free_timestamp(ts);
vb@214
   253
        }
vb@214
   254
    }
vb@0
   255
vb@0
   256
    if (identity->fpr)
vb@0
   257
        free(identity->fpr);
vb@0
   258
    identity->fpr = strdup(keylist->value);
vb@0
   259
    assert(identity->fpr);
Edouard@499
   260
    if (identity->fpr == NULL){
Edouard@499
   261
        status = PEP_OUT_OF_MEMORY;
Edouard@499
   262
        goto free_keylist;
Edouard@499
   263
    }
vb@8
   264
    identity->fpr_size = strlen(identity->fpr);
vb@0
   265
vb@0
   266
    status = set_identity(session, identity);
vb@0
   267
    assert(status == PEP_STATUS_OK);
Edouard@499
   268
    if (status != PEP_STATUS_OK) {
Edouard@499
   269
        goto free_keylist;
Edouard@499
   270
    }
vb@0
   271
vb@0
   272
    return PEP_STATUS_OK;
Edouard@499
   273
Edouard@499
   274
free_keylist:
Edouard@499
   275
    free_stringlist(keylist);
Edouard@499
   276
    return status;
vb@0
   277
}
vb@0
   278
vb@296
   279
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   280
        PEP_SESSION session, 
vb@292
   281
        examine_identity_t examine_identity,
vb@292
   282
        void *management
vb@292
   283
    )
vb@292
   284
{
vb@292
   285
    assert(session);
vb@292
   286
    if (!session)
vb@292
   287
        return PEP_ILLEGAL_VALUE;
vb@292
   288
vb@292
   289
    session->examine_management = management;
vb@292
   290
    session->examine_identity = examine_identity;
vb@292
   291
vb@292
   292
    return PEP_STATUS_OK;
vb@292
   293
}
vb@292
   294
vb@0
   295
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   296
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   297
        void *management
vb@0
   298
    )
vb@0
   299
{
vb@0
   300
    PEP_SESSION session;
vb@0
   301
    pEp_identity *identity;
Edouard@499
   302
    PEP_STATUS status;
vb@0
   303
Edouard@499
   304
    assert(retrieve_next_identity);
Edouard@499
   305
    assert(management);
Edouard@499
   306
Edouard@499
   307
    if (!retrieve_next_identity || !management)
Edouard@499
   308
        return PEP_ILLEGAL_VALUE;
Edouard@499
   309
Edouard@499
   310
    status = init(&session);
vb@0
   311
    assert(status == PEP_STATUS_OK);
vb@0
   312
    if (status != PEP_STATUS_OK)
vb@0
   313
        return status;
vb@0
   314
vb@0
   315
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   316
Edouard@499
   317
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   318
    {
vb@0
   319
        assert(identity->address);
Edouard@499
   320
        if(identity->address)
Edouard@499
   321
        {
Edouard@499
   322
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   323
Edouard@499
   324
            if (identity->me) {
Edouard@499
   325
                status = myself(session, identity);
Edouard@499
   326
            } else {
Edouard@499
   327
                status = recv_key(session, identity->address);
Edouard@499
   328
            }
Edouard@499
   329
vb@0
   330
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   331
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   332
                return PEP_OUT_OF_MEMORY;
vb@0
   333
        }
vb@0
   334
        free_identity(identity);
vb@0
   335
    }
vb@0
   336
vb@0
   337
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   338
vb@0
   339
    release(session);
vb@0
   340
    return PEP_STATUS_OK;
vb@0
   341
}
vb@0
   342
vb@357
   343
DYNAMIC_API PEP_STATUS key_compromized(
vb@357
   344
        PEP_SESSION session,
vb@357
   345
        pEp_identity *ident
vb@357
   346
    )
vb@215
   347
{
vb@218
   348
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   349
vb@215
   350
    assert(session);
vb@357
   351
    assert(ident);
Edouard@439
   352
    assert(!EMPTYSTR(ident->fpr));
vb@215
   353
vb@357
   354
    if (!(session && ident && ident->fpr))
vb@215
   355
        return PEP_ILLEGAL_VALUE;
vb@215
   356
vb@357
   357
    if (ident->me)
vb@357
   358
        revoke_key(session, ident->fpr, NULL);
vb@357
   359
    status = mark_as_compromized(session, ident->fpr);
vb@218
   360
vb@218
   361
    return status;
vb@215
   362
}
vb@215
   363
Edouard@410
   364
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   365
        PEP_SESSION session,
Edouard@410
   366
        pEp_identity *ident
Edouard@410
   367
    )
Edouard@410
   368
{
Edouard@410
   369
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   370
Edouard@410
   371
    assert(session);
Edouard@410
   372
    assert(ident);
vb@420
   373
    assert(!ident->me);
Edouard@439
   374
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   375
    assert(!EMPTYSTR(ident->address));
Edouard@439
   376
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   377
vb@420
   378
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   379
            ident->user_id))
Edouard@410
   380
        return PEP_ILLEGAL_VALUE;
Edouard@410
   381
vb@420
   382
    status = update_identity(session, ident);
vb@420
   383
    if (status != PEP_STATUS_OK)
vb@420
   384
        return status;
Edouard@410
   385
Edouard@442
   386
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   387
        ident->comm_type = PEP_ct_unknown;
vb@421
   388
    else
vb@421
   389
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   390
vb@420
   391
    status = set_identity(session, ident);
vb@421
   392
    if (status != PEP_STATUS_OK)
vb@421
   393
        return status;
vb@421
   394
vb@422
   395
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   396
        status = update_identity(session, ident);
Edouard@410
   397
    return status;
Edouard@410
   398
}
Edouard@410
   399
vb@354
   400
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   401
        PEP_SESSION session,
vb@354
   402
        pEp_identity *ident
vb@354
   403
    )
vb@354
   404
{
vb@354
   405
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   406
vb@354
   407
    assert(session);
vb@354
   408
    assert(ident);
Edouard@439
   409
    assert(!EMPTYSTR(ident->address));
Edouard@439
   410
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   411
    assert(!EMPTYSTR(ident->fpr));
vb@354
   412
    assert(!ident->me);
vb@354
   413
Edouard@439
   414
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   415
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   416
        return PEP_ILLEGAL_VALUE;
vb@354
   417
vb@354
   418
    status = update_identity(session, ident);
vb@354
   419
    if (status != PEP_STATUS_OK)
vb@354
   420
        return status;
vb@354
   421
vb@356
   422
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   423
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   424
        status = set_identity(session, ident);
vb@356
   425
    }
vb@356
   426
    else {
vb@356
   427
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   428
        status = PEP_CANNOT_SET_TRUST;
vb@356
   429
    }
vb@354
   430
vb@354
   431
    return status;
vb@354
   432
}
vb@354
   433