src/keymanagement.c
author Edouard Tisserant
Wed, 29 Jun 2016 17:31:50 +0200
changeset 774 bc90d5bf74d5
parent 755 3bac36e6fb86
child 775 af16c9faedf2
permissions -rw-r--r--
Moved myself() own key election to a separate function
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@439
    12
#ifndef EMPTYSTR
roker@500
    13
#define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
vb@8
    14
#endif
vb@8
    15
vb@214
    16
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
vb@214
    17
Edouard@512
    18
// Space tolerant and case insensitive fingerprint string compare
Edouard@512
    19
static int _same_fpr(
Edouard@512
    20
        const char* fpra,
Edouard@512
    21
        size_t fpras,
Edouard@512
    22
        const char* fprb,
Edouard@512
    23
        size_t fprbs
Edouard@512
    24
    )
Edouard@512
    25
{
Edouard@512
    26
    size_t ai = 0;
Edouard@512
    27
    size_t bi = 0;
Edouard@512
    28
    
Edouard@544
    29
    do
Edouard@544
    30
    {
Edouard@544
    31
        if(fpra[ai] == 0 || fprb[bi] == 0)
Edouard@544
    32
        {
Edouard@512
    33
            return 0;
Edouard@544
    34
        }
Edouard@544
    35
        else if(fpra[ai] == ' ')
Edouard@544
    36
        {
Edouard@512
    37
            ai++;
Edouard@544
    38
        }
Edouard@544
    39
        else if(fprb[bi] == ' ')
Edouard@544
    40
        {
Edouard@512
    41
            bi++;
Edouard@544
    42
        }
Edouard@544
    43
        else if(toupper(fpra[ai]) == toupper(fprb[bi]))
Edouard@544
    44
        {
Edouard@512
    45
            ai++;
Edouard@512
    46
            bi++;
Edouard@512
    47
        }
Edouard@544
    48
        else
Edouard@544
    49
        {
Edouard@544
    50
            return 0;
Edouard@544
    51
        }
Edouard@544
    52
        
Edouard@544
    53
    }
Edouard@544
    54
    while(ai < fpras && bi < fprbs);
Edouard@512
    55
    
Edouard@512
    56
    return ai == fpras && bi == fprbs;
Edouard@512
    57
}
Edouard@512
    58
Edouard@774
    59
PEP_STATUS elect_pubkey(
Edouard@755
    60
        PEP_SESSION session, pEp_identity * identity
Edouard@755
    61
    )
Edouard@755
    62
{
Edouard@755
    63
    PEP_STATUS status;
Edouard@755
    64
    stringlist_t *keylist;
Edouard@755
    65
    char *_fpr = NULL;
Edouard@755
    66
    identity->comm_type = PEP_ct_unknown;
Edouard@755
    67
Edouard@755
    68
    status = find_keys(session, identity->address, &keylist);
Edouard@755
    69
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    70
    if (status == PEP_OUT_OF_MEMORY)
Edouard@755
    71
        return PEP_OUT_OF_MEMORY;
Edouard@755
    72
Edouard@755
    73
    stringlist_t *_keylist;
Edouard@755
    74
    for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@755
    75
        PEP_comm_type _comm_type_key;
Edouard@755
    76
Edouard@755
    77
        status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@755
    78
        assert(status != PEP_OUT_OF_MEMORY);
Edouard@755
    79
        if (status == PEP_OUT_OF_MEMORY) {
Edouard@755
    80
            free_stringlist(keylist);
Edouard@755
    81
            return PEP_OUT_OF_MEMORY;
Edouard@755
    82
        }
Edouard@755
    83
Edouard@755
    84
        if (_comm_type_key != PEP_ct_compromized &&
Edouard@755
    85
            _comm_type_key != PEP_ct_unknown)
Edouard@755
    86
        {
Edouard@755
    87
            if (identity->comm_type == PEP_ct_unknown ||
Edouard@755
    88
                _comm_type_key > identity->comm_type)
Edouard@755
    89
            {
Edouard@755
    90
                identity->comm_type = _comm_type_key;
Edouard@755
    91
                _fpr = _keylist->value;
Edouard@755
    92
            }
Edouard@755
    93
        }
Edouard@755
    94
    }
