src/keymanagement.c
author Krista Bennett <krista@pep-project.org>
Sun, 11 Feb 2018 17:02:19 +0100
changeset 2494 56c577c84996
parent 2493 a4fd5af7fde1
child 2496 389abc0e9b54
permissions -rw-r--r--
Fixed ENGINE-364 and some test bugs. Users now much be CERTAIN to call update_identity if they need the comm_type afte trust_personal_key (this is in accord with the API)
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
            }
krista@2461
    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@2493
   397
            (strcasecmp(return_id->username, return_id->address) == 0)) {
krista@2461
   398
            free(return_id->username);
krista@2461
   399
        }
krista@2461
   400
        if (EMPTYSTR(return_id->username)) {
krista@2461
   401
            free(return_id->username);
krista@2461
   402
            return_id->username = strdup(stored_ident->username);
krista@2461
   403
        }
krista@2461
   404
    }
krista@2493
   405
    else {
krista@2493
   406
        if (EMPTYSTR(return_id->username))
krista@2493
   407
            return_id->username = strdup(return_id->address);
krista@2493
   408
    }
krista@2461
   409
    
krista@2461
   410
    return_id->me = stored_ident->me;
krista@2461
   411
    
krista@2461
   412
    // FIXME: Do we ALWAYS do this? We probably should...
krista@2461
   413
    if (EMPTYSTR(return_id->user_id)) {
krista@2461
   414
        free(return_id->user_id);
krista@2461
   415
        return_id->user_id = strdup(stored_ident->user_id);
krista@2461
   416
    }    
krista@2461
   417
    // Call set_identity() to store
krista@2461
   418
    if ((is_identity_default || is_user_default) &&
krista@2461
   419
         is_address_default) {                 
krista@2461
   420
         // if we got an fpr which is default for either user
krista@2461
   421
         // or identity AND is valid for this address, set in DB
krista@2461
   422
         // as default
krista@2461
   423
         status = set_identity(session, return_id);
krista@2461
   424
    }
krista@2461
   425
    else {
krista@2461
   426
        // Store without default fpr/ct, but return the fpr and ct 
krista@2461
   427
        // for current use
krista@2461
   428
        char* save_fpr = return_id->fpr;
krista@2461
   429
        PEP_comm_type save_ct = return_id->comm_type;
krista@2461
   430
        return_id->fpr = NULL;
krista@2461
   431
        return_id->comm_type = PEP_ct_unknown;
krista@2461
   432
        PEP_STATUS save_status = status;
krista@2461
   433
        status = set_identity(session, return_id);
krista@2461
   434
        if (save_status != PEP_STATUS_OK)
krista@2461
   435
            status = save_status;
krista@2461
   436
        return_id->fpr = save_fpr;
krista@2461
   437
        return_id->comm_type = save_ct;
krista@2461
   438
    }
krista@2461
   439
    
krista@2461
   440
    transfer_ident_lang_and_flags(return_id, stored_ident);
krista@2461
   441
    
krista@2477
   442
    if (return_id->comm_type == PEP_ct_unknown)
krista@2477
   443
        return_id->comm_type = PEP_ct_key_not_found;
krista@2477
   444
    
krista@2461
   445
    return status;
krista@2461
   446
}
krista@2461
   447
edouard@1385
   448
edouard@1385
   449
DYNAMIC_API PEP_STATUS update_identity(
edouard@1385
   450
        PEP_SESSION session, pEp_identity * identity
vb@0
   451
    )
vb@0
   452
{
vb@0
   453
    PEP_STATUS status;
vb@0
   454
vb@0
   455
    assert(session);
vb@0
   456
    assert(identity);
Edouard@439
   457
    assert(!EMPTYSTR(identity->address));
vb@0
   458
Edouard@439
   459
    if (!(session && identity && !EMPTYSTR(identity->address)))
roker@1853
   460
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
   461
krista@2461
   462
    char* default_own_id = NULL;
krista@2461
   463
    status = get_default_own_userid(session, &default_own_id);    
krista@2461
   464
krista@2461
   465
    // Is this me, temporary or not? If so, BAIL.
krista@2461
   466
    if (identity->me || 
krista@2461
   467
       (default_own_id && identity->user_id && (strcmp(default_own_id, identity->user_id) == 0))) 
krista@2461
   468
    {
krista@2461
   469
        return PEP_ILLEGAL_VALUE;
vb@1078
   470
    }
vb@1078
   471
krista@2461
   472
    // We have, at least, an address.
krista@2461
   473
    // Retrieve stored identity information!    
krista@2461
   474
    pEp_identity* stored_ident = NULL;
krista@2461
   475
krista@2461
   476
    if (!EMPTYSTR(identity->user_id)) {            
krista@2461
   477
        // (we're gonna update the trust/fpr anyway, so we use the no-fpr-from-trust-db variant)
krista@2461
   478
        //      * do get_identity() to retrieve stored identity information
krista@2461
   479
        status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
krista@2461
   480
krista@2461
   481
        // Before we start - if there was no stored identity, we should check to make sure we don't
krista@2461
   482
        // have a stored identity with a temporary user_id that differs from the input user_id. This
krista@2461
   483
        // happens in multithreaded environments sometimes.
krista@2461
   484
        if (!stored_ident) {
krista@2461
   485
            identity_list* id_list = NULL;
krista@2461
   486
            status = get_identities_by_address(session, identity->address, &id_list);
Edouard@559
   487
krista@2461
   488
            if (id_list) {
krista@2461
   489
                identity_list* id_curr = id_list;
krista@2461
   490
                while (id_curr) {
krista@2461
   491
                    pEp_identity* this_id = id_curr->ident;
krista@2461
   492
                    if (this_id) {
krista@2461
   493
                        char* this_uid = this_id->user_id;
krista@2461
   494
                        if (this_uid && (strstr(this_uid, "TOFU_") == this_uid)) {
krista@2461
   495
                            // FIXME: should we also be fixing pEp_own_userId in this
krista@2461
   496
                            // function here?
krista@2461
   497
                            
krista@2461
   498
                            // if usernames match, we replace the userid. Or if the temp username
krista@2461
   499
                            // is anonymous.
krista@2493
   500
                            // FIXME: do we need to create an address match function which
krista@2493
   501
                            // matches the whole dot-and-case rigamarole from 
krista@2461
   502
                            if (EMPTYSTR(this_id->username) ||
krista@2493
   503
                                strcasecmp(this_id->username, this_id->address) == 0 ||
krista@2461
   504
                                (identity->username && 
krista@2461
   505
                                 strcasecmp(identity->username, 
krista@2461
   506
                                            this_id->username) == 0)) {
krista@2461
   507
                                
krista@2461
   508
                                // Ok, we have a temp ID. We have to replace this
krista@2461
   509
                                // with the real ID.
krista@2461
   510
                                status = replace_userid(session, 
krista@2461
   511
                                                        this_uid, 
krista@2461
   512
                                                        identity->user_id);
krista@2461
   513
                                if (status != PEP_STATUS_OK) {
krista@2461
   514
                                    free_identity_list(id_list);
krista@2461
   515
                                    return status;
krista@2461
   516
                                }
krista@2461
   517
                                    
krista@2461
   518
                                free(this_uid);
krista@2461
   519
                                this_uid = NULL;
krista@2461
   520
                                
krista@2461
   521
                                // Reflect the change we just made to the DB
krista@2461
   522
                                this_id->user_id = strdup(identity->user_id);
krista@2461
   523
                                stored_ident = this_id;
krista@2461
   524
                                // FIXME: free list.
krista@2461
   525
                                break;                                
krista@2461
   526
                            }                            
krista@2461
   527
                        } 
krista@2461
   528
                    }
krista@2461
   529
                    id_curr = id_curr->next;
krista@2461
   530
                }
krista@2461
   531
            }
krista@2461
   532
        } 
krista@2461
   533
                
krista@2461
   534
        if (status == PEP_STATUS_OK && stored_ident) { 
krista@2461
   535
            //  * if identity available
krista@2461
   536
            //      * patch it with username
krista@2461
   537
            //          (note: this will happen when 
krista@2461
   538
            //           setting automatically below...)
krista@2461
   539
            //      * elect valid key for identity
krista@2461
   540
            //    * if valid key exists
krista@2461
   541
            //        * set return value's fpr
krista@2461
   542
            status = prepare_updated_identity(session,
krista@2461
   543
                                              identity,
krista@2461
   544
                                              stored_ident, true);
Edouard@559
   545
        }
krista@2461
   546
        //  * else (identity unavailable)
krista@2461
   547
        else {
krista@2461
   548
            status = PEP_STATUS_OK;
krista@2493
   549
krista@2493
   550
            // FIXME: We may need to roll this back.
krista@2493
   551
            // FIXME: change docs if we don't
krista@2461
   552
            //  if we only have user_id and address and identity not available
krista@2461
   553
            //      * return error status (identity not found)
krista@2493
   554
            if (EMPTYSTR(identity->username)) {
krista@2493
   555
                free(identity->username);
krista@2493
   556
                identity->username = strdup(identity->address);
krista@2493
   557
            }
krista@1188
   558
            
krista@2461
   559
            // Otherwise, if we had user_id, address, and username:
krista@2461
   560
            //    * create identity with user_id, address, username
krista@2461
   561
            //      (this is the input id without the fpr + comm type!)
krista@2493
   562
krista@2493
   563
            if (status == PEP_STATUS_OK) {
krista@2493
   564
                elect_pubkey(session, identity);
krista@2493
   565
            }
krista@2493
   566
                        
krista@2461
   567
            //    * We've already checked and retrieved
krista@2461
   568
            //      any applicable temporary identities above. If we're 
krista@2461
   569
            //      here, none of them fit.
krista@2461
   570
            //    * call set_identity() to store
krista@2493
   571
            if (status == PEP_STATUS_OK)
krista@2493
   572
                // FIXME: Do we set if we had to copy in the address?
krista@2461
   573
                status = set_identity(session, identity);
krista@2461
   574
            //  * Return: created identity
krista@2461
   575
        }        
krista@2461
   576
    }
krista@2461
   577
    else if (!EMPTYSTR(identity->username)) {
krista@2461
   578
        /*
krista@2461
   579
         * Temporary identity information with username supplied
krista@2461
   580
            * Input: address, username (no others)
krista@2461
   581
         */
krista@2461
   582
         
krista@2461
   583
        //  * See if there is an own identity that uses this address. If so, we'll
krista@2461
   584
        //    prefer that
krista@2461
   585
        stored_ident = NULL;
krista@2461
   586
        
krista@2461
   587
        if (default_own_id) {
krista@2461
   588
            status = get_identity(session, 
krista@2461
   589
                                  default_own_id, 
krista@2461
   590
                                  identity->address, 
krista@2461
   591
                                  &stored_ident);
krista@2461
   592
        }
krista@2461
   593
        // If there isn't an own identity, search for a non-temp stored ident
krista@2461
   594
        // with this address.                      
krista@2461
   595
        if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
krista@2461
   596
 
krista@2461
   597
            identity_list* id_list = NULL;
krista@2461
   598
            status = get_identities_by_address(session, identity->address, &id_list);
edouard@1522
   599
krista@2461
   600
            if (id_list) {
krista@2461
   601
                identity_list* id_curr = id_list;
krista@2461
   602
                while (id_curr) {
krista@2461
   603
                    pEp_identity* this_id = id_curr->ident;
krista@2461
   604
                    if (this_id) {
krista@2461
   605
                        char* this_uid = this_id->user_id;
krista@2461
   606
                        if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
krista@2461
   607
                            // if usernames match, we replace the userid.
krista@2461
   608
                            if (identity->username && 
krista@2461
   609
                                strcasecmp(identity->username, 
krista@2461
   610
                                           this_id->username) == 0) {
krista@2461
   611
                                
krista@2461
   612
                                // Ok, we have a real ID. Copy it!
krista@2461
   613
                                identity->user_id = strdup(this_uid);
krista@2461
   614
                                
krista@2461
   615
                                if (!identity->user_id)
krista@2461
   616
                                    status = PEP_OUT_OF_MEMORY;
krista@2461
   617
                                stored_ident = this_id;
krista@2461
   618
                                
krista@2461
   619
                                break;                                
krista@2461
   620
                            }                            
krista@2461
   621
                        } 
edouard@1522
   622
                    }
krista@2461
   623
                    id_curr = id_curr->next;
edouard@1501
   624
                }
edouard@1501
   625
            }
krista@1220
   626
        }
