src/keymanagement.c
author Krista Bennett <krista@pep-project.org>
Tue, 09 Jan 2018 13:27:56 +0100
branchENGINE-289
changeset 2368 9e1609eeade3
parent 2327 cc04433060ff
child 2369 5c1d477fd5ad
permissions -rw-r--r--
ENGINE-289: updated test and code to deal with trust correctly on elected, not-in-DB keys after revocation
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
krista@2311
    24
krista@2324
    25
static bool key_matches_address(PEP_SESSION session, const char* address,
krista@2324
    26
                                const char* fpr) {
krista@2324
    27
    if (!session || !address || !fpr)
krista@2324
    28
        return false;
krista@2324
    29
    
krista@2324
    30
    bool retval = false;
krista@2324
    31
    stringlist_t *keylist = NULL;
krista@2324
    32
    PEP_STATUS status = find_keys(session, address, &keylist);
krista@2324
    33
    if (status == PEP_STATUS_OK && keylist) {
krista@2324
    34
        stringlist_t* curr = keylist;
krista@2326
    35
        while (curr) {
krista@2326
    36
            if (curr->value) {
krista@2326
    37
                if (strcasecmp(curr->value, fpr)) {
krista@2326
    38
                    retval = true;
krista@2326
    39
                    break;
krista@2326
    40
                }
krista@2324
    41
            }
krista@2326
    42
            curr = curr->next;
krista@2324
    43
        }
krista@2324
    44
    }
krista@2324
    45
    
krista@2324
    46
    free_stringlist(keylist);
krista@2324
    47
    return retval;                             
krista@2324
    48
}
krista@2324
    49
Edouard@774
    50
PEP_STATUS elect_pubkey(
Edouard@755
    51
        PEP_SESSION session, pEp_identity * identity
Edouard@755
    52
    )
Edouard@755
    53
{
Edouard@755
    54
    PEP_STATUS status;
roker@1559
    55
    stringlist_t *keylist = NULL;
krista@1342
    56
    char *_fpr = "";
Edouard@755
    57
    identity->comm_type = PEP_ct_unknown;
Edouard@755
    58
Edouard@755
    59
    status = find_keys(session, identity->address, &keylist);
Edouard@755
    60
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    61
    if (status == PEP_OUT_OF_MEMORY)
Edouard@755
    62
        return PEP_OUT_OF_MEMORY;
Edouard@755
    63
Edouard@755
    64
    stringlist_t *_keylist;
Edouard@755
    65
    for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@755
    66
        PEP_comm_type _comm_type_key;
Edouard@755
    67
Edouard@755
    68
        status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@755
    69
        assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    70
        if (status == PEP_OUT_OF_MEMORY) {
Edouard@755
    71
            free_stringlist(keylist);
Edouard@755
    72
            return PEP_OUT_OF_MEMORY;
Edouard@755
    73
        }
Edouard@755
    74
Edouard@755
    75
        if (_comm_type_key != PEP_ct_compromized &&
Edouard@755
    76
            _comm_type_key != PEP_ct_unknown)
Edouard@755
    77
        {
Edouard@755
    78
            if (identity->comm_type == PEP_ct_unknown ||
Edouard@755
    79
                _comm_type_key > identity->comm_type)
Edouard@755
    80
            {
krista@1275
    81
                bool blacklisted;
krista@1275
    82
                status = blacklist_is_listed(session, _keylist->value, &blacklisted);
krista@1275
    83
                if (status == PEP_STATUS_OK && !blacklisted) {
krista@1275
    84
                    identity->comm_type = _comm_type_key;
krista@1275
    85
                    _fpr = _keylist->value;
krista@1275
    86
                }
Edouard@755
    87
            }
Edouard@755
    88
        }
Edouard@755
    89
    }
krista@1342
    90
    
krista@1342
    91
//    if (_fpr) {
krista@1342
    92
    free(identity->fpr);
Edouard@755
    93
krista@1342
    94
    identity->fpr = strdup(_fpr);
krista@1342
    95
    if (identity->fpr == NULL) {
krista@1342
    96
        free_stringlist(keylist);
krista@1342
    97
        return PEP_OUT_OF_MEMORY;
Edouard@755
    98
    }
krista@1342
    99
//    }
Edouard@755
   100
    free_stringlist(keylist);
Edouard@755
   101
    return PEP_STATUS_OK;
Edouard@755
   102
}
Edouard@755
   103
krista@2326
   104
static PEP_STATUS validate_fpr(PEP_SESSION session, 
krista@2326
   105
                               pEp_identity* ident) {
krista@2311
   106
    
krista@2324
   107
    if (!session || !ident || !ident->fpr)
krista@2324
   108
        return PEP_ILLEGAL_VALUE;    
krista@2324
   109
        
krista@2311
   110
    char* fpr = ident->fpr;
krista@2324
   111
    
krista@2324
   112
    PEP_STATUS status = get_trust(session, ident);
krista@2324
   113
    if (status != PEP_STATUS_OK)
krista@2324
   114
        return ADD_TO_LOG(status);
krista@2326
   115
    
krista@2326
   116
    PEP_comm_type ct = ident->comm_type;
krista@2326
   117
    
krista@2325
   118
    bool revoked, expired;
krista@2326
   119
    bool blacklisted = false;
krista@2324
   120
    
krista@2311
   121
    status = key_revoked(session, fpr, &revoked);    
krista@2311
   122
    
krista@2311
   123
    assert(status == PEP_STATUS_OK);
krista@2319
   124
    
krista@2311
   125
    if (status != PEP_STATUS_OK) {
krista@2368
   126
        return ADD_TO_LOG(status);
krista@2368
   127
    }
krista@2311
   128
    
krista@2368
   129
    if (!revoked) {
krista@2368
   130
        status = key_expired(session, fpr, 
krista@2368
   131
                             time(NULL), // NOW. For _myself, this is different.
krista@2368
   132
                             &expired);
krista@2311
   133
    
krista@2368
   134
        assert(status == PEP_STATUS_OK);
krista@2326
   135
        if (status != PEP_STATUS_OK)
krista@2326
   136
            return ADD_TO_LOG(status);
krista@2368
   137
krista@2368
   138
        if ((ct | PEP_ct_confirmed) == PEP_ct_OpenPGP) {
krista@2368
   139
            status = blacklist_is_listed(session, 
krista@2368
   140
                                         fpr, 
krista@2368
   141
                                         &blacklisted);
krista@2368
   142
                                         
krista@2368
   143
            if (status != PEP_STATUS_OK)
krista@2368
   144
                return ADD_TO_LOG(status);
krista@2368
   145
        }
krista@2326
   146
    }
krista@2368
   147
            
krista@2311
   148
    if (ident->me && (ct == PEP_ct_pEp) && !revoked && expired) {
krista@2311
   149
        // extend key
krista@2311
   150
        timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
krista@2311
   151
        status = renew_key(session, fpr, ts);
krista@2311
   152
        free_timestamp(ts);
krista@2311
   153
krista@2311
   154
        if (status == PEP_STATUS_OK) {
krista@2311
   155
            // if key is valid (second check because pEp key might be extended above)
krista@2311
   156
            //      Return fpr        
krista@2326
   157
            status = key_expired(session, fpr, time(NULL), &expired);            
krista@2319
   158
            if (status != PEP_STATUS_OK)
krista@2319
   159
                 return ADD_TO_LOG(status);
krista@2311
   160
            // communicate key(?)
krista@2311
   161
        }        
krista@2311
   162
    }
krista@2311
   163
    
krista@2311
   164
    if (revoked)
krista@2326
   165
        ct = PEP_ct_key_revoked;
krista@2311
   166
    else if (expired)
krista@2326
   167
        ct = PEP_ct_key_expired;        
krista@2326
   168
    else if (blacklisted) {
krista@2326
   169
        ident->comm_type = ct = PEP_ct_key_not_found;
krista@2325
   170
        free(ident->fpr);
krista@2325
   171
            ident->fpr = strdup("");
krista@2325
   172
        status = PEP_KEY_UNSUITABLE;
krista@2325
   173
    }
krista@2311
   174
    
krista@2311
   175
    switch (ct) {
krista@2311
   176
        case PEP_ct_key_expired:
krista@2326
   177
        case PEP_ct_key_revoked:
krista@2311
   178
        case PEP_ct_key_b0rken:
krista@2311
   179
            // delete key from being default key for all users/identities
krista@2319
   180
            status = remove_fpr_as_default(session, fpr);
krista@2326
   181
            status = update_trust_for_fpr(session, 
krista@2326
   182
                                          fpr, 
krista@2326
   183
                                          ct);
krista@2325
   184
            free(ident->fpr);
krista@2325
   185
            ident->fpr = strdup("");
krista@2326
   186
            ident->comm_type = PEP_ct_key_not_found;            
krista@2325
   187
            status = PEP_KEY_UNSUITABLE;
krista@2311
   188
        default:
krista@2311
   189
            break;
krista@2311
   190
    }            
krista@2311
   191
krista@2325
   192
    return status;
krista@2311
   193
}
krista@2311
   194
