src/pEpEngine.c
author Volker Birk <vb@pep.foundation>
Mon, 29 Aug 2016 17:39:35 +0200
branchkeysync
changeset 1091 0571ce4b97a2
parent 1090 c15ef7d00170
child 1103 aba4f9ea5bce
permissions -rw-r--r--
fixes
vb@125
     1
#include "pEp_internal.h"
vb@98
     2
#include "dynamic_api.h"
vb@28
     3
#include "cryptotech.h"
vb@28
     4
#include "transport.h"
vb@515
     5
#include "blacklist.h"
vb@944
     6
#include "sync_fsm.h"
vb@0
     7
vb@221
     8
static int init_count = -1;
vb@1091
     9
char sync_uuid[37];
vb@62
    10
vb@928
    11
static int user_version(void *_version, int count, char **text, char **name)
vb@928
    12
{
vb@928
    13
    assert(_version);
vb@928
    14
    assert(count == 1);
vb@928
    15
    assert(text && text[0]);
vb@928
    16
    if (!(_version && count == 1 && text && text[0]))
vb@928
    17
        return -1;
vb@928
    18
vb@928
    19
    int *version = (int *) _version;
vb@928
    20
    *version = atoi(text[0]);
vb@928
    21
    return 0;
vb@928
    22
}
vb@928
    23
vb@0
    24
DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
vb@0
    25
{
vb@65
    26
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
    27
    int int_result;
roker@529
    28
    static const char *sql_log;
roker@529
    29
    static const char *sql_trustword;
roker@529
    30
    static const char *sql_get_identity;
roker@529
    31
    static const char *sql_set_person;
roker@529
    32
    static const char *sql_set_pgp_keypair;
roker@529
    33
    static const char *sql_set_identity;
vb@932
    34
    static const char *sql_set_identity_flags;
roker@529
    35
    static const char *sql_set_trust;
vb@62
    36
    static const char *sql_get_trust;
vb@251
    37
    static const char *sql_least_trust;
vb@357
    38
    static const char *sql_mark_as_compromized;
vb@450
    39
    static const char *sql_crashdump;
vb@458
    40
    static const char *sql_languagelist;
vb@458
    41
    static const char *sql_i18n_token;
fdik@494
    42
fdik@494
    43
    // blacklist
fdik@494
    44
    static const char *sql_blacklist_add;
fdik@494
    45
    static const char *sql_blacklist_delete;
fdik@494
    46
    static const char *sql_blacklist_is_listed;
fdik@494
    47
    static const char *sql_blacklist_retrieve;
vb@450
    48
Edouard@584
    49
    // Own keys
Edouard@584
    50
    static const char *sql_own_key_is_listed;
vb@955
    51
    static const char *sql_own_identities_retrieve;
Edouard@584
    52
Edouard@693
    53
    // Sequence
vb@633
    54
    static const char *sql_sequence_value1;
vb@633
    55
    static const char *sql_sequence_value2;
vb@1085
    56
    static const char *sql_sequence_value3;
vb@632
    57
Edouard@693
    58
    // Revocation tracking
Edouard@693
    59
    static const char *sql_set_revoked;
Edouard@693
    60
    static const char *sql_get_revoked;
Edouard@693
    61
    
vb@62
    62
    bool in_first = false;
vb@8
    63
vb@62
    64
    assert(sqlite3_threadsafe());
vb@62
    65
    if (!sqlite3_threadsafe())
vb@62
    66
        return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
vb@62
    67
vb@62
    68
    // a little race condition - but still a race condition
vb@113
    69
    // mitigated by calling caveat (see documentation)
vb@62
    70
vb@62
    71
    ++init_count;
vb@67
    72
    if (init_count == 0)
vb@62
    73
        in_first = true;
vb@0
    74
roker@529
    75
    assert(session);
vb@191
    76
    if (session == NULL)
vb@191
    77
        return PEP_ILLEGAL_VALUE;
vb@191
    78
roker@529
    79
    *session = NULL;
vb@0
    80
vb@107
    81
    pEpSession *_session = calloc(1, sizeof(pEpSession));
roker@529
    82
    assert(_session);
roker@529
    83
    if (_session == NULL)
roker@529
    84
        goto enomem;
vb@62
    85
roker@529
    86
    _session->version = PEP_ENGINE_VERSION;
vb@0
    87
vb@0
    88
    assert(LOCAL_DB);
vb@0
    89
    if (LOCAL_DB == NULL) {
vb@65
    90
        status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    91
        goto pep_error;
vb@0
    92
    }
vb@0
    93
roker@529
    94
    int_result = sqlite3_open_v2(
roker@529
    95
            LOCAL_DB,
roker@529
    96
            &_session->db,
roker@529
    97
            SQLITE_OPEN_READWRITE
roker@529
    98
                | SQLITE_OPEN_CREATE
roker@529
    99
                | SQLITE_OPEN_FULLMUTEX
roker@529
   100
                | SQLITE_OPEN_PRIVATECACHE,
roker@529
   101
            NULL 
roker@529
   102
        );
vb@0
   103
roker@529
   104
    if (int_result != SQLITE_OK) {
roker@529
   105
        status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
   106
        goto pep_error;
roker@529
   107
    }
vb@0
   108
roker@529
   109
    sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
vb@0
   110
vb@0
   111
    assert(SYSTEM_DB);
vb@0
   112
    if (SYSTEM_DB == NULL) {
roker@529
   113
        status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
   114
        goto pep_error;
vb@0
   115
    }
vb@0
   116
roker@529
   117
    int_result = sqlite3_open_v2(
roker@529
   118
            SYSTEM_DB, &_session->system_db,
roker@529
   119
            SQLITE_OPEN_READONLY
roker@529
   120
                | SQLITE_OPEN_FULLMUTEX
roker@529
   121
                | SQLITE_OPEN_SHAREDCACHE,
roker@529
   122
            NULL
roker@529
   123
        );
vb@0
   124
roker@529
   125
    if (int_result != SQLITE_OK) {
roker@529
   126
        status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
   127
        goto pep_error;
roker@529
   128
    }
vb@0
   129
roker@529
   130
    sqlite3_busy_timeout(_session->system_db, 1000);
vb@0
   131
vb@928
   132
// increment this when patching DDL
vb@1085
   133
#define _DDL_USER_VERSION "3"
vb@928
   134
vb@62
   135
    if (in_first) {
vb@1091
   136
        memset(sync_uuid, 0, 37);
vb@1091
   137
vb@62
   138
        int_result = sqlite3_exec(
vb@62
   139
            _session->db,
vb@1008
   140
                "create table if not exists version_info (\n"
vb@456
   141
                "   id integer primary key,\n"
vb@1085
   142
                "   timestamp integer default (datetime('now')),\n"
vb@456
   143
                "   version text,\n"
vb@456
   144
                "   comment text\n"
vb@928
   145
                ");\n",
vb@928
   146
                NULL,
vb@928
   147
                NULL,
vb@928
   148
                NULL
vb@928
   149
        );
vb@928
   150
        int_result = sqlite3_exec(
vb@928
   151
            _session->db,
vb@948
   152
                "PRAGMA application_id = 0x23423423;\n"
vb@456
   153
                "create table if not exists log (\n"
vb@1085
   154
                "   timestamp integer default (datetime('now')),\n"
vb@456
   155
                "   title text not null,\n"
vb@456
   156
                "   entity text not null,\n"
vb@456
   157
                "   description text,\n"
vb@456
   158
                "   comment text\n"
vb@456
   159
                ");\n"
vb@456
   160
                "create index if not exists log_timestamp on log (\n"
vb@456
   161
                "   timestamp\n"
vb@456
   162
                ");\n"
vb@456
   163
                "create table if not exists pgp_keypair (\n"
vb@456
   164
                "   fpr text primary key,\n"
vb@456
   165
                "   public_id text unique,\n"
vb@456
   166
                "   private_id text,\n"
vb@456
   167
                "   created integer,\n"
vb@456
   168
                "   expires integer,\n"
vb@944
   169
                "   comment text,\n"
vb@1085
   170
                "   flags integer default 0\n"
vb@456
   171
                ");\n"
vb@456
   172
                "create index if not exists pgp_keypair_expires on pgp_keypair (\n"
vb@456
   173
                "   expires\n"
vb@456
   174
                ");\n"
vb@456
   175
                "create table if not exists person (\n"
vb@456
   176
                "   id text primary key,\n"
vb@456
   177
                "   username text not null,\n"
vb@456
   178
                "   main_key_id text\n"
vb@456
   179
                "       references pgp_keypair (fpr)\n"
vb@456
   180
                "       on delete set null,\n"
vb@456
   181
                "   lang text,\n"
vb@951
   182
                "   comment text,\n"
vb@951
   183
                "   device_group text\n"
vb@456
   184
                ");\n"
vb@456
   185
                "create table if not exists identity (\n"
Edouard@559
   186
                "   address text,\n"
vb@456
   187
                "   user_id text\n"
vb@456
   188
                "       references person (id)\n"
vb@456
   189
                "       on delete cascade,\n"
vb@456
   190
                "   main_key_id text\n"
vb@456
   191
                "       references pgp_keypair (fpr)\n"
vb@456
   192
                "       on delete set null,\n"
Edouard@559
   193
                "   comment text,\n"
vb@1085
   194
                "   flags integer default 0,"
Edouard@559
   195
                "   primary key (address, user_id)\n"
vb@456
   196
                ");\n"
vb@456
   197
                "create table if not exists trust (\n"
vb@456
   198
                "   user_id text not null\n"
vb@456
   199
                "       references person (id)\n"
vb@456
   200
                "       on delete cascade,\n"
vb@456
   201
                "   pgp_keypair_fpr text not null\n"
vb@456
   202
                "       references pgp_keypair (fpr)\n"
vb@456
   203
                "       on delete cascade,\n"
vb@456
   204
                "   comm_type integer not null,\n"
Edouard@684
   205
                "   comment text,\n"
Edouard@684
   206
                "   primary key (user_id, pgp_keypair_fpr)\n"
vb@456
   207
                ");\n"
fdik@494
   208
                // blacklist
vb@456
   209
                "create table if not exists blacklist_keys (\n"
vb@456
   210
                "   fpr text primary key\n"
vb@456
   211
                ");\n"
vb@632
   212
                // sequences
vb@632
   213
                "create table if not exists sequences(\n"
vb@632
   214
                "   name text primary key,\n"
vb@1085
   215
                "   value integer default 0,\n"
vb@1085
   216
                "   own integer default 0\n"
vb@632
   217
                ");\n"
Edouard@693
   218
                "create table if not exists revoked_keys (\n"
Edouard@693
   219
                "   revoked_fpr text primary key,\n"
Edouard@693
   220
                "   replacement_fpr text not null\n"
Edouard@693
   221
                "       references pgp_keypair (fpr)\n"
Edouard@693
   222
                "       on delete cascade,\n"
Edouard@693
   223
                "   revocation_date integer\n"
Edouard@693
   224
                ");\n"
vb@456
   225
                ,
vb@62
   226
            NULL,
vb@62
   227
            NULL,
vb@62
   228
            NULL
vb@62
   229
        );
vb@62
   230
        assert(int_result == SQLITE_OK);
vb@0
   231
vb@928
   232
        int version;
vb@62
   233
        int_result = sqlite3_exec(
vb@62
   234
            _session->db,
vb@928
   235
            "pragma user_version;",
vb@928
   236
            user_version,
vb@928
   237
            &version,
vb@62
   238
            NULL
vb@62
   239
        );
vb@62
   240
        assert(int_result == SQLITE_OK);
vb@0
   241
vb@928
   242
        if (version < 1) {
vb@928
   243
            int_result = sqlite3_exec(
vb@928
   244
                _session->db,
vb@928
   245
                "alter table identity\n"
vb@1085
   246
                "   add column flags integer default 0;\n",
vb@934
   247
                NULL,
vb@934
   248
                NULL,
vb@934
   249
                NULL
vb@934
   250
            );
vb@934
   251
        }
vb@934
   252
vb@944
   253
        if (version < 2) {
vb@944
   254
            int_result = sqlite3_exec(
vb@944
   255
                _session->db,
vb@944
   256
                "alter table pgp_keypair\n"
vb@1085
   257
                "   add column flags integer default 0;\n"
vb@951
   258
                "alter table person\n"
vb@951
   259
                "   add column device_group text;\n",
vb@928
   260
                NULL,
vb@928
   261
                NULL,
vb@928
   262
                NULL
vb@928
   263
            );
vb@928
   264
        }
vb@928
   265
vb@1085
   266
        if (version < 3) {
vb@1085
   267
            int_result = sqlite3_exec(
vb@1085
   268
                _session->db,
vb@1085
   269
                "alter table sequences\n"
vb@1085
   270
                "   add column own integer default 0;\n",
vb@1085
   271
                NULL,
vb@1085
   272
                NULL,
vb@1085
   273
                NULL
vb@1085
   274
            );
vb@1085
   275
        }
vb@1085
   276
vb@928
   277
        if (version < atoi(_DDL_USER_VERSION)) {
vb@928
   278
            int_result = sqlite3_exec(
vb@928
   279
                _session->db,
vb@928
   280
                "pragma user_version = "_DDL_USER_VERSION";\n"
vb@928
   281
                "insert or replace into version_info (id, version)"
vb@928
   282
                    "values (1, '" PEP_ENGINE_VERSION "');",
vb@928
   283
                NULL,
vb@928
   284
                NULL,
vb@928
   285
                NULL
vb@928
   286
            );
vb@928
   287
            assert(int_result == SQLITE_OK);
vb@928
   288
        }
vb@928
   289
vb@1085
   290
        if (version <= atoi(_DDL_USER_VERSION)) {
vb@1085
   291
            int_result = sqlite3_exec(
vb@1085
   292
                _session->db,
vb@1085
   293
                "pragma user_version = "_DDL_USER_VERSION";\n"
vb@1085
   294
                "insert or replace into version_info (id, version)"
vb@1085
   295
                    "values (1, '" PEP_ENGINE_VERSION "');",
vb@1085
   296
                NULL,
vb@1085
   297
                NULL,
vb@1085
   298
                NULL
vb@1085
   299
            );
vb@1085
   300
            assert(int_result == SQLITE_OK);
vb@1085
   301
        }
vb@1085
   302
vb@62
   303
        sql_log = "insert into log (title, entity, description, comment)"
vb@62
   304
                  "values (?1, ?2, ?3, ?4);";
vb@62
   305
vb@945
   306
        sql_get_identity =  "select fpr, username, comm_type, lang,"
vb@945
   307
                            "   identity.flags | pgp_keypair.flags"
vb@62
   308
                            "   from identity"
vb@62
   309
                            "   join person on id = identity.user_id"
vb@62
   310
                            "   join pgp_keypair on fpr = identity.main_key_id"
vb@62
   311
                            "   join trust on id = trust.user_id"
vb@62
   312
                            "       and pgp_keypair_fpr = identity.main_key_id"
Edouard@559
   313
                            "   where address = ?1 and identity.user_id = ?2;";
Edouard@559
   314
vb@233
   315
        sql_trustword = "select id, word from wordlist where lang = lower(?1) "
vb@62
   316
                       "and id = ?2 ;";
vb@62
   317
Edouard@641
   318
        // Set person, but if already exist, only update.
Edouard@641
   319
        // if main_key_id already set, don't touch.
Edouard@641
   320
        sql_set_person = "insert or replace into person (id, username, lang, main_key_id)"
Edouard@641
   321
                         "  values (?1, ?2, ?3,"
Edouard@641
   322
                         "    (select coalesce((select main_key_id from person "
Edouard@644
   323
                         "      where id = ?1), upper(replace(?4,' ',''))))) ;";
vb@62
   324
vb@62
   325
        sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr) "
Edouard@559
   326
                              "values (upper(replace(?1,' ',''))) ;";
vb@62
   327
vb@62
   328
        sql_set_identity = "insert or replace into identity (address, main_key_id, "
vb@929
   329
                           "user_id, flags) values (?1, upper(replace(?2,' ','')),"
vb@946
   330
                           "?3, ?4 & 255) ;";
vb@62
   331
vb@946
   332
        sql_set_identity_flags = "update identity set flags = ?1 & 255 "
vb@932
   333
                                 "where address = ?2 and user_id = ?3 ;";
vb@932
   334
vb@62
   335
        sql_set_trust = "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
Edouard@559
   336
                        "values (?1, upper(replace(?2,' ','')), ?3) ;";
vb@62
   337
Edouard@559
   338
        sql_get_trust = "select comm_type from trust where user_id = ?1 "
Edouard@559
   339
                        "and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
vb@251
   340
Edouard@559
   341
        sql_least_trust = "select min(comm_type) from trust where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
vb@357
   342
vb@457
   343
        sql_mark_as_compromized = "update trust not indexed set comm_type = 15"
Edouard@559
   344
                                  " where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
vb@450
   345
vb@457
   346
        sql_crashdump = "select timestamp, title, entity, description, comment"
vb@457
   347
                        " from log order by timestamp desc limit ?1 ;";
vb@458
   348
vb@619
   349
        sql_languagelist = "select i18n_language.lang, name, phrase from i18n_language join i18n_token using (lang) where i18n_token.id = 1000;" ;
vb@458
   350
vb@461
   351
        sql_i18n_token = "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
vb@482
   352
fdik@494
   353
        // blacklist
fdik@494
   354
Edouard@559
   355
        sql_blacklist_add = "insert or replace into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
Edouard@559
   356
                            "delete from identity where main_key_id = upper(replace(?1,' ','')) ;"
Edouard@559
   357
                            "delete from pgp_keypair where fpr = upper(replace(?1,' ','')) ;";
vb@514
   358
Edouard@559
   359
        sql_blacklist_delete = "delete from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
vb@514
   360
Edouard@559
   361
        sql_blacklist_is_listed = "select count(*) from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
vb@514
   362
fdik@494
   363
        sql_blacklist_retrieve = "select * from blacklist_keys ;";
Edouard@584
   364
        
Edouard@584
   365
        // Own keys
Edouard@584
   366
        
Edouard@658
   367
        sql_own_key_is_listed =
Edouard@658
   368
                                "select count(*) from ("
Edouard@658
   369
                                " select main_key_id from person "
Edouard@658
   370
                                "   where main_key_id = upper(replace(?1,' ',''))"
Edouard@658
   371
                                "    and id = '" PEP_OWN_USERID "' "
Edouard@658
   372
                                " union "
Edouard@658
   373
                                "  select main_key_id from identity "
Edouard@658
   374
                                "   where main_key_id = upper(replace(?1,' ',''))"
Edouard@658
   375
                                "    and user_id = '" PEP_OWN_USERID "' );";
Edouard@658
   376
vb@1071
   377
        sql_own_identities_retrieve =  "select address, fpr, username, "
vb@1071
   378
                            "   lang, identity.flags | pgp_keypair.flags"
vb@955
   379
                            "   from identity"
vb@955
   380
                            "   join person on id = identity.user_id"
vb@955
   381
                            "   join pgp_keypair on fpr = identity.main_key_id"
vb@955
   382
                            "   join trust on id = trust.user_id"
vb@955
   383
                            "       and pgp_keypair_fpr = identity.main_key_id"
vb@955
   384
                            "   where identity.user_id = '" PEP_OWN_USERID "';";
Edouard@584
   385
        
vb@1086
   386
        sql_sequence_value1 = "insert or replace into sequences (name, value, own) "
vb@633
   387
                              "values (?1, "
vb@633
   388
                              "(select coalesce((select value + 1 from sequences "
vb@1086
   389
                              "where name = ?1), 1 )), ?2) ; ";
vb@1086
   390
        sql_sequence_value2 = "select value, own from sequences where name = ?1 ;";
vb@1091
   391
        sql_sequence_value3 = "update sequences set value = ?2, own = ?3 where name = ?1 ;";
Edouard@693
   392
        
Edouard@693
   393
        sql_set_revoked =     "insert or replace into revoked_keys ("
Edouard@693
   394
                              "    revoked_fpr, replacement_fpr, revocation_date) "
Edouard@693
   395
                              "values (upper(replace(?1,' ','')),"
Edouard@693
   396
                              "        upper(replace(?2,' ','')),"
Edouard@693
   397
                              "        ?3) ;";
Edouard@693
   398
        
Edouard@693
   399
        sql_get_revoked =     "select revoked_fpr, revocation_date from revoked_keys"
Edouard@693
   400
                              "    where replacement_fpr = upper(replace(?1,' ','')) ;";
vb@62
   401
    }
