src/keymanagement.c
author Edouard Tisserant
Wed, 18 May 2016 15:11:39 +0200
changeset 658 244734164566
parent 591 a3dbbf610d32
child 686 7ebd63dbedca
permissions -rw-r--r--
Remove own_keys table, added PEP_OWN_USERID, unique person userId for any own identities and keys.
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
Edouard@560
   307
        free(identity->fpr);
Edouard@560
   308
        
Edouard@560
   309
        status = find_keys(session, identity->address, &keylist);
Edouard@560
   310
        assert(status != PEP_OUT_OF_MEMORY);
Edouard@560
   311
        if (status == PEP_OUT_OF_MEMORY)
Edouard@560
   312
            return PEP_OUT_OF_MEMORY;
Edouard@560
   313
        
Edouard@560
   314
        if (keylist != NULL && keylist->value != NULL)
Edouard@560
   315
        {
Edouard@588
   316
            char *_fpr = NULL;
Edouard@588
   317
            identity->comm_type = PEP_ct_unknown;
Edouard@588
   318
Edouard@588
   319
            stringlist_t *_keylist;
Edouard@588
   320
            for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@588
   321
                bool is_own = false;
Edouard@588
   322
                
Edouard@588
   323
                status = own_key_is_listed(session, _keylist->value, &is_own);
Edouard@588
   324
                assert(status == PEP_STATUS_OK);
Edouard@588
   325
                if (status != PEP_STATUS_OK) {
Edouard@588
   326
                    free_stringlist(keylist);
Edouard@588
   327
                    return status;
Edouard@588
   328
                }
Edouard@588
   329
Edouard@588
   330
                // TODO : also accept synchronized device group keys ?
Edouard@588
   331
                
Edouard@588
   332
                if (is_own)
Edouard@588
   333
                {
Edouard@588
   334
                    PEP_comm_type _comm_type_key;
Edouard@588
   335
                    
Edouard@588
   336
                    status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@588
   337
                    assert(status != PEP_OUT_OF_MEMORY);
Edouard@588
   338
                    if (status == PEP_OUT_OF_MEMORY) {
Edouard@588
   339
                        free_stringlist(keylist);
Edouard@588
   340
                        return PEP_OUT_OF_MEMORY;
Edouard@588
   341
                    }
Edouard@588
   342
                    
Edouard@588
   343
                    if (_comm_type_key != PEP_ct_compromized &&
Edouard@588
   344
                        _comm_type_key != PEP_ct_unknown)
Edouard@588
   345
                    {
Edouard@588
   346
                        if (identity->comm_type == PEP_ct_unknown ||
Edouard@588
   347
                            _comm_type_key > identity->comm_type)
Edouard@588
   348
                        {
Edouard@588
   349
                            identity->comm_type = _comm_type_key;
Edouard@588
   350
                            _fpr = _keylist->value;
Edouard@588
   351
                        }
Edouard@588
   352
                    }
Edouard@588
   353
                }
Edouard@588
   354
            }
Edouard@560
   355
            
Edouard@588
   356
            if (_fpr)
Edouard@560
   357
            {
Edouard@588
   358
                identity->fpr = strdup(_fpr);
Edouard@588
   359
                assert(identity->fpr);
Edouard@588
   360
                if (identity->fpr == NULL)
Edouard@588
   361
                {
Edouard@588
   362
                    free_stringlist(keylist);
Edouard@588
   363
                    return PEP_OUT_OF_MEMORY;
Edouard@588
   364
                }
Edouard@560
   365
            }
Edouard@588
   366
            free_stringlist(keylist);
Edouard@560
   367
        }
Edouard@560
   368
    }
Edouard@560
   369
    
Edouard@560
   370
    // TODO : Check key for revoked state
Edouard@560
   371
    
Edouard@560
   372
    if (EMPTYSTR(identity->fpr) /* or revoked */)
