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