src/keymanagement.c
author Edouard Tisserant
Wed, 29 Jun 2016 19:20:10 +0200
changeset 775 af16c9faedf2
parent 774 bc90d5bf74d5
child 829 7bb16947031a
permissions -rw-r--r--
Myself() : elect another pre-existing private key if stored own identity key have been removed from GPG keyring (Outlook+GPA case)
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@559
   131
        {
Edouard@562
   132
            return PEP_OUT_OF_MEMORY;
Edouard@559
   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@775
   404
Edouard@775
   405
        // Forces re-election if key is missing and own-key-only not forced
Edouard@775
   406
        if (!session->use_only_own_private_keys && status == PEP_KEY_NOT_FOUND) 
Edouard@775
   407
        {
Edouard@775
   408
            status = elect_ownkey(session, identity);
Edouard@775
   409
            assert(status == PEP_STATUS_OK);
Edouard@775
   410
            if (status != PEP_STATUS_OK) {
Edouard@775
   411
                return status;
Edouard@775
   412
            }
Edouard@775
   413
        } 
Edouard@775
   414
        else if (status != PEP_STATUS_OK) 
Edouard@775
   415
        {
Edouard@695
   416
            return status;
Edouard@695
   417
        }
Edouard@695
   418
    }
Edouard@560
   419
    
Edouard@695
   420
    if (EMPTYSTR(identity->fpr) || revoked)
Edouard@695
   421
    {        
Edouard@695
   422
        if(revoked)
Edouard@695
   423
        {
Edouard@697
   424
            r_fpr = identity->fpr;
Edouard@697
   425
            identity->fpr = NULL;
Edouard@695
   426
        }
Edouard@560
   427
        
vb@215
   428
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   429
        status = generate_keypair(session, identity);
vb@0
   430
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   431
        if (status != PEP_STATUS_OK) {
vb@0
   432
            char buf[11];
vb@0
   433
            snprintf(buf, 11, "%d", status);
vb@215
   434
            DEBUG_LOG("generating key pair failed", "debug", buf);
Edouard@695
   435
            if(revoked && r_fpr)
Edouard@695
   436
                free(r_fpr);
vb@0
   437
            return status;
vb@0
   438
        }
Edouard@560
   439
        
Edouard@695
   440
        if(revoked)
Edouard@695
   441
        {
Edouard@695
   442
            status = set_revoked(session, r_fpr,
Edouard@695
   443
                                 identity->fpr, time(NULL));
Edouard@695
   444
            free(r_fpr);
Edouard@695
   445
            if (status != PEP_STATUS_OK) {
Edouard@695
   446
                return status;
Edouard@695
   447
            }
Edouard@371
   448
        }
vb@0
   449
    }
Edouard@560
   450
    else
Edouard@560
   451
    {
vb@214
   452
        bool expired;
Edouard@701
   453
        status = key_expired(session, identity->fpr, 
Edouard@701
   454
                             time(NULL) + (7*24*3600), // In a week
Edouard@701
   455
                             &expired);
Edouard@701
   456
vb@214
   457
        assert(status == PEP_STATUS_OK);
Edouard@499
   458
        if (status != PEP_STATUS_OK) {
Edouard@588
   459
            return status;
Edouard@499
   460
        }
vb@214
   461
vb@214
   462
        if (status == PEP_STATUS_OK && expired) {
vb@214
   463
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
Edouard@560
   464
            renew_key(session, identity->fpr, ts);
vb@214
   465
            free_timestamp(ts);
vb@214
   466
        }
vb@214
   467
    }
vb@0
   468
vb@0
   469
    status = set_identity(session, identity);
vb@0
   470
    assert(status == PEP_STATUS_OK);
Edouard@499
   471
    if (status != PEP_STATUS_OK) {
Edouard@588
   472
        return status;
Edouard@499
   473
    }
vb@0
   474
vb@0
   475
    return PEP_STATUS_OK;
Edouard@499
   476
