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