src/keymanagement.c
author Edouard Tisserant <edouard@pep-project.org>
Mon, 05 Sep 2016 18:01:42 +0200
branchkeysync
changeset 1140 c11cfcd37237
parent 1080 59f4dd88605c
child 1145 78f8723ccaba
permissions -rw-r--r--
moved KeyGen state injection from generate_keypair() to the bottom of myself() (in case of successfull keygen), thus ensuring that new identity has been already created when starting to multicast beacons to all own identities.
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
vb@1078
   123
    if (identity->me || (identity->user_id && strcmp(identity->user_id, PEP_OWN_USERID) == 0)) {
vb@1078
   124
        identity->me = true;
vb@1078
   125
        return myself(session, identity);
vb@1078
   126
    }
vb@1078
   127
Edouard@559
   128
    int _no_user_id = EMPTYSTR(identity->user_id);
Edouard@559
   129
Edouard@559
   130
    if (_no_user_id)
Edouard@559
   131
    {
vb@1015
   132
        status = get_identity(session, identity->address, PEP_OWN_USERID,
vb@1015
   133
                &stored_identity);
vb@1015
   134
        if (status == PEP_STATUS_OK) {
vb@1015
   135
            free_identity(stored_identity);
vb@1015
   136
            return myself(session, identity);
vb@1015
   137
        }
vb@1015
   138
Edouard@559
   139
        free(identity->user_id);
Edouard@559
   140
vb@591
   141
        identity->user_id = calloc(1, strlen(identity->address) + 6);
Edouard@562
   142
        if (!identity->user_id)
Edouard@559
   143
        {
Edouard@562
   144
            return PEP_OUT_OF_MEMORY;
Edouard@559
   145
        }
vb@983
   146
        snprintf(identity->user_id, strlen(identity->address) + 6,
Edouard@562
   147
                 "TOFU_%s", identity->address);
Edouard@559
   148
    }
vb@934
   149
 
Edouard@559
   150
    status = get_identity(session,
Edouard@559
   151
                          identity->address,
Edouard@559
   152
                          identity->user_id,
Edouard@559
   153
                          &stored_identity);
Edouard@559
   154
    
vb@0
   155
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   156
    if (status == PEP_OUT_OF_MEMORY)
Edouard@829
   157
        goto exit_free;
vb@0
   158
vb@14
   159
    if (stored_identity) {
vb@14
   160
        PEP_comm_type _comm_type_key;
vb@14
   161
        status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
vb@14
   162
        assert(status != PEP_OUT_OF_MEMORY);
vb@14
   163
        if (status == PEP_OUT_OF_MEMORY)
Edouard@829
   164
            goto exit_free;
vb@10
   165
Edouard@439
   166
        if (EMPTYSTR(identity->username)) {
vb@22
   167
            free(identity->username);
vb@591
   168
            identity->username = strdup(stored_identity->username);
Edouard@498
   169
            assert(identity->username);
Edouard@829
   170
            if (identity->username == NULL){
Edouard@829
   171
                status = PEP_OUT_OF_MEMORY;
Edouard@829
   172
                goto exit_free;
Edouard@829
   173
            }
vb@22
   174
        }
vb@22
   175
Edouard@439
   176
        if (EMPTYSTR(identity->fpr)) {
vb@591
   177
            identity->fpr = strdup(stored_identity->fpr);
vb@8
   178
            assert(identity->fpr);
vb@8
   179
            if (identity->fpr == NULL)
vb@8
   180
                return PEP_OUT_OF_MEMORY;
vb@14
   181
            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
Edouard@774
   182
                PEP_STATUS status = elect_pubkey(session, identity);
Edouard@755
   183
                if (status != PEP_STATUS_OK)
Edouard@829
   184
                    goto exit_free;
vb@14
   185
            }
vb@14
   186
            else {
vb@14
   187
                identity->comm_type = stored_identity->comm_type;
vb@14
   188
            }
vb@8
   189
        }
