src/keymanagement.c
author Krista Bennett <krista@pep-project.org>
Sat, 09 Dec 2017 13:04:40 +0100
branchENGINE-293
changeset 2308 b7ef1c9005ae
parent 2305 f617e0acae94
child 2311 688b925c3e73
permissions -rw-r--r--
ENGINE-293: initialise_own_identities (cheap myself() only keygen/retrieval) + additional internal fprless get_identity function added so that temporary identities w/o fpr can be retrieved
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
krista@2308
   440
Edouard@658
   441
    assert(EMPTYSTR(identity->user_id) ||
krista@2305
   442
           (own_id && strcmp(identity->user_id, own_id) == 0) ||
krista@2305
   443
           !own_id);
vb@0
   444
vb@1044
   445
    if (!(session && identity && !EMPTYSTR(identity->address) &&
vb@1043
   446
            (EMPTYSTR(identity->user_id) ||
krista@2305
   447
            (own_id && strcmp(identity->user_id, own_id) == 0) ||
krista@2305
   448
             !own_id)))
roker@1853
   449
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
   450
krista@2308
   451
    if (!own_id) {
krista@2308
   452
        // check to see if we have ANY identity for this address... could have
krista@2308
   453
        // happened due to race condition
krista@2308
   454
    }
krista@2308
   455
    
vb@0
   456
    identity->comm_type = PEP_ct_pEp;
krista@2288
   457
    identity->me = true;
edouard@1406
   458
    if(ignore_flags)
edouard@1406
   459
        identity->flags = 0;
Edouard@658
   460
    
vb@1043
   461
    if (EMPTYSTR(identity->user_id))
Edouard@658
   462
    {
Edouard@658
   463
        free(identity->user_id);
krista@2303
   464
        identity->user_id = (own_id ? own_id : strdup(PEP_OWN_USERID));
Edouard@658
   465
        assert(identity->user_id);
Edouard@658
   466
        if (identity->user_id == NULL)
Edouard@658
   467
            return PEP_OUT_OF_MEMORY;
Edouard@658
   468
    }
vb@0
   469
edouard@1488
   470
    if (EMPTYSTR(identity->username))
edouard@1488
   471
    {
edouard@1488
   472
        free(identity->username);
krista@2219
   473
        identity->username = strdup("Anonymous");
edouard@1488
   474
        assert(identity->username);
edouard@1488
   475
        if (identity->username == NULL)
edouard@1488
   476
            return PEP_OUT_OF_MEMORY;
edouard@1488
   477
    }
edouard@1488
   478
vb@215
   479
    DEBUG_LOG("myself", "debug", identity->address);
vb@1044
   480
 
Edouard@560
   481
    status = get_identity(session,
Edouard@560
   482
                          identity->address,
Edouard@560
   483
                          identity->user_id,
Edouard@560
   484
                          &stored_identity);
Edouard@560
   485
    
vb@0
   486
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   487
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   488
        return PEP_OUT_OF_MEMORY;
krista@1357
   489
krista@1357
   490
    bool dont_use_stored_fpr = true;
krista@1357
   491
    bool dont_use_input_fpr = true;
krista@1359
   492
        
Edouard@560
   493
    if (stored_identity)
Edouard@560
   494
    {
Edouard@560
   495
        if (EMPTYSTR(identity->fpr)) {
krista@1352
   496
            
krista@1357
   497
            bool has_private = false;
krista@1352
   498
            
krista@1357
   499
            status = _has_usable_priv_key(session, stored_identity->fpr, &has_private); 
krista@1352
   500
            
krista@1371
   501
            // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
krista@1357
   502
            if (has_private) {
krista@1357
   503
                identity->fpr = strdup(stored_identity->fpr);
krista@1357
   504
                assert(identity->fpr);
krista@1357
   505
                if (identity->fpr == NULL)
krista@1357
   506
                {
krista@1357
   507
                    return PEP_OUT_OF_MEMORY;
krista@1357
   508
                }
krista@1357
   509
                dont_use_stored_fpr = false;
Edouard@560
   510
            }
Edouard@560
   511
        }
krista@1357
   512
        
edouard@1406
   513
        identity->flags = (identity->flags & 255) | stored_identity->flags;
edouard@1367
   514
        free_identity(stored_identity);
Edouard@588
   515
    }
