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