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