Edouard@439
   190
        else /* !EMPTYSTR(identity->fpr) */ {
Edouard@512
   191
            if (_same_fpr(identity->fpr,
vb@591
   192
                          strlen(identity->fpr),
Edouard@512
   193
                          stored_identity->fpr,
vb@591
   194
                          strlen(stored_identity->fpr))) {
vb@14
   195
                if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
vb@14
   196
                    identity->comm_type = _comm_type_key;
Edouard@488
   197
                }else{
Edouard@488
   198
                    identity->comm_type = stored_identity->comm_type;
Edouard@488
   199
                    if (identity->comm_type == PEP_ct_unknown) {
Edouard@488
   200
                        identity->comm_type = _comm_type_key;
vb@8
   201
                    }
vb@8
   202
                }
vb@496
   203
            } else {
Edouard@488
   204
                status = get_trust(session, identity);
Edouard@488
   205
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@488
   206
                if (status == PEP_OUT_OF_MEMORY)
Edouard@829
   207
                    goto exit_free;
vb@496
   208
                if (identity->comm_type < stored_identity->comm_type)
vb@496
   209
                    identity->comm_type = PEP_ct_unknown;
vb@8
   210
            }
vb@8
   211
        }
vb@8
   212
vb@21
   213
        if (identity->lang[0] == 0) {
vb@21
   214
            identity->lang[0] = stored_identity->lang[0];
vb@21
   215
            identity->lang[1] = stored_identity->lang[1];
vb@21
   216
            identity->lang[2] = 0;
vb@21
   217
        }
vb@932
   218
vb@932
   219
        identity->flags = stored_identity->flags;
vb@21
   220
    }
vb@21
   221
    else /* stored_identity == NULL */ {
vb@934
   222
        identity->flags = 0;
vb@934
   223
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
        }
Edouard@439
   234
        else /* EMPTYSTR(identity->fpr) */ {
Edouard@774
   235
            PEP_STATUS status = elect_pubkey(session, identity);
Edouard@755
   236
            if (status != PEP_STATUS_OK)
Edouard@829
   237
                goto exit_free;
vb@21
   238
        }
vb@21
   239
    }
vb@21
   240
vb@21
   241
    status = PEP_STATUS_OK;
vb@21
   242
Edouard@439
   243
    if (identity->comm_type != PEP_ct_unknown && !EMPTYSTR(identity->user_id)) {
Edouard@439
   244
        assert(!EMPTYSTR(identity->username)); // this should not happen
vb@22
   245
Edouard@439
   246
        if (EMPTYSTR(identity->username)) { // mitigate
vb@0
   247
            free(identity->username);
vb@22
   248
            identity->username = strdup("anonymous");
vb@931
   249
            assert(identity->username);
Edouard@829
   250
            if (identity->username == NULL){
Edouard@829
   251
                status = PEP_OUT_OF_MEMORY;
Edouard@829
   252
                goto exit_free;
Edouard@829
   253
            }
vb@0
   254
        }
vb@8
   255
Edouard@755
   256
        // Identity doesn't get stored if call was just about checking existing
Edouard@755
   257
        // user by address (i.e. no user id given but already stored)
Edouard@559
   258
        if (!(_no_user_id && stored_identity))
Edouard@559
   259
        {
Edouard@559
   260
            status = set_identity(session, identity);
Edouard@559
   261
            assert(status == PEP_STATUS_OK);
Edouard@559
   262
            if (status != PEP_STATUS_OK) {
Edouard@829
   263
                goto exit_free;
Edouard@559
   264
            }
Edouard@499
   265
        }
vb@8
   266
    }
vb@8
   267
vb@297
   268
    if (identity->comm_type != PEP_ct_compromized &&
vb@297
   269
            identity->comm_type < PEP_ct_strong_but_unconfirmed)
vb@297
   270
        if (session->examine_identity)
