src/keymanagement.c
author Krista Grothoff <krista@pep-project.org>
Sun, 25 Sep 2016 17:46:27 +0200
branchENGINE-27
changeset 1188 cfe67b49b72b
parent 1185 4b01328f3cf2
child 1193 01d0a3e883cc
permissions -rw-r--r--
ENGINE-27: intermittent commit, update_identity should now never try to set or return a blacklisted key
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
Edouard@774
    59
PEP_STATUS elect_pubkey(
Edouard@755
    60
        PEP_SESSION session, pEp_identity * identity
Edouard@755
    61
    )
Edouard@755
    62
{
Edouard@755
    63
    PEP_STATUS status;
Edouard@755
    64
    stringlist_t *keylist;
Edouard@755
    65
    char *_fpr = NULL;
Edouard@755
    66
    identity->comm_type = PEP_ct_unknown;
Edouard@755
    67
Edouard@755
    68
    status = find_keys(session, identity->address, &keylist);
Edouard@755
    69
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    70
    if (status == PEP_OUT_OF_MEMORY)
Edouard@755
    71
        return PEP_OUT_OF_MEMORY;
Edouard@755
    72
Edouard@755
    73
    stringlist_t *_keylist;
Edouard@755
    74
    for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@755
    75
        PEP_comm_type _comm_type_key;
Edouard@755
    76
Edouard@755
    77
        status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@755
    78
        assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    79
        if (status == PEP_OUT_OF_MEMORY) {
Edouard@755
    80
            free_stringlist(keylist);
Edouard@755
    81
            return PEP_OUT_OF_MEMORY;
Edouard@755
    82
        }
Edouard@755
    83
Edouard@755
    84
        if (_comm_type_key != PEP_ct_compromized &&
Edouard@755
    85
            _comm_type_key != PEP_ct_unknown)
Edouard@755
    86
        {
Edouard@755
    87
            if (identity->comm_type == PEP_ct_unknown ||
Edouard@755
    88
                _comm_type_key > identity->comm_type)
Edouard@755
    89
            {
Edouard@755
    90
                identity->comm_type = _comm_type_key;
Edouard@755
    91
                _fpr = _keylist->value;
Edouard@755
    92
            }
Edouard@755
    93
        }
Edouard@755
    94
    }
Edouard@755
    95
Edouard@755
    96
    if (_fpr) {
Edouard@755
    97
        free(identity->fpr);
Edouard@755
    98
Edouard@755
    99
        identity->fpr = strdup(_fpr);
Edouard@755
   100
        if (identity->fpr == NULL) {
Edouard@755
   101
            free_stringlist(keylist);
Edouard@755
   102
            return PEP_OUT_OF_MEMORY;
Edouard@755
   103
        }
Edouard@755
   104
    }
Edouard@755
   105
    free_stringlist(keylist);
Edouard@755
   106
    return PEP_STATUS_OK;
Edouard@755
   107
}
Edouard@755
   108
vb@0
   109
DYNAMIC_API PEP_STATUS update_identity(
vb@0
   110
        PEP_SESSION session, pEp_identity * identity
vb@0
   111
    )
