src/keymanagement.c
author Krista Bennett <krista@pep-project.org>
Mon, 18 Dec 2017 23:10:29 +0100
branchENGINE-289
changeset 2327 cc04433060ff
parent 2326 6d2a523aad35
child 2368 9e1609eeade3
permissions -rw-r--r--
ENGINE-289: So it compiles, but there are sql errors before we get very far. Too tired to debug tonight.
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@2327
   265
    new_ident->me = new_ident->me || stored_ident->me;
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@2327
   427
                
krista@2324
   428
        if (status == PEP_STATUS_OK && stored_ident) { 
krista@2324
   429
            //  * if identity available
krista@2324
   430
            //      * patch it with username
krista@2324
   431
            //          (note: this will happen when 
krista@2324
   432
            //           setting automatically below...)
krista@2324
   433
            //      * elect valid key for identity
krista@2324
   434
            //    * if valid key exists
krista@2324
   435
            //        * set return value's fpr
krista@2325
   436
            status = prepare_updated_identity(session,
krista@2325
   437
                                              identity,
krista@2325
   438
                                              stored_ident, true);
krista@2311
   439
        }
krista@2324
   440
        //  * else (identity unavailable)
krista@2311
   441
        else {
krista@2324
   442
            //  if we only have user_id and address and identity not available
krista@2311
   443
            //      * return error status (identity not found)
krista@2324
   444
            if (!identity->username)
krista@2326
   445
                status = PEP_CANNOT_FIND_IDENTITY;
krista@2311
   446
            
krista@2324
   447
            // Otherwise, if we had user_id, address, and username:
krista@2324
   448
            //    * create identity with user_id, address, username
krista@2324
   449
            //      (this is the input id without the fpr + comm type!)
krista@2324
   450
            free(identity->fpr);
krista@2324
   451
            identity->fpr = NULL;
krista@2324
   452
            identity->comm_type = PEP_ct_unknown;
krista@2324
   453
            
krista@2324
   454
            //    * We've already checked and retrieved
krista@2324
   455
            //      any applicable temporary identities above. If we're 
krista@2324
   456
            //      here, none of them fit.
krista@2324
   457
            //    * call set_identity() to store
krista@2324
   458
            if (status == PEP_STATUS_OK) {
krista@2324
   459
                status = set_identity(session, identity);
krista@2324
   460
            }
krista@2324
   461
            //  * Return: created identity
krista@2324
   462
        }        
krista@2311
   463
    }
krista@2311
   464
    else if (identity->username) {
krista@2311
   465
        /*
krista@2311
   466
         * Temporary identity information with username supplied
krista@2311
   467
            * Input: address, username (no others)
krista@2311
   468
         */
krista@2324
   469
        identity_list* id_list = NULL;
krista@2324
   470
        status = get_identities_by_address(session, identity->address, &id_list);
krista@2324
   471
krista@2311
   472
        //  * Search for an identity with non-temporary user_id with that mapping
krista@2324
   473
        if (id_list) {
krista@2324
   474
            identity_list* id_curr = id_list;
krista@2324
   475
            while (id_curr) {
krista@2324
   476
                pEp_identity* this_id = id_curr->ident;
krista@2324
   477
                if (this_id) {
krista@2324
   478
                    char* this_uid = this_id->user_id;
krista@2325
   479
                    if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
krista@2324
   480
                        // FIXME: should we also be fixing pEp_own_userId in this
krista@2324
   481
                        // function here?
krista@2324
   482
                        
krista@2324
   483
                        // if usernames match, we replace the userid.
krista@2324
   484
                        if (identity->username && 
krista@2324
   485
                            strcasecmp(identity->username, 
krista@2324
   486
                                       this_id->username) == 0) {
krista@2324
   487
                            
krista@2324
   488
                            // Ok, we have a real ID. Copy it!
krista@2324
   489
                            identity->user_id = strdup(this_uid);
krista@2324
   490
                            
krista@2324
   491
                            if (!identity->user_id)
krista@2324
   492
                                status = PEP_OUT_OF_MEMORY;
krista@2324
   493
                            stored_ident = this_id;
krista@2324
   494
                            
krista@2324
   495
                            break;                                
krista@2324
   496
                        }                            
krista@2324
   497
                    } 
krista@2324
   498
                }
krista@2324
   499
                id_curr = id_curr->next;
krista@2324
   500
            }
krista@2324
   501
        }
krista@2324
   502
krista@2324
   503
        if (stored_ident) {
krista@2325
   504
            status = prepare_updated_identity(session,
krista@2325
   505
                                              identity,
krista@2325
   506
                                              stored_ident, true);
krista@2324
   507
        }
krista@2324
   508
        else {
krista@2325
   509
            // create temporary identity, store it, and Return this
krista@2325
   510
            // This means TOFU_ user_id
krista@2325
   511
            identity->user_id = calloc(1, strlen(identity->address) + 6);
krista@2325
   512
            if (!identity->user_id)
krista@2325
   513
                return PEP_OUT_OF_MEMORY;
krista@2325
   514
krista@2325
   515
            snprintf(identity->user_id, strlen(identity->address) + 6,
krista@2325
   516
                     "TOFU_%s", identity->address);        
krista@2325
   517
            
krista@2325
   518
            free(identity->fpr);
krista@2325
   519
            identity->fpr = NULL;
krista@2325
   520
            identity->comm_type = PEP_ct_unknown;
krista@2325
   521
             
krista@2325
   522
            //    * We've already checked and retrieved
krista@2325
   523
            //      any applicable temporary identities above. If we're 
krista@2325
   524
            //      here, none of them fit.
krista@2325
   525
            //    * call set_identity() to store
krista@2325
   526
            status = set_identity(session, identity);
krista@2324
   527
        }
krista@2311
   528
    }
