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