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