src/keymanagement.c
author Krista Grothoff <krista@pep-project.org>
Tue, 08 Nov 2016 00:12:17 +0100
branchENGINE-84
changeset 1357 b1677cd84729
parent 1352 239640860531
child 1358 ff3053a671dc
permissions -rw-r--r--
ENGINE-84: working on ensuring keypairs missing private keys are not selected for encryption candidates
vb@130
     1
#include "platform.h"
vb@0
     2
vb@0
     3
#include <string.h>
vb@0
     4
#include <stdio.h>
vb@0
     5
#include <stdlib.h>
vb@0
     6
#include <assert.h>
Edouard@512
     7
#include <ctype.h>
vb@0
     8
vb@217
     9
#include "pEp_internal.h"
vb@0
    10
#include "keymanagement.h"
vb@0
    11
edouard@1195
    12
#include "sync_fsm.h"
krista@1253
    13
#include "blacklist.h"
edouard@1195
    14
Edouard@439
    15
#ifndef EMPTYSTR
roker@500
    16
#define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
vb@8
    17
#endif
vb@8
    18
vb@214
    19
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
vb@214
    20
Edouard@774
    21
PEP_STATUS elect_pubkey(
Edouard@755
    22
        PEP_SESSION session, pEp_identity * identity
Edouard@755
    23
    )
Edouard@755
    24
{
Edouard@755
    25
    PEP_STATUS status;
Edouard@755
    26
    stringlist_t *keylist;
krista@1342
    27
    char *_fpr = "";
Edouard@755
    28
    identity->comm_type = PEP_ct_unknown;
Edouard@755
    29
Edouard@755
    30
    status = find_keys(session, identity->address, &keylist);
Edouard@755
    31
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    32
    if (status == PEP_OUT_OF_MEMORY)
Edouard@755
    33
        return PEP_OUT_OF_MEMORY;
Edouard@755
    34
Edouard@755
    35
    stringlist_t *_keylist;
Edouard@755
    36
    for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@755
    37
        PEP_comm_type _comm_type_key;
Edouard@755
    38
Edouard@755
    39
        status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@755
    40
        assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    41
        if (status == PEP_OUT_OF_MEMORY) {
Edouard@755
    42
            free_stringlist(keylist);
Edouard@755
    43
            return PEP_OUT_OF_MEMORY;
Edouard@755
    44
        }
Edouard@755
    45
Edouard@755
    46
        if (_comm_type_key != PEP_ct_compromized &&
Edouard@755
    47
            _comm_type_key != PEP_ct_unknown)
Edouard@755
    48
        {
Edouard@755
    49
            if (identity->comm_type == PEP_ct_unknown ||
Edouard@755
    50
                _comm_type_key > identity->comm_type)
Edouard@755
    51
            {
krista@1275
    52
                bool blacklisted;
krista@1275
    53
                status = blacklist_is_listed(session, _keylist->value, &blacklisted);
krista@1275
    54
                if (status == PEP_STATUS_OK && !blacklisted) {
krista@1275
    55
                    identity->comm_type = _comm_type_key;
krista@1275
    56
                    _fpr = _keylist->value;
krista@1275
    57
                }
Edouard@755
    58
            }
Edouard@755
    59
        }
Edouard@755
    60
    }
Edouard@755
    61
krista@1342
    62
    
krista@1342
    63
//    if (_fpr) {
krista@1342
    64
    free(identity->fpr);
Edouard@755
    65
krista@1342
    66
    identity->fpr = strdup(_fpr);
krista@1342
    67
    if (identity->fpr == NULL) {
krista@1342
    68
        free_stringlist(keylist);
krista@1342
    69
        return PEP_OUT_OF_MEMORY;
Edouard@755
    70
    }
krista@1342
    71
//    }
Edouard@755
    72
    free_stringlist(keylist);
Edouard@755
    73
    return PEP_STATUS_OK;
Edouard@755
    74
}
Edouard@755
    75
vb@0
    76
DYNAMIC_API PEP_STATUS update_identity(
vb@0
    77
        PEP_SESSION session, pEp_identity * identity
vb@0
    78
    )
