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