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