src/keymanagement.c
author Edouard Tisserant <edouard@pep-project.org>
Tue, 06 Sep 2016 13:45:06 +0200
branchkeysync
changeset 1145 78f8723ccaba
parent 1140 c11cfcd37237
child 1169 7d666711da67
permissions -rw-r--r--
KeyGen state injection should happen only when key have been generated...
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@1145
   510
    if(new_key_generated)
edouard@1145
   511
    {
edouard@1145
   512
        // if a state machine for keysync is in place, inject notify
edouard@1145
   513
        if (session->sync_state != DeviceState_state_NONE)
edouard@1145
   514
            status = fsm_DeviceState_inject(session, KeyGen, NULL, NULL);
edouard@1145
   515
        if (status != PEP_STATUS_OK)
edouard@1145
   516
            return status;
edouard@1145
   517
    }
edouard@1140
   518
vb@0
   519
    return PEP_STATUS_OK;
Edouard@499
   520
vb@0
   521
}
vb@0
   522
vb@296
   523
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   524
        PEP_SESSION session, 
vb@292
   525
        examine_identity_t examine_identity,
vb@292
   526
        void *management
vb@292
   527
    )
vb@292
   528
{
vb@292
   529
    assert(session);
vb@292
   530
    if (!session)
vb@292
   531
        return PEP_ILLEGAL_VALUE;
vb@292
   532
vb@292
   533
    session->examine_management = management;
vb@292
   534
    session->examine_identity = examine_identity;
vb@292
   535
vb@292
   536
    return PEP_STATUS_OK;
vb@292
   537
}
vb@292
   538
vb@0
   539
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   540
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   541
        void *management
vb@0
   542
    )
vb@0
   543
{
vb@0
   544
    PEP_SESSION session;
vb@0
   545
    pEp_identity *identity;
Edouard@499
   546
    PEP_STATUS status;
vb@0
   547
vb@24
   548
    assert(retrieve_next_identity);
vb@24
   549
    assert(management);
vb@24
   550
Edouard@499
   551
    if (!retrieve_next_identity || !management)
Edouard@499
   552
        return PEP_ILLEGAL_VALUE;
Edouard@499
   553
Edouard@499
   554
    status = init(&session);
Edouard@499
   555
    assert(status == PEP_STATUS_OK);
Edouard@499
   556
    if (status != PEP_STATUS_OK)
Edouard@499
   557
        return status;
Edouard@499
   558
vb@0
   559
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   560
Edouard@499
   561
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   562
    {
vb@0
   563
        assert(identity->address);
Edouard@499
   564
        if(identity->address)
Edouard@499
   565
        {
Edouard@499
   566
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   567
Edouard@499
   568
            if (identity->me) {
Edouard@499
   569
                status = myself(session, identity);
Edouard@499
   570
            } else {
Edouard@499
   571
                status = recv_key(session, identity->address);
Edouard@499
   572
            }
Edouard@499
   573
vb@0
   574
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   575
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   576
                return PEP_OUT_OF_MEMORY;
vb@0
   577
        }
vb@0
   578
        free_identity(identity);
vb@0
   579
    }
vb@0
   580
vb@0
   581
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   582
vb@0
   583
    release(session);
vb@0
   584
    return PEP_STATUS_OK;
vb@0
   585
}
vb@0
   586
vb@357
   587
DYNAMIC_API PEP_STATUS key_compromized(
vb@357
   588
        PEP_SESSION session,
vb@357
   589
        pEp_identity *ident
vb@357
   590
    )
vb@215
   591
{
vb@218
   592
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   593
vb@215
   594
    assert(session);
vb@357
   595
    assert(ident);
Edouard@439
   596
    assert(!EMPTYSTR(ident->fpr));
vb@215
   597
vb@357
   598
    if (!(session && ident && ident->fpr))
vb@215
   599
        return PEP_ILLEGAL_VALUE;
vb@215
   600
vb@357
   601
    if (ident->me)
Edouard@697
   602
    {
vb@357
   603
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   604
        myself(session, ident);
Edouard@697
   605
    }
Edouard@697
   606
    else
Edouard@697
   607
    {
Edouard@697
   608
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
   609
    }
vb@218
   610
vb@218
   611
    return status;
vb@215
   612
}
vb@215
   613