krista@1357
   516
    
krista@1357
   517
    if (dont_use_stored_fpr && !EMPTYSTR(identity->fpr))
Edouard@588
   518
    {
Edouard@588
   519
        // App must have a good reason to give fpr, such as explicit
Edouard@588
   520
        // import of private key, or similar.
Edouard@588
   521
Edouard@658
   522
        // Take given fpr as-is.
vb@934
   523
krista@1357
   524
        // BUT:
krista@1357
   525
        // First check to see if it's blacklisted or private part is missing?
krista@1357
   526
        bool has_private = false;
krista@1357
   527
        
krista@1357
   528
        status = _has_usable_priv_key(session, identity->fpr, &has_private); 
krista@1357
   529
        
krista@1371
   530
        // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
krista@1357
   531
        if (has_private) {
krista@1357
   532
            dont_use_input_fpr = false;
krista@1357
   533
        }
Edouard@560
   534
    }
krista@1357
   535
    
krista@1357
   536
    // Ok, we failed to get keys either way, so let's elect one.
krista@1357
   537
    if (dont_use_input_fpr && dont_use_stored_fpr)
Edouard@560
   538
    {
Edouard@774
   539
        status = elect_ownkey(session, identity);
Edouard@774
   540
        assert(status == PEP_STATUS_OK);
Edouard@774
   541
        if (status != PEP_STATUS_OK) {
roker@1853
   542
            return ADD_TO_LOG(status);
Edouard@560
   543
        }
vb@934
   544
krista@1357
   545
        bool has_private = false;
krista@1359
   546
        if (identity->fpr) {
krista@1359
   547
            // ok, we elected something.
krista@1359
   548
            // elect_ownkey only returns private keys, so we don't check again.
krista@1359
   549
            // Check to see if it's blacklisted
krista@1359
   550
            bool listed;
krista@1359
   551
            status = blacklist_is_listed(session, identity->fpr, &listed); 
krista@1371
   552
krista@1371
   553
            if (status == PEP_STATUS_OK)
krista@1371
   554
                has_private = !listed;
krista@1359
   555
        }
krista@1357
   556
        
krista@1357
   557
        if (has_private) {
krista@1357
   558
            dont_use_input_fpr = false;
krista@1357
   559
        }
krista@1357
   560
        else { // OK, we've tried everything. Time to generate new keys.
krista@1359
   561
            free(identity->fpr); // It can stay in this state (unallocated) because we'll generate a new key 
krista@1359
   562
            identity->fpr = NULL;
krista@1357
   563
        }
Edouard@560
   564
    }
Edouard@695
   565
Edouard@695
   566
    bool revoked = false;
Edouard@695
   567
    char *r_fpr = NULL;
Edouard@695
   568
    if (!EMPTYSTR(identity->fpr))
Edouard@695
   569
    {
Edouard@695
   570
        status = key_revoked(session, identity->fpr, &revoked);
Edouard@775
   571
edouard@1752
   572
        if (status != PEP_STATUS_OK) 
Edouard@775
   573
        {
roker@1853
   574
            return ADD_TO_LOG(status);
Edouard@695
   575
        }
Edouard@695
   576
    }
edouard@1140
   577
   
Edouard@695
   578
    if (EMPTYSTR(identity->fpr) || revoked)
roker@1734
   579
    {
edouard@1385
   580
        if(!do_keygen){
roker@1853
   581
            return ADD_TO_LOG(PEP_GET_KEY_FAILED);
edouard@1385
   582
        }
edouard@1385
   583
Edouard@695
   584
        if(revoked)
Edouard@695
   585
        {
Edouard@697
   586
            r_fpr = identity->fpr;
Edouard@697
   587
            identity->fpr = NULL;
Edouard@695
   588
        }
Edouard@560
   589
        
vb@215
   590
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   591
        status = generate_keypair(session, identity);
vb@0
   592
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   593
        if (status != PEP_STATUS_OK) {
vb@0
   594
            char buf[11];
vb@0
   595
            snprintf(buf, 11, "%d", status);
vb@215
   596
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   597
            if(revoked && r_fpr)
Edouard@695
   598
                free(r_fpr);
roker@1853
   599
            return ADD_TO_LOG(status);
vb@0
   600
        }
edouard@1140
   601
Edouard@560
   602
        
Edouard@695
   603
        if(revoked)
Edouard@695
   604
        {
Edouard@695
   605
            status = set_revoked(session, r_fpr,
Edouard@695
   606
                                 identity->fpr, time(NULL));
Edouard@695
   607
            free(r_fpr);
Edouard@695
   608
            if (status != PEP_STATUS_OK) {
roker@1853
   609
                return ADD_TO_LOG(status);
Edouard@695
   610
            }
Edouard@371
   611
        }
vb@0
   612
    }