Edouard@755
    95
Edouard@755
    96
    if (_fpr) {
Edouard@755
    97
        free(identity->fpr);
Edouard@755
    98
Edouard@755
    99
        identity->fpr = strdup(_fpr);
Edouard@755
   100
        if (identity->fpr == NULL) {
Edouard@755
   101
            free_stringlist(keylist);
Edouard@755
   102
            return PEP_OUT_OF_MEMORY;
Edouard@755
   103
        }
Edouard@755
   104
    }
Edouard@755
   105
    free_stringlist(keylist);
Edouard@755
   106
    return PEP_STATUS_OK;
Edouard@755
   107
}
Edouard@755
   108
vb@0
   109
DYNAMIC_API PEP_STATUS update_identity(
vb@0
   110
        PEP_SESSION session, pEp_identity * identity
vb@0
   111
    )
vb@0
   112
{
vb@0
   113
    pEp_identity *stored_identity;
vb@0
   114
    PEP_STATUS status;
vb@0
   115
vb@0
   116
    assert(session);
vb@0
   117
    assert(identity);
Edouard@439
   118
    assert(!EMPTYSTR(identity->address));
vb@0
   119
Edouard@439
   120
    if (!(session && identity && !EMPTYSTR(identity->address)))
vb@191
   121
        return PEP_ILLEGAL_VALUE;
vb@191
   122
Edouard@559
   123
    int _no_user_id = EMPTYSTR(identity->user_id);
Edouard@559
   124
Edouard@559
   125
    if (_no_user_id)
Edouard@559
   126
    {
Edouard@559
   127
        free(identity->user_id);
Edouard@559
   128
vb@591
   129
        identity->user_id = calloc(1, strlen(identity->address) + 6);
Edouard@562
   130
        if (!identity->user_id)
Edouard@562
   131
        {
Edouard@562
   132
            return PEP_OUT_OF_MEMORY;
Edouard@562
   133
        }
vb@591
   134
        snprintf(identity->user_id, strlen(identity->address) + 5,
Edouard@562
   135
                 "TOFU_%s", identity->address);
Edouard@559
   136
    }
Edouard@559
   137
    
Edouard@559
   138
    status = get_identity(session,
Edouard@559
   139
                          identity->address,
Edouard@559
   140
                          identity->user_id,
Edouard@559
   141
                          &stored_identity);
Edouard@559
   142
    
vb@0
   143
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   144
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   145
        return PEP_OUT_OF_MEMORY;
vb@0
   146
vb@14
   147
    if (stored_identity) {
vb@14
   148
        PEP_comm_type _comm_type_key;
vb@14
   149
        status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
vb@14
   150
        assert(status != PEP_OUT_OF_MEMORY);
vb@14
   151
        if (status == PEP_OUT_OF_MEMORY)
vb@14
   152
            return PEP_OUT_OF_MEMORY;
vb@10
   153
Edouard@439
   154
        if (EMPTYSTR(identity->username)) {
vb@22
   155
            free(identity->username);
vb@591
   156
            identity->username = strdup(stored_identity->username);
Edouard@498
   157
            assert(identity->username);
vb@22
   158
            if (identity->username == NULL)
vb@22
   159
                return PEP_OUT_OF_MEMORY;
vb@22
   160
        }
vb@22
   161
Edouard@439
   162
        if (EMPTYSTR(identity->fpr)) {
vb@591
   163
            identity->fpr = strdup(stored_identity->fpr);
vb@8
   164
            assert(identity->fpr);
vb@8
   165
            if (identity->fpr == NULL)
vb@8
   166
                return PEP_OUT_OF_MEMORY;
vb@14
   167
            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
Edouard@774
   168
                PEP_STATUS status = elect_pubkey(session, identity);
Edouard@755
   169
                if (status != PEP_STATUS_OK)
Edouard@755
   170
                    return status;
vb@14
   171
            }
vb@14
   172
            else {
vb@14
   173
                identity->comm_type = stored_identity->comm_type;
vb@14
   174
            }
vb@8
   175
        }
Edouard@439
   176
        else /* !EMPTYSTR(identity->fpr) */ {
Edouard@512
   177
            if (_same_fpr(identity->fpr,
vb@591
   178
                          strlen(identity->fpr),
Edouard@512
   179
                          stored_identity->fpr,
vb@591
   180
                          strlen(stored_identity->fpr))) {
vb@14
   181
                if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
vb@14
   182
                    identity->comm_type = _comm_type_key;
Edouard@488
   183
                }else{
Edouard@488
   184
                    identity->comm_type = stored_identity->comm_type;
Edouard@488
   185
                    if (identity->comm_type == PEP_ct_unknown) {
Edouard@488
   186
                        identity->comm_type = _comm_type_key;
vb@8
   187
                    }
vb@8
   188
                }
vb@496
   189
            } else {
Edouard@488
   190
                status = get_trust(session, identity);
Edouard@488
   191
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@488
   192
                if (status == PEP_OUT_OF_MEMORY)
Edouard@488
   193
                    return PEP_OUT_OF_MEMORY;
vb@496
   194
                if (identity->comm_type < stored_identity->comm_type)
vb@496
   195
                    identity->comm_type = PEP_ct_unknown;
vb@8
   196
            }
vb@8
   197
        }