vb@62
   402
vb@951
   403
    int_result = sqlite3_prepare_v2(_session->db, sql_log,
vb@951
   404
            (int)strlen(sql_log), &_session->log, NULL);
roker@529
   405
    assert(int_result == SQLITE_OK);
vb@0
   406
vb@233
   407
    int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
Edouard@417
   408
            (int)strlen(sql_trustword), &_session->trustword, NULL);
roker@529
   409
    assert(int_result == SQLITE_OK);
vb@0
   410
vb@0
   411
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
Edouard@417
   412
            (int)strlen(sql_get_identity), &_session->get_identity, NULL);
roker@529
   413
    assert(int_result == SQLITE_OK);
vb@0
   414
vb@0
   415
    int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
Edouard@417
   416
            (int)strlen(sql_set_person), &_session->set_person, NULL);
vb@0
   417
    assert(int_result == SQLITE_OK);
vb@62
   418
vb@0
   419
    int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
vb@951
   420
            (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair,
vb@951
   421
            NULL);
roker@529
   422
    assert(int_result == SQLITE_OK);
vb@62
   423
vb@0
   424
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity,
Edouard@417
   425
            (int)strlen(sql_set_identity), &_session->set_identity, NULL);
roker@529
   426
    assert(int_result == SQLITE_OK);
vb@62
   427
vb@932
   428
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_flags,
vb@951
   429
            (int)strlen(sql_set_identity_flags), &_session->set_identity_flags,
vb@951
   430
            NULL);
vb@932
   431
    assert(int_result == SQLITE_OK);