krista@2311
   195
// Only call on retrieval of previously stored identity!
krista@2311
   196
// Also, we presume that if the stored_identity was sent in
krista@2311
   197
// without an fpr, there wasn't one in the trust DB for this
krista@2311
   198
// identity.
krista@2326
   199
PEP_STATUS get_valid_pubkey(PEP_SESSION session,
krista@2326
   200
                            pEp_identity* stored_identity,
krista@2324
   201
                            bool* is_identity_default,
krista@2324
   202
                            bool* is_user_default,
krista@2324
   203
                            bool* is_address_default) {
krista@2311
   204
    
krista@2311
   205
    PEP_STATUS status = PEP_STATUS_OK;
krista@2311
   206
krista@2324
   207
    if (!stored_identity || !stored_identity->user_id
krista@2324
   208
        || !is_identity_default || !is_user_default || !is_address_default)
krista@2311
   209
        return PEP_ILLEGAL_VALUE;
krista@2311
   210
        
krista@2324
   211
    *is_identity_default = *is_user_default = *is_address_default = false;
krista@2324
   212
    
krista@2311
   213
    char* stored_fpr = stored_identity->fpr;
krista@2311
   214
    // Input: stored identity retrieved from database
krista@2311
   215
    // if stored identity contains a default key
krista@2311
   216
    if (stored_fpr) {
krista@2311
   217
        status = validate_fpr(session, stored_identity);    
krista@2324
   218
        if (status == PEP_STATUS_OK && stored_identity->fpr) {
krista@2324
   219
            *is_identity_default = *is_address_default = true;
krista@2311
   220
            return status;
krista@2324
   221
        }
krista@2311
   222
    }
krista@2311
   223
    // if no valid default stored identity key found
krista@2324
   224
    free(stored_identity->fpr);
krista@2324
   225
    stored_identity->fpr = NULL;
krista@2324
   226
    
krista@2311
   227
    // try to get default key for user_data
krista@2311
   228
    sqlite3_reset(session->get_user_default_key);
krista@2311
   229
    sqlite3_bind_text(session->get_user_default_key, 1, stored_identity->user_id, 
krista@2311
   230
                      -1, SQLITE_STATIC);
krista@2311
   231
    
krista@2311
   232
    const int result = sqlite3_step(session->get_user_default_key);
krista@2324
   233
    const char* user_fpr = NULL;
krista@2311
   234
    if (result == SQLITE_ROW) {
krista@2326
   235
        user_fpr =
krista@2326
   236
            (char *) sqlite3_column_text(session->get_user_default_key, 0);
krista@2311
   237
    }
krista@2324
   238
    sqlite3_reset(session->get_user_default_key);
krista@2311
   239
    
krista@2324
   240
    if (user_fpr) {             
krista@2324
   241
        // There exists a default key for user, so validate
krista@2326
   242
        stored_identity->fpr = strdup(user_fpr);
krista@2324
   243
        status = validate_fpr(session, stored_identity);
krista@2324
   244
        if (status == PEP_STATUS_OK && stored_identity->fpr) {
krista@2324
   245
            *is_user_default = true;
krista@2324
   246
            *is_address_default = key_matches_address(session, 
krista@2324
   247
                                                      stored_identity->address,
krista@2324
   248
                                                      stored_identity->fpr);
krista@2324
   249
            return status;
krista@2324
   250
        }        
krista@2311
   251
    }
krista@2324
   252
    
krista@2368
   253
    status = elect_pubkey(session, stored_identity);
krista@2368
   254
    
krista@2368
   255
    if (status == PEP_STATUS_OK) {
krista@2368
   256
        switch (stored_identity->comm_type) {
krista@2368
   257
            case PEP_ct_key_revoked:
krista@2368
   258
            case PEP_ct_key_b0rken:
krista@2368
   259
            case PEP_ct_key_expired:
krista@2368
   260
            case PEP_ct_compromized:
krista@2368
   261
            case PEP_ct_mistrusted:
krista@2368
   262
                // this only happens when it's all there is
krista@2368
   263
                status = PEP_KEY_NOT_FOUND;
krista@2368
   264
                free(stored_identity->fpr);
krista@2368
   265
                stored_identity->fpr = NULL;
krista@2368
   266
                stored_identity->comm_type = PEP_ct_unknown;
krista@2368
   267
                break;
krista@2368
   268
            default:
krista@2368
   269
                // FIXME: blacklisting?
krista@2368
   270
                break;
krista@2368
   271
        }
krista@2368
   272
    }
krista@2368
   273
    return status;
krista@2311
   274
}
krista@2311
   275
edouard@1406
   276
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
edouard@1385
   277
krista@2325
   278
static void transfer_ident_lang_and_flags(pEp_identity* new_ident,
krista@2325
   279
                                          pEp_identity* stored_ident) {
krista@2325
   280
    if (new_ident->lang[0] == 0) {
krista@2325
   281
      new_ident->lang[0] = stored_ident->lang[0];
krista@2325
   282
      new_ident->lang[1] = stored_ident->lang[1];
krista@2325
   283
      new_ident->lang[2] = 0;
krista@2325
   284
    }
krista@2325
   285
krista@2325
   286
    new_ident->flags = stored_ident->flags;
krista@2327
   287
    new_ident->me = new_ident->me || stored_ident->me;
krista@2325
   288
}
krista@2325
   289
krista@2325
   290
static PEP_STATUS prepare_updated_identity(PEP_SESSION session,
krista@2368
   291
                                                 pEp_identity* return_id,
krista@2325
   292
                                                 pEp_identity* stored_ident,
krista@2325
   293
                                                 bool store) {
krista@2325
   294
    
krista@2368
   295
    if (!session || !return_id || !stored_ident)
krista@2325
   296
        return PEP_ILLEGAL_VALUE;
krista@2326
   297
    
krista@2326
   298
    PEP_STATUS status;
krista@2326
   299
    
krista@2325
   300
    bool is_identity_default, is_user_default, is_address_default;
krista@2325
   301
    status = get_valid_pubkey(session, stored_ident,
krista@2325
   302
                                &is_identity_default,
krista@2325
   303
                                &is_user_default,
krista@2325
   304
                                &is_address_default);
krista@2325
   305
                                
krista@2368
   306
    if (status == PEP_STATUS_OK && stored_ident->fpr && *(stored_ident->fpr) != '\0') {
krista@2368
   307
    // set identity comm_type from trust db (user_id, FPR)
krista@2325
   308
        status = get_trust(session, stored_ident);
krista@2368
   309
        if (status == PEP_CANNOT_FIND_IDENTITY) {
krista@2368
   310
            // This is OK - there is no trust DB entry, but we
krista@2368
   311
            // found a key. We won't store this, but we'll
krista@2368
   312
            // use it.
krista@2368
   313
            PEP_comm_type ct = PEP_ct_unknown;
krista@2368
   314
            status = get_key_rating(session, stored_ident->fpr, &ct);
krista@2368
   315
            stored_ident->comm_type = ct;
krista@2368
   316
        }
krista@2368
   317
        if (status != PEP_STATUS_OK) {
krista@2325
   318
            return status; // FIXME - free mem
krista@2368
   319
        }
krista@2368
   320
        if (return_id->fpr) {
krista@2368
   321
            if (strcasecmp(stored_ident->fpr, return_id->fpr) != 0) {
krista@2368
   322
                free(return_id->fpr);
krista@2368
   323
                
krista@2368
   324
                // Copy in the result from get_valid_pubkey
krista@2368
   325
                return_id->fpr = strdup(stored_ident->fpr);
krista@2368
   326
            }
krista@2368
   327
            return_id->comm_type = stored_ident->comm_type;            
krista@2325
   328
        }
krista@2325
   329
    }
krista@2325
   330
    else {
krista@2325
   331
        return status; // Couldn't find a key.
krista@2325
   332
    }
krista@2325
   333
                
krista@2325
   334
    // We patch the DB with the input username, but if we didn't have
krista@2325
   335
    // one, we pull it out of storage if available.
krista@2325
   336
    // (also, if the input username is "anonymous" and there exists
krista@2325
   337
    //  a DB username, we replace)
krista@2325
   338
    if (stored_ident->username) {
krista@2368
   339
        if (return_id->username && 
krista@2368
   340
            (strcasecmp(return_id->username, "anonymous") == 0)) {
krista@2368
   341
            free(return_id->username);
krista@2368
   342
            return_id->username = NULL;
krista@2325
   343
        }
krista@2368
   344
        if (!return_id->username)
krista@2368
   345
            return_id->username = strdup(stored_ident->username);
krista@2325
   346
    }
krista@2325
   347
        
krista@2325
   348
    // Call set_identity() to store
krista@2325
   349
    if ((is_identity_default || is_user_default) &&
krista@2325
   350
         is_address_default) {                 
krista@2325
   351
         // if we got an fpr which is default for either user
krista@2325
   352
         // or identity AND is valid for this address, set in DB
krista@2325
   353
         // as default
krista@2368
   354
         status = set_identity(session, return_id);
krista@2325
   355
    }
krista@2325
   356
    else {
krista@2325
   357
        // Store without default fpr/ct, but return the fpr and ct 
krista@2325
   358
        // for current use
krista@2368
   359
        char* save_fpr = return_id->fpr;
krista@2368
   360
        PEP_comm_type save_ct = return_id->comm_type;
krista@2368
   361
        return_id->fpr = NULL;
krista@2368
   362
        return_id->comm_type = PEP_ct_unknown;
krista@2368
   363
        status = set_identity(session, return_id);
krista@2368
   364
        return_id->fpr = save_fpr;
krista@2368
   365
        return_id->comm_type = save_ct;
krista@2325
   366
    }
krista@2325
   367
    
krista@2368
   368
    transfer_ident_lang_and_flags(return_id, stored_ident);
krista@2325
   369
    
krista@2325
   370
    return status;
krista@2325
   371
}
krista@2325
   372