krista@1188
   627
        
krista@2461
   628
        if (stored_ident) {
krista@2461
   629
            status = prepare_updated_identity(session,
krista@2461
   630
                                              identity,
krista@2461
   631
                                              stored_ident, true);
krista@1243
   632
        }
krista@2461
   633
        else {
krista@2461
   634
            // create temporary identity, store it, and Return this
krista@2461
   635
            // This means TOFU_ user_id
krista@2461
   636
            identity->user_id = calloc(1, strlen(identity->address) + 6);
krista@2461
   637
            if (!identity->user_id)
krista@2461
   638
                return PEP_OUT_OF_MEMORY;
vb@934
   639
krista@2461
   640
            snprintf(identity->user_id, strlen(identity->address) + 6,
krista@2461
   641
                     "TOFU_%s", identity->address);        
edouard@1502
   642
            
krista@2461
   643
            free(identity->fpr);
krista@2461
   644
            identity->fpr = NULL;
krista@2461
   645
            identity->comm_type = PEP_ct_unknown;
krista@2461
   646
             
krista@2461
   647
            //    * We've already checked and retrieved
krista@2461
   648
            //      any applicable temporary identities above. If we're 
krista@2461
   649
            //      here, none of them fit.
krista@2461
   650
            //    * call set_identity() to store
krista@2461
   651
            status = set_identity(session, identity);
krista@2461
   652
            if (status == PEP_STATUS_OK) {
krista@2461
   653
                elect_pubkey(session, identity);
Edouard@559
   654
            }
Edouard@499
   655
        }
vb@8
   656
    }
krista@2461
   657
    else {
krista@2461
   658
        /*
krista@2493
   659
        * Input: address (no others)
krista@2461
   660
         * Temporary identity information without username suplied
krista@2461
   661
         */
krista@2461
   662
         
krista@2461
   663
        //  * Again, see if there is an own identity that uses this address. If so, we'll
krista@2461
   664
        //    prefer that
krista@2461
   665
        stored_ident = NULL;
krista@2461
   666
         
krista@2461
   667
        if (default_own_id) {
krista@2461
   668
            status = get_identity(session, 
krista@2461
   669
                                  default_own_id, 
krista@2461
   670
                                  identity->address, 
krista@2461
   671
                                  &stored_ident);
krista@2461
   672
        }
krista@2461
   673
        // If there isn't an own identity, search for a non-temp stored ident
krista@2461
   674
        // with this address.                      
krista@2461
   675
        if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
krista@2461
   676
 
krista@2461
   677
            identity_list* id_list = NULL;
krista@2461
   678
            status = get_identities_by_address(session, identity->address, &id_list);
vb@8
   679
krista@2461
   680
            //    * Search for identity with this address
krista@2461
   681
            if (id_list && !(id_list->next)) { // exactly one            
krista@2461
   682
                //    * If exactly one found
krista@2461
   683
                //      * elect valid key for identity (see below)
krista@2461
   684
                //      * Return this identity
krista@2461
   685
                stored_ident = id_list->ident;
krista@2461
   686
            }
krista@2461
   687
        }
krista@2461
   688
        if (stored_ident)
krista@2461
   689
            status = prepare_updated_identity(session, identity,
krista@2461
   690
                                              stored_ident, false);
krista@2461
   691
        else // too little info
krista@2461
   692
            status = PEP_CANNOT_FIND_IDENTITY; 
krista@2461
   693
    }
krista@1220
   694
    
krista@2461
   695
    // FIXME: This is legacy. I presume it's a notification for the caller...
krista@2461
   696
    // Revisit once I can talk to Volker
krista@2461
   697
    if (identity->comm_type != PEP_ct_compromized &&
krista@2461
   698
        identity->comm_type < PEP_ct_strong_but_unconfirmed)
krista@2461
   699
        if (session->examine_identity)
krista@2461
   700
            session->examine_identity(identity, session->examine_management);
vb@297
   701
roker@1853
   702
    return ADD_TO_LOG(status);
vb@0
   703
}
vb@0
   704
Edouard@774
   705
PEP_STATUS elect_ownkey(
krista@1194
   706
        PEP_SESSION session, pEp_identity * identity
Edouard@774
   707
    )
