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