src/keymanagement.c
author Krista Bennett <krista@pep-project.org>
Sun, 04 Feb 2018 03:36:08 +0100
branchmistrust_fixes
changeset 2471 4f1c2d62b2dd
parent 2467 ef6bb2e8a84c
child 2477 45e8ed722fec
permissions -rw-r--r--
A whole horde of trust fixes (mistrust list added, tests fixed, and sneaky null string in elect key removed)
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@2461
    15
#include "sync_fsm.h"
krista@1253
    16
#include "blacklist.h"
edouard@1195
    17
krista@2461
    18
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
vb@8
    19
krista@2461
    20
static bool key_matches_address(PEP_SESSION session, const char* address,
krista@2461
    21
                                const char* fpr) {
krista@2461
    22
    if (!session || !address || !fpr)
krista@2461
    23
        return false;
krista@2461
    24
    
krista@2461
    25
    bool retval = false;
krista@2461
    26
    stringlist_t *keylist = NULL;
krista@2461
    27
    PEP_STATUS status = find_keys(session, address, &keylist);
krista@2461
    28
    if (status == PEP_STATUS_OK && keylist) {
krista@2461
    29
        stringlist_t* curr = keylist;
krista@2461
    30
        while (curr) {
krista@2461
    31
            if (curr->value) {
krista@2461
    32
                if (strcasecmp(curr->value, fpr)) {
krista@2461
    33
                    retval = true;
krista@2461
    34
                    break;
krista@2461
    35
                }
krista@2461
    36
            }
krista@2461
    37
            curr = curr->next;
krista@2461
    38
        }
krista@2461
    39
    }
krista@2461
    40
    
krista@2461
    41
    free_stringlist(keylist);
krista@2461
    42
    return retval;                             
krista@2461
    43
}
vb@214
    44
Edouard@774
    45
PEP_STATUS elect_pubkey(
Edouard@755
    46
        PEP_SESSION session, pEp_identity * identity
Edouard@755
    47
    )
Edouard@755
    48
{
Edouard@755
    49
    PEP_STATUS status;
roker@1559
    50
    stringlist_t *keylist = NULL;
krista@1342
    51
    char *_fpr = "";
Edouard@755
    52
    identity->comm_type = PEP_ct_unknown;
Edouard@755
    53
Edouard@755
    54
    status = find_keys(session, identity->address, &keylist);
Edouard@755
    55
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    56
    if (status == PEP_OUT_OF_MEMORY)
Edouard@755
    57
        return PEP_OUT_OF_MEMORY;
krista@2461
    58
    
krista@2461
    59
    if (!keylist || !keylist->value)
krista@2461
    60
        identity->comm_type = PEP_ct_key_not_found;    
krista@2461
    61
    else {
krista@2461
    62
        stringlist_t *_keylist;
krista@2461
    63
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
krista@2461
    64
            PEP_comm_type _comm_type_key;
Edouard@755
    65
krista@2461
    66
            status = get_key_rating(session, _keylist->value, &_comm_type_key);
krista@2461
    67
            assert(status != PEP_OUT_OF_MEMORY);
krista@2461
    68
            if (status == PEP_OUT_OF_MEMORY) {
krista@2461
    69
                free_stringlist(keylist);
krista@2461
    70
                return PEP_OUT_OF_MEMORY;
krista@2461
    71
            }
Edouard@755
    72
krista@2461
    73
            if (_comm_type_key != PEP_ct_compromized &&
krista@2461
    74
                _comm_type_key != PEP_ct_unknown)
Edouard@755
    75
            {
krista@2461
    76
                if (identity->comm_type == PEP_ct_unknown ||
krista@2461
    77
                    _comm_type_key > identity->comm_type)
krista@2461
    78
                {
krista@2461
    79
                    bool blacklisted;
krista@2471
    80
                    bool mistrusted;
krista@2471
    81
                    status = is_mistrusted_key(session, _keylist->value, &mistrusted);
krista@2471
    82
                    if (status == PEP_STATUS_OK)
krista@2471
    83
                        status = blacklist_is_listed(session, _keylist->value, &blacklisted);
krista@2471
    84
                    if (status == PEP_STATUS_OK && !mistrusted && !blacklisted) {
krista@2461
    85
                        identity->comm_type = _comm_type_key;
krista@2461
    86
                        _fpr = _keylist->value;
krista@2461
    87
                    }
krista@1275
    88
                }
Edouard@755
    89
            }
Edouard@755
    90
        }
Edouard@755
    91
    }
krista@1342
    92
    free(identity->fpr);
Edouard@755
    93
krista@2471
    94
    if (!_fpr || _fpr[0] == '\0')
krista@2471
    95
        identity->fpr = NULL;
krista@2471
    96
    else {    
krista@2471
    97
        identity->fpr = strdup(_fpr);
krista@2471
    98
        if (identity->fpr == NULL) {
krista@2471
    99
            free_stringlist(keylist);
krista@2471
   100
            return PEP_OUT_OF_MEMORY;
krista@2471
   101
        }
Edouard@755
   102
    }
krista@2461
   103
    
Edouard@755
   104
    free_stringlist(keylist);
Edouard@755
   105
    return PEP_STATUS_OK;
Edouard@755
   106
}
Edouard@755
   107
krista@2461
   108
static PEP_STATUS validate_fpr(PEP_SESSION session, 
krista@2461
   109
                               pEp_identity* ident) {
krista@2461
   110
    
krista@2461
   111
    PEP_STATUS status = PEP_STATUS_OK;
krista@2461
   112
    
krista@2461
   113
    if (!session || !ident || !ident->fpr || !ident->fpr[0])
krista@2461
   114
        return PEP_ILLEGAL_VALUE;    
krista@2461
   115
        
krista@2461
   116
    char* fpr = ident->fpr;
krista@2461
   117
    
krista@2461
   118
    bool has_private = false;
krista@2461
   119
    
krista@2461
   120
    if (ident->me) {
krista@2461
   121
        status = contains_priv_key(session, fpr, &has_private);
krista@2461
   122
        if (status != PEP_STATUS_OK || !has_private)
krista@2461
   123
            return PEP_KEY_UNSUITABLE;
krista@2461
   124
    }
krista@2461
   125
    
krista@2461
   126
    status = get_trust(session, ident);
krista@2461
   127
    if (status != PEP_STATUS_OK)
krista@2461
   128
        ident->comm_type = PEP_ct_unknown;
krista@2461
   129
            
krista@2461
   130
    PEP_comm_type ct = ident->comm_type;
krista@2461
   131
krista@2461
   132
    if (ct == PEP_ct_unknown) {
krista@2461
   133
        // If status is bad, it's ok, we get the rating
krista@2461
   134
        // we should use then (PEP_ct_unknown)
krista@2461
   135
        get_key_rating(session, fpr, &ct);
krista@2461
   136
        ident->comm_type = ct;
krista@2461
   137
    }
krista@2461
   138
    
krista@2461
   139
    bool revoked, expired;
krista@2461
   140
    bool blacklisted = false;
krista@2461
   141
    
krista@2461
   142
    status = key_revoked(session, fpr, &revoked);    
krista@2461
   143
        
krista@2461
   144
    if (status != PEP_STATUS_OK) {
krista@2461
   145
        return ADD_TO_LOG(status);
krista@2461
   146
    }
krista@2461
   147
    
krista@2461
   148
    if (!revoked) {
krista@2461
   149
        time_t exp_time = (ident->me ? 
krista@2461
   150
                           time(NULL) + (7*24*3600) : time(NULL));
krista@2461
   151
                           
krista@2461
   152
        status = key_expired(session, fpr, 
krista@2461
   153
                             exp_time,
krista@2461
   154
                             &expired);
krista@2461
   155
                             
krista@2461
   156
        assert(status == PEP_STATUS_OK);
krista@2461
   157
        if (status != PEP_STATUS_OK)
krista@2461
   158
            return ADD_TO_LOG(status);
krista@2461
   159
krista@2461
   160
        if ((ct | PEP_ct_confirmed) == PEP_ct_OpenPGP &&
krista@2461
   161
            !ident->me) {
krista@2461
   162
            status = blacklist_is_listed(session, 
krista@2461
   163
                                         fpr, 
krista@2461
   164
                                         &blacklisted);
krista@2461
   165
                                         
krista@2461
   166
            if (status != PEP_STATUS_OK)
krista@2461
   167
                return ADD_TO_LOG(status);
krista@2461
   168
        }
krista@2461
   169
    }
krista@2461
   170
            
krista@2461
   171
    if (ident->me && (ct >= PEP_ct_strong_but_unconfirmed) && !revoked && expired) {
krista@2461
   172
        // extend key
krista@2461
   173
        timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
krista@2461
   174
        status = renew_key(session, fpr, ts);
krista@2461
   175
        free_timestamp(ts);
krista@2461
   176
krista@2461
   177
        if (status == PEP_STATUS_OK) {
krista@2461
   178
            // if key is valid (second check because pEp key might be extended above)
krista@2461
   179
            //      Return fpr        
krista@2461
   180
            status = key_expired(session, fpr, time(NULL), &expired);            
krista@2461
   181
            if (status != PEP_STATUS_OK) {
krista@2461
   182
                 ident->comm_type = PEP_ct_key_expired;
krista@2461
   183
                 return ADD_TO_LOG(status);
krista@2461
   184
             }
krista@2461
   185
            // communicate key(?)
krista@2461
   186
        }        
krista@2461
   187
    }
krista@2461
   188
     
krista@2461
   189
    if (revoked) 
krista@2461
   190
        ct = PEP_ct_key_revoked;
krista@2461
   191
    else if (expired)
krista@2461
   192
        ct = PEP_ct_key_expired;        
krista@2461
   193
    else if (blacklisted) { // never true for .me
krista@2461
   194
        ident->comm_type = ct = PEP_ct_key_not_found;
krista@2461
   195
        free(ident->fpr);
krista@2461
   196
            ident->fpr = strdup("");
krista@2461
   197
        status = PEP_KEY_BLACKLISTED;
krista@2461
   198
    }
krista@2461
   199
    
krista@2461
   200
    switch (ct) {
krista@2461
   201
        case PEP_ct_key_expired:
krista@2461
   202
        case PEP_ct_key_revoked:
krista@2461
   203
        case PEP_ct_key_b0rken:
krista@2461
   204
            // delete key from being default key for all users/identities
krista@2461
   205
            status = remove_fpr_as_default(session, fpr);
krista@2461
   206
            status = update_trust_for_fpr(session, 
krista@2461
   207
                                          fpr, 
krista@2461
   208
                                          ct);
krista@2467
   209
        case PEP_ct_mistrusted:                                  
krista@2461
   210
            free(ident->fpr);
krista@2461
   211
            ident->fpr = NULL;
krista@2461
   212
            ident->comm_type = ct;            
krista@2461
   213
            status = PEP_KEY_UNSUITABLE;
krista@2461
   214
        default:
krista@2461
   215
            break;
krista@2461
   216
    }            
krista@2461
   217
krista@2461
   218
    return status;
krista@2461
   219
}
krista@2461
   220