vb@0
    79
{
vb@0
    80
    pEp_identity *stored_identity;
krista@1224
    81
    pEp_identity* temp_id = NULL;
vb@0
    82
    PEP_STATUS status;
vb@0
    83
vb@0
    84
    assert(session);
vb@0
    85
    assert(identity);
Edouard@439
    86
    assert(!EMPTYSTR(identity->address));
vb@0
    87
Edouard@439
    88
    if (!(session && identity && !EMPTYSTR(identity->address)))
vb@191
    89
        return PEP_ILLEGAL_VALUE;
vb@191
    90
vb@1078
    91
    if (identity->me || (identity->user_id && strcmp(identity->user_id, PEP_OWN_USERID) == 0)) {
vb@1078
    92
        identity->me = true;
vb@1078
    93
        return myself(session, identity);
vb@1078
    94
    }
vb@1078
    95
Edouard@559
    96
    int _no_user_id = EMPTYSTR(identity->user_id);
edouard@1164
    97
    int _did_elect_new_key = 0;
Edouard@559
    98
Edouard@559
    99
    if (_no_user_id)
Edouard@559
   100
    {
vb@1015
   101
        status = get_identity(session, identity->address, PEP_OWN_USERID,
vb@1015
   102
                &stored_identity);
vb@1015
   103
        if (status == PEP_STATUS_OK) {
vb@1015
   104
            free_identity(stored_identity);
vb@1015
   105
            return myself(session, identity);
vb@1015
   106
        }
vb@1015
   107
Edouard@559
   108
        free(identity->user_id);
Edouard@559
   109
vb@591
   110
        identity->user_id = calloc(1, strlen(identity->address) + 6);
Edouard@562
   111
        if (!identity->user_id)
Edouard@562
   112
        {
Edouard@562
   113
            return PEP_OUT_OF_MEMORY;
Edouard@562
   114
        }
vb@983
   115
        snprintf(identity->user_id, strlen(identity->address) + 6,
Edouard@562
   116
                 "TOFU_%s", identity->address);
Edouard@559
   117
    }
vb@934
   118
 
Edouard@559
   119
    status = get_identity(session,
Edouard@559
   120
                          identity->address,
Edouard@559
   121
                          identity->user_id,
Edouard@559
   122
                          &stored_identity);
Edouard@559
   123
    
vb@0
   124
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   125
    if (status == PEP_OUT_OF_MEMORY)
Edouard@829
   126
        goto exit_free;
vb@0
   127
krista@1188
   128
    /* We elect a pubkey first in case there's no acceptable stored fpr */
krista@1224
   129
    temp_id = identity_dup(identity);
krista@1220
   130
    
krista@1220
   131
    status = elect_pubkey(session, temp_id);
krista@1185
   132
    if (status != PEP_STATUS_OK)
krista@1185
   133
        goto exit_free;
krista@1188
   134
        
vb@14
   135
    if (stored_identity) {
vb@14
   136
        PEP_comm_type _comm_type_key;
krista@1188
   137
        
krista@1255
   138
        bool dont_use_fpr = true;
krista@1220
   139
krista@1220
   140
        /* if we have a stored_identity fpr */
krista@1342
   141
        if (!EMPTYSTR(stored_identity->fpr) && !EMPTYSTR(temp_id->fpr)) {
krista@1220
   142
            status = blacklist_is_listed(session, stored_identity->fpr, &dont_use_fpr);
krista@1220
   143
            if (status != PEP_STATUS_OK)
krista@1220
   144
                dont_use_fpr = true; 
krista@1220
   145
        }
krista@1188
   146
            
krista@1220
   147
krista@1220
   148
        if (!dont_use_fpr) {
krista@1220
   149
            free(temp_id->fpr);
krista@1220
   150
            temp_id->fpr = strdup(stored_identity->fpr);
krista@1220
   151
            assert(temp_id->fpr);
krista@1220
   152
            if (temp_id->fpr == NULL) {
krista@1220
   153
                status = PEP_OUT_OF_MEMORY;
krista@1188
   154
                goto exit_free;
krista@1220
   155
            }
krista@1220
   156
        }
krista@1220
   157
        else if (!EMPTYSTR(temp_id->fpr)) {
krista@1220
   158
            status = blacklist_is_listed(session, temp_id->fpr, &dont_use_fpr);
krista@1188
   159
            if (dont_use_fpr) {
krista@1220
   160
                free(temp_id->fpr);
krista@1220
   161
                temp_id->fpr = strdup("");
krista@1188
   162
            }
krista@1188
   163
            else {
krista@1188
   164
                _did_elect_new_key = 1;
krista@1188
   165
            }
krista@1188
   166
        }
krista@1188
   167
        else {
krista@1220
   168
            if (temp_id->fpr == NULL)
krista@1220
   169
                temp_id->fpr = strdup("");
krista@1188
   170
        }
krista@1188
   171
        
krista@1220
   172
        /* ok, from here on out, use temp_id */
krista@1220
   173
        
krista@1220
   174
        
krista@1220
   175
        /* At this point, we either have a non-blacklisted fpr we can work */
krista@1220
   176
        /* with, or we've got nada.                                        */        
krista@1220
   177
        if (!EMPTYSTR(temp_id->fpr)) {
krista@1220
   178
            status = get_key_rating(session, temp_id->fpr, &_comm_type_key);
krista@1188
   179
            assert(status != PEP_OUT_OF_MEMORY);
krista@1188
   180
            if (status == PEP_OUT_OF_MEMORY)
krista@1188
   181
                goto exit_free;
krista@1188
   182
            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
krista@1220
   183
                temp_id->comm_type = _comm_type_key;
krista@1188
   184
            } else{
krista@1220
   185
                temp_id->comm_type = stored_identity->comm_type;
krista@1220
   186
                if (temp_id->comm_type == PEP_ct_unknown) {
krista@1220
   187
                    temp_id->comm_type = _comm_type_key;
krista@1188
   188
                }
krista@1188
   189
            }
krista@1188
   190
        }
krista@1243
   191
        else {
krista@1243
   192
            /* Set comm_type accordingly */
krista@1243
   193
            temp_id->comm_type = PEP_ct_key_not_found;
krista@1243
   194
        }
krista@1243
   195
        
krista@1220
   196
        if (EMPTYSTR(temp_id->username)) {
krista@1220
   197
            free(temp_id->username);
krista@1220
   198
            temp_id->username = strdup(stored_identity->username);
krista@1220
   199
            assert(temp_id->username);
krista@1220
   200
            if (temp_id->username == NULL){
Edouard@829
   201
                status = PEP_OUT_OF_MEMORY;
Edouard@829
   202
                goto exit_free;
Edouard@829
   203
            }
vb@22
   204
        }
vb@22
   205
krista@1220
   206
        if (temp_id->lang[0] == 0) {
krista@1220
   207
            temp_id->lang[0] = stored_identity->lang[0];
krista@1220
   208
            temp_id->lang[1] = stored_identity->lang[1];
krista@1220
   209
            temp_id->lang[2] = 0;
vb@21
   210
        }
vb@932
   211
krista@1220
   212
        temp_id->flags = stored_identity->flags;
vb@21
   213
    }
