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