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