vb@932
   432
vb@0
   433
    int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
Edouard@417
   434
            (int)strlen(sql_set_trust), &_session->set_trust, NULL);
roker@529
   435
    assert(int_result == SQLITE_OK);
vb@62
   436
vb@8
   437
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
Edouard@417
   438
            (int)strlen(sql_get_trust), &_session->get_trust, NULL);
vb@8
   439
    assert(int_result == SQLITE_OK);
vb@0
   440
vb@251
   441
    int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
Edouard@417
   442
            (int)strlen(sql_least_trust), &_session->least_trust, NULL);
vb@251
   443
    assert(int_result == SQLITE_OK);
vb@251
   444
vb@357
   445
    int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromized,
vb@951
   446
            (int)strlen(sql_mark_as_compromized), &_session->mark_compromized,
vb@951
   447
            NULL);
vb@357
   448
    assert(int_result == SQLITE_OK);
vb@357
   449
vb@453
   450
    int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
vb@453
   451
            (int)strlen(sql_crashdump), &_session->crashdump, NULL);
vb@453
   452
    assert(int_result == SQLITE_OK);
vb@453
   453
vb@458
   454
    int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
vb@458
   455
            (int)strlen(sql_languagelist), &_session->languagelist, NULL);
roker@529
   456
    assert(int_result == SQLITE_OK);
vb@458
   457
vb@458
   458
    int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
vb@458
   459
            (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
roker@529
   460
    assert(int_result == SQLITE_OK);
vb@458
   461
fdik@494
   462
    // blacklist
fdik@494
   463
fdik@494
   464
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
fdik@494
   465
            (int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
fdik@494
   466
    assert(int_result == SQLITE_OK);
fdik@494
   467
fdik@494
   468
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
vb@951
   469
            (int)strlen(sql_blacklist_delete), &_session->blacklist_delete,
vb@951
   470
            NULL);
fdik@494
   471
    assert(int_result == SQLITE_OK);
fdik@494
   472
fdik@494
   473
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
vb@951
   474
            (int)strlen(sql_blacklist_is_listed),
vb@951
   475
            &_session->blacklist_is_listed, NULL);
fdik@494
   476
    assert(int_result == SQLITE_OK);
fdik@494
   477
fdik@494
   478
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
vb@951
   479
            (int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve,
vb@951
   480
            NULL);
vb@482
   481
    assert(int_result == SQLITE_OK);
vb@482
   482
Edouard@584
   483
    // Own keys
Edouard@584
   484
    
Edouard@584
   485
    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
vb@951
   486
            (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed,
vb@951
   487
            NULL);
Edouard@584
   488
    assert(int_result == SQLITE_OK);
Edouard@584
   489
    
vb@955
   490
    int_result = sqlite3_prepare_v2(_session->db, sql_own_identities_retrieve,
vb@955
   491
            (int)strlen(sql_own_identities_retrieve),
vb@955
   492
            &_session->own_identities_retrieve, NULL);
Edouard@584
   493
    assert(int_result == SQLITE_OK);
vb@633
   494
 
vb@633
   495
    // Sequence
vb@633
   496
vb@633
   497
    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
vb@951
   498
            (int)strlen(sql_sequence_value1), &_session->sequence_value1,
vb@951
   499
            NULL);
vb@633
   500
    assert(int_result == SQLITE_OK);
vb@633
   501
vb@633
   502
    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value2,
vb@951
   503
            (int)strlen(sql_sequence_value2), &_session->sequence_value2,
vb@951
   504
            NULL);
vb@633
   505
    assert(int_result == SQLITE_OK);
vb@633
   506
vb@1085
   507
    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value3,
vb@1085
   508
            (int)strlen(sql_sequence_value3), &_session->sequence_value3,
vb@1085
   509
            NULL);
vb@1085
   510
    assert(int_result == SQLITE_OK);
vb@1085
   511
Edouard@693
   512
    // Revocation tracking
Edouard@693
   513
    
Edouard@693
   514
    int_result = sqlite3_prepare_v2(_session->db, sql_set_revoked,
vb@951
   515
            (int)strlen(sql_set_revoked), &_session->set_revoked, NULL);
Edouard@693
   516
    assert(int_result == SQLITE_OK);
Edouard@693
   517
    
Edouard@693
   518
    int_result = sqlite3_prepare_v2(_session->db, sql_get_revoked,
vb@951
   519
            (int)strlen(sql_get_revoked), &_session->get_revoked, NULL);
Edouard@693
   520
    assert(int_result == SQLITE_OK);
Edouard@693
   521
    
vb@65
   522
    status = init_cryptotech(_session, in_first);
vb@65
   523
    if (status != PEP_STATUS_OK)
vb@65
   524
        goto pep_error;
vb@0
   525
vb@65
   526
    status = init_transport_system(_session, in_first);
vb@65
   527
    if (status != PEP_STATUS_OK)
vb@65
   528
        goto pep_error;
vb@62
   529
vb@65
   530
    status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
vb@65
   531
    if (status != PEP_STATUS_OK)
vb@65
   532
        goto pep_error;
vb@65
   533
vb@464
   534
    // runtime config
vb@464
   535
vb@464
   536
    _session->passive_mode = false;
vb@464
   537
    _session->unencrypted_subject = false;
Edouard@720
   538
#ifdef ANDROID
Edouard@720
   539
    _session->use_only_own_private_keys = true;
Edouard@720
   540
#elif TARGET_OS_IPHONE
Edouard@720
   541
    _session->use_only_own_private_keys = true;
Edouard@720
   542
#else
Edouard@720
   543
    _session->use_only_own_private_keys = false;
Edouard@720
   544
#endif
vb@464
   545
roker@529
   546
    *session = _session;
roker@529
   547
    return PEP_STATUS_OK;
vb@65
   548
vb@65
   549
enomem:
vb@65
   550
    status = PEP_OUT_OF_MEMORY;
vb@65
   551
vb@65
   552
pep_error:
vb@65
   553
    release(_session);
vb@65
   554
    return status;
vb@0
   555
}
vb@0
   556
vb@0
   557
DYNAMIC_API void release(PEP_SESSION session)
vb@0
   558
{
vb@62
   559
    bool out_last = false;
vb@62
   560
vb@62
   561
    assert(init_count >= 0);
roker@529
   562
    assert(session);
vb@0
   563
Edouard@385
   564
    if (!((init_count >= 0) && session))
vb@191
   565
        return;
vb@191
   566
vb@62
   567
    // a small race condition but still a race condition
vb@113
   568
    // mitigated by calling caveat (see documentation)
vb@62
   569
vb@62
   570
    if (init_count == 0)
vb@62
   571
        out_last = true;
vb@62
   572
    --init_count;
vb@62
   573
roker@529
   574
    if (session) {
vb@986
   575
        if (session->sync_state != DeviceState_state_NONE)
vb@986
   576
            unregister_sync_callbacks(session);
vb@986
   577
roker@529
   578
        if (session->db) {
vb@517
   579
            if (session->log)
vb@517
   580
                sqlite3_finalize(session->log);
vb@233
   581
            if (session->trustword)
vb@233
   582
                sqlite3_finalize(session->trustword);
vb@65
   583
            if (session->get_identity)
vb@65
   584
                sqlite3_finalize(session->get_identity);
vb@65
   585
            if (session->set_person)
vb@65
   586
                sqlite3_finalize(session->set_person);
vb@65
   587
            if (session->set_pgp_keypair)
vb@65
   588
                sqlite3_finalize(session->set_pgp_keypair);
vb@517
   589
            if (session->set_identity)
vb@517
   590
                sqlite3_finalize(session->set_identity);
vb@932
   591
            if (session->set_identity_flags)
vb@932
   592
                sqlite3_finalize(session->set_identity_flags);
vb@65
   593
            if (session->set_trust)
vb@65
   594
                sqlite3_finalize(session->set_trust);
vb@65
   595
            if (session->get_trust)
vb@65
   596
                sqlite3_finalize(session->get_trust);
vb@251
   597
            if (session->least_trust)
vb@251
   598
                sqlite3_finalize(session->least_trust);
vb@517
   599
            if (session->mark_compromized)
vb@517
   600
                sqlite3_finalize(session->mark_compromized);
vb@517
   601
            if (session->crashdump)
vb@517
   602
                sqlite3_finalize(session->crashdump);
vb@517
   603
            if (session->languagelist)
vb@517
   604
                sqlite3_finalize(session->languagelist);
vb@517
   605
            if (session->i18n_token)
vb@517
   606
                sqlite3_finalize(session->i18n_token);
vb@517
   607
            if (session->blacklist_add)
vb@517
   608
                sqlite3_finalize(session->blacklist_add);
vb@517
   609
            if (session->blacklist_delete)
vb@517
   610
                sqlite3_finalize(session->blacklist_delete);
vb@517
   611
            if (session->blacklist_is_listed)
vb@517
   612
                sqlite3_finalize(session->blacklist_is_listed);
vb@517
   613
            if (session->blacklist_retrieve)
vb@517
   614
                sqlite3_finalize(session->blacklist_retrieve);
krista@957
   615
            if (session->own_key_is_listed)
krista@957
   616
                sqlite3_finalize(session->own_key_is_listed);
roker@1002
   617
            if (session->own_identities_retrieve)
roker@1002
   618
                sqlite3_finalize(session->own_identities_retrieve);
krista@957
   619
            if (session->sequence_value1)
krista@957
   620
                sqlite3_finalize(session->sequence_value1);
krista@957
   621
            if (session->sequence_value2)
krista@960
   622
                sqlite3_finalize(session->sequence_value2);
vb@1085
   623
            if (session->sequence_value3)
vb@1085
   624
                sqlite3_finalize(session->sequence_value3);
krista@957
   625
            if (session->set_revoked)
krista@957
   626
                sqlite3_finalize(session->set_revoked);
krista@957
   627
            if (session->get_revoked)
krista@957
   628
                sqlite3_finalize(session->get_revoked);
vb@26
   629
vb@65
   630
            if (session->db)
vb@65
   631
                sqlite3_close_v2(session->db);
vb@65
   632
            if (session->system_db)
vb@65
   633
                sqlite3_close_v2(session->system_db);
roker@529
   634
        }
vb@65
   635
vb@65
   636
        release_transport_system(session, out_last);
vb@65
   637
        release_cryptotech(session, out_last);
vb@65
   638
vb@65
   639
        free(session);
vb@62
   640
    }
vb@0
   641
}
vb@0
   642
vb@467
   643
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
   644
{
vb@464
   645
    assert(session);
vb@467
   646
    session->passive_mode = enable;
vb@464
   647
}
vb@464
   648
vb@467
   649
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
   650
{
vb@464
   651
    assert(session);
vb@467
   652
    session->unencrypted_subject = enable;
vb@464
   653
}
vb@464
   654
vb@951
   655
DYNAMIC_API void config_use_only_own_private_keys(PEP_SESSION session,
vb@951
   656
        bool enable)
Edouard@720
   657
{
Edouard@720
   658
    assert(session);
Edouard@720
   659
    session->use_only_own_private_keys = enable;
Edouard@720
   660
}
Edouard@720
   661