vb@0
   477
}
vb@0
   478
vb@296
   479
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   480
        PEP_SESSION session, 
vb@292
   481
        examine_identity_t examine_identity,
vb@292
   482
        void *management
vb@292
   483
    )
vb@292
   484
{
vb@292
   485
    assert(session);
vb@292
   486
    if (!session)
vb@292
   487
        return PEP_ILLEGAL_VALUE;
vb@292
   488
vb@292
   489
    session->examine_management = management;
vb@292
   490
    session->examine_identity = examine_identity;
vb@292
   491
vb@292
   492
    return PEP_STATUS_OK;
vb@292
   493
}
vb@292
   494
vb@0
   495
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   496
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   497
        void *management
vb@0
   498
    )
vb@0
   499
{
vb@0
   500
    PEP_SESSION session;
vb@0
   501
    pEp_identity *identity;
Edouard@499
   502
    PEP_STATUS status;
vb@0
   503
vb@24
   504
    assert(retrieve_next_identity);
vb@24
   505
    assert(management);
vb@24
   506
Edouard@499
   507
    if (!retrieve_next_identity || !management)
Edouard@499
   508
        return PEP_ILLEGAL_VALUE;
Edouard@499
   509
Edouard@499
   510
    status = init(&session);
Edouard@499
   511
    assert(status == PEP_STATUS_OK);
Edouard@499
   512
    if (status != PEP_STATUS_OK)
Edouard@499
   513
        return status;
Edouard@499
   514
vb@0
   515
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   516
Edouard@499
   517
    while ((identity = retrieve_next_identity(management))) 
Edouard@499
   518
    {
vb@0
   519
        assert(identity->address);
Edouard@499
   520
        if(identity->address)
Edouard@499
   521
        {
Edouard@499
   522
            DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
Edouard@499
   523
Edouard@499
   524
            if (identity->me) {
Edouard@499
   525
                status = myself(session, identity);
Edouard@499
   526
            } else {
Edouard@499
   527
                status = recv_key(session, identity->address);
Edouard@499
   528
            }
Edouard@499
   529
vb@0
   530
            assert(status != PEP_OUT_OF_MEMORY);
Edouard@499
   531
            if(status == PEP_OUT_OF_MEMORY)
Edouard@499
   532
                return PEP_OUT_OF_MEMORY;
vb@0
   533
        }
vb@0
   534
        free_identity(identity);
vb@0
   535
    }
vb@0
   536
vb@0
   537
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   538
vb@0
   539
    release(session);
vb@0
   540
    return PEP_STATUS_OK;
vb@0
   541
}
vb@0
   542
vb@357
   543
DYNAMIC_API PEP_STATUS key_compromized(
vb@357
   544
        PEP_SESSION session,
vb@357
   545
        pEp_identity *ident
vb@357
   546
    )
vb@215
   547
{
vb@218
   548
    PEP_STATUS status = PEP_STATUS_OK;
vb@218
   549
vb@215
   550
    assert(session);
vb@357
   551
    assert(ident);
Edouard@439
   552
    assert(!EMPTYSTR(ident->fpr));
vb@215
   553
vb@357
   554
    if (!(session && ident && ident->fpr))
vb@215
   555
        return PEP_ILLEGAL_VALUE;
vb@215
   556
vb@357
   557
    if (ident->me)
Edouard@697
   558
    {
vb@357
   559
        revoke_key(session, ident->fpr, NULL);
Edouard@697
   560
        myself(session, ident);
Edouard@697
   561
    }
Edouard@697
   562
    else
Edouard@697
   563
    {
Edouard@697
   564
        status = mark_as_compromized(session, ident->fpr);
Edouard@697
   565
    }
vb@218
   566
vb@218
   567
    return status;
vb@215
   568
}
vb@215
   569
Edouard@410
   570
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   571
        PEP_SESSION session,
Edouard@410
   572
        pEp_identity *ident
Edouard@410
   573
    )