vb@297
   271
            session->examine_identity(identity, session->examine_management);
vb@297
   272
Edouard@829
   273
exit_free :
Edouard@829
   274
    
Edouard@829
   275
    if (stored_identity){
Edouard@829
   276
        free_identity(stored_identity);
Edouard@829
   277
    }
Edouard@829
   278
vb@8
   279
    return status;
vb@0
   280
}
vb@0
   281
Edouard@774
   282
PEP_STATUS elect_ownkey(
Edouard@774
   283
        PEP_SESSION session, pEp_identity * identity
Edouard@774
   284
    )
Edouard@774
   285
{
Edouard@774
   286
    PEP_STATUS status;
Edouard@774
   287
    stringlist_t *keylist = NULL;
Edouard@774
   288
Edouard@774
   289
    free(identity->fpr);
Edouard@774
   290
    identity->fpr = NULL;
Edouard@774
   291
Edouard@774
   292
    status = find_keys(session, identity->address, &keylist);
Edouard@774
   293
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   294
    if (status == PEP_OUT_OF_MEMORY)
Edouard@774
   295
        return PEP_OUT_OF_MEMORY;
Edouard@774
   296
    
Edouard@774
   297
    if (keylist != NULL && keylist->value != NULL)
Edouard@774
   298
    {
Edouard@774
   299
        char *_fpr = NULL;
Edouard@774
   300
        identity->comm_type = PEP_ct_unknown;
Edouard@774
   301
Edouard@774
   302
        stringlist_t *_keylist;
Edouard@774
   303
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@774
   304
            bool is_own = false;
Edouard@774
   305
            
Edouard@774
   306
            if (session->use_only_own_private_keys)
Edouard@774
   307
            {
Edouard@774
   308
                status = own_key_is_listed(session, _keylist->value, &is_own);
Edouard@774
   309
                assert(status == PEP_STATUS_OK);
Edouard@774
   310
                if (status != PEP_STATUS_OK) {
Edouard@774
   311
                    free_stringlist(keylist);
Edouard@774
   312
                    return status;
Edouard@774
   313
                }
Edouard@774
   314
            }
Edouard@774
   315
Edouard@774
   316
            // TODO : also accept synchronized device group keys ?
Edouard@774
   317
            
Edouard@774
   318
            if (!session->use_only_own_private_keys || is_own)
Edouard@774
   319
            {
Edouard@774
   320
                PEP_comm_type _comm_type_key;
Edouard@774
   321
                
Edouard@774
   322
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@774
   323
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   324
                if (status == PEP_OUT_OF_MEMORY) {
Edouard@774
   325
                    free_stringlist(keylist);
Edouard@774
   326
                    return PEP_OUT_OF_MEMORY;
Edouard@774
   327
                }
Edouard@774
   328
                
Edouard@774
   329
                if (_comm_type_key != PEP_ct_compromized &&
Edouard@774
   330
                    _comm_type_key != PEP_ct_unknown)
Edouard@774
   331
                {
Edouard@774
   332
                    if (identity->comm_type == PEP_ct_unknown ||
Edouard@774
   333
                        _comm_type_key > identity->comm_type)
Edouard@774
   334
                    {
Edouard@774
   335
                        identity->comm_type = _comm_type_key;
Edouard@774
   336
                        _fpr = _keylist->value;
Edouard@774
   337
                    }
Edouard@774
   338
                }
Edouard@774
   339
            }
Edouard@774
   340
        }
Edouard@774
   341
        
Edouard@774
   342
        if (_fpr)
Edouard@774
   343
        {
Edouard@774
   344
            identity->fpr = strdup(_fpr);
Edouard@774
   345
            assert(identity->fpr);
Edouard@774
   346
            if (identity->fpr == NULL)
Edouard@774
   347
            {
Edouard@774
   348
                free_stringlist(keylist);
Edouard@774
   349
                return PEP_OUT_OF_MEMORY;
Edouard@774
   350
            }
Edouard@774
   351
        }
Edouard@774
   352
        free_stringlist(keylist);
Edouard@774
   353
    }