krista@2461
   221
PEP_STATUS get_user_default_key(PEP_SESSION session, const char* user_id,
krista@2461
   222
                                char** default_key) {
krista@2461
   223
    assert(session);
krista@2461
   224
    assert(user_id);
krista@2461
   225
    
krista@2461
   226
    if (!session || !user_id)
krista@2461
   227
        return PEP_ILLEGAL_VALUE;
krista@2461
   228
krista@2461
   229
    PEP_STATUS status = PEP_STATUS_OK;
krista@2461
   230
            
krista@2461
   231
    // try to get default key for user_data
krista@2461
   232
    sqlite3_reset(session->get_user_default_key);
krista@2461
   233
    sqlite3_bind_text(session->get_user_default_key, 1, user_id, 
krista@2461
   234
                      -1, SQLITE_STATIC);
krista@2461
   235
    
krista@2461
   236
    const int result = sqlite3_step(session->get_user_default_key);
krista@2461
   237
    char* user_fpr = NULL;
krista@2461
   238
    if (result == SQLITE_ROW) {
krista@2461
   239
        const char* u_fpr =
krista@2461
   240
            (char *) sqlite3_column_text(session->get_user_default_key, 0);
krista@2461
   241
        if (u_fpr)
krista@2461
   242
            user_fpr = strdup(u_fpr);
krista@2461
   243
    }
krista@2461
   244
    else
krista@2461
   245
        status = PEP_GET_KEY_FAILED;
krista@2461
   246
        
krista@2461
   247
    sqlite3_reset(session->get_user_default_key);
krista@2461
   248
    
krista@2461
   249
    *default_key = user_fpr;
krista@2461
   250
    return status;     
krista@2461
   251
}
krista@2461
   252
krista@2461
   253
// Only call on retrieval of previously stored identity!
krista@2461
   254
// Also, we presume that if the stored_identity was sent in
krista@2461
   255
// without an fpr, there wasn't one in the trust DB for this
krista@2461
   256
// identity.
krista@2461
   257
PEP_STATUS get_valid_pubkey(PEP_SESSION session,
krista@2461
   258
                         pEp_identity* stored_identity,
krista@2461
   259
                         bool* is_identity_default,
krista@2461
   260
                         bool* is_user_default,
krista@2461
   261
                         bool* is_address_default) {
krista@2461
   262
    
krista@2461
   263
    PEP_STATUS status = PEP_STATUS_OK;
krista@2461
   264
krista@2461
   265
    if (!stored_identity || EMPTYSTR(stored_identity->user_id)
krista@2461
   266
        || !is_identity_default || !is_user_default || !is_address_default)
krista@2461
   267
        return PEP_ILLEGAL_VALUE;
krista@2461
   268
        
krista@2461
   269
    *is_identity_default = *is_user_default = *is_address_default = false;
krista@2461
   270
krista@2461
   271
    PEP_comm_type first_reject_comm_type = PEP_ct_key_not_found;
krista@2461
   272
    PEP_STATUS first_reject_status = PEP_KEY_NOT_FOUND;
krista@2461
   273
    
krista@2461
   274
    char* stored_fpr = stored_identity->fpr;
krista@2461
   275
    // Input: stored identity retrieved from database
krista@2461
   276
    // if stored identity contains a default key
krista@2461
   277
    if (!EMPTYSTR(stored_fpr)) {
krista@2461
   278
        status = validate_fpr(session, stored_identity);    
krista@2461
   279
        if (status == PEP_STATUS_OK && !EMPTYSTR(stored_identity->fpr)) {
krista@2461
   280
            *is_identity_default = *is_address_default = true;
krista@2461
   281
            return status;
krista@2461
   282
        }
krista@2461
   283
        else if (status != PEP_KEY_NOT_FOUND) {
krista@2461
   284
            first_reject_status = status;
krista@2461
   285
            first_reject_comm_type = stored_identity->comm_type;
krista@2461
   286
        }
krista@2461
   287
    }
krista@2461
   288
    // if no valid default stored identity key found
krista@2461
   289
    free(stored_identity->fpr);
krista@2461
   290
    stored_identity->fpr = NULL;
krista@2461
   291
    
krista@2461
   292
    char* user_fpr = NULL;
krista@2461
   293
    status = get_user_default_key(session, stored_identity->user_id, &user_fpr);
krista@2461
   294
    
krista@2461
   295
    if (!EMPTYSTR(user_fpr)) {             
krista@2461
   296
        // There exists a default key for user, so validate
krista@2461
   297
        stored_identity->fpr = user_fpr;
krista@2461
   298
        status = validate_fpr(session, stored_identity);
krista@2461
   299
        if (status == PEP_STATUS_OK && stored_identity->fpr) {
krista@2461
   300
            *is_user_default = true;
krista@2461
   301
            *is_address_default = key_matches_address(session, 
krista@2461
   302
                                                      stored_identity->address,
krista@2461
   303
                                                      stored_identity->fpr);
krista@2461
   304
            return status;
krista@2461
   305
        }        
krista@2461
   306
        else if (status != PEP_KEY_NOT_FOUND && first_reject_status != PEP_KEY_NOT_FOUND) {
krista@2461
   307
            first_reject_status = status;
krista@2461
   308
            first_reject_comm_type = stored_identity->comm_type;
krista@2461
   309
        }
krista@2461
   310
    }
krista@2461
   311
    
krista@2461
   312
    status = elect_pubkey(session, stored_identity);
krista@2461
   313
    if (status == PEP_STATUS_OK) {
krista@2461
   314
        if (!EMPTYSTR(stored_identity->fpr))
krista@2461
   315
            validate_fpr(session, stored_identity);
krista@2461
   316
    }    
krista@2461
   317
    else if (status != PEP_KEY_NOT_FOUND && first_reject_status != PEP_KEY_NOT_FOUND) {
krista@2461
   318
        first_reject_status = status;
krista@2461
   319
        first_reject_comm_type = stored_identity->comm_type;
krista@2461
   320
    }
krista@2461
   321
    
krista@2461
   322
    switch (stored_identity->comm_type) {
krista@2461
   323
        case PEP_ct_key_revoked:
krista@2461
   324
        case PEP_ct_key_b0rken:
krista@2461
   325
        case PEP_ct_key_expired:
krista@2461
   326
        case PEP_ct_compromized:
krista@2461
   327
        case PEP_ct_mistrusted:
krista@2461
   328
            // this only happens when it's all there is
krista@2461
   329
            status = first_reject_status;
krista@2461
   330
            free(stored_identity->fpr);
krista@2461
   331
            stored_identity->fpr = NULL;
krista@2461
   332
            stored_identity->comm_type = first_reject_comm_type;
krista@2461
   333
            break;    
krista@2461
   334
        default:
krista@2461
   335
            break;
krista@2461
   336
    }
krista@2461
   337
    return status;
krista@2461
   338
}
krista@2461
   339
krista@2461
   340
static void transfer_ident_lang_and_flags(pEp_identity* new_ident,
krista@2461
   341
                                          pEp_identity* stored_ident) {
krista@2461
   342
    if (new_ident->lang[0] == 0) {
krista@2461
   343
      new_ident->lang[0] = stored_ident->lang[0];
krista@2461
   344
      new_ident->lang[1] = stored_ident->lang[1];
krista@2461
   345
      new_ident->lang[2] = 0;
krista@2461
   346
    }
krista@2461
   347
krista@2461
   348
    new_ident->flags = stored_ident->flags;
krista@2461
   349
    new_ident->me = new_ident->me || stored_ident->me;
krista@2461
   350
}
krista@2461
   351
krista@2461
   352
static PEP_STATUS prepare_updated_identity(PEP_SESSION session,
krista@2461
   353
                                                 pEp_identity* return_id,
krista@2461
   354
                                                 pEp_identity* stored_ident,
krista@2461
   355
                                                 bool store) {
krista@2461
   356
    
krista@2461
   357
    if (!session || !return_id || !stored_ident)
krista@2461
   358
        return PEP_ILLEGAL_VALUE;
krista@2461
   359
    
krista@2461
   360
    PEP_STATUS status;
krista@2461
   361
    
krista@2461
   362
    bool is_identity_default, is_user_default, is_address_default;
krista@2461
   363
    status = get_valid_pubkey(session, stored_ident,
krista@2461
   364
                                &is_identity_default,
krista@2461
   365
                                &is_user_default,
krista@2461
   366
                                &is_address_default);
krista@2461
   367
                                
krista@2461
   368
    if (status == PEP_STATUS_OK && stored_ident->fpr && *(stored_ident->fpr) != '\0') {
krista@2461
   369
    // set identity comm_type from trust db (user_id, FPR)
krista@2461
   370
        status = get_trust(session, stored_ident);
krista@2461
   371
        if (status == PEP_CANNOT_FIND_IDENTITY || stored_ident->comm_type == PEP_ct_unknown) {
krista@2461
   372
            // This is OK - there is no trust DB entry, but we
krista@2461
   373
            // found a key. We won't store this, but we'll
krista@2461
   374
            // use it.
krista@2461
   375
            PEP_comm_type ct = PEP_ct_unknown;
krista@2461
   376
            status = get_key_rating(session, stored_ident->fpr, &ct);
krista@2461
   377
            stored_ident->comm_type = ct;
krista@2461
   378
        }
krista@2461
   379
    }
krista@2471
   380
    else {
krista@2471
   381
        if (stored_ident->comm_type == PEP_ct_unknown)
krista@2471
   382
            stored_ident->comm_type = PEP_ct_key_not_found;
krista@2471
   383
    }
krista@2461
   384
    free(return_id->fpr);
krista@2461
   385
    return_id->fpr = NULL;
krista@2461
   386
    if (status == PEP_STATUS_OK && !EMPTYSTR(stored_ident->fpr))
krista@2461
   387
        return_id->fpr = strdup(stored_ident->fpr);
krista@2461
   388
        
krista@2461
   389
    return_id->comm_type = stored_ident->comm_type;
krista@2461
   390
                
krista@2461
   391
    // We patch the DB with the input username, but if we didn't have
krista@2461
   392
    // one, we pull it out of storage if available.
krista@2461
   393
    // (also, if the input username is "anonymous" and there exists
krista@2461
   394
    //  a DB username, we replace)
krista@2461
   395
    if (!EMPTYSTR(stored_ident->username)) {
krista@2461
   396
        if (!EMPTYSTR(return_id->username) && 
krista@2461
   397
            (strcasecmp(return_id->username, "anonymous") == 0)) {
krista@2461
   398
            free(return_id->username);
krista@2461
   399
            return_id->username = NULL;
krista@2461
   400
        }
krista@2461
   401
        if (EMPTYSTR(return_id->username)) {
krista@2461
   402
            free(return_id->username);
krista@2461
   403
            return_id->username = strdup(stored_ident->username);
krista@2461
   404
        }
krista@2461
   405
    }
krista@2461
   406
    
krista@2461
   407
    return_id->me = stored_ident->me;
krista@2461
   408
    
krista@2461
   409
    // FIXME: Do we ALWAYS do this? We probably should...
krista@2461
   410
    if (EMPTYSTR(return_id->user_id)) {
krista@2461
   411
        free(return_id->user_id);
krista@2461
   412
        return_id->user_id = strdup(stored_ident->user_id);
krista@2461
   413
    }    
krista@2461
   414
    // Call set_identity() to store
krista@2461
   415
    if ((is_identity_default || is_user_default) &&
krista@2461
   416
         is_address_default) {                 
krista@2461
   417
         // if we got an fpr which is default for either user
krista@2461
   418
         // or identity AND is valid for this address, set in DB
krista@2461
   419
         // as default
krista@2461
   420
         status = set_identity(session, return_id);
krista@2461
   421
    }
krista@2461
   422
    else {
krista@2461
   423
        // Store without default fpr/ct, but return the fpr and ct 
krista@2461
   424
        // for current use
krista@2461
   425
        char* save_fpr = return_id->fpr;
krista@2461
   426
        PEP_comm_type save_ct = return_id->comm_type;
krista@2461
   427
        return_id->fpr = NULL;
krista@2461
   428
        return_id->comm_type = PEP_ct_unknown;
krista@2461
   429
        PEP_STATUS save_status = status;
krista@2461
   430
        status = set_identity(session, return_id);
krista@2461
   431
        if (save_status != PEP_STATUS_OK)
krista@2461
   432
            status = save_status;
krista@2461
   433
        return_id->fpr = save_fpr;
krista@2461
   434
        return_id->comm_type = save_ct;
krista@2461
   435
    }
krista@2461
   436
    
krista@2461
   437
    transfer_ident_lang_and_flags(return_id, stored_ident);
krista@2461
   438
    
krista@2461
   439
    return status;
krista@2461
   440
}
krista@2461
   441