vb@0
   662
DYNAMIC_API PEP_STATUS log_event(
vb@450
   663
        PEP_SESSION session,
vb@451
   664
        const char *title,
vb@451
   665
        const char *entity,
vb@451
   666
        const char *description,
vb@451
   667
        const char *comment
vb@0
   668
    )
vb@0
   669
{
roker@529
   670
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   671
    int result;
vb@0
   672
roker@529
   673
    assert(session);
roker@529
   674
    assert(title);
roker@529
   675
    assert(entity);
vb@0
   676
vb@191
   677
    if (!(session && title && entity))
vb@191
   678
        return PEP_ILLEGAL_VALUE;
vb@191
   679
roker@529
   680
    sqlite3_reset(session->log);
roker@529
   681
    sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
roker@529
   682
    sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
roker@529
   683
    if (description)
vb@46
   684
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
roker@529
   685
    else
roker@529
   686
        sqlite3_bind_null(session->log, 3);
roker@529
   687
    if (comment)
roker@529
   688
        sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
roker@529
   689
    else
roker@529
   690
        sqlite3_bind_null(session->log, 4);
roker@529
   691
    do {
roker@529
   692
        result = sqlite3_step(session->log);
roker@529
   693
        assert(result == SQLITE_DONE || result == SQLITE_BUSY);
roker@529
   694
        if (result != SQLITE_DONE && result != SQLITE_BUSY)
roker@529
   695
            status = PEP_UNKNOWN_ERROR;
roker@529
   696
    } while (result == SQLITE_BUSY);
roker@529
   697
    sqlite3_reset(session->log);
vb@0
   698
roker@529
   699
    return status;
vb@0
   700
}
vb@0
   701
vb@233
   702
DYNAMIC_API PEP_STATUS trustword(
vb@0
   703
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   704
            char **word, size_t *wsize
vb@0
   705
        )
vb@0
   706
{
roker@529
   707
    PEP_STATUS status = PEP_STATUS_OK;
vb@0
   708
roker@529
   709
    assert(session);
roker@529
   710
    assert(word);
roker@529
   711
    assert(wsize);
vb@0
   712
vb@191
   713
    if (!(session && word && wsize))
vb@191
   714
        return PEP_ILLEGAL_VALUE;
vb@191
   715
roker@529
   716
    *word = NULL;
roker@529
   717
    *wsize = 0;
vb@0
   718
roker@529
   719
    if (lang == NULL)
roker@529
   720
        lang = "en";
vb@0
   721
roker@529
   722
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   723
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   724
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   725
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   726
    assert(lang[2] == 0);
vb@0
   727
roker@529
   728
    sqlite3_reset(session->trustword);
vb@233
   729
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
roker@529
   730
    sqlite3_bind_int(session->trustword, 2, value);
vb@0
   731
roker@877
   732
    const int result = sqlite3_step(session->trustword);
roker@529
   733
    if (result == SQLITE_ROW) {
vb@233
   734
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
   735
                    1));
roker@529
   736
        if (*word)
vb@233
   737
            *wsize = sqlite3_column_bytes(session->trustword, 1);
roker@529
   738
        else
Edouard@693
   739
            status = PEP_OUT_OF_MEMORY;
roker@529
   740
    } else
roker@529
   741
        status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   742
roker@529
   743
    sqlite3_reset(session->trustword);
roker@529
   744
    return status;
vb@0
   745
}
vb@0
   746
vb@233
   747
DYNAMIC_API PEP_STATUS trustwords(
vb@0
   748
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   749
        char **words, size_t *wsize, int max_words
vb@0
   750
    )
vb@0
   751
{
roker@529
   752
    const char *source = fingerprint;
roker@529
   753
    char *buffer;
roker@529
   754
    char *dest;
roker@529
   755
    size_t fsize;
vb@0
   756
roker@529
   757
    assert(session);
roker@529
   758
    assert(fingerprint);
roker@529
   759
    assert(words);
roker@529
   760
    assert(wsize);
roker@529
   761
    assert(max_words >= 0);
vb@0
   762
vb@191
   763
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
   764
        return PEP_ILLEGAL_VALUE;
vb@191
   765
roker@529
   766
    *words = NULL;
roker@529
   767
    *wsize = 0;
vb@0
   768
vb@251
   769
    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
   770
    assert(buffer);
vb@0
   771
    if (buffer == NULL)
vb@0
   772
        return PEP_OUT_OF_MEMORY;
vb@125
   773
    dest = buffer;
vb@0
   774
roker@529
   775
    fsize = strlen(fingerprint);
vb@0
   776
roker@529
   777
    if (!lang || !lang[0])
roker@529
   778
        lang = "en";
vb@0
   779
roker@529
   780
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   781
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   782
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   783
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   784
    assert(lang[2] == 0);
vb@0
   785
roker@529
   786
    int n_words = 0;
roker@529
   787
    while (source < fingerprint + fsize) {
vb@939
   788
        PEP_STATUS _status;
roker@529
   789
        uint16_t value;
roker@529
   790
        char *word;
roker@529
   791
        size_t _wsize;
roker@529
   792
        int j;
vb@0
   793
vb@0
   794
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
roker@529
   795
            if (*source >= 'a' && *source <= 'f')
roker@529
   796
                value += (*source - 'a' + 10) << (3 - j++) * 4;
roker@529
   797
            else if (*source >= 'A' && *source <= 'F')
roker@529
   798
                value += (*source - 'A' + 10) << (3 - j++) * 4;
roker@529
   799
            else if (*source >= '0' && *source <= '9')
roker@529
   800
                value += (*source - '0') << (3 - j++) * 4;
roker@529
   801
            
roker@529
   802
            source++;
roker@529
   803
        }
vb@0
   804
roker@529
   805
        _status = trustword(session, value, lang, &word, &_wsize);
vb@0
   806
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   807
            free(buffer);
vb@0
   808
            return PEP_OUT_OF_MEMORY;
vb@0
   809
        }
roker@529
   810
        if (word == NULL) {
vb@0
   811
            free(buffer);
roker@529
   812
            return PEP_TRUSTWORD_NOT_FOUND;
vb@0
   813
        }
vb@0
   814
roker@529
   815
        if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
roker@529
   816
            strncpy(dest, word, _wsize);
vb@0
   817
            free(word);
roker@529
   818
            dest += _wsize;
roker@529
   819
        }
roker@529
   820
        else {
vb@0
   821
            free(word);
roker@529
   822
            break; // buffer full
vb@0
   823
        }
vb@0
   824
roker@529
   825
        if (source < fingerprint + fsize
vb@251
   826
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
roker@529
   827
            *dest++ = ' ';
vb@0
   828
roker@529
   829
        ++n_words;
roker@529
   830
        if (max_words && n_words >= max_words)
roker@529
   831
            break;
roker@529
   832
    }
vb@0
   833
roker@529
   834
    *words = buffer;
roker@529
   835
    *wsize = dest - buffer;
roker@529
   836
    return PEP_STATUS_OK;
vb@0
   837
}
vb@0
   838
vb@0
   839
pEp_identity *new_identity(
vb@0
   840
        const char *address, const char *fpr, const char *user_id,
vb@0
   841
        const char *username
vb@0
   842
    )
vb@0
   843
{
vb@0
   844
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   845
    assert(result);
vb@0
   846
    if (result) {
vb@0
   847
        if (address) {
vb@0
   848
            result->address = strdup(address);
vb@0
   849
            assert(result->address);
vb@0
   850
            if (result->address == NULL) {
vb@0
   851
                free(result);
vb@0
   852
                return NULL;
vb@0
   853
            }
vb@0
   854
        }
vb@0
   855
        if (fpr) {
vb@0
   856
            result->fpr = strdup(fpr);
vb@0
   857
            assert(result->fpr);
vb@0
   858
            if (result->fpr == NULL) {
vb@0
   859
                free_identity(result);
vb@0
   860
                return NULL;
vb@0
   861
            }
vb@0
   862
        }
vb@0
   863
        if (user_id) {
vb@0
   864
            result->user_id = strdup(user_id);
vb@0
   865
            assert(result->user_id);
vb@0
   866
            if (result->user_id == NULL) {
vb@0
   867
                free_identity(result);
vb@0
   868
                return NULL;
vb@0
   869
            }
vb@0
   870
        }
vb@0
   871
        if (username) {
vb@0
   872
            result->username = strdup(username);
vb@0
   873
            assert(result->username);
vb@0
   874
            if (result->username == NULL) {
vb@0
   875
                free_identity(result);
vb@0
   876
                return NULL;
vb@0
   877
            }
vb@0
   878
        }
vb@0
   879
    }
vb@0
   880
    return result;
vb@0
   881
}
vb@0
   882
vb@37
   883
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   884
{
vb@37
   885
    assert(src);
vb@37
   886
vb@951
   887
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id,
vb@951
   888
            src->username);
vb@37
   889
    assert(dup);
vb@37
   890
    if (dup == NULL)
vb@37
   891
        return NULL;
vb@37
   892
    
vb@37
   893
    dup->comm_type = src->comm_type;
vb@37
   894
    dup->lang[0] = src->lang[0];
vb@37
   895
    dup->lang[1] = src->lang[1];
vb@37
   896
    dup->lang[2] = 0;
vb@37
   897
    dup->me = src->me;
vb@930
   898
    dup->flags = src->flags;
vb@37
   899
vb@37
   900
    return dup;
vb@37
   901
}
vb@37
   902
vb@0
   903
void free_identity(pEp_identity *identity)
vb@0
   904
{
vb@0
   905
    if (identity) {
vb@0
   906
        free(identity->address);
vb@0
   907
        free(identity->fpr);
vb@0
   908
        free(identity->user_id);
vb@0
   909
        free(identity->username);
vb@0
   910
        free(identity);
vb@0
   911
    }
vb@0
   912
}
vb@0
   913
vb@0
   914
DYNAMIC_API PEP_STATUS get_identity(
Edouard@559
   915
        PEP_SESSION session,
Edouard@559
   916
        const char *address,
Edouard@559
   917
        const char *user_id,
vb@0
   918
        pEp_identity **identity
vb@0
   919
    )