krista@2311
   529
    else {
krista@2311
   530
        /*
krista@2311
   531
         * Temporary identity information without username suplied
krista@2311
   532
            * Input: address (no others)
krista@2311
   533
         */
krista@2325
   534
        identity_list* id_list = NULL;
krista@2325
   535
        status = get_identities_by_address(session, identity->address, &id_list);
krista@2325
   536
krista@2311
   537
        //    * Search for identity with this address
krista@2325
   538
        if (id_list && !(id_list->next)) { // exactly one            
krista@2325
   539
            //    * If exactly one found
krista@2325
   540
            //      * elect valid key for identity (see below)
krista@2325
   541
            //      * Return this identity
krista@2326
   542
            stored_ident = id_list->ident;
krista@2325
   543
            
krista@2326
   544
            if (stored_ident)
krista@2325
   545
                status = prepare_updated_identity(session, identity,
krista@2325
   546
                                                  stored_ident, false);
krista@2325
   547
            else
krista@2325
   548
                status = PEP_CANNOT_FIND_IDENTITY;
krista@2325
   549
        }
krista@2325
   550
        else // too little info
krista@2325
   551
            status = PEP_CANNOT_FIND_IDENTITY; 
krista@2311
   552
    }
krista@2326
   553
    
krista@2326
   554
    // FIXME: This is legacy. I presume it's a notification for the caller...
krista@2326
   555
    // Revisit once I can talk to Volker
krista@2326
   556
    if (identity->comm_type != PEP_ct_compromized &&
krista@2326
   557
        identity->comm_type < PEP_ct_strong_but_unconfirmed)
krista@2326
   558
    if (session->examine_identity)
krista@2326
   559
        session->examine_identity(identity, session->examine_management);
krista@2326
   560
roker@1853
   561
    return ADD_TO_LOG(status);
vb@0
   562
}
vb@0
   563
Edouard@774
   564
PEP_STATUS elect_ownkey(
krista@1194
   565
        PEP_SESSION session, pEp_identity * identity
Edouard@774
   566
    )
Edouard@774
   567
{
Edouard@774
   568
    PEP_STATUS status;
Edouard@774
   569
    stringlist_t *keylist = NULL;
Edouard@774
   570
Edouard@774
   571
    free(identity->fpr);
Edouard@774
   572
    identity->fpr = NULL;
Edouard@774
   573
krista@1357
   574
    status = find_private_keys(session, identity->address, &keylist);
Edouard@774
   575
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   576
    if (status == PEP_OUT_OF_MEMORY)
Edouard@774
   577
        return PEP_OUT_OF_MEMORY;
Edouard@774
   578
    
Edouard@774
   579
    if (keylist != NULL && keylist->value != NULL)
Edouard@774
   580
    {
Edouard@774
   581
        char *_fpr = NULL;
Edouard@774
   582
        identity->comm_type = PEP_ct_unknown;
Edouard@774
   583
Edouard@774
   584
        stringlist_t *_keylist;
Edouard@774
   585
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@774
   586
            bool is_own = false;
Edouard@774
   587
            
edouard@1752
   588
            status = own_key_is_listed(session, _keylist->value, &is_own);
edouard@1752
   589
            assert(status == PEP_STATUS_OK);
edouard@1752
   590
            if (status != PEP_STATUS_OK) {
edouard@1752
   591
                free_stringlist(keylist);
edouard@1752
   592
                return status;
Edouard@774
   593
            }
Edouard@774
   594
            
edouard@1752
   595
            if (is_own)
Edouard@774
   596
            {
Edouard@774
   597
                PEP_comm_type _comm_type_key;
Edouard@774
   598
                
Edouard@774
   599
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@774
   600
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   601
                if (status == PEP_OUT_OF_MEMORY) {
Edouard@774
   602
                    free_stringlist(keylist);
Edouard@774
   603
                    return PEP_OUT_OF_MEMORY;
Edouard@774
   604
                }
Edouard@774
   605
                
Edouard@774
   606
                if (_comm_type_key != PEP_ct_compromized &&
Edouard@774
   607
                    _comm_type_key != PEP_ct_unknown)
Edouard@774
   608
                {
Edouard@774
   609
                    if (identity->comm_type == PEP_ct_unknown ||
Edouard@774
   610
                        _comm_type_key > identity->comm_type)
Edouard@774
   611
                    {
Edouard@774
   612
                        identity->comm_type = _comm_type_key;
Edouard@774
   613
                        _fpr = _keylist->value;
Edouard@774
   614
                    }
Edouard@774
   615
                }
Edouard@774
   616
            }
Edouard@774
   617
        }
Edouard@774
   618
        
Edouard@774
   619
        if (_fpr)
Edouard@774
   620
        {
Edouard@774
   621
            identity->fpr = strdup(_fpr);
Edouard@774
   622
            assert(identity->fpr);
Edouard@774
   623
            if (identity->fpr == NULL)
Edouard@774
   624
            {
Edouard@774
   625
                free_stringlist(keylist);
Edouard@774
   626
                return PEP_OUT_OF_MEMORY;
Edouard@774
   627
            }
Edouard@774
   628
        }
Edouard@774
   629
        free_stringlist(keylist);
Edouard@774
   630
    }
Edouard@774
   631
    return PEP_STATUS_OK;
Edouard@774
   632
}
Edouard@774
   633
krista@1357
   634
PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
krista@1357
   635
                                bool* is_usable) {
krista@1357
   636
    
krista@1357
   637
    bool dont_use_fpr = true;
krista@1357
   638
    
krista@1357
   639
    PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
krista@1371
   640
    if (status == PEP_STATUS_OK && !dont_use_fpr) {
krista@1357
   641
        // Make sure there is a *private* key associated with this fpr
krista@1357
   642
        bool has_private = false;
krista@1357
   643
        status = contains_priv_key(session, fpr, &has_private);
krista@1371
   644
krista@1371
   645
        if (status == PEP_STATUS_OK)
krista@1371
   646
            dont_use_fpr = !has_private;
krista@1357
   647
    }
krista@1357
   648
    
krista@1357
   649
    *is_usable = !dont_use_fpr;
krista@1357
   650
    
roker@1853
   651
    return ADD_TO_LOG(status);
krista@1357
   652
}
krista@1357
   653
edouard@1406
   654
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
vb@0
   655
{
roker@1559
   656
    pEp_identity *stored_identity = NULL;
vb@0
   657
    PEP_STATUS status;
vb@0
   658
vb@0
   659
    assert(session);
vb@0
   660
    assert(identity);
vb@1044
   661
    assert(!EMPTYSTR(identity->address));
vb@1043
   662
krista@2303
   663
    char* own_id = NULL;
krista@2303
   664
    status = get_own_userid(session, &own_id);
krista@2303
   665
krista@2308
   666
Edouard@658
   667
    assert(EMPTYSTR(identity->user_id) ||
krista@2305
   668
           (own_id && strcmp(identity->user_id, own_id) == 0) ||
krista@2305
   669
           !own_id);
vb@0
   670
vb@1044
   671
    if (!(session && identity && !EMPTYSTR(identity->address) &&
vb@1043
   672
            (EMPTYSTR(identity->user_id) ||
krista@2305
   673
            (own_id && strcmp(identity->user_id, own_id) == 0) ||
krista@2305
   674
             !own_id)))
roker@1853
   675
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
   676
krista@2308
   677
    if (!own_id) {
krista@2308
   678
        // check to see if we have ANY identity for this address... could have
krista@2308
   679
        // happened due to race condition
krista@2308
   680
    }
krista@2308
   681
    
vb@0
   682
    identity->comm_type = PEP_ct_pEp;
krista@2288
   683
    identity->me = true;
edouard@1406
   684
    if(ignore_flags)
edouard@1406
   685
        identity->flags = 0;
Edouard@658
   686
    
vb@1043
   687
    if (EMPTYSTR(identity->user_id))
Edouard@658
   688
    {
Edouard@658
   689
        free(identity->user_id);
krista@2303
   690
        identity->user_id = (own_id ? own_id : strdup(PEP_OWN_USERID));
Edouard@658
   691
        assert(identity->user_id);
Edouard@658
   692
        if (identity->user_id == NULL)
Edouard@658
   693
            return PEP_OUT_OF_MEMORY;
Edouard@658
   694
    }
vb@0
   695
edouard@1488
   696
    if (EMPTYSTR(identity->username))
edouard@1488
   697
    {
edouard@1488
   698
        free(identity->username);
krista@2219
   699
        identity->username = strdup("Anonymous");
edouard@1488
   700
        assert(identity->username);
edouard@1488
   701
        if (identity->username == NULL)
edouard@1488
   702
            return PEP_OUT_OF_MEMORY;
edouard@1488
   703
    }
edouard@1488
   704
vb@215
   705
    DEBUG_LOG("myself", "debug", identity->address);
vb@1044
   706
 
Edouard@560
   707
    status = get_identity(session,
Edouard@560
   708
                          identity->address,
Edouard@560
   709
                          identity->user_id,
Edouard@560
   710
                          &stored_identity);
Edouard@560
   711
    
vb@0
   712
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   713
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   714
        return PEP_OUT_OF_MEMORY;
krista@1357
   715
krista@1357
   716
    bool dont_use_stored_fpr = true;
krista@1357
   717
    bool dont_use_input_fpr = true;
krista@1359
   718
        
Edouard@560
   719
    if (stored_identity)
Edouard@560
   720
    {
Edouard@560
   721
        if (EMPTYSTR(identity->fpr)) {
krista@1352
   722
            
krista@1357
   723
            bool has_private = false;
krista@1352
   724
            
krista@1357
   725
            status = _has_usable_priv_key(session, stored_identity->fpr, &has_private); 
krista@1352
   726
            
krista@1371
   727
            // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
krista@1357
   728
            if (has_private) {
krista@1357
   729
                identity->fpr = strdup(stored_identity->fpr);
krista@1357
   730
                assert(identity->fpr);
krista@1357
   731
                if (identity->fpr == NULL)
krista@1357
   732
                {
krista@1357
   733
                    return PEP_OUT_OF_MEMORY;
krista@1357
   734
                }
krista@1357
   735
                dont_use_stored_fpr = false;
Edouard@560
   736
            }
Edouard@560
   737
        }
krista@1357
   738
        
edouard@1406
   739
        identity->flags = (identity->flags & 255) | stored_identity->flags;
edouard@1367
   740
        free_identity(stored_identity);
Edouard@588
   741
    }
krista@1357
   742
    
krista@1357
   743
    if (dont_use_stored_fpr && !EMPTYSTR(identity->fpr))
Edouard@588
   744
    {
Edouard@588
   745
        // App must have a good reason to give fpr, such as explicit
Edouard@588
   746
        // import of private key, or similar.
Edouard@588
   747
Edouard@658
   748
        // Take given fpr as-is.
vb@934
   749
krista@1357
   750
        // BUT:
krista@1357
   751
        // First check to see if it's blacklisted or private part is missing?
krista@1357
   752
        bool has_private = false;
krista@1357
   753
        
krista@1357
   754
        status = _has_usable_priv_key(session, identity->fpr, &has_private); 
krista@1357
   755
        
krista@1371
   756
        // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
krista@1357
   757
        if (has_private) {
krista@1357
   758
            dont_use_input_fpr = false;
krista@1357
   759
        }
Edouard@560
   760
    }
krista@1357
   761
    
krista@1357
   762
    // Ok, we failed to get keys either way, so let's elect one.
krista@1357
   763
    if (dont_use_input_fpr && dont_use_stored_fpr)
Edouard@560
   764
    {
Edouard@774
   765
        status = elect_ownkey(session, identity);
Edouard@774
   766
        assert(status == PEP_STATUS_OK);
Edouard@774
   767
        if (status != PEP_STATUS_OK) {
roker@1853
   768
            return ADD_TO_LOG(status);
Edouard@560
   769
        }
vb@934
   770
krista@1357
   771
        bool has_private = false;
krista@1359
   772
        if (identity->fpr) {
krista@1359
   773
            // ok, we elected something.
krista@1359
   774
            // elect_ownkey only returns private keys, so we don't check again.
krista@1359
   775
            // Check to see if it's blacklisted
krista@1359
   776
            bool listed;
krista@1359
   777
            status = blacklist_is_listed(session, identity->fpr, &listed); 
krista@1371
   778
krista@1371
   779
            if (status == PEP_STATUS_OK)
krista@1371
   780
                has_private = !listed;
krista@1359
   781
        }
krista@1357
   782
        
krista@1357
   783
        if (has_private) {
krista@1357
   784
            dont_use_input_fpr = false;
krista@1357
   785
        }
krista@1357
   786
        else { // OK, we've tried everything. Time to generate new keys.
krista@1359
   787
            free(identity->fpr); // It can stay in this state (unallocated) because we'll generate a new key 
krista@1359
   788
            identity->fpr = NULL;
krista@1357
   789
        }
Edouard@560
   790
    }
Edouard@695
   791
Edouard@695
   792
    bool revoked = false;
Edouard@695
   793
    char *r_fpr = NULL;
Edouard@695
   794
    if (!EMPTYSTR(identity->fpr))
Edouard@695
   795
    {
Edouard@695
   796
        status = key_revoked(session, identity->fpr, &revoked);
Edouard@775
   797
edouard@1752
   798
        if (status != PEP_STATUS_OK) 
Edouard@775
   799
        {
roker@1853
   800
            return ADD_TO_LOG(status);
Edouard@695
   801
        }
Edouard@695
   802
    }
edouard@1140
   803
   
Edouard@695
   804
    if (EMPTYSTR(identity->fpr) || revoked)
roker@1734
   805
    {
edouard@1385
   806
        if(!do_keygen){
roker@1853
   807
            return ADD_TO_LOG(PEP_GET_KEY_FAILED);
edouard@1385
   808
        }
edouard@1385
   809
Edouard@695
   810
        if(revoked)
Edouard@695
   811
        {
Edouard@697
   812
            r_fpr = identity->fpr;
Edouard@697
   813
            identity->fpr = NULL;
Edouard@695
   814
        }
Edouard@560
   815
        
vb@215
   816
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   817
        status = generate_keypair(session, identity);
vb@0
   818
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   819
        if (status != PEP_STATUS_OK) {
vb@0
   820
            char buf[11];
vb@0
   821
            snprintf(buf, 11, "%d", status);
vb@215
   822
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   823
            if(revoked && r_fpr)
Edouard@695
   824
                free(r_fpr);
roker@1853
   825
            return ADD_TO_LOG(status);
vb@0
   826
        }
edouard@1140
   827
Edouard@560
   828
        
Edouard@695
   829
        if(revoked)
Edouard@695
   830
        {
Edouard@695
   831
            status = set_revoked(session, r_fpr,
Edouard@695
   832
                                 identity->fpr, time(NULL));
Edouard@695
   833
            free(r_fpr);
Edouard@695
   834
            if (status != PEP_STATUS_OK) {
roker@1853
   835
                return ADD_TO_LOG(status);
Edouard@695
   836
            }
Edouard@588
   837
        }
vb@0
   838
    }
Edouard@560
   839
    else
Edouard@560
   840
    {
vb@214
   841
        bool expired;
Edouard@701
   842
        status = key_expired(session, identity->fpr, 
Edouard@701
   843
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   844
                             &expired);
Edouard@701
   845
vb@214
   846
        assert(status == PEP_STATUS_OK);
Edouard@499
   847
        if (status != PEP_STATUS_OK) {
roker@1853
   848
            return ADD_TO_LOG(status);
Edouard@499
   849
        }
vb@214
   850
vb@214
   851
        if (status == PEP_STATUS_OK && expired) {
vb@214
   852
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   853
            renew_key(session, identity->fpr, ts);
vb@214
   854
            free_timestamp(ts);
vb@214
   855
        }
vb@214
   856
    }
vb@0
   857
krista@1353
   858
    if (!identity->username)
krista@1353
   859
        identity->username = strdup("");
krista@1353
   860
    
vb@0
   861
    status = set_identity(session, identity);
vb@0
   862
    assert(status == PEP_STATUS_OK);
Edouard@499
   863
    if (status != PEP_STATUS_OK) {
Edouard@588
   864
        return status;
Edouard@499
   865
    }
vb@0
   866
roker@1853
   867
    return ADD_TO_LOG(PEP_STATUS_OK);
vb@0
   868
}
vb@0
   869
