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