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