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