Edouard@774
   708
{
Edouard@774
   709
    PEP_STATUS status;
Edouard@774
   710
    stringlist_t *keylist = NULL;
Edouard@774
   711
Edouard@774
   712
    free(identity->fpr);
Edouard@774
   713
    identity->fpr = NULL;
Edouard@774
   714
krista@1357
   715
    status = find_private_keys(session, identity->address, &keylist);
Edouard@774
   716
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   717
    if (status == PEP_OUT_OF_MEMORY)
Edouard@774
   718
        return PEP_OUT_OF_MEMORY;
Edouard@774
   719
    
Edouard@774
   720
    if (keylist != NULL && keylist->value != NULL)
Edouard@774
   721
    {
Edouard@774
   722
        char *_fpr = NULL;
Edouard@774
   723
        identity->comm_type = PEP_ct_unknown;
Edouard@774
   724
Edouard@774
   725
        stringlist_t *_keylist;
Edouard@774
   726
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@774
   727
            bool is_own = false;
Edouard@774
   728
            
edouard@1752
   729
            status = own_key_is_listed(session, _keylist->value, &is_own);
edouard@1752
   730
            assert(status == PEP_STATUS_OK);
edouard@1752
   731
            if (status != PEP_STATUS_OK) {
edouard@1752
   732
                free_stringlist(keylist);
edouard@1752
   733
                return status;
Edouard@774
   734
            }
Edouard@774
   735
            
edouard@1752
   736
            if (is_own)
Edouard@774
   737
            {
Edouard@774
   738
                PEP_comm_type _comm_type_key;
Edouard@774
   739
                
Edouard@774
   740
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@774
   741
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   742
                if (status == PEP_OUT_OF_MEMORY) {
Edouard@774
   743
                    free_stringlist(keylist);
Edouard@774
   744
                    return PEP_OUT_OF_MEMORY;
Edouard@774
   745
                }
Edouard@774
   746
                
Edouard@774
   747
                if (_comm_type_key != PEP_ct_compromized &&
Edouard@774
   748
                    _comm_type_key != PEP_ct_unknown)
Edouard@774
   749
                {
Edouard@774
   750
                    if (identity->comm_type == PEP_ct_unknown ||
Edouard@774
   751
                        _comm_type_key > identity->comm_type)
Edouard@774
   752
                    {
Edouard@774
   753
                        identity->comm_type = _comm_type_key;
Edouard@774
   754
                        _fpr = _keylist->value;
Edouard@774
   755
                    }
Edouard@774
   756
                }
Edouard@774
   757
            }
Edouard@774
   758
        }
Edouard@774
   759
        
Edouard@774
   760
        if (_fpr)
Edouard@774
   761
        {
Edouard@774
   762
            identity->fpr = strdup(_fpr);
Edouard@774
   763
            assert(identity->fpr);
Edouard@774
   764
            if (identity->fpr == NULL)
Edouard@774
   765
            {
Edouard@774
   766
                free_stringlist(keylist);
Edouard@774
   767
                return PEP_OUT_OF_MEMORY;
Edouard@774
   768
            }
Edouard@774
   769
        }
Edouard@774
   770
        free_stringlist(keylist);
Edouard@774
   771
    }
Edouard@774
   772
    return PEP_STATUS_OK;
Edouard@774
   773
}
Edouard@774
   774
krista@1357
   775
PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
krista@1357
   776
                                bool* is_usable) {
krista@1357
   777
    
krista@1357
   778
    bool dont_use_fpr = true;
krista@1357
   779
    
krista@1357
   780
    PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
krista@1371
   781
    if (status == PEP_STATUS_OK && !dont_use_fpr) {
krista@1357
   782
        // Make sure there is a *private* key associated with this fpr
krista@1357
   783
        bool has_private = false;
krista@1357
   784
        status = contains_priv_key(session, fpr, &has_private);
krista@1371
   785
krista@1371
   786
        if (status == PEP_STATUS_OK)
krista@1371
   787
            dont_use_fpr = !has_private;
krista@1357
   788
    }
krista@1357
   789
    
krista@1357
   790
    *is_usable = !dont_use_fpr;
krista@1357
   791
    
roker@1853
   792
    return ADD_TO_LOG(status);
krista@1357
   793
}
krista@1357
   794