krista@2325
   373
edouard@1385
   374
DYNAMIC_API PEP_STATUS update_identity(
edouard@1385
   375
        PEP_SESSION session, pEp_identity * identity
vb@0
   376
    )
vb@0
   377
{
vb@0
   378
    PEP_STATUS status;
vb@0
   379
vb@0
   380
    assert(session);
vb@0
   381
    assert(identity);
Edouard@439
   382
    assert(!EMPTYSTR(identity->address));
vb@0
   383
Edouard@439
   384
    if (!(session && identity && !EMPTYSTR(identity->address)))
roker@1853
   385
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
   386
krista@2311
   387
    char* own_id = NULL;
krista@2311
   388
    status = get_own_userid(session, &own_id);    
krista@2311
   389
krista@2311
   390
    // Is this me, temporary or not? If so, _myself() is the right call.
krista@2311
   391
    if (identity->me || 
krista@2311
   392
       (own_id && identity->user_id && (strcmp(own_id, identity->user_id) == 0))) 
krista@2311
   393
    {
krista@2311
   394
        status = _myself(session, identity, false, true);
krista@2311
   395
        free(own_id);
krista@2311
   396
        return status;
vb@1078
   397
    }
vb@1078
   398
krista@2311
   399
    // We have, at least, an address.
krista@2311
   400
    // Retrieve stored identity information!    
krista@2311
   401
    pEp_identity* stored_ident = NULL;
krista@2324
   402
krista@2311
   403
    if (identity->user_id) {            
krista@2324
   404
        // (we're gonna update the trust/fpr anyway, so we user the no-fpr-from-trust-db variant)
krista@2311
   405
        //      * do get_identity() to retrieve stored identity information
krista@2324
   406
        status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
krista@2317
   407
krista@2317
   408
        // Before we start - if there was no stored identity, we should check to make sure we don't
krista@2317
   409
        // have a stored identity with a temporary user_id that differs from the input user_id. This
krista@2317
   410
        // happens in multithreaded environments sometimes.
krista@2317
   411
        if (!stored_ident) {
krista@2317
   412
            identity_list* id_list = NULL;
krista@2317
   413
            status = get_identities_by_address(session, identity->address, &id_list);
krista@2317
   414
krista@2317
   415
            if (id_list) {
krista@2317
   416
                identity_list* id_curr = id_list;
krista@2317
   417
                while (id_curr) {
krista@2317
   418
                    pEp_identity* this_id = id_curr->ident;
krista@2317
   419
                    if (this_id) {
krista@2317
   420
                        char* this_uid = this_id->user_id;
krista@2317
   421
                        if (this_uid && (strstr(this_uid, "TOFU_") == this_uid)) {
krista@2317
   422
                            // FIXME: should we also be fixing pEp_own_userId in this
krista@2317
   423
                            // function here?
krista@2317
   424
                            
krista@2324
   425
                            // if usernames match, we replace the userid. Or if the temp username
krista@2324
   426
                            // is anonymous.
krista@2324
   427
                            if (!this_id->username ||
krista@2324
   428
                                strcasecmp(this_id->username, "anonymous") == 0 ||
krista@2324
   429
                                (identity->username && 
krista@2324
   430
                                 strcasecmp(identity->username, 
krista@2324
   431
                                            this_id->username) == 0)) {
krista@2317
   432
                                
krista@2324
   433
                                // Ok, we have a temp ID. We have to replace this
krista@2324
   434
                                // with the real ID.
krista@2326
   435
                                status = replace_userid(session, 
krista@2326
   436
                                                        this_uid, 
krista@2326
   437
                                                        identity->user_id);
krista@2324
   438
                                if (status != PEP_STATUS_OK) {
krista@2324
   439
                                    free_identity_list(id_list);
krista@2324
   440
                                    return status;
krista@2324
   441
                                }
krista@2324
   442
                                    
krista@2324
   443
                                free(this_uid);
krista@2324
   444
                                
krista@2324
   445
                                // Reflect the change we just made to the DB
krista@2324
   446
                                this_id->user_id = strdup(identity->user_id);
krista@2324
   447
                                stored_ident = this_id;
krista@2324
   448
                                // FIXME: free list.
krista@2324
   449
                                break;                                
krista@2324
   450
                            }                            
krista@2317
   451
                        } 
krista@2317
   452
                    }
krista@2317
   453
                    id_curr = id_curr->next;
krista@2317
   454
                }
krista@2317
   455
            }
krista@2317
   456
        } 
krista@2327
   457
                
krista@2324
   458
        if (status == PEP_STATUS_OK && stored_ident) { 
krista@2324
   459
            //  * if identity available
krista@2324
   460
            //      * patch it with username
krista@2324
   461
            //          (note: this will happen when 
krista@2324
   462
            //           setting automatically below...)
krista@2324
   463
            //      * elect valid key for identity
krista@2324
   464
            //    * if valid key exists
krista@2324
   465
            //        * set return value's fpr
krista@2325
   466
            status = prepare_updated_identity(session,
krista@2325
   467
                                              identity,
krista@2325
   468
                                              stored_ident, true);
krista@2311
   469
        }
krista@2324
   470
        //  * else (identity unavailable)
krista@2311
   471
        else {
krista@2324
   472
            //  if we only have user_id and address and identity not available
krista@2311
   473
            //      * return error status (identity not found)
krista@2324
   474
            if (!identity->username)
krista@2326
   475
                status = PEP_CANNOT_FIND_IDENTITY;
krista@2311
   476
            
krista@2324
   477
            // Otherwise, if we had user_id, address, and username:
krista@2324
   478
            //    * create identity with user_id, address, username
krista@2324
   479
            //      (this is the input id without the fpr + comm type!)
krista@2324
   480
            free(identity->fpr);
krista@2324
   481
            identity->fpr = NULL;
krista@2324
   482
            identity->comm_type = PEP_ct_unknown;
krista@2324
   483
            
krista@2324
   484
            //    * We've already checked and retrieved
krista@2324
   485
            //      any applicable temporary identities above. If we're 
krista@2324
   486
            //      here, none of them fit.
krista@2324
   487
            //    * call set_identity() to store
krista@2324
   488
            if (status == PEP_STATUS_OK) {
krista@2324
   489
                status = set_identity(session, identity);
krista@2368
   490
                if (status == PEP_STATUS_OK) {
krista@2368
   491
                    elect_pubkey(session, identity);
krista@2368
   492
                }
krista@2324
   493
            }
krista@2324
   494
            //  * Return: created identity
krista@2324
   495
        }        
krista@2311
   496
    }
krista@2311
   497
    else if (identity->username) {
krista@2311
   498
        /*
krista@2311
   499
         * Temporary identity information with username supplied
krista@2311
   500
            * Input: address, username (no others)
krista@2311
   501
         */
krista@2324
   502
        identity_list* id_list = NULL;
krista@2324
   503
        status = get_identities_by_address(session, identity->address, &id_list);
krista@2324
   504
krista@2311
   505
        //  * Search for an identity with non-temporary user_id with that mapping
krista@2324
   506
        if (id_list) {
krista@2324
   507
            identity_list* id_curr = id_list;
krista@2324
   508
            while (id_curr) {
krista@2324
   509
                pEp_identity* this_id = id_curr->ident;
krista@2324
   510
                if (this_id) {
krista@2324
   511
                    char* this_uid = this_id->user_id;
krista@2325
   512
                    if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
krista@2324
   513
                        // FIXME: should we also be fixing pEp_own_userId in this
krista@2324
   514
                        // function here?
krista@2324
   515
                        
krista@2324
   516
                        // if usernames match, we replace the userid.
krista@2324
   517
                        if (identity->username && 
krista@2324
   518
                            strcasecmp(identity->username, 
krista@2324
   519
                                       this_id->username) == 0) {
krista@2324
   520
                            
krista@2324
   521
                            // Ok, we have a real ID. Copy it!
krista@2324
   522
                            identity->user_id = strdup(this_uid);
krista@2324
   523
                            
krista@2324
   524
                            if (!identity->user_id)
krista@2324
   525
                                status = PEP_OUT_OF_MEMORY;
krista@2324
   526
                            stored_ident = this_id;
krista@2324
   527
                            
krista@2324
   528
                            break;                                
krista@2324
   529
                        }                            
krista@2324
   530
                    } 
krista@2324
   531
                }
krista@2324
   532
                id_curr = id_curr->next;
krista@2324
   533
            }
krista@2324
   534
        }
krista@2324
   535
krista@2324
   536
        if (stored_ident) {
krista@2325
   537
            status = prepare_updated_identity(session,
krista@2325
   538
                                              identity,
krista@2325
   539
                                              stored_ident, true);
krista@2324
   540
        }
krista@2324
   541
        else {
krista@2325
   542
            // create temporary identity, store it, and Return this
krista@2325
   543
            // This means TOFU_ user_id
krista@2325
   544
            identity->user_id = calloc(1, strlen(identity->address) + 6);
krista@2325
   545
            if (!identity->user_id)
krista@2325
   546
                return PEP_OUT_OF_MEMORY;
krista@2325
   547
krista@2325
   548
            snprintf(identity->user_id, strlen(identity->address) + 6,
krista@2325
   549
                     "TOFU_%s", identity->address);        
krista@2325
   550
            
krista@2325
   551
            free(identity->fpr);
krista@2325
   552
            identity->fpr = NULL;
krista@2325
   553
            identity->comm_type = PEP_ct_unknown;
krista@2325
   554
             
krista@2325
   555
            //    * We've already checked and retrieved
krista@2325
   556
            //      any applicable temporary identities above. If we're 
krista@2325
   557
            //      here, none of them fit.
krista@2325
   558
            //    * call set_identity() to store
krista@2325
   559
            status = set_identity(session, identity);
krista@2368
   560
            if (status == PEP_STATUS_OK) {
krista@2368
   561
                elect_pubkey(session, identity);
krista@2368
   562
            }
krista@2324
   563
        }
krista@2311
   564
    }