edouard@1385
   442
edouard@1385
   443
DYNAMIC_API PEP_STATUS update_identity(
edouard@1385
   444
        PEP_SESSION session, pEp_identity * identity
vb@0
   445
    )
vb@0
   446
{
vb@0
   447
    PEP_STATUS status;
vb@0
   448
vb@0
   449
    assert(session);
vb@0
   450
    assert(identity);
Edouard@439
   451
    assert(!EMPTYSTR(identity->address));
vb@0
   452
Edouard@439
   453
    if (!(session && identity && !EMPTYSTR(identity->address)))
roker@1853
   454
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
   455
krista@2461
   456
    char* default_own_id = NULL;
krista@2461
   457
    status = get_default_own_userid(session, &default_own_id);    
krista@2461
   458
krista@2461
   459
    // Is this me, temporary or not? If so, BAIL.
krista@2461
   460
    if (identity->me || 
krista@2461
   461
       (default_own_id && identity->user_id && (strcmp(default_own_id, identity->user_id) == 0))) 
krista@2461
   462
    {
krista@2461
   463
        return PEP_ILLEGAL_VALUE;
vb@1078
   464
    }
vb@1078
   465
krista@2461
   466
    // We have, at least, an address.
krista@2461
   467
    // Retrieve stored identity information!    
krista@2461
   468
    pEp_identity* stored_ident = NULL;
Edouard@559
   469
krista@2461
   470
    if (!EMPTYSTR(identity->user_id)) {            
krista@2461
   471
        // (we're gonna update the trust/fpr anyway, so we use the no-fpr-from-trust-db variant)
krista@2461
   472
        //      * do get_identity() to retrieve stored identity information
krista@2461
   473
        status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
krista@2461
   474
krista@2461
   475
        // Before we start - if there was no stored identity, we should check to make sure we don't
krista@2461
   476
        // have a stored identity with a temporary user_id that differs from the input user_id. This
krista@2461
   477
        // happens in multithreaded environments sometimes.
krista@2461
   478
        if (!stored_ident) {
krista@2461
   479
            identity_list* id_list = NULL;
krista@2461
   480
            status = get_identities_by_address(session, identity->address, &id_list);
krista@2461
   481
krista@2461
   482
            if (id_list) {
krista@2461
   483
                identity_list* id_curr = id_list;
krista@2461
   484
                while (id_curr) {
krista@2461
   485
                    pEp_identity* this_id = id_curr->ident;
krista@2461
   486
                    if (this_id) {
krista@2461
   487
                        char* this_uid = this_id->user_id;
krista@2461
   488
                        if (this_uid && (strstr(this_uid, "TOFU_") == this_uid)) {
krista@2461
   489
                            // FIXME: should we also be fixing pEp_own_userId in this
krista@2461
   490
                            // function here?
krista@2461
   491
                            
krista@2461
   492
                            // if usernames match, we replace the userid. Or if the temp username
krista@2461
   493
                            // is anonymous.
krista@2461
   494
                            if (EMPTYSTR(this_id->username) ||
krista@2461
   495
                                strcasecmp(this_id->username, "anonymous") == 0 ||
krista@2461
   496
                                (identity->username && 
krista@2461
   497
                                 strcasecmp(identity->username, 
krista@2461
   498
                                            this_id->username) == 0)) {
krista@2461
   499
                                
krista@2461
   500
                                // Ok, we have a temp ID. We have to replace this
krista@2461
   501
                                // with the real ID.
krista@2461
   502
                                status = replace_userid(session, 
krista@2461
   503
                                                        this_uid, 
krista@2461
   504
                                                        identity->user_id);
krista@2461
   505
                                if (status != PEP_STATUS_OK) {
krista@2461
   506
                                    free_identity_list(id_list);
krista@2461
   507
                                    return status;
krista@2461
   508
                                }
krista@2461
   509
                                    
krista@2461
   510
                                free(this_uid);
krista@2461
   511
                                this_uid = NULL;
krista@2461
   512
                                
krista@2461
   513
                                // Reflect the change we just made to the DB
krista@2461
   514
                                this_id->user_id = strdup(identity->user_id);
krista@2461
   515
                                stored_ident = this_id;
krista@2461
   516
                                // FIXME: free list.
krista@2461
   517
                                break;                                
krista@2461
   518
                            }                            
krista@2461
   519
                        } 
krista@2461
   520
                    }
krista@2461
   521
                    id_curr = id_curr->next;
krista@2461
   522
                }
krista@2461
   523
            }
krista@2461
   524
        } 
krista@2461
   525
                
krista@2461
   526
        if (status == PEP_STATUS_OK && stored_ident) { 
krista@2461
   527
            //  * if identity available
krista@2461
   528
            //      * patch it with username
krista@2461
   529
            //          (note: this will happen when 
krista@2461
   530
            //           setting automatically below...)
krista@2461
   531
            //      * elect valid key for identity
krista@2461
   532
            //    * if valid key exists
krista@2461
   533
            //        * set return value's fpr
krista@2461
   534
            status = prepare_updated_identity(session,
krista@2461
   535
                                              identity,
krista@2461
   536
                                              stored_ident, true);
vb@1015
   537
        }
krista@2461
   538
        //  * else (identity unavailable)
krista@2461
   539
        else {
krista@2461
   540
            status = PEP_STATUS_OK;
krista@2461
   541
            
krista@2461
   542
            //  if we only have user_id and address and identity not available
krista@2461
   543
            //      * return error status (identity not found)
krista@2461
   544
            if (EMPTYSTR(identity->username))
krista@2461
   545
                status = PEP_CANNOT_FIND_IDENTITY;
krista@2461
   546
            
krista@2461
   547
            // Otherwise, if we had user_id, address, and username:
krista@2461
   548
            //    * create identity with user_id, address, username
krista@2461
   549
            //      (this is the input id without the fpr + comm type!)
krista@2461
   550
            free(identity->fpr);
krista@2461
   551
            identity->fpr = NULL;
krista@2461
   552
            identity->comm_type = PEP_ct_unknown;
krista@2461
   553
            
krista@2461
   554
            //    * We've already checked and retrieved
krista@2461
   555
            //      any applicable temporary identities above. If we're 
krista@2461
   556
            //      here, none of them fit.
krista@2461
   557
            //    * call set_identity() to store
krista@2461
   558
            if (status == PEP_STATUS_OK) {
krista@2461
   559
                status = set_identity(session, identity);
krista@2461
   560
                if (status == PEP_STATUS_OK) {
krista@2461
   561
                    elect_pubkey(session, identity);
krista@2461
   562
                }
krista@2461
   563
            }
krista@2461
   564
            //  * Return: created identity
krista@2461
   565
        }        
krista@2461
   566
    }
krista@2461
   567
    else if (!EMPTYSTR(identity->username)) {
krista@2461
   568
        /*
krista@2461
   569
         * Temporary identity information with username supplied
krista@2461
   570
            * Input: address, username (no others)
krista@2461
   571
         */
krista@2461
   572
         
krista@2461
   573
        //  * See if there is an own identity that uses this address. If so, we'll
krista@2461
   574
        //    prefer that
krista@2461
   575
        stored_ident = NULL;
krista@2461
   576
        
krista@2461
   577
        if (default_own_id) {
krista@2461
   578
            status = get_identity(session, 
krista@2461
   579
                                  default_own_id, 
krista@2461
   580
                                  identity->address, 
krista@2461
   581
                                  &stored_ident);
krista@2461
   582
        }
krista@2461
   583
        // If there isn't an own identity, search for a non-temp stored ident
krista@2461
   584
        // with this address.                      
krista@2461
   585
        if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
krista@2461
   586
 
krista@2461
   587
            identity_list* id_list = NULL;
krista@2461
   588
            status = get_identities_by_address(session, identity->address, &id_list);
vb@1015
   589
krista@2461
   590
            if (id_list) {
krista@2461
   591
                identity_list* id_curr = id_list;
krista@2461
   592
                while (id_curr) {
krista@2461
   593
                    pEp_identity* this_id = id_curr->ident;
krista@2461
   594
                    if (this_id) {
krista@2461
   595
                        char* this_uid = this_id->user_id;
krista@2461
   596
                        if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
krista@2461
   597
                            // if usernames match, we replace the userid.
krista@2461
   598
                            if (identity->username && 
krista@2461
   599
                                strcasecmp(identity->username, 
krista@2461
   600
                                           this_id->username) == 0) {
krista@2461
   601
                                
krista@2461
   602
                                // Ok, we have a real ID. Copy it!
krista@2461
   603
                                identity->user_id = strdup(this_uid);
krista@2461
   604
                                
krista@2461
   605
                                if (!identity->user_id)
krista@2461
   606
                                    status = PEP_OUT_OF_MEMORY;
krista@2461
   607
                                stored_ident = this_id;
krista@2461
   608
                                
krista@2461
   609
                                break;                                
krista@2461
   610
                            }                            
krista@2461
   611
                        } 
edouard@1522
   612
                    }
krista@2461
   613
                    id_curr = id_curr->next;
edouard@1501
   614
                }
edouard@1501
   615
            }
krista@1220
   616
        }