Edouard@560
   613
    else
Edouard@560
   614
    {
vb@214
   615
        bool expired;
Edouard@701
   616
        status = key_expired(session, identity->fpr, 
Edouard@701
   617
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   618
                             &expired);
Edouard@701
   619
vb@214
   620
        assert(status == PEP_STATUS_OK);
Edouard@499
   621
        if (status != PEP_STATUS_OK) {
roker@1853
   622
            return ADD_TO_LOG(status);
Edouard@499
   623
        }
vb@214
   624
vb@214
   625
        if (status == PEP_STATUS_OK && expired) {
vb@214
   626
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   627
            renew_key(session, identity->fpr, ts);
vb@214
   628
            free_timestamp(ts);
vb@214
   629
        }
vb@214
   630
    }
vb@0
   631
krista@1353
   632
    if (!identity->username)
krista@1353
   633
        identity->username = strdup("");
krista@1353
   634
    
vb@0
   635
    status = set_identity(session, identity);
vb@0
   636
    assert(status == PEP_STATUS_OK);
Edouard@499
   637
    if (status != PEP_STATUS_OK) {
Edouard@588
   638
        return status;
Edouard@499
   639
    }
vb@0
   640
roker@1853
   641
    return ADD_TO_LOG(PEP_STATUS_OK);
vb@0
   642
}
vb@0
   643
krista@2308
   644
DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
krista@2308
   645
                                                 identity_list* my_idents) {
krista@2308
   646
    PEP_STATUS status = PEP_STATUS_OK;
krista@2308
   647
    if (!session)
krista@2308
   648
        return PEP_ILLEGAL_VALUE;
krista@2308
   649
        
krista@2308
   650
    char* stored_own_userid = NULL;
krista@2308
   651
    get_own_userid(session, &stored_own_userid);
krista@2308
   652
    
krista@2308
   653
    identity_list* ident_curr = my_idents;
krista@2308
   654
    while (ident_curr) {
krista@2308
   655
        pEp_identity* ident = ident_curr->ident;
krista@2308
   656
        if (!ident)
krista@2308
   657
            return PEP_ILLEGAL_VALUE;
krista@2308
   658
            
krista@2308
   659
        if (stored_own_userid) {
krista@2308
   660
            if (!ident->user_id) 
krista@2308
   661
                ident->user_id = strdup(stored_own_userid);
krista@2308
   662
            else if (strcmp(stored_own_userid, ident->user_id) != 0)
krista@2308
   663
                return PEP_ILLEGAL_VALUE;
krista@2308
   664
        }
krista@2308
   665
        else if (!ident->user_id) {
krista@2308
   666
            stored_own_userid = PEP_OWN_USERID;
krista@2308
   667
            ident->user_id = strdup(PEP_OWN_USERID);
krista@2308
   668
        }
krista@2308
   669
        
krista@2308
   670
        ident->me = true; // Just in case.
krista@2308
   671
        
krista@2308
   672
        // Ok, do it...
krista@2308
   673
        status = set_identity(session, ident);
krista@2308
   674
        if (status != PEP_STATUS_OK)
krista@2308
   675
            return status;
krista@2308
   676
        
krista@2308
   677
        ident_curr = ident_curr->next;
krista@2308
   678
    }
krista@2308
   679
    
krista@2308
   680
    return status;
krista@2308
   681
}
krista@2308
   682
edouard@1385
   683
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
edouard@1385
   684
{
roker@1853
   685
    return ADD_TO_LOG(_myself(session, identity, true, false));
edouard@1385
   686
}
edouard@1385
   687
