src/keymanagement.c
author Krista Grothoff <krista@pep-project.org>
Tue, 08 Nov 2016 00:34:53 +0100
branchENGINE-84
changeset 1359 e63926f8b166
parent 1358 ff3053a671dc
child 1371 e2423e1df7e7
permissions -rw-r--r--
ENGINE-84: #comment now generating new keys when no usable private key is available, and not electing/selecting/attempting to use own keys as private keys where private half of the keypair is blacklisted, revoked, or missing. TODO: fix status TODOs, test more before merging back. But it's working, in general.
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@559
   112
        {
Edouard@562
   113
            return PEP_OUT_OF_MEMORY;
Edouard@559
   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; 
krista@1196
   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;
vb@21
   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;
krista@1359
   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@1359
   492
krista@1357
   493
        bool has_private = false;
krista@1359
   494
        if (identity->fpr) {
krista@1359
   495
            // ok, we elected something.
krista@1359
   496
            // elect_ownkey only returns private keys, so we don't check again.
krista@1359
   497
            // Check to see if it's blacklisted
krista@1359
   498
            bool listed;
krista@1359
   499
            status = blacklist_is_listed(session, identity->fpr, &listed); 
krista@1359
   500
            // TODO: check status
krista@1359
   501
            has_private = !listed;
krista@1359
   502
        }
krista@1357
   503
        
krista@1357
   504
        if (has_private) {
krista@1357
   505
            identity->flags = 0;
krista@1357
   506
            dont_use_input_fpr = false;
krista@1357
   507
        }
krista@1357
   508
        else { // OK, we've tried everything. Time to generate new keys.
krista@1359
   509
            free(identity->fpr); // It can stay in this state (unallocated) because we'll generate a new key 
krista@1359
   510
            identity->fpr = NULL;
krista@1357
   511
        }
Edouard@560
   512
    }
Edouard@695
   513
Edouard@695
   514
    bool revoked = false;
Edouard@695
   515
    char *r_fpr = NULL;
Edouard@695
   516
    if (!EMPTYSTR(identity->fpr))
Edouard@695
   517
    {
Edouard@695
   518
        status = key_revoked(session, identity->fpr, &revoked);
Edouard@775
   519
Edouard@775
   520
        // Forces re-election if key is missing and own-key-only not forced
Edouard@775
   521
        if (!session->use_only_own_private_keys && status == PEP_KEY_NOT_FOUND) 
Edouard@775
   522
        {
Edouard@775
   523
            status = elect_ownkey(session, identity);
Edouard@775
   524
            assert(status == PEP_STATUS_OK);
Edouard@775
   525
            if (status != PEP_STATUS_OK) {
Edouard@775
   526
                return status;
Edouard@775
   527
            }
Edouard@775
   528
        } 
Edouard@775
   529
        else if (status != PEP_STATUS_OK) 
Edouard@775
   530
        {
Edouard@695
   531
            return status;
Edouard@695
   532
        }
Edouard@695
   533
    }
edouard@1140
   534
   
edouard@1140
   535
    bool new_key_generated = false;
edouard@1140
   536
Edouard@695
   537
    if (EMPTYSTR(identity->fpr) || revoked)
Edouard@695
   538
    {        
Edouard@695
   539
        if(revoked)
Edouard@695
   540
        {
Edouard@697
   541
            r_fpr = identity->fpr;
Edouard@697
   542
            identity->fpr = NULL;
Edouard@695
   543
        }
Edouard@560
   544
        
vb@215
   545
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   546
        status = generate_keypair(session, identity);
vb@0
   547
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   548
        if (status != PEP_STATUS_OK) {
vb@0
   549
            char buf[11];
vb@0
   550
            snprintf(buf, 11, "%d", status);
vb@215
   551
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   552
            if(revoked && r_fpr)
Edouard@695
   553
                free(r_fpr);
vb@0
   554
            return status;
vb@0
   555
        }
edouard@1140
   556
edouard@1140
   557
        new_key_generated = true;
Edouard@560
   558
        
Edouard@695
   559
        if(revoked)
Edouard@695
   560
        {
Edouard@695
   561
            status = set_revoked(session, r_fpr,
Edouard@695
   562
                                 identity->fpr, time(NULL));
Edouard@695
   563
            free(r_fpr);
Edouard@695
   564
            if (status != PEP_STATUS_OK) {
Edouard@695
   565
                return status;
Edouard@695
   566
            }
Edouard@371
   567
        }
vb@0
   568
    }
Edouard@560
   569
    else