edouard@1406
   795
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
vb@0
   796
{
krista@2461
   797
vb@0
   798
    PEP_STATUS status;
vb@0
   799
vb@0
   800
    assert(session);
vb@0
   801
    assert(identity);
vb@1044
   802
    assert(!EMPTYSTR(identity->address));
krista@2461
   803
    assert(!EMPTYSTR(identity->user_id));
vb@0
   804
krista@2461
   805
    if (!session || !identity || EMPTYSTR(identity->address) ||
krista@2461
   806
        EMPTYSTR(identity->user_id))
roker@1853
   807
        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
vb@191
   808
krista@2461
   809
    pEp_identity *stored_identity = NULL;
krista@2461
   810
    char* revoked_fpr = NULL; 
krista@2461
   811
        
krista@2461
   812
    char* default_own_id = NULL;
krista@2461
   813
    status = get_default_own_userid(session, &default_own_id);
krista@2461
   814
krista@2461
   815
    // Deal with non-default user_ids.
krista@2461
   816
    if (default_own_id && strcmp(default_own_id, identity->user_id) != 0) {
krista@2461
   817
        
krista@2461
   818
        status = set_userid_alias(session, default_own_id, identity->user_id);
krista@2461
   819
        // Do we want this to be fatal? For now, we'll do it...
krista@2461
   820
        if (status != PEP_STATUS_OK)
krista@2461
   821
            goto pep_free;
krista@2461
   822
            
krista@2461
   823
        free(identity->user_id);
krista@2461
   824
        identity->user_id = strdup(default_own_id);
krista@2461
   825
        if (identity->user_id == NULL) {
krista@2461
   826
            status = PEP_OUT_OF_MEMORY;
krista@2461
   827
            goto pep_free;
krista@2461
   828
        }
krista@2461
   829
    }
krista@2461
   830
krista@2461
   831
    // NOTE: IF WE DON'T YET HAVE AN OWN_ID, WE IGNORE REFERENCES TO THIS ADDRESS IN THE
krista@2461
   832
    // DB (WHICH MAY HAVE BEEN SET BEFORE MYSELF WAS CALLED BY RECEIVING AN EMAIL FROM
krista@2461
   833
    // THIS ADDRESS), AS IT IS NOT AN OWN_IDENTITY AND HAS NO INFORMATION WE NEED OR WHAT TO
krista@2461
   834
    // SET FOR MYSELF
krista@2461
   835
    
krista@2461
   836
    // Ok, so now, set up the own_identity:
vb@0
   837
    identity->comm_type = PEP_ct_pEp;
krista@2461
   838
    identity->me = true;
edouard@1406
   839
    if(ignore_flags)
edouard@1406
   840
        identity->flags = 0;
Edouard@658
   841
    
krista@2461
   842
    // Let's see if we have an identity record in the DB for 
krista@2461
   843
    // this user_id + address
vb@215
   844
    DEBUG_LOG("myself", "debug", identity->address);
vb@1044
   845
 
Edouard@560
   846
    status = get_identity(session,
Edouard@560
   847
                          identity->address,
Edouard@560
   848
                          identity->user_id,
Edouard@560
   849
                          &stored_identity);
krista@2461
   850
vb@0
   851
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   852
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   853
        return PEP_OUT_OF_MEMORY;
krista@1357
   854
krista@2493
   855
    // Set usernames - priority is input username > stored name > address
krista@2461
   856
    // If there's an input username, we always patch the username with that
krista@2461
   857
    // input.
krista@2461
   858
    if (EMPTYSTR(identity->username)) {
krista@2494
   859
        bool stored_uname = (stored_identity && !EMPTYSTR(stored_identity->username));
krista@2493
   860
        char* uname = (stored_uname ? stored_identity->username : identity->address);
krista@2461
   861
        free(identity->username);
krista@2461
   862
        identity->username = strdup(uname);
krista@2461
   863
        if (identity->username == NULL)
krista@2461
   864
            return PEP_OUT_OF_MEMORY;
krista@2461
   865
    }
krista@2461
   866
krista@2461
   867
    bool valid_key_found = false;
krista@2461
   868
    
krista@2461
   869
    // Now deal with keys.
krista@2461
   870
    // Different from update_identity(), the input fpr here
krista@2461
   871
    // MATTERS. 
krista@2461
   872
    // If the input fpr is invalid, we return, giving the reason why.
krista@2461
   873
    if (!EMPTYSTR(identity->fpr)) {
krista@2461
   874
        status = validate_fpr(session, identity);
krista@2461
   875
    
krista@2461
   876
        if (status != PEP_STATUS_OK || 
krista@2461
   877
            identity->comm_type < PEP_ct_strong_but_unconfirmed) {
krista@2461
   878
            if (identity->comm_type != PEP_ct_key_expired)
krista@2461
   879
                goto pep_free;
krista@2461
   880
            // Otherwise, it was expired and key renewal failed
krista@2461
   881
            // and we take the stored one or do keygen. 
krista@2461
   882
        } 
krista@2461
   883
        else
krista@2461
   884
            valid_key_found = true;
krista@2461
   885
    }    
krista@2461
   886
    
krista@2461
   887
    // Ok, if there wasn't a valid input fpr, check stored identity
krista@2461
   888
    if (!valid_key_found && stored_identity && 
krista@2461
   889
        (EMPTYSTR(identity->fpr) || strcmp(stored_identity->fpr, identity->fpr) != 0)) {
krista@1359
   890
        
krista@2461
   891
        // Fall back / retrieve
krista@2461
   892
        status = validate_fpr(session, stored_identity);
krista@2461
   893
        if (status == PEP_STATUS_OK && 
krista@2461
   894
            stored_identity->comm_type >= PEP_ct_strong_but_unconfirmed) {
krista@2461
   895
          
krista@2461
   896
            free(identity->fpr);
krista@2461
   897
            identity->fpr = strdup(stored_identity->fpr);
krista@2461
   898
            valid_key_found = true;            
Edouard@560
   899
        }
krista@2461
   900
        else {
krista@2461
   901
            bool revoked = false;
krista@2461
   902
            if (!EMPTYSTR(stored_identity->fpr)) {
krista@2461
   903
                status = key_revoked(session, stored_identity->fpr, &revoked);
krista@2461
   904
                if (revoked)
krista@2461
   905
                    revoked_fpr = strdup(stored_identity->fpr);
krista@2461
   906
            }        
krista@1357
   907
        }
Edouard@560
   908
    }
krista@1357
   909
    
krista@2461
   910
    // Nothing left to do but generate a key
krista@2461
   911
    if (!valid_key_found) {
krista@2461
   912
        if (!do_keygen)
krista@2461
   913
            status = PEP_GET_KEY_FAILED;
krista@2461
   914
        else {
krista@2461
   915
            DEBUG_LOG("Generating key pair", "debug", identity->address);
krista@2461
   916
krista@2461
   917
            free(identity->fpr);
krista@2461
   918
            identity->fpr = NULL;
krista@2461
   919
            status = generate_keypair(session, identity);
krista@2461
   920
            assert(status != PEP_OUT_OF_MEMORY);
vb@934
   921
krista@2461
   922
            if (status != PEP_STATUS_OK) {
krista@2461
   923
                char buf[11];
krista@2461
   924
                snprintf(buf, 11, "%d", status); // uh, this is kludgey. FIXME
krista@2461
   925
                DEBUG_LOG("Generating key pair failed", "debug", buf);
krista@2461
   926
            }        
krista@2461
   927
            else {
krista@2461
   928
                valid_key_found = true;
krista@2461
   929
                if (revoked_fpr) {
krista@2461
   930
                    status = set_revoked(session, revoked_fpr,
krista@2461
   931
                                         stored_identity->fpr, time(NULL));
krista@2461
   932
                }
krista@2461
   933
            }
krista@1357
   934
        }
Edouard@560
   935
    }
Edouard@695
   936
krista@2461
   937
    if (valid_key_found) {
krista@2461
   938
        identity->comm_type = PEP_ct_pEp;
krista@2461
   939
        status = PEP_STATUS_OK;
krista@2461
   940
    }
krista@2461
   941
    else {
krista@2461
   942
        free(identity->fpr);
krista@2461
   943
        identity->fpr = NULL;
krista@2461
   944
        identity->comm_type = PEP_ct_unknown;
Edouard@695
   945
    }
krista@2461
   946
    
krista@2461
   947
    status = set_identity(session, identity);
krista@2461
   948
krista@2461
   949
pep_free:    
krista@2461
   950
    free(default_own_id);
krista@2461
   951
    free(revoked_fpr);                     
krista@2461
   952
    free_identity(stored_identity);
krista@2461
   953
    return ADD_TO_LOG(status);
krista@2461
   954
}
edouard@1385
   955
krista@2461
   956
DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
krista@2461
   957
                                                 identity_list* my_idents) {
krista@2461
   958
    PEP_STATUS status = PEP_STATUS_OK;
krista@2461
   959
    if (!session)
krista@2461
   960
        return PEP_ILLEGAL_VALUE;
Edouard@560
   961
        
krista@2461
   962
    if (!my_idents)
krista@2461
   963
        return PEP_STATUS_OK;
krista@2461
   964
            
krista@2461
   965
    identity_list* ident_curr = my_idents;
krista@2461
   966
    while (ident_curr) {
krista@2461
   967
        pEp_identity* ident = ident_curr->ident;
krista@2461
   968
        if (!ident || !ident->address) {
krista@2461
   969
            status = PEP_ILLEGAL_VALUE;
krista@2461
   970
            goto pep_error;
vb@0
   971
        }
edouard@1140
   972
krista@2461
   973
        status = _myself(session, ident, false, false);
Edouard@560
   974
        
krista@2461
   975
        ident_curr = ident_curr->next;
vb@0
   976
    }
krista@1353
   977
    
krista@2461
   978
pep_error:
krista@2461
   979
    return status;
vb@0
   980
}
vb@0
   981
edouard@1385
   982
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
edouard@1385
   983
{
roker@1853
   984
    return ADD_TO_LOG(_myself(session, identity, true, false));
edouard@1385
   985
}
edouard@1385
   986
vb@296
   987
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   988
        PEP_SESSION session, 
vb@292
   989
        examine_identity_t examine_identity,
vb@292
   990
        void *management
vb@292
   991
    )
vb@292
   992
{
vb@292
   993
    assert(session);
vb@292
   994
    if (!session)
vb@292
   995
        return PEP_ILLEGAL_VALUE;
vb@292
   996
vb@292
   997
    session->examine_management = management;
vb@292
   998
    session->examine_identity = examine_identity;
vb@292
   999
vb@292
  1000
    return PEP_STATUS_OK;
vb@292
  1001
}
vb@292
  1002
vb@0
  1003
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
  1004
        retrieve_next_identity_t retrieve_next_identity,
vb@0
  1005
        void *management
vb@0
  1006
    )
vb@0
  1007
{
vb@0
  1008
    PEP_SESSION session;
vb@0
  1009
    pEp_identity *identity;
Edouard@499
  1010
    PEP_STATUS status;
vb@0
  1011
vb@24
  1012
    assert(retrieve_next_identity);
vb@24
  1013
    assert(management);
vb@24
  1014
Edouard@499
  1015
    if (!retrieve_next_identity || !management)
Edouard@499
  1016
        return PEP_ILLEGAL_VALUE;
Edouard@499
  1017
Edouard@499
  1018
    status = init(&session);
Edouard@499
  1019
    assert(status == PEP_STATUS_OK);
Edouard@499
  1020
    if (status != PEP_STATUS_OK)
Edouard@499
  1021
        return status;
Edouard@499
  1022
vb@0
  1023
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
  1024
Edouard@499
  1025
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
  1026
    {
vb@0
  1027
        assert(identity->address);
Edouard@499
  1028
        if(identity->address)
Edouard@499
  1029
        {
Edouard@499
  1030
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
  1031
krista@2461
  1032
            if (identity->me) {
Edouard@499
  1033
                status = myself(session, identity);
Edouard@499
  1034
            } else {
Edouard@499
  1035
                status = recv_key(session, identity->address);
Edouard@499
  1036
            }
Edouard@499
  1037
vb@0
  1038
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
  1039
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
  1040
                return PEP_OUT_OF_MEMORY;
vb@0
  1041
        }
vb@0
  1042
        free_identity(identity);
vb@0
  1043
    }
vb@0
  1044
vb@0
  1045
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
  1046
vb@0
  1047
    release(session);
vb@0
  1048
    return PEP_STATUS_OK;
vb@0
  1049
}
vb@0
  1050
krista@1213
  1051
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
  1052
        PEP_SESSION session,