krista@2308
   870
DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
krista@2308
   871
                                                 identity_list* my_idents) {
krista@2308
   872
    PEP_STATUS status = PEP_STATUS_OK;
krista@2308
   873
    if (!session)
krista@2308
   874
        return PEP_ILLEGAL_VALUE;
krista@2308
   875
        
krista@2308
   876
    char* stored_own_userid = NULL;
krista@2308
   877
    get_own_userid(session, &stored_own_userid);
krista@2308
   878
    
krista@2308
   879
    identity_list* ident_curr = my_idents;
krista@2308
   880
    while (ident_curr) {
krista@2308
   881
        pEp_identity* ident = ident_curr->ident;
krista@2308
   882
        if (!ident)
krista@2308
   883
            return PEP_ILLEGAL_VALUE;
krista@2308
   884
            
krista@2308
   885
        if (stored_own_userid) {
krista@2308
   886
            if (!ident->user_id) 
krista@2308
   887
                ident->user_id = strdup(stored_own_userid);
krista@2308
   888
            else if (strcmp(stored_own_userid, ident->user_id) != 0)
krista@2308
   889
                return PEP_ILLEGAL_VALUE;
krista@2308
   890
        }
krista@2308
   891
        else if (!ident->user_id) {
krista@2308
   892
            stored_own_userid = PEP_OWN_USERID;
krista@2308
   893
            ident->user_id = strdup(PEP_OWN_USERID);
krista@2308
   894
        }
krista@2308
   895
        
krista@2308
   896
        ident->me = true; // Just in case.
krista@2308
   897
        
krista@2308
   898
        // Ok, do it...
krista@2308
   899
        status = set_identity(session, ident);
krista@2308
   900
        if (status != PEP_STATUS_OK)
krista@2308
   901
            return status;
krista@2308
   902
        
krista@2308
   903
        ident_curr = ident_curr->next;
krista@2308
   904
    }
krista@2308
   905
    
krista@2308
   906
    return status;
krista@2308
   907
}
krista@2308
   908