vb@8
   198
vb@21
   199
        if (identity->lang[0] == 0) {
vb@21
   200
            identity->lang[0] = stored_identity->lang[0];
vb@21
   201
            identity->lang[1] = stored_identity->lang[1];
vb@21
   202
            identity->lang[2] = 0;
vb@21
   203
        }
vb@21
   204
    }
vb@21
   205
    else /* stored_identity == NULL */ {
Edouard@439
   206
        if (!EMPTYSTR(identity->fpr)) {
vb@21
   207
            PEP_comm_type _comm_type_key;
vb@21
   208
vb@21
   209
            status = get_key_rating(session, identity->fpr, &_comm_type_key);
vb@21
   210
            assert(status != PEP_OUT_OF_MEMORY);
vb@21
   211
            if (status == PEP_OUT_OF_MEMORY)
vb@21
   212
                return PEP_OUT_OF_MEMORY;
vb@21
   213
vb@21
   214
            identity->comm_type = _comm_type_key;
vb@21
   215
        }
Edouard@439
   216
        else /* EMPTYSTR(identity->fpr) */ {
Edouard@774
   217
            PEP_STATUS status = elect_pubkey(session, identity);
Edouard@755
   218
            if (status != PEP_STATUS_OK)
Edouard@755
   219
                return status;
vb@21
   220
        }
vb@21
   221
    }
vb@21
   222
vb@21
   223
    status = PEP_STATUS_OK;
vb@21
   224
Edouard@439
   225
    if (identity->comm_type != PEP_ct_unknown && !EMPTYSTR(identity->user_id)) {
Edouard@439
   226
        assert(!EMPTYSTR(identity->username)); // this should not happen
vb@22
   227
Edouard@439
   228
        if (EMPTYSTR(identity->username)) { // mitigate
vb@0
   229
            free(identity->username);
vb@22
   230
            identity->username = strdup("anonymous");
vb@8
   231
            if (identity->username == NULL)
vb@8
   232
                return PEP_OUT_OF_MEMORY;
vb@0
   233
        }
vb@8
   234
Edouard@755
   235
        // Identity doesn't get stored if call was just about checking existing
Edouard@755
   236
        // user by address (i.e. no user id given but already stored)
Edouard@559
   237
        if (!(_no_user_id && stored_identity))
Edouard@559
   238
        {
Edouard@559
   239
            status = set_identity(session, identity);
Edouard@559
   240
            assert(status == PEP_STATUS_OK);
Edouard@559
   241
            if (status != PEP_STATUS_OK) {
Edouard@559
   242
                return status;
Edouard@559
   243
            }
Edouard@499
   244
        }
vb@8
   245
    }
vb@8
   246
vb@297
   247
    if (identity->comm_type != PEP_ct_compromized &&
vb@297
   248
            identity->comm_type < PEP_ct_strong_but_unconfirmed)