vb@357
  1053
        pEp_identity *ident
vb@357
  1054
    )
vb@215
  1055
{
vb@218
  1056
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
  1057
vb@215
  1058
    assert(session);
vb@357
  1059
    assert(ident);
Edouard@439
  1060
    assert(!EMPTYSTR(ident->fpr));
vb@215
  1061
vb@357
  1062
    if (!(session && ident && ident->fpr))
vb@215
  1063
        return PEP_ILLEGAL_VALUE;
vb@215
  1064
krista@2461
  1065
    if (ident->me)
Edouard@697
  1066
    {
vb@357
  1067
        revoke_key(session, ident->fpr, NULL);
Edouard@697
  1068
        myself(session, ident);
Edouard@697
  1069
    }
Edouard@697
  1070
    else
Edouard@697
  1071
    {
krista@2129
  1072
        // for undo
krista@2129
  1073
        if (session->cached_mistrusted)
krista@2129
  1074
            free(session->cached_mistrusted);
krista@2129
  1075
        session->cached_mistrusted = identity_dup(ident);
krista@2461
  1076
        
krista@2461
  1077
        // set mistrust for this user_id/keypair (even if there's not an
krista@2461
  1078
        // identity set yet, this is important, as we need to record the mistrust
krista@2461
  1079
        // action)
krista@2478
  1080
        
krista@2483
  1081
        // double-check to be sure key is even in the DB
krista@2483
  1082
        if (ident->fpr)
krista@2483
  1083
            status = set_pgp_keypair(session, ident->fpr);
krista@2483
  1084
krista@2478
  1085
        // We set this temporarily but will grab it back from the cache afterwards
krista@2478
  1086
        ident->comm_type = PEP_ct_mistrusted;
krista@2478
  1087
        status = set_trust(session, ident);
krista@2478
  1088
        ident->comm_type = session->cached_mistrusted->comm_type;
krista@2478
  1089
        
krista@2461
  1090
        if (status == PEP_STATUS_OK)
krista@2461
  1091
            // cascade that mistrust for anyone using this key
krista@2461
  1092
            status = mark_as_compromized(session, ident->fpr);
krista@2461
  1093
        if (status == PEP_STATUS_OK)
krista@2461
  1094
            status = remove_fpr_as_default(session, ident->fpr);
krista@2471
  1095
        if (status == PEP_STATUS_OK)
krista@2471
  1096
            status = add_mistrusted_key(session, ident->fpr);
Edouard@697
  1097
    }
vb@218
  1098
vb@218
  1099
    return status;
vb@215
  1100
}
vb@215
  1101
krista@2129
  1102
DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session) {
krista@2129
  1103
    assert(session);
krista@2129
  1104
    
krista@2129
  1105
    if (!session)
krista@2129
  1106
        return PEP_ILLEGAL_VALUE;
krista@2129
  1107
    
krista@2129
  1108
    PEP_STATUS status = PEP_STATUS_OK;
krista@2129
  1109
        
krista@2129
  1110
    pEp_identity* cached_ident = session->cached_mistrusted;
krista@2129
  1111
    
krista@2129
  1112
    if (!cached_ident)
krista@2129
  1113
        status = PEP_CANNOT_FIND_IDENTITY;
krista@2129
  1114
    else {
krista@2471
  1115
        status = delete_mistrusted_key(session, cached_ident->fpr);
krista@2471
  1116
        if (status == PEP_STATUS_OK) {
krista@2471
  1117
            status = set_identity(session, cached_ident);            
krista@2471
  1118
            free_identity(session->cached_mistrusted);
krista@2471
  1119
        }
krista@2129
  1120
    }
krista@2129
  1121
    
krista@2129
  1122
    session->cached_mistrusted = NULL;
krista@2471
  1123
    
krista@2129
  1124
    return status;
krista@2129
  1125
}
krista@2129
  1126
Edouard@410
  1127
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
  1128
        PEP_SESSION session,
Edouard@410
  1129
        pEp_identity *ident
Edouard@410
  1130
    )
Edouard@410
  1131
{
Edouard@410
  1132
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
  1133
Edouard@410
  1134
    assert(session);
Edouard@410
  1135
    assert(ident);
Edouard@439
  1136
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
  1137
    assert(!EMPTYSTR(ident->address));
Edouard@439
  1138
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
  1139
krista@2461
  1140
    if (!(session && ident && ident->fpr && ident->fpr[0] != '\0' && ident->address &&
vb@420
  1141
            ident->user_id))
Edouard@410
  1142
        return PEP_ILLEGAL_VALUE;
Edouard@410
  1143
krista@2461
  1144
    // we do not change the input struct at ALL.
krista@2461
  1145
    pEp_identity* input_copy = identity_dup(ident);
krista@2461
  1146
    
krista@2461
  1147
    pEp_identity* tmp_ident = NULL;
krista@2461
  1148
    
krista@2461
  1149
    status = get_trust(session, input_copy);
krista@2461
  1150
    
vb@420
  1151
    if (status != PEP_STATUS_OK)
krista@2461
  1152
        goto pep_free;
krista@2461
  1153
        
krista@2461
  1154
    PEP_comm_type new_trust = PEP_ct_unknown;
krista@2461
  1155
krista@2461
  1156
    if (input_copy->comm_type != PEP_ct_mistrusted)
krista@2461
  1157
        new_trust = input_copy->comm_type & ~PEP_ct_confirmed;
krista@2461
  1158
krista@2478
  1159
    // We'll return the status from the input_copy cache afterward
krista@2478
  1160
    input_copy->comm_type = new_trust;
krista@2478
  1161
    status = set_trust(session, input_copy);
krista@2461
  1162
    
krista@2461
  1163
    if (status != PEP_STATUS_OK)
krista@2461
  1164
        goto pep_free;
krista@2461
  1165
krista@2471
  1166
    bool mistrusted_key = false;
krista@2461
  1167
        
krista@2471
  1168
    status = is_mistrusted_key(session, ident->fpr, &mistrusted_key);
krista@2471
  1169
krista@2471
  1170
    if (status != PEP_STATUS_OK)
krista@2471
  1171
        goto pep_free;
krista@2471
  1172
    
krista@2471
  1173
    if (mistrusted_key)
krista@2471
  1174
        status = delete_mistrusted_key(session, ident->fpr);
krista@2471
  1175
krista@2471
  1176
    if (status != PEP_STATUS_OK)
krista@2471
  1177
        goto pep_free;
krista@2461
  1178
        
krista@2461
  1179
    tmp_ident = new_identity(ident->address, NULL, ident->user_id, NULL);
Edouard@410
  1180
krista@2461
  1181
    if (!tmp_ident)
krista@2461
  1182
        return PEP_OUT_OF_MEMORY;
krista@2461
  1183
    
krista@2461
  1184
    status = update_identity(session, tmp_ident);
krista@2461
  1185
    
vb@421
  1186
    if (status != PEP_STATUS_OK)
krista@2461
  1187
        goto pep_free;
krista@2461
  1188
    
krista@2461
  1189
    // remove as default if necessary
krista@2461
  1190
    if (strcmp(tmp_ident->fpr, ident->fpr) == 0) {
krista@2461
  1191
        free(tmp_ident->fpr);
krista@2461
  1192
        tmp_ident->fpr = NULL;
krista@2461
  1193
        tmp_ident->comm_type = PEP_ct_unknown;
krista@2461
  1194
        status = set_identity(session, tmp_ident);
krista@2461
  1195
        if (status != PEP_STATUS_OK)
krista@2461
  1196
            goto pep_free;
krista@2461
  1197
    }
krista@2461
  1198
    
krista@2461
  1199
    char* user_default = NULL;
krista@2461
  1200
    status = get_main_user_fpr(session, tmp_ident->user_id, &user_default);
krista@2461
  1201
    
krista@2461
  1202
    if (!EMPTYSTR(user_default)) {
krista@2461
  1203
        if (strcmp(user_default, ident->fpr) == 0)
krista@2461
  1204
            status = refresh_userid_default_key(session, ident->user_id);
krista@2461
  1205
        if (status != PEP_STATUS_OK)
krista@2461
  1206
            goto pep_free;    
krista@2461
  1207
    }
krista@2461
  1208
            
krista@2461
  1209
pep_free:
krista@2461
  1210
    free_identity(tmp_ident);
krista@2461
  1211
    free_identity(input_copy);
Edouard@410
  1212
    return status;
Edouard@410
  1213
}
Edouard@410
  1214