Edouard@410
   614
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   615
        PEP_SESSION session,
Edouard@410
   616
        pEp_identity *ident
Edouard@410
   617
    )
Edouard@410
   618
{
Edouard@410
   619
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   620
Edouard@410
   621
    assert(session);
Edouard@410
   622
    assert(ident);
vb@420
   623
    assert(!ident->me);
Edouard@439
   624
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   625
    assert(!EMPTYSTR(ident->address));
Edouard@439
   626
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   627
vb@420
   628
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   629
            ident->user_id))
Edouard@410
   630
        return PEP_ILLEGAL_VALUE;
Edouard@410
   631
vb@420
   632
    status = update_identity(session, ident);
vb@420
   633
    if (status != PEP_STATUS_OK)
vb@420
   634
        return status;
Edouard@410
   635
Edouard@442
   636
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   637
        ident->comm_type = PEP_ct_unknown;
vb@421
   638
    else
vb@421
   639
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   640
vb@420
   641
    status = set_identity(session, ident);
vb@421
   642
    if (status != PEP_STATUS_OK)
vb@421
   643
        return status;
vb@421
   644
vb@422
   645
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   646
        status = update_identity(session, ident);
Edouard@410
   647
    return status;
Edouard@410
   648
}
Edouard@410
   649
vb@354
   650
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   651
        PEP_SESSION session,
vb@354
   652
        pEp_identity *ident
vb@354
   653
    )
vb@354
   654
{
vb@354
   655
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   656
vb@354
   657
    assert(session);
vb@354
   658
    assert(ident);
Edouard@439
   659
    assert(!EMPTYSTR(ident->address));
Edouard@439
   660
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   661
    assert(!EMPTYSTR(ident->fpr));
vb@354
   662
    assert(!ident->me);
vb@354
   663
Edouard@439
   664
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   665
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   666
        return PEP_ILLEGAL_VALUE;
vb@354
   667
vb@354
   668
    status = update_identity(session, ident);
vb@354
   669
    if (status != PEP_STATUS_OK)
vb@354
   670
        return status;
vb@354
   671
vb@356
   672
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   673
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   674
        status = set_identity(session, ident);
vb@356
   675
    }
vb@356
   676
    else {
vb@356
   677
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   678
        status = PEP_CANNOT_SET_TRUST;
vb@356
   679
    }
vb@354
   680
vb@354
   681
    return status;
vb@354
   682
}
vb@354
   683
Edouard@584
   684
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
   685
        PEP_SESSION session,
vb@955
   686
        const char *fpr,
vb@955
   687
        bool *listed
vb@955
   688
    )
Edouard@584
   689
{
Edouard@584
   690
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   691
    int count;
Edouard@584
   692
    
Edouard@584
   693
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   694
    
Edouard@584
   695
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   696
        return PEP_ILLEGAL_VALUE;
Edouard@584
   697
    
Edouard@584
   698
    *listed = false;
Edouard@584
   699
    
Edouard@584
   700
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   701
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   702
    
Edouard@584
   703
    int result;
Edouard@584
   704
    
Edouard@584
   705
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   706
    switch (result) {
Edouard@584
   707
        case SQLITE_ROW:
Edouard@584
   708
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   709
            *listed = count > 0;
Edouard@584
   710
            status = PEP_STATUS_OK;
Edouard@584
   711
            break;
Edouard@584
   712
            
Edouard@584
   713
        default:
Edouard@584
   714
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   715
    }
Edouard@584
   716
    
Edouard@584
   717
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   718
    return status;
Edouard@584
   719
}
Edouard@584
   720