Edouard@560
   373
    {
Edouard@588
   374
        stringlist_t *keylist = NULL;
Edouard@560
   375
        
vb@215
   376
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   377
        status = generate_keypair(session, identity);
vb@0
   378
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   379
        if (status != PEP_STATUS_OK) {
vb@0
   380
            char buf[11];
vb@0
   381
            snprintf(buf, 11, "%d", status);
vb@215
   382
            DEBUG_LOG("generating key pair failed", "debug", buf);
vb@0
   383
            return status;
vb@0
   384
        }
Edouard@560
   385
        
vb@0
   386
        status = find_keys(session, identity->address, &keylist);
vb@0
   387
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   388
        if (status == PEP_OUT_OF_MEMORY)
vb@0
   389
            return PEP_OUT_OF_MEMORY;
Edouard@560
   390
        
vb@372
   391
        assert(keylist && keylist->value);
Edouard@588
   392
        if (keylist == NULL) {
Edouard@588
   393
            return PEP_UNKNOWN_ERROR;
Edouard@588
   394
        }else if (keylist->value == NULL) {
Edouard@588
   395
            free_stringlist(keylist);
Edouard@371
   396
            return PEP_UNKNOWN_ERROR;
Edouard@371
   397
        }
vb@0
   398
    }
Edouard@560
   399
    else
Edouard@560
   400
    {
vb@214
   401
        bool expired;
Edouard@560
   402
        status = key_expired(session, identity->fpr, &expired);
vb@214
   403
        assert(status == PEP_STATUS_OK);
Edouard@499
   404
        if (status != PEP_STATUS_OK) {
Edouard@588
   405
            return status;
Edouard@499
   406
        }
vb@214
   407
vb@214
   408
        if (status == PEP_STATUS_OK && expired) {
vb@214
   409
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   410
            renew_key(session, identity->fpr, ts);
vb@214
   411
            free_timestamp(ts);
vb@214
   412
        }
vb@214
   413
    }
vb@0
   414
vb@0
   415
    status = set_identity(session, identity);
vb@0
   416
    assert(status == PEP_STATUS_OK);
Edouard@499
   417
    if (status != PEP_STATUS_OK) {
Edouard@588
   418
        return status;
Edouard@499
   419
    }
vb@0
   420
vb@0
   421
    return PEP_STATUS_OK;
Edouard@499
   422
vb@0
   423
}
vb@0
   424
vb@296
   425
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   426
        PEP_SESSION session, 
vb@292
   427
        examine_identity_t examine_identity,
vb@292
   428
        void *management
vb@292
   429
    )
vb@292
   430
{
vb@292
   431
    assert(session);
vb@292
   432
    if (!session)
vb@292
   433
        return PEP_ILLEGAL_VALUE;
vb@292
   434
vb@292
   435
    session->examine_management = management;
vb@292
   436
    session->examine_identity = examine_identity;
vb@292
   437
vb@292
   438
    return PEP_STATUS_OK;
vb@292
   439
}
vb@292
   440
vb@0
   441
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   442
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   443
        void *management
vb@0
   444
    )
vb@0
   445
{
vb@0
   446
    PEP_SESSION session;
vb@0
   447
    pEp_identity *identity;
Edouard@499
   448
    PEP_STATUS status;
vb@0
   449
vb@24
   450
    assert(retrieve_next_identity);
vb@24
   451
    assert(management);
vb@24
   452
Edouard@499
   453
    if (!retrieve_next_identity || !management)
Edouard@499
   454
        return PEP_ILLEGAL_VALUE;
Edouard@499
   455
Edouard@499
   456
    status = init(&session);
Edouard@499
   457
    assert(status == PEP_STATUS_OK);
Edouard@499
   458
    if (status != PEP_STATUS_OK)
Edouard@499
   459
        return status;
Edouard@499
   460
vb@0
   461
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   462
Edouard@499
   463
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   464
    {
vb@0
   465
        assert(identity->address);
Edouard@499
   466
        if(identity->address)
Edouard@499
   467
        {
Edouard@499
   468
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   469
Edouard@499
   470
            if (identity->me) {
Edouard@499
   471
                status = myself(session, identity);
Edouard@499
   472
            } else {
Edouard@499
   473
                status = recv_key(session, identity->address);
Edouard@499
   474
            }
Edouard@499
   475
vb@0
   476
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   477
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   478
                return PEP_OUT_OF_MEMORY;
vb@0
   479
        }
vb@0
   480
        free_identity(identity);
vb@0
   481
    }
vb@0
   482
vb@0
   483
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   484
vb@0
   485
    release(session);
vb@0
   486
    return PEP_STATUS_OK;
vb@0
   487
}
vb@0
   488
