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