krista@2461
   617
        
krista@2461
   618
        if (stored_ident) {
krista@2461
   619
            status = prepare_updated_identity(session,
krista@2461
   620
                                              identity,
krista@2461
   621
                                              stored_ident, true);
krista@2461
   622
        }
edouard@1501
   623
        else {
krista@2461
   624
            // create temporary identity, store it, and Return this
krista@2461
   625
            // This means TOFU_ user_id
krista@2461
   626
            identity->user_id = calloc(1, strlen(identity->address) + 6);
krista@2461
   627
            if (!identity->user_id)
krista@2461
   628
                return PEP_OUT_OF_MEMORY;
edouard@1501
   629
krista@2461
   630
            snprintf(identity->user_id, strlen(identity->address) + 6,
krista@2461
   631
                     "TOFU_%s", identity->address);        
krista@1196
   632
            
krista@2461
   633
            free(identity->fpr);
krista@2461
   634
            identity->fpr = NULL;
krista@2461
   635
            identity->comm_type = PEP_ct_unknown;
krista@2461
   636
             
krista@2461
   637
            //    * We've already checked and retrieved
krista@2461
   638
            //      any applicable temporary identities above. If we're 
krista@2461
   639
            //      here, none of them fit.
krista@2461
   640
            //    * call set_identity() to store
krista@2461
   641
            status = set_identity(session, identity);
krista@2461
   642
            if (status == PEP_STATUS_OK) {
krista@2461
   643
                elect_pubkey(session, identity);
Edouard@559
   644
            }
Edouard@499
   645
        }
vb@8
   646
    }
krista@2461
   647
    else {
krista@2461
   648
        /*
krista@2461
   649
         * Temporary identity information without username suplied
krista@2461
   650
            * Input: address (no others)
krista@2461
   651
         */
krista@2461
   652
         
krista@2461
   653
        //  * Again, see if there is an own identity that uses this address. If so, we'll
krista@2461
   654
        //    prefer that
krista@2461
   655
        stored_ident = NULL;
krista@2461
   656
         
krista@2461
   657
        if (default_own_id) {
krista@2461
   658
            status = get_identity(session, 
krista@2461
   659
                                  default_own_id, 
krista@2461
   660
                                  identity->address, 
krista@2461
   661
                                  &stored_ident);
krista@2461
   662
        }
krista@2461
   663
        // If there isn't an own identity, search for a non-temp stored ident
krista@2461
   664
        // with this address.                      
krista@2461
   665
        if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
krista@2461
   666
 
krista@2461
   667
            identity_list* id_list = NULL;
krista@2461
   668
            status = get_identities_by_address(session, identity->address, &id_list);
vb@8
   669
krista@2461
   670
            //    * Search for identity with this address
krista@2461
   671
            if (id_list && !(id_list->next)) { // exactly one            
krista@2461
   672
                //    * If exactly one found
krista@2461
   673
                //      * elect valid key for identity (see below)
krista@2461
   674
                //      * Return this identity
krista@2461
   675
                stored_ident = id_list->ident;
krista@2461
   676
            }
krista@2461
   677
        }
krista@2461
   678
        if (stored_ident)
krista@2461
   679
            status = prepare_updated_identity(session, identity,
krista@2461
   680
                                              stored_ident, false);
krista@2461
   681
        else // too little info
krista@2461
   682
            status = PEP_CANNOT_FIND_IDENTITY; 
krista@2461
   683
    }
krista@2461
   684
    
krista@2461
   685
    // FIXME: This is legacy. I presume it's a notification for the caller...
krista@2461
   686
    // Revisit once I can talk to Volker
krista@2461
   687
    if (identity->comm_type != PEP_ct_compromized &&
krista@2461
   688
        identity->comm_type < PEP_ct_strong_but_unconfirmed)
vb@297
   689
        if (session->examine_identity)
krista@2461
   690
            session->examine_identity(identity, session->examine_management);
vb@297
   691
roker@1853
   692
    return ADD_TO_LOG(status);
vb@0
   693
}
vb@0
   694
Edouard@774
   695
PEP_STATUS elect_ownkey(
krista@1194
   696
        PEP_SESSION session, pEp_identity * identity
Edouard@774
   697
    )
Edouard@774
   698
{
Edouard@774
   699
    PEP_STATUS status;
Edouard@774
   700
    stringlist_t *keylist = NULL;
Edouard@774
   701
Edouard@774
   702
    free(identity->fpr);
Edouard@774
   703
    identity->fpr = NULL;
Edouard@774
   704
krista@1357
   705
    status = find_private_keys(session, identity->address, &keylist);
Edouard@774
   706
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   707
    if (status == PEP_OUT_OF_MEMORY)
Edouard@774
   708
        return PEP_OUT_OF_MEMORY;
Edouard@774
   709
    
Edouard@774
   710
    if (keylist != NULL && keylist->value != NULL)
Edouard@774
   711
    {
Edouard@774
   712
        char *_fpr = NULL;
Edouard@774
   713
        identity->comm_type = PEP_ct_unknown;
Edouard@774
   714
Edouard@774
   715
        stringlist_t *_keylist;
Edouard@774
   716
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@774
   717
            bool is_own = false;
Edouard@774
   718
            
edouard@1752
   719
            status = own_key_is_listed(session, _keylist->value, &is_own);
edouard@1752
   720
            assert(status == PEP_STATUS_OK);
edouard@1752
   721
            if (status != PEP_STATUS_OK) {
edouard@1752
   722
                free_stringlist(keylist);
edouard@1752
   723
                return status;
Edouard@774
   724
            }
Edouard@774
   725
            
edouard@1752
   726
            if (is_own)
Edouard@774
   727
            {
Edouard@774
   728
                PEP_comm_type _comm_type_key;
Edouard@774
   729
                
Edouard@774
   730
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@774
   731
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   732
                if (status == PEP_OUT_OF_MEMORY) {
Edouard@774
   733
                    free_stringlist(keylist);
Edouard@774
   734
                    return PEP_OUT_OF_MEMORY;
Edouard@774
   735
                }
Edouard@774
   736
                
Edouard@774
   737
                if (_comm_type_key != PEP_ct_compromized &&
Edouard@774
   738
                    _comm_type_key != PEP_ct_unknown)
Edouard@774
   739
                {
Edouard@774
   740
                    if (identity->comm_type == PEP_ct_unknown ||
Edouard@774
   741
                        _comm_type_key > identity->comm_type)
Edouard@774
   742
                    {
Edouard@774
   743
                        identity->comm_type = _comm_type_key;
Edouard@774
   744
                        _fpr = _keylist->value;
Edouard@774
   745
                    }
Edouard@774
   746
                }
Edouard@774
   747
            }
Edouard@774
   748
        }
Edouard@774
   749
        
Edouard@774
   750
        if (_fpr)
Edouard@774
   751
        {
Edouard@774
   752
            identity->fpr = strdup(_fpr);
Edouard@774
   753
            assert(identity->fpr);
Edouard@774
   754
            if (identity->fpr == NULL)
Edouard@774
   755
            {
Edouard@774
   756
                free_stringlist(keylist);
Edouard@774
   757
                return PEP_OUT_OF_MEMORY;
Edouard@774
   758
            }
Edouard@774
   759
        }
Edouard@774
   760
        free_stringlist(keylist);
Edouard@774
   761
    }
Edouard@774
   762
    return PEP_STATUS_OK;
Edouard@774
   763
}
Edouard@774
   764
krista@1357
   765
PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
krista@1357
   766
                                bool* is_usable) {
krista@1357
   767
    
krista@1357
   768
    bool dont_use_fpr = true;
krista@1357
   769
    
krista@1357
   770
    PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
krista@1371
   771
    if (status == PEP_STATUS_OK && !dont_use_fpr) {
krista@1357
   772
        // Make sure there is a *private* key associated with this fpr
krista@1357
   773
        bool has_private = false;
krista@1357
   774
        status = contains_priv_key(session, fpr, &has_private);
krista@1371
   775
krista@1371
   776
        if (status == PEP_STATUS_OK)
krista@1371
   777
            dont_use_fpr = !has_private;
krista@1357
   778
    }
krista@1357
   779
    
krista@1357
   780
    *is_usable = !dont_use_fpr;
krista@1357
   781
    
roker@1853
   782
    return ADD_TO_LOG(status);
krista@1357
   783
}
krista@1357
   784
