src/keymanagement.c
author Edouard Tisserant <edouard@pep-project.org>
Tue, 20 Dec 2016 22:51:41 +0100
changeset 1506 fc7d96185247
parent 1502 ab5de5449d7d
child 1508 ab010a3b7894
child 1533 ca90b6245a51
permissions -rw-r--r--
ENGINE-137 (fixing side effect of optimization) update_identity: added key re-election when a key has been deleted from keyring. fixed test blacklist_accept_new_key_test using that feature
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@1195
    12
#include "sync_fsm.h"
krista@1253
    13
#include "blacklist.h"
edouard@1195
    14
Edouard@439
    15
#ifndef EMPTYSTR
roker@500
    16
#define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
vb@8
    17
#endif
vb@8
    18
vb@214
    19
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
vb@214
    20
Edouard@774
    21
PEP_STATUS elect_pubkey(
Edouard@755
    22
        PEP_SESSION session, pEp_identity * identity
Edouard@755
    23
    )
Edouard@755
    24
{
Edouard@755
    25
    PEP_STATUS status;
Edouard@755
    26
    stringlist_t *keylist;
krista@1342
    27
    char *_fpr = "";
Edouard@755
    28
    identity->comm_type = PEP_ct_unknown;
Edouard@755
    29
Edouard@755
    30
    status = find_keys(session, identity->address, &keylist);
Edouard@755
    31
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    32
    if (status == PEP_OUT_OF_MEMORY)
Edouard@755
    33
        return PEP_OUT_OF_MEMORY;
Edouard@755
    34
Edouard@755
    35
    stringlist_t *_keylist;
Edouard@755
    36
    for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@755
    37
        PEP_comm_type _comm_type_key;
Edouard@755
    38
Edouard@755
    39
        status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@755
    40
        assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    41
        if (status == PEP_OUT_OF_MEMORY) {
Edouard@755
    42
            free_stringlist(keylist);
Edouard@755
    43
            return PEP_OUT_OF_MEMORY;
Edouard@755
    44
        }
Edouard@755
    45
Edouard@755
    46
        if (_comm_type_key != PEP_ct_compromized &&
Edouard@755
    47
            _comm_type_key != PEP_ct_unknown)
Edouard@755
    48
        {
Edouard@755
    49
            if (identity->comm_type == PEP_ct_unknown ||
Edouard@755
    50
                _comm_type_key > identity->comm_type)
Edouard@755
    51
            {
krista@1275
    52
                bool blacklisted;
krista@1275
    53
                status = blacklist_is_listed(session, _keylist->value, &blacklisted);
krista@1275
    54
                if (status == PEP_STATUS_OK && !blacklisted) {
krista@1275
    55
                    identity->comm_type = _comm_type_key;
krista@1275
    56
                    _fpr = _keylist->value;
krista@1275
    57
                }
Edouard@755
    58
            }
Edouard@755
    59
        }
Edouard@755
    60
    }
Edouard@755
    61
krista@1342
    62
    
krista@1342
    63
//    if (_fpr) {
krista@1342
    64
    free(identity->fpr);
Edouard@755
    65
krista@1342
    66
    identity->fpr = strdup(_fpr);
krista@1342
    67
    if (identity->fpr == NULL) {
krista@1342
    68
        free_stringlist(keylist);
krista@1342
    69
        return PEP_OUT_OF_MEMORY;
Edouard@755
    70
    }
krista@1342
    71
//    }
Edouard@755
    72
    free_stringlist(keylist);
Edouard@755
    73
    return PEP_STATUS_OK;
Edouard@755
    74
}
Edouard@755
    75
edouard@1406
    76
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
edouard@1385
    77
edouard@1385
    78
DYNAMIC_API PEP_STATUS update_identity(
edouard@1385
    79
        PEP_SESSION session, pEp_identity * identity
vb@0
    80
    )
