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