vb@0
   920
{
roker@529
   921
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   922
    static pEp_identity *_identity;
vb@0
   923
roker@529
   924
    assert(session);
roker@529
   925
    assert(address);
vb@8
   926
    assert(address[0]);
vb@632
   927
    assert(identity);
vb@0
   928
vb@632
   929
    if (!(session && address && address[0] && identity))
vb@191
   930
        return PEP_ILLEGAL_VALUE;
vb@191
   931
vb@632
   932
    *identity = NULL;
vb@632
   933
vb@46
   934
    sqlite3_reset(session->get_identity);
vb@46
   935
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
Edouard@559
   936
    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
vb@0
   937
roker@876
   938
    const int result = sqlite3_step(session->get_identity);
roker@529
   939
    switch (result) {
roker@529
   940
    case SQLITE_ROW:
vb@0
   941
        _identity = new_identity(
vb@0
   942
                address,
vb@46
   943
                (const char *) sqlite3_column_text(session->get_identity, 0),
Edouard@559
   944
                user_id,
Edouard@559
   945
                (const char *) sqlite3_column_text(session->get_identity, 1)
vb@0
   946
                );
vb@0
   947
        assert(_identity);
vb@0
   948
        if (_identity == NULL)
vb@0
   949
            return PEP_OUT_OF_MEMORY;
vb@0
   950
vb@951
   951
        _identity->comm_type = (PEP_comm_type)
vb@951
   952
            sqlite3_column_int(session->get_identity, 2);
vb@951
   953
        const char* const _lang = (const char *)
vb@951
   954
            sqlite3_column_text(session->get_identity, 3);
vb@0
   955
        if (_lang && _lang[0]) {
roker@529
   956
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
roker@529
   957
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
roker@529
   958
            assert(_lang[2] == 0);
roker@529
   959
            _identity->lang[0] = _lang[0];
roker@529
   960
            _identity->lang[1] = _lang[1];
vb@0
   961
            _identity->lang[2] = 0;
roker@529
   962
        }
vb@951
   963
        _identity->flags = (unsigned int)
vb@951
   964
            sqlite3_column_int(session->get_identity, 4);
roker@529
   965
        *identity = _identity;
roker@529
   966
        break;
roker@529
   967
    default:
vb@0
   968
        status = PEP_CANNOT_FIND_IDENTITY;
roker@529
   969
        *identity = NULL;
roker@529
   970
    }
vb@0
   971
vb@46
   972
    sqlite3_reset(session->get_identity);
roker@529
   973
    return status;
vb@0
   974
}
vb@0
   975
vb@0
   976
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   977
        PEP_SESSION session, const pEp_identity *identity
vb@0
   978
    )
vb@0
   979
{
roker@529
   980
    int result;
vb@0
   981
roker@529
   982
    assert(session);
roker@529
   983
    assert(identity);
roker@529
   984
    assert(identity->address);
roker@529
   985
    assert(identity->fpr);
roker@529
   986
    assert(identity->user_id);
roker@529
   987
    assert(identity->username);
vb@0
   988
vb@191
   989
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   990
                identity->user_id && identity->username))
vb@191
   991
        return PEP_ILLEGAL_VALUE;
vb@191
   992
vb@515
   993
    bool listed;
vb@515
   994
    PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
vb@515
   995
    assert(status == PEP_STATUS_OK);
vb@515
   996
    if (status != PEP_STATUS_OK)
vb@515
   997
        return status;
vb@515
   998
vb@515
   999
    if (listed)
vb@515
  1000
        return PEP_KEY_BLACKLISTED;
vb@515
  1001
roker@529
  1002
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
  1003
vb@1079
  1004
    if (identity->lang[0]) {
vb@1079
  1005
        assert(identity->lang[0] >= 'a' && identity->lang[0] <= 'z');
vb@1079
  1006
        assert(identity->lang[1] >= 'a' && identity->lang[1] <= 'z');
vb@1079
  1007
        assert(identity->lang[2] == 0);
vb@1079
  1008
    }
vb@1079
  1009
roker@529
  1010
    sqlite3_reset(session->set_person);
vb@46
  1011
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
  1012
            SQLITE_STATIC);
vb@46
  1013
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
  1014
            SQLITE_STATIC);
roker@529
  1015
    if (identity->lang[0])
vb@1081
  1016
        sqlite3_bind_text(session->set_person, 3, identity->lang, 2,
vb@0
  1017
                SQLITE_STATIC);
roker@529
  1018
    else
roker@529
  1019
        sqlite3_bind_null(session->set_person, 3);
Edouard@641
  1020
    sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
Edouard@641
  1021
                      SQLITE_STATIC);
roker@529
  1022
    result = sqlite3_step(session->set_person);
roker@529
  1023
    sqlite3_reset(session->set_person);
roker@529
  1024
    if (result != SQLITE_DONE) {
roker@529
  1025
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1026
        return PEP_CANNOT_SET_PERSON;
roker@529
  1027
    }
vb@0
  1028
roker@529
  1029
    sqlite3_reset(session->set_pgp_keypair);
vb@46
  1030
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
  1031
            SQLITE_STATIC);
roker@529
  1032
    result = sqlite3_step(session->set_pgp_keypair);
roker@529
  1033
    sqlite3_reset(session->set_pgp_keypair);
roker@529
  1034
    if (result != SQLITE_DONE) {
roker@529
  1035
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1036
        return PEP_CANNOT_SET_PGP_KEYPAIR;
roker@529
  1037
    }
vb@0
  1038
roker@529
  1039
    sqlite3_reset(session->set_identity);
vb@46
  1040
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
  1041
            SQLITE_STATIC);
vb@46
  1042
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
  1043
            SQLITE_STATIC);
vb@46
  1044
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
  1045
            SQLITE_STATIC);
vb@932
  1046
    sqlite3_bind_int(session->set_identity, 4, identity->flags);
roker@529
  1047
    result = sqlite3_step(session->set_identity);
roker@529
  1048
    sqlite3_reset(session->set_identity);
roker@529
  1049
    if (result != SQLITE_DONE) {
roker@529
  1050
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1051
        return PEP_CANNOT_SET_IDENTITY;
roker@529
  1052
    }
vb@0
  1053
roker@529
  1054
    sqlite3_reset(session->set_trust);
vb@46
  1055
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
  1056
            SQLITE_STATIC);
vb@46
  1057
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
  1058
            SQLITE_STATIC);
roker@529
  1059
    sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
roker@529
  1060
    result = sqlite3_step(session->set_trust);
roker@529
  1061
    sqlite3_reset(session->set_trust);
roker@529
  1062
    if (result != SQLITE_DONE) {
roker@529
  1063
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
Edouard@584
  1064
        return PEP_CANNOT_SET_TRUST;
roker@529
  1065
    }
vb@0
  1066
vb@46
  1067
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
roker@529
  1068
    if (result == SQLITE_OK)
roker@529
  1069
        return PEP_STATUS_OK;
roker@529
  1070
    else
roker@529
  1071
        return PEP_COMMIT_FAILED;
vb@0
  1072
}
vb@0
  1073
vb@932
  1074
DYNAMIC_API PEP_STATUS set_identity_flags(
vb@934
  1075
        PEP_SESSION session,
vb@934
  1076
        pEp_identity *identity,
vb@934
  1077
        unsigned int flags
vb@932
  1078
    )
vb@932
  1079
{
vb@932
  1080
    int result;
vb@932
  1081
vb@932
  1082
    assert(session);
vb@932
  1083
    assert(identity);
vb@932
  1084
    assert(identity->address);
vb@932
  1085
    assert(identity->user_id);
vb@932
  1086
vb@932
  1087
    if (!(session && identity && identity->address && identity->user_id))
vb@932
  1088
        return PEP_ILLEGAL_VALUE;
vb@932
  1089
vb@932
  1090
    sqlite3_reset(session->set_identity_flags);
vb@934
  1091
    sqlite3_bind_int(session->set_identity_flags, 1, flags);
vb@932
  1092
    sqlite3_bind_text(session->set_identity_flags, 2, identity->address, -1,
vb@932
  1093
            SQLITE_STATIC);
vb@932
  1094
    sqlite3_bind_text(session->set_identity_flags, 3, identity->user_id, -1,
vb@932
  1095
            SQLITE_STATIC);
vb@932
  1096
    result = sqlite3_step(session->set_identity_flags);
vb@932
  1097
    sqlite3_reset(session->set_identity_flags);
vb@932
  1098
    if (result != SQLITE_DONE)
vb@932
  1099
        return PEP_CANNOT_SET_IDENTITY;
vb@932
  1100
vb@934
  1101
    identity->flags = flags;
vb@932
  1102
    return PEP_STATUS_OK;
vb@932
  1103
}
vb@932
  1104
vb@357
  1105
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
  1106
        PEP_SESSION session,
vb@357
  1107
        const char *fpr
vb@357
  1108
    )
vb@357
  1109
{
roker@529
  1110
    int result;
vb@357
  1111
roker@529
  1112
    assert(session);
vb@357
  1113
    assert(fpr && fpr[0]);
vb@357
  1114
vb@357
  1115
    if (!(session && fpr && fpr[0]))
vb@357
  1116
        return PEP_ILLEGAL_VALUE;
vb@357
  1117
roker@529
  1118
    sqlite3_reset(session->mark_compromized);
vb@357
  1119
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
  1120
            SQLITE_STATIC);
vb@357
  1121
    result = sqlite3_step(session->mark_compromized);
roker@529
  1122
    sqlite3_reset(session->mark_compromized);
vb@357
  1123
vb@357
  1124
    if (result != SQLITE_DONE)
Edouard@584
  1125
        return PEP_CANNOT_SET_TRUST;
vb@357
  1126
vb@357
  1127
    return PEP_STATUS_OK;
vb@357
  1128
}
vb@357
  1129
vb@0
  1130
void pEp_free(void *p)
vb@0
  1131
{
vb@0
  1132
    free(p);
vb@0
  1133
}
vb@0
  1134
vb@8
  1135
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
  1136
{
vb@8
  1137
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
  1138
    int result;
vb@8
  1139
vb@8
  1140
    assert(session);
vb@8
  1141
    assert(identity);
vb@8
  1142
    assert(identity->user_id);
vb@8
  1143
    assert(identity->user_id[0]);
vb@8
  1144
    assert(identity->fpr);
vb@8
  1145
    assert(identity->fpr[0]);
vb@8
  1146
vb@191
  1147
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
  1148
                identity->fpr && identity->fpr[0]))
vb@191
  1149
        return PEP_ILLEGAL_VALUE;
vb@191
  1150
vb@8
  1151
    identity->comm_type = PEP_ct_unknown;
vb@8
  1152
vb@46
  1153
    sqlite3_reset(session->get_trust);
vb@951
  1154
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1,
vb@951
  1155
            SQLITE_STATIC);
vb@46
  1156
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
  1157
vb@46
  1158
    result = sqlite3_step(session->get_trust);
vb@8
  1159
    switch (result) {
vb@8
  1160
    case SQLITE_ROW: {
vb@951
  1161
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust,
vb@951
  1162
                0);
vb@8
  1163
        identity->comm_type = comm_type;
vb@8
  1164
        break;
vb@8
  1165
    }
vb@8
  1166
 
vb@8
  1167
    default:
vb@8
  1168
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
  1169
    }
vb@8
  1170
vb@46
  1171
    sqlite3_reset(session->get_trust);
vb@8
  1172
    return status;
vb@9
  1173
}
vb@9
  1174
vb@251
  1175
DYNAMIC_API PEP_STATUS least_trust(
vb@251
  1176
        PEP_SESSION session,
vb@251
  1177
        const char *fpr,
vb@251
  1178
        PEP_comm_type *comm_type
vb@251
  1179
    )