vb@0
   112
{
vb@0
   113
    pEp_identity *stored_identity;
vb@0
   114
    PEP_STATUS status;
vb@0
   115
vb@0
   116
    assert(session);
vb@0
   117
    assert(identity);
Edouard@439
   118
    assert(!EMPTYSTR(identity->address));
vb@0
   119
Edouard@439
   120
    if (!(session && identity && !EMPTYSTR(identity->address)))
vb@191
   121
        return PEP_ILLEGAL_VALUE;
vb@191
   122
Edouard@559
   123
    int _no_user_id = EMPTYSTR(identity->user_id);
edouard@1164
   124
    int _did_elect_new_key = 0;
Edouard@559
   125
Edouard@559
   126
    if (_no_user_id)
Edouard@559
   127
    {
Edouard@559
   128
        free(identity->user_id);
Edouard@559
   129
vb@591
   130
        identity->user_id = calloc(1, strlen(identity->address) + 6);
Edouard@562
   131
        if (!identity->user_id)
Edouard@559
   132
        {
Edouard@562
   133
            return PEP_OUT_OF_MEMORY;
Edouard@559
   134
        }
vb@983
   135
        snprintf(identity->user_id, strlen(identity->address) + 6,
Edouard@562
   136
                 "TOFU_%s", identity->address);
Edouard@559
   137
    }
vb@934
   138
 
Edouard@559
   139
    status = get_identity(session,
Edouard@559
   140
                          identity->address,
Edouard@559
   141
                          identity->user_id,
Edouard@559
   142
                          &stored_identity);
Edouard@559
   143
    
vb@0
   144
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   145
    if (status == PEP_OUT_OF_MEMORY)
Edouard@829
   146
        goto exit_free;
vb@0
   147
krista@1188
   148
    /* We elect a pubkey first in case there's no acceptable stored fpr */
krista@1185
   149
    status = elect_pubkey(session, identity);
krista@1185
   150
    if (status != PEP_STATUS_OK)
krista@1185
   151
        goto exit_free;
krista@1188
   152
        
vb@14
   153
    if (stored_identity) {
vb@14
   154
        PEP_comm_type _comm_type_key;
krista@1188
   155
        
krista@1188
   156
        bool dont_use_fpr = true;
krista@1188
   157
        status = blacklist_is_listed(session, stored_identity->fpr, &dont_use_fpr);
krista@1188
   158
        if (status != PEP_STATUS_OK)
krista@1188
   159
            dont_use_fpr = true; 
krista@1188
   160
            
krista@1188
   161
        if (dont_use_fpr && !(EMPTYSTR(identity->fpr))) {
krista@1188
   162
            /* elected pubkey */
krista@1188
   163
            if (status != PEP_STATUS_OK)
krista@1188
   164
                goto exit_free;
krista@1188
   165
            status = blacklist_is_listed(session, identity->fpr, &dont_use_fpr);
krista@1188
   166
            if (dont_use_fpr) {
krista@1188
   167
                free(identity->fpr);
krista@1188
   168
                identity->fpr = NULL;
krista@1188
   169
            }
krista@1188
   170
            else {
krista@1188
   171
                _did_elect_new_key = 1;
krista@1188
   172
            }
krista@1188
   173
        }
krista@1188
   174
        else {
krista@1188
   175
            identity->fpr = strdup(stored_identity->fpr);
krista@1188
   176
            assert(identity->fpr);
krista@1188
   177
            if (identity->fpr == NULL)
krista@1188
   178
                return PEP_OUT_OF_MEMORY;
krista@1188
   179
            
krista@1188
   180
        }
krista@1188
   181
        
krista@1188
   182
        /* Ok, at this point, we either have a non-blacklisted fpr we can work */
krista@1188
   183
        /* with, or we've got nada.                                            */        
krista@1188
   184
        if (!EMPTYSTR(identity->fpr)) {
krista@1188
   185
            status = get_key_rating(session, identity->fpr, &_comm_type_key);
krista@1188
   186
            assert(status != PEP_OUT_OF_MEMORY);
krista@1188
   187
            if (status == PEP_OUT_OF_MEMORY)
krista@1188
   188
                goto exit_free;
krista@1188
   189
            status = get_trust(session, identity);
krista@1188
   190
            if (status == PEP_OUT_OF_MEMORY)
krista@1188
   191
                goto exit_free;
krista@1188
   192
            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
krista@1188
   193
                identity->comm_type = _comm_type_key;
krista@1188
   194
            } else{
krista@1188
   195
                identity->comm_type = stored_identity->comm_type;
krista@1188
   196
                if (identity->comm_type == PEP_ct_unknown) {
krista@1188
   197
                    identity->comm_type = _comm_type_key;
krista@1188
   198
                }
krista@1188
   199
            }
krista@1188
   200
        }
krista@1188
   201
            
Edouard@439
   202
        if (EMPTYSTR(identity->username)) {
vb@22
   203
            free(identity->username);
vb@591
   204
            identity->username = strdup(stored_identity->username);
Edouard@498
   205
            assert(identity->username);
Edouard@829
   206
            if (identity->username == NULL){
Edouard@829
   207
                status = PEP_OUT_OF_MEMORY;
Edouard@829
   208
                goto exit_free;
Edouard@829
   209
            }
vb@22
   210
        }
vb@22
   211
vb@21
   212
        if (identity->lang[0] == 0) {
vb@21
   213
            identity->lang[0] = stored_identity->lang[0];
vb@21
   214
            identity->lang[1] = stored_identity->lang[1];
vb@21
   215
            identity->lang[2] = 0;
vb@21
   216
        }
vb@932
   217
vb@932
   218
        identity->flags = stored_identity->flags;
vb@21
   219
    }