Edouard@560
   570
    {
vb@214
   571
        bool expired;
Edouard@701
   572
        status = key_expired(session, identity->fpr, 
Edouard@701
   573
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   574
                             &expired);
Edouard@701
   575
vb@214
   576
        assert(status == PEP_STATUS_OK);
Edouard@499
   577
        if (status != PEP_STATUS_OK) {
Edouard@588
   578
            return status;
Edouard@499
   579
        }
vb@214
   580
vb@214
   581
        if (status == PEP_STATUS_OK && expired) {
vb@214
   582
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   583
            renew_key(session, identity->fpr, ts);
vb@214
   584
            free_timestamp(ts);
vb@214
   585
        }
vb@214
   586
    }
vb@0
   587
krista@1353
   588
    if (!identity->username)
krista@1353
   589
        identity->username = strdup("");
krista@1353
   590
    
vb@0
   591
    status = set_identity(session, identity);
vb@0
   592
    assert(status == PEP_STATUS_OK);
Edouard@499
   593
    if (status != PEP_STATUS_OK) {
Edouard@588
   594
        return status;
Edouard@499
   595
    }
vb@0
   596
edouard@1145
   597
    if(new_key_generated)
edouard@1145
   598
    {
edouard@1145
   599
        // if a state machine for keysync is in place, inject notify
edouard@1195
   600
        status = inject_DeviceState_event(session, KeyGen, NULL, NULL);
edouard@1299
   601
        if (status == PEP_OUT_OF_MEMORY){
edouard@1299
   602
            return PEP_OUT_OF_MEMORY;
edouard@1299
   603
        }
edouard@1145
   604
    }
edouard@1140
   605
vb@0
   606
    return PEP_STATUS_OK;
Edouard@499
   607
vb@0
   608
}
vb@0
   609
vb@296
   610
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   611
        PEP_SESSION session, 
vb@292
   612
        examine_identity_t examine_identity,
vb@292
   613
        void *management
vb@292
   614
    )
vb@292
   615
{
vb@292
   616
    assert(session);
vb@292
   617
    if (!session)
vb@292
   618
        return PEP_ILLEGAL_VALUE;
vb@292
   619
vb@292
   620
    session->examine_management = management;
vb@292
   621
    session->examine_identity = examine_identity;
vb@292
   622
vb@292
   623
    return PEP_STATUS_OK;
vb@292
   624
}
vb@292
   625
vb@0
   626
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   627
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   628
        void *management
vb@0
   629
    )
vb@0
   630
{
vb@0
   631
    PEP_SESSION session;
vb@0
   632
    pEp_identity *identity;
Edouard@499
   633
    PEP_STATUS status;
vb@0
   634
vb@24
   635
    assert(retrieve_next_identity);
vb@24
   636
    assert(management);
vb@24
   637
Edouard@499
   638
    if (!retrieve_next_identity || !management)
Edouard@499
   639
        return PEP_ILLEGAL_VALUE;
Edouard@499
   640
Edouard@499
   641
    status = init(&session);
Edouard@499
   642
    assert(status == PEP_STATUS_OK);
Edouard@499
   643
    if (status != PEP_STATUS_OK)
Edouard@499
   644
        return status;
Edouard@499
   645
vb@0
   646
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   647
Edouard@499
   648
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   649
    {
vb@0
   650
        assert(identity->address);
Edouard@499
   651
        if(identity->address)
Edouard@499
   652
        {
Edouard@499
   653
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   654
Edouard@499
   655
            if (identity->me) {
Edouard@499
   656
                status = myself(session, identity);
Edouard@499
   657
            } else {
Edouard@499
   658
                status = recv_key(session, identity->address);
Edouard@499
   659
            }
Edouard@499
   660
vb@0
   661
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   662
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   663
                return PEP_OUT_OF_MEMORY;
vb@0
   664
        }
vb@0
   665
        free_identity(identity);
vb@0
   666
    }
vb@0
   667
vb@0
   668
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   669
vb@0
   670
    release(session);
vb@0
   671
    return PEP_STATUS_OK;
vb@0
   672
}
vb@0
   673
krista@1213
   674
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
   675
        PEP_SESSION session,
vb@357
   676
        pEp_identity *ident
vb@357
   677
    )