vb@21
   214
    else /* stored_identity == NULL */ {
krista@1220
   215
        temp_id->flags = 0;
vb@934
   216
krista@1188
   217
        /* Work with the elected key from above */
krista@1220
   218
        if (!EMPTYSTR(temp_id->fpr)) {
krista@1196
   219
            
krista@1196
   220
            bool dont_use_fpr = true;
krista@1220
   221
            status = blacklist_is_listed(session, temp_id->fpr, &dont_use_fpr);
krista@1196
   222
            if (status != PEP_STATUS_OK)
krista@1196
   223
                dont_use_fpr = true; 
vb@21
   224
krista@1196
   225
            if (!dont_use_fpr) {
krista@1196
   226
                PEP_comm_type _comm_type_key;
vb@21
   227
krista@1220
   228
                status = get_key_rating(session, temp_id->fpr, &_comm_type_key);
krista@1196
   229
                assert(status != PEP_OUT_OF_MEMORY);
krista@1196
   230
                if (status == PEP_OUT_OF_MEMORY)
krista@1196
   231
                    goto exit_free;
krista@1196
   232
krista@1220
   233
                temp_id->comm_type = _comm_type_key;
krista@1196
   234
            }
krista@1196
   235
            else {
krista@1220
   236
                free(temp_id->fpr);
krista@1220
   237
                temp_id->fpr = strdup("");
krista@1196
   238
            }
vb@21
   239
        }
vb@21
   240
    }
vb@21
   241
krista@1220
   242
    if (temp_id->fpr == NULL)
krista@1220
   243
        temp_id->fpr = strdup("");
krista@1193
   244
    
krista@1193
   245
    
vb@21
   246
    status = PEP_STATUS_OK;
vb@21
   247
krista@1220
   248
    if (temp_id->comm_type != PEP_ct_unknown && !EMPTYSTR(temp_id->user_id)) {
krista@1220
   249
        assert(!EMPTYSTR(temp_id->username)); // this should not happen
vb@22
   250
krista@1220
   251
        if (EMPTYSTR(temp_id->username)) { // mitigate
krista@1220
   252
            free(temp_id->username);
krista@1220
   253
            temp_id->username = strdup("anonymous");
krista@1220
   254
            assert(temp_id->username);
krista@1220
   255
            if (temp_id->username == NULL){
Edouard@829
   256
                status = PEP_OUT_OF_MEMORY;
Edouard@829
   257
                goto exit_free;
Edouard@829
   258
            }
vb@0
   259
        }
vb@8
   260
Edouard@755
   261
        // Identity doesn't get stored if call was just about checking existing
Edouard@755
   262
        // user by address (i.e. no user id given but already stored)
krista@1223
   263
        if (!(_no_user_id && stored_identity) || _did_elect_new_key)
Edouard@559
   264
        {
krista@1220
   265
            status = set_identity(session, temp_id);
Edouard@559
   266
            assert(status == PEP_STATUS_OK);
Edouard@559
   267
            if (status != PEP_STATUS_OK) {
Edouard@829
   268
                goto exit_free;
Edouard@559
   269
            }
Edouard@499
   270
        }
vb@8
   271
    }
vb@8
   272
krista@1220
   273
    if (temp_id->comm_type != PEP_ct_compromized &&
krista@1220
   274
            temp_id->comm_type < PEP_ct_strong_but_unconfirmed)
vb@297
   275
        if (session->examine_identity)
krista@1220
   276
            session->examine_identity(temp_id, session->examine_management);
krista@1220
   277
    
krista@1220
   278
    /* ok, we got to the end. So we can assign the output identity */
krista@1220
   279
    free(identity->address);
krista@1220
   280
    identity->address = strdup(temp_id->address);
krista@1220
   281
    free(identity->fpr);
krista@1220
   282
    identity->fpr = strdup(temp_id->fpr);
krista@1220
   283
    free(identity->user_id);
krista@1220
   284
    identity->user_id = strdup(temp_id->user_id);
krista@1220
   285
    free(identity->username);
krista@1220
   286
    identity->username = strdup(temp_id->username);
krista@1220
   287
    identity->comm_type = temp_id->comm_type;
krista@1220
   288
    identity->lang[0] = temp_id->lang[0];