Edouard@774
   354
    return PEP_STATUS_OK;
Edouard@774
   355
}
Edouard@774
   356
vb@0
   357
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
vb@0
   358
{
Edouard@560
   359
    pEp_identity *stored_identity;
vb@0
   360
    PEP_STATUS status;
vb@0
   361
vb@0
   362
    assert(session);
vb@0
   363
    assert(identity);
vb@1044
   364
    assert(!EMPTYSTR(identity->address));
vb@1043
   365
Edouard@658
   366
    assert(EMPTYSTR(identity->user_id) ||
Edouard@658
   367
           strcmp(identity->user_id, PEP_OWN_USERID) == 0);
vb@0
   368
vb@1044
   369
    if (!(session && identity && !EMPTYSTR(identity->address) &&
vb@1043
   370
            (EMPTYSTR(identity->user_id) ||
vb@1043
   371
            strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
vb@191
   372
        return PEP_ILLEGAL_VALUE;
vb@191
   373
vb@0
   374
    identity->comm_type = PEP_ct_pEp;
vb@0
   375
    identity->me = true;
Edouard@658
   376
    
vb@1043
   377
    if (EMPTYSTR(identity->user_id))
Edouard@658
   378
    {
Edouard@658
   379
        free(identity->user_id);
Edouard@658
   380
        identity->user_id = strdup(PEP_OWN_USERID);
Edouard@658
   381
        assert(identity->user_id);
Edouard@658
   382
        if (identity->user_id == NULL)
Edouard@658
   383
            return PEP_OUT_OF_MEMORY;
Edouard@658
   384
    }
vb@0
   385
vb@1044
   386
    DEBUG_LOG("myself", "debug", identity->address);
vb@1044
   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@1140
   450
   
edouard@1140
   451
    bool new_key_generated = false;
edouard@1140
   452
Edouard@695
   453
    if (EMPTYSTR(identity->fpr) || revoked)
Edouard@695
   454
    {        
Edouard@695
   455
        if(revoked)
Edouard@695
   456
        {
Edouard@697
   457
            r_fpr = identity->fpr;
Edouard@697
   458
            identity->fpr = NULL;
Edouard@695
   459
        }
Edouard@560
   460
        
vb@215
   461
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   462
        status = generate_keypair(session, identity);
vb@0
   463
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   464
        if (status != PEP_STATUS_OK) {
vb@0
   465
            char buf[11];
vb@0
   466
            snprintf(buf, 11, "%d", status);
vb@215
   467
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   468
            if(revoked && r_fpr)
Edouard@695
   469
                free(r_fpr);
vb@0
   470
            return status;
vb@0
   471
        }
edouard@1140
   472
edouard@1140
   473
        new_key_generated = true;
Edouard@560
   474
        
Edouard@695
   475
        if(revoked)
Edouard@695
   476
        {
Edouard@695
   477
            status = set_revoked(session, r_fpr,
Edouard@695
   478
                                 identity->fpr, time(NULL));
Edouard@695
   479
            free(r_fpr);
Edouard@695
   480
            if (status != PEP_STATUS_OK) {
Edouard@695
   481
                return status;
Edouard@695
   482
            }
Edouard@371
   483
        }
vb@0
   484
    }
Edouard@560
   485
    else
Edouard@560
   486
    {
vb@214
   487
        bool expired;
Edouard@701
   488
        status = key_expired(session, identity->fpr, 
Edouard@701
   489
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   490
                             &expired);
Edouard@701
   491
vb@214
   492
        assert(status == PEP_STATUS_OK);
Edouard@499
   493
        if (status != PEP_STATUS_OK) {
Edouard@588
   494
            return status;
Edouard@499
   495
        }
vb@214
   496
vb@214
   497
        if (status == PEP_STATUS_OK && expired) {
vb@214
   498
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   499
            renew_key(session, identity->fpr, ts);
vb@214
   500
            free_timestamp(ts);
vb@214
   501
        }
vb@214
   502
    }
vb@0
   503
vb@0
   504
    status = set_identity(session, identity);
vb@0
   505
    assert(status == PEP_STATUS_OK);
Edouard@499
   506
    if (status != PEP_STATUS_OK) {
Edouard@588
   507
        return status;
Edouard@499
   508
    }
vb@0
   509
edouard@1140
   510
    // if a state machine for keysync is in place, inject notify
edouard@1140
   511
    if (session->sync_state != DeviceState_state_NONE)
edouard@1140
   512
        status = fsm_DeviceState_inject(session, KeyGen, NULL, NULL);
edouard@1140
   513
    if (status != PEP_STATUS_OK)
edouard@1140
   514
        return status;
edouard@1140
   515
vb@0
   516
    return PEP_STATUS_OK;
Edouard@499
   517
vb@0
   518
}
vb@0
   519
vb@296
   520
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   521
        PEP_SESSION session, 
vb@292
   522
        examine_identity_t examine_identity,
vb@292
   523
        void *management
vb@292
   524
    )
vb@292
   525
{
vb@292
   526
    assert(session);
vb@292
   527
    if (!session)
vb@292
   528
        return PEP_ILLEGAL_VALUE;
vb@292
   529
vb@292
   530
    session->examine_management = management;
vb@292
   531
    session->examine_identity = examine_identity;
vb@292
   532
vb@292
   533
    return PEP_STATUS_OK;
vb@292
   534
}
vb@292
   535
vb@0
   536
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   537
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   538
        void *management
vb@0
   539
    )
vb@0
   540
{
vb@0
   541
    PEP_SESSION session;
vb@0
   542
    pEp_identity *identity;
Edouard@499
   543
    PEP_STATUS status;
vb@0
   544
vb@24
   545
    assert(retrieve_next_identity);
vb@24
   546
    assert(management);
vb@24
   547
Edouard@499
   548
    if (!retrieve_next_identity || !management)
Edouard@499
   549
        return PEP_ILLEGAL_VALUE;
Edouard@499
   550
Edouard@499
   551
    status = init(&session);
Edouard@499
   552
    assert(status == PEP_STATUS_OK);
Edouard@499
   553
    if (status != PEP_STATUS_OK)
Edouard@499
   554
        return status;
Edouard@499
   555
vb@0
   556
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   557
Edouard@499
   558
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   559
    {
vb@0
   560
        assert(identity->address);
Edouard@499
   561
        if(identity->address)
Edouard@499
   562
        {
Edouard@499
   563
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   564
Edouard@499
   565
            if (identity->me) {
Edouard@499
   566
                status = myself(session, identity);
Edouard@499
   567
            } else {
Edouard@499
   568
                status = recv_key(session, identity->address);
Edouard@499
   569
            }
Edouard@499
   570
vb@0
   571
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   572
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   573
                return PEP_OUT_OF_MEMORY;
vb@0
   574
        }
vb@0
   575
        free_identity(identity);
vb@0
   576
    }
vb@0
   577
vb@0
   578
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   579
vb@0
   580
    release(session);
vb@0
   581
    return PEP_STATUS_OK;
vb@0
   582
}
vb@0
   583
vb@357
   584
DYNAMIC_API PEP_STATUS key_compromized(
vb@357
   585
        PEP_SESSION session,
vb@357
   586
        pEp_identity *ident
vb@357
   587
    )
vb@215
   588
{
vb@218
   589
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   590
vb@215
   591
    assert(session);
vb@357
   592
    assert(ident);
Edouard@439
   593
    assert(!EMPTYSTR(ident->fpr));
vb@215
   594
vb@357
   595
    if (!(session && ident && ident->fpr))
vb@215
   596
        return PEP_ILLEGAL_VALUE;
vb@215
   597
vb@357
   598
    if (ident->me)
Edouard@697
   599
    {
vb@357
   600
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   601
        myself(session, ident);
Edouard@697
   602
    }
Edouard@697
   603
    else
Edouard@697
   604
    {
Edouard@697
   605
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
   606
    }
vb@218
   607
vb@218
   608
    return status;
vb@215
   609
}
vb@215
   610
Edouard@410
   611
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   612
        PEP_SESSION session,
Edouard@410
   613
        pEp_identity *ident
Edouard@410
   614
    )