edouard@1385
   909
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
edouard@1385
   910
{
roker@1853
   911
    return ADD_TO_LOG(_myself(session, identity, true, false));
edouard@1385
   912
}
edouard@1385
   913
vb@296
   914
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   915
        PEP_SESSION session, 
vb@292
   916
        examine_identity_t examine_identity,
vb@292
   917
        void *management
vb@292
   918
    )
vb@292
   919
{
vb@292
   920
    assert(session);
vb@292
   921
    if (!session)
vb@292
   922
        return PEP_ILLEGAL_VALUE;
vb@292
   923
vb@292
   924
    session->examine_management = management;
vb@292
   925
    session->examine_identity = examine_identity;
vb@292
   926
vb@292
   927
    return PEP_STATUS_OK;
vb@292
   928
}
vb@292
   929
vb@0
   930
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   931
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   932
        void *management
vb@0
   933
    )
vb@0
   934
{
vb@0
   935
    PEP_SESSION session;
vb@0
   936
    pEp_identity *identity;
Edouard@499
   937
    PEP_STATUS status;
vb@0
   938
Edouard@499
   939
    assert(retrieve_next_identity);
Edouard@499
   940
    assert(management);
Edouard@499
   941
Edouard@499
   942
    if (!retrieve_next_identity || !management)
Edouard@499
   943
        return PEP_ILLEGAL_VALUE;
Edouard@499
   944
Edouard@499
   945
    status = init(&session);
vb@0
   946
    assert(status == PEP_STATUS_OK);
vb@0
   947
    if (status != PEP_STATUS_OK)
vb@0
   948
        return status;
vb@0
   949
vb@0
   950
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   951
Edouard@499
   952
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   953
    {
vb@0
   954
        assert(identity->address);
Edouard@499
   955
        if(identity->address)
Edouard@499
   956
        {
Edouard@499
   957
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   958
krista@2327
   959
            if (identity->me) {
Edouard@499
   960
                status = myself(session, identity);
Edouard@499
   961
            } else {
Edouard@499
   962
                status = recv_key(session, identity->address);
Edouard@499
   963
            }
Edouard@499
   964
vb@0
   965
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   966
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   967
                return PEP_OUT_OF_MEMORY;
vb@0
   968
        }
vb@0
   969
        free_identity(identity);
vb@0
   970
    }
vb@0
   971
vb@0
   972
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   973
vb@0
   974
    release(session);
vb@0
   975
    return PEP_STATUS_OK;
vb@0
   976
}
vb@0
   977
krista@1213
   978
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
   979
        PEP_SESSION session,
vb@357
   980
        pEp_identity *ident
vb@357
   981
    )
vb@215
   982
{
vb@218
   983
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   984
vb@215
   985
    assert(session);
vb@357
   986
    assert(ident);
Edouard@439
   987
    assert(!EMPTYSTR(ident->fpr));
vb@215
   988
vb@357
   989
    if (!(session && ident && ident->fpr))
vb@215
   990
        return PEP_ILLEGAL_VALUE;
vb@215
   991
krista@2327
   992
    if (ident->me)
Edouard@697
   993
    {
vb@357
   994
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   995
        myself(session, ident);
Edouard@697
   996
    }
Edouard@697
   997
    else
Edouard@697
   998
    {
krista@2129
   999
        // for undo
krista@2129
  1000
        if (session->cached_mistrusted)
krista@2129
  1001
            free(session->cached_mistrusted);
krista@2129
  1002
        session->cached_mistrusted = identity_dup(ident);
Edouard@697
  1003
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
  1004
    }
vb@218
  1005
vb@218
  1006
    return status;
vb@215
  1007
}
vb@215
  1008
krista@2129
  1009
DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session) {
krista@2129
  1010
    assert(session);
krista@2129
  1011
    
krista@2129
  1012
    if (!session)
krista@2129
  1013
        return PEP_ILLEGAL_VALUE;
krista@2129
  1014
    
krista@2129
  1015
    PEP_STATUS status = PEP_STATUS_OK;
krista@2129
  1016
        
krista@2129
  1017
    pEp_identity* cached_ident = session->cached_mistrusted;
krista@2129
  1018
    
krista@2129
  1019
    if (!cached_ident)
krista@2129
  1020
        status = PEP_CANNOT_FIND_IDENTITY;
krista@2129
  1021
    else {
krista@2129
  1022
        status = set_identity(session, cached_ident);            
krista@2129
  1023
        free_identity(session->cached_mistrusted);
krista@2129
  1024
    }
krista@2129
  1025
    
krista@2129
  1026
    session->cached_mistrusted = NULL;
krista@2129
  1027
krista@2129
  1028
    return status;
krista@2129
  1029
}
krista@2129
  1030