krista@1220
   289
    identity->lang[1] = temp_id->lang[1];
krista@1220
   290
    identity->lang[2] = 0;
krista@1220
   291
    identity->me = temp_id->me;
krista@1220
   292
    identity->flags = temp_id->flags;
vb@297
   293
Edouard@829
   294
exit_free :
Edouard@829
   295
    
Edouard@829
   296
    if (stored_identity){
Edouard@829
   297
        free_identity(stored_identity);
Edouard@829
   298
    }
Edouard@829
   299
krista@1220
   300
    if (temp_id)
krista@1220
   301
        free_identity(temp_id);
krista@1220
   302
    
vb@8
   303
    return status;
vb@0
   304
}
vb@0
   305
Edouard@774
   306
PEP_STATUS elect_ownkey(
krista@1194
   307
        PEP_SESSION session, pEp_identity * identity
Edouard@774
   308
    )
Edouard@774
   309
{
Edouard@774
   310
    PEP_STATUS status;
Edouard@774
   311
    stringlist_t *keylist = NULL;
Edouard@774
   312
Edouard@774
   313
    free(identity->fpr);
Edouard@774
   314
    identity->fpr = NULL;
Edouard@774
   315
krista@1357
   316
    status = find_private_keys(session, identity->address, &keylist);
Edouard@774
   317
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   318
    if (status == PEP_OUT_OF_MEMORY)
Edouard@774
   319
        return PEP_OUT_OF_MEMORY;
Edouard@774
   320
    
Edouard@774
   321
    if (keylist != NULL && keylist->value != NULL)
Edouard@774
   322
    {
Edouard@774
   323
        char *_fpr = NULL;
Edouard@774
   324
        identity->comm_type = PEP_ct_unknown;
Edouard@774
   325
Edouard@774
   326
        stringlist_t *_keylist;
Edouard@774
   327
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@774
   328
            bool is_own = false;
Edouard@774
   329
            
Edouard@774
   330
            if (session->use_only_own_private_keys)
Edouard@774
   331
            {
Edouard@774
   332
                status = own_key_is_listed(session, _keylist->value, &is_own);
Edouard@774
   333
                assert(status == PEP_STATUS_OK);
Edouard@774
   334
                if (status != PEP_STATUS_OK) {
Edouard@774
   335
                    free_stringlist(keylist);
Edouard@774
   336
                    return status;
Edouard@774
   337
                }
Edouard@774
   338
            }
Edouard@774
   339
Edouard@774
   340
            // TODO : also accept synchronized device group keys ?
Edouard@774
   341
            
Edouard@774
   342
            if (!session->use_only_own_private_keys || is_own)
Edouard@774
   343
            {
Edouard@774
   344
                PEP_comm_type _comm_type_key;
Edouard@774
   345
                
Edouard@774
   346
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@774
   347
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   348
                if (status == PEP_OUT_OF_MEMORY) {
Edouard@774
   349
                    free_stringlist(keylist);
Edouard@774
   350
                    return PEP_OUT_OF_MEMORY;
Edouard@774
   351
                }
Edouard@774
   352
                
Edouard@774
   353
                if (_comm_type_key != PEP_ct_compromized &&
Edouard@774
   354
                    _comm_type_key != PEP_ct_unknown)
Edouard@774
   355
                {
Edouard@774
   356
                    if (identity->comm_type == PEP_ct_unknown ||
Edouard@774
   357
                        _comm_type_key > identity->comm_type)
Edouard@774
   358
                    {
Edouard@774
   359
                        identity->comm_type = _comm_type_key;
Edouard@774
   360
                        _fpr = _keylist->value;
Edouard@774
   361
                    }
Edouard@774
   362
                }
Edouard@774
   363
            }
Edouard@774
   364
        }
Edouard@774
   365
        
Edouard@774
   366
        if (_fpr)
Edouard@774
   367
        {
Edouard@774
   368
            identity->fpr = strdup(_fpr);
Edouard@774
   369
            assert(identity->fpr);
Edouard@774
   370
            if (identity->fpr == NULL)
Edouard@774
   371
            {
Edouard@774
   372
                free_stringlist(keylist);
Edouard@774
   373
                return PEP_OUT_OF_MEMORY;
Edouard@774
   374
            }
Edouard@774
   375
        }
Edouard@774
   376
        free_stringlist(keylist);
Edouard@774
   377
    }
Edouard@774
   378
    return PEP_STATUS_OK;
Edouard@774
   379
}
Edouard@774
   380
krista@1357
   381
PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
krista@1357
   382
                                bool* is_usable) {
krista@1357
   383
    
krista@1357
   384
    bool dont_use_fpr = true;
krista@1357
   385
    
krista@1357
   386
    PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
krista@1357
   387
    if (!dont_use_fpr) {
krista@1357
   388
        // Make sure there is a *private* key associated with this fpr
krista@1357
   389
        bool has_private = false;
krista@1357
   390
        status = contains_priv_key(session, fpr, &has_private);
krista@1357
   391
        // TODO: check status
krista@1357
   392
        dont_use_fpr = !has_private;
krista@1357
   393
    }
krista@1357
   394
    
krista@1357
   395
    *is_usable = !dont_use_fpr;
krista@1357
   396
    
krista@1357
   397
    return status;
krista@1357
   398
}
krista@1357
   399