Edouard@410
   615
{
Edouard@410
   616
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   617
Edouard@410
   618
    assert(session);
Edouard@410
   619
    assert(ident);
vb@420
   620
    assert(!ident->me);
Edouard@439
   621
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   622
    assert(!EMPTYSTR(ident->address));
Edouard@439
   623
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   624
vb@420
   625
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   626
            ident->user_id))
Edouard@410
   627
        return PEP_ILLEGAL_VALUE;
Edouard@410
   628
vb@420
   629
    status = update_identity(session, ident);
vb@420
   630
    if (status != PEP_STATUS_OK)
vb@420
   631
        return status;
Edouard@410
   632
Edouard@442
   633
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   634
        ident->comm_type = PEP_ct_unknown;
vb@421
   635
    else
vb@421
   636
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   637
vb@420
   638
    status = set_identity(session, ident);
vb@421
   639
    if (status != PEP_STATUS_OK)
vb@421
   640
        return status;
vb@421
   641
vb@422
   642
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   643
        status = update_identity(session, ident);
Edouard@410
   644
    return status;
Edouard@410
   645
}
Edouard@410
   646
vb@354
   647
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   648
        PEP_SESSION session,
vb@354
   649
        pEp_identity *ident
vb@354
   650
    )
vb@354
   651
{
vb@354
   652
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   653
vb@354
   654
    assert(session);
vb@354
   655
    assert(ident);
Edouard@439
   656
    assert(!EMPTYSTR(ident->address));
Edouard@439
   657
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   658
    assert(!EMPTYSTR(ident->fpr));
vb@354
   659
    assert(!ident->me);
vb@354
   660
Edouard@439
   661
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   662
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   663
        return PEP_ILLEGAL_VALUE;
vb@354
   664
vb@354
   665
    status = update_identity(session, ident);
vb@354
   666
    if (status != PEP_STATUS_OK)
vb@354
   667
        return status;
vb@354
   668
vb@356
   669
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   670
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   671
        status = set_identity(session, ident);
vb@356
   672
    }
