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