Edouard@410
  1031
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
  1032
        PEP_SESSION session,
Edouard@410
  1033
        pEp_identity *ident
Edouard@410
  1034
    )
Edouard@410
  1035
{
Edouard@410
  1036
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
  1037
Edouard@410
  1038
    assert(session);
Edouard@410
  1039
    assert(ident);
krista@2327
  1040
    assert(!ident->me);
Edouard@439
  1041
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
  1042
    assert(!EMPTYSTR(ident->address));
Edouard@439
  1043
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
  1044
krista@2327
  1045
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
  1046
            ident->user_id))
Edouard@410
  1047
        return PEP_ILLEGAL_VALUE;
Edouard@410
  1048
vb@420
  1049
    status = update_identity(session, ident);
vb@420
  1050
    if (status != PEP_STATUS_OK)
vb@420
  1051
        return status;
Edouard@410
  1052
Edouard@442
  1053
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
  1054
        ident->comm_type = PEP_ct_unknown;
vb@421
  1055
    else
vb@421
  1056
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
  1057
vb@420
  1058
    status = set_identity(session, ident);
vb@421
  1059
    if (status != PEP_STATUS_OK)
vb@421
  1060
        return status;
vb@421
  1061
vb@422
  1062
    if (ident->comm_type == PEP_ct_unknown)
vb@422
  1063
        status = update_identity(session, ident);
Edouard@410
  1064
    return status;
Edouard@410
  1065
}
Edouard@410
  1066
vb@354
  1067
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
  1068
        PEP_SESSION session,
vb@354
  1069
        pEp_identity *ident
vb@354
  1070
    )
vb@354
  1071
{
vb@354
  1072
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
  1073
vb@354
  1074
    assert(session);
vb@354
  1075
    assert(ident);
Edouard@439
  1076
    assert(!EMPTYSTR(ident->address));
Edouard@439
  1077
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
  1078
    assert(!EMPTYSTR(ident->fpr));
vb@354
  1079
Edouard@439
  1080
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
edouard@1945
  1081
            EMPTYSTR(ident->fpr))
vb@354
  1082
        return PEP_ILLEGAL_VALUE;
vb@354
  1083
vb@354
  1084
    status = update_identity(session, ident);
vb@354
  1085
    if (status != PEP_STATUS_OK)
vb@354
  1086
        return status;
vb@354
  1087
vb@356
  1088
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
  1089
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
  1090
        status = set_identity(session, ident);
vb@356
  1091
    }
vb@356
  1092
    else {
vb@356
  1093
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
  1094
        status = PEP_CANNOT_SET_TRUST;
vb@356
  1095
    }
vb@354
  1096
vb@354
  1097
    return status;
vb@354
  1098
}
vb@354
  1099
Edouard@584
  1100
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
  1101
        PEP_SESSION session,
vb@955
  1102
        const char *fpr,
vb@955
  1103
        bool *listed
vb@955
  1104
    )
Edouard@584
  1105
{
Edouard@584
  1106
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
  1107
    int count;
Edouard@584
  1108
    
Edouard@584
  1109
    assert(session && fpr && fpr[0] && listed);
Edouard@584
  1110
    
Edouard@584
  1111
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
  1112
        return PEP_ILLEGAL_VALUE;
Edouard@584
  1113
    
Edouard@584
  1114
    *listed = false;
Edouard@584
  1115
    
Edouard@584
  1116
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
  1117
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
  1118
    
Edouard@584
  1119
    int result;
Edouard@584
  1120
    
Edouard@584
  1121
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
  1122
    switch (result) {
Edouard@584
  1123
        case SQLITE_ROW:
Edouard@584
  1124
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
  1125
            *listed = count > 0;
krista@2288
  1126
            status = PEP_STATUS_OK;
Edouard@584
  1127
            break;
Edouard@584
  1128
            
Edouard@584
  1129
        default:
Edouard@584
  1130
            status = PEP_UNKNOWN_ERROR;
Edouard@584
  1131
    }
Edouard@584
  1132
    
Edouard@584
  1133
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
  1134
    return status;
Edouard@584
  1135
}
Edouard@584
  1136
edouard@1412
  1137
PEP_STATUS _own_identities_retrieve(
vb@955
  1138
        PEP_SESSION session,
edouard@1412
  1139
        identity_list **own_identities,
edouard@1412
  1140
        identity_flags_t excluded_flags
vb@955
  1141
      )