krista@2311
   565
    else {
krista@2311
   566
        /*
krista@2311
   567
         * Temporary identity information without username suplied
krista@2311
   568
            * Input: address (no others)
krista@2311
   569
         */
krista@2325
   570
        identity_list* id_list = NULL;
krista@2325
   571
        status = get_identities_by_address(session, identity->address, &id_list);
krista@2325
   572
krista@2311
   573
        //    * Search for identity with this address
krista@2325
   574
        if (id_list && !(id_list->next)) { // exactly one            
krista@2325
   575
            //    * If exactly one found
krista@2325
   576
            //      * elect valid key for identity (see below)
krista@2325
   577
            //      * Return this identity
krista@2326
   578
            stored_ident = id_list->ident;
krista@2325
   579
            
krista@2326
   580
            if (stored_ident)
krista@2325
   581
                status = prepare_updated_identity(session, identity,
krista@2325
   582
                                                  stored_ident, false);
krista@2325
   583
            else
krista@2325
   584
                status = PEP_CANNOT_FIND_IDENTITY;
krista@2325
   585
        }
krista@2325
   586
        else // too little info
krista@2325
   587
            status = PEP_CANNOT_FIND_IDENTITY; 
krista@2311
   588
    }
krista@2326
   589
    
krista@2326
   590
    // FIXME: This is legacy. I presume it's a notification for the caller...
krista@2326
   591
    // Revisit once I can talk to Volker
krista@2326
   592
    if (identity->comm_type != PEP_ct_compromized &&
krista@2326
   593
        identity->comm_type < PEP_ct_strong_but_unconfirmed)
krista@2326
   594
    if (session->examine_identity)
krista@2326
   595
        session->examine_identity(identity, session->examine_management);
krista@2326
   596
roker@1853
   597
    return ADD_TO_LOG(status);
vb@0
   598
}
vb@0
   599
Edouard@774
   600
PEP_STATUS elect_ownkey(
krista@1194
   601
        PEP_SESSION session, pEp_identity * identity
Edouard@774
   602
    )
Edouard@774
   603
{
Edouard@774
   604
    PEP_STATUS status;
Edouard@774
   605
    stringlist_t *keylist = NULL;
Edouard@774
   606
Edouard@774
   607
    free(identity->fpr);
Edouard@774
   608
    identity->fpr = NULL;
Edouard@774
   609
krista@1357
   610
    status = find_private_keys(session, identity->address, &keylist);
Edouard@774
   611
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   612
    if (status == PEP_OUT_OF_MEMORY)
Edouard@774
   613
        return PEP_OUT_OF_MEMORY;
Edouard@774
   614
    
Edouard@774
   615
    if (keylist != NULL && keylist->value != NULL)
Edouard@774
   616
    {
Edouard@774
   617
        char *_fpr = NULL;
Edouard@774
   618
        identity->comm_type = PEP_ct_unknown;
Edouard@774
   619
Edouard@774
   620
        stringlist_t *_keylist;
Edouard@774
   621
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@774
   622
            bool is_own = false;
Edouard@774
   623
            
edouard@1752
   624
            status = own_key_is_listed(session, _keylist->value, &is_own);
edouard@1752
   625
            assert(status == PEP_STATUS_OK);
edouard@1752
   626
            if (status != PEP_STATUS_OK) {
edouard@1752
   627
                free_stringlist(keylist);
edouard@1752
   628
                return status;
Edouard@774
   629
            }
Edouard@774
   630
            
edouard@1752
   631
            if (is_own)
Edouard@774
   632
            {
Edouard@774
   633
                PEP_comm_type _comm_type_key;
Edouard@774
   634
                
Edouard@774
   635
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@774
   636
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   637
                if (status == PEP_OUT_OF_MEMORY) {
Edouard@774
   638
                    free_stringlist(keylist);
Edouard@774
   639
                    return PEP_OUT_OF_MEMORY;
Edouard@774
   640
                }
Edouard@774
   641
                
Edouard@774
   642
                if (_comm_type_key != PEP_ct_compromized &&
Edouard@774
   643
                    _comm_type_key != PEP_ct_unknown)
Edouard@774
   644
                {
Edouard@774
   645
                    if (identity->comm_type == PEP_ct_unknown ||
Edouard@774
   646
                        _comm_type_key > identity->comm_type)
Edouard@774
   647
                    {
Edouard@774
   648
                        identity->comm_type = _comm_type_key;
Edouard@774
   649
                        _fpr = _keylist->value;
Edouard@774
   650
                    }
Edouard@774
   651
                }
Edouard@774
   652
            }
Edouard@774
   653
        }
Edouard@774
   654
        
Edouard@774
   655
        if (_fpr)
Edouard@774
   656
        {
Edouard@774
   657
            identity->fpr = strdup(_fpr);
Edouard@774
   658
            assert(identity->fpr);
Edouard@774
   659
            if (identity->fpr == NULL)
Edouard@774
   660
            {
Edouard@774
   661
                free_stringlist(keylist);
Edouard@774
   662
                return PEP_OUT_OF_MEMORY;
Edouard@774
   663
            }
Edouard@774
   664
        }
Edouard@774
   665
        free_stringlist(keylist);
Edouard@774
   666
    }
Edouard@774
   667
    return PEP_STATUS_OK;
Edouard@774
   668
}
Edouard@774
   669
krista@1357
   670
PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
krista@1357
   671
                                bool* is_usable) {
krista@1357
   672
    
krista@1357
   673
    bool dont_use_fpr = true;
krista@1357
   674
    
krista@1357
   675
    PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
krista@1371
   676
    if (status == PEP_STATUS_OK && !dont_use_fpr) {
krista@1357
   677
        // Make sure there is a *private* key associated with this fpr
krista@1357
   678
        bool has_private = false;
krista@1357
   679
        status = contains_priv_key(session, fpr, &has_private);
krista@1371
   680
krista@1371
   681
        if (status == PEP_STATUS_OK)
krista@1371
   682
            dont_use_fpr = !has_private;
krista@1357
   683
    }
krista@1357
   684
    
krista@1357
   685
    *is_usable = !dont_use_fpr;
krista@1357
   686
    
roker@1853
   687
    return ADD_TO_LOG(status);
krista@1357
   688
}
krista@1357
   689
edouard@1406
   690
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
vb@0
   691
{
roker@1559
   692
    pEp_identity *stored_identity = NULL;
vb@0
   693
    PEP_STATUS status;
vb@0
   694
vb@0
   695
    assert(session);
vb@0
   696
    assert(identity);
vb@1044
   697
    assert(!EMPTYSTR(identity->address));
vb@1043
   698
krista@2303
   699
    char* own_id = NULL;
krista@2303
   700
    status = get_own_userid(session, &own_id);
krista@2303
   701
krista@2308
   702
Edouard@658
   703
    assert(EMPTYSTR(identity->user_id) ||
krista@2305
   704
           (own_id && strcmp(identity->user_id, own_id) == 0) ||
krista@2305
   705
           !own_id);
vb@0
   706
vb@1044
   707
    if (!(session && identity && !EMPTYSTR(identity->address) &&
vb@1043
   708
            (EMPTYSTR(identity->user_id) ||
krista@2305
   709
            (own_id && strcmp(identity->user_id, own_id) == 0) ||
krista@2305
   710
             !own_id)))
roker@1853
   711
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
   712
krista@2308
   713
    if (!own_id) {
krista@2308
   714
        // check to see if we have ANY identity for this address... could have
krista@2308
   715
        // happened due to race condition
krista@2308
   716
    }
krista@2308
   717
    
vb@0
   718
    identity->comm_type = PEP_ct_pEp;
krista@2288
   719
    identity->me = true;
edouard@1406
   720
    if(ignore_flags)
edouard@1406
   721
        identity->flags = 0;
Edouard@658
   722
    
vb@1043
   723
    if (EMPTYSTR(identity->user_id))
Edouard@658
   724
    {
Edouard@658
   725
        free(identity->user_id);
krista@2303
   726
        identity->user_id = (own_id ? own_id : strdup(PEP_OWN_USERID));
Edouard@658
   727
        assert(identity->user_id);
Edouard@658
   728
        if (identity->user_id == NULL)
Edouard@658
   729
            return PEP_OUT_OF_MEMORY;
Edouard@658
   730
    }
vb@0
   731
edouard@1488
   732
    if (EMPTYSTR(identity->username))
edouard@1488
   733
    {
edouard@1488
   734
        free(identity->username);
krista@2219
   735
        identity->username = strdup("Anonymous");
edouard@1488
   736
        assert(identity->username);
edouard@1488
   737
        if (identity->username == NULL)
edouard@1488
   738
            return PEP_OUT_OF_MEMORY;
edouard@1488
   739
    }
edouard@1488
   740
vb@215
   741
    DEBUG_LOG("myself", "debug", identity->address);
vb@1044
   742
 
Edouard@560
   743
    status = get_identity(session,
Edouard@560
   744
                          identity->address,
Edouard@560
   745
                          identity->user_id,
Edouard@560
   746
                          &stored_identity);
Edouard@560
   747
    
vb@0
   748
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   749
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   750
        return PEP_OUT_OF_MEMORY;
krista@1357
   751
krista@1357
   752
    bool dont_use_stored_fpr = true;
krista@1357
   753
    bool dont_use_input_fpr = true;
krista@1359
   754
        
Edouard@560
   755
    if (stored_identity)
Edouard@560
   756
    {
Edouard@560
   757
        if (EMPTYSTR(identity->fpr)) {
krista@1352
   758
            
krista@1357
   759
            bool has_private = false;
krista@1352
   760
            
krista@1357
   761
            status = _has_usable_priv_key(session, stored_identity->fpr, &has_private); 
krista@1352
   762
            
krista@1371
   763
            // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
krista@1357
   764
            if (has_private) {
krista@1357
   765
                identity->fpr = strdup(stored_identity->fpr);
krista@1357
   766
                assert(identity->fpr);
krista@1357
   767
                if (identity->fpr == NULL)
krista@1357
   768
                {
krista@1357
   769
                    return PEP_OUT_OF_MEMORY;
krista@1357
   770
                }
krista@1357
   771
                dont_use_stored_fpr = false;
Edouard@560
   772
            }
Edouard@560
   773
        }
krista@1357
   774
        
edouard@1406
   775
        identity->flags = (identity->flags & 255) | stored_identity->flags;
edouard@1367
   776
        free_identity(stored_identity);
Edouard@588
   777
    }
krista@1357
   778
    
krista@1357
   779
    if (dont_use_stored_fpr && !EMPTYSTR(identity->fpr))
Edouard@588
   780
    {
Edouard@588
   781
        // App must have a good reason to give fpr, such as explicit
Edouard@588
   782
        // import of private key, or similar.
Edouard@588
   783
Edouard@658
   784
        // Take given fpr as-is.
vb@934
   785
krista@1357
   786
        // BUT:
krista@1357
   787
        // First check to see if it's blacklisted or private part is missing?
krista@1357
   788
        bool has_private = false;
krista@1357
   789
        
krista@1357
   790
        status = _has_usable_priv_key(session, identity->fpr, &has_private); 
krista@1357
   791
        
krista@1371
   792
        // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
krista@1357
   793
        if (has_private) {
krista@1357
   794
            dont_use_input_fpr = false;
krista@1357
   795
        }
Edouard@560
   796
    }
krista@1357
   797
    
krista@1357
   798
    // Ok, we failed to get keys either way, so let's elect one.
krista@1357
   799
    if (dont_use_input_fpr && dont_use_stored_fpr)
Edouard@560
   800
    {
Edouard@774
   801
        status = elect_ownkey(session, identity);
Edouard@774
   802
        assert(status == PEP_STATUS_OK);
Edouard@774
   803
        if (status != PEP_STATUS_OK) {
roker@1853
   804
            return ADD_TO_LOG(status);
Edouard@560
   805
        }
vb@934
   806
krista@1357
   807
        bool has_private = false;
krista@1359
   808
        if (identity->fpr) {
krista@1359
   809
            // ok, we elected something.
krista@1359
   810
            // elect_ownkey only returns private keys, so we don't check again.
krista@1359
   811
            // Check to see if it's blacklisted
krista@1359
   812
            bool listed;
krista@1359
   813
            status = blacklist_is_listed(session, identity->fpr, &listed); 
krista@1371
   814
krista@1371
   815
            if (status == PEP_STATUS_OK)
krista@1371
   816
                has_private = !listed;
krista@1359
   817
        }
krista@1357
   818
        
krista@1357
   819
        if (has_private) {
krista@1357
   820
            dont_use_input_fpr = false;
krista@1357
   821
        }
krista@1357
   822
        else { // OK, we've tried everything. Time to generate new keys.
krista@1359
   823
            free(identity->fpr); // It can stay in this state (unallocated) because we'll generate a new key 
krista@1359
   824
            identity->fpr = NULL;
krista@1357
   825
        }
Edouard@560
   826
    }
Edouard@695
   827
Edouard@695
   828
    bool revoked = false;
Edouard@695
   829
    char *r_fpr = NULL;
Edouard@695
   830
    if (!EMPTYSTR(identity->fpr))
Edouard@695
   831
    {
Edouard@695
   832
        status = key_revoked(session, identity->fpr, &revoked);
Edouard@775
   833
edouard@1752
   834
        if (status != PEP_STATUS_OK) 
Edouard@775
   835
        {
roker@1853
   836
            return ADD_TO_LOG(status);
Edouard@695
   837
        }
Edouard@695
   838
    }
edouard@1140
   839
   
Edouard@695
   840
    if (EMPTYSTR(identity->fpr) || revoked)
roker@1734
   841
    {
edouard@1385
   842
        if(!do_keygen){
roker@1853
   843
            return ADD_TO_LOG(PEP_GET_KEY_FAILED);
edouard@1385
   844
        }
edouard@1385
   845
Edouard@695
   846
        if(revoked)
Edouard@695
   847
        {
Edouard@697
   848
            r_fpr = identity->fpr;
Edouard@697
   849
            identity->fpr = NULL;
Edouard@695
   850
        }
Edouard@560
   851
        
vb@215
   852
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   853
        status = generate_keypair(session, identity);
vb@0
   854
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   855
        if (status != PEP_STATUS_OK) {
vb@0
   856
            char buf[11];
vb@0
   857
            snprintf(buf, 11, "%d", status);
vb@215
   858
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   859
            if(revoked && r_fpr)
Edouard@695
   860
                free(r_fpr);
roker@1853
   861
            return ADD_TO_LOG(status);
vb@0
   862
        }
edouard@1140
   863
Edouard@560
   864
        
Edouard@695
   865
        if(revoked)
Edouard@695
   866
        {
Edouard@695
   867
            status = set_revoked(session, r_fpr,
Edouard@695
   868
                                 identity->fpr, time(NULL));
Edouard@695
   869
            free(r_fpr);
Edouard@695
   870
            if (status != PEP_STATUS_OK) {
roker@1853
   871
                return ADD_TO_LOG(status);
Edouard@695
   872
            }
Edouard@588
   873
        }
vb@0
   874
    }
Edouard@560
   875
    else
Edouard@560
   876
    {
vb@214
   877
        bool expired;
Edouard@701
   878
        status = key_expired(session, identity->fpr, 
Edouard@701
   879
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   880
                             &expired);
Edouard@701
   881
vb@214
   882
        assert(status == PEP_STATUS_OK);
Edouard@499
   883
        if (status != PEP_STATUS_OK) {
roker@1853
   884
            return ADD_TO_LOG(status);
Edouard@499
   885
        }
vb@214
   886
vb@214
   887
        if (status == PEP_STATUS_OK && expired) {
vb@214
   888
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   889
            renew_key(session, identity->fpr, ts);
vb@214
   890
            free_timestamp(ts);
vb@214
   891
        }
vb@214
   892
    }
vb@0
   893
krista@1353
   894
    if (!identity->username)
krista@1353
   895
        identity->username = strdup("");
krista@1353
   896
    
vb@0
   897
    status = set_identity(session, identity);
vb@0
   898
    assert(status == PEP_STATUS_OK);
Edouard@499
   899
    if (status != PEP_STATUS_OK) {
Edouard@588
   900
        return status;
Edouard@499
   901
    }
vb@0
   902
roker@1853
   903
    return ADD_TO_LOG(PEP_STATUS_OK);
vb@0
   904
}
vb@0
   905