vb@296
   688
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   689
        PEP_SESSION session, 
vb@292
   690
        examine_identity_t examine_identity,
vb@292
   691
        void *management
vb@292
   692
    )
vb@292
   693
{
vb@292
   694
    assert(session);
vb@292
   695
    if (!session)
vb@292
   696
        return PEP_ILLEGAL_VALUE;
vb@292
   697
vb@292
   698
    session->examine_management = management;
vb@292
   699
    session->examine_identity = examine_identity;
vb@292
   700
vb@292
   701
    return PEP_STATUS_OK;
vb@292
   702
}
vb@292
   703
vb@0
   704
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   705
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   706
        void *management
vb@0
   707
    )
vb@0
   708
{
vb@0
   709
    PEP_SESSION session;
vb@0
   710
    pEp_identity *identity;
Edouard@499
   711
    PEP_STATUS status;
vb@0
   712
vb@24
   713
    assert(retrieve_next_identity);
vb@24
   714
    assert(management);
vb@24
   715
Edouard@499
   716
    if (!retrieve_next_identity || !management)
Edouard@499
   717
        return PEP_ILLEGAL_VALUE;
Edouard@499
   718
Edouard@499
   719
    status = init(&session);
Edouard@499
   720
    assert(status == PEP_STATUS_OK);
Edouard@499
   721
    if (status != PEP_STATUS_OK)
Edouard@499
   722
        return status;
Edouard@499
   723
vb@0
   724
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   725
Edouard@499
   726
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   727
    {
vb@0
   728
        assert(identity->address);
Edouard@499
   729
        if(identity->address)
Edouard@499
   730
        {
Edouard@499
   731
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   732
edouard@1945
   733
            if (_identity_me(identity)) {
Edouard@499
   734
                status = myself(session, identity);
Edouard@499
   735
            } else {
Edouard@499
   736
                status = recv_key(session, identity->address);
Edouard@499
   737
            }
Edouard@499
   738
vb@0
   739
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   740
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   741
                return PEP_OUT_OF_MEMORY;
vb@0
   742
        }
vb@0
   743
        free_identity(identity);
vb@0
   744
    }
vb@0
   745
vb@0
   746
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   747
vb@0
   748
    release(session);
vb@0
   749
    return PEP_STATUS_OK;
vb@0
   750
}
vb@0
   751
krista@1213
   752
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
   753
        PEP_SESSION session,
vb@357
   754
        pEp_identity *ident
vb@357
   755
    )
vb@215
   756
{
vb@218
   757
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   758
vb@215
   759
    assert(session);
vb@357
   760
    assert(ident);
Edouard@439
   761
    assert(!EMPTYSTR(ident->fpr));
vb@215
   762
vb@357
   763
    if (!(session && ident && ident->fpr))
vb@215
   764
        return PEP_ILLEGAL_VALUE;
vb@215
   765
edouard@1945
   766
    if (_identity_me(ident))
Edouard@697
   767
    {
vb@357
   768
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   769
        myself(session, ident);
Edouard@697
   770
    }
Edouard@697
   771
    else
Edouard@697
   772
    {
krista@2129
   773
        // for undo
krista@2129
   774
        if (session->cached_mistrusted)
krista@2129
   775
            free(session->cached_mistrusted);
krista@2129
   776
        session->cached_mistrusted = identity_dup(ident);
Edouard@697
   777
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
   778
    }
vb@218
   779
vb@218
   780
    return status;
vb@215
   781
}
vb@215
   782
krista@2129
   783
DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session) {
krista@2129
   784
    assert(session);
krista@2129
   785
    
krista@2129
   786
    if (!session)
krista@2129
   787
        return PEP_ILLEGAL_VALUE;
krista@2129
   788
    
krista@2129
   789
    PEP_STATUS status = PEP_STATUS_OK;
krista@2129
   790
        
krista@2129
   791
    pEp_identity* cached_ident = session->cached_mistrusted;
krista@2129
   792
    
krista@2129
   793
    if (!cached_ident)
krista@2129
   794
        status = PEP_CANNOT_FIND_IDENTITY;
krista@2129
   795
    else {
krista@2129
   796
        status = set_identity(session, cached_ident);            
krista@2129
   797
        free_identity(session->cached_mistrusted);
krista@2129
   798
    }
krista@2129
   799
    
krista@2129
   800
    session->cached_mistrusted = NULL;
krista@2129
   801
krista@2129
   802
    return status;
krista@2129
   803
}
krista@2129
   804