vb@297
   249
        if (session->examine_identity)
vb@297
   250
            session->examine_identity(identity, session->examine_management);
vb@297
   251
vb@8
   252
    return status;
vb@0
   253
}
vb@0
   254
Edouard@774
   255
PEP_STATUS elect_ownkey(
Edouard@774
   256
        PEP_SESSION session, pEp_identity * identity
Edouard@774
   257
    )
Edouard@774
   258
{
Edouard@774
   259
    PEP_STATUS status;
Edouard@774
   260
    stringlist_t *keylist = NULL;
Edouard@774
   261
Edouard@774
   262
    free(identity->fpr);
Edouard@774
   263
    identity->fpr = NULL;
Edouard@774
   264
Edouard@774
   265
    status = find_keys(session, identity->address, &keylist);
Edouard@774
   266
    assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   267
    if (status == PEP_OUT_OF_MEMORY)
Edouard@774
   268
        return PEP_OUT_OF_MEMORY;
Edouard@774
   269
    
Edouard@774
   270
    if (keylist != NULL && keylist->value != NULL)
Edouard@774
   271
    {
Edouard@774
   272
        char *_fpr = NULL;
Edouard@774
   273
        identity->comm_type = PEP_ct_unknown;
Edouard@774
   274
Edouard@774
   275
        stringlist_t *_keylist;
Edouard@774
   276
        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
Edouard@774
   277
            bool is_own = false;
Edouard@774
   278
            
Edouard@774
   279
            if (session->use_only_own_private_keys)
Edouard@774
   280
            {
Edouard@774
   281
                status = own_key_is_listed(session, _keylist->value, &is_own);
Edouard@774
   282
                assert(status == PEP_STATUS_OK);
Edouard@774
   283
                if (status != PEP_STATUS_OK) {
Edouard@774
   284
                    free_stringlist(keylist);
Edouard@774
   285
                    return status;
Edouard@774
   286
                }
Edouard@774
   287
            }
Edouard@774
   288
Edouard@774
   289
            // TODO : also accept synchronized device group keys ?
Edouard@774
   290
            
Edouard@774
   291
            if (!session->use_only_own_private_keys || is_own)
Edouard@774
   292
            {
Edouard@774
   293
                PEP_comm_type _comm_type_key;
Edouard@774
   294
                
Edouard@774
   295
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
Edouard@774
   296
                assert(status != PEP_OUT_OF_MEMORY);
Edouard@774
   297
                if (status == PEP_OUT_OF_MEMORY) {
Edouard@774
   298
                    free_stringlist(keylist);
Edouard@774
   299
                    return PEP_OUT_OF_MEMORY;
Edouard@774
   300
                }
Edouard@774
   301
                
Edouard@774
   302
                if (_comm_type_key != PEP_ct_compromized &&
Edouard@774
   303
                    _comm_type_key != PEP_ct_unknown)
Edouard@774
   304
                {
Edouard@774
   305
                    if (identity->comm_type == PEP_ct_unknown ||
Edouard@774
   306
                        _comm_type_key > identity->comm_type)
Edouard@774
   307
                    {
Edouard@774
   308
                        identity->comm_type = _comm_type_key;
Edouard@774
   309
                        _fpr = _keylist->value;
Edouard@774
   310
                    }
Edouard@774
   311
                }
Edouard@774
   312
            }
Edouard@774
   313
        }
Edouard@774
   314
        
Edouard@774
   315
        if (_fpr)
Edouard@774
   316
        {
Edouard@774
   317
            identity->fpr = strdup(_fpr);
Edouard@774
   318
            assert(identity->fpr);
Edouard@774
   319
            if (identity->fpr == NULL)
Edouard@774
   320
            {
Edouard@774
   321
                free_stringlist(keylist);
Edouard@774
   322
                return PEP_OUT_OF_MEMORY;
Edouard@774
   323
            }
Edouard@774
   324
        }
Edouard@774
   325
        free_stringlist(keylist);
Edouard@774
   326
    }
Edouard@774
   327
    return PEP_STATUS_OK;