vb@354
  1215
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
  1216
        PEP_SESSION session,
vb@354
  1217
        pEp_identity *ident
vb@354
  1218
    )
vb@354
  1219
{
vb@354
  1220
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
  1221
vb@354
  1222
    assert(session);
vb@354
  1223
    assert(ident);
Edouard@439
  1224
    assert(!EMPTYSTR(ident->address));
Edouard@439
  1225
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
  1226
    assert(!EMPTYSTR(ident->fpr));
vb@354
  1227
Edouard@439
  1228
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
edouard@1945
  1229
            EMPTYSTR(ident->fpr))
vb@354
  1230
        return PEP_ILLEGAL_VALUE;
vb@354
  1231
krista@2461
  1232
    //bool ident_has_trusted_default = false;
krista@2461
  1233
    char* ident_default_fpr = NULL;
krista@2461
  1234
krista@2461
  1235
    // Before we do anything, be sure the input fpr is even eligible to be trusted
krista@2461
  1236
    PEP_comm_type input_default_ct = PEP_ct_unknown;
krista@2461
  1237
    status = get_key_rating(session, ident->fpr, &input_default_ct);
krista@2461
  1238
    if (input_default_ct < PEP_ct_strong_but_unconfirmed)
krista@2461
  1239
        return PEP_KEY_UNSUITABLE;
krista@2461
  1240
krista@2477
  1241
    status = set_pgp_keypair(session, ident->fpr);
krista@2477
  1242
    if (status != PEP_STATUS_OK)
krista@2477
  1243
        return status;
krista@2461
  1244
krista@2461
  1245
    bool me = is_me(session, ident);
krista@2461
  1246
krista@2494
  1247
    pEp_identity* ident_copy = identity_dup(ident);
krista@2494
  1248
    char* cached_fpr = NULL;
krista@2494
  1249
krista@2494
  1250
    // for setting up a temp trusted identity for the input fpr
krista@2494
  1251
    pEp_identity* tmp_id = NULL;
krista@2494
  1252
krista@2494
  1253
    if (me) {
krista@2494
  1254
        status = myself(session, ident_copy); 
krista@2494
  1255
        goto pep_free;
krista@2494
  1256
    }
krista@2494
  1257
    
krista@2494
  1258
    // For later, in case we need to check the user default key
krista@2494
  1259
    pEp_identity* tmp_user_ident = NULL;
krista@2494
  1260
    
krista@2494
  1261
    // Save the input fpr
krista@2494
  1262
    cached_fpr = strdup(ident->fpr);
krista@2461
  1263
krista@2461
  1264
    // First, set up a temp trusted identity for the input fpr without a comm type;
krista@2494
  1265
    tmp_id = new_identity(ident->address, ident->fpr, ident->user_id, NULL);
krista@2461
  1266
    status = validate_fpr(session, tmp_id);
krista@2461
  1267
        
krista@2461
  1268
    if (status == PEP_STATUS_OK) {
krista@2461
  1269
        // Validate fpr gets trust DB or, when that fails, key comm type. we checked
krista@2461
  1270
        // above that the key was ok. (not revoked or expired), but we want the max.
krista@2461
  1271
        tmp_id->comm_type = _MAX(tmp_id->comm_type, input_default_ct) | PEP_ct_confirmed;
krista@2461
  1272
                                       
krista@2461
  1273
        // Get the default identity without setting the fpr
krista@2494
  1274
        status = update_identity(session, ident_copy);
krista@2494
  1275
        ident_default_fpr = strdup(ident_copy->fpr);
krista@2461
  1276
krista@2461
  1277
        if (status == PEP_STATUS_OK) {
krista@2461
  1278
            bool trusted_default = false;
krista@2461
  1279
krista@2461
  1280
            // If there's no default, or the default is different from the input...
krista@2461
  1281
            if (EMPTYSTR(ident_default_fpr) || strcmp(cached_fpr, ident_default_fpr) != 0) {
krista@2461
  1282
                
krista@2461
  1283
                // If the default fpr (if there is one) is trusted and key is strong enough,
krista@2461
  1284
                // don't replace, we just set the trusted bit on this key for this user_id...
krista@2461
  1285
                // (If there's no default fpr, this won't be true anyway.)
krista@2461
  1286
                if (ident->comm_type >= PEP_ct_strong_but_unconfirmed && 
krista@2461
  1287
                    (ident->comm_type & PEP_ct_confirmed)) {                        
vb@354
  1288
krista@2461
  1289
                    trusted_default = true;
krista@2461
  1290
                                    
krista@2478
  1291
                    status = set_trust(session, tmp_id);
krista@2461
  1292
                    input_default_ct = tmp_id->comm_type;                    
krista@2461
  1293
                }
krista@2461
  1294
                else {
krista@2494
  1295
                    free(ident_copy->fpr);
krista@2494
  1296
                    ident_copy->fpr = strdup(cached_fpr);
krista@2494
  1297
                    ident_copy->comm_type = tmp_id->comm_type;
krista@2494
  1298
                    status = set_identity(session, ident_copy); // replace identity default            
krista@2461
  1299
                }
krista@2461
  1300
            }
krista@2461
  1301
            else { // we're setting this on the default fpr
krista@2461
  1302
                ident->comm_type = tmp_id->comm_type;
krista@2461
  1303
                status = set_identity(session, ident);
krista@2461
  1304
                trusted_default = true;
krista@2461
  1305
            }
krista@2461
  1306
            if (status == PEP_STATUS_OK && !trusted_default) {
krista@2461
  1307
                // Ok, there wasn't a trusted default, so we replaced. Thus, we also
krista@2461
  1308
                // make sure there's a trusted default on the user_id. If there
krista@2461
  1309
                // is not, we make this the default.
krista@2461
  1310
                char* user_default = NULL;
krista@2461
  1311
                status = get_main_user_fpr(session, ident->user_id, &user_default);
krista@2461
  1312
            
krista@2461
  1313
                if (status == PEP_STATUS_OK && user_default) {
krista@2494
  1314
                    tmp_user_ident = new_identity(ident->address, 
krista@2494
  1315
                                                  user_default, 
krista@2494
  1316
                                                  ident->user_id, 
krista@2494
  1317
                                                  NULL);
krista@2461
  1318
                    if (!tmp_user_ident)
krista@2461
  1319
                        status = PEP_OUT_OF_MEMORY;
krista@2461
  1320
                    else {
krista@2461
  1321
                        status = validate_fpr(session, tmp_user_ident);
krista@2461
  1322
                        
krista@2461
  1323
                        if (status != PEP_STATUS_OK ||
krista@2461
  1324
                            tmp_user_ident->comm_type < PEP_ct_strong_but_unconfirmed ||
krista@2461
  1325
                            !(tmp_user_ident->comm_type & PEP_ct_confirmed)) 
krista@2461
  1326
                        {
krista@2461
  1327
                            char* trusted_fpr = (trusted_default ? ident_default_fpr : cached_fpr);
krista@2461
  1328
                            status = replace_main_user_fpr(session, ident->user_id, trusted_fpr);
krista@2461
  1329
                        } 
krista@2461
  1330
                    }
krista@2461
  1331
                }
krista@2461
  1332
            }
krista@2461
  1333
        }
krista@2461
  1334
    }    
vb@354
  1335
krista@2494
  1336
pep_free:
krista@2494
  1337
    free(ident_default_fpr);
krista@2494
  1338
    free(cached_fpr);
krista@2494
  1339
    free_identity(tmp_id);
krista@2494
  1340
    free_identity(ident_copy);
krista@2494
  1341
    free_identity(tmp_user_ident);
vb@354
  1342
    return status;
vb@354
  1343
}
vb@354
  1344
Edouard@584
  1345
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
  1346
        PEP_SESSION session,
vb@955
  1347
        const char *fpr,
vb@955
  1348
        bool *listed
vb@955
  1349
    )