vb@0
    81
{
vb@0
    82
    pEp_identity *stored_identity;
krista@1224
    83
    pEp_identity* temp_id = NULL;
vb@0
    84
    PEP_STATUS status;
vb@0
    85
vb@0
    86
    assert(session);
vb@0
    87
    assert(identity);
Edouard@439
    88
    assert(!EMPTYSTR(identity->address));
vb@0
    89
Edouard@439
    90
    if (!(session && identity && !EMPTYSTR(identity->address)))
vb@191
    91
        return PEP_ILLEGAL_VALUE;
vb@191
    92
vb@1078
    93
    if (identity->me || (identity->user_id && strcmp(identity->user_id, PEP_OWN_USERID) == 0)) {
edouard@1385
    94
        identity->me = true;
edouard@1406
    95
        return _myself(session, identity, false, true);
vb@1078
    96
    }
vb@1078
    97
Edouard@559
    98
    int _no_user_id = EMPTYSTR(identity->user_id);
edouard@1164
    99
    int _did_elect_new_key = 0;
Edouard@559
   100
Edouard@559
   101
    if (_no_user_id)
Edouard@559
   102
    {
vb@1015
   103
        status = get_identity(session, identity->address, PEP_OWN_USERID,
vb@1015
   104
                &stored_identity);
vb@1015
   105
        if (status == PEP_STATUS_OK) {
vb@1015
   106
            free_identity(stored_identity);
edouard@1406
   107
            return _myself(session, identity, false, true);
vb@1015
   108
        }
vb@1015
   109
Edouard@559
   110
        free(identity->user_id);
Edouard@559
   111
vb@591
   112
        identity->user_id = calloc(1, strlen(identity->address) + 6);
Edouard@562
   113
        if (!identity->user_id)
Edouard@562
   114
        {
Edouard@562
   115
            return PEP_OUT_OF_MEMORY;
Edouard@562
   116
        }
vb@983
   117
        snprintf(identity->user_id, strlen(identity->address) + 6,
Edouard@562
   118
                 "TOFU_%s", identity->address);
Edouard@559
   119
    }
vb@934
   120
 
Edouard@559
   121
    status = get_identity(session,
Edouard@559
   122
                          identity->address,
Edouard@559
   123
                          identity->user_id,
Edouard@559
   124
                          &stored_identity);
Edouard@559
   125
    
vb@0
   126
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   127
    if (status == PEP_OUT_OF_MEMORY)
Edouard@829
   128
        goto exit_free;
vb@0
   129
krista@1224
   130
    temp_id = identity_dup(identity);
krista@1220
   131
    
edouard@1501
   132
    /* We don't take given fpr. 
edouard@1501
   133
       In case there's no acceptable stored fpr, it will be elected. */
edouard@1501
   134
    free(temp_id->fpr);
edouard@1501
   135
    temp_id->fpr = NULL;
edouard@1501
   136
    temp_id->comm_type = PEP_ct_unknown;
edouard@1501
   137
            
edouard@1501
   138
    if (stored_identity) {
krista@1188
   139
        
edouard@1501
   140
        bool dont_use_stored_fpr = true;
krista@1220
   141
krista@1220
   142
        /* if we have a stored_identity fpr */
edouard@1501
   143
        if (!EMPTYSTR(stored_identity->fpr)) {
edouard@1501
   144
            status = blacklist_is_listed(session, stored_identity->fpr, &dont_use_stored_fpr);
krista@1220
   145
            if (status != PEP_STATUS_OK)
edouard@1501
   146
                dont_use_stored_fpr = true; 
krista@1220
   147
        }
krista@1188
   148
            
krista@1220
   149
edouard@1501
   150
        if (!dont_use_stored_fpr) {
edouard@1506
   151
            /* Check stored comm_type */
edouard@1506
   152
            PEP_comm_type _comm_type_key;
edouard@1506
   153
            status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
edouard@1506
   154
            assert(status != PEP_OUT_OF_MEMORY);
edouard@1506
   155
            if (status == PEP_OUT_OF_MEMORY) {
krista@1188
   156
                goto exit_free;
krista@1220
   157
            }
edouard@1506
   158
            if (status == PEP_KEY_NOT_FOUND){
edouard@1506
   159
                /* stored key was deleted from keyring. any other candidate ?*/
edouard@1506
   160
                status = elect_pubkey(session, temp_id);
edouard@1506
   161
                if (status != PEP_STATUS_OK) {
edouard@1506
   162
                    goto exit_free;
edouard@1506
   163
                } else {
edouard@1506
   164
                    _did_elect_new_key = 1;
edouard@1506
   165
                }
edouard@1506
   166
            } else {
edouard@1506
   167
                temp_id->fpr = strdup(stored_identity->fpr);
edouard@1506
   168
                assert(temp_id->fpr);
edouard@1506
   169
                if (temp_id->fpr == NULL) {
edouard@1506
   170
                    status = PEP_OUT_OF_MEMORY;
edouard@1506
   171
                    goto exit_free;
edouard@1506
   172
                }
edouard@1501
   173
edouard@1506
   174
                if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
edouard@1506
   175
                    /* if key not good anymore, 
edouard@1506
   176
                       downgrade eventually trusted comm_type */
edouard@1501
   177
                    temp_id->comm_type = _comm_type_key;
edouard@1506
   178
                } else {
edouard@1506
   179
                    /* otherwise take stored comm_type as-is */
edouard@1506
   180
                    temp_id->comm_type = stored_identity->comm_type;
edouard@1506
   181
                    if (temp_id->comm_type == PEP_ct_unknown) {
edouard@1506
   182
                        /* except if unknown */
edouard@1506
   183
                        temp_id->comm_type = _comm_type_key;
edouard@1506
   184
                    }
edouard@1501
   185
                }
edouard@1501
   186
            }
krista@1220
   187
        }
edouard@1501
   188
        else {
edouard@1501
   189
            status = elect_pubkey(session, temp_id);
edouard@1506
   190
            if (status != PEP_STATUS_OK){
edouard@1501
   191
                goto exit_free;
edouard@1506
   192
            } else {
krista@1188
   193
                _did_elect_new_key = 1;
krista@1188
   194
            }
krista@1188
   195
        }
krista@1188
   196
        
krista@1220
   197
        /* ok, from here on out, use temp_id */
krista@1220
   198
        
krista@1220
   199
        
krista@1220
   200
        /* At this point, we either have a non-blacklisted fpr we can work */
krista@1220
   201
        /* with, or we've got nada.                                        */        
edouard@1501
   202
edouard@1501
   203
        if (EMPTYSTR(temp_id->fpr)) {
edouard@1501
   204
            /* nada : set comm_type accordingly */
krista@1243
   205
            temp_id->comm_type = PEP_ct_key_not_found;
krista@1243
   206
        }
krista@1243
   207
        
krista@1220
   208
        if (EMPTYSTR(temp_id->username)) {
krista@1220
   209
            free(temp_id->username);
krista@1220
   210
            temp_id->username = strdup(stored_identity->username);
krista@1220
   211
            assert(temp_id->username);
krista@1220
   212
            if (temp_id->username == NULL){
Edouard@829
   213
                status = PEP_OUT_OF_MEMORY;
Edouard@829
   214
                goto exit_free;
Edouard@829
   215
            }
vb@22
   216
        }
vb@22
   217
krista@1220
   218
        if (temp_id->lang[0] == 0) {
krista@1220
   219
            temp_id->lang[0] = stored_identity->lang[0];
krista@1220
   220
            temp_id->lang[1] = stored_identity->lang[1];
krista@1220
   221
            temp_id->lang[2] = 0;
vb@21
   222
        }
vb@932
   223
krista@1220
   224
        temp_id->flags = stored_identity->flags;
vb@21
   225
    }
vb@21
   226
    else /* stored_identity == NULL */ {
krista@1220
   227
        temp_id->flags = 0;
vb@934
   228
edouard@1501
   229
        /* We elect a pubkey */
edouard@1501
   230
        status = elect_pubkey(session, temp_id);
edouard@1501
   231
        if (status != PEP_STATUS_OK)
edouard@1501
   232
            goto exit_free;
edouard@1501
   233
        
edouard@1501
   234
        /* Work with the elected key */
krista@1220
   235
        if (!EMPTYSTR(temp_id->fpr)) {
krista@1196
   236
            
edouard@1502
   237
            PEP_comm_type _comm_type_key = temp_id->comm_type;
edouard@1502
   238
            
edouard@1502
   239
            _did_elect_new_key = 1;
vb@21
   240
edouard@1502
   241
            // We don't want to lose a previous trust entry!!!
edouard@1502
   242
            status = get_trust(session, temp_id);
edouard@1501
   243
edouard@1502
   244
            bool has_trust_status = (status == PEP_STATUS_OK);
vb@21
   245
edouard@1502
   246
            if (!has_trust_status)
edouard@1502
   247
                temp_id->comm_type = _comm_type_key;
vb@21
   248
        }
vb@21
   249
    }
