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