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