vb@21
   250
edouard@1501
   251
    if (temp_id->fpr == NULL) {
krista@1220
   252
        temp_id->fpr = strdup("");
edouard@1501
   253
        if (temp_id->fpr == NULL) {
edouard@1501
   254
            status = PEP_OUT_OF_MEMORY;
edouard@1501
   255
            goto exit_free;
edouard@1501
   256
        }
edouard@1501
   257
    }
krista@1193
   258
    
krista@1193
   259
    
vb@21
   260
    status = PEP_STATUS_OK;
vb@21
   261
krista@1220
   262
    if (temp_id->comm_type != PEP_ct_unknown && !EMPTYSTR(temp_id->user_id)) {
krista@1220
   263
        assert(!EMPTYSTR(temp_id->username)); // this should not happen
vb@22
   264
krista@1220
   265
        if (EMPTYSTR(temp_id->username)) { // mitigate
krista@1220
   266
            free(temp_id->username);
krista@1220
   267
            temp_id->username = strdup("anonymous");
krista@1220
   268
            assert(temp_id->username);
krista@1220
   269
            if (temp_id->username == NULL){
Edouard@829
   270
                status = PEP_OUT_OF_MEMORY;
Edouard@829
   271
                goto exit_free;
Edouard@829
   272
            }
vb@0
   273
        }
vb@8
   274
Edouard@755
   275
        // Identity doesn't get stored if call was just about checking existing
Edouard@755
   276
        // user by address (i.e. no user id given but already stored)
krista@1223
   277
        if (!(_no_user_id && stored_identity) || _did_elect_new_key)
Edouard@559
   278
        {
krista@1220
   279
            status = set_identity(session, temp_id);
Edouard@559
   280
            assert(status == PEP_STATUS_OK);
Edouard@559
   281
            if (status != PEP_STATUS_OK) {
Edouard@829
   282
                goto exit_free;
Edouard@559
   283
            }
Edouard@499
   284
        }
vb@8
   285
    }
vb@8
   286
krista@1220
   287
    if (temp_id->comm_type != PEP_ct_compromized &&
krista@1220
   288
            temp_id->comm_type < PEP_ct_strong_but_unconfirmed)
vb@297
   289
        if (session->examine_identity)
krista@1220
   290
            session->examine_identity(temp_id, session->examine_management);
krista@1220
   291
    
krista@1220
   292
    /* ok, we got to the end. So we can assign the output identity */
krista@1220
   293
    free(identity->address);
krista@1220
   294
    identity->address = strdup(temp_id->address);
krista@1220
   295
    free(identity->fpr);
krista@1220
   296
    identity->fpr = strdup(temp_id->fpr);
krista@1220
   297
    free(identity->user_id);
krista@1220
   298
    identity->user_id = strdup(temp_id->user_id);
krista@1220
   299
    free(identity->username);
krista@1492
   300
    identity->username = strdup(temp_id->username ? temp_id->username : "anonymous");
krista@1220
   301
    identity->comm_type = temp_id->comm_type;
krista@1220
   302
    identity->lang[0] = temp_id->lang[0];
krista@1220
   303
    identity->lang[1] = temp_id->lang[1];
krista@1220
   304
    identity->lang[2] = 0;
krista@1220
   305
    identity->me = temp_id->me;
krista@1220
   306
    identity->flags = temp_id->flags;
vb@297
   307
Edouard@829
   308
exit_free :
Edouard@829
   309
    
Edouard@829
   310
    if (stored_identity){
Edouard@829
   311
        free_identity(stored_identity);
Edouard@829
   312
    }
Edouard@829
   313
krista@1220
   314
    if (temp_id)
krista@1220
   315
        free_identity(temp_id);
krista@1220
   316
    
vb@8
   317
    return status;
vb@0
   318
}
vb@0
   319
Edouard@774
   320
PEP_STATUS elect_ownkey(
krista@1194
   321
        PEP_SESSION session, pEp_identity * identity
Edouard@774
   322
    )