vb@21
   220
    else /* stored_identity == NULL */ {
vb@934
   221
        identity->flags = 0;
vb@934
   222
krista@1188
   223
        /* Work with the elected key from above */
Edouard@439
   224
        if (!EMPTYSTR(identity->fpr)) {
vb@21
   225
            PEP_comm_type _comm_type_key;
vb@21
   226
vb@21
   227
            status = get_key_rating(session, identity->fpr, &_comm_type_key);
vb@21
   228
            assert(status != PEP_OUT_OF_MEMORY);
vb@21
   229
            if (status == PEP_OUT_OF_MEMORY)
Edouard@829
   230
                goto exit_free;
vb@21
   231
vb@21
   232
            identity->comm_type = _comm_type_key;
vb@21
   233
        }
vb@21
   234
    }
vb@21
   235
vb@21
   236
    status = PEP_STATUS_OK;
vb@21
   237
Edouard@439
   238
    if (identity->comm_type != PEP_ct_unknown && !EMPTYSTR(identity->user_id)) {
Edouard@439
   239
        assert(!EMPTYSTR(identity->username)); // this should not happen
vb@22
   240
Edouard@439
   241
        if (EMPTYSTR(identity->username)) { // mitigate
vb@0
   242
            free(identity->username);
vb@22
   243
            identity->username = strdup("anonymous");
vb@931
   244
            assert(identity->username);
Edouard@829
   245
            if (identity->username == NULL){
Edouard@829
   246
                status = PEP_OUT_OF_MEMORY;
Edouard@829
   247
                goto exit_free;
Edouard@829
   248
            }
vb@0
   249
        }
vb@8
   250
Edouard@755
   251
        // Identity doesn't get stored if call was just about checking existing
Edouard@755
   252
        // user by address (i.e. no user id given but already stored)
edouard@1164
   253
        if (!(_no_user_id && stored_identity) || _did_elect_new_key)
Edouard@559
   254
        {
Edouard@559
   255
            status = set_identity(session, identity);
Edouard@559
   256
            assert(status == PEP_STATUS_OK);
Edouard@559
   257
            if (status != PEP_STATUS_OK) {
Edouard@829
   258
                goto exit_free;
Edouard@559
   259
            }
Edouard@499
   260
        }
vb@8
   261
    }
vb@8
   262
vb@297
   263
    if (identity->comm_type != PEP_ct_compromized &&
vb@297
   264
            identity->comm_type < PEP_ct_strong_but_unconfirmed)
vb@297
   265
        if (session->examine_identity)
vb@297
   266
            session->examine_identity(identity, session->examine_management);
vb@297
   267
Edouard@829
   268
exit_free :
Edouard@829
   269
    
Edouard@829
   270
    if (stored_identity){
Edouard@829
   271
        free_identity(stored_identity);
Edouard@829
   272
    }
Edouard@829
   273
vb@8
   274
    return status;
vb@0
   275
}
vb@0
   276
Edouard@774
   277
PEP_STATUS elect_ownkey(
Edouard@774
   278
        PEP_SESSION session, pEp_identity * identity
Edouard@774
   279
    )