Edouard@774
   328
}
Edouard@774
   329
vb@0
   330
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
vb@0
   331
{
Edouard@560
   332
    pEp_identity *stored_identity;
vb@0
   333
    PEP_STATUS status;
vb@0
   334
vb@0
   335
    assert(session);
vb@0
   336
    assert(identity);
vb@0
   337
    assert(identity->address);
vb@0
   338
    assert(identity->username);
Edouard@658
   339
    assert(EMPTYSTR(identity->user_id) ||
Edouard@658
   340
           strcmp(identity->user_id, PEP_OWN_USERID) == 0);
vb@0
   341
vb@191
   342
    if (!(session && identity && identity->address && identity->username &&
Edouard@658
   343
          (EMPTYSTR(identity->user_id) ||
Edouard@658
   344
           strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
vb@191
   345
        return PEP_ILLEGAL_VALUE;
vb@191
   346
vb@0
   347
    identity->comm_type = PEP_ct_pEp;
vb@0
   348
    identity->me = true;
Edouard@658
   349
    
Edouard@658
   350
    if(EMPTYSTR(identity->user_id))
Edouard@658
   351
    {
Edouard@658
   352
        free(identity->user_id);
Edouard@658
   353
        identity->user_id = strdup(PEP_OWN_USERID);
Edouard@658
   354
        assert(identity->user_id);
Edouard@658
   355
        if (identity->user_id == NULL)
Edouard@658
   356
        {
Edouard@658
   357
            return PEP_OUT_OF_MEMORY;
Edouard@658
   358
        }
Edouard@658
   359
    }
vb@0
   360
vb@215
   361
    DEBUG_LOG("myself", "debug", identity->address);
Edouard@560
   362
    
Edouard@560
   363
    status = get_identity(session,
Edouard@560
   364
                          identity->address,
Edouard@560
   365
                          identity->user_id,
Edouard@560
   366
                          &stored_identity);
Edouard@560
   367
    
vb@0
   368
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   369
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   370
        return PEP_OUT_OF_MEMORY;
Edouard@560
   371
    
Edouard@560
   372
    if (stored_identity)
Edouard@560
   373
    {
Edouard@560
   374
        if (EMPTYSTR(identity->fpr)) {
vb@591
   375
            identity->fpr = strdup(stored_identity->fpr);
Edouard@560
   376
            assert(identity->fpr);
Edouard@560
   377
            if (identity->fpr == NULL)
Edouard@560
   378
            {
Edouard@560
   379
                return PEP_OUT_OF_MEMORY;
Edouard@560
   380
            }
Edouard@560
   381
        }
Edouard@588
   382
    }
Edouard@588
   383
    else if (!EMPTYSTR(identity->fpr))
Edouard@588
   384
    {
Edouard@588
   385
        // App must have a good reason to give fpr, such as explicit
Edouard@588
   386
        // import of private key, or similar.
Edouard@588
   387
Edouard@658
   388
        // Take given fpr as-is.
Edouard@560
   389
    }
Edouard@560
   390
    else
Edouard@560
   391
    {
Edouard@774
   392
        status = elect_ownkey(session, identity);
Edouard@774
   393
        assert(status == PEP_STATUS_OK);
Edouard@774
   394
        if (status != PEP_STATUS_OK) {
Edouard@774
   395
            return status;
Edouard@560
   396
        }
Edouard@560
   397
    }
Edouard@695
   398
Edouard@695
   399
    bool revoked = false;
Edouard@695
   400
    char *r_fpr = NULL;
Edouard@695
   401
    if (!EMPTYSTR(identity->fpr))
Edouard@695
   402
    {
Edouard@695
   403
        status = key_revoked(session, identity->fpr, &revoked);
Edouard@695
   404
        assert(status == PEP_STATUS_OK);
Edouard@695
   405
        if (status != PEP_STATUS_OK) {
Edouard@695
   406
            return status;
Edouard@695
   407
        }
Edouard@695
   408
    }
Edouard@560
   409
    
Edouard@695
   410
    if (EMPTYSTR(identity->fpr) || revoked)
Edouard@695
   411
    {        
Edouard@695
   412
        if(revoked)
Edouard@695
   413
        {
Edouard@697
   414
            r_fpr = identity->fpr;
Edouard@697
   415
            identity->fpr = NULL;
Edouard@695
   416
        }
Edouard@560
   417
        
vb@215
   418
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   419
        status = generate_keypair(session, identity);
vb@0
   420
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   421
        if (status != PEP_STATUS_OK) {
vb@0
   422
            char buf[11];
vb@0
   423
            snprintf(buf, 11, "%d", status);
vb@215
   424
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   425
            if(revoked && r_fpr)
Edouard@695
   426
                free(r_fpr);
vb@0
   427
            return status;
vb@0
   428
        }
Edouard@560
   429
        
Edouard@695
   430
        if(revoked)
Edouard@695
   431
        {
Edouard@695
   432
            status = set_revoked(session, r_fpr,
Edouard@695
   433
                                 identity->fpr, time(NULL));
Edouard@695
   434
            free(r_fpr);
Edouard@695
   435
            if (status != PEP_STATUS_OK) {
Edouard@695
   436
                return status;
Edouard@695
   437
            }
Edouard@588
   438
        }
vb@0
   439
    }
Edouard@560
   440
    else
Edouard@560
   441
    {
vb@214
   442
        bool expired;
Edouard@701
   443
        status = key_expired(session, identity->fpr, 
Edouard@701
   444
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   445
                             &expired);
Edouard@701
   446
vb@214
   447
        assert(status == PEP_STATUS_OK);
Edouard@499
   448
        if (status != PEP_STATUS_OK) {
Edouard@588
   449
            return status;
Edouard@499
   450
        }
vb@214
   451
vb@214
   452
        if (status == PEP_STATUS_OK && expired) {
vb@214
   453
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   454
            renew_key(session, identity->fpr, ts);
vb@214
   455
            free_timestamp(ts);
vb@214
   456
        }
vb@214
   457
    }
vb@0
   458
vb@0
   459
    status = set_identity(session, identity);
vb@0
   460
    assert(status == PEP_STATUS_OK);
Edouard@499
   461
    if (status != PEP_STATUS_OK) {
Edouard@588
   462
        return status;
Edouard@499
   463
    }
vb@0
   464
vb@0
   465
    return PEP_STATUS_OK;
Edouard@499
   466
vb@0
   467
}
vb@0
   468
vb@296
   469
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   470
        PEP_SESSION session, 
vb@292
   471
        examine_identity_t examine_identity,
vb@292
   472
        void *management
vb@292
   473
    )
vb@292
   474
{
vb@292
   475
    assert(session);
vb@292
   476
    if (!session)
vb@292
   477
        return PEP_ILLEGAL_VALUE;
vb@292
   478
vb@292
   479
    session->examine_management = management;
vb@292
   480
    session->examine_identity = examine_identity;
vb@292
   481
vb@292
   482
    return PEP_STATUS_OK;
vb@292
   483
}
vb@292
   484
vb@0
   485
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   486
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   487
        void *management
vb@0
   488
    )
vb@0
   489
{
vb@0
   490
    PEP_SESSION session;
vb@0
   491
    pEp_identity *identity;
Edouard@499
   492
    PEP_STATUS status;
vb@0
   493
Edouard@499
   494
    assert(retrieve_next_identity);
Edouard@499
   495
    assert(management);
Edouard@499
   496
Edouard@499
   497
    if (!retrieve_next_identity || !management)
Edouard@499
   498
        return PEP_ILLEGAL_VALUE;
Edouard@499
   499
Edouard@499
   500
    status = init(&session);
vb@0
   501
    assert(status == PEP_STATUS_OK);
vb@0
   502
    if (status != PEP_STATUS_OK)
vb@0
   503
        return status;
vb@0
   504
vb@0
   505
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   506
Edouard@499
   507
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   508
    {
vb@0
   509
        assert(identity->address);
Edouard@499
   510
        if(identity->address)
Edouard@499
   511
        {
Edouard@499
   512
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   513
Edouard@499
   514
            if (identity->me) {
Edouard@499
   515
                status = myself(session, identity);
Edouard@499
   516
            } else {
Edouard@499
   517
                status = recv_key(session, identity->address);
Edouard@499
   518
            }
Edouard@499
   519
vb@0
   520
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   521
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   522
                return PEP_OUT_OF_MEMORY;
vb@0
   523
        }
vb@0
   524
        free_identity(identity);
vb@0
   525
    }
vb@0
   526
vb@0
   527
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   528
vb@0
   529
    release(session);
vb@0
   530
    return PEP_STATUS_OK;
vb@0
   531
}
vb@0
   532
vb@357
   533
DYNAMIC_API PEP_STATUS key_compromized(
vb@357
   534
        PEP_SESSION session,
vb@357
   535
        pEp_identity *ident
vb@357
   536
    )
vb@215
   537
{
vb@218
   538
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   539
vb@215
   540
    assert(session);
vb@357
   541
    assert(ident);
Edouard@439
   542
    assert(!EMPTYSTR(ident->fpr));
vb@215
   543
vb@357
   544
    if (!(session && ident && ident->fpr))
vb@215
   545
        return PEP_ILLEGAL_VALUE;
vb@215
   546
vb@357
   547
    if (ident->me)
Edouard@697
   548
    {
vb@357
   549
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   550
        myself(session, ident);
Edouard@697
   551
    }
Edouard@697
   552
    else
Edouard@697
   553
    {
Edouard@697
   554
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
   555
    }
vb@218
   556
vb@218
   557
    return status;
vb@215
   558
}
vb@215
   559
Edouard@410
   560
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   561
        PEP_SESSION session,
Edouard@410
   562
        pEp_identity *ident
Edouard@410
   563
    )