edouard@1406
   785
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
vb@0
   786
{
krista@2461
   787
vb@0
   788
    PEP_STATUS status;
vb@0
   789
vb@0
   790
    assert(session);
vb@0
   791
    assert(identity);
vb@1044
   792
    assert(!EMPTYSTR(identity->address));
krista@2461
   793
    assert(!EMPTYSTR(identity->user_id));
vb@1043
   794
krista@2461
   795
    if (!session || !identity || EMPTYSTR(identity->address) ||
krista@2461
   796
        EMPTYSTR(identity->user_id))
roker@1853
   797
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
   798
krista@2461
   799
    pEp_identity *stored_identity = NULL;
krista@2461
   800
    char* revoked_fpr = NULL; 
krista@2461
   801
        
krista@2461
   802
    char* default_own_id = NULL;
krista@2461
   803
    status = get_default_own_userid(session, &default_own_id);
krista@2461
   804
krista@2461
   805
    // Deal with non-default user_ids.
krista@2461
   806
    if (default_own_id && strcmp(default_own_id, identity->user_id) != 0) {
krista@2461
   807
        
krista@2461
   808
        status = set_userid_alias(session, default_own_id, identity->user_id);
krista@2461
   809
        // Do we want this to be fatal? For now, we'll do it...
krista@2461
   810
        if (status != PEP_STATUS_OK)
krista@2461
   811
            goto pep_free;
krista@2461
   812
            
krista@2461
   813
        free(identity->user_id);
krista@2461
   814
        identity->user_id = strdup(default_own_id);
krista@2461
   815
        if (identity->user_id == NULL) {
krista@2461
   816
            status = PEP_OUT_OF_MEMORY;
krista@2461
   817
            goto pep_free;
krista@2461
   818
        }
krista@2461
   819
    }
krista@2461
   820
krista@2461
   821
    // NOTE: IF WE DON'T YET HAVE AN OWN_ID, WE IGNORE REFERENCES TO THIS ADDRESS IN THE
krista@2461
   822
    // DB (WHICH MAY HAVE BEEN SET BEFORE MYSELF WAS CALLED BY RECEIVING AN EMAIL FROM
krista@2461
   823
    // THIS ADDRESS), AS IT IS NOT AN OWN_IDENTITY AND HAS NO INFORMATION WE NEED OR WHAT TO
krista@2461
   824
    // SET FOR MYSELF
krista@2461
   825
    
krista@2461
   826
    // Ok, so now, set up the own_identity:
vb@0
   827
    identity->comm_type = PEP_ct_pEp;
krista@2461
   828
    identity->me = true;
edouard@1406
   829
    if(ignore_flags)
edouard@1406
   830
        identity->flags = 0;
Edouard@658
   831
    
krista@2461
   832
    // Let's see if we have an identity record in the DB for 
krista@2461
   833
    // this user_id + address
vb@215
   834
    DEBUG_LOG("myself", "debug", identity->address);
vb@1044
   835
 
Edouard@560
   836
    status = get_identity(session,
Edouard@560
   837
                          identity->address,
Edouard@560
   838
                          identity->user_id,
Edouard@560
   839
                          &stored_identity);
krista@2461
   840
vb@0
   841
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   842
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   843
        return PEP_OUT_OF_MEMORY;
krista@1357
   844
krista@2461
   845
    // Set usernames - priority is input username > stored name > "Anonymous"
krista@2461
   846
    // If there's an input username, we always patch the username with that
krista@2461
   847
    // input.
krista@2461
   848
    if (EMPTYSTR(identity->username)) {
krista@2461
   849
        bool stored_uname = (stored_identity && stored_identity->username);
krista@2461
   850
        char* uname = (stored_uname ? stored_identity->username : "Anonymous");
krista@2461
   851
        free(identity->username);
krista@2461
   852
        identity->username = strdup(uname);
krista@2461
   853
        if (identity->username == NULL)
krista@2461
   854
            return PEP_OUT_OF_MEMORY;
krista@2461
   855
    }
krista@2461
   856
krista@2461
   857
    bool valid_key_found = false;
krista@2461
   858
    
krista@2461
   859
    // Now deal with keys.
krista@2461
   860
    // Different from update_identity(), the input fpr here
krista@2461
   861
    // MATTERS. 
krista@2461
   862
    // If the input fpr is invalid, we return, giving the reason why.
krista@2461
   863
    if (!EMPTYSTR(identity->fpr)) {
krista@2461
   864
        status = validate_fpr(session, identity);
krista@2461
   865
    
krista@2461
   866
        if (status != PEP_STATUS_OK || 
krista@2461
   867
            identity->comm_type < PEP_ct_strong_but_unconfirmed) {
krista@2461
   868
            if (identity->comm_type != PEP_ct_key_expired)
krista@2461
   869
                goto pep_free;
krista@2461
   870
            // Otherwise, it was expired and key renewal failed
krista@2461
   871
            // and we take the stored one or do keygen. 
krista@2461
   872
        } 
krista@2461
   873
        else
krista@2461
   874
            valid_key_found = true;
krista@2461
   875
    }    
krista@2461
   876
    
krista@2461
   877
    // Ok, if there wasn't a valid input fpr, check stored identity
krista@2461
   878
    if (!valid_key_found && stored_identity && 
krista@2461
   879
        (EMPTYSTR(identity->fpr) || strcmp(stored_identity->fpr, identity->fpr) != 0)) {
krista@1359
   880
        
krista@2461
   881
        // Fall back / retrieve
krista@2461
   882
        status = validate_fpr(session, stored_identity);
krista@2461
   883
        if (status == PEP_STATUS_OK && 
krista@2461
   884
            stored_identity->comm_type >= PEP_ct_strong_but_unconfirmed) {
krista@2461
   885
          
krista@2461
   886
            free(identity->fpr);
krista@2461
   887
            identity->fpr = strdup(stored_identity->fpr);
krista@2461
   888
            valid_key_found = true;            
Edouard@560
   889
        }
krista@2461
   890
        else {
krista@2461
   891
            bool revoked = false;
krista@2461
   892
            if (!EMPTYSTR(stored_identity->fpr)) {
krista@2461
   893
                status = key_revoked(session, stored_identity->fpr, &revoked);
krista@2461
   894
                if (revoked)
krista@2461
   895
                    revoked_fpr = strdup(stored_identity->fpr);
krista@2461
   896
            }        
krista@1357
   897
        }
Edouard@560
   898
    }
krista@1357
   899
    
krista@2461
   900
    // Nothing left to do but generate a key
krista@2461
   901
    if (!valid_key_found) {
krista@2461
   902
        if (!do_keygen)
krista@2461
   903
            status = PEP_GET_KEY_FAILED;
krista@2461
   904
        else {
krista@2461
   905
            DEBUG_LOG("Generating key pair", "debug", identity->address);
vb@934
   906
krista@2461
   907
            free(identity->fpr);
krista@2461
   908
            identity->fpr = NULL;
krista@2461
   909
            status = generate_keypair(session, identity);
krista@2461
   910
            assert(status != PEP_OUT_OF_MEMORY);
krista@1371
   911
krista@2461
   912
            if (status != PEP_STATUS_OK) {
krista@2461
   913
                char buf[11];
krista@2461
   914
                snprintf(buf, 11, "%d", status); // uh, this is kludgey. FIXME
krista@2461
   915
                DEBUG_LOG("Generating key pair failed", "debug", buf);
krista@2461
   916
            }        
krista@2461
   917
            else {
krista@2461
   918
                valid_key_found = true;
krista@2461
   919
                if (revoked_fpr) {
krista@2461
   920
                    status = set_revoked(session, revoked_fpr,
krista@2461
   921
                                         stored_identity->fpr, time(NULL));
krista@2461
   922
                }
krista@2461
   923
            }
krista@1357
   924
        }
Edouard@560
   925
    }
Edouard@695
   926
krista@2461
   927
    if (valid_key_found) {
krista@2461
   928
        identity->comm_type = PEP_ct_pEp;
krista@2461
   929
        status = PEP_STATUS_OK;
krista@2461
   930
    }
krista@2461
   931
    else {
krista@2461
   932
        free(identity->fpr);
krista@2461
   933
        identity->fpr = NULL;
krista@2461
   934
        identity->comm_type = PEP_ct_unknown;
krista@2461
   935
    }
krista@2461
   936
    
krista@2461
   937
    status = set_identity(session, identity);
Edouard@775
   938
krista@2461
   939
pep_free:    
krista@2461
   940
    free(default_own_id);
krista@2461
   941
    free(revoked_fpr);                     
krista@2461
   942
    free_identity(stored_identity);
krista@2461
   943
    return ADD_TO_LOG(status);
krista@2461
   944
}
krista@2461
   945
krista@2461
   946
DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
krista@2461
   947
                                                 identity_list* my_idents) {
krista@2461
   948
    PEP_STATUS status = PEP_STATUS_OK;
krista@2461
   949
    if (!session)
krista@2461
   950
        return PEP_ILLEGAL_VALUE;
krista@2461
   951
        
krista@2461
   952
    if (!my_idents)
krista@2461
   953
        return PEP_STATUS_OK;
krista@2461
   954
            
krista@2461
   955
    identity_list* ident_curr = my_idents;
krista@2461
   956
    while (ident_curr) {
krista@2461
   957
        pEp_identity* ident = ident_curr->ident;
krista@2461
   958
        if (!ident || !ident->address) {
krista@2461
   959
            status = PEP_ILLEGAL_VALUE;
krista@2461
   960
            goto pep_error;
edouard@1385
   961
        }
edouard@1385
   962
krista@2461
   963
        status = _myself(session, ident, false, false);
Edouard@560
   964
        
krista@2461
   965
        ident_curr = ident_curr->next;
vb@0
   966
    }
krista@1353
   967
    
krista@2461
   968
pep_error:
krista@2461
   969
    return status;
vb@0
   970
}
vb@0
   971
edouard@1385
   972
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
edouard@1385
   973
{
roker@1853
   974
    return ADD_TO_LOG(_myself(session, identity, true, false));
edouard@1385
   975
}
edouard@1385
   976
vb@296
   977
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   978
        PEP_SESSION session, 
vb@292
   979
        examine_identity_t examine_identity,
vb@292
   980
        void *management
vb@292
   981
    )
vb@292
   982
{
vb@292
   983
    assert(session);
vb@292
   984
    if (!session)
vb@292
   985
        return PEP_ILLEGAL_VALUE;
vb@292
   986
vb@292
   987
    session->examine_management = management;
vb@292
   988
    session->examine_identity = examine_identity;
vb@292
   989
vb@292
   990
    return PEP_STATUS_OK;
vb@292
   991
}
vb@292
   992
vb@0
   993
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   994
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   995
        void *management
vb@0
   996
    )
vb@0
   997
{
vb@0
   998
    PEP_SESSION session;
vb@0
   999
    pEp_identity *identity;
Edouard@499
  1000
    PEP_STATUS status;
vb@0
  1001
Edouard@499
  1002
    assert(retrieve_next_identity);
Edouard@499
  1003
    assert(management);
Edouard@499
  1004
Edouard@499
  1005
    if (!retrieve_next_identity || !management)
Edouard@499
  1006
        return PEP_ILLEGAL_VALUE;
Edouard@499
  1007
Edouard@499
  1008
    status = init(&session);
vb@0
  1009
    assert(status == PEP_STATUS_OK);
vb@0
  1010
    if (status != PEP_STATUS_OK)
vb@0
  1011
        return status;
vb@0
  1012
vb@0
  1013
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
  1014
Edouard@499
  1015
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
  1016
    {
vb@0
  1017
        assert(identity->address);
Edouard@499
  1018
        if(identity->address)
Edouard@499
  1019
        {
Edouard@499
  1020
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
  1021
krista@2461
  1022
            if (identity->me) {
Edouard@499
  1023
                status = myself(session, identity);
Edouard@499
  1024
            } else {
Edouard@499
  1025
                status = recv_key(session, identity->address);
Edouard@499
  1026
            }
Edouard@499
  1027
vb@0
  1028
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
  1029
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
  1030
                return PEP_OUT_OF_MEMORY;
vb@0
  1031
        }
vb@0
  1032
        free_identity(identity);
vb@0
  1033
    }
vb@0
  1034
vb@0
  1035
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
  1036
vb@0
  1037
    release(session);
vb@0
  1038
    return PEP_STATUS_OK;
vb@0
  1039
}
vb@0
  1040