Edouard@584
  1350
{
Edouard@584
  1351
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
  1352
    int count;
Edouard@584
  1353
    
Edouard@584
  1354
    assert(session && fpr && fpr[0] && listed);
Edouard@584
  1355
    
Edouard@584
  1356
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
  1357
        return PEP_ILLEGAL_VALUE;
Edouard@584
  1358
    
Edouard@584
  1359
    *listed = false;
Edouard@584
  1360
    
Edouard@584
  1361
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
  1362
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
  1363
    
Edouard@584
  1364
    int result;
Edouard@584
  1365
    
Edouard@584
  1366
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
  1367
    switch (result) {
Edouard@584
  1368
        case SQLITE_ROW:
Edouard@584
  1369
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
  1370
            *listed = count > 0;
krista@2461
  1371
            status = PEP_STATUS_OK;
Edouard@584
  1372
            break;
Edouard@584
  1373
            
Edouard@584
  1374
        default:
Edouard@584
  1375
            status = PEP_UNKNOWN_ERROR;
Edouard@584
  1376
    }
Edouard@584
  1377
    
Edouard@584
  1378
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
  1379
    return status;
Edouard@584
  1380
}
Edouard@584
  1381
edouard@1412
  1382
PEP_STATUS _own_identities_retrieve(
vb@955
  1383
        PEP_SESSION session,
edouard@1412
  1384
        identity_list **own_identities,
edouard@1412
  1385
        identity_flags_t excluded_flags
vb@955
  1386
      )
Edouard@584
  1387
{
Edouard@584
  1388
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
  1389
    
vb@955
  1390
    assert(session && own_identities);
vb@955
  1391
    if (!(session && own_identities))
Edouard@584
  1392
        return PEP_ILLEGAL_VALUE;
Edouard@584
  1393
    
vb@955
  1394
    *own_identities = NULL;
vb@955
  1395
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
  1396
    if (_own_identities == NULL)
Edouard@584
  1397
        goto enomem;
Edouard@584
  1398
    
vb@955
  1399
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
  1400
    
Edouard@584
  1401
    int result;
vb@955
  1402
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
  1403
    const char *address = NULL;
Edouard@584
  1404
    const char *fpr = NULL;
vb@955
  1405
    const char *username = NULL;
vb@955
  1406
    const char *user_id = NULL;
vb@955
  1407
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
  1408
    const char *lang = NULL;
vb@955
  1409
    unsigned int flags = 0;
Edouard@584
  1410
    
vb@955
  1411
    identity_list *_bl = _own_identities;
Edouard@584
  1412
    do {
edouard@1412
  1413
        sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
vb@955
  1414
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
  1415
        switch (result) {
Edouard@584
  1416
            case SQLITE_ROW:
vb@955
  1417
                address = (const char *)
vb@955
  1418
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
  1419
                fpr = (const char *)
vb@955
  1420
                    sqlite3_column_text(session->own_identities_retrieve, 1);
krista@2461
  1421
                user_id = (const char *)
krista@2461
  1422
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
  1423
                username = (const char *)
krista@2461
  1424
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
  1425
                comm_type = PEP_ct_pEp;
vb@1071
  1426
                lang = (const char *)
krista@2461
  1427
                    sqlite3_column_text(session->own_identities_retrieve, 4);
vb@1071
  1428
                flags = (unsigned int)
krista@2461
  1429
                    sqlite3_column_int(session->own_identities_retrieve, 5);
vb@955
  1430
vb@1078
  1431
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
  1432
                if (!ident)
Edouard@584
  1433
                    goto enomem;
vb@955
  1434
                ident->comm_type = comm_type;
vb@955
  1435
                if (lang && lang[0]) {
vb@955
  1436
                    ident->lang[0] = lang[0];
vb@955
  1437
                    ident->lang[1] = lang[1];
vb@1078
  1438
                    ident->lang[2] = 0;
vb@955
  1439
                }
krista@2461
  1440
                ident->me = true;
vb@955
  1441
                ident->flags = flags;
vb@955
  1442
vb@955
  1443
                _bl = identity_list_add(_bl, ident);
vb@1080
  1444
                if (_bl == NULL) {
vb@1080
  1445
                    free_identity(ident);
Edouard@584
  1446
                    goto enomem;
vb@1080
  1447
                }
Edouard@584
  1448
                
Edouard@584
  1449
                break;
Edouard@584
  1450
                
Edouard@584
  1451
            case SQLITE_DONE:
Edouard@584
  1452
                break;
Edouard@584
  1453
                
Edouard@584
  1454
            default:
Edouard@584
  1455
                status = PEP_UNKNOWN_ERROR;
Edouard@584
  1456
                result = SQLITE_DONE;
Edouard@584
  1457
        }
Edouard@584
  1458
    } while (result != SQLITE_DONE);
Edouard@584
  1459
    
vb@955
  1460
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
  1461
    if (status == PEP_STATUS_OK)
vb@955
  1462
        *own_identities = _own_identities;
Edouard@584
  1463
    else
vb@955
  1464
        free_identity_list(_own_identities);
Edouard@584
  1465
    
Edouard@584
  1466
    goto the_end;
Edouard@584
  1467
    
Edouard@584
  1468
enomem:
vb@955
  1469
    free_identity_list(_own_identities);
Edouard@584
  1470
    status = PEP_OUT_OF_MEMORY;
Edouard@584
  1471
    
Edouard@584
  1472
the_end:
Edouard@584
  1473
    return status;
Edouard@584
  1474
}
vb@955
  1475
edouard@1412
  1476
DYNAMIC_API PEP_STATUS own_identities_retrieve(
edouard@1364
  1477
        PEP_SESSION session,
edouard@1412
  1478
        identity_list **own_identities
edouard@1412
  1479
      )
edouard@1412
  1480
{
edouard@1412
  1481
    return _own_identities_retrieve(session, own_identities, 0);
edouard@1412
  1482
}
edouard@1412
  1483
edouard@1412
  1484
PEP_STATUS _own_keys_retrieve(
edouard@1412
  1485
        PEP_SESSION session,
edouard@1412
  1486
        stringlist_t **keylist,
edouard@1412
  1487
        identity_flags_t excluded_flags
edouard@1364
  1488
      )
edouard@1364
  1489
{
edouard@1364
  1490
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1364
  1491
    
edouard@1364
  1492
    assert(session && keylist);
edouard@1364
  1493
    if (!(session && keylist))
edouard@1364
  1494
        return PEP_ILLEGAL_VALUE;
edouard@1364
  1495
    
edouard@1364
  1496
    *keylist = NULL;
edouard@1364
  1497
    stringlist_t *_keylist = NULL;
edouard@1364
  1498
    
edouard@1394
  1499
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1500
    
edouard@1364
  1501
    int result;
edouard@1364
  1502
    char *fpr = NULL;
edouard@1364
  1503
    
edouard@1364
  1504
    stringlist_t *_bl = _keylist;
edouard@1364
  1505
    do {
edouard@1412
  1506
        sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
edouard@1394
  1507
        result = sqlite3_step(session->own_keys_retrieve);
edouard@1364
  1508
        switch (result) {
edouard@1364
  1509
            case SQLITE_ROW:
edouard@1394
  1510
                fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
edouard@1364
  1511
                if(fpr == NULL)
edouard@1364
  1512
                    goto enomem;
edouard@1364
  1513
edouard@1364
  1514
                _bl = stringlist_add(_bl, fpr);
edouard@1364
  1515
                if (_bl == NULL) {
edouard@1364
  1516
                    free(fpr);
edouard@1364
  1517
                    goto enomem;
edouard@1364
  1518
                }
edouard@1364
  1519
                if (_keylist == NULL)
edouard@1364
  1520
                    _keylist = _bl;
edouard@1364
  1521
                
edouard@1364
  1522
                break;
edouard@1364
  1523
                
edouard@1364
  1524
            case SQLITE_DONE:
edouard@1364
  1525
                break;
edouard@1364
  1526
                
edouard@1364
  1527
            default:
edouard@1364
  1528
                status = PEP_UNKNOWN_ERROR;
edouard@1364
  1529
                result = SQLITE_DONE;
edouard@1364
  1530
        }
edouard@1364
  1531
    } while (result != SQLITE_DONE);
edouard@1364
  1532
    
edouard@1394
  1533
    sqlite3_reset(session->own_keys_retrieve);
edouard@1364
  1534
    if (status == PEP_STATUS_OK)
edouard@1364
  1535
        *keylist = _keylist;
edouard@1364
  1536
    else
edouard@1364
  1537
        free_stringlist(_keylist);
edouard@1364
  1538
    
edouard@1364
  1539
    goto the_end;
edouard@1364
  1540
    
edouard@1364
  1541
enomem:
edouard@1364
  1542
    free_stringlist(_keylist);
edouard@1364
  1543
    status = PEP_OUT_OF_MEMORY;
edouard@1364
  1544
    
edouard@1364
  1545
the_end:
edouard@1364
  1546
    return status;
edouard@1364
  1547
}
edouard@1364
  1548