vb@0
   400
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
vb@0
   401
{
Edouard@560
   402
    pEp_identity *stored_identity;
vb@0
   403
    PEP_STATUS status;
vb@0
   404
vb@0
   405
    assert(session);
vb@0
   406
    assert(identity);
vb@1044
   407
    assert(!EMPTYSTR(identity->address));
vb@1043
   408
Edouard@658
   409
    assert(EMPTYSTR(identity->user_id) ||
Edouard@658
   410
           strcmp(identity->user_id, PEP_OWN_USERID) == 0);
vb@0
   411
vb@1044
   412
    if (!(session && identity && !EMPTYSTR(identity->address) &&
vb@1043
   413
            (EMPTYSTR(identity->user_id) ||
vb@1043
   414
            strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
vb@191
   415
        return PEP_ILLEGAL_VALUE;
vb@191
   416
vb@0
   417
    identity->comm_type = PEP_ct_pEp;
vb@0
   418
    identity->me = true;
Edouard@658
   419
    
vb@1043
   420
    if (EMPTYSTR(identity->user_id))
Edouard@658
   421
    {
Edouard@658
   422
        free(identity->user_id);
Edouard@658
   423
        identity->user_id = strdup(PEP_OWN_USERID);
Edouard@658
   424
        assert(identity->user_id);
Edouard@658
   425
        if (identity->user_id == NULL)
Edouard@658
   426
            return PEP_OUT_OF_MEMORY;
Edouard@658
   427
    }
vb@0
   428
vb@215
   429
    DEBUG_LOG("myself", "debug", identity->address);
vb@1044
   430
 
Edouard@560
   431
    status = get_identity(session,
Edouard@560
   432
                          identity->address,
Edouard@560
   433
                          identity->user_id,
Edouard@560
   434
                          &stored_identity);
Edouard@560
   435
    
vb@0
   436
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   437
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   438
        return PEP_OUT_OF_MEMORY;
krista@1357
   439
krista@1357
   440
    bool dont_use_stored_fpr = true;
krista@1357
   441
    bool dont_use_input_fpr = true;
Edouard@560
   442
    
Edouard@560
   443
    if (stored_identity)
Edouard@560
   444
    {
Edouard@560
   445
        if (EMPTYSTR(identity->fpr)) {
krista@1352
   446
            
krista@1357
   447
            bool has_private = false;
krista@1352
   448
            
krista@1357
   449
            status = _has_usable_priv_key(session, stored_identity->fpr, &has_private); 
krista@1352
   450
            
krista@1357
   451
            if (has_private) {
krista@1357
   452
                identity->fpr = strdup(stored_identity->fpr);
krista@1357
   453
                assert(identity->fpr);
krista@1357
   454
                if (identity->fpr == NULL)
krista@1357
   455
                {
krista@1357
   456
                    return PEP_OUT_OF_MEMORY;
krista@1357
   457
                }
krista@1357
   458
                dont_use_stored_fpr = false;
Edouard@560
   459
            }
Edouard@560
   460
        }
krista@1357
   461
        
vb@932
   462
        identity->flags = stored_identity->flags;
Edouard@588
   463
    }
krista@1357
   464
    
krista@1357
   465
    if (dont_use_stored_fpr && !EMPTYSTR(identity->fpr))
Edouard@588
   466
    {
Edouard@588
   467
        // App must have a good reason to give fpr, such as explicit
Edouard@588
   468
        // import of private key, or similar.
Edouard@588
   469
Edouard@658
   470
        // Take given fpr as-is.
vb@934
   471
krista@1357
   472
        // BUT:
krista@1357
   473
        // First check to see if it's blacklisted or private part is missing?
krista@1357
   474
        bool has_private = false;
krista@1357
   475
        
krista@1357
   476
        status = _has_usable_priv_key(session, identity->fpr, &has_private); 
krista@1357
   477
        
krista@1357
   478
        if (has_private) {
krista@1357
   479
            identity->flags = 0;
krista@1357
   480
            dont_use_input_fpr = false;
krista@1357
   481
        }
Edouard@560
   482
    }
krista@1357
   483
    
krista@1357
   484
    // Ok, we failed to get keys either way, so let's elect one.
krista@1357
   485
    if (dont_use_input_fpr && dont_use_stored_fpr)
Edouard@560
   486
    {
Edouard@774
   487
        status = elect_ownkey(session, identity);
Edouard@774
   488
        assert(status == PEP_STATUS_OK);
Edouard@774
   489
        if (status != PEP_STATUS_OK) {
Edouard@774
   490
            return status;
Edouard@560
   491
        }
krista@1357
   492
        
krista@1357
   493
        // Check to see if it's blacklisted or private part is missing
krista@1357
   494
        bool has_private = false;
krista@1357
   495
        
krista@1357
   496
        status = _has_usable_priv_key(session, identity->fpr, &has_private); 
krista@1357
   497
        
krista@1357
   498
        if (has_private) {
krista@1357
   499
            identity->flags = 0;
krista@1357
   500
            dont_use_input_fpr = false;
krista@1357
   501
        }
krista@1357
   502
        else { // OK, we've tried everything. Time to generate new keys.
krista@1357
   503
            
krista@1357
   504
        }
krista@1357
   505
        
Edouard@560
   506
    }
Edouard@695
   507
Edouard@695
   508
    bool revoked = false;
Edouard@695
   509
    char *r_fpr = NULL;
Edouard@695
   510
    if (!EMPTYSTR(identity->fpr))
Edouard@695
   511
    {
Edouard@695
   512
        status = key_revoked(session, identity->fpr, &revoked);
Edouard@775
   513
Edouard@775
   514
        // Forces re-election if key is missing and own-key-only not forced
Edouard@775
   515
        if (!session->use_only_own_private_keys && status == PEP_KEY_NOT_FOUND) 
Edouard@775
   516
        {
Edouard@775
   517
            status = elect_ownkey(session, identity);
Edouard@775
   518
            assert(status == PEP_STATUS_OK);
Edouard@775
   519
            if (status != PEP_STATUS_OK) {
Edouard@775
   520
                return status;
Edouard@775
   521
            }
Edouard@775
   522
        } 
Edouard@775
   523
        else if (status != PEP_STATUS_OK) 
Edouard@775
   524
        {
Edouard@695
   525
            return status;
Edouard@695
   526
        }
Edouard@695
   527
    }
edouard@1140
   528
   
edouard@1140
   529
    bool new_key_generated = false;
edouard@1140
   530
Edouard@695
   531
    if (EMPTYSTR(identity->fpr) || revoked)
Edouard@695
   532
    {        
Edouard@695
   533
        if(revoked)
Edouard@695
   534
        {
Edouard@697
   535
            r_fpr = identity->fpr;
Edouard@697
   536
            identity->fpr = NULL;
Edouard@695
   537
        }
Edouard@560
   538
        
vb@215
   539
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   540
        status = generate_keypair(session, identity);
vb@0
   541
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   542
        if (status != PEP_STATUS_OK) {
vb@0
   543
            char buf[11];
vb@0
   544
            snprintf(buf, 11, "%d", status);
vb@215
   545
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   546
            if(revoked && r_fpr)
Edouard@695
   547
                free(r_fpr);
vb@0
   548
            return status;
vb@0
   549
        }
edouard@1140
   550
edouard@1140
   551
        new_key_generated = true;
Edouard@560
   552
        
Edouard@695
   553
        if(revoked)
Edouard@695
   554
        {
Edouard@695
   555
            status = set_revoked(session, r_fpr,
Edouard@695
   556
                                 identity->fpr, time(NULL));
Edouard@695
   557
            free(r_fpr);
Edouard@695
   558
            if (status != PEP_STATUS_OK) {
Edouard@695
   559
                return status;
Edouard@695
   560
            }
Edouard@588
   561
        }
vb@0
   562
    }
Edouard@560
   563
    else
Edouard@560
   564
    {
vb@214
   565
        bool expired;
Edouard@701
   566
        status = key_expired(session, identity->fpr, 
Edouard@701
   567
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   568
                             &expired);
Edouard@701
   569
vb@214
   570
        assert(status == PEP_STATUS_OK);
Edouard@499
   571
        if (status != PEP_STATUS_OK) {
Edouard@588
   572
            return status;
Edouard@499
   573
        }
vb@214
   574
vb@214
   575
        if (status == PEP_STATUS_OK && expired) {
vb@214
   576
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   577
            renew_key(session, identity->fpr, ts);
vb@214
   578
            free_timestamp(ts);
vb@214
   579
        }
vb@214
   580
    }
vb@0
   581
vb@0
   582
    status = set_identity(session, identity);
vb@0
   583
    assert(status == PEP_STATUS_OK);
Edouard@499
   584
    if (status != PEP_STATUS_OK) {
Edouard@588
   585
        return status;
Edouard@499
   586
    }
vb@0
   587
edouard@1145
   588
    if(new_key_generated)
edouard@1145
   589
    {
edouard@1145
   590
        // if a state machine for keysync is in place, inject notify
edouard@1195
   591
        status = inject_DeviceState_event(session, KeyGen, NULL, NULL);
edouard@1299
   592
        if (status == PEP_OUT_OF_MEMORY){
edouard@1299
   593
            return PEP_OUT_OF_MEMORY;
edouard@1299
   594
        }
edouard@1145
   595
    }
edouard@1140
   596
vb@0
   597
    return PEP_STATUS_OK;
Edouard@499
   598
vb@0
   599
}
vb@0
   600
vb@296
   601
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   602
        PEP_SESSION session, 
vb@292
   603
        examine_identity_t examine_identity,
vb@292
   604
        void *management
vb@292
   605
    )
vb@292
   606
{
vb@292
   607
    assert(session);
vb@292
   608
    if (!session)
vb@292
   609
        return PEP_ILLEGAL_VALUE;
vb@292
   610
vb@292
   611
    session->examine_management = management;
vb@292
   612
    session->examine_identity = examine_identity;
vb@292
   613
vb@292
   614
    return PEP_STATUS_OK;
vb@292
   615
}
vb@292
   616
vb@0
   617
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   618
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   619
        void *management
vb@0
   620
    )
vb@0
   621
{
vb@0
   622
    PEP_SESSION session;
vb@0
   623
    pEp_identity *identity;
Edouard@499
   624
    PEP_STATUS status;
vb@0
   625
Edouard@499
   626
    assert(retrieve_next_identity);
Edouard@499
   627
    assert(management);
Edouard@499
   628
Edouard@499
   629
    if (!retrieve_next_identity || !management)
Edouard@499
   630
        return PEP_ILLEGAL_VALUE;
Edouard@499
   631
Edouard@499
   632
    status = init(&session);
vb@0
   633
    assert(status == PEP_STATUS_OK);
vb@0
   634
    if (status != PEP_STATUS_OK)
vb@0
   635
        return status;
vb@0
   636
vb@0
   637
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   638
Edouard@499
   639
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   640
    {
vb@0
   641
        assert(identity->address);
Edouard@499
   642
        if(identity->address)
Edouard@499
   643
        {
Edouard@499
   644
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   645
Edouard@499
   646
            if (identity->me) {
Edouard@499
   647
                status = myself(session, identity);
Edouard@499
   648
            } else {
Edouard@499
   649
                status = recv_key(session, identity->address);
Edouard@499
   650
            }
Edouard@499
   651
vb@0
   652
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   653
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   654
                return PEP_OUT_OF_MEMORY;
vb@0
   655
        }
vb@0
   656
        free_identity(identity);
vb@0
   657
    }
vb@0
   658
vb@0
   659
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   660
vb@0
   661
    release(session);
vb@0
   662
    return PEP_STATUS_OK;
vb@0
   663
}
vb@0
   664
krista@1213
   665
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
   666
        PEP_SESSION session,
vb@357
   667
        pEp_identity *ident
vb@357
   668
    )
