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