Edouard@584
  1142
{
Edouard@584
  1143
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
  1144
    
vb@955
  1145
    assert(session && own_identities);
vb@955
  1146
    if (!(session && own_identities))
Edouard@584
  1147
        return PEP_ILLEGAL_VALUE;
Edouard@584
  1148
    
vb@955
  1149
    *own_identities = NULL;
vb@955
  1150
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
  1151
    if (_own_identities == NULL)
Edouard@584
  1152
        goto enomem;
Edouard@584
  1153
    
vb@955
  1154
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
  1155
    
Edouard@584
  1156
    int result;
vb@955
  1157
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
  1158
    const char *address = NULL;
Edouard@584
  1159
    const char *fpr = NULL;
vb@955
  1160
    const char *username = NULL;
vb@955
  1161
    const char *user_id = NULL;
vb@955
  1162
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
  1163
    const char *lang = NULL;
vb@955
  1164
    unsigned int flags = 0;
Edouard@584
  1165
    
vb@955
  1166
    identity_list *_bl = _own_identities;
Edouard@584
  1167
    do {
edouard@1412
  1168
        sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
vb@955
  1169
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
  1170
        switch (result) {
Edouard@584
  1171
            case SQLITE_ROW:
vb@955
  1172
                address = (const char *)
vb@955
  1173
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
  1174
                fpr = (const char *)
vb@955
  1175
                    sqlite3_column_text(session->own_identities_retrieve, 1);
krista@2301
  1176
                user_id = (const char *)
krista@2301
  1177
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
  1178
                username = (const char *)
krista@2301
  1179
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
  1180
                comm_type = PEP_ct_pEp;
vb@1071
  1181
                lang = (const char *)
krista@2301
  1182
                    sqlite3_column_text(session->own_identities_retrieve, 4);
vb@1071
  1183
                flags = (unsigned int)
krista@2301
  1184
                    sqlite3_column_int(session->own_identities_retrieve, 5);
vb@955
  1185
vb@1078
  1186
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
  1187
                if (!ident)
Edouard@584
  1188
                    goto enomem;
vb@955
  1189
                ident->comm_type = comm_type;
vb@955
  1190
                if (lang && lang[0]) {
vb@955
  1191
                    ident->lang[0] = lang[0];
vb@955
  1192
                    ident->lang[1] = lang[1];
vb@1078
  1193
                    ident->lang[2] = 0;
vb@955
  1194
                }
krista@2288
  1195
                ident->me = true;
vb@955
  1196
                ident->flags = flags;
vb@955
  1197
vb@955
  1198
                _bl = identity_list_add(_bl, ident);
vb@1080
  1199
                if (_bl == NULL) {
vb@1080
  1200
                    free_identity(ident);
vb@983
  1201
                    goto enomem;
vb@1080
  1202
                }
Edouard@584
  1203
                
Edouard@584
  1204
                break;
Edouard@584
  1205
                
Edouard@584
  1206
            case SQLITE_DONE:
Edouard@584
  1207
                break;
Edouard@584
  1208
                
Edouard@584
  1209
            default:
Edouard@584
  1210
                status = PEP_UNKNOWN_ERROR;
Edouard@584
  1211
                result = SQLITE_DONE;
Edouard@584
  1212
        }
Edouard@584
  1213
    } while (result != SQLITE_DONE);
Edouard@584
  1214
    
vb@955
  1215
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
  1216
    if (status == PEP_STATUS_OK)
vb@955
  1217
        *own_identities = _own_identities;
Edouard@584
  1218
    else
vb@955
  1219
        free_identity_list(_own_identities);
Edouard@584
  1220
    
Edouard@584
  1221
    goto the_end;
Edouard@584
  1222
    
Edouard@584
  1223
enomem:
vb@955
  1224
    free_identity_list(_own_identities);
Edouard@584
  1225
    status = PEP_OUT_OF_MEMORY;
Edouard@584
  1226
    
Edouard@584
  1227
the_end:
Edouard@584
  1228
    return status;
Edouard@584
  1229
}
vb@955
  1230
edouard@1412
  1231
DYNAMIC_API PEP_STATUS own_identities_retrieve(
edouard@1364
  1232
        PEP_SESSION session,
edouard@1412
  1233
        identity_list **own_identities
edouard@1412
  1234
      )
edouard@1412
  1235
{
edouard@1412
  1236
    return _own_identities_retrieve(session, own_identities, 0);
edouard@1412
  1237
}
edouard@1412
  1238
edouard@1412
  1239
PEP_STATUS _own_keys_retrieve(
edouard@1412
  1240
        PEP_SESSION session,
edouard@1412
  1241
        stringlist_t **keylist,
edouard@1412
  1242
        identity_flags_t excluded_flags
edouard@1364
  1243
      )
edouard@1364
  1244
{
edouard@1364
  1245
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1364
  1246
    
edouard@1364
  1247
    assert(session && keylist);
edouard@1364
  1248
    if (!(session && keylist))
edouard@1364
  1249
        return PEP_ILLEGAL_VALUE;
edouard@1364
  1250
    
edouard@1364
  1251
    *keylist = NULL;
edouard@1364
  1252
    stringlist_t *_keylist = NULL;
edouard@1364
  1253
    
edouard@1394
  1254
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1255
    
edouard@1364
  1256
    int result;
edouard@1364
  1257
    char *fpr = NULL;
edouard@1364
  1258
    
edouard@1364
  1259
    stringlist_t *_bl = _keylist;
edouard@1364
  1260
    do {
edouard@1412
  1261
        sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
edouard@1394
  1262
        result = sqlite3_step(session->own_keys_retrieve);
edouard@1364
  1263
        switch (result) {
edouard@1364
  1264
            case SQLITE_ROW:
edouard@1394
  1265
                fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
edouard@1364
  1266
                if(fpr == NULL)
edouard@1364
  1267
                    goto enomem;
edouard@1364
  1268
edouard@1364
  1269
                _bl = stringlist_add(_bl, fpr);
edouard@1364
  1270
                if (_bl == NULL) {
edouard@1364
  1271
                    free(fpr);
edouard@1364
  1272
                    goto enomem;
edouard@1364
  1273
                }
edouard@1364
  1274
                if (_keylist == NULL)
edouard@1364
  1275
                    _keylist = _bl;
edouard@1364
  1276
                
edouard@1364
  1277
                break;
edouard@1364
  1278
                
edouard@1364
  1279
            case SQLITE_DONE:
edouard@1364
  1280
                break;
edouard@1364
  1281
                
edouard@1364
  1282
            default:
edouard@1364
  1283
                status = PEP_UNKNOWN_ERROR;
edouard@1364
  1284
                result = SQLITE_DONE;
edouard@1364
  1285
        }
edouard@1364
  1286
    } while (result != SQLITE_DONE);
edouard@1364
  1287
    
edouard@1394
  1288
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1289
    if (status == PEP_STATUS_OK)
edouard@1364
  1290
        *keylist = _keylist;
edouard@1364
  1291
    else
edouard@1364
  1292
        free_stringlist(_keylist);
edouard@1364
  1293
    
edouard@1364
  1294
    goto the_end;
edouard@1364
  1295
    
edouard@1364
  1296
enomem:
edouard@1364
  1297
    free_stringlist(_keylist);
edouard@1364
  1298
    status = PEP_OUT_OF_MEMORY;
edouard@1364
  1299
    
edouard@1364
  1300
the_end:
edouard@1364
  1301
    return status;
edouard@1364
  1302
}
edouard@1364
  1303