edouard@1412
  1549
DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
edouard@1412
  1550
{
edouard@1412
  1551
    return _own_keys_retrieve(session, keylist, 0);
edouard@1412
  1552
}
edouard@1412
  1553
edouard@1760
  1554
// FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
edouard@1394
  1555
DYNAMIC_API PEP_STATUS set_own_key(
edouard@1394
  1556
       PEP_SESSION session,
edouard@1394
  1557
       const char *address,
edouard@1394
  1558
       const char *fpr
edouard@1394
  1559
    )
edouard@1394
  1560
{
edouard@1394
  1561
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1394
  1562
    
edouard@1394
  1563
    assert(session &&
edouard@1760
  1564
           address &&
edouard@1394
  1565
           fpr && fpr[0]
edouard@1394
  1566
          );
edouard@1394
  1567
    
edouard@1394
  1568
    if (!(session &&
edouard@1760
  1569
          address &&
edouard@1394
  1570
          fpr && fpr[0]
edouard@1394
  1571
         ))
edouard@1394
  1572
        return PEP_ILLEGAL_VALUE;
krista@2461
  1573
                        
krista@2461
  1574
    // First see if we have it in own identities already, AND we retrieve
krista@2461
  1575
    // our own user_id
krista@2461
  1576
    char* my_user_id = NULL;
krista@2461
  1577
    status = get_default_own_userid(session, &my_user_id);
krista@2461
  1578
    if (status != PEP_STATUS_OK)
krista@2461
  1579
        return status;
krista@2461
  1580
        
krista@2461
  1581
    if (!my_user_id) {
krista@2461
  1582
        // We have no own user_id. So we cannot set it for an identity.
krista@2461
  1583
        return PEP_CANNOT_FIND_IDENTITY;
edouard@1394
  1584
    }
edouard@1394
  1585
    
krista@2461
  1586
    pEp_identity* my_id = NULL;
krista@2461
  1587
    
krista@2461
  1588
    status = get_identity(session, my_user_id, address, &my_id);
krista@2461
  1589
krista@2461
  1590
    if (status == PEP_STATUS_OK && my_id) {
krista@2461
  1591
        if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
krista@2461
  1592
            // We're done. It was already here.
krista@2461
  1593
            goto pep_free;
krista@2461
  1594
        }           
krista@2461
  1595
    }
krista@2461
  1596
                
krista@2461
  1597
    // If there's an id w/ user_id + address
krista@2461
  1598
    if (my_id) {
krista@2461
  1599
        free(my_id->fpr);
krista@2461
  1600
        my_id->fpr = my_user_id;
krista@2461
  1601
        my_id->comm_type = PEP_ct_pEp;
krista@2461
  1602
        my_id->me = true;
krista@2461
  1603
    }
krista@2461
  1604
    else { // Else, we need a new identity
krista@2461
  1605
        my_id = new_identity(address, fpr, my_user_id, NULL); 
krista@2461
  1606
        if (status != PEP_STATUS_OK)
krista@2461
  1607
            goto pep_free; 
krista@2461
  1608
        my_id->me = true;
krista@2461
  1609
        my_id->comm_type = PEP_ct_pEp;
krista@2461
  1610
    }
krista@2461
  1611
        
krista@2461
  1612
    status = set_identity(session, my_id);
krista@2461
  1613
    
krista@2461
  1614
pep_free:
krista@2461
  1615
    free(my_id);
krista@2461
  1616
    free(my_user_id);
edouard@1394
  1617
    return status;
edouard@1394
  1618
}
krista@1357
  1619
krista@1357
  1620
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
  1621
                             bool *has_private) {
krista@1357
  1622
krista@1357
  1623
    assert(session);
krista@1357
  1624
    assert(fpr);
krista@1357
  1625
    assert(has_private);
krista@1357
  1626
    
krista@1357
  1627
    if (!(session && fpr && has_private))
krista@1357
  1628
        return PEP_ILLEGAL_VALUE;
krista@1357
  1629
krista@1357
  1630
    return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
edouard@1385
  1631
}
krista@2471
  1632
krista@2471
  1633
PEP_STATUS add_mistrusted_key(PEP_SESSION session, const char* fpr)
krista@2471
  1634
{
krista@2471
  1635
    int result;
krista@2471
  1636
krista@2471
  1637
    assert(!EMPTYSTR(fpr));
krista@2471
  1638
    
krista@2471
  1639
    if (!(session) || EMPTYSTR(fpr))
krista@2471
  1640
        return PEP_ILLEGAL_VALUE;
krista@2471
  1641
krista@2471
  1642
    sqlite3_reset(session->add_mistrusted_key);
krista@2471
  1643
    sqlite3_bind_text(session->add_mistrusted_key, 1, fpr, -1,
krista@2471
  1644
            SQLITE_STATIC);
krista@2471
  1645
krista@2471
  1646
    result = sqlite3_step(session->add_mistrusted_key);
krista@2471
  1647
    sqlite3_reset(session->add_mistrusted_key);
krista@2471
  1648
krista@2471
  1649
    if (result != SQLITE_DONE)
krista@2471
  1650
        return PEP_CANNOT_SET_PGP_KEYPAIR; // FIXME: Better status?
krista@2471
  1651
krista@2471
  1652
    return PEP_STATUS_OK;
krista@2471
  1653
}
krista@2471
  1654
krista@2471
  1655
PEP_STATUS delete_mistrusted_key(PEP_SESSION session, const char* fpr)
krista@2471
  1656
{
krista@2471
  1657
    int result;
krista@2471
  1658
krista@2471
  1659
    assert(!EMPTYSTR(fpr));
krista@2471
  1660
    
krista@2471
  1661
    if (!(session) || EMPTYSTR(fpr))
krista@2471
  1662
        return PEP_ILLEGAL_VALUE;
krista@2471
  1663
krista@2471
  1664
    sqlite3_reset(session->delete_mistrusted_key);
krista@2471
  1665
    sqlite3_bind_text(session->delete_mistrusted_key, 1, fpr, -1,
krista@2471
  1666
            SQLITE_STATIC);
krista@2471
  1667
krista@2471
  1668
    result = sqlite3_step(session->delete_mistrusted_key);
krista@2471
  1669
    sqlite3_reset(session->delete_mistrusted_key);
krista@2471
  1670
krista@2471
  1671
    if (result != SQLITE_DONE)
krista@2471
  1672
        return PEP_UNKNOWN_ERROR; // FIXME: Better status?
krista@2471
  1673
krista@2471
  1674
    return PEP_STATUS_OK;
krista@2471
  1675
}
krista@2471
  1676
krista@2471
  1677
PEP_STATUS is_mistrusted_key(PEP_SESSION session, const char* fpr,
krista@2471
  1678
                             bool* mistrusted)
krista@2471
  1679
{
krista@2471
  1680
    PEP_STATUS status = PEP_STATUS_OK;
krista@2471
  1681
krista@2471
  1682
    assert(session);
krista@2471
  1683
    assert(!EMPTYSTR(fpr));
krista@2471
  1684
krista@2471
  1685
    if (!(session && fpr))
krista@2471
  1686
        return PEP_ILLEGAL_VALUE;
krista@2471
  1687
krista@2471
  1688
    *mistrusted = false;
krista@2471
  1689
krista@2471
  1690
    sqlite3_reset(session->is_mistrusted_key);
krista@2471
  1691
    sqlite3_bind_text(session->is_mistrusted_key, 1, fpr, -1, SQLITE_STATIC);
krista@2471
  1692
krista@2471
  1693
    int result;
krista@2471
  1694
krista@2471
  1695
    result = sqlite3_step(session->is_mistrusted_key);
krista@2471
  1696
    switch (result) {
krista@2471
  1697
    case SQLITE_ROW:
krista@2471
  1698
        *mistrusted = sqlite3_column_int(session->is_mistrusted_key, 0);
krista@2471
  1699
        status = PEP_STATUS_OK;
krista@2471
  1700
        break;
krista@2471
  1701
krista@2471
  1702
    default:
krista@2471
  1703
        status = PEP_UNKNOWN_ERROR;
krista@2471
  1704
    }
krista@2471
  1705
krista@2471
  1706
    sqlite3_reset(session->is_mistrusted_key);
krista@2471
  1707
    return status;
krista@2471
  1708
}