Edouard@410
   574
{
Edouard@410
   575
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@410
   576
Edouard@410
   577
    assert(session);
Edouard@410
   578
    assert(ident);
vb@420
   579
    assert(!ident->me);
Edouard@439
   580
    assert(!EMPTYSTR(ident->fpr));
Edouard@439
   581
    assert(!EMPTYSTR(ident->address));
Edouard@439
   582
    assert(!EMPTYSTR(ident->user_id));
Edouard@410
   583
vb@420
   584
    if (!(session && ident && !ident->me && ident->fpr && ident->address &&
vb@420
   585
            ident->user_id))
Edouard@410
   586
        return PEP_ILLEGAL_VALUE;
Edouard@410
   587
vb@420
   588
    status = update_identity(session, ident);
vb@420
   589
    if (status != PEP_STATUS_OK)
vb@420
   590
        return status;
Edouard@410
   591
Edouard@442
   592
    if (ident->comm_type == PEP_ct_mistrusted)
vb@421
   593
        ident->comm_type = PEP_ct_unknown;
vb@421
   594
    else
vb@421
   595
        ident->comm_type &= ~PEP_ct_confirmed;
vb@421
   596
vb@420
   597
    status = set_identity(session, ident);
vb@421
   598
    if (status != PEP_STATUS_OK)
vb@421
   599
        return status;
vb@421
   600
vb@422
   601
    if (ident->comm_type == PEP_ct_unknown)
vb@422
   602
        status = update_identity(session, ident);
Edouard@410
   603
    return status;
Edouard@410
   604
}
Edouard@410
   605
vb@354
   606
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   607
        PEP_SESSION session,
vb@354
   608
        pEp_identity *ident
vb@354
   609
    )
vb@354
   610
{
vb@354
   611
    PEP_STATUS status = PEP_STATUS_OK;
vb@354
   612
vb@354
   613
    assert(session);
vb@354
   614
    assert(ident);
Edouard@439
   615
    assert(!EMPTYSTR(ident->address));
Edouard@439
   616
    assert(!EMPTYSTR(ident->user_id));
Edouard@439
   617
    assert(!EMPTYSTR(ident->fpr));
vb@354
   618
    assert(!ident->me);
vb@354
   619
Edouard@439
   620
    if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
Edouard@439
   621
            EMPTYSTR(ident->fpr) || ident->me)
vb@354
   622
        return PEP_ILLEGAL_VALUE;
vb@354
   623
vb@354
   624
    status = update_identity(session, ident);
vb@354
   625
    if (status != PEP_STATUS_OK)
vb@354
   626
        return status;
vb@354
   627
vb@356
   628
    if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
vb@356
   629
        ident->comm_type |= PEP_ct_confirmed;
Edouard@488
   630
        status = set_identity(session, ident);
vb@356
   631
    }
vb@356
   632
    else {
vb@356
   633
        // MISSING: S/MIME has to be handled depending on trusted CAs
vb@370
   634
        status = PEP_CANNOT_SET_TRUST;
vb@356
   635
    }
vb@354
   636
vb@354
   637
    return status;
vb@354
   638
}
vb@354
   639
Edouard@584
   640
DYNAMIC_API PEP_STATUS own_key_is_listed(
Edouard@584
   641
                                           PEP_SESSION session,
Edouard@584
   642
                                           const char *fpr,
Edouard@584
   643
                                           bool *listed
Edouard@584
   644
                                           )