vb@357
   489
DYNAMIC_API PEP_STATUS key_compromized(
vb@357
   490
        PEP_SESSION session,
vb@357
   491
        pEp_identity *ident
vb@357
   492
    )
vb@215
   493
{
vb@218
   494
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   495
vb@215
   496
    assert(session);
vb@357
   497
    assert(ident);
Edouard@439
   498
    assert(!EMPTYSTR(ident->fpr));
vb@215
   499
vb@357
   500
    if (!(session && ident && ident->fpr))
vb@215
   501
        return PEP_ILLEGAL_VALUE;
vb@215
   502
vb@357
   503
    if (ident->me)
vb@357
   504
        revoke_key(session, ident->fpr, NULL);
vb@357
   505
    status = mark_as_compromized(session, ident->fpr);
vb@218
   506
vb@218
   507
    return status;
vb@215
   508
}
vb@215
   509
Edouard@410
   510
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   511
        PEP_SESSION session,
Edouard@410
   512
        pEp_identity *ident
Edouard@410
   513
    )
Edouard@410
   514
{
Edouard@410
   515
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   516
Edouard@410
   517
    assert(session);
Edouard@410
   518
    assert(ident);
vb@420
   519
    assert(!ident->me);
Edouard@439
   520
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   521
    assert(!EMPTYSTR(ident->address));
Edouard@439
   522
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   523
vb@420
   524
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   525
            ident->user_id))
Edouard@410
   526
        return PEP_ILLEGAL_VALUE;
Edouard@410
   527
vb@420
   528
    status = update_identity(session, ident);
vb@420
   529
    if (status != PEP_STATUS_OK)
vb@420
   530
        return status;
Edouard@410
   531
Edouard@442
   532
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   533
        ident->comm_type = PEP_ct_unknown;
vb@421
   534
    else
vb@421
   535
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   536
vb@420
   537
    status = set_identity(session, ident);
vb@421
   538
    if (status != PEP_STATUS_OK)
vb@421
   539
        return status;
vb@421
   540
vb@422
   541
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   542
        status = update_identity(session, ident);
Edouard@410
   543
    return status;
Edouard@410
   544
}
Edouard@410
   545
vb@354
   546
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   547
        PEP_SESSION session,
vb@354
   548
        pEp_identity *ident
vb@354
   549
    )
vb@354
   550
{
vb@354
   551
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   552
vb@354
   553
    assert(session);
vb@354
   554
    assert(ident);
Edouard@439
   555
    assert(!EMPTYSTR(ident->address));
Edouard@439
   556
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   557
    assert(!EMPTYSTR(ident->fpr));
vb@354
   558
    assert(!ident->me);
vb@354
   559
Edouard@439
   560
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   561
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   562
        return PEP_ILLEGAL_VALUE;
vb@354
   563
vb@354
   564
    status = update_identity(session, ident);
vb@354
   565
    if (status != PEP_STATUS_OK)
vb@354
   566
        return status;
vb@354
   567
vb@356
   568
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   569
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   570
        status = set_identity(session, ident);
vb@356
   571
    }
vb@356
   572
    else {
vb@356
   573
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   574
        status = PEP_CANNOT_SET_TRUST;
vb@356
   575
    }
vb@354
   576
vb@354
   577
    return status;
vb@354
   578
}
vb@354
   579
Edouard@584
   580
DYNAMIC_API PEP_STATUS own_key_is_listed(
Edouard@584
   581
                                           PEP_SESSION session,
Edouard@584
   582
                                           const char *fpr,
Edouard@584
   583
                                           bool *listed
Edouard@584
   584
                                           )
