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