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