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