vb@215
   669
{
vb@218
   670
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   671
vb@215
   672
    assert(session);
vb@357
   673
    assert(ident);
Edouard@439
   674
    assert(!EMPTYSTR(ident->fpr));
vb@215
   675
vb@357
   676
    if (!(session && ident && ident->fpr))
vb@215
   677
        return PEP_ILLEGAL_VALUE;
vb@215
   678
vb@357
   679
    if (ident->me)
Edouard@697
   680
    {
vb@357
   681
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   682
        myself(session, ident);
Edouard@697
   683
    }
Edouard@697
   684
    else
Edouard@697
   685
    {
Edouard@697
   686
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
   687
    }
vb@218
   688
vb@218
   689
    return status;
vb@215
   690
}
vb@215
   691
Edouard@410
   692
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   693
        PEP_SESSION session,
Edouard@410
   694
        pEp_identity *ident
Edouard@410
   695
    )
Edouard@410
   696
{
Edouard@410
   697
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   698
Edouard@410
   699
    assert(session);
Edouard@410
   700
    assert(ident);
vb@420
   701
    assert(!ident->me);
Edouard@439
   702
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   703
    assert(!EMPTYSTR(ident->address));
Edouard@439
   704
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   705
vb@420
   706
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   707
            ident->user_id))
