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