Edouard@410
   805
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   806
        PEP_SESSION session,
Edouard@410
   807
        pEp_identity *ident
Edouard@410
   808
    )
Edouard@410
   809
{
Edouard@410
   810
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   811
Edouard@410
   812
    assert(session);
Edouard@410
   813
    assert(ident);
edouard@1945
   814
    assert(!_identity_me(ident));
Edouard@439
   815
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   816
    assert(!EMPTYSTR(ident->address));
Edouard@439
   817
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   818
edouard@1945
   819
    if (!(session && ident && !_identity_me(ident) && ident->fpr && ident->address &&
vb@420
   820
            ident->user_id))
Edouard@410
   821
        return PEP_ILLEGAL_VALUE;
Edouard@410
   822
vb@420
   823
    status = update_identity(session, ident);
vb@420
   824
    if (status != PEP_STATUS_OK)
vb@420
   825
        return status;
Edouard@410
   826
Edouard@442
   827
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   828
        ident->comm_type = PEP_ct_unknown;
vb@421
   829
    else
vb@421
   830
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   831
vb@420
   832
    status = set_identity(session, ident);
vb@421
   833
    if (status != PEP_STATUS_OK)
vb@421
   834
        return status;
vb@421
   835
vb@422
   836
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   837
        status = update_identity(session, ident);
Edouard@410
   838
    return status;
Edouard@410
   839
}
Edouard@410
   840
vb@354
   841
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   842
        PEP_SESSION session,
vb@354
   843
        pEp_identity *ident
vb@354
   844
    )
vb@354
   845
{
vb@354
   846
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   847
vb@354
   848
    assert(session);
vb@354
   849
    assert(ident);
Edouard@439
   850
    assert(!EMPTYSTR(ident->address));
Edouard@439
   851
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   852
    assert(!EMPTYSTR(ident->fpr));
vb@354
   853
Edouard@439
   854
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
edouard@1945
   855
            EMPTYSTR(ident->fpr))
vb@354
   856
        return PEP_ILLEGAL_VALUE;
vb@354
   857
vb@354
   858
    status = update_identity(session, ident);
vb@354
   859
    if (status != PEP_STATUS_OK)
vb@354
   860
        return status;
vb@354
   861
vb@356
   862
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   863
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   864
        status = set_identity(session, ident);
vb@356
   865
    }
vb@356
   866
    else {
vb@356
   867
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   868
        status = PEP_CANNOT_SET_TRUST;
vb@356
   869
    }
vb@354
   870
vb@354
   871
    return status;
vb@354
   872
}
vb@354
   873
Edouard@584
   874
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
   875
        PEP_SESSION session,
vb@955
   876
        const char *fpr,
vb@955
   877
        bool *listed
vb@955
   878
    )
Edouard@584
   879
{
Edouard@584
   880
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   881
    int count;
Edouard@584
   882
    
Edouard@584
   883
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   884
    
Edouard@584
   885
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   886
        return PEP_ILLEGAL_VALUE;
Edouard@584
   887
    
Edouard@584
   888
    *listed = false;
Edouard@584
   889
    
Edouard@584
   890
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   891
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   892
    
Edouard@584
   893
    int result;
Edouard@584
   894
    
Edouard@584
   895
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   896
    switch (result) {
Edouard@584
   897
        case SQLITE_ROW:
Edouard@584
   898
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   899
            *listed = count > 0;
krista@2288
   900
            status = PEP_STATUS_OK;
Edouard@584
   901
            break;
Edouard@584
   902
            
Edouard@584
   903
        default:
Edouard@584
   904
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   905
    }
Edouard@584
   906
    
Edouard@584
   907
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   908
    return status;
Edouard@584
   909
}
Edouard@584
   910
edouard@1412
   911