Edouard@410
   564
{
Edouard@410
   565
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   566
Edouard@410
   567
    assert(session);
Edouard@410
   568
    assert(ident);
vb@420
   569
    assert(!ident->me);
Edouard@439
   570
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   571
    assert(!EMPTYSTR(ident->address));
Edouard@439
   572
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   573
vb@420
   574
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   575
            ident->user_id))
Edouard@410
   576
        return PEP_ILLEGAL_VALUE;
Edouard@410
   577
vb@420
   578
    status = update_identity(session, ident);
vb@420
   579
    if (status != PEP_STATUS_OK)
vb@420
   580
        return status;
Edouard@410
   581
Edouard@442
   582
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   583
        ident->comm_type = PEP_ct_unknown;
vb@421
   584
    else
vb@421
   585
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   586
vb@420
   587
    status = set_identity(session, ident);
vb@421
   588
    if (status != PEP_STATUS_OK)
vb@421
   589
        return status;
vb@421
   590
vb@422
   591
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   592
        status = update_identity(session, ident);
Edouard@410
   593
    return status;
Edouard@410
   594
}
Edouard@410
   595
vb@354
   596
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   597
        PEP_SESSION session,
vb@354
   598
        pEp_identity *ident
vb@354
   599
    )
vb@354
   600
{
vb@354
   601
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   602
vb@354
   603
    assert(session);
vb@354
   604
    assert(ident);
Edouard@439
   605
    assert(!EMPTYSTR(ident->address));
Edouard@439
   606
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   607
    assert(!EMPTYSTR(ident->fpr));
vb@354
   608
    assert(!ident->me);
vb@354
   609
Edouard@439
   610
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   611
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   612
        return PEP_ILLEGAL_VALUE;
vb@354
   613
vb@354
   614
    status = update_identity(session, ident);
vb@354
   615
    if (status != PEP_STATUS_OK)
vb@354
   616
        return status;
vb@354
   617
vb@356
   618
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   619
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   620
        status = set_identity(session, ident);
vb@356
   621
    }