Edouard@774
   323
{
Edouard@774
   324
    PEP_STATUS status;
Edouard@774
   325
    stringlist_t *keylist = NULL;
Edouard@774
   326
Edouard@774
   327
    free(identity->fpr);
Edouard@774
   328
    identity->fpr = NULL;
Edouard@774
   329
krista@1357
   330
    status = find_private_keys(session, identity->address, &keylist);
Edouard@774
   331
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   332
    if (status == PEP_OUT_OF_MEMORY)
Edouard@774
   333
        return PEP_OUT_OF_MEMORY;
Edouard@774
   334
    
Edouard@774
   335
    if (keylist != NULL && keylist->value != NULL)
Edouard@774
   336
    {
Edouard@774
   337
        char *_fpr = NULL;
Edouard@774
   338
        identity->comm_type = PEP_ct_unknown;
Edouard@774
   339
Edouard@774
   340
        stringlist_t *_keylist;
Edouard@774
   341
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@774
   342
            bool is_own = false;
Edouard@774
   343
            
Edouard@774
   344
            if (session->use_only_own_private_keys)
Edouard@774
   345
            {
Edouard@774
   346
                status = own_key_is_listed(session, _keylist->value, &is_own);
Edouard@774
   347
                assert(status == PEP_STATUS_OK);
Edouard@774
   348
                if (status != PEP_STATUS_OK) {
Edouard@774
   349
                    free_stringlist(keylist);
Edouard@774
   350
                    return status;
Edouard@774
   351
                }
Edouard@774
   352
            }
Edouard@774
   353
Edouard@774
   354
            // TODO : also accept synchronized device group keys ?
Edouard@774
   355
            
Edouard@774
   356
            if (!session->use_only_own_private_keys || is_own)
Edouard@774
   357
            {
Edouard@774
   358
                PEP_comm_type _comm_type_key;
Edouard@774
   359
                
Edouard@774
   360
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@774
   361
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   362
                if (status == PEP_OUT_OF_MEMORY) {
Edouard@774
   363
                    free_stringlist(keylist);
Edouard@774
   364
                    return PEP_OUT_OF_MEMORY;
Edouard@774
   365
                }
Edouard@774
   366
                
Edouard@774
   367
                if (_comm_type_key != PEP_ct_compromized &&
Edouard@774
   368
                    _comm_type_key != PEP_ct_unknown)
Edouard@774
   369
                {
Edouard@774
   370
                    if (identity->comm_type == PEP_ct_unknown ||
Edouard@774
   371
                        _comm_type_key > identity->comm_type)
Edouard@774
   372
                    {
Edouard@774
   373
                        identity->comm_type = _comm_type_key;
Edouard@774
   374
                        _fpr = _keylist->value;
Edouard@774
   375
                    }
Edouard@774
   376
                }
Edouard@774
   377
            }
Edouard@774
   378
        }
Edouard@774
   379
        
Edouard@774
   380
        if (_fpr)
Edouard@774
   381
        {
Edouard@774
   382
            identity->fpr = strdup(_fpr);
Edouard@774
   383
            assert(identity->fpr);
Edouard@774
   384
            if (identity->fpr == NULL)
Edouard@774
   385
            {
Edouard@774
   386
                free_stringlist(keylist);
Edouard@774
   387
                return PEP_OUT_OF_MEMORY;
Edouard@774
   388
            }
Edouard@774
   389
        }
Edouard@774
   390
        free_stringlist(keylist);
Edouard@774
   391
    }
Edouard@774
   392
    return PEP_STATUS_OK;
Edouard@774
   393
}
Edouard@774
   394
krista@1357
   395
PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
krista@1357
   396
                                bool* is_usable) {
krista@1357
   397
    
krista@1357
   398
    bool dont_use_fpr = true;
krista@1357
   399
    
krista@1357
   400
    PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
krista@1371
   401
    if (status == PEP_STATUS_OK && !dont_use_fpr) {
krista@1357
   402
        // Make sure there is a *private* key associated with this fpr
krista@1357
   403
        bool has_private = false;
krista@1357
   404
        status = contains_priv_key(session, fpr, &has_private);
krista@1371
   405
krista@1371
   406
        if (status == PEP_STATUS_OK)
krista@1371
   407
            dont_use_fpr = !has_private;
krista@1357
   408
    }
krista@1357
   409
    
krista@1357
   410
    *is_usable = !dont_use_fpr;
krista@1357
   411
    
krista@1357
   412
    return status;
krista@1357
   413
}
krista@1357
   414
