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