Edouard@584
   645
{
Edouard@584
   646
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   647
    int count;
Edouard@584
   648
    
Edouard@584
   649
    assert(session && fpr && fpr[0] && listed);
Edouard@584
   650
    
Edouard@584
   651
    if (!(session && fpr && fpr[0] && listed))
Edouard@584
   652
        return PEP_ILLEGAL_VALUE;
Edouard@584
   653
    
Edouard@584
   654
    *listed = false;
Edouard@584
   655
    
Edouard@584
   656
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   657
    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
Edouard@584
   658
    
Edouard@584
   659
    int result;
Edouard@584
   660
    
Edouard@584
   661
    result = sqlite3_step(session->own_key_is_listed);
Edouard@584
   662
    switch (result) {
Edouard@584
   663
        case SQLITE_ROW:
Edouard@584
   664
            count = sqlite3_column_int(session->own_key_is_listed, 0);
Edouard@584
   665
            *listed = count > 0;
Edouard@584
   666
            status = PEP_STATUS_OK;
Edouard@584
   667
            break;
Edouard@584
   668
            
Edouard@584
   669
        default:
Edouard@584
   670
            status = PEP_UNKNOWN_ERROR;
Edouard@584
   671
    }
Edouard@584
   672
    
Edouard@584
   673
    sqlite3_reset(session->own_key_is_listed);
Edouard@584
   674
    return status;
Edouard@584
   675
}
Edouard@584
   676
Edouard@584
   677
DYNAMIC_API PEP_STATUS own_key_retrieve(
Edouard@584
   678
                                          PEP_SESSION session,
Edouard@584
   679
                                          stringlist_t **own_key
Edouard@584
   680
                                          )
Edouard@584
   681
{
Edouard@584
   682
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@584
   683
    
Edouard@584
   684
    assert(session);
Edouard@584
   685
    assert(own_key);
Edouard@584
   686
    
Edouard@584
   687
    if (!(session && own_key))
Edouard@584
   688
        return PEP_ILLEGAL_VALUE;
Edouard@584
   689
    
Edouard@584
   690
    *own_key = NULL;
Edouard@584
   691
    stringlist_t *_own_key = new_stringlist(NULL);
Edouard@584
   692
    if (_own_key == NULL)
Edouard@584
   693
        goto enomem;
Edouard@584
   694
    
Edouard@584
   695
    sqlite3_reset(session->own_key_retrieve);
Edouard@584
   696
    
Edouard@584
   697
    int result;
Edouard@584
   698
    const char *fpr = NULL;
Edouard@584
   699
    
Edouard@584
   700
    stringlist_t *_bl = _own_key;
Edouard@584
   701
    do {
Edouard@584
   702
        result = sqlite3_step(session->own_key_retrieve);
Edouard@584
   703
        switch (result) {
Edouard@584
   704
            case SQLITE_ROW:
Edouard@584
   705
                fpr = (const char *) sqlite3_column_text(session->own_key_retrieve, 0);
Edouard@584
   706
                
Edouard@584
   707
                _bl = stringlist_add(_bl, fpr);
Edouard@584
   708
                if (_bl == NULL)
Edouard@584
   709
                    goto enomem;
Edouard@584
   710
                
Edouard@584
   711
                break;
Edouard@584
   712
                
Edouard@584
   713
            case SQLITE_DONE:
Edouard@584
   714
                break;
Edouard@584
   715
                
Edouard@584
   716
            default:
Edouard@584
   717
                status = PEP_UNKNOWN_ERROR;
Edouard@584
   718
                result = SQLITE_DONE;
Edouard@584
   719
        }
Edouard@584
   720
    } while (result != SQLITE_DONE);
Edouard@584
   721
    
Edouard@584
   722
    sqlite3_reset(session->own_key_retrieve);
Edouard@584
   723
    if (status == PEP_STATUS_OK)
Edouard@584
   724
        *own_key = _own_key;
Edouard@584
   725
    else
Edouard@584
   726
        free_stringlist(_own_key);
Edouard@584
   727
    
Edouard@584
   728
    goto the_end;
Edouard@584
   729
    
Edouard@584
   730
enomem:
Edouard@584
   731
    free_stringlist(_own_key);
Edouard@584
   732
    status = PEP_OUT_OF_MEMORY;
Edouard@584
   733
    
Edouard@584
   734
the_end:
Edouard@584
   735
    return status;
Edouard@584
   736
}