vb@356
   673
    else {
vb@356
   674
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   675
        status = PEP_CANNOT_SET_TRUST;
vb@356
   676
    }
vb@354
   677
vb@354
   678
    return status;
vb@354
   679
}
vb@354
   680
Edouard@584
   681
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
   682
        PEP_SESSION session,
vb@955
   683
        const char *fpr,
vb@955
   684
        bool *listed
vb@955
   685
    )
Edouard@584
   686
{
Edouard@584
   687
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   688
    int count;
Edouard@584
   689
    
Edouard@584
   690
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   691
    
Edouard@584
   692
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   693
        return PEP_ILLEGAL_VALUE;
Edouard@584
   694
    
Edouard@584
   695
    *listed = false;
Edouard@584
   696
    
Edouard@584
   697
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   698
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   699
    
Edouard@584
   700
    int result;
Edouard@584
   701
    
Edouard@584
   702
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   703
    switch (result) {
Edouard@584
   704
        case SQLITE_ROW:
Edouard@584
   705
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   706
            *listed = count > 0;
Edouard@584
   707
            status = PEP_STATUS_OK;
Edouard@584
   708
            break;
Edouard@584
   709
            
Edouard@584
   710
        default:
Edouard@584
   711
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   712
    }
Edouard@584
   713
    
Edouard@584
   714
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   715
    return status;
Edouard@584
   716
}
Edouard@584
   717