edouard@1406
   415
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
vb@0
   416
{
Edouard@560
   417
    pEp_identity *stored_identity;
vb@0
   418
    PEP_STATUS status;
vb@0
   419
vb@0
   420
    assert(session);
vb@0
   421
    assert(identity);
vb@1044
   422
    assert(!EMPTYSTR(identity->address));
vb@1043
   423
Edouard@658
   424
    assert(EMPTYSTR(identity->user_id) ||
Edouard@658
   425
           strcmp(identity->user_id, PEP_OWN_USERID) == 0);
vb@0
   426
vb@1044
   427
    if (!(session && identity && !EMPTYSTR(identity->address) &&
vb@1043
   428
            (EMPTYSTR(identity->user_id) ||
vb@1043
   429
            strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
vb@191
   430
        return PEP_ILLEGAL_VALUE;
vb@191
   431
vb@0
   432
    identity->comm_type = PEP_ct_pEp;
vb@0
   433
    identity->me = true;
edouard@1406
   434
    if(ignore_flags)
edouard@1406
   435
        identity->flags = 0;
Edouard@658
   436
    
vb@1043
   437
    if (EMPTYSTR(identity->user_id))
Edouard@658
   438
    {
Edouard@658
   439
        free(identity->user_id);
Edouard@658
   440
        identity->user_id = strdup(PEP_OWN_USERID);
Edouard@658
   441
        assert(identity->user_id);
Edouard@658
   442
        if (identity->user_id == NULL)
Edouard@658
   443
            return PEP_OUT_OF_MEMORY;
Edouard@658
   444
    }
vb@0
   445
edouard@1488
   446
    if (EMPTYSTR(identity->username))
edouard@1488
   447
    {
edouard@1488
   448
        free(identity->username);
edouard@1488
   449
        identity->username = strdup("anonymous");
edouard@1488
   450
        assert(identity->username);
edouard@1488
   451
        if (identity->username == NULL)
edouard@1488
   452
            return PEP_OUT_OF_MEMORY;
edouard@1488
   453
    }
edouard@1488
   454
vb@215
   455
    DEBUG_LOG("myself", "debug", identity->address);
vb@1044
   456
 
Edouard@560
   457
    status = get_identity(session,
Edouard@560
   458
                          identity->address,
Edouard@560
   459
                          identity->user_id,
Edouard@560
   460
                          &stored_identity);
Edouard@560
   461
    
vb@0
   462
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   463
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   464
        return PEP_OUT_OF_MEMORY;
krista@1357
   465
krista@1357
   466
    bool dont_use_stored_fpr = true;
krista@1357
   467
    bool dont_use_input_fpr = true;
krista@1359
   468
        
Edouard@560
   469
    if (stored_identity)
Edouard@560
   470
    {
Edouard@560
   471
        if (EMPTYSTR(identity->fpr)) {
krista@1352
   472
            
krista@1357
   473
            bool has_private = false;
krista@1352
   474
            
krista@1357
   475
            status = _has_usable_priv_key(session, stored_identity->fpr, &has_private); 
krista@1352
   476
            
krista@1371
   477
            // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
krista@1357
   478
            if (has_private) {
krista@1357
   479
                identity->fpr = strdup(stored_identity->fpr);
krista@1357
   480
                assert(identity->fpr);
krista@1357
   481
                if (identity->fpr == NULL)
krista@1357
   482
                {
krista@1357
   483
                    return PEP_OUT_OF_MEMORY;
krista@1357
   484
                }
krista@1357
   485
                dont_use_stored_fpr = false;
Edouard@560
   486
            }
Edouard@560
   487
        }
krista@1357
   488
        
edouard@1406
   489
        identity->flags = (identity->flags & 255) | stored_identity->flags;
edouard@1367
   490
edouard@1367
   491
        free_identity(stored_identity);
Edouard@588
   492
    }
krista@1357
   493
    
krista@1357
   494
    if (dont_use_stored_fpr && !EMPTYSTR(identity->fpr))
Edouard@588
   495
    {
Edouard@588
   496
        // App must have a good reason to give fpr, such as explicit
Edouard@588
   497
        // import of private key, or similar.
Edouard@588
   498
Edouard@658
   499
        // Take given fpr as-is.
vb@934
   500
krista@1357
   501
        // BUT:
krista@1357
   502
        // First check to see if it's blacklisted or private part is missing?
krista@1357
   503
        bool has_private = false;
krista@1357
   504
        
krista@1357
   505
        status = _has_usable_priv_key(session, identity->fpr, &has_private); 
krista@1357
   506
        
krista@1371
   507
        // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
krista@1357
   508
        if (has_private) {
krista@1357
   509
            dont_use_input_fpr = false;
krista@1357
   510
        }
Edouard@560
   511
    }
krista@1357
   512
    
krista@1357
   513
    // Ok, we failed to get keys either way, so let's elect one.
krista@1357
   514
    if (dont_use_input_fpr && dont_use_stored_fpr)
Edouard@560
   515
    {
Edouard@774
   516
        status = elect_ownkey(session, identity);
Edouard@774
   517
        assert(status == PEP_STATUS_OK);
Edouard@774
   518
        if (status != PEP_STATUS_OK) {
Edouard@774
   519
            return status;
Edouard@560
   520
        }
vb@934
   521
krista@1357
   522
        bool has_private = false;
krista@1359
   523
        if (identity->fpr) {
krista@1359
   524
            // ok, we elected something.
krista@1359
   525
            // elect_ownkey only returns private keys, so we don't check again.
krista@1359
   526
            // Check to see if it's blacklisted
krista@1359
   527
            bool listed;
krista@1359
   528
            status = blacklist_is_listed(session, identity->fpr, &listed); 
krista@1371
   529
krista@1371
   530
            if (status == PEP_STATUS_OK)
krista@1371
   531
                has_private = !listed;
krista@1359
   532
        }
krista@1357
   533
        
krista@1357
   534
        if (has_private) {
krista@1357
   535
            dont_use_input_fpr = false;
krista@1357
   536
        }
krista@1357
   537
        else { // OK, we've tried everything. Time to generate new keys.
krista@1359
   538
            free(identity->fpr); // It can stay in this state (unallocated) because we'll generate a new key 
krista@1359
   539
            identity->fpr = NULL;
krista@1357
   540
        }
Edouard@560
   541
    }
Edouard@695
   542
Edouard@695
   543
    bool revoked = false;
Edouard@695
   544
    char *r_fpr = NULL;
Edouard@695
   545
    if (!EMPTYSTR(identity->fpr))
Edouard@695
   546
    {
Edouard@695
   547
        status = key_revoked(session, identity->fpr, &revoked);
Edouard@775
   548
Edouard@775
   549
        // Forces re-election if key is missing and own-key-only not forced
Edouard@775
   550
        if (!session->use_only_own_private_keys && status == PEP_KEY_NOT_FOUND) 
Edouard@775
   551
        {
Edouard@775
   552
            status = elect_ownkey(session, identity);
Edouard@775
   553
            assert(status == PEP_STATUS_OK);
Edouard@775
   554
            if (status != PEP_STATUS_OK) {
Edouard@775
   555
                return status;
Edouard@775
   556
            }
Edouard@775
   557
        } 
Edouard@775
   558
        else if (status != PEP_STATUS_OK) 
Edouard@775
   559
        {
Edouard@695
   560
            return status;
Edouard@695
   561
        }
Edouard@695
   562
    }
edouard@1140
   563
   
edouard@1140
   564
    bool new_key_generated = false;
edouard@1140
   565
Edouard@695
   566
    if (EMPTYSTR(identity->fpr) || revoked)
Edouard@695
   567
    {        
edouard@1385
   568
        if(!do_keygen){
edouard@1385
   569
            return PEP_GET_KEY_FAILED;
edouard@1385
   570
        }
edouard@1385
   571
Edouard@695
   572
        if(revoked)
Edouard@695
   573
        {
Edouard@697
   574
            r_fpr = identity->fpr;
Edouard@697
   575
            identity->fpr = NULL;
Edouard@695
   576
        }
Edouard@560
   577
        
vb@215
   578
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   579
        status = generate_keypair(session, identity);
vb@0
   580
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   581
        if (status != PEP_STATUS_OK) {
vb@0
   582
            char buf[11];
vb@0
   583
            snprintf(buf, 11, "%d", status);
vb@215
   584
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   585
            if(revoked && r_fpr)
Edouard@695
   586
                free(r_fpr);
vb@0
   587
            return status;
vb@0
   588
        }
edouard@1140
   589
edouard@1140
   590
        new_key_generated = true;
Edouard@560
   591
        
Edouard@695
   592
        if(revoked)
Edouard@695
   593
        {
Edouard@695
   594
            status = set_revoked(session, r_fpr,
Edouard@695
   595
                                 identity->fpr, time(NULL));
Edouard@695
   596
            free(r_fpr);
Edouard@695
   597
            if (status != PEP_STATUS_OK) {
Edouard@695
   598
                return status;
Edouard@695
   599
            }
Edouard@588
   600
        }
vb@0
   601
    }
Edouard@560
   602
    else
Edouard@560
   603
    {
vb@214
   604
        bool expired;
Edouard@701
   605
        status = key_expired(session, identity->fpr, 
Edouard@701
   606
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   607
                             &expired);
Edouard@701
   608
vb@214
   609
        assert(status == PEP_STATUS_OK);
Edouard@499
   610
        if (status != PEP_STATUS_OK) {
Edouard@588
   611
            return status;
Edouard@499
   612
        }
vb@214
   613
vb@214
   614
        if (status == PEP_STATUS_OK && expired) {
vb@214
   615
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   616
            renew_key(session, identity->fpr, ts);
vb@214
   617
            free_timestamp(ts);
vb@214
   618
        }
vb@214
   619
    }
vb@0
   620
krista@1353
   621
    if (!identity->username)
krista@1353
   622
        identity->username = strdup("");
krista@1353
   623
    
vb@0
   624
    status = set_identity(session, identity);
vb@0
   625
    assert(status == PEP_STATUS_OK);
Edouard@499
   626
    if (status != PEP_STATUS_OK) {
Edouard@588
   627
        return status;
Edouard@499
   628
    }
vb@0
   629
edouard@1145
   630
    if(new_key_generated)
edouard@1145
   631
    {
edouard@1145
   632
        // if a state machine for keysync is in place, inject notify
edouard@1195
   633
        status = inject_DeviceState_event(session, KeyGen, NULL, NULL);
edouard@1299
   634
        if (status == PEP_OUT_OF_MEMORY){
edouard@1299
   635
            return PEP_OUT_OF_MEMORY;
edouard@1299
   636
        }
edouard@1145
   637
    }
edouard@1140
   638
vb@0
   639
    return PEP_STATUS_OK;
Edouard@499
   640
vb@0
   641
}
vb@0
   642
edouard@1385
   643
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
edouard@1385
   644
{
edouard@1406
   645
    return _myself(session, identity, true, false);
edouard@1385
   646
}
edouard@1385
   647
vb@296
   648
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   649
        PEP_SESSION session, 
vb@292
   650
        examine_identity_t examine_identity,
vb@292
   651
        void *management
vb@292
   652
    )
vb@292
   653
{
vb@292
   654
    assert(session);
vb@292
   655
    if (!session)
vb@292
   656
        return PEP_ILLEGAL_VALUE;
vb@292
   657
vb@292
   658
    session->examine_management = management;
vb@292
   659
    session->examine_identity = examine_identity;
vb@292
   660
vb@292
   661
    return PEP_STATUS_OK;
vb@292
   662
}
vb@292
   663
vb@0
   664
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   665
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   666
        void *management
vb@0
   667
    )
vb@0
   668
{
vb@0
   669
    PEP_SESSION session;
vb@0
   670
    pEp_identity *identity;
Edouard@499
   671
    PEP_STATUS status;
vb@0
   672
Edouard@499
   673
    assert(retrieve_next_identity);
Edouard@499
   674
    assert(management);
Edouard@499
   675
Edouard@499
   676
    if (!retrieve_next_identity || !management)
Edouard@499
   677
        return PEP_ILLEGAL_VALUE;
Edouard@499
   678
Edouard@499
   679
    status = init(&session);
vb@0
   680
    assert(status == PEP_STATUS_OK);
vb@0
   681
    if (status != PEP_STATUS_OK)
vb@0
   682
        return status;
vb@0
   683
vb@0
   684
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   685
Edouard@499
   686
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   687
    {
vb@0
   688
        assert(identity->address);
Edouard@499
   689
        if(identity->address)
Edouard@499
   690
        {
Edouard@499
   691
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   692
Edouard@499
   693
            if (identity->me) {
Edouard@499
   694
                status = myself(session, identity);
Edouard@499
   695
            } else {
Edouard@499
   696
                status = recv_key(session, identity->address);
Edouard@499
   697
            }
Edouard@499
   698
vb@0
   699
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   700
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   701
                return PEP_OUT_OF_MEMORY;
vb@0
   702
        }
vb@0
   703
        free_identity(identity);
vb@0
   704
    }
vb@0
   705
vb@0
   706
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   707
vb@0
   708
    release(session);
vb@0
   709
    return PEP_STATUS_OK;
vb@0
   710
}
vb@0
   711
krista@1213
   712
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
   713
        PEP_SESSION session,
vb@357
   714
        pEp_identity *ident
vb@357
   715
    )