krista@2308
   906
DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
krista@2308
   907
                                                 identity_list* my_idents) {
krista@2308
   908
    PEP_STATUS status = PEP_STATUS_OK;
krista@2308
   909
    if (!session)
krista@2308
   910
        return PEP_ILLEGAL_VALUE;
krista@2308
   911
        
krista@2308
   912
    char* stored_own_userid = NULL;
krista@2308
   913
    get_own_userid(session, &stored_own_userid);
krista@2308
   914
    
krista@2308
   915
    identity_list* ident_curr = my_idents;
krista@2308
   916
    while (ident_curr) {
krista@2308
   917
        pEp_identity* ident = ident_curr->ident;
krista@2308
   918
        if (!ident)
krista@2308
   919
            return PEP_ILLEGAL_VALUE;
krista@2308
   920
            
krista@2308
   921
        if (stored_own_userid) {
krista@2308
   922
            if (!ident->user_id) 
krista@2308
   923
                ident->user_id = strdup(stored_own_userid);
krista@2308
   924
            else if (strcmp(stored_own_userid, ident->user_id) != 0)
krista@2308
   925
                return PEP_ILLEGAL_VALUE;
krista@2308
   926
        }
krista@2308
   927
        else if (!ident->user_id) {
krista@2308
   928
            stored_own_userid = PEP_OWN_USERID;
krista@2308
   929
            ident->user_id = strdup(PEP_OWN_USERID);
krista@2308
   930
        }
krista@2308
   931
        
krista@2308
   932
        ident->me = true; // Just in case.
krista@2308
   933
        
krista@2308
   934
        // Ok, do it...
krista@2308
   935
        status = set_identity(session, ident);
krista@2308
   936
        if (status != PEP_STATUS_OK)
krista@2308
   937
            return status;
krista@2308
   938
        
krista@2308
   939
        ident_curr = ident_curr->next;
krista@2308
   940
    }
krista@2308
   941
    
krista@2308
   942
    return status;
krista@2308
   943
}
krista@2308
   944
edouard@1385
   945
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
edouard@1385
   946
{
roker@1853
   947
    return ADD_TO_LOG(_myself(session, identity, true, false));
edouard@1385
   948
}
edouard@1385
   949
vb@296
   950
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   951
        PEP_SESSION session, 
vb@292
   952
        examine_identity_t examine_identity,
vb@292
   953
        void *management
vb@292
   954
    )
vb@292
   955
{
vb@292
   956
    assert(session);
vb@292
   957
    if (!session)
vb@292
   958
        return PEP_ILLEGAL_VALUE;
vb@292
   959
vb@292
   960
    session->examine_management = management;
vb@292
   961
    session->examine_identity = examine_identity;
vb@292
   962
vb@292
   963
    return PEP_STATUS_OK;
vb@292
   964
}
vb@292
   965
vb@0
   966
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   967
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   968
        void *management
vb@0
   969
    )
vb@0
   970
{
vb@0
   971
    PEP_SESSION session;
vb@0
   972
    pEp_identity *identity;
Edouard@499
   973
    PEP_STATUS status;
vb@0
   974
Edouard@499
   975
    assert(retrieve_next_identity);
Edouard@499
   976
    assert(management);
Edouard@499
   977
Edouard@499
   978
    if (!retrieve_next_identity || !management)
Edouard@499
   979
        return PEP_ILLEGAL_VALUE;
Edouard@499
   980
Edouard@499
   981
    status = init(&session);
vb@0
   982
    assert(status == PEP_STATUS_OK);
vb@0
   983
    if (status != PEP_STATUS_OK)
vb@0
   984
        return status;
vb@0
   985
vb@0
   986
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   987
Edouard@499
   988
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   989
    {
vb@0
   990
        assert(identity->address);
Edouard@499
   991
        if(identity->address)
Edouard@499
   992
        {
Edouard@499
   993
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   994
krista@2327
   995
            if (identity->me) {
Edouard@499
   996
                status = myself(session, identity);
Edouard@499
   997
            } else {
Edouard@499
   998
                status = recv_key(session, identity->address);
Edouard@499
   999
            }
Edouard@499
  1000
vb@0
  1001
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
  1002
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
  1003
                return PEP_OUT_OF_MEMORY;
vb@0
  1004
        }
vb@0
  1005
        free_identity(identity);
vb@0
  1006
    }
vb@0
  1007
vb@0
  1008
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
  1009
vb@0
  1010
    release(session);
vb@0
  1011
    return PEP_STATUS_OK;
vb@0
  1012
}
vb@0
  1013
krista@1213
  1014
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
  1015
        PEP_SESSION session,
vb@357
  1016
        pEp_identity *ident
vb@357
  1017
    )
vb@215
  1018
{
vb@218
  1019
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
  1020
vb@215
  1021
    assert(session);
vb@357
  1022
    assert(ident);
Edouard@439
  1023
    assert(!EMPTYSTR(ident->fpr));
vb@215
  1024
vb@357
  1025
    if (!(session && ident && ident->fpr))
vb@215
  1026
        return PEP_ILLEGAL_VALUE;
vb@215
  1027
krista@2327
  1028
    if (ident->me)
Edouard@697
  1029
    {
vb@357
  1030
        revoke_key(session, ident->fpr, NULL);
Edouard@697
  1031
        myself(session, ident);
Edouard@697
  1032
    }
Edouard@697
  1033
    else
Edouard@697
  1034
    {
krista@2129
  1035
        // for undo
krista@2129
  1036
        if (session->cached_mistrusted)
krista@2129
  1037
            free(session->cached_mistrusted);
krista@2129
  1038
        session->cached_mistrusted = identity_dup(ident);
Edouard@697
  1039
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
  1040
    }
vb@218
  1041
vb@218
  1042
    return status;
vb@215
  1043
}
vb@215
  1044
krista@2129
  1045
DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session) {
krista@2129
  1046
    assert(session);
krista@2129
  1047
    
krista@2129
  1048
    if (!session)
krista@2129
  1049
        return PEP_ILLEGAL_VALUE;
krista@2129
  1050
    
krista@2129
  1051
    PEP_STATUS status = PEP_STATUS_OK;
krista@2129
  1052
        
krista@2129
  1053
    pEp_identity* cached_ident = session->cached_mistrusted;
krista@2129
  1054
    
krista@2129
  1055
    if (!cached_ident)
krista@2129
  1056
        status = PEP_CANNOT_FIND_IDENTITY;
krista@2129
  1057
    else {
krista@2129
  1058
        status = set_identity(session, cached_ident);            
krista@2129
  1059
        free_identity(session->cached_mistrusted);
krista@2129
  1060
    }
krista@2129
  1061
    
krista@2129
  1062
    session->cached_mistrusted = NULL;
krista@2129
  1063
krista@2129
  1064
    return status;
krista@2129
  1065
}
krista@2129
  1066