vb@955
   718
DYNAMIC_API PEP_STATUS own_identities_retrieve(
vb@955
   719
        PEP_SESSION session,
vb@955
   720
        identity_list **own_identities
vb@955
   721
      )
Edouard@584
   722
{
Edouard@584
   723
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   724
    
vb@955
   725
    assert(session && own_identities);
vb@955
   726
    if (!(session && own_identities))
Edouard@584
   727
        return PEP_ILLEGAL_VALUE;
Edouard@584
   728
    
vb@955
   729
    *own_identities = NULL;
vb@955
   730
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
   731
    if (_own_identities == NULL)
Edouard@584
   732
        goto enomem;
Edouard@584
   733
    
vb@955
   734
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   735
    
Edouard@584
   736
    int result;
vb@955
   737
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
   738
    const char *address = NULL;
Edouard@584
   739
    const char *fpr = NULL;
vb@955
   740
    const char *username = NULL;
vb@955
   741
    const char *user_id = NULL;
vb@955
   742
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
   743
    const char *lang = NULL;
vb@955
   744
    unsigned int flags = 0;
Edouard@584
   745
    
vb@955
   746
    identity_list *_bl = _own_identities;
Edouard@584
   747
    do {
vb@955
   748
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
   749
        switch (result) {
Edouard@584
   750
            case SQLITE_ROW:
vb@955
   751
                address = (const char *)
vb@955
   752
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
   753
                fpr = (const char *)
vb@955
   754
                    sqlite3_column_text(session->own_identities_retrieve, 1);
vb@1071
   755
                user_id = PEP_OWN_USERID;
vb@1071
   756
                username = (const char *)
vb@955
   757
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
   758
                comm_type = PEP_ct_pEp;
vb@1071
   759
                lang = (const char *)
vb@955
   760
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
   761
                flags = (unsigned int)
vb@955
   762
                    sqlite3_column_int(session->own_key_is_listed, 4);
vb@955
   763
vb@1078
   764
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
   765
                if (!ident)
vb@1079
   766
                    goto enomem;
vb@955
   767
                ident->comm_type = comm_type;
vb@955
   768
                if (lang && lang[0]) {
vb@955
   769
                    ident->lang[0] = lang[0];
vb@955
   770
                    ident->lang[1] = lang[1];
vb@1078
   771
                    ident->lang[2] = 0;
vb@955
   772
                }
vb@1068
   773
                ident->me = true;
vb@955
   774
                ident->flags = flags;
vb@955
   775
vb@955
   776
                _bl = identity_list_add(_bl, ident);
vb@1080
   777
                if (_bl == NULL) {
vb@1080
   778
                    free_identity(ident);
Edouard@584
   779
                    goto enomem;
vb@1080
   780
                }
Edouard@584
   781
                
Edouard@584
   782
                break;
Edouard@584
   783
                
Edouard@584
   784
            case SQLITE_DONE:
Edouard@584
   785
                break;
Edouard@584
   786
                
Edouard@584
   787
            default:
Edouard@584
   788
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   789
                result = SQLITE_DONE;
Edouard@584
   790
        }
Edouard@584
   791
    } while (result != SQLITE_DONE);
Edouard@584
   792
    
vb@955
   793
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   794
    if (status == PEP_STATUS_OK)
vb@955
   795
        *own_identities = _own_identities;
Edouard@584
   796
    else
vb@955
   797
        free_identity_list(_own_identities);
Edouard@584
   798
    
Edouard@584
   799
    goto the_end;
Edouard@584
   800
    
Edouard@584
   801
enomem:
vb@955
   802
    free_identity_list(_own_identities);
Edouard@584
   803
    status = PEP_OUT_OF_MEMORY;
Edouard@584
   804
    
Edouard@584
   805
the_end:
Edouard@584
   806
    return status;
Edouard@584
   807
}
vb@955
   808