krista@1213
  1041
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
  1042
        PEP_SESSION session,
vb@357
  1043
        pEp_identity *ident
vb@357
  1044
    )
vb@215
  1045
{
vb@218
  1046
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
  1047
vb@215
  1048
    assert(session);
vb@357
  1049
    assert(ident);
Edouard@439
  1050
    assert(!EMPTYSTR(ident->fpr));
vb@215
  1051
vb@357
  1052
    if (!(session && ident && ident->fpr))
vb@215
  1053
        return PEP_ILLEGAL_VALUE;
vb@215
  1054
krista@2461
  1055
    if (ident->me)
Edouard@697
  1056
    {
vb@357
  1057
        revoke_key(session, ident->fpr, NULL);
Edouard@697
  1058
        myself(session, ident);
Edouard@697
  1059
    }
Edouard@697
  1060
    else
Edouard@697
  1061
    {
krista@2129
  1062
        // for undo
krista@2129
  1063
        if (session->cached_mistrusted)
krista@2129
  1064
            free(session->cached_mistrusted);
krista@2129
  1065
        session->cached_mistrusted = identity_dup(ident);
krista@2461
  1066
        
krista@2461
  1067
        // set mistrust for this user_id/keypair (even if there's not an
krista@2461
  1068
        // identity set yet, this is important, as we need to record the mistrust
krista@2461
  1069
        // action)
krista@2461
  1070
        status = set_trust(session, ident->user_id, ident->fpr, PEP_ct_mistrusted);
krista@2461
  1071
        if (status == PEP_STATUS_OK)
krista@2461
  1072
            // cascade that mistrust for anyone using this key
krista@2461
  1073
            status = mark_as_compromized(session, ident->fpr);
krista@2461
  1074
        if (status == PEP_STATUS_OK)
krista@2461
  1075
            status = remove_fpr_as_default(session, ident->fpr);
krista@2471
  1076
        if (status == PEP_STATUS_OK)
krista@2471
  1077
            status = add_mistrusted_key(session, ident->fpr);
Edouard@697
  1078
    }
vb@218
  1079
vb@218
  1080
    return status;
vb@215
  1081
}
vb@215
  1082
krista@2129
  1083
DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session) {
krista@2129
  1084
    assert(session);
krista@2129
  1085
    
krista@2129
  1086
    if (!session)
krista@2129
  1087
        return PEP_ILLEGAL_VALUE;
krista@2129
  1088
    
krista@2129
  1089
    PEP_STATUS status = PEP_STATUS_OK;
krista@2129
  1090
        
krista@2129
  1091
    pEp_identity* cached_ident = session->cached_mistrusted;
krista@2129
  1092
    
krista@2129
  1093
    if (!cached_ident)
krista@2129
  1094
        status = PEP_CANNOT_FIND_IDENTITY;
krista@2129
  1095
    else {
krista@2471
  1096
        status = delete_mistrusted_key(session, cached_ident->fpr);
krista@2471
  1097
        if (status == PEP_STATUS_OK) {
krista@2471
  1098
            status = set_identity(session, cached_ident);            
krista@2471
  1099
            free_identity(session->cached_mistrusted);
krista@2471
  1100
        }
krista@2129
  1101
    }
krista@2129
  1102
    
krista@2129
  1103
    session->cached_mistrusted = NULL;
krista@2471
  1104
    
krista@2129
  1105
    return status;
krista@2129
  1106
}
krista@2129
  1107
Edouard@410
  1108
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
  1109
        PEP_SESSION session,
Edouard@410
  1110
        pEp_identity *ident
Edouard@410
  1111
    )
Edouard@410
  1112
{
Edouard@410
  1113
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
  1114
Edouard@410
  1115
    assert(session);
Edouard@410
  1116
    assert(ident);
Edouard@439
  1117
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
  1118
    assert(!EMPTYSTR(ident->address));
Edouard@439
  1119
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
  1120
krista@2461
  1121
    if (!(session && ident && ident->fpr && ident->fpr[0] != '\0' && ident->address &&
vb@420
  1122
            ident->user_id))
Edouard@410
  1123
        return PEP_ILLEGAL_VALUE;
Edouard@410
  1124
krista@2461
  1125
    // we do not change the input struct at ALL.
krista@2461
  1126
    pEp_identity* input_copy = identity_dup(ident);
krista@2461
  1127
    
krista@2461
  1128
    pEp_identity* tmp_ident = NULL;
krista@2461
  1129
    
krista@2461
  1130
    status = get_trust(session, input_copy);
krista@2461
  1131
    
vb@420
  1132
    if (status != PEP_STATUS_OK)
krista@2461
  1133
        goto pep_free;
krista@2461
  1134
        
krista@2461
  1135
    PEP_comm_type new_trust = PEP_ct_unknown;
Edouard@410
  1136
krista@2461
  1137
    if (input_copy->comm_type != PEP_ct_mistrusted)
krista@2461
  1138
        new_trust = input_copy->comm_type & ~PEP_ct_confirmed;
vb@421
  1139
krista@2461
  1140
    status = set_trust(session, ident->user_id, ident->fpr, new_trust);
krista@2461
  1141
    
vb@421
  1142
    if (status != PEP_STATUS_OK)
krista@2461
  1143
        goto pep_free;
vb@421
  1144
krista@2471
  1145
    bool mistrusted_key = false;
krista@2461
  1146
        
krista@2471
  1147
    status = is_mistrusted_key(session, ident->fpr, &mistrusted_key);
krista@2471
  1148
krista@2471
  1149
    if (status != PEP_STATUS_OK)
krista@2471
  1150
        goto pep_free;
krista@2471
  1151
    
krista@2471
  1152
    if (mistrusted_key)
krista@2471
  1153
        status = delete_mistrusted_key(session, ident->fpr);
krista@2471
  1154
krista@2471
  1155
    if (status != PEP_STATUS_OK)
krista@2471
  1156
        goto pep_free;
krista@2471
  1157
    
krista@2461
  1158
    input_copy->comm_type = new_trust;
krista@2461
  1159
        
krista@2461
  1160
    tmp_ident = new_identity(ident->address, NULL, ident->user_id, NULL);
krista@2461
  1161
krista@2461
  1162
    if (!tmp_ident)
krista@2461
  1163
        return PEP_OUT_OF_MEMORY;
krista@2461
  1164
    
krista@2461
  1165
    status = update_identity(session, tmp_ident);
krista@2461
  1166
    
krista@2461
  1167
    if (status != PEP_STATUS_OK)
krista@2461
  1168
        goto pep_free;
krista@2461
  1169
    
krista@2461
  1170
    // remove as default if necessary
krista@2461
  1171
    if (strcmp(tmp_ident->fpr, ident->fpr) == 0) {
krista@2461
  1172
        free(tmp_ident->fpr);
krista@2461
  1173
        tmp_ident->fpr = NULL;
krista@2461
  1174
        tmp_ident->comm_type = PEP_ct_unknown;
krista@2461
  1175
        status = set_identity(session, tmp_ident);
krista@2461
  1176
        if (status != PEP_STATUS_OK)
krista@2461
  1177
            goto pep_free;
krista@2461
  1178
    }
krista@2461
  1179
    
krista@2461
  1180
    char* user_default = NULL;
krista@2461
  1181
    status = get_main_user_fpr(session, tmp_ident->user_id, &user_default);
krista@2461
  1182
    
krista@2461
  1183
    if (!EMPTYSTR(user_default)) {
krista@2461
  1184
        if (strcmp(user_default, ident->fpr) == 0)
krista@2461
  1185
            status = refresh_userid_default_key(session, ident->user_id);
krista@2461
  1186
        if (status != PEP_STATUS_OK)
krista@2461
  1187
            goto pep_free;    
krista@2461
  1188
    }
krista@2461
  1189
            
krista@2461
  1190
pep_free:
krista@2461
  1191
    free_identity(tmp_ident);
krista@2461
  1192
    free_identity(input_copy);
Edouard@410
  1193
    return status;
Edouard@410
  1194
}
Edouard@410
  1195
vb@354
  1196
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
  1197
        PEP_SESSION session,
vb@354
  1198
        pEp_identity *ident
vb@354
  1199
    )
vb@354
  1200
{
vb@354
  1201
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
  1202
vb@354
  1203
    assert(session);
vb@354
  1204
    assert(ident);
Edouard@439
  1205
    assert(!EMPTYSTR(ident->address));
Edouard@439
  1206
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
  1207
    assert(!EMPTYSTR(ident->fpr));
vb@354
  1208
Edouard@439
  1209
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
edouard@1945
  1210
            EMPTYSTR(ident->fpr))
vb@354
  1211
        return PEP_ILLEGAL_VALUE;
vb@354
  1212
krista@2461
  1213
    //bool ident_has_trusted_default = false;
krista@2461
  1214
    char* ident_default_fpr = NULL;
vb@354
  1215
krista@2461
  1216
    // Before we do anything, be sure the input fpr is even eligible to be trusted
krista@2461
  1217
    PEP_comm_type input_default_ct = PEP_ct_unknown;
krista@2461
  1218
    status = get_key_rating(session, ident->fpr, &input_default_ct);
krista@2461
  1219
    if (input_default_ct < PEP_ct_strong_but_unconfirmed)
krista@2461
  1220
        return PEP_KEY_UNSUITABLE;
krista@2461
  1221
krista@2461
  1222
    // Save the input fpr
krista@2461
  1223
    char* cached_fpr = strdup(ident->fpr);
krista@2461
  1224
    ident->fpr = NULL;
krista@2461
  1225
krista@2461
  1226
    bool me = is_me(session, ident);
krista@2461
  1227
krista@2461
  1228
    if (me)
krista@2461
  1229
        return myself(session, ident); // FIXME: Not the right thing if we 
krista@2461
  1230
                                       // don't always replace user default!!!
krista@2461
  1231
krista@2461
  1232
    // First, set up a temp trusted identity for the input fpr without a comm type;
krista@2461
  1233
    pEp_identity* tmp_id = new_identity(ident->address, cached_fpr, ident->user_id, NULL);
krista@2461
  1234
    status = validate_fpr(session, tmp_id);
krista@2461
  1235
        