vb@215
   716
{
vb@218
   717
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   718
vb@215
   719
    assert(session);
vb@357
   720
    assert(ident);
Edouard@439
   721
    assert(!EMPTYSTR(ident->fpr));
vb@215
   722
vb@357
   723
    if (!(session && ident && ident->fpr))
vb@215
   724
        return PEP_ILLEGAL_VALUE;
vb@215
   725
vb@357
   726
    if (ident->me)
Edouard@697
   727
    {
vb@357
   728
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   729
        myself(session, ident);
Edouard@697
   730
    }
Edouard@697
   731
    else
Edouard@697
   732
    {
Edouard@697
   733
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
   734
    }
vb@218
   735
vb@218
   736
    return status;
vb@215
   737
}
vb@215
   738
Edouard@410
   739
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   740
        PEP_SESSION session,
Edouard@410
   741
        pEp_identity *ident
Edouard@410
   742
    )
Edouard@410
   743
{
Edouard@410
   744
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   745
Edouard@410
   746
    assert(session);
Edouard@410
   747
    assert(ident);
vb@420
   748
    assert(!ident->me);
Edouard@439
   749
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   750
    assert(!EMPTYSTR(ident->address));
Edouard@439
   751
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   752
vb@420
   753
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   754
            ident->user_id))
Edouard@410
   755
        return PEP_ILLEGAL_VALUE;
Edouard@410
   756
vb@420
   757
    status = update_identity(session, ident);
vb@420
   758
    if (status != PEP_STATUS_OK)
vb@420
   759
        return status;
Edouard@410
   760
Edouard@442
   761
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   762
        ident->comm_type = PEP_ct_unknown;
vb@421
   763
    else
vb@421
   764
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   765
vb@420
   766
    status = set_identity(session, ident);
vb@421
   767
    if (status != PEP_STATUS_OK)
vb@421
   768
        return status;
vb@421
   769
vb@422
   770
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   771
        status = update_identity(session, ident);
Edouard@410
   772
    return status;
Edouard@410
   773
}
Edouard@410
   774
vb@354
   775
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   776
        PEP_SESSION session,