Edouard@774
   280
{
Edouard@774
   281
    PEP_STATUS status;
Edouard@774
   282
    stringlist_t *keylist = NULL;
Edouard@774
   283
Edouard@774
   284
    free(identity->fpr);
Edouard@774
   285
    identity->fpr = NULL;
Edouard@774
   286
Edouard@774
   287
    status = find_keys(session, identity->address, &keylist);
Edouard@774
   288
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   289
    if (status == PEP_OUT_OF_MEMORY)
Edouard@774
   290
        return PEP_OUT_OF_MEMORY;
Edouard@774
   291
    
Edouard@774
   292
    if (keylist != NULL && keylist->value != NULL)
Edouard@774
   293
    {
Edouard@774
   294
        char *_fpr = NULL;
Edouard@774
   295
        identity->comm_type = PEP_ct_unknown;
Edouard@774
   296
Edouard@774
   297
        stringlist_t *_keylist;
Edouard@774
   298
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@774
   299
            bool is_own = false;
Edouard@774
   300
            
Edouard@774
   301
            if (session->use_only_own_private_keys)
Edouard@774
   302
            {
Edouard@774
   303
                status = own_key_is_listed(session, _keylist->value, &is_own);
Edouard@774
   304
                assert(status == PEP_STATUS_OK);
Edouard@774
   305
                if (status != PEP_STATUS_OK) {
Edouard@774
   306
                    free_stringlist(keylist);
Edouard@774
   307
                    return status;
Edouard@774
   308
                }
Edouard@774
   309
            }
Edouard@774
   310
Edouard@774
   311
            // TODO : also accept synchronized device group keys ?
Edouard@774
   312
            
Edouard@774
   313
            if (!session->use_only_own_private_keys || is_own)
Edouard@774
   314
            {
Edouard@774
   315
                PEP_comm_type _comm_type_key;
Edouard@774
   316
                
Edouard@774
   317
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@774
   318
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   319
                if (status == PEP_OUT_OF_MEMORY) {
Edouard@774
   320
                    free_stringlist(keylist);
Edouard@774
   321
                    return PEP_OUT_OF_MEMORY;
Edouard@774
   322
                }
Edouard@774
   323
                
Edouard@774
   324
                if (_comm_type_key != PEP_ct_compromized &&
Edouard@774
   325
                    _comm_type_key != PEP_ct_unknown)
Edouard@774
   326
                {
Edouard@774
   327
                    if (identity->comm_type == PEP_ct_unknown ||
Edouard@774
   328
                        _comm_type_key > identity->comm_type)
Edouard@774
   329
                    {
Edouard@774
   330
                        identity->comm_type = _comm_type_key;
Edouard@774
   331
                        _fpr = _keylist->value;
Edouard@774
   332
                    }
Edouard@774
   333
                }
Edouard@774
   334
            }
Edouard@774
   335
        }
Edouard@774
   336
        
Edouard@774
   337
        if (_fpr)
Edouard@774
   338
        {
Edouard@774
   339
            identity->fpr = strdup(_fpr);
Edouard@774
   340
            assert(identity->fpr);
Edouard@774
   341
            if (identity->fpr == NULL)
Edouard@774
   342
            {
Edouard@774
   343
                free_stringlist(keylist);
Edouard@774
   344
                return PEP_OUT_OF_MEMORY;
Edouard@774
   345
            }
Edouard@774
   346
        }
Edouard@774
   347
        free_stringlist(keylist);
Edouard@774
   348
    }
Edouard@774
   349
    return PEP_STATUS_OK;
Edouard@774
   350
}
Edouard@774
   351