krista@2461
  1236
    if (status == PEP_STATUS_OK) {
krista@2461
  1237
        // Validate fpr gets trust DB or, when that fails, key comm type. we checked
krista@2461
  1238
        // above that the key was ok. (not revoked or expired), but we want the max.
krista@2461
  1239
        tmp_id->comm_type = _MAX(tmp_id->comm_type, input_default_ct) | PEP_ct_confirmed;
krista@2461
  1240
                                       
krista@2461
  1241
        // Get the default identity without setting the fpr
krista@2461
  1242
        status = update_identity(session, ident);
krista@2461
  1243
        ident_default_fpr = strdup(ident->fpr);
krista@2461
  1244
krista@2461
  1245
        if (status == PEP_STATUS_OK) {
krista@2461
  1246
            bool trusted_default = false;
krista@2461
  1247
krista@2461
  1248
            // If there's no default, or the default is different from the input...
krista@2461
  1249
            if (EMPTYSTR(ident_default_fpr) || strcmp(cached_fpr, ident_default_fpr) != 0) {
krista@2461
  1250
                
krista@2461
  1251
                // If the default fpr (if there is one) is trusted and key is strong enough,
krista@2461
  1252
                // don't replace, we just set the trusted bit on this key for this user_id...
krista@2461
  1253
                // (If there's no default fpr, this won't be true anyway.)
krista@2461
  1254
                if (ident->comm_type >= PEP_ct_strong_but_unconfirmed && 
krista@2461
  1255
                    (ident->comm_type & PEP_ct_confirmed)) {                        
krista@2461
  1256
krista@2461
  1257
                    trusted_default = true;
krista@2461
  1258
                                    
krista@2461
  1259
                    status = set_trust(session, tmp_id->user_id, cached_fpr, tmp_id->comm_type);
krista@2461
  1260
                    input_default_ct = tmp_id->comm_type;                    
krista@2461
  1261
                }
krista@2461
  1262
                else {
krista@2461
  1263
                    free(ident->fpr);
krista@2461
  1264
                    ident->fpr = strdup(cached_fpr);
krista@2461
  1265
                    ident->comm_type = tmp_id->comm_type;
krista@2461
  1266
                    status = set_identity(session, ident); // replace identity default            
krista@2461
  1267
                }
krista@2461
  1268
            }
krista@2461
  1269
            else { // we're setting this on the default fpr
krista@2461
  1270
                ident->comm_type = tmp_id->comm_type;
krista@2461
  1271
                status = set_identity(session, ident);
krista@2461
  1272
                trusted_default = true;
krista@2461
  1273
            }
krista@2461
  1274
            if (status == PEP_STATUS_OK && !trusted_default) {
krista@2461
  1275
                // Ok, there wasn't a trusted default, so we replaced. Thus, we also
krista@2461
  1276
                // make sure there's a trusted default on the user_id. If there
krista@2461
  1277
                // is not, we make this the default.
krista@2461
  1278
                char* user_default = NULL;
krista@2461
  1279
                status = get_main_user_fpr(session, ident->user_id, &user_default);
krista@2461
  1280
            
krista@2461
  1281
                if (status == PEP_STATUS_OK && user_default) {
krista@2461
  1282
                    pEp_identity* tmp_user_ident = new_identity(ident->address, 
krista@2461
  1283
                                                                user_default, 
krista@2461
  1284
                                                                ident->user_id, 
krista@2461
  1285
                                                                NULL);
krista@2461
  1286
                    if (!tmp_user_ident)
krista@2461
  1287
                        status = PEP_OUT_OF_MEMORY;
krista@2461
  1288
                    else {
krista@2461
  1289
                        status = validate_fpr(session, tmp_user_ident);
krista@2461
  1290
                        
krista@2461
  1291
                        if (status != PEP_STATUS_OK ||
krista@2461
  1292
                            tmp_user_ident->comm_type < PEP_ct_strong_but_unconfirmed ||
krista@2461
  1293
                            !(tmp_user_ident->comm_type & PEP_ct_confirmed)) 
krista@2461
  1294
                        {
krista@2461
  1295
                            char* trusted_fpr = (trusted_default ? ident_default_fpr : cached_fpr);
krista@2461
  1296
                            status = replace_main_user_fpr(session, ident->user_id, trusted_fpr);
krista@2461
  1297
                        } 
krista@2461
  1298
                    }
krista@2461
  1299
                }
krista@2461
  1300
            }
krista@2461
  1301
        }
krista@2461
  1302
        free(ident_default_fpr);
krista@2461
  1303
        free(cached_fpr);
krista@2461
  1304
        free_identity(tmp_id);
krista@2461
  1305
    }    
vb@354
  1306
vb@354
  1307
    return status;
vb@354
  1308
}
vb@354
  1309
Edouard@584
  1310
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
  1311
        PEP_SESSION session,
vb@955
  1312
        const char *fpr,
vb@955
  1313
        bool *listed
vb@955
  1314
    )
Edouard@584
  1315
{
Edouard@584
  1316
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
  1317
    int count;
Edouard@584
  1318
    
Edouard@584
  1319
    assert(session && fpr && fpr[0] && listed);
Edouard@584
  1320
    
Edouard@584
  1321
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
  1322
        return PEP_ILLEGAL_VALUE;
Edouard@584
  1323
    
Edouard@584
  1324
    *listed = false;
Edouard@584
  1325
    
Edouard@584
  1326
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
  1327
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
  1328
    
Edouard@584
  1329
    int result;
Edouard@584
  1330
    
Edouard@584
  1331
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
  1332
    switch (result) {
Edouard@584
  1333
        case SQLITE_ROW:
Edouard@584
  1334
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
  1335
            *listed = count > 0;
krista@2461
  1336
            status = PEP_STATUS_OK;
Edouard@584
  1337
            break;
Edouard@584
  1338
            
Edouard@584
  1339
        default:
Edouard@584
  1340
            status = PEP_UNKNOWN_ERROR;
Edouard@584
  1341
    }
Edouard@584
  1342
    
Edouard@584
  1343
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
  1344
    return status;
Edouard@584
  1345
}
Edouard@584
  1346
edouard@1412
  1347
PEP_STATUS _own_identities_retrieve(
vb@955
  1348
        PEP_SESSION session,
edouard@1412
  1349
        identity_list **own_identities,
edouard@1412
  1350
        identity_flags_t excluded_flags
vb@955
  1351
      )
Edouard@584
  1352
{
Edouard@584
  1353
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
  1354
    
vb@955
  1355
    assert(session && own_identities);
vb@955
  1356
    if (!(session && own_identities))
Edouard@584
  1357
        return PEP_ILLEGAL_VALUE;
Edouard@584
  1358
    
vb@955
  1359
    *own_identities = NULL;
vb@955
  1360
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
  1361
    if (_own_identities == NULL)
Edouard@584
  1362
        goto enomem;
Edouard@584
  1363
    
vb@955
  1364
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
  1365
    
Edouard@584
  1366
    int result;
vb@955
  1367
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
  1368
    const char *address = NULL;
Edouard@584
  1369
    const char *fpr = NULL;
vb@955
  1370
    const char *username = NULL;
vb@955
  1371
    const char *user_id = NULL;
vb@955
  1372
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
  1373
    const char *lang = NULL;
vb@955
  1374
    unsigned int flags = 0;
Edouard@584
  1375
    
vb@955
  1376
    identity_list *_bl = _own_identities;
Edouard@584
  1377
    do {
edouard@1412
  1378
        sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
vb@955
  1379
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
  1380
        switch (result) {
Edouard@584
  1381
            case SQLITE_ROW:
vb@955
  1382
                address = (const char *)
vb@955
  1383
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
  1384
                fpr = (const char *)
vb@955
  1385
                    sqlite3_column_text(session->own_identities_retrieve, 1);
krista@2461
  1386
                user_id = (const char *)
krista@2461
  1387
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
  1388
                username = (const char *)
krista@2461
  1389
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
  1390
                comm_type = PEP_ct_pEp;
vb@1071
  1391
                lang = (const char *)
krista@2461
  1392
                    sqlite3_column_text(session->own_identities_retrieve, 4);
vb@1071
  1393
                flags = (unsigned int)
krista@2461
  1394
                    sqlite3_column_int(session->own_identities_retrieve, 5);
vb@955
  1395
vb@1078
  1396
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
  1397
                if (!ident)
Edouard@584
  1398
                    goto enomem;
vb@955
  1399
                ident->comm_type = comm_type;
vb@955
  1400
                if (lang && lang[0]) {
vb@955
  1401
                    ident->lang[0] = lang[0];
vb@955
  1402
                    ident->lang[1] = lang[1];
vb@1078
  1403
                    ident->lang[2] = 0;
vb@955
  1404
                }
krista@2461
  1405
                ident->me = true;
vb@955
  1406
                ident->flags = flags;
vb@955
  1407
vb@955
  1408
                _bl = identity_list_add(_bl, ident);
vb@1080
  1409
                if (_bl == NULL) {
vb@1080
  1410
                    free_identity(ident);
vb@983
  1411
                    goto enomem;
vb@1080
  1412
                }
Edouard@584
  1413
                
Edouard@584
  1414
                break;
Edouard@584
  1415
                
Edouard@584
  1416
            case SQLITE_DONE:
Edouard@584
  1417
                break;
Edouard@584
  1418
                
Edouard@584
  1419
            default:
Edouard@584
  1420
                status = PEP_UNKNOWN_ERROR;
Edouard@584
  1421
                result = SQLITE_DONE;
Edouard@584
  1422
        }
Edouard@584
  1423
    } while (result != SQLITE_DONE);
Edouard@584
  1424
    
vb@955
  1425
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
  1426
    if (status == PEP_STATUS_OK)
vb@955
  1427
        *own_identities = _own_identities;
Edouard@584
  1428
    else
vb@955
  1429
        free_identity_list(_own_identities);
Edouard@584
  1430
    
Edouard@584
  1431
    goto the_end;
Edouard@584
  1432
    
Edouard@584
  1433
enomem:
vb@955
  1434
    free_identity_list(_own_identities);
Edouard@584
  1435
    status = PEP_OUT_OF_MEMORY;
Edouard@584
  1436
    
Edouard@584
  1437
the_end:
Edouard@584
  1438
    return status;
Edouard@584
  1439
}
vb@955
  1440
edouard@1412
  1441
DYNAMIC_API PEP_STATUS own_identities_retrieve(
edouard@1364
  1442
        PEP_SESSION session,
edouard@1412
  1443
        identity_list **own_identities
edouard@1412
  1444
      )
edouard@1412
  1445
{
edouard@1412
  1446
    return _own_identities_retrieve(session, own_identities, 0);
edouard@1412
  1447
}
edouard@1412
  1448
edouard@1412
  1449
PEP_STATUS _own_keys_retrieve(
edouard@1412
  1450
        PEP_SESSION session,
edouard@1412
  1451
        stringlist_t **keylist,
edouard@1412
  1452
        identity_flags_t excluded_flags
edouard@1364
  1453
      )