edouard@1412
  1304
DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
edouard@1412
  1305
{
edouard@1412
  1306
    return _own_keys_retrieve(session, keylist, 0);
edouard@1412
  1307
}
edouard@1412
  1308
edouard@1760
  1309
// FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
edouard@1394
  1310
DYNAMIC_API PEP_STATUS set_own_key(
edouard@1394
  1311
       PEP_SESSION session,
edouard@1394
  1312
       const char *address,
edouard@1394
  1313
       const char *fpr
edouard@1394
  1314
    )
edouard@1394
  1315
{
edouard@1394
  1316
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1394
  1317
    
edouard@1394
  1318
    assert(session &&
edouard@1760
  1319
           address &&
edouard@1394
  1320
           fpr && fpr[0]
edouard@1394
  1321
          );
edouard@1394
  1322
    
edouard@1394
  1323
    if (!(session &&
edouard@1760
  1324
          address &&
edouard@1394
  1325
          fpr && fpr[0]
edouard@1394
  1326
         ))
edouard@1394
  1327
        return PEP_ILLEGAL_VALUE;
krista@2301
  1328
            
krista@2301
  1329
            
krista@2301
  1330
    // First see if we have it in own identities already, AND we retrieve
krista@2301
  1331
    // our own user_id
krista@2301
  1332
    pEp_identity* my_id = NULL;
krista@2301
  1333
    identity_list* my_identities = NULL;
krista@2301
  1334
    char* my_user_id = NULL;
krista@2301
  1335
    status = own_identities_retrieve(session, &my_identities);
edouard@1394
  1336
    
krista@2301
  1337
    if (status == PEP_STATUS_OK) {
krista@2301
  1338
        if (my_identities) {
krista@2301
  1339
            if (!(my_identities->ident && my_identities->ident->user_id))
krista@2301
  1340
                return PEP_ILLEGAL_VALUE;
edouard@1394
  1341
krista@2301
  1342
            my_user_id = strdup(my_identities->ident->user_id);
krista@2301
  1343
krista@2301
  1344
            if (!my_user_id) 
krista@2301
  1345
                return PEP_OUT_OF_MEMORY;
krista@2301
  1346
            
krista@2301
  1347
            // Probably cheaper than all the strcmps if there are many,
krista@2301
  1348
            // plus this avoids the capitalisation and . problems:
krista@2301
  1349
            
krista@2301
  1350
            status = get_identity(session, my_user_id, address, &my_id);
krista@2301
  1351
            
krista@2301
  1352
            if (status == PEP_STATUS_OK && my_id) {
krista@2304
  1353
                if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
krista@2301
  1354
                    // We're done. It was already here.
krista@2301
  1355
                    // FIXME: Do we check trust/revocation/?
krista@2301
  1356
                    goto pep_free;
krista@2301
  1357
                }            
krista@2301
  1358
            }
krista@2301
  1359
            
krista@2301
  1360
            // Otherwise, we see if there's a binding for this user_id/key
krista@2301
  1361
            // in the trust DB
krista@2301
  1362
            
krista@2301
  1363
            // If there's an id w/ user_id + address
krista@2301
  1364
            if (my_id) {
krista@2301
  1365
                free(my_id->fpr);
krista@2301
  1366
                my_id->fpr = my_user_id;
krista@2304
  1367
                my_id->comm_type = PEP_ct_pEp;
krista@2304
  1368
                my_id->me = true; // just in case? 
krista@2301
  1369
            }
krista@2301
  1370
            else { // Else, we need a new identity
krista@2304
  1371
                my_id = new_identity(address, fpr, my_user_id, NULL); 
krista@2301
  1372
                if (status != PEP_STATUS_OK)
krista@2301
  1373
                    goto pep_free; 
krista@2304
  1374
                my_id->me = true;
krista@2304
  1375
                my_id->comm_type = PEP_ct_pEp;
krista@2301
  1376
            }
krista@2301
  1377
        }
krista@2301
  1378
        else {
krista@2301
  1379
            // I think the prerequisite should be that at least one own identity
krista@2301
  1380
            // already in the DB, so REALLY look at this.
krista@2301
  1381
            return PEP_CANNOT_FIND_IDENTITY;
krista@2301
  1382
        }
krista@2301
  1383
        
krista@2301
  1384
        status = set_identity(session, my_id);
krista@2301
  1385
    }  
edouard@1394
  1386
    
krista@2301
  1387
pep_free:
krista@2301
  1388
    free(my_id);
krista@2301
  1389
    free(my_user_id);
edouard@1394
  1390
    return status;
edouard@1394
  1391
}
krista@1357
  1392
krista@1357
  1393
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
  1394
                             bool *has_private) {
krista@1357
  1395
krista@1357
  1396
    assert(session);
krista@1357
  1397
    assert(fpr);
krista@1357
  1398
    assert(has_private);
krista@1357
  1399
    
krista@1357
  1400
    if (!(session && fpr && has_private))
krista@1357
  1401
        return PEP_ILLEGAL_VALUE;
krista@1357
  1402
krista@1357
  1403
    return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
edouard@1385
  1404
}