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