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