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