vb@354
   777
        pEp_identity *ident
vb@354
   778
    )
vb@354
   779
{
vb@354
   780
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   781
vb@354
   782
    assert(session);
vb@354
   783
    assert(ident);
Edouard@439
   784
    assert(!EMPTYSTR(ident->address));
Edouard@439
   785
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   786
    assert(!EMPTYSTR(ident->fpr));
vb@354
   787
    assert(!ident->me);
vb@354
   788
Edouard@439
   789
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   790
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   791
        return PEP_ILLEGAL_VALUE;
vb@354
   792
vb@354
   793
    status = update_identity(session, ident);
vb@354
   794
    if (status != PEP_STATUS_OK)
vb@354
   795
        return status;
vb@354
   796
vb@356
   797
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   798
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   799
        status = set_identity(session, ident);
vb@356
   800
    }
vb@356
   801
    else {
vb@356
   802
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   803
        status = PEP_CANNOT_SET_TRUST;
vb@356
   804
    }
vb@354
   805
vb@354
   806
    return status;
vb@354
   807
}
vb@354
   808
Edouard@584
   809
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
   810
        PEP_SESSION session,
vb@955
   811
        const char *fpr,
vb@955
   812
        bool *listed
vb@955
   813
    )
Edouard@584
   814
{
Edouard@584
   815
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   816
    int count;
Edouard@584
   817
    
Edouard@584
   818
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   819
    
Edouard@584
   820
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   821
        return PEP_ILLEGAL_VALUE;
Edouard@584
   822
    
Edouard@584
   823
    *listed = false;
Edouard@584
   824
    
Edouard@584
   825
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   826
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   827
    
Edouard@584
   828
    int result;
Edouard@584
   829
    
Edouard@584
   830
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   831
    switch (result) {
Edouard@584
   832
        case SQLITE_ROW:
Edouard@584
   833
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   834
            *listed = count > 0;
Edouard@584
   835
            status = PEP_STATUS_OK;
Edouard@584
   836
            break;
Edouard@584
   837
            
Edouard@584
   838
        default:
Edouard@584
   839
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   840
    }
Edouard@584
   841
    
Edouard@584
   842
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   843
    return status;
Edouard@584
   844
}
Edouard@584
   845
edouard@1412
   846
PEP_STATUS _own_identities_retrieve(
vb@955
   847
        PEP_SESSION session,
edouard@1412
   848
        identity_list **own_identities,
edouard@1412
   849
        identity_flags_t excluded_flags
vb@955
   850
      )
Edouard@584
   851
{
Edouard@584
   852
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   853
    
vb@955
   854
    assert(session && own_identities);
vb@955
   855
    if (!(session && own_identities))
Edouard@584
   856
        return PEP_ILLEGAL_VALUE;
Edouard@584
   857
    
vb@955
   858
    *own_identities = NULL;
vb@955
   859
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
   860
    if (_own_identities == NULL)
Edouard@584
   861
        goto enomem;
Edouard@584
   862
    
vb@955
   863
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   864
    
Edouard@584
   865
    int result;
vb@955
   866
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
   867
    const char *address = NULL;
Edouard@584
   868
    const char *fpr = NULL;
vb@955
   869
    const char *username = NULL;
vb@955
   870
    const char *user_id = NULL;
vb@955
   871
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
   872
    const char *lang = NULL;
vb@955
   873
    unsigned int flags = 0;
Edouard@584
   874
    
vb@955
   875
    identity_list *_bl = _own_identities;
Edouard@584
   876
    do {
edouard@1412
   877
        sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
vb@955
   878
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
   879
        switch (result) {
Edouard@584
   880
            case SQLITE_ROW:
vb@955
   881
                address = (const char *)
vb@955
   882
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
   883
                fpr = (const char *)
vb@955
   884
                    sqlite3_column_text(session->own_identities_retrieve, 1);
vb@1071
   885
                user_id = PEP_OWN_USERID;
vb@1071
   886
                username = (const char *)
vb@1069
   887
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
   888
                comm_type = PEP_ct_pEp;
vb@1071
   889
                lang = (const char *)
vb@955
   890
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
   891
                flags = (unsigned int)
edouard@1444
   892
                    sqlite3_column_int(session->own_identities_retrieve, 4);
vb@955
   893
vb@1078
   894
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
   895
                if (!ident)
Edouard@584
   896
                    goto enomem;
vb@955
   897
                ident->comm_type = comm_type;
vb@955
   898
                if (lang && lang[0]) {
vb@955
   899
                    ident->lang[0] = lang[0];
vb@955
   900
                    ident->lang[1] = lang[1];
vb@1078
   901
                    ident->lang[2] = 0;
vb@955
   902
                }
vb@1068
   903
                ident->me = true;
vb@955
   904
                ident->flags = flags;
vb@955
   905
vb@955
   906
                _bl = identity_list_add(_bl, ident);
vb@1080
   907
                if (_bl == NULL) {
vb@1080
   908
                    free_identity(ident);
vb@983
   909
                    goto enomem;
vb@1080
   910
                }
Edouard@584
   911
                
Edouard@584
   912
                break;
Edouard@584
   913
                
Edouard@584
   914
            case SQLITE_DONE:
Edouard@584
   915
                break;
Edouard@584
   916
                
Edouard@584
   917
            default:
Edouard@584
   918
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   919
                result = SQLITE_DONE;
Edouard@584
   920
        }
Edouard@584
   921
    } while (result != SQLITE_DONE);
Edouard@584
   922
    
vb@955
   923
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   924
    if (status == PEP_STATUS_OK)
vb@955
   925
        *own_identities = _own_identities;
Edouard@584
   926
    else
vb@955
   927
        free_identity_list(_own_identities);
Edouard@584
   928
    
Edouard@584
   929
    goto the_end;
Edouard@584
   930
    
Edouard@584
   931
enomem:
vb@955
   932
    free_identity_list(_own_identities);
Edouard@584
   933
    status = PEP_OUT_OF_MEMORY;
Edouard@584
   934
    
Edouard@584
   935
the_end:
Edouard@584
   936
    return status;
Edouard@584
   937
}
vb@955
   938
edouard@1412
   939
