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