vb@251
  1180
{
vb@251
  1181
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
  1182
    int result;
vb@251
  1183
vb@251
  1184
    assert(session);
vb@251
  1185
    assert(fpr);
vb@251
  1186
    assert(comm_type);
vb@251
  1187
vb@251
  1188
    if (!(session && fpr && comm_type))
vb@251
  1189
        return PEP_ILLEGAL_VALUE;
vb@251
  1190
vb@632
  1191
    *comm_type = PEP_ct_unknown;
vb@632
  1192
vb@251
  1193
    sqlite3_reset(session->least_trust);
vb@251
  1194
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
  1195
vb@251
  1196
    result = sqlite3_step(session->least_trust);
vb@251
  1197
    switch (result) {
vb@251
  1198
        case SQLITE_ROW: {
vb@259
  1199
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
  1200
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
  1201
            break;
vb@251
  1202
        }
vb@251
  1203
        default:
vb@251
  1204
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
  1205
    }
vb@251
  1206
vb@251
  1207
    sqlite3_reset(session->least_trust);
vb@251
  1208
    return status;
vb@251
  1209
}
vb@251
  1210
vb@24
  1211
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
  1212
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
  1213
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
  1214
    )
vb@24
  1215
{
vb@191
  1216
    assert(session);
vb@191
  1217
    assert(ctext);
vb@191
  1218
    assert(csize);
vb@191
  1219
    assert(ptext);
vb@191
  1220
    assert(psize);
vb@191
  1221
    assert(keylist);
vb@191
  1222
vb@939
  1223
    if (!(session && ctext && csize && ptext && psize && keylist))
vb@191
  1224
        return PEP_ILLEGAL_VALUE;
vb@191
  1225
vb@939
  1226
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(
vb@939
  1227
            session, ctext, csize, ptext, psize, keylist);
vb@24
  1228
}
vb@24
  1229
vb@24
  1230
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
  1231
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
  1232
    size_t psize, char **ctext, size_t *csize
vb@24
  1233
    )
vb@24
  1234
{
vb@191
  1235
    assert(session);
vb@191
  1236
    assert(keylist);
vb@191
  1237
    assert(ptext);
vb@191
  1238
    assert(psize);
vb@191
  1239
    assert(ctext);
vb@191
  1240
    assert(csize);
vb@191
  1241
vb@191
  1242
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
  1243
        return PEP_ILLEGAL_VALUE;
vb@191
  1244
vb@951
  1245
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session,
vb@951
  1246
            keylist, ptext, psize, ctext, csize);
vb@24
  1247
}
vb@24
  1248
vb@24
  1249
DYNAMIC_API PEP_STATUS verify_text(
vb@24
  1250
    PEP_SESSION session, const char *text, size_t size,
vb@24
  1251
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
  1252
    )
vb@24
  1253
{
vb@191
  1254
    assert(session);
vb@191
  1255
    assert(text);
vb@191
  1256
    assert(size);
vb@191
  1257
    assert(signature);
vb@191
  1258
    assert(sig_size);
vb@191
  1259
    assert(keylist);
vb@191
  1260
vb@191
  1261
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
  1262
        return PEP_ILLEGAL_VALUE;
vb@191
  1263
vb@951
  1264
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text,
vb@951
  1265
            size, signature, sig_size, keylist);
vb@24
  1266
}
vb@24
  1267
vb@24
  1268
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1269
{
vb@191
  1270
    assert(session);
vb@191
  1271
    assert(fpr);
vb@191
  1272
vb@191
  1273
    if (!(session && fpr))
vb@191
  1274
        return PEP_ILLEGAL_VALUE;
vb@191
  1275
vb@46
  1276
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
  1277
}
vb@24
  1278
vb@24
  1279
DYNAMIC_API PEP_STATUS export_key(
vb@46
  1280
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1281
    )
vb@24
  1282
{
vb@191
  1283
    assert(session);
vb@191
  1284
    assert(fpr);
vb@191
  1285
    assert(key_data);
vb@191
  1286
    assert(size);
vb@191
  1287
vb@191
  1288
    if (!(session && fpr && key_data && size))
vb@191
  1289
        return PEP_ILLEGAL_VALUE;
vb@191
  1290
vb@951
  1291
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr,
vb@951
  1292
            key_data, size);
vb@24
  1293
}
vb@24
  1294
vb@24
  1295
DYNAMIC_API PEP_STATUS find_keys(
vb@46
  1296
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1297
    )
vb@24
  1298
{
vb@191
  1299
    assert(session);
vb@191
  1300
    assert(pattern);
vb@191
  1301
    assert(keylist);
vb@191
  1302
vb@191
  1303
    if (!(session && pattern && keylist))
vb@191
  1304
        return PEP_ILLEGAL_VALUE;
vb@191
  1305
vb@951
  1306
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern,
vb@951
  1307
            keylist);
vb@24
  1308
}
vb@24
  1309
krista@1005
  1310
vb@24
  1311
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
  1312
        PEP_SESSION session, pEp_identity *identity
vb@24
  1313
    )
vb@24
  1314
{
vb@191
  1315
    assert(session);
vb@191
  1316
    assert(identity);
vb@191
  1317
    assert(identity->address);
vb@298
  1318
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
  1319
    assert(identity->username);
vb@191
  1320
vb@298
  1321
    if (!(session && identity && identity->address &&
vb@951
  1322
            (identity->fpr == NULL || identity->fpr[0] == 0) &&
vb@951
  1323
            identity->username))
vb@191
  1324
        return PEP_ILLEGAL_VALUE;
vb@191
  1325
vb@944
  1326
    PEP_STATUS status =
vb@944
  1327
        session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session,
vb@944
  1328
                identity);
vb@944
  1329
    if (status != PEP_STATUS_OK)
vb@944
  1330
        return status;
vb@944
  1331
vb@944
  1332
    // if a state machine for keysync is in place, inject notify
vb@944
  1333
    if (session->sync_state != DeviceState_state_NONE)
vb@951
  1334
        status = fsm_DeviceState_inject(session, KeyGen, NULL, NULL);
vb@944
  1335
vb@944
  1336
    return status;
vb@24
  1337
}
vb@24
  1338
vb@9
  1339
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
  1340
        PEP_SESSION session,
vb@46
  1341
        const char *fpr,
vb@46
  1342
        PEP_comm_type *comm_type
vb@9
  1343
    )
vb@9
  1344
{
vb@191
  1345
    assert(session);
vb@191
  1346
    assert(fpr);
vb@191
  1347
    assert(comm_type);
vb@191
  1348
vb@191
  1349
    if (!(session && fpr && comm_type))
vb@191
  1350
        return PEP_ILLEGAL_VALUE;
vb@191
  1351
vb@951
  1352
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr,
vb@951
  1353
            comm_type);
vb@24
  1354
}
vb@9
  1355
Edouard@728
  1356
DYNAMIC_API PEP_STATUS import_key(
Edouard@728
  1357
        PEP_SESSION session,
Edouard@728
  1358
        const char *key_data,
Edouard@728
  1359
        size_t size,
Edouard@728
  1360
        identity_list **private_keys
Edouard@728
  1361
    )
vb@24
  1362
{
vb@191
  1363
    assert(session);
vb@191
  1364
    assert(key_data);
vb@191
  1365
vb@191
  1366
    if (!(session && key_data))
vb@191
  1367
        return PEP_ILLEGAL_VALUE;
vb@191
  1368
vb@951
  1369
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data,
vb@951
  1370
            size, private_keys);
vb@24
  1371
}
vb@9
  1372
vb@24
  1373
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1374
{
vb@191
  1375
    assert(session);
vb@191
  1376
    assert(pattern);
vb@191
  1377
vb@191
  1378
    if (!(session && pattern))
vb@191
  1379
        return PEP_ILLEGAL_VALUE;
vb@191
  1380
vb@46
  1381
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1382
}
vb@9
  1383
vb@24
  1384
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1385
{
vb@191
  1386
    assert(session);
vb@191
  1387
    assert(pattern);
vb@191
  1388
vb@191
  1389
    if (!(session && pattern))
vb@191
  1390
        return PEP_ILLEGAL_VALUE;
vb@191
  1391
vb@46
  1392
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1393
}
vb@196
  1394
vb@201
  1395
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1396
        PEP_SESSION session,
vb@201
  1397
        const char *fpr,
vb@201
  1398
        const timestamp *ts
vb@201
  1399
    )
vb@196
  1400
{
vb@196
  1401
    assert(session);
vb@200
  1402
    assert(fpr);
vb@196
  1403
vb@200
  1404
    if (!(session && fpr))
vb@196
  1405
        return PEP_ILLEGAL_VALUE;
vb@196
  1406
vb@201
  1407
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1408
}
vb@196
  1409
vb@211
  1410
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1411
        PEP_SESSION session,
vb@211
  1412
        const char *fpr,
vb@211
  1413
        const char *reason
vb@211
  1414
    )
vb@197
  1415
{
vb@197
  1416
    assert(session);
vb@200
  1417
    assert(fpr);
vb@197
  1418
vb@200
  1419
    if (!(session && fpr))
vb@197
  1420
        return PEP_ILLEGAL_VALUE;
vb@197
  1421
vb@214
  1422
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1423
            reason);
vb@197
  1424
}
vb@197
  1425
vb@214
  1426
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1427
        PEP_SESSION session,
vb@214
  1428
        const char *fpr,
Edouard@701
  1429
        const time_t when,
vb@214
  1430
        bool *expired
vb@214
  1431
    )
vb@214
  1432
{
vb@214
  1433
    assert(session);
vb@214
  1434
    assert(fpr);
vb@214
  1435
    assert(expired);
vb@214
  1436
vb@214
  1437
    if (!(session && fpr && expired))
vb@214
  1438
        return PEP_ILLEGAL_VALUE;
vb@214
  1439
vb@214
  1440
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
Edouard@701
  1441
            when, expired);
vb@214
  1442
}
vb@214
  1443
Edouard@694
  1444
DYNAMIC_API PEP_STATUS key_revoked(
vb@951
  1445
       PEP_SESSION session,
vb@951
  1446
       const char *fpr,
vb@951
  1447
       bool *revoked
vb@951
  1448
   )
Edouard@694
  1449
{
Edouard@694
  1450
    assert(session);
Edouard@694
  1451
    assert(fpr);
Edouard@694
  1452
    assert(revoked);
Edouard@694
  1453
    
Edouard@694
  1454
    if (!(session && fpr && revoked))
Edouard@694
  1455
        return PEP_ILLEGAL_VALUE;
Edouard@694
  1456
    
Edouard@694
  1457
    return session->cryptotech[PEP_crypt_OpenPGP].key_revoked(session, fpr,
vb@951
  1458
            revoked);
Edouard@694
  1459
}
Edouard@694
  1460
vb@451
  1461
static void _clean_log_value(char *text)
vb@451
  1462
{
vb@451
  1463
    if (text) {
vb@451
  1464
        for (char *c = text; *c; c++) {
vb@722
  1465
            if (*c < 32 && *c != '\n')
vb@451
  1466
                *c = 32;
vb@451
  1467
            else if (*c == '"')
vb@451
  1468
                *c = '\'';
vb@451
  1469
        }
vb@451
  1470
    }
vb@451
  1471
}
vb@451
  1472
