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