vb@0
   352
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
vb@0
   353
{
Edouard@560
   354
    pEp_identity *stored_identity;
vb@0
   355
    PEP_STATUS status;
vb@0
   356
vb@0
   357
    assert(session);
vb@0
   358
    assert(identity);
vb@0
   359
    assert(identity->address);
vb@0
   360
    assert(identity->username);
Edouard@658
   361
    assert(EMPTYSTR(identity->user_id) ||
Edouard@658
   362
           strcmp(identity->user_id, PEP_OWN_USERID) == 0);
vb@0
   363
vb@191
   364
    if (!(session && identity && identity->address && identity->username &&
Edouard@658
   365
          (EMPTYSTR(identity->user_id) ||
Edouard@658
   366
           strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
vb@191
   367
        return PEP_ILLEGAL_VALUE;
vb@191
   368
vb@0
   369
    identity->comm_type = PEP_ct_pEp;
vb@0
   370
    identity->me = true;
Edouard@658
   371
    
Edouard@658
   372
    if(EMPTYSTR(identity->user_id))
Edouard@658
   373
    {
Edouard@658
   374
        free(identity->user_id);
Edouard@658
   375
        identity->user_id = strdup(PEP_OWN_USERID);
Edouard@658
   376
        assert(identity->user_id);
Edouard@658
   377
        if (identity->user_id == NULL)
Edouard@658
   378
        {
Edouard@658
   379
            return PEP_OUT_OF_MEMORY;
Edouard@658
   380
        }
Edouard@658
   381
    }
vb@0
   382
vb@215
   383
    DEBUG_LOG("myself", "debug", identity->address);
Edouard@560
   384
    
Edouard@560
   385
    status = get_identity(session,
Edouard@560
   386
                          identity->address,
Edouard@560
   387
                          identity->user_id,
Edouard@560
   388
                          &stored_identity);
Edouard@560
   389
    
vb@0
   390
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   391
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   392
        return PEP_OUT_OF_MEMORY;
Edouard@560
   393
    
Edouard@560
   394
    if (stored_identity)
Edouard@560
   395
    {
Edouard@560
   396
        if (EMPTYSTR(identity->fpr)) {
vb@591
   397
            identity->fpr = strdup(stored_identity->fpr);
Edouard@560
   398
            assert(identity->fpr);
Edouard@560
   399
            if (identity->fpr == NULL)
Edouard@560
   400
            {
Edouard@560
   401
                return PEP_OUT_OF_MEMORY;
Edouard@560
   402
            }
Edouard@560
   403
        }
vb@932
   404
vb@932
   405
        identity->flags = stored_identity->flags;
Edouard@588
   406
    }
Edouard@588
   407
    else if (!EMPTYSTR(identity->fpr))
Edouard@588
   408
    {
Edouard@588
   409
        // App must have a good reason to give fpr, such as explicit
Edouard@588
   410
        // import of private key, or similar.
Edouard@588
   411
Edouard@658
   412
        // Take given fpr as-is.
vb@934
   413
vb@934
   414
        identity->flags = 0;
Edouard@560
   415
    }
Edouard@560
   416
    else
Edouard@560
   417
    {
Edouard@774
   418
        status = elect_ownkey(session, identity);
Edouard@774
   419
        assert(status == PEP_STATUS_OK);
Edouard@774
   420
        if (status != PEP_STATUS_OK) {
Edouard@774
   421
            return status;
Edouard@560
   422
        }
vb@934
   423
vb@934
   424
        identity->flags = 0;
Edouard@560
   425
    }
Edouard@695
   426
Edouard@695
   427
    bool revoked = false;
Edouard@695
   428
    char *r_fpr = NULL;
Edouard@695
   429
    if (!EMPTYSTR(identity->fpr))
Edouard@695
   430
    {
Edouard@695
   431
        status = key_revoked(session, identity->fpr, &revoked);
Edouard@775
   432
Edouard@775
   433
        // Forces re-election if key is missing and own-key-only not forced
Edouard@775
   434
        if (!session->use_only_own_private_keys && status == PEP_KEY_NOT_FOUND) 
Edouard@775
   435
        {
Edouard@775
   436
            status = elect_ownkey(session, identity);
Edouard@775
   437
            assert(status == PEP_STATUS_OK);
Edouard@775
   438
            if (status != PEP_STATUS_OK) {
Edouard@775
   439
                return status;
Edouard@775
   440
            }
Edouard@775
   441
        } 
Edouard@775
   442
        else if (status != PEP_STATUS_OK) 
Edouard@775
   443
        {
Edouard@695
   444
            return status;
Edouard@695
   445
        }
Edouard@695
   446
    }
Edouard@560
   447
    
Edouard@695
   448
    if (EMPTYSTR(identity->fpr) || revoked)
Edouard@695
   449
    {        
Edouard@695
   450
        if(revoked)
Edouard@695
   451
        {
Edouard@697
   452
            r_fpr = identity->fpr;
Edouard@697
   453
            identity->fpr = NULL;
Edouard@695
   454
        }
Edouard@560
   455
        
vb@215
   456
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   457
        status = generate_keypair(session, identity);
vb@0
   458
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   459
        if (status != PEP_STATUS_OK) {
vb@0
   460
            char buf[11];
vb@0
   461
            snprintf(buf, 11, "%d", status);
vb@215
   462
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   463
            if(revoked && r_fpr)
Edouard@695
   464
                free(r_fpr);
vb@0
   465
            return status;
vb@0
   466
        }
Edouard@560
   467
        
Edouard@695
   468
        if(revoked)
Edouard@695
   469
        {
Edouard@695
   470
            status = set_revoked(session, r_fpr,
Edouard@695
   471
                                 identity->fpr, time(NULL));
Edouard@695
   472
            free(r_fpr);
Edouard@695
   473
            if (status != PEP_STATUS_OK) {
Edouard@695
   474
                return status;
Edouard@695
   475
            }
Edouard@371
   476
        }
vb@0
   477
    }
Edouard@560
   478
    else
Edouard@560
   479
    {
vb@214
   480
        bool expired;
Edouard@701
   481
        status = key_expired(session, identity->fpr, 
Edouard@701
   482
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   483
                             &expired);
Edouard@701
   484
vb@214
   485
        assert(status == PEP_STATUS_OK);
Edouard@499
   486
        if (status != PEP_STATUS_OK) {
Edouard@588
   487
            return status;
Edouard@499
   488
        }
vb@214
   489
vb@214
   490
        if (status == PEP_STATUS_OK && expired) {
vb@214
   491
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   492
            renew_key(session, identity->fpr, ts);
vb@214
   493
            free_timestamp(ts);
vb@214
   494
        }
vb@214
   495
    }
vb@0
   496
vb@0
   497
    status = set_identity(session, identity);
vb@0
   498
    assert(status == PEP_STATUS_OK);
Edouard@499
   499
    if (status != PEP_STATUS_OK) {
Edouard@588
   500
        return status;
Edouard@499
   501
    }
vb@0
   502
vb@0
   503
    return PEP_STATUS_OK;
Edouard@499
   504
vb@0
   505
}
vb@0
   506
vb@296
   507
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   508
        PEP_SESSION session, 
vb@292
   509
        examine_identity_t examine_identity,
vb@292
   510
        void *management
vb@292
   511
    )
vb@292
   512
{
vb@292
   513
    assert(session);
vb@292
   514
    if (!session)
vb@292
   515
        return PEP_ILLEGAL_VALUE;
vb@292
   516
vb@292
   517
    session->examine_management = management;
vb@292
   518
    session->examine_identity = examine_identity;
vb@292
   519
vb@292
   520
    return PEP_STATUS_OK;
vb@292
   521
}
vb@292
   522
vb@0
   523
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   524
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   525
        void *management
vb@0
   526
    )
vb@0
   527
{
vb@0
   528
    PEP_SESSION session;
vb@0
   529
    pEp_identity *identity;
Edouard@499
   530
    PEP_STATUS status;
vb@0
   531
vb@24
   532
    assert(retrieve_next_identity);
vb@24
   533
    assert(management);
vb@24
   534
Edouard@499
   535
    if (!retrieve_next_identity || !management)
Edouard@499
   536
        return PEP_ILLEGAL_VALUE;
Edouard@499
   537
Edouard@499
   538
    status = init(&session);
Edouard@499
   539
    assert(status == PEP_STATUS_OK);
Edouard@499
   540
    if (status != PEP_STATUS_OK)
Edouard@499
   541
        return status;
Edouard@499
   542
vb@0
   543
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   544
Edouard@499
   545
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   546
    {
vb@0
   547
        assert(identity->address);
Edouard@499
   548
        if(identity->address)
Edouard@499
   549
        {
Edouard@499
   550
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   551
Edouard@499
   552
            if (identity->me) {
Edouard@499
   553
                status = myself(session, identity);
Edouard@499
   554
            } else {
Edouard@499
   555
                status = recv_key(session, identity->address);
Edouard@499
   556
            }
Edouard@499
   557
vb@0
   558
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   559
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   560
                return PEP_OUT_OF_MEMORY;
vb@0
   561
        }
vb@0
   562
        free_identity(identity);
vb@0
   563
    }
vb@0
   564
vb@0
   565
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   566
vb@0
   567
    release(session);
vb@0
   568
    return PEP_STATUS_OK;
vb@0
   569
}
vb@0
   570
vb@357
   571
DYNAMIC_API PEP_STATUS key_compromized(
vb@357
   572
        PEP_SESSION session,
vb@357
   573
        pEp_identity *ident
vb@357
   574
    )
vb@215
   575
{
vb@218
   576
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   577
vb@215
   578
    assert(session);
vb@357
   579
    assert(ident);
Edouard@439
   580
    assert(!EMPTYSTR(ident->fpr));
vb@215
   581
vb@357
   582
    if (!(session && ident && ident->fpr))
vb@215
   583
        return PEP_ILLEGAL_VALUE;
vb@215
   584
vb@357
   585
    if (ident->me)
Edouard@697
   586
    {
vb@357
   587
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   588
        myself(session, ident);
Edouard@697
   589
    }
Edouard@697
   590
    else
Edouard@697
   591
    {
Edouard@697
   592
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
   593
    }
vb@218
   594
vb@218
   595
    return status;
vb@215
   596
}
vb@215
   597
Edouard@410
   598
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   599
        PEP_SESSION session,
Edouard@410
   600
        pEp_identity *ident
Edouard@410
   601
    )