Edouard@584
   585
{
Edouard@584
   586
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   587
    int count;
Edouard@584
   588
    
Edouard@584
   589
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   590
    
Edouard@584
   591
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   592
        return PEP_ILLEGAL_VALUE;
Edouard@584
   593
    
Edouard@584
   594
    *listed = false;
Edouard@584
   595
    
Edouard@584
   596
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   597
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   598
    
Edouard@584
   599
    int result;
Edouard@584
   600
    
Edouard@584
   601
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   602
    switch (result) {
Edouard@584
   603
        case SQLITE_ROW:
Edouard@584
   604
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   605
            *listed = count > 0;
Edouard@584
   606
            status = PEP_STATUS_OK;
Edouard@584
   607
            break;
Edouard@584
   608
            
Edouard@584
   609
        default:
Edouard@584
   610
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   611
    }
Edouard@584
   612
    
Edouard@584
   613
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   614
    return status;
Edouard@584
   615
}
Edouard@584
   616
Edouard@584
   617
DYNAMIC_API PEP_STATUS own_key_retrieve(
Edouard@584
   618
                                          PEP_SESSION session,
Edouard@584
   619
                                          stringlist_t **own_key
Edouard@584
   620
                                          )
Edouard@584
   621
{
Edouard@584
   622
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   623
    
Edouard@584
   624
    assert(session);
Edouard@584
   625
    assert(own_key);
Edouard@584
   626
    
Edouard@584
   627
    if (!(session && own_key))
Edouard@584
   628
        return PEP_ILLEGAL_VALUE;
Edouard@584
   629
    
Edouard@584
   630
    *own_key = NULL;
Edouard@584
   631
    stringlist_t *_own_key = new_stringlist(NULL);
Edouard@584
   632
    if (_own_key == NULL)
Edouard@584
   633
        goto enomem;
Edouard@584
   634
    
Edouard@584
   635
    sqlite3_reset(session->own_key_retrieve);
Edouard@584
   636
    
Edouard@584
   637
    int result;
Edouard@584
   638
    const char *fpr = NULL;
Edouard@584
   639
    
Edouard@584
   640
    stringlist_t *_bl = _own_key;
Edouard@584
   641
    do {
Edouard@584
   642
        result = sqlite3_step(session->own_key_retrieve);
Edouard@584
   643
        switch (result) {
Edouard@584
   644
            case SQLITE_ROW:
Edouard@584
   645
                fpr = (const char *) sqlite3_column_text(session->own_key_retrieve, 0);
Edouard@584
   646
                
Edouard@584
   647
                _bl = stringlist_add(_bl, fpr);
Edouard@584
   648
                if (_bl == NULL)
Edouard@584
   649
                    goto enomem;
Edouard@584
   650
                
Edouard@584
   651
                break;
Edouard@584
   652
                
Edouard@584
   653
            case SQLITE_DONE:
Edouard@584
   654
                break;
Edouard@584
   655
                
Edouard@584
   656
            default:
Edouard@584
   657
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   658
                result = SQLITE_DONE;
Edouard@584
   659
        }
Edouard@584
   660
    } while (result != SQLITE_DONE);
Edouard@584
   661
    
Edouard@584
   662
    sqlite3_reset(session->own_key_retrieve);
Edouard@584
   663
    if (status == PEP_STATUS_OK)
Edouard@584
   664
        *own_key = _own_key;
Edouard@584
   665
    else
Edouard@584
   666
        free_stringlist(_own_key);
Edouard@584
   667
    
Edouard@584
   668
    goto the_end;
Edouard@584
   669
    
Edouard@584
   670
enomem:
Edouard@584
   671
    free_stringlist(_own_key);
Edouard@584
   672
    status = PEP_OUT_OF_MEMORY;
Edouard@584
   673
    
Edouard@584
   674
the_end:
Edouard@584
   675
    return status;
Edouard@584
   676
}