PEP_STATUS _own_identities_retrieve(
vb@955
   912
        PEP_SESSION session,
edouard@1412
   913
        identity_list **own_identities,
edouard@1412
   914
        identity_flags_t excluded_flags
vb@955
   915
      )
Edouard@584
   916
{
Edouard@584
   917
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   918
    
vb@955
   919
    assert(session && own_identities);
vb@955
   920
    if (!(session && own_identities))
Edouard@584
   921
        return PEP_ILLEGAL_VALUE;
Edouard@584
   922
    
vb@955
   923
    *own_identities = NULL;
vb@955
   924
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
   925
    if (_own_identities == NULL)
Edouard@584
   926
        goto enomem;
Edouard@584
   927
    
vb@955
   928
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   929
    
Edouard@584
   930
    int result;
vb@955
   931
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
   932
    const char *address = NULL;
Edouard@584
   933
    const char *fpr = NULL;
vb@955
   934
    const char *username = NULL;
vb@955
   935
    const char *user_id = NULL;
vb@955
   936
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
   937
    const char *lang = NULL;
vb@955
   938
    unsigned int flags = 0;
Edouard@584
   939
    
vb@955
   940
    identity_list *_bl = _own_identities;
Edouard@584
   941
    do {
edouard@1412
   942
        sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
vb@955
   943
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
   944
        switch (result) {
Edouard@584
   945
            case SQLITE_ROW:
vb@955
   946
                address = (const char *)
vb@955
   947
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
   948
                fpr = (const char *)
vb@955
   949
                    sqlite3_column_text(session->own_identities_retrieve, 1);
krista@2301
   950
                user_id = (const char *)
krista@2301
   951
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
   952
                username = (const char *)
krista@2301
   953
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
   954
                comm_type = PEP_ct_pEp;
vb@1071
   955
                lang = (const char *)
krista@2301
   956
                    sqlite3_column_text(session->own_identities_retrieve, 4);
vb@1071
   957
                flags = (unsigned int)
krista@2301
   958
                    sqlite3_column_int(session->own_identities_retrieve, 5);
vb@955
   959
vb@1078
   960
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
   961
                if (!ident)
Edouard@584
   962
                    goto enomem;
vb@955
   963
                ident->comm_type = comm_type;
vb@955
   964
                if (lang && lang[0]) {
vb@955
   965
                    ident->lang[0] = lang[0];
vb@955
   966
                    ident->lang[1] = lang[1];
vb@1078
   967
                    ident->lang[2] = 0;
vb@955
   968
                }
krista@2288
   969
                ident->me = true;
vb@955
   970
                ident->flags = flags;
vb@955
   971
vb@955
   972
                _bl = identity_list_add(_bl, ident);
vb@1080
   973
                if (_bl == NULL) {
vb@1080
   974
                    free_identity(ident);
Edouard@584
   975
                    goto enomem;
vb@1080
   976
                }
Edouard@584
   977
                
Edouard@584
   978
                break;
Edouard@584
   979
                
Edouard@584
   980
            case SQLITE_DONE:
Edouard@584
   981
                break;
Edouard@584
   982
                
Edouard@584
   983
            default:
Edouard@584
   984
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   985
                result = SQLITE_DONE;
Edouard@584
   986
        }
Edouard@584
   987
    } while (result != SQLITE_DONE);
Edouard@584
   988
    
vb@955
   989
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   990
    if (status == PEP_STATUS_OK)
vb@955
   991
        *own_identities = _own_identities;
Edouard@584
   992
    else
vb@955
   993
        free_identity_list(_own_identities);
Edouard@584
   994
    
Edouard@584
   995
    goto the_end;
Edouard@584
   996
    
Edouard@584
   997
enomem:
vb@955
   998
    free_identity_list(_own_identities);
Edouard@584
   999
    status = PEP_OUT_OF_MEMORY;
Edouard@584
  1000
    
Edouard@584
  1001
the_end:
Edouard@584
  1002
    return status;
Edouard@584
  1003
}
vb@955
  1004
edouard@1412
  1005
DYNAMIC_API PEP_STATUS own_identities_retrieve(
edouard@1364
  1006
        PEP_SESSION session,
edouard@1412
  1007
        identity_list **own_identities
edouard@1412
  1008
      )
