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