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