vb@450
  1473
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1474
{
vb@454
  1475
    str2 = str2 ? str2 : "";
vb@450
  1476
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1477
    size_t len2 = strlen(str2);
vb@450
  1478
    size_t len = len1 + len2 + 3;
vb@450
  1479
    char * result = realloc(str1, len + 1);
vb@450
  1480
vb@450
  1481
    if (result) {
vb@450
  1482
        result[len1] = '"';
vb@450
  1483
        strcpy(result + len1 + 1, str2);
vb@450
  1484
        result[len - 2] = '"';
vb@450
  1485
        result[len - 1] = delim;
vb@450
  1486
        result[len] = 0;
vb@450
  1487
    }
vb@450
  1488
    else {
vb@450
  1489
        free(str1);
vb@450
  1490
    }
vb@450
  1491
vb@450
  1492
    return result;
vb@450
  1493
}
vb@450
  1494
vb@450
  1495
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1496
        PEP_SESSION session,
vb@450
  1497
        int maxlines,
vb@450
  1498
        char **logdata
vb@450
  1499
    )
vb@450
  1500
{
vb@450
  1501
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1502
    char *_logdata= NULL;
vb@450
  1503
vb@450
  1504
    assert(session);
vb@450
  1505
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1506
    assert(logdata);
vb@450
  1507
vb@450
  1508
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1509
            CRASHDUMP_MAX_LINES))
vb@450
  1510
        return PEP_ILLEGAL_VALUE;
vb@450
  1511
vb@632
  1512
    *logdata = NULL;
vb@632
  1513
vb@450
  1514
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1515
    const char *timestamp = NULL;
vb@458
  1516
    const char *title = NULL;
vb@458
  1517
    const char *entity = NULL;
vb@458
  1518
    const char *desc = NULL;
vb@458
  1519
    const char *comment = NULL;
vb@450
  1520
vb@450
  1521
    sqlite3_reset(session->crashdump);
vb@452
  1522
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1523
vb@450
  1524
    int result;
vb@450
  1525
vb@450
  1526
    do {
vb@450
  1527
        result = sqlite3_step(session->crashdump);
vb@450
  1528
        switch (result) {
vb@450
  1529
        case SQLITE_ROW:
vb@951
  1530
            timestamp = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1531
                    0);
vb@951
  1532
            title   = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1533
                    1);
vb@951
  1534
            entity  = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1535
                    2);
vb@951
  1536
            desc    = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1537
                    3);
vb@951
  1538
            comment = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1539
                    4);
vb@452
  1540
vb@452
  1541
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1542
            if (_logdata == NULL)
vb@452
  1543
                goto enomem;
vb@450
  1544
vb@450
  1545
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1546
            if (_logdata == NULL)
vb@450
  1547
                goto enomem;
vb@450
  1548
vb@450
  1549
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1550
            if (_logdata == NULL)
vb@450
  1551
                goto enomem;
vb@450
  1552
vb@450
  1553
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1554
            if (_logdata == NULL)
vb@450
  1555
                goto enomem;
vb@450
  1556
vb@450
  1557
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1558
            if (_logdata == NULL)
vb@450
  1559
                goto enomem;
vb@450
  1560
vb@687
  1561
            _clean_log_value(_logdata);
vb@450
  1562
            break;
vb@450
  1563
vb@450
  1564
        case SQLITE_DONE:
vb@450
  1565
            break;
vb@450
  1566
vb@450
  1567
        default:
vb@450
  1568
            status = PEP_UNKNOWN_ERROR;
vb@450
  1569
            result = SQLITE_DONE;
vb@450
  1570
        }
vb@450
  1571
    } while (result != SQLITE_DONE);
vb@450
  1572
vb@450
  1573
    sqlite3_reset(session->crashdump);
vb@450
  1574
    if (status == PEP_STATUS_OK)
vb@450
  1575
        *logdata = _logdata;
vb@450
  1576
vb@450
  1577
    goto the_end;
vb@450
  1578
vb@450
  1579
enomem:
vb@450
  1580
    status = PEP_OUT_OF_MEMORY;
vb@450
  1581
vb@450
  1582
the_end:
vb@450
  1583
    return status;
vb@450
  1584
}
vb@450
  1585
vb@458
  1586
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  1587
        PEP_SESSION session,
vb@458
  1588
        char **languages
vb@458
  1589
    )
vb@458
  1590
{
vb@458
  1591
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1592
    char *_languages= NULL;
vb@458
  1593
vb@458
  1594
    assert(session);
vb@458
  1595
    assert(languages);
vb@458
  1596
vb@458
  1597
    if (!(session && languages))
vb@458
  1598
        return PEP_ILLEGAL_VALUE;
vb@458
  1599
vb@632
  1600
    *languages = NULL;
vb@632
  1601
vb@458
  1602
    const char *lang = NULL;
vb@458
  1603
    const char *name = NULL;
vb@617
  1604
    const char *phrase = NULL;
vb@458
  1605
vb@458
  1606
    sqlite3_reset(session->languagelist);
vb@458
  1607
vb@458
  1608
    int result;
vb@458
  1609
vb@458
  1610
    do {
vb@458
  1611
        result = sqlite3_step(session->languagelist);
vb@458
  1612
        switch (result) {
vb@458
  1613
        case SQLITE_ROW:
vb@951
  1614
            lang = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  1615
                    0);
vb@951
  1616
            name = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  1617
                    1);
vb@951
  1618
            phrase = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  1619
                    2);
vb@458
  1620
vb@458
  1621
            _languages = _concat_string(_languages, lang, ',');
vb@458
  1622
            if (_languages == NULL)
vb@458
  1623
                goto enomem;
vb@458
  1624
vb@617
  1625
            _languages = _concat_string(_languages, name, ',');
vb@617
  1626
            if (_languages == NULL)
vb@617
  1627
                goto enomem;
vb@617
  1628
vb@617
  1629
            _languages = _concat_string(_languages, phrase, '\n');
vb@458
  1630
            if (_languages == NULL)
vb@458
  1631
                goto enomem;
vb@458
  1632
vb@458
  1633
            break;
vb@458
  1634
vb@458
  1635
        case SQLITE_DONE:
vb@458
  1636
            break;
vb@458
  1637
vb@458
  1638
        default:
vb@458
  1639
            status = PEP_UNKNOWN_ERROR;
vb@458
  1640
            result = SQLITE_DONE;
vb@458
  1641
        }
vb@458
  1642
    } while (result != SQLITE_DONE);
vb@458
  1643
vb@458
  1644
    sqlite3_reset(session->languagelist);
vb@458
  1645
    if (status == PEP_STATUS_OK)
vb@458
  1646
        *languages = _languages;
vb@458
  1647
vb@458
  1648
    goto the_end;
vb@458
  1649
vb@458
  1650
enomem:
vb@458
  1651
    status = PEP_OUT_OF_MEMORY;
vb@458
  1652
vb@458
  1653
the_end:
vb@458
  1654
    return status;
vb@458
  1655
}
vb@458
  1656
vb@458
  1657
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  1658
        PEP_SESSION session,
vb@458
  1659
        const char *lang,
vb@458
  1660
        int phrase_id,
vb@458
  1661
        char **phrase
vb@458
  1662
    )
vb@458
  1663
{
vb@458
  1664
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1665
vb@479
  1666
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@479
  1667
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  1668
        return PEP_ILLEGAL_VALUE;
vb@458
  1669
vb@632
  1670
    *phrase = NULL;
vb@632
  1671
vb@458
  1672
    sqlite3_reset(session->i18n_token);
roker@529
  1673
    sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
roker@529
  1674
    sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  1675
vb@458
  1676
    const char *_phrase = NULL;
vb@458
  1677
    int result;
vb@458
  1678
vb@458
  1679
    result = sqlite3_step(session->i18n_token);
vb@458
  1680
    switch (result) {
vb@458
  1681
    case SQLITE_ROW:
vb@458
  1682
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  1683
        break;
vb@458
  1684
vb@458
  1685
    case SQLITE_DONE:
vb@458
  1686
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  1687
        break;
vb@458
  1688
vb@458
  1689
    default:
vb@458
  1690
        status = PEP_UNKNOWN_ERROR;
vb@458
  1691
    }
vb@458
  1692
vb@458
  1693
    if (status == PEP_STATUS_OK) {
vb@458
  1694
        *phrase = strdup(_phrase);
vb@458
  1695
        if (*phrase == NULL)
vb@458
  1696
            goto enomem;
vb@458
  1697
    }
vb@459
  1698
vb@459
  1699
    sqlite3_reset(session->i18n_token);
vb@458
  1700
    goto the_end;
vb@458
  1701
vb@458
  1702
enomem:
vb@458
  1703
    status = PEP_OUT_OF_MEMORY;
vb@458
  1704
vb@458
  1705
the_end:
vb@458
  1706
    return status;
vb@458
  1707
}
vb@458
  1708
vb@1085
  1709
static PEP_STATUS _get_sequence_value(PEP_SESSION session, const char *name,
vb@1085
  1710
        int32_t *value)
vb@1085
  1711
{
vb@1086
  1712
    assert(session && name && value);
vb@1086
  1713
    if (!(session && name && value))
vb@1086
  1714
        return PEP_ILLEGAL_VALUE;
vb@1086
  1715
vb@1085
  1716
    PEP_STATUS status = PEP_STATUS_OK;
vb@1085
  1717
vb@1085
  1718
    sqlite3_reset(session->sequence_value2);
vb@1085
  1719
    sqlite3_bind_text(session->sequence_value2, 1, name, -1,
vb@1085
  1720
            SQLITE_STATIC);
vb@1085
  1721
    int result = sqlite3_step(session->sequence_value2);
vb@1085
  1722
    switch (result) {
vb@1085
  1723
        case SQLITE_ROW: {
vb@1085
  1724
            int32_t _value = (int32_t)
vb@1086
  1725
                    sqlite3_column_int(session->sequence_value2, 0);
vb@1086
  1726
            int _own = (int)
vb@1086
  1727
                    sqlite3_column_int(session->sequence_value2, 1);
vb@1085
  1728
            *value = _value;
vb@1086
  1729
            if (_own)
vb@1086
  1730
                status = PEP_OWN_SEQUENCE;
vb@1085
  1731
            break;
vb@1085
  1732
        }
vb@1091
  1733
        case SQLITE_DONE:
vb@1091
  1734
            status = PEP_RECORD_NOT_FOUND;
vb@1091
  1735
            break;
vb@1085
  1736
        default:
vb@1085
  1737
            status = PEP_UNKNOWN_ERROR;
vb@1085
  1738
    }
vb@1085
  1739
    sqlite3_reset(session->sequence_value2);
vb@1085
  1740
vb@1085
  1741
    return status;
vb@1085
  1742
}
vb@1085
  1743
vb@1086
  1744
static PEP_STATUS _increment_sequence_value(PEP_SESSION session,
vb@1086
  1745
        const char *name, int own)