DYNAMIC_API PEP_STATUS own_identities_retrieve(
edouard@1364
   940
        PEP_SESSION session,
edouard@1412
   941
        identity_list **own_identities
edouard@1412
   942
      )
edouard@1412
   943
{
edouard@1412
   944
    return _own_identities_retrieve(session, own_identities, 0);
edouard@1412
   945
}
edouard@1412
   946
edouard@1412
   947
PEP_STATUS _own_keys_retrieve(
edouard@1412
   948
        PEP_SESSION session,
edouard@1412
   949
        stringlist_t **keylist,
edouard@1412
   950
        identity_flags_t excluded_flags
edouard@1364
   951
      )
edouard@1364
   952
{
edouard@1364
   953
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1364
   954
    
edouard@1364
   955
    assert(session && keylist);
edouard@1364
   956
    if (!(session && keylist))
edouard@1364
   957
        return PEP_ILLEGAL_VALUE;
edouard@1364
   958
    
edouard@1364
   959
    *keylist = NULL;
edouard@1364
   960
    stringlist_t *_keylist = NULL;
edouard@1364
   961
    
edouard@1394
   962
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
   963
    
edouard@1364
   964
    int result;
edouard@1364
   965
    char *fpr = NULL;
edouard@1364
   966
    
edouard@1364
   967
    stringlist_t *_bl = _keylist;
edouard@1364
   968
    do {
edouard@1412
   969
        sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
edouard@1394
   970
        result = sqlite3_step(session->own_keys_retrieve);
edouard@1364
   971
        switch (result) {
edouard@1364
   972
            case SQLITE_ROW:
edouard@1394
   973
                fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
edouard@1364
   974
                if(fpr == NULL)
edouard@1364
   975
                    goto enomem;
edouard@1364
   976
edouard@1364
   977
                _bl = stringlist_add(_bl, fpr);
edouard@1364
   978
                if (_bl == NULL) {
edouard@1364
   979
                    free(fpr);
edouard@1364
   980
                    goto enomem;
edouard@1364
   981
                }
edouard@1364
   982
                if (_keylist == NULL)
edouard@1364
   983
                    _keylist = _bl;
edouard@1364
   984
                
edouard@1364
   985
                break;
edouard@1364
   986
                
edouard@1364
   987
            case SQLITE_DONE:
edouard@1364
   988
                break;
edouard@1364
   989
                
edouard@1364
   990
            default:
edouard@1364
   991
                status = PEP_UNKNOWN_ERROR;
edouard@1364
   992
                result = SQLITE_DONE;
edouard@1364
   993
        }
edouard@1364
   994
    } while (result != SQLITE_DONE);
edouard@1364
   995
    
edouard@1394
   996
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
   997
    if (status == PEP_STATUS_OK)
edouard@1364
   998
        *keylist = _keylist;
edouard@1364
   999
    else
edouard@1364
  1000
        free_stringlist(_keylist);
edouard@1364
  1001
    
edouard@1364
  1002
    goto the_end;
edouard@1364
  1003
    
edouard@1364
  1004
enomem:
edouard@1364
  1005
    free_stringlist(_keylist);
edouard@1364
  1006
    status = PEP_OUT_OF_MEMORY;
edouard@1364
  1007
    
edouard@1364
  1008
the_end:
edouard@1364
  1009
    return status;
edouard@1364
  1010
}
edouard@1364
  1011
edouard@1412
  1012
DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
edouard@1412
  1013
{
edouard@1412
  1014
    return _own_keys_retrieve(session, keylist, 0);
edouard@1412
  1015
}
edouard@1412
  1016
edouard@1394
  1017
// TODO: Unused for now, but should be used when sync receive old keys (ENGINE-145)
edouard@1394
  1018
DYNAMIC_API PEP_STATUS set_own_key(
edouard@1394
  1019
       PEP_SESSION session,
edouard@1394
  1020
       const char *address,
edouard@1394
  1021
       const char *fpr
edouard@1394
  1022
    )
edouard@1394
  1023
{
edouard@1394
  1024
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1394
  1025
    
edouard@1394
  1026
    assert(session &&
edouard@1394
  1027
           address && address[0] &&
edouard@1394
  1028
           fpr && fpr[0]
edouard@1394
  1029
          );
edouard@1394
  1030
    
edouard@1394
  1031
    if (!(session &&
edouard@1394
  1032
          address && address[0] &&
edouard@1394
  1033
          fpr && fpr[0]
edouard@1394
  1034
         ))
edouard@1394
  1035
        return PEP_ILLEGAL_VALUE;
edouard@1394
  1036
    
edouard@1394
  1037
    sqlite3_reset(session->set_own_key);
edouard@1394
  1038
    sqlite3_bind_text(session->set_own_key, 1, address, -1, SQLITE_STATIC);
edouard@1394
  1039
    sqlite3_bind_text(session->set_own_key, 2, fpr, -1, SQLITE_STATIC);
edouard@1394
  1040
edouard@1394
  1041
    int result;
edouard@1394
  1042
    
edouard@1394
  1043
    result = sqlite3_step(session->set_own_key);
edouard@1394
  1044
    switch (result) {
edouard@1394
  1045
        case SQLITE_DONE:
edouard@1394
  1046
            status = PEP_STATUS_OK;
edouard@1394
  1047
            break;
edouard@1394
  1048
            
edouard@1394
  1049
        default:
edouard@1394
  1050
            status = PEP_UNKNOWN_ERROR;
edouard@1394
  1051
    }
edouard@1394
  1052
    
edouard@1394
  1053
    sqlite3_reset(session->set_own_key);
edouard@1394
  1054
    return status;
edouard@1394
  1055
}
krista@1357
  1056
krista@1357
  1057
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
  1058
                             bool *has_private) {
krista@1357
  1059
krista@1357
  1060
    assert(session);
krista@1357
  1061
    assert(fpr);
krista@1357
  1062
    assert(has_private);
krista@1357
  1063
    
krista@1357
  1064
    if (!(session && fpr && has_private))
krista@1357
  1065
        return PEP_ILLEGAL_VALUE;
krista@1357
  1066
krista@1357
  1067
    return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
edouard@1385
  1068
}