edouard@1364
  1454
{
edouard@1364
  1455
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1364
  1456
    
edouard@1364
  1457
    assert(session && keylist);
edouard@1364
  1458
    if (!(session && keylist))
edouard@1364
  1459
        return PEP_ILLEGAL_VALUE;
edouard@1364
  1460
    
edouard@1364
  1461
    *keylist = NULL;
edouard@1364
  1462
    stringlist_t *_keylist = NULL;
edouard@1364
  1463
    
edouard@1394
  1464
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1465
    
edouard@1364
  1466
    int result;
edouard@1364
  1467
    char *fpr = NULL;
edouard@1364
  1468
    
edouard@1364
  1469
    stringlist_t *_bl = _keylist;
edouard@1364
  1470
    do {
edouard@1412
  1471
        sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
edouard@1394
  1472
        result = sqlite3_step(session->own_keys_retrieve);
edouard@1364
  1473
        switch (result) {
edouard@1364
  1474
            case SQLITE_ROW:
edouard@1394
  1475
                fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
edouard@1364
  1476
                if(fpr == NULL)
edouard@1364
  1477
                    goto enomem;
edouard@1364
  1478
edouard@1364
  1479
                _bl = stringlist_add(_bl, fpr);
edouard@1364
  1480
                if (_bl == NULL) {
edouard@1364
  1481
                    free(fpr);
edouard@1364
  1482
                    goto enomem;
edouard@1364
  1483
                }
edouard@1364
  1484
                if (_keylist == NULL)
edouard@1364
  1485
                    _keylist = _bl;
edouard@1364
  1486
                
edouard@1364
  1487
                break;
edouard@1364
  1488
                
edouard@1364
  1489
            case SQLITE_DONE:
edouard@1364
  1490
                break;
edouard@1364
  1491
                
edouard@1364
  1492
            default:
edouard@1364
  1493
                status = PEP_UNKNOWN_ERROR;
edouard@1364
  1494
                result = SQLITE_DONE;
edouard@1364
  1495
        }
edouard@1364
  1496
    } while (result != SQLITE_DONE);
edouard@1364
  1497
    
edouard@1394
  1498
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1499
    if (status == PEP_STATUS_OK)
edouard@1364
  1500
        *keylist = _keylist;
edouard@1364
  1501
    else
edouard@1364
  1502
        free_stringlist(_keylist);
edouard@1364
  1503
    
edouard@1364
  1504
    goto the_end;
edouard@1364
  1505
    
edouard@1364
  1506
enomem:
edouard@1364
  1507
    free_stringlist(_keylist);
edouard@1364
  1508
    status = PEP_OUT_OF_MEMORY;
edouard@1364
  1509
    
edouard@1364
  1510
the_end:
edouard@1364
  1511
    return status;
edouard@1364
  1512
}
edouard@1364
  1513
edouard@1412
  1514
DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
edouard@1412
  1515
{
edouard@1412
  1516
    return _own_keys_retrieve(session, keylist, 0);
edouard@1412
  1517
}
edouard@1412
  1518
edouard@1760
  1519
// FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
edouard@1394
  1520
DYNAMIC_API PEP_STATUS set_own_key(
edouard@1394
  1521
       PEP_SESSION session,
edouard@1394
  1522
       const char *address,
edouard@1394
  1523
       const char *fpr
edouard@1394
  1524
    )
edouard@1394
  1525
{
edouard@1394
  1526
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1394
  1527
    
edouard@1394
  1528
    assert(session &&
edouard@1760
  1529
           address &&
edouard@1394
  1530
           fpr && fpr[0]
edouard@1394
  1531
          );
edouard@1394
  1532
    
edouard@1394
  1533
    if (!(session &&
edouard@1760
  1534
          address &&
edouard@1394
  1535
          fpr && fpr[0]
edouard@1394
  1536
         ))
edouard@1394
  1537
        return PEP_ILLEGAL_VALUE;
krista@2461
  1538
                        
krista@2461
  1539
    // First see if we have it in own identities already, AND we retrieve
krista@2461
  1540
    // our own user_id
krista@2461
  1541
    char* my_user_id = NULL;
krista@2461
  1542
    status = get_default_own_userid(session, &my_user_id);
krista@2461
  1543
    if (status != PEP_STATUS_OK)
krista@2461
  1544
        return status;
krista@2461
  1545
        
krista@2461
  1546
    if (!my_user_id) {
krista@2461
  1547
        // We have no own user_id. So we cannot set it for an identity.
krista@2461
  1548
        return PEP_CANNOT_FIND_IDENTITY;
edouard@1394
  1549
    }
edouard@1394
  1550
    
krista@2461
  1551
    pEp_identity* my_id = NULL;
krista@2461
  1552
    
krista@2461
  1553
    status = get_identity(session, my_user_id, address, &my_id);
krista@2461
  1554
krista@2461
  1555
    if (status == PEP_STATUS_OK && my_id) {
krista@2461
  1556
        if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
krista@2461
  1557
            // We're done. It was already here.
krista@2461
  1558
            goto pep_free;
krista@2461
  1559
        }           
krista@2461
  1560
    }
krista@2461
  1561
                
krista@2461
  1562
    // If there's an id w/ user_id + address
krista@2461
  1563
    if (my_id) {
krista@2461
  1564
        free(my_id->fpr);
krista@2461
  1565
        my_id->fpr = my_user_id;
krista@2461
  1566
        my_id->comm_type = PEP_ct_pEp;
krista@2461
  1567
        my_id->me = true;
krista@2461
  1568
    }
krista@2461
  1569
    else { // Else, we need a new identity
krista@2461
  1570
        my_id = new_identity(address, fpr, my_user_id, NULL); 
krista@2461
  1571
        if (status != PEP_STATUS_OK)
krista@2461
  1572
            goto pep_free; 
krista@2461
  1573
        my_id->me = true;
krista@2461
  1574
        my_id->comm_type = PEP_ct_pEp;
krista@2461
  1575
    }
krista@2461
  1576
        
krista@2461
  1577
    status = set_identity(session, my_id);
krista@2461
  1578
    
krista@2461
  1579
pep_free:
krista@2461
  1580
    free(my_id);
krista@2461
  1581
    free(my_user_id);
edouard@1394
  1582
    return status;
edouard@1394
  1583
}
krista@1357
  1584
krista@1357
  1585
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
  1586
                             bool *has_private) {
krista@1357
  1587
krista@1357
  1588
    assert(session);
krista@1357
  1589
    assert(fpr);
krista@1357
  1590
    assert(has_private);
krista@1357
  1591
    
krista@1357
  1592
    if (!(session && fpr && has_private))
krista@1357
  1593
        return PEP_ILLEGAL_VALUE;
krista@1357
  1594
krista@1357
  1595
    return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
edouard@1385
  1596
}
krista@2471
  1597
krista@2471
  1598
PEP_STATUS add_mistrusted_key(PEP_SESSION session, const char* fpr)
krista@2471
  1599
{
krista@2471
  1600
    int result;
krista@2471
  1601
krista@2471
  1602
    assert(!EMPTYSTR(fpr));
krista@2471
  1603
    
krista@2471
  1604
    if (!(session) || EMPTYSTR(fpr))
krista@2471
  1605
        return PEP_ILLEGAL_VALUE;
krista@2471
  1606
krista@2471
  1607
    sqlite3_reset(session->add_mistrusted_key);
krista@2471
  1608
    sqlite3_bind_text(session->add_mistrusted_key, 1, fpr, -1,
krista@2471
  1609
            SQLITE_STATIC);
krista@2471
  1610
krista@2471
  1611
    result = sqlite3_step(session->add_mistrusted_key);
krista@2471
  1612
    sqlite3_reset(session->add_mistrusted_key);
krista@2471
  1613
krista@2471
  1614
    if (result != SQLITE_DONE)
krista@2471
  1615
        return PEP_CANNOT_SET_PGP_KEYPAIR; // FIXME: Better status?
krista@2471
  1616
krista@2471
  1617
    return PEP_STATUS_OK;
krista@2471
  1618
}
krista@2471
  1619
krista@2471
  1620
PEP_STATUS delete_mistrusted_key(PEP_SESSION session, const char* fpr)
krista@2471
  1621
{
krista@2471
  1622
    int result;
krista@2471
  1623
krista@2471
  1624
    assert(!EMPTYSTR(fpr));
krista@2471
  1625
    
krista@2471
  1626
    if (!(session) || EMPTYSTR(fpr))
krista@2471
  1627
        return PEP_ILLEGAL_VALUE;
krista@2471
  1628
krista@2471
  1629
    sqlite3_reset(session->delete_mistrusted_key);
krista@2471
  1630
    sqlite3_bind_text(session->delete_mistrusted_key, 1, fpr, -1,
krista@2471
  1631
            SQLITE_STATIC);
krista@2471
  1632
krista@2471
  1633
    result = sqlite3_step(session->delete_mistrusted_key);
krista@2471
  1634
    sqlite3_reset(session->delete_mistrusted_key);
krista@2471
  1635
krista@2471
  1636
    if (result != SQLITE_DONE)
krista@2471
  1637
        return PEP_UNKNOWN_ERROR; // FIXME: Better status?
krista@2471
  1638
krista@2471
  1639
    return PEP_STATUS_OK;
krista@2471
  1640
}
krista@2471
  1641
krista@2471
  1642
PEP_STATUS is_mistrusted_key(PEP_SESSION session, const char* fpr,
krista@2471
  1643
                             bool* mistrusted)
krista@2471
  1644
{
krista@2471
  1645
    PEP_STATUS status = PEP_STATUS_OK;
krista@2471
  1646
krista@2471
  1647
    assert(session);
krista@2471
  1648
    assert(!EMPTYSTR(fpr));
krista@2471
  1649
krista@2471
  1650
    if (!(session && fpr))
krista@2471
  1651
        return PEP_ILLEGAL_VALUE;
krista@2471
  1652
krista@2471
  1653
    *mistrusted = false;
krista@2471
  1654
krista@2471
  1655
    sqlite3_reset(session->is_mistrusted_key);
krista@2471
  1656
    sqlite3_bind_text(session->is_mistrusted_key, 1, fpr, -1, SQLITE_STATIC);
krista@2471
  1657
krista@2471
  1658
    int result;
krista@2471
  1659
krista@2471
  1660
    result = sqlite3_step(session->is_mistrusted_key);
krista@2471
  1661
    switch (result) {
krista@2471
  1662
    case SQLITE_ROW:
krista@2471
  1663
        *mistrusted = sqlite3_column_int(session->is_mistrusted_key, 0);
krista@2471
  1664
        status = PEP_STATUS_OK;
krista@2471
  1665
        break;
krista@2471
  1666
krista@2471
  1667
    default:
krista@2471
  1668
        status = PEP_UNKNOWN_ERROR;
krista@2471
  1669
    }
krista@2471
  1670
krista@2471
  1671
    sqlite3_reset(session->is_mistrusted_key);
krista@2471
  1672
    return status;
krista@2471
  1673
}