edouard@1412
  1009
{
edouard@1412
  1010
    return _own_identities_retrieve(session, own_identities, 0);
edouard@1412
  1011
}
edouard@1412
  1012
edouard@1412
  1013
PEP_STATUS _own_keys_retrieve(
edouard@1412
  1014
        PEP_SESSION session,
edouard@1412
  1015
        stringlist_t **keylist,
edouard@1412
  1016
        identity_flags_t excluded_flags
edouard@1364
  1017
      )
edouard@1364
  1018
{
edouard@1364
  1019
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1364
  1020
    
edouard@1364
  1021
    assert(session && keylist);
edouard@1364
  1022
    if (!(session && keylist))
edouard@1364
  1023
        return PEP_ILLEGAL_VALUE;
edouard@1364
  1024
    
edouard@1364
  1025
    *keylist = NULL;
edouard@1364
  1026
    stringlist_t *_keylist = NULL;
edouard@1364
  1027
    
edouard@1394
  1028
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1029
    
edouard@1364
  1030
    int result;
edouard@1364
  1031
    char *fpr = NULL;
edouard@1364
  1032
    
edouard@1364
  1033
    stringlist_t *_bl = _keylist;
edouard@1364
  1034
    do {
edouard@1412
  1035
        sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
edouard@1394
  1036
        result = sqlite3_step(session->own_keys_retrieve);
edouard@1364
  1037
        switch (result) {
edouard@1364
  1038
            case SQLITE_ROW:
edouard@1394
  1039
                fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
edouard@1364
  1040
                if(fpr == NULL)
edouard@1364
  1041
                    goto enomem;
edouard@1364
  1042
edouard@1364
  1043
                _bl = stringlist_add(_bl, fpr);
edouard@1364
  1044
                if (_bl == NULL) {
edouard@1364
  1045
                    free(fpr);
edouard@1364
  1046
                    goto enomem;
edouard@1364
  1047
                }
edouard@1364
  1048
                if (_keylist == NULL)
edouard@1364
  1049
                    _keylist = _bl;
edouard@1364
  1050
                
edouard@1364
  1051
                break;
edouard@1364
  1052
                
edouard@1364
  1053
            case SQLITE_DONE:
edouard@1364
  1054
                break;
edouard@1364
  1055
                
edouard@1364
  1056
            default:
edouard@1364
  1057
                status = PEP_UNKNOWN_ERROR;
edouard@1364
  1058
                result = SQLITE_DONE;
edouard@1364
  1059
        }
edouard@1364
  1060
    } while (result != SQLITE_DONE);
edouard@1364
  1061
    
edouard@1394
  1062
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1063
    if (status == PEP_STATUS_OK)
edouard@1364
  1064
        *keylist = _keylist;
edouard@1364
  1065
    else
edouard@1364
  1066
        free_stringlist(_keylist);
edouard@1364
  1067
    
edouard@1364
  1068
    goto the_end;
edouard@1364
  1069
    
edouard@1364
  1070
enomem:
edouard@1364
  1071
    free_stringlist(_keylist);
edouard@1364
  1072
    status = PEP_OUT_OF_MEMORY;
edouard@1364
  1073
    
edouard@1364
  1074
the_end:
edouard@1364
  1075
    return status;
edouard@1364
  1076
}
edouard@1364
  1077
edouard@1412
  1078
DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
edouard@1412
  1079
{
edouard@1412
  1080
    return _own_keys_retrieve(session, keylist, 0);
edouard@1412
  1081
}
edouard@1412
  1082
edouard@1760
  1083
// FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
edouard@1394
  1084
DYNAMIC_API PEP_STATUS set_own_key(
edouard@1394
  1085
       PEP_SESSION session,
edouard@1394
  1086
       const char *address,
edouard@1394
  1087
       const char *fpr
edouard@1394
  1088
    )
