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