vb@215
   678
{
vb@218
   679
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   680
vb@215
   681
    assert(session);
vb@357
   682
    assert(ident);
Edouard@439
   683
    assert(!EMPTYSTR(ident->fpr));
vb@215
   684
vb@357
   685
    if (!(session && ident && ident->fpr))
vb@215
   686
        return PEP_ILLEGAL_VALUE;
vb@215
   687
vb@357
   688
    if (ident->me)
Edouard@697
   689
    {
vb@357
   690
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   691
        myself(session, ident);
Edouard@697
   692
    }
Edouard@697
   693
    else
Edouard@697
   694
    {
Edouard@697
   695
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
   696
    }
vb@218
   697
vb@218
   698
    return status;
vb@215
   699
}
vb@215
   700
Edouard@410
   701
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   702
        PEP_SESSION session,
Edouard@410
   703
        pEp_identity *ident
Edouard@410
   704
    )
Edouard@410
   705
{
Edouard@410
   706
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   707
Edouard@410
   708
    assert(session);
Edouard@410
   709
    assert(ident);
vb@420
   710
    assert(!ident->me);
Edouard@439
   711
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   712
    assert(!EMPTYSTR(ident->address));
Edouard@439
   713
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   714
vb@420
   715
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   716
            ident->user_id))
Edouard@410
   717
        return PEP_ILLEGAL_VALUE;
Edouard@410
   718
vb@420
   719
    status = update_identity(session, ident);
vb@420
   720
    if (status != PEP_STATUS_OK)
vb@420
   721
        return status;
Edouard@410
   722
Edouard@442
   723
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   724
        ident->comm_type = PEP_ct_unknown;
vb@421
   725
    else
vb@421
   726
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   727
vb@420
   728
    status = set_identity(session, ident);
vb@421
   729
    if (status != PEP_STATUS_OK)
vb@421
   730
        return status;
vb@421
   731
vb@422
   732
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   733
        status = update_identity(session, ident);
Edouard@410
   734
    return status;
Edouard@410
   735
}
Edouard@410
   736
vb@354
   737
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   738
        PEP_SESSION session,
vb@354
   739
        pEp_identity *ident
vb@354
   740
    )
vb@354
   741
{
vb@354
   742
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   743
vb@354
   744
    assert(session);
vb@354
   745
    assert(ident);
Edouard@439
   746
    assert(!EMPTYSTR(ident->address));
Edouard@439
   747
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   748
    assert(!EMPTYSTR(ident->fpr));
vb@354
   749
    assert(!ident->me);
vb@354
   750
Edouard@439
   751
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   752
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   753
        return PEP_ILLEGAL_VALUE;
vb@354
   754
vb@354
   755
    status = update_identity(session, ident);
vb@354
   756
    if (status != PEP_STATUS_OK)
vb@354
   757
        return status;
vb@354
   758
vb@356
   759
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   760
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   761
        status = set_identity(session, ident);
vb@356
   762
    }
vb@356
   763
    else {
vb@356
   764
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   765
        status = PEP_CANNOT_SET_TRUST;
vb@356
   766
    }
vb@354
   767
vb@354
   768
    return status;
vb@354
   769
}
vb@354
   770
Edouard@584
   771
DYNAMIC_API PEP_STATUS own_key_is_listed(
vb@955
   772
        PEP_SESSION session,
vb@955
   773
        const char *fpr,
vb@955
   774
        bool *listed
vb@955
   775
    )
Edouard@584
   776
{
Edouard@584
   777
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   778
    int count;
Edouard@584
   779
    
Edouard@584
   780
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   781
    
Edouard@584
   782
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   783
        return PEP_ILLEGAL_VALUE;
Edouard@584
   784
    
Edouard@584
   785
    *listed = false;
Edouard@584
   786
    
Edouard@584
   787
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   788
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   789
    
Edouard@584
   790
    int result;
Edouard@584
   791
    
Edouard@584
   792
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   793
    switch (result) {
Edouard@584
   794
        case SQLITE_ROW:
Edouard@584
   795
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   796
            *listed = count > 0;
Edouard@584
   797
            status = PEP_STATUS_OK;
Edouard@584
   798
            break;
Edouard@584
   799
            
Edouard@584
   800
        default:
Edouard@584
   801
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   802
    }
Edouard@584
   803
    
Edouard@584
   804
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   805
    return status;
Edouard@584
   806
}
Edouard@584
   807
vb@955
   808
DYNAMIC_API PEP_STATUS own_identities_retrieve(
vb@955
   809
        PEP_SESSION session,
vb@955
   810
        identity_list **own_identities
vb@955
   811
      )