vb@1085
  1746
{
vb@1086
  1747
    assert(session && name);
vb@1086
  1748
    if (!(session && name))
vb@1086
  1749
        return PEP_ILLEGAL_VALUE;
vb@1086
  1750
vb@1085
  1751
    sqlite3_reset(session->sequence_value1);
vb@1085
  1752
    sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
vb@1086
  1753
    sqlite3_bind_int(session->sequence_value1, 2, own);
vb@1085
  1754
    int result = sqlite3_step(session->sequence_value1);
vb@1085
  1755
    assert(result == SQLITE_DONE);
vb@1085
  1756
    sqlite3_reset(session->sequence_value1);
vb@1085
  1757
    if (result == SQLITE_DONE)
vb@1085
  1758
        return PEP_STATUS_OK;
vb@1085
  1759
    else
vb@1085
  1760
        return PEP_CANNOT_INCREASE_SEQUENCE;
vb@1085
  1761
}
vb@1085
  1762
vb@1087
  1763
static PEP_STATUS _set_sequence_value(PEP_SESSION session,
vb@1091
  1764
        const char *name, int32_t value, int own)
vb@1087
  1765
{
vb@1087
  1766
    assert(session && name && value > 0);
vb@1087
  1767
    if (!(session && name && value > 0))
vb@1087
  1768
        return PEP_ILLEGAL_VALUE;
vb@1087
  1769
vb@1087
  1770
    sqlite3_reset(session->sequence_value3);
vb@1087
  1771
    sqlite3_bind_text(session->sequence_value3, 1, name, -1, SQLITE_STATIC);
vb@1087
  1772
    sqlite3_bind_int(session->sequence_value3, 2, value);
vb@1091
  1773
    sqlite3_bind_int(session->sequence_value3, 3, own);
vb@1087
  1774
    int result = sqlite3_step(session->sequence_value3);
vb@1087
  1775
    assert(result == SQLITE_DONE);
vb@1087
  1776
    sqlite3_reset(session->sequence_value3);
vb@1087
  1777
    if (result == SQLITE_DONE)
vb@1087
  1778
        return PEP_STATUS_OK;
vb@1087
  1779
    else
vb@1087
  1780
        return PEP_CANNOT_SET_SEQUENCE_VALUE;
vb@1087
  1781
}
vb@1087
  1782
vb@632
  1783
DYNAMIC_API PEP_STATUS sequence_value(
vb@632
  1784
        PEP_SESSION session,
vb@1085
  1785
        char *name,
vb@652
  1786
        int32_t *value
vb@632
  1787
    )
vb@632
  1788
{
vb@632
  1789
    PEP_STATUS status = PEP_STATUS_OK;
vb@632
  1790
vb@632
  1791
    assert(session);
vb@1085
  1792
    assert(name && value && *value >= 0);
vb@632
  1793
vb@1085
  1794
    if (!(session && name && value && *value >= 0))
vb@632
  1795
        return PEP_ILLEGAL_VALUE;
vb@632
  1796
vb@1086
  1797
    int own = 0;
vb@1085
  1798
    if (!name[0]) {
vb@1085
  1799
        unsigned char uuid[16];
vb@1085
  1800
        uuid_generate_random(uuid);
vb@1085
  1801
        uuid_unparse_upper(uuid, name);
vb@1086
  1802
        own = 1;
vb@1085
  1803
    }
vb@1091
  1804
    else {
vb@1091
  1805
        if (name == sync_uuid || strcmp(name, sync_uuid) == 0)
vb@1091
  1806
            own = 1;
vb@1091
  1807
    }
vb@632
  1808
vb@1085
  1809
    if (*value) {
vb@1085
  1810
        int32_t old_value = 0;
vb@1085
  1811
        status = _get_sequence_value(session, name, &old_value);
vb@1091
  1812
        if (status != PEP_STATUS_OK && status != PEP_RECORD_NOT_FOUND)
vb@1085
  1813
            return status;
vb@1085
  1814
vb@1087
  1815
        if (old_value >= *value) {
vb@1085
  1816
            return PEP_SEQUENCE_VIOLATED;
vb@1087
  1817
        }
vb@1087
  1818
        else {
vb@1091
  1819
            status = _set_sequence_value(session, name, *value, own);
vb@1087
  1820
            return status;
vb@1087
  1821
        }
vb@633
  1822
    }
vb@1085
  1823
vb@1085
  1824
    assert(*value == 0);
vb@1086
  1825
    status = _increment_sequence_value(session, name, own);
vb@1086
  1826
    if (status == PEP_STATUS_OK) {
vb@1085
  1827
        status = _get_sequence_value(session, name, value);
vb@1086
  1828
        assert(*value < INT32_MAX);
vb@1086
  1829
        if (*value == INT32_MAX)
vb@1086
  1830
            return PEP_CANNOT_INCREASE_SEQUENCE;
vb@1086
  1831
    }
vb@632
  1832
    return status;
vb@632
  1833
}
vb@632
  1834
Edouard@693
  1835
DYNAMIC_API PEP_STATUS set_revoked(
Edouard@693
  1836
       PEP_SESSION session,
Edouard@693
  1837
       const char *revoked_fpr,
Edouard@693
  1838
       const char *replacement_fpr,
Edouard@693
  1839
       const uint64_t revocation_date
Edouard@693
  1840
    )
Edouard@693
  1841
{
Edouard@693
  1842
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  1843
    
Edouard@693
  1844
    assert(session &&
Edouard@693
  1845
           revoked_fpr && revoked_fpr[0] &&
Edouard@693
  1846
           replacement_fpr && replacement_fpr[0]
Edouard@693
  1847
          );
Edouard@693
  1848
    
Edouard@693
  1849
    if (!(session &&
Edouard@693
  1850
          revoked_fpr && revoked_fpr[0] &&
Edouard@693
  1851
          replacement_fpr && replacement_fpr[0]
Edouard@693
  1852
         ))
Edouard@693
  1853
        return PEP_ILLEGAL_VALUE;
Edouard@693
  1854
    
Edouard@693
  1855
    sqlite3_reset(session->set_revoked);
Edouard@693
  1856
    sqlite3_bind_text(session->set_revoked, 1, revoked_fpr, -1, SQLITE_STATIC);
vb@951
  1857
    sqlite3_bind_text(session->set_revoked, 2, replacement_fpr, -1,
vb@951
  1858
            SQLITE_STATIC);
Edouard@693
  1859
    sqlite3_bind_int64(session->set_revoked, 3, revocation_date);
Edouard@693
  1860
Edouard@693
  1861
    int result;
Edouard@693
  1862
    
Edouard@693
  1863
    result = sqlite3_step(session->set_revoked);
Edouard@693
  1864
    switch (result) {
Edouard@693
  1865
        case SQLITE_DONE:
Edouard@693
  1866
            status = PEP_STATUS_OK;
Edouard@693
  1867
            break;
Edouard@693
  1868
            
Edouard@693
  1869
        default:
Edouard@693
  1870
            status = PEP_UNKNOWN_ERROR;
Edouard@693
  1871
    }
Edouard@693
  1872
    
Edouard@693
  1873
    sqlite3_reset(session->set_revoked);
Edouard@693
  1874
    return status;
Edouard@693
  1875
}
Edouard@693
  1876
Edouard@693
  1877
DYNAMIC_API PEP_STATUS get_revoked(
Edouard@693
  1878
        PEP_SESSION session,
Edouard@693
  1879
        const char *fpr,
Edouard@693
  1880
        char **revoked_fpr,
Edouard@693
  1881
        uint64_t *revocation_date
Edouard@693
  1882
    )
Edouard@693
  1883
{
Edouard@693
  1884
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  1885
Edouard@693
  1886
    assert(session &&
Edouard@693
  1887
           revoked_fpr &&
Edouard@693
  1888
           fpr && fpr[0]
Edouard@693
  1889
          );
Edouard@693
  1890
    
Edouard@693
  1891
    if (!(session &&
Edouard@693
  1892
           revoked_fpr &&
Edouard@693
  1893
           fpr && fpr[0]
Edouard@693
  1894
          ))
Edouard@693
  1895
        return PEP_ILLEGAL_VALUE;
Edouard@693
  1896
Edouard@693
  1897
    *revoked_fpr = NULL;
Edouard@693
  1898
    *revocation_date = 0;
Edouard@693
  1899
Edouard@693
  1900
    sqlite3_reset(session->get_revoked);
Edouard@693
  1901
    sqlite3_bind_text(session->get_revoked, 1, fpr, -1, SQLITE_STATIC);
Edouard@693
  1902
Edouard@693
  1903
    int result;
Edouard@693
  1904
    
Edouard@693
  1905
    result = sqlite3_step(session->get_revoked);
Edouard@693
  1906
    switch (result) {
Edouard@693
  1907
        case SQLITE_ROW: {
vb@951
  1908
            *revoked_fpr = strdup((const char *)
vb@951
  1909
                    sqlite3_column_text(session->get_revoked, 0));
Edouard@693
  1910
            if(*revoked_fpr)
vb@951
  1911
                *revocation_date = sqlite3_column_int64(session->get_revoked,
vb@951
  1912
                        1);
Edouard@693
  1913
            else
Edouard@693
  1914
                status = PEP_OUT_OF_MEMORY;
Edouard@693
  1915
Edouard@693
  1916
            break;
Edouard@693
  1917
        }
Edouard@693
  1918
        default:
Edouard@693
  1919
            status = PEP_CANNOT_FIND_IDENTITY;
Edouard@693
  1920
    }
Edouard@693
  1921
Edouard@693
  1922
    sqlite3_reset(session->get_revoked);
Edouard@693
  1923
Edouard@693
  1924
    return status;
Edouard@693
  1925
}
Edouard@693
  1926
vb@958
  1927
PEP_STATUS key_created(
vb@958
  1928
        PEP_SESSION session,
vb@958
  1929
        const char *fpr,
vb@958
  1930
        time_t *created
vb@958
  1931
    )
vb@958
  1932
{
vb@958
  1933
    assert(session && fpr && created);
vb@958
  1934
    if (!(session && fpr && created))
vb@958
  1935
        return PEP_ILLEGAL_VALUE;
vb@958
  1936
vb@958
  1937
    return session->cryptotech[PEP_crypt_OpenPGP].key_created(session, fpr,
vb@958
  1938
            created);
vb@958
  1939
}
vb@958
  1940
krista@1011
  1941
DYNAMIC_API const char* get_engine_version() {
krista@1011
  1942
    return PEP_ENGINE_VERSION;
krista@1011
  1943
}
krista@1011
  1944
krista@1011
  1945
vb@482
  1946
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  1947
{
vb@482
  1948
    assert(session);
vb@482
  1949
vb@482
  1950
    if (!session)
vb@482
  1951
        return PEP_ILLEGAL_VALUE;
vb@482
  1952
fdik@494
  1953
    int int_result = sqlite3_exec(
fdik@494
  1954
        session->db,
fdik@494
  1955
        "delete from identity where address like '%@peptest.ch' ;",
fdik@494
  1956
        NULL,
fdik@494
  1957
        NULL,
fdik@494
  1958
        NULL
fdik@494
  1959
    );
fdik@494
  1960
    assert(int_result == SQLITE_OK);
vb@482
  1961
fdik@494
  1962
    return PEP_STATUS_OK;
vb@482
  1963
}
vb@482
  1964