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