Edouard@410
   708
        return PEP_ILLEGAL_VALUE;
Edouard@410
   709
vb@420
   710
    status = update_identity(session, ident);
vb@420
   711
    if (status != PEP_STATUS_OK)
vb@420
   712
        return status;
Edouard@410
   713
Edouard@442
   714
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   715
        ident->comm_type = PEP_ct_unknown;
vb@421
   716
    else
vb@421
   717
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   718
vb@420
   719
    status = set_identity(session, ident);
vb@421
   720
    if (status != PEP_STATUS_OK)
vb@421
   721
        return status;
vb@421
   722
vb@422
   723
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   724
        status = update_identity(session, ident);
Edouard@410
   725
    return status;
Edouard@410
   726
}
Edouard@410
   727
vb@354
   728
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   729
        PEP_SESSION session,
vb@354
   730
        pEp_identity *ident
vb@354
   731
    )
vb@354
   732
{
vb@354
   733
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   734
vb@354
   735
    assert(session);
vb@354
   736
    assert(ident);
Edouard@439
   737
    assert(!EMPTYSTR(ident->address));
Edouard@439
   738
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   739
    assert(!EMPTYSTR(ident->fpr));
vb@354
   740
    assert(!ident->me);
vb@354
   741
Edouard@439
   742
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   743
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   744
        return PEP_ILLEGAL_VALUE;
vb@354
   745
vb@354
   746
    status = update_identity(session, ident);
vb@354
   747
    if (status != PEP_STATUS_OK)
vb@354
   748
        return status;
vb@354
   749
vb@356
   750
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   751
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   752
        status = set_identity(session, ident);
vb@356
   753
    }
vb@356
   754
    else {
vb@356
   755
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   756
        status = PEP_CANNOT_SET_TRUST;
vb@356
   757
    }
vb@354
   758
vb@354
   759
    return status;
vb@354
   760
}
vb@354
   761
Edouard@584
   762
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
   763
        PEP_SESSION session,
vb@955
   764
        const char *fpr,
vb@955
   765
        bool *listed
vb@955
   766
    )
