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