Edouard@584
   812
{
Edouard@584
   813
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   814
    
vb@955
   815
    assert(session && own_identities);
vb@955
   816
    if (!(session && own_identities))
Edouard@584
   817
        return PEP_ILLEGAL_VALUE;
Edouard@584
   818
    
vb@955
   819
    *own_identities = NULL;
vb@955
   820
    identity_list *_own_identities = new_identity_list(NULL);
vb@955
   821
    if (_own_identities == NULL)
Edouard@584
   822
        goto enomem;
Edouard@584
   823
    
vb@955
   824
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   825
    
Edouard@584
   826
    int result;
vb@955
   827
    // address, fpr, username, user_id, comm_type, lang, flags
vb@955
   828
    const char *address = NULL;
Edouard@584
   829
    const char *fpr = NULL;
vb@955
   830
    const char *username = NULL;
vb@955
   831
    const char *user_id = NULL;
vb@955
   832
    PEP_comm_type comm_type = PEP_ct_unknown;
vb@955
   833
    const char *lang = NULL;
vb@955
   834
    unsigned int flags = 0;
Edouard@584
   835
    
vb@955
   836
    identity_list *_bl = _own_identities;
Edouard@584
   837
    do {
vb@955
   838
        result = sqlite3_step(session->own_identities_retrieve);
Edouard@584
   839
        switch (result) {
Edouard@584
   840
            case SQLITE_ROW:
vb@955
   841
                address = (const char *)
vb@955
   842
                    sqlite3_column_text(session->own_identities_retrieve, 0);
vb@955
   843
                fpr = (const char *)
vb@955
   844
                    sqlite3_column_text(session->own_identities_retrieve, 1);
vb@1071
   845
                user_id = PEP_OWN_USERID;
vb@1071
   846
                username = (const char *)
vb@955
   847
                    sqlite3_column_text(session->own_identities_retrieve, 2);
vb@1071
   848
                comm_type = PEP_ct_pEp;
vb@1071
   849
                lang = (const char *)
vb@955
   850
                    sqlite3_column_text(session->own_identities_retrieve, 3);
vb@1071
   851
                flags = (unsigned int)
vb@955
   852
                    sqlite3_column_int(session->own_key_is_listed, 4);
vb@955
   853
vb@1078
   854
                pEp_identity *ident = new_identity(address, fpr, user_id, username);
vb@1079
   855
                if (!ident)
Edouard@584
   856
                    goto enomem;
vb@955
   857
                ident->comm_type = comm_type;
vb@955
   858
                if (lang && lang[0]) {
vb@955
   859
                    ident->lang[0] = lang[0];
vb@955
   860
                    ident->lang[1] = lang[1];
vb@1078
   861
                    ident->lang[2] = 0;
vb@955
   862
                }
vb@1068
   863
                ident->me = true;
vb@955
   864
                ident->flags = flags;
vb@955
   865
vb@955
   866
                _bl = identity_list_add(_bl, ident);
vb@1080
   867
                if (_bl == NULL) {
vb@1080
   868
                    free_identity(ident);
Edouard@584
   869
                    goto enomem;
vb@1080
   870
                }
Edouard@584
   871
                
Edouard@584
   872
                break;
Edouard@584
   873
                
Edouard@584
   874
            case SQLITE_DONE:
Edouard@584
   875
                break;
Edouard@584
   876
                
Edouard@584
   877
            default:
Edouard@584
   878
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   879
                result = SQLITE_DONE;
Edouard@584
   880
        }
Edouard@584
   881
    } while (result != SQLITE_DONE);
Edouard@584
   882
    
vb@955
   883
    sqlite3_reset(session->own_identities_retrieve);
Edouard@584
   884
    if (status == PEP_STATUS_OK)
vb@955
   885
        *own_identities = _own_identities;
Edouard@584
   886
    else
vb@955
   887
        free_identity_list(_own_identities);
Edouard@584
   888
    
Edouard@584
   889
    goto the_end;
Edouard@584
   890
    
Edouard@584
   891
enomem:
vb@955
   892
    free_identity_list(_own_identities);
Edouard@584
   893
    status = PEP_OUT_OF_MEMORY;
Edouard@584
   894
    
Edouard@584
   895
the_end:
Edouard@584
   896
    return status;
Edouard@584
   897
}
vb@955
   898
krista@1357
   899
krista@1357
   900
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
   901
                             bool *has_private) {
krista@1357
   902
krista@1357
   903
    assert(session);
krista@1357
   904
    assert(fpr);
krista@1357
   905
    assert(has_private);
krista@1357
   906
    
krista@1357
   907
    if (!(session && fpr && has_private))
krista@1357
   908
        return PEP_ILLEGAL_VALUE;
krista@1357
   909
krista@1357
   910
    return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
krista@1357
   911
}