Edouard@584
   767
{
Edouard@584
   768
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   769
    int count;
Edouard@584
   770
    
Edouard@584
   771
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   772
    
Edouard@584
   773
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   774
        return PEP_ILLEGAL_VALUE;
Edouard@584
   775
    
Edouard@584
   776
    *listed = false;
Edouard@584
   777
    
Edouard@584
   778
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   779
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   780
    
Edouard@584
   781
    int result;
Edouard@584
   782
    
Edouard@584
   783
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   784
    switch (result) {
Edouard@584
   785
        case SQLITE_ROW:
Edouard@584
   786
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   787
            *listed = count > 0;
Edouard@584
   788
            status = PEP_STATUS_OK;
Edouard@584
   789
            break;
Edouard@584
   790
            
Edouard@584
   791
        default:
Edouard@584
   792
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   793
    }
Edouard@584
   794
    
Edouard@584
   795
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   796
    return status;
Edouard@584
   797
}
Edouard@584
   798
vb@955
   799
DYNAMIC_API PEP_STATUS own_identities_retrieve(
vb@955
   800
        PEP_SESSION session,
vb@955
   801
        identity_list **own_identities
vb@955
   802
      )
Edouard@584
   803
{
Edouard@584
   804
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   805
    
vb@955
   806
    assert(session && own_identities);
vb@955
   807
    if (!(session && own_identities))
Edouard@584
   808
        return PEP_ILLEGAL_VALUE;
Edouard@584
   809
    
vb@955
   810
    *own_identities = NULL;
vb@955
   811
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
   812
    if (_own_identities == NULL)
Edouard@584
   813
        goto enomem;
Edouard@584
   814
    
vb@955
   815
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   816
    
Edouard@584
   817
    int result;
vb@955
   818
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
   819
    const char *address = NULL;
Edouard@584
   820
    const char *fpr = NULL;
vb@955
   821
    const char *username = NULL;
vb@955
   822
    const char *user_id = NULL;
vb@955
   823
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
   824
    const char *lang = NULL;
vb@955
   825
    unsigned int flags = 0;
Edouard@584
   826
    
vb@955
   827
    identity_list *_bl = _own_identities;
Edouard@584
   828
    do {
vb@955
   829
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
   830
        switch (result) {
Edouard@584
   831
            case SQLITE_ROW:
vb@955
   832
                address = (const char *)
vb@955
   833
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
   834
                fpr = (const char *)
vb@955
   835
                    sqlite3_column_text(session->own_identities_retrieve, 1);
vb@1071
   836
                user_id = PEP_OWN_USERID;
vb@1071
   837
                username = (const char *)
vb@1069
   838
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
   839
                comm_type = PEP_ct_pEp;
vb@1071
   840
                lang = (const char *)
vb@955
   841
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
   842
                flags = (unsigned int)
vb@955
   843
                    sqlite3_column_int(session->own_key_is_listed, 4);
vb@955
   844
vb@1078
   845
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
   846
                if (!ident)
Edouard@584
   847
                    goto enomem;
vb@955
   848
                ident->comm_type = comm_type;
vb@955
   849
                if (lang && lang[0]) {
vb@955
   850
                    ident->lang[0] = lang[0];
vb@955
   851
                    ident->lang[1] = lang[1];
vb@1078
   852
                    ident->lang[2] = 0;
vb@955
   853
                }
vb@1068
   854
                ident->me = true;
vb@955
   855
                ident->flags = flags;
vb@955
   856
vb@955
   857
                _bl = identity_list_add(_bl, ident);
vb@1080
   858
                if (_bl == NULL) {
vb@1080
   859
                    free_identity(ident);
vb@983
   860
                    goto enomem;
vb@1080
   861
                }
Edouard@584
   862
                
Edouard@584
   863
                break;
Edouard@584
   864
                
Edouard@584
   865
            case SQLITE_DONE:
Edouard@584
   866
                break;
Edouard@584
   867
                
Edouard@584
   868
            default:
Edouard@584
   869
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   870
                result = SQLITE_DONE;
Edouard@584
   871
        }
Edouard@584
   872
    } while (result != SQLITE_DONE);
Edouard@584
   873
    
vb@955
   874
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   875
    if (status == PEP_STATUS_OK)
vb@955
   876
        *own_identities = _own_identities;
Edouard@584
   877
    else
vb@955
   878
        free_identity_list(_own_identities);
Edouard@584
   879
    
Edouard@584
   880
    goto the_end;
Edouard@584
   881
    
Edouard@584
   882
enomem:
vb@955
   883
    free_identity_list(_own_identities);
Edouard@584
   884
    status = PEP_OUT_OF_MEMORY;
Edouard@584
   885
    
Edouard@584
   886
the_end:
Edouard@584
   887
    return status;
Edouard@584
   888
}
vb@955
   889
krista@1357
   890
krista@1357
   891
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
   892
                             bool *has_private) {
krista@1357
   893
krista@1357
   894
    assert(session);
krista@1357
   895
    assert(fpr);
krista@1357
   896
    assert(has_private);
krista@1357
   897
    
krista@1357
   898
    if (!(session && fpr && has_private))
krista@1357
   899
        return PEP_ILLEGAL_VALUE;
krista@1357
   900
krista@1357
   901
    return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
krista@1357
   902
}