Edouard@410
  1067
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
  1068
        PEP_SESSION session,
Edouard@410
  1069
        pEp_identity *ident
Edouard@410
  1070
    )
Edouard@410
  1071
{
Edouard@410
  1072
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
  1073
Edouard@410
  1074
    assert(session);
Edouard@410
  1075
    assert(ident);
krista@2327
  1076
    assert(!ident->me);
Edouard@439
  1077
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
  1078
    assert(!EMPTYSTR(ident->address));
Edouard@439
  1079
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
  1080
krista@2327
  1081
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
  1082
            ident->user_id))
Edouard@410
  1083
        return PEP_ILLEGAL_VALUE;
Edouard@410
  1084
vb@420
  1085
    status = update_identity(session, ident);
vb@420
  1086
    if (status != PEP_STATUS_OK)
vb@420
  1087
        return status;
Edouard@410
  1088
Edouard@442
  1089
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
  1090
        ident->comm_type = PEP_ct_unknown;
vb@421
  1091
    else
vb@421
  1092
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
  1093
vb@420
  1094
    status = set_identity(session, ident);
vb@421
  1095
    if (status != PEP_STATUS_OK)
vb@421
  1096
        return status;
vb@421
  1097
vb@422
  1098
    if (ident->comm_type == PEP_ct_unknown)
vb@422
  1099
        status = update_identity(session, ident);
Edouard@410
  1100
    return status;
Edouard@410
  1101
}
Edouard@410
  1102
vb@354
  1103
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
  1104
        PEP_SESSION session,
vb@354
  1105
        pEp_identity *ident
vb@354
  1106
    )
vb@354
  1107
{
vb@354
  1108
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
  1109
vb@354
  1110
    assert(session);
vb@354
  1111
    assert(ident);
Edouard@439
  1112
    assert(!EMPTYSTR(ident->address));
Edouard@439
  1113
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
  1114
    assert(!EMPTYSTR(ident->fpr));
vb@354
  1115
Edouard@439
  1116
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
edouard@1945
  1117
            EMPTYSTR(ident->fpr))
vb@354
  1118
        return PEP_ILLEGAL_VALUE;
vb@354
  1119
vb@354
  1120
    status = update_identity(session, ident);
vb@354
  1121
    if (status != PEP_STATUS_OK)
vb@354
  1122
        return status;
vb@354
  1123
vb@356
  1124
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
  1125
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
  1126
        status = set_identity(session, ident);
vb@356
  1127
    }
vb@356
  1128
    else {
vb@356
  1129
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
  1130
        status = PEP_CANNOT_SET_TRUST;
vb@356
  1131
    }
vb@354
  1132
vb@354
  1133
    return status;
vb@354
  1134
}
vb@354
  1135
Edouard@584
  1136
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
  1137
        PEP_SESSION session,
vb@955
  1138
        const char *fpr,
vb@955
  1139
        bool *listed
vb@955
  1140
    )
Edouard@584
  1141
{
Edouard@584
  1142
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
  1143
    int count;
Edouard@584
  1144
    
Edouard@584
  1145
    assert(session && fpr && fpr[0] && listed);
Edouard@584
  1146
    
Edouard@584
  1147
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
  1148
        return PEP_ILLEGAL_VALUE;
Edouard@584
  1149
    
Edouard@584
  1150
    *listed = false;
Edouard@584
  1151
    
Edouard@584
  1152
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
  1153
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
  1154
    
Edouard@584
  1155
    int result;
Edouard@584
  1156
    
Edouard@584
  1157
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
  1158
    switch (result) {
Edouard@584
  1159
        case SQLITE_ROW:
Edouard@584
  1160
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
  1161
            *listed = count > 0;
krista@2288
  1162
            status = PEP_STATUS_OK;
Edouard@584
  1163
            break;
Edouard@584
  1164
            
Edouard@584
  1165
        default:
Edouard@584
  1166
            status = PEP_UNKNOWN_ERROR;
Edouard@584
  1167
    }
Edouard@584
  1168
    
Edouard@584
  1169
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
  1170
    return status;
Edouard@584
  1171
}
Edouard@584
  1172
edouard@1412
  1173
PEP_STATUS _own_identities_retrieve(
vb@955
  1174
        PEP_SESSION session,
edouard@1412
  1175
        identity_list **own_identities,
edouard@1412
  1176
        identity_flags_t excluded_flags
vb@955
  1177
      )
Edouard@584
  1178
{
Edouard@584
  1179
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
  1180
    
vb@955
  1181
    assert(session && own_identities);
vb@955
  1182
    if (!(session && own_identities))
Edouard@584
  1183
        return PEP_ILLEGAL_VALUE;
Edouard@584
  1184
    
vb@955
  1185
    *own_identities = NULL;
vb@955
  1186
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
  1187
    if (_own_identities == NULL)
Edouard@584
  1188
        goto enomem;
Edouard@584
  1189
    
vb@955
  1190
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
  1191
    
Edouard@584
  1192
    int result;
vb@955
  1193
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
  1194
    const char *address = NULL;
Edouard@584
  1195
    const char *fpr = NULL;
vb@955
  1196
    const char *username = NULL;
vb@955
  1197
    const char *user_id = NULL;
vb@955
  1198
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
  1199
    const char *lang = NULL;
vb@955
  1200
    unsigned int flags = 0;
Edouard@584
  1201
    
vb@955
  1202
    identity_list *_bl = _own_identities;
Edouard@584
  1203
    do {
edouard@1412
  1204
        sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
vb@955
  1205
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
  1206
        switch (result) {
Edouard@584
  1207
            case SQLITE_ROW:
vb@955
  1208
                address = (const char *)
vb@955
  1209
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
  1210
                fpr = (const char *)
vb@955
  1211
                    sqlite3_column_text(session->own_identities_retrieve, 1);
krista@2301
  1212
                user_id = (const char *)
krista@2301
  1213
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
  1214
                username = (const char *)
krista@2301
  1215
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
  1216
                comm_type = PEP_ct_pEp;
vb@1071
  1217
                lang = (const char *)
krista@2301
  1218
                    sqlite3_column_text(session->own_identities_retrieve, 4);
vb@1071
  1219
                flags = (unsigned int)
krista@2301
  1220
                    sqlite3_column_int(session->own_identities_retrieve, 5);
vb@955
  1221
vb@1078
  1222
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
  1223
                if (!ident)
Edouard@584
  1224
                    goto enomem;
vb@955
  1225
                ident->comm_type = comm_type;
vb@955
  1226
                if (lang && lang[0]) {
vb@955
  1227
                    ident->lang[0] = lang[0];
vb@955
  1228
                    ident->lang[1] = lang[1];
vb@1078
  1229
                    ident->lang[2] = 0;
vb@955
  1230
                }
krista@2288
  1231
                ident->me = true;
vb@955
  1232
                ident->flags = flags;
vb@955
  1233
vb@955
  1234
                _bl = identity_list_add(_bl, ident);
vb@1080
  1235
                if (_bl == NULL) {
vb@1080
  1236
                    free_identity(ident);
vb@983
  1237
                    goto enomem;
vb@1080
  1238
                }
Edouard@584
  1239
                
Edouard@584
  1240
                break;
Edouard@584
  1241
                
Edouard@584
  1242
            case SQLITE_DONE:
Edouard@584
  1243
                break;
Edouard@584
  1244
                
Edouard@584
  1245
            default:
Edouard@584
  1246
                status = PEP_UNKNOWN_ERROR;
Edouard@584
  1247
                result = SQLITE_DONE;
Edouard@584
  1248
        }
Edouard@584
  1249
    } while (result != SQLITE_DONE);
Edouard@584
  1250
    
vb@955
  1251
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
  1252
    if (status == PEP_STATUS_OK)
vb@955
  1253
        *own_identities = _own_identities;
Edouard@584
  1254
    else
vb@955
  1255
        free_identity_list(_own_identities);
Edouard@584
  1256
    
Edouard@584
  1257
    goto the_end;
Edouard@584
  1258
    
Edouard@584
  1259
enomem:
vb@955
  1260
    free_identity_list(_own_identities);
Edouard@584
  1261
    status = PEP_OUT_OF_MEMORY;
Edouard@584
  1262
    
Edouard@584
  1263
the_end:
Edouard@584
  1264
    return status;
Edouard@584
  1265
}
vb@955
  1266