Edouard@410
   602
{
Edouard@410
   603
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   604
Edouard@410
   605
    assert(session);
Edouard@410
   606
    assert(ident);
vb@420
   607
    assert(!ident->me);
Edouard@439
   608
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   609
    assert(!EMPTYSTR(ident->address));
Edouard@439
   610
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   611
vb@420
   612
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   613
            ident->user_id))
Edouard@410
   614
        return PEP_ILLEGAL_VALUE;
Edouard@410
   615
vb@420
   616
    status = update_identity(session, ident);
vb@420
   617
    if (status != PEP_STATUS_OK)
vb@420
   618
        return status;
Edouard@410
   619
Edouard@442
   620
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   621
        ident->comm_type = PEP_ct_unknown;
vb@421
   622
    else
vb@421
   623
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   624
vb@420
   625
    status = set_identity(session, ident);
vb@421
   626
    if (status != PEP_STATUS_OK)
vb@421
   627
        return status;
vb@421
   628
vb@422
   629
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   630
        status = update_identity(session, ident);
Edouard@410
   631
    return status;
Edouard@410
   632
}
Edouard@410
   633
vb@354
   634
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   635
        PEP_SESSION session,
vb@354
   636
        pEp_identity *ident
vb@354
   637
    )
vb@354
   638
{
vb@354
   639
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   640
vb@354
   641
    assert(session);
vb@354
   642
    assert(ident);
Edouard@439
   643
    assert(!EMPTYSTR(ident->address));
Edouard@439
   644
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   645
    assert(!EMPTYSTR(ident->fpr));
vb@354
   646
    assert(!ident->me);
vb@354
   647
Edouard@439
   648
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   649
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   650
        return PEP_ILLEGAL_VALUE;
vb@354
   651
vb@354
   652
    status = update_identity(session, ident);
vb@354
   653
    if (status != PEP_STATUS_OK)
vb@354
   654
        return status;
vb@354
   655
vb@356
   656
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   657
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   658
        status = set_identity(session, ident);
vb@356
   659
    }
