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