edouard@1394
  1089
{
edouard@1394
  1090
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1394
  1091
    
edouard@1394
  1092
    assert(session &&
edouard@1760
  1093
           address &&
edouard@1394
  1094
           fpr && fpr[0]
edouard@1394
  1095
          );
edouard@1394
  1096
    
edouard@1394
  1097
    if (!(session &&
edouard@1760
  1098
          address &&
edouard@1394
  1099
          fpr && fpr[0]
edouard@1394
  1100
         ))
edouard@1394
  1101
        return PEP_ILLEGAL_VALUE;
krista@2301
  1102
            
krista@2301
  1103
            
krista@2301
  1104
    // First see if we have it in own identities already, AND we retrieve
krista@2301
  1105
    // our own user_id
krista@2301
  1106
    pEp_identity* my_id = NULL;
krista@2301
  1107
    identity_list* my_identities = NULL;
krista@2301
  1108
    char* my_user_id = NULL;
krista@2301
  1109
    status = own_identities_retrieve(session, &my_identities);
edouard@1394
  1110
    
krista@2301
  1111
    if (status == PEP_STATUS_OK) {
krista@2301
  1112
        if (my_identities) {
krista@2301
  1113
            if (!(my_identities->ident && my_identities->ident->user_id))
krista@2301
  1114
                return PEP_ILLEGAL_VALUE;
krista@2301
  1115
krista@2301
  1116
            my_user_id = strdup(my_identities->ident->user_id);
krista@2301
  1117
krista@2301
  1118
            if (!my_user_id) 
krista@2301
  1119
                return PEP_OUT_OF_MEMORY;
krista@2301
  1120
            
krista@2301
  1121
            // Probably cheaper than all the strcmps if there are many,
krista@2301
  1122
            // plus this avoids the capitalisation and . problems:
krista@2301
  1123
            
krista@2301
  1124
            status = get_identity(session, my_user_id, address, &my_id);
krista@2301
  1125
            
krista@2301
  1126
            if (status == PEP_STATUS_OK && my_id) {
krista@2304
  1127
                if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
krista@2301
  1128
                    // We're done. It was already here.
krista@2301
  1129
                    // FIXME: Do we check trust/revocation/?
krista@2301
  1130
                    goto pep_free;
krista@2301
  1131
                }            
krista@2301
  1132
            }
krista@2301
  1133
            
krista@2301
  1134
            // Otherwise, we see if there's a binding for this user_id/key
krista@2301
  1135
            // in the trust DB
edouard@1394
  1136
            
krista@2301
  1137
            // If there's an id w/ user_id + address
krista@2301
  1138
            if (my_id) {
krista@2301
  1139
                free(my_id->fpr);
krista@2301
  1140
                my_id->fpr = my_user_id;
krista@2304
  1141
                my_id->comm_type = PEP_ct_pEp;
krista@2304
  1142
                my_id->me = true; // just in case? 
krista@2301
  1143
            }
krista@2301
  1144
            else { // Else, we need a new identity
krista@2304
  1145
                my_id = new_identity(address, fpr, my_user_id, NULL); 
krista@2301
  1146
                if (status != PEP_STATUS_OK)
krista@2301
  1147
                    goto pep_free; 
krista@2304
  1148
                my_id->me = true;
krista@2304
  1149
                my_id->comm_type = PEP_ct_pEp;
krista@2301
  1150
            }
krista@2301
  1151
        }
krista@2301
  1152
        else {
krista@2301
  1153
            // I think the prerequisite should be that at least one own identity
krista@2301
  1154
            // already in the DB, so REALLY look at this.
krista@2301
  1155
            return PEP_CANNOT_FIND_IDENTITY;
krista@2301
  1156
        }
krista@2301
  1157
        
krista@2301
  1158
        status = set_identity(session, my_id);
krista@2301
  1159
    }  
edouard@1394
  1160
    
krista@2301
  1161
pep_free:
krista@2301
  1162
    free(my_id);
krista@2301
  1163
    free(my_user_id);
edouard@1394
  1164
    return status;
edouard@1394
  1165
}
krista@1357
  1166
krista@1357
  1167
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
  1168
                             bool *has_private) {
krista@1357
  1169
krista@1357
  1170
    assert(session);
krista@1357
  1171
    assert(fpr);
krista@1357
  1172
    assert(has_private);
krista@1357
  1173
    
krista@1357
  1174
    if (!(session && fpr && has_private))
krista@1357
  1175
        return PEP_ILLEGAL_VALUE;
krista@1357
  1176
krista@1357
  1177
    return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
edouard@1385
  1178
}