vb@356
   660
    else {
vb@356
   661
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   662
        status = PEP_CANNOT_SET_TRUST;
vb@356
   663
    }
vb@354
   664
vb@354
   665
    return status;
vb@354
   666
}
vb@354
   667
Edouard@584
   668
DYNAMIC_API PEP_STATUS own_key_is_listed(
Edouard@584
   669
                                           PEP_SESSION session,
Edouard@584
   670
                                           const char *fpr,
Edouard@584
   671
                                           bool *listed
Edouard@584
   672
                                           )
Edouard@584
   673
{
Edouard@584
   674
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   675
    int count;
Edouard@584
   676
    
Edouard@584
   677
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   678
    
Edouard@584
   679
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   680
        return PEP_ILLEGAL_VALUE;
Edouard@584
   681
    
Edouard@584
   682
    *listed = false;
Edouard@584
   683
    
Edouard@584
   684
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   685
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   686
    
Edouard@584
   687
    int result;
Edouard@584
   688
    
Edouard@584
   689
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   690
    switch (result) {
Edouard@584
   691
        case SQLITE_ROW:
Edouard@584
   692
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   693
            *listed = count > 0;
Edouard@584
   694
            status = PEP_STATUS_OK;
Edouard@584
   695
            break;
Edouard@584
   696
            
Edouard@584
   697
        default:
Edouard@584
   698
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   699
    }
Edouard@584
   700
    
Edouard@584
   701
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   702
    return status;
Edouard@584
   703
}
Edouard@584
   704