vb@955
   721
DYNAMIC_API PEP_STATUS own_identities_retrieve(
vb@955
   722
        PEP_SESSION session,
vb@955
   723
        identity_list **own_identities
vb@955
   724
      )
Edouard@584
   725
{
Edouard@584
   726
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   727
    
vb@955
   728
    assert(session && own_identities);
vb@955
   729
    if (!(session && own_identities))
Edouard@584
   730
        return PEP_ILLEGAL_VALUE;
Edouard@584
   731
    
vb@955
   732
    *own_identities = NULL;
vb@955
   733
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
   734
    if (_own_identities == NULL)
Edouard@584
   735
        goto enomem;
Edouard@584
   736
    
vb@955
   737
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   738
    
Edouard@584
   739
    int result;
vb@955
   740
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
   741
    const char *address = NULL;
Edouard@584
   742
    const char *fpr = NULL;
vb@955
   743
    const char *username = NULL;
vb@955
   744
    const char *user_id = NULL;
vb@955
   745
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
   746
    const char *lang = NULL;
vb@955
   747
    unsigned int flags = 0;
Edouard@584
   748
    
vb@955
   749
    identity_list *_bl = _own_identities;
Edouard@584
   750
    do {
vb@955
   751
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
   752
        switch (result) {
Edouard@584
   753
            case SQLITE_ROW:
vb@955
   754
                address = (const char *)
vb@955
   755
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
   756
                fpr = (const char *)
vb@955
   757
                    sqlite3_column_text(session->own_identities_retrieve, 1);
vb@1071
   758
                user_id = PEP_OWN_USERID;
vb@1071
   759
                username = (const char *)
vb@955
   760
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
   761
                comm_type = PEP_ct_pEp;
vb@1071
   762
                lang = (const char *)
vb@955
   763
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
   764
                flags = (unsigned int)
vb@955
   765
                    sqlite3_column_int(session->own_key_is_listed, 4);
vb@955
   766
vb@1078
   767
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
   768
                if (!ident)
vb@1079
   769
                    goto enomem;
vb@955
   770
                ident->comm_type = comm_type;
vb@955
   771
                if (lang && lang[0]) {
vb@955
   772
                    ident->lang[0] = lang[0];
vb@955
   773
                    ident->lang[1] = lang[1];
vb@1078
   774
                    ident->lang[2] = 0;
vb@955
   775
                }
vb@1068
   776
                ident->me = true;
vb@955
   777
                ident->flags = flags;
vb@955
   778
vb@955
   779
                _bl = identity_list_add(_bl, ident);
vb@1080
   780
                if (_bl == NULL) {
vb@1080
   781
                    free_identity(ident);
Edouard@584
   782
                    goto enomem;
vb@1080
   783
                }
Edouard@584
   784
                
Edouard@584
   785
                break;
Edouard@584
   786
                
Edouard@584
   787
            case SQLITE_DONE:
Edouard@584
   788
                break;
Edouard@584
   789
                
Edouard@584
   790
            default:
Edouard@584
   791
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   792
                result = SQLITE_DONE;
Edouard@584
   793
        }
Edouard@584
   794
    } while (result != SQLITE_DONE);
Edouard@584
   795
    
vb@955
   796
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   797
    if (status == PEP_STATUS_OK)
vb@955
   798
        *own_identities = _own_identities;
Edouard@584
   799
    else
vb@955
   800
        free_identity_list(_own_identities);
Edouard@584
   801
    
Edouard@584
   802
    goto the_end;
Edouard@584
   803
    
Edouard@584
   804
enomem:
vb@955
   805
    free_identity_list(_own_identities);
Edouard@584
   806
    status = PEP_OUT_OF_MEMORY;
Edouard@584
   807
    
Edouard@584
   808
the_end:
Edouard@584
   809
    return status;
Edouard@584
   810
}
vb@955
   811