edouard@1412
  1267
DYNAMIC_API PEP_STATUS own_identities_retrieve(
edouard@1364
  1268
        PEP_SESSION session,
edouard@1412
  1269
        identity_list **own_identities
edouard@1412
  1270
      )
edouard@1412
  1271
{
edouard@1412
  1272
    return _own_identities_retrieve(session, own_identities, 0);
edouard@1412
  1273
}
edouard@1412
  1274
edouard@1412
  1275
PEP_STATUS _own_keys_retrieve(
edouard@1412
  1276
        PEP_SESSION session,
edouard@1412
  1277
        stringlist_t **keylist,
edouard@1412
  1278
        identity_flags_t excluded_flags
edouard@1364
  1279
      )
edouard@1364
  1280
{
edouard@1364
  1281
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1364
  1282
    
edouard@1364
  1283
    assert(session && keylist);
edouard@1364
  1284
    if (!(session && keylist))
edouard@1364
  1285
        return PEP_ILLEGAL_VALUE;
edouard@1364
  1286
    
edouard@1364
  1287
    *keylist = NULL;
edouard@1364
  1288
    stringlist_t *_keylist = NULL;
edouard@1364
  1289
    
edouard@1394
  1290
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1291
    
edouard@1364
  1292
    int result;
edouard@1364
  1293
    char *fpr = NULL;
edouard@1364
  1294
    
edouard@1364
  1295
    stringlist_t *_bl = _keylist;
edouard@1364
  1296
    do {
edouard@1412
  1297
        sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
edouard@1394
  1298
        result = sqlite3_step(session->own_keys_retrieve);
edouard@1364
  1299
        switch (result) {
edouard@1364
  1300
            case SQLITE_ROW:
edouard@1394
  1301
                fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
edouard@1364
  1302
                if(fpr == NULL)
edouard@1364
  1303
                    goto enomem;
edouard@1364
  1304
edouard@1364
  1305
                _bl = stringlist_add(_bl, fpr);
edouard@1364
  1306
                if (_bl == NULL) {
edouard@1364
  1307
                    free(fpr);
edouard@1364
  1308
                    goto enomem;
edouard@1364
  1309
                }
edouard@1364
  1310
                if (_keylist == NULL)
edouard@1364
  1311
                    _keylist = _bl;
edouard@1364
  1312
                
edouard@1364
  1313
                break;
edouard@1364
  1314
                
edouard@1364
  1315
            case SQLITE_DONE:
edouard@1364
  1316
                break;
edouard@1364
  1317
                
edouard@1364
  1318
            default:
edouard@1364
  1319
                status = PEP_UNKNOWN_ERROR;
edouard@1364
  1320
                result = SQLITE_DONE;
edouard@1364
  1321
        }
edouard@1364
  1322
    } while (result != SQLITE_DONE);
edouard@1364
  1323
    
edouard@1394
  1324
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1325
    if (status == PEP_STATUS_OK)
edouard@1364
  1326
        *keylist = _keylist;
edouard@1364
  1327
    else
edouard@1364
  1328
        free_stringlist(_keylist);
edouard@1364
  1329
    
edouard@1364
  1330
    goto the_end;
edouard@1364
  1331
    
edouard@1364
  1332
enomem:
edouard@1364
  1333
    free_stringlist(_keylist);
edouard@1364
  1334
    status = PEP_OUT_OF_MEMORY;
edouard@1364
  1335
    
edouard@1364
  1336
the_end:
edouard@1364
  1337
    return status;
edouard@1364
  1338
}
edouard@1364
  1339
edouard@1412
  1340
DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
edouard@1412
  1341
{
edouard@1412
  1342
    return _own_keys_retrieve(session, keylist, 0);
edouard@1412
  1343
}
edouard@1412
  1344
edouard@1760
  1345
// FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
edouard@1394
  1346
DYNAMIC_API PEP_STATUS set_own_key(
edouard@1394
  1347
       PEP_SESSION session,
edouard@1394
  1348
       const char *address,
edouard@1394
  1349
       const char *fpr
edouard@1394
  1350
    )
edouard@1394
  1351
{
edouard@1394
  1352
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1394
  1353
    
edouard@1394
  1354
    assert(session &&
edouard@1760
  1355
           address &&
edouard@1394
  1356
           fpr && fpr[0]
edouard@1394
  1357
          );
edouard@1394
  1358
    
edouard@1394
  1359
    if (!(session &&
edouard@1760
  1360
          address &&
edouard@1394
  1361
          fpr && fpr[0]
edouard@1394
  1362
         ))
edouard@1394
  1363
        return PEP_ILLEGAL_VALUE;
krista@2301
  1364
            
krista@2301
  1365
            
krista@2301
  1366
    // First see if we have it in own identities already, AND we retrieve
krista@2301
  1367
    // our own user_id
krista@2301
  1368
    pEp_identity* my_id = NULL;
krista@2301
  1369
    identity_list* my_identities = NULL;
krista@2301
  1370
    char* my_user_id = NULL;
krista@2301
  1371
    status = own_identities_retrieve(session, &my_identities);
edouard@1394
  1372
    
krista@2301
  1373
    if (status == PEP_STATUS_OK) {
krista@2301
  1374
        if (my_identities) {
krista@2301
  1375
            if (!(my_identities->ident && my_identities->ident->user_id))
krista@2301
  1376
                return PEP_ILLEGAL_VALUE;
edouard@1394
  1377
krista@2301
  1378
            my_user_id = strdup(my_identities->ident->user_id);
krista@2301
  1379
krista@2301
  1380
            if (!my_user_id) 
krista@2301
  1381
                return PEP_OUT_OF_MEMORY;
krista@2301
  1382
            
krista@2301
  1383
            // Probably cheaper than all the strcmps if there are many,
krista@2301
  1384
            // plus this avoids the capitalisation and . problems:
krista@2301
  1385
            
krista@2301
  1386
            status = get_identity(session, my_user_id, address, &my_id);
krista@2301
  1387
            
krista@2301
  1388
            if (status == PEP_STATUS_OK && my_id) {
krista@2304
  1389
                if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
krista@2301
  1390
                    // We're done. It was already here.
krista@2301
  1391
                    // FIXME: Do we check trust/revocation/?
krista@2301
  1392
                    goto pep_free;
krista@2301
  1393
                }            
krista@2301
  1394
            }
krista@2301
  1395
            
krista@2301
  1396
            // Otherwise, we see if there's a binding for this user_id/key
krista@2301
  1397
            // in the trust DB
krista@2301
  1398
            
krista@2301
  1399
            // If there's an id w/ user_id + address
krista@2301
  1400
            if (my_id) {
krista@2301
  1401
                free(my_id->fpr);
krista@2301
  1402
                my_id->fpr = my_user_id;
krista@2304
  1403
                my_id->comm_type = PEP_ct_pEp;
krista@2304
  1404
                my_id->me = true; // just in case? 
krista@2301
  1405
            }
krista@2301
  1406
            else { // Else, we need a new identity
krista@2304
  1407
                my_id = new_identity(address, fpr, my_user_id, NULL); 
krista@2301
  1408
                if (status != PEP_STATUS_OK)
krista@2301
  1409
                    goto pep_free; 
krista@2304
  1410
                my_id->me = true;
krista@2304
  1411
                my_id->comm_type = PEP_ct_pEp;
krista@2301
  1412
            }
krista@2301
  1413
        }
krista@2301
  1414
        else {
krista@2301
  1415
            // I think the prerequisite should be that at least one own identity
krista@2301
  1416
            // already in the DB, so REALLY look at this.
krista@2301
  1417
            return PEP_CANNOT_FIND_IDENTITY;
krista@2301
  1418
        }
krista@2301
  1419
        
krista@2301
  1420
        status = set_identity(session, my_id);
krista@2301
  1421
    }  
edouard@1394
  1422
    
krista@2301
  1423
pep_free:
krista@2301
  1424
    free(my_id);
krista@2301
  1425
    free(my_user_id);
edouard@1394
  1426
    return status;
edouard@1394
  1427
}
krista@1357
  1428
krista@1357
  1429
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
  1430
                             bool *has_private) {
krista@1357
  1431
krista@1357
  1432
    assert(session);
krista@1357
  1433
    assert(fpr);
krista@1357
  1434
    assert(has_private);
krista@1357
  1435
    
krista@1357
  1436
    if (!(session && fpr && has_private))
krista@1357
  1437
        return PEP_ILLEGAL_VALUE;
krista@1357
  1438
krista@1357
  1439
    return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
edouard@1385
  1440
}