Edouard@584
   705
DYNAMIC_API PEP_STATUS own_key_retrieve(
Edouard@584
   706
                                          PEP_SESSION session,
Edouard@584
   707
                                          stringlist_t **own_key
Edouard@584
   708
                                          )
Edouard@584
   709
{
Edouard@584
   710
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   711
    
Edouard@584
   712
    assert(session);
Edouard@584
   713
    assert(own_key);
Edouard@584
   714
    
Edouard@584
   715
    if (!(session && own_key))
Edouard@584
   716
        return PEP_ILLEGAL_VALUE;
Edouard@584
   717
    
Edouard@584
   718
    *own_key = NULL;
Edouard@584
   719
    stringlist_t *_own_key = new_stringlist(NULL);
Edouard@584
   720
    if (_own_key == NULL)
Edouard@584
   721
        goto enomem;
Edouard@584
   722
    
Edouard@584
   723
    sqlite3_reset(session->own_key_retrieve);
Edouard@584
   724
    
Edouard@584
   725
    int result;
Edouard@584
   726
    const char *fpr = NULL;
Edouard@584
   727
    
Edouard@584
   728
    stringlist_t *_bl = _own_key;
Edouard@584
   729
    do {
Edouard@584
   730
        result = sqlite3_step(session->own_key_retrieve);
Edouard@584
   731
        switch (result) {
Edouard@584
   732
            case SQLITE_ROW:
Edouard@584
   733
                fpr = (const char *) sqlite3_column_text(session->own_key_retrieve, 0);
Edouard@584
   734
                
Edouard@584
   735
                _bl = stringlist_add(_bl, fpr);
Edouard@584
   736
                if (_bl == NULL)
Edouard@584
   737
                    goto enomem;
Edouard@584
   738
                
Edouard@584
   739
                break;
Edouard@584
   740
                
Edouard@584
   741
            case SQLITE_DONE:
Edouard@584
   742
                break;
Edouard@584
   743
                
Edouard@584
   744
            default:
Edouard@584
   745
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   746
                result = SQLITE_DONE;
Edouard@584
   747
        }
Edouard@584
   748
    } while (result != SQLITE_DONE);
Edouard@584
   749
    
Edouard@584
   750
    sqlite3_reset(session->own_key_retrieve);
Edouard@584
   751
    if (status == PEP_STATUS_OK)
Edouard@584
   752
        *own_key = _own_key;
Edouard@584
   753
    else
Edouard@584
   754
        free_stringlist(_own_key);
Edouard@584
   755
    
Edouard@584
   756
    goto the_end;
Edouard@584
   757
    
Edouard@584
   758
enomem:
Edouard@584
   759
    free_stringlist(_own_key);
Edouard@584
   760
    status = PEP_OUT_OF_MEMORY;
Edouard@584
   761
    
Edouard@584
   762
the_end:
Edouard@584
   763
    return status;
Edouard@584
   764
}