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