vb@356
   622
    else {
vb@356
   623
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   624
        status = PEP_CANNOT_SET_TRUST;
vb@356
   625
    }
vb@354
   626
vb@354
   627
    return status;
vb@354
   628
}
vb@354
   629
Edouard@584
   630
DYNAMIC_API PEP_STATUS own_key_is_listed(
Edouard@584
   631
                                           PEP_SESSION session,
Edouard@584
   632
                                           const char *fpr,
Edouard@584
   633
                                           bool *listed
Edouard@584
   634
                                           )
Edouard@584
   635
{
Edouard@584
   636
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   637
    int count;
Edouard@584
   638
    
Edouard@584
   639
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   640
    
Edouard@584
   641
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   642
        return PEP_ILLEGAL_VALUE;
Edouard@584
   643
    
Edouard@584
   644
    *listed = false;
Edouard@584
   645
    
Edouard@584
   646
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   647
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   648
    
Edouard@584
   649
    int result;
Edouard@584
   650
    
Edouard@584
   651
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   652
    switch (result) {
Edouard@584
   653
        case SQLITE_ROW:
Edouard@584
   654
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   655
            *listed = count > 0;
Edouard@584
   656
            status = PEP_STATUS_OK;
Edouard@584
   657
            break;
Edouard@584
   658
            
Edouard@584
   659
        default:
Edouard@584
   660
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   661
    }
Edouard@584
   662
    
Edouard@584
   663
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   664
    return status;
Edouard@584
   665
}
Edouard@584
   666
