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