Edouard@584
   667
DYNAMIC_API PEP_STATUS own_key_retrieve(
Edouard@584
   668
                                          PEP_SESSION session,
Edouard@584
   669
                                          stringlist_t **own_key
Edouard@584
   670
                                          )
Edouard@584
   671
{
Edouard@584
   672
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   673
    
Edouard@584
   674
    assert(session);
Edouard@584
   675
    assert(own_key);
Edouard@584
   676
    
Edouard@584
   677
    if (!(session && own_key))
Edouard@584
   678
        return PEP_ILLEGAL_VALUE;
Edouard@584
   679
    
Edouard@584
   680
    *own_key = NULL;
Edouard@584
   681
    stringlist_t *_own_key = new_stringlist(NULL);
Edouard@584
   682
    if (_own_key == NULL)
Edouard@584
   683
        goto enomem;
Edouard@584
   684
    
Edouard@584
   685
    sqlite3_reset(session->own_key_retrieve);
Edouard@584
   686
    
Edouard@584
   687
    int result;
Edouard@584
   688
    const char *fpr = NULL;
Edouard@584
   689
    
Edouard@584
   690
    stringlist_t *_bl = _own_key;
Edouard@584
   691
    do {
Edouard@584
   692
        result = sqlite3_step(session->own_key_retrieve);
Edouard@584
   693
        switch (result) {
Edouard@584
   694
            case SQLITE_ROW:
Edouard@584
   695
                fpr = (const char *) sqlite3_column_text(session->own_key_retrieve, 0);
Edouard@584
   696
                
Edouard@584
   697
                _bl = stringlist_add(_bl, fpr);
Edouard@584
   698
                if (_bl == NULL)
Edouard@584
   699
                    goto enomem;
Edouard@584
   700
                
Edouard@584
   701
                break;
Edouard@584
   702
                
Edouard@584
   703
            case SQLITE_DONE:
Edouard@584
   704
                break;
Edouard@584
   705
                
Edouard@584
   706
            default:
Edouard@584
   707
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   708
                result = SQLITE_DONE;
Edouard@584
   709
        }
Edouard@584
   710
    } while (result != SQLITE_DONE);
Edouard@584
   711
    
Edouard@584
   712
    sqlite3_reset(session->own_key_retrieve);
Edouard@584
   713
    if (status == PEP_STATUS_OK)
Edouard@584
   714
        *own_key = _own_key;
Edouard@584
   715
    else
Edouard@584
   716
        free_stringlist(_own_key);
Edouard@584
   717
    
Edouard@584
   718
    goto the_end;
Edouard@584
   719
    
Edouard@584
   720
enomem:
Edouard@584
   721
    free_stringlist(_own_key);
Edouard@584
   722
    status = PEP_OUT_OF_MEMORY;
Edouard@584
   723
    
Edouard@584
   724
the_end:
Edouard@584
   725
    return status;
Edouard@584
   726
}