src/pEpEngine.c
author Volker Birk <vb@pep.foundation>
Thu, 01 Sep 2016 17:54:33 +0200
branchkeysync
changeset 1110 d37d730fa8e5
parent 1109 6be9dc59517d
child 1133 12bff61b08a8
permissions -rw-r--r--
implementing keep_sync_msg flag
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@62
   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@928
   247
                NULL,
vb@928
   248
                NULL,
vb@928
   249
                NULL
vb@928
   250
            );
vb@928
   251
        }
vb@928
   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@65
   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@0
   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
Edouard@720
   536
#ifdef ANDROID
Edouard@720
   537
    _session->use_only_own_private_keys = true;
Edouard@720
   538
#elif TARGET_OS_IPHONE
Edouard@720
   539
    _session->use_only_own_private_keys = true;
Edouard@720
   540
#else
Edouard@720
   541
    _session->use_only_own_private_keys = false;
Edouard@720
   542
#endif
vb@464
   543
roker@529
   544
    *session = _session;
roker@529
   545
    return PEP_STATUS_OK;
vb@62
   546
vb@65
   547
enomem:
vb@65
   548
    status = PEP_OUT_OF_MEMORY;
vb@65
   549
vb@65
   550
pep_error:
vb@65
   551
    release(_session);
vb@65
   552
    return status;
vb@0
   553
}
vb@0
   554
vb@0
   555
DYNAMIC_API void release(PEP_SESSION session)
vb@0
   556
{
vb@62
   557
    bool out_last = false;
vb@62
   558
vb@62
   559
    assert(init_count >= 0);
roker@529
   560
    assert(session);
vb@0
   561
Edouard@385
   562
    if (!((init_count >= 0) && session))
vb@191
   563
        return;
vb@191
   564
vb@62
   565
    // a small race condition but still a race condition
vb@113
   566
    // mitigated by calling caveat (see documentation)
vb@62
   567
vb@62
   568
    if (init_count == 0)
vb@62
   569
        out_last = true;
vb@62
   570
    --init_count;
vb@62
   571
roker@529
   572
    if (session) {
vb@986
   573
        if (session->sync_state != DeviceState_state_NONE)
vb@986
   574
            unregister_sync_callbacks(session);
vb@986
   575
roker@529
   576
        if (session->db) {
vb@65
   577
            if (session->log)
vb@65
   578
                sqlite3_finalize(session->log);
vb@517
   579
            if (session->trustword)
vb@517
   580
                sqlite3_finalize(session->trustword);
vb@65
   581
            if (session->get_identity)
vb@65
   582
                sqlite3_finalize(session->get_identity);
vb@65
   583
            if (session->set_person)
vb@65
   584
                sqlite3_finalize(session->set_person);
vb@65
   585
            if (session->set_pgp_keypair)
vb@65
   586
                sqlite3_finalize(session->set_pgp_keypair);
vb@517
   587
            if (session->set_identity)
vb@517
   588
                sqlite3_finalize(session->set_identity);
vb@932
   589
            if (session->set_identity_flags)
vb@932
   590
                sqlite3_finalize(session->set_identity_flags);
vb@65
   591
            if (session->set_trust)
vb@65
   592
                sqlite3_finalize(session->set_trust);
vb@65
   593
            if (session->get_trust)
vb@65
   594
                sqlite3_finalize(session->get_trust);
vb@251
   595
            if (session->least_trust)
vb@251
   596
                sqlite3_finalize(session->least_trust);
vb@517
   597
            if (session->mark_compromized)
vb@517
   598
                sqlite3_finalize(session->mark_compromized);
vb@517
   599
            if (session->crashdump)
vb@517
   600
                sqlite3_finalize(session->crashdump);
vb@517
   601
            if (session->languagelist)
vb@517
   602
                sqlite3_finalize(session->languagelist);
vb@517
   603
            if (session->i18n_token)
vb@517
   604
                sqlite3_finalize(session->i18n_token);
vb@517
   605
            if (session->blacklist_add)
vb@517
   606
                sqlite3_finalize(session->blacklist_add);
vb@517
   607
            if (session->blacklist_delete)
vb@517
   608
                sqlite3_finalize(session->blacklist_delete);
vb@517
   609
            if (session->blacklist_is_listed)
vb@517
   610
                sqlite3_finalize(session->blacklist_is_listed);
vb@517
   611
            if (session->blacklist_retrieve)
vb@517
   612
                sqlite3_finalize(session->blacklist_retrieve);
krista@957
   613
            if (session->own_key_is_listed)
krista@957
   614
                sqlite3_finalize(session->own_key_is_listed);
roker@1002
   615
            if (session->own_identities_retrieve)
roker@1002
   616
                sqlite3_finalize(session->own_identities_retrieve);
krista@957
   617
            if (session->sequence_value1)
krista@957
   618
                sqlite3_finalize(session->sequence_value1);
krista@957
   619
            if (session->sequence_value2)
krista@960
   620
                sqlite3_finalize(session->sequence_value2);
vb@1085
   621
            if (session->sequence_value3)
vb@1085
   622
                sqlite3_finalize(session->sequence_value3);
krista@957
   623
            if (session->set_revoked)
krista@957
   624
                sqlite3_finalize(session->set_revoked);
krista@957
   625
            if (session->get_revoked)
krista@957
   626
                sqlite3_finalize(session->get_revoked);
vb@26
   627
vb@65
   628
            if (session->db)
vb@65
   629
                sqlite3_close_v2(session->db);
vb@65
   630
            if (session->system_db)
vb@65
   631
                sqlite3_close_v2(session->system_db);
roker@529
   632
        }
vb@28
   633
vb@65
   634
        release_transport_system(session, out_last);
vb@65
   635
        release_cryptotech(session, out_last);
vb@62
   636
vb@65
   637
        free(session);
vb@65
   638
    }
vb@0
   639
}
vb@0
   640
vb@467
   641
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
   642
{
vb@464
   643
    assert(session);
vb@467
   644
    session->passive_mode = enable;
vb@464
   645
}
vb@464
   646
vb@467
   647
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
   648
{
vb@464
   649
    assert(session);
vb@467
   650
    session->unencrypted_subject = enable;
vb@464
   651
}
vb@464
   652
vb@951
   653
DYNAMIC_API void config_use_only_own_private_keys(PEP_SESSION session,
vb@951
   654
        bool enable)
Edouard@720
   655
{
Edouard@720
   656
    assert(session);
Edouard@720
   657
    session->use_only_own_private_keys = enable;
Edouard@720
   658
}
Edouard@720
   659
vb@1110
   660
DYNAMIC_API void config_keep_sync_msg(PEP_SESSION session, bool enable)
vb@1109
   661
{
vb@1109
   662
    assert(session);
vb@1110
   663
    session->keep_sync_msg = enable;
vb@1109
   664
}
vb@1109
   665
vb@0
   666
DYNAMIC_API PEP_STATUS log_event(
vb@450
   667
        PEP_SESSION session,
vb@451
   668
        const char *title,
vb@451
   669
        const char *entity,
vb@451
   670
        const char *description,
vb@451
   671
        const char *comment
vb@0
   672
    )
vb@0
   673
{
roker@529
   674
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   675
    int result;
vb@0
   676
roker@529
   677
    assert(session);
roker@529
   678
    assert(title);
roker@529
   679
    assert(entity);
vb@0
   680
vb@191
   681
    if (!(session && title && entity))
vb@191
   682
        return PEP_ILLEGAL_VALUE;
vb@191
   683
roker@529
   684
    sqlite3_reset(session->log);
roker@529
   685
    sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
roker@529
   686
    sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
roker@529
   687
    if (description)
vb@46
   688
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
roker@529
   689
    else
roker@529
   690
        sqlite3_bind_null(session->log, 3);
roker@529
   691
    if (comment)
roker@529
   692
        sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
roker@529
   693
    else
roker@529
   694
        sqlite3_bind_null(session->log, 4);
roker@529
   695
    do {
roker@529
   696
        result = sqlite3_step(session->log);
roker@529
   697
        assert(result == SQLITE_DONE || result == SQLITE_BUSY);
roker@529
   698
        if (result != SQLITE_DONE && result != SQLITE_BUSY)
roker@529
   699
            status = PEP_UNKNOWN_ERROR;
roker@529
   700
    } while (result == SQLITE_BUSY);
roker@529
   701
    sqlite3_reset(session->log);
vb@0
   702
roker@529
   703
    return status;
vb@0
   704
}
vb@0
   705
vb@233
   706
DYNAMIC_API PEP_STATUS trustword(
vb@0
   707
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   708
            char **word, size_t *wsize
vb@0
   709
        )
vb@0
   710
{
roker@529
   711
    PEP_STATUS status = PEP_STATUS_OK;
vb@0
   712
roker@529
   713
    assert(session);
roker@529
   714
    assert(word);
roker@529
   715
    assert(wsize);
vb@0
   716
vb@191
   717
    if (!(session && word && wsize))
vb@191
   718
        return PEP_ILLEGAL_VALUE;
vb@191
   719
roker@529
   720
    *word = NULL;
roker@529
   721
    *wsize = 0;
vb@0
   722
roker@529
   723
    if (lang == NULL)
roker@529
   724
        lang = "en";
vb@0
   725
roker@529
   726
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   727
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   728
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   729
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   730
    assert(lang[2] == 0);
vb@0
   731
roker@529
   732
    sqlite3_reset(session->trustword);
vb@233
   733
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
roker@529
   734
    sqlite3_bind_int(session->trustword, 2, value);
vb@0
   735
roker@880
   736
    const int result = sqlite3_step(session->trustword);
roker@529
   737
    if (result == SQLITE_ROW) {
vb@233
   738
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
   739
                    1));
roker@529
   740
        if (*word)
vb@233
   741
            *wsize = sqlite3_column_bytes(session->trustword, 1);
roker@529
   742
        else
Edouard@693
   743
            status = PEP_OUT_OF_MEMORY;
roker@529
   744
    } else
roker@529
   745
        status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   746
roker@529
   747
    sqlite3_reset(session->trustword);
roker@529
   748
    return status;
vb@0
   749
}
vb@0
   750
vb@233
   751
DYNAMIC_API PEP_STATUS trustwords(
vb@0
   752
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   753
        char **words, size_t *wsize, int max_words
vb@0
   754
    )
vb@0
   755
{
roker@529
   756
    const char *source = fingerprint;
roker@529
   757
    char *buffer;
roker@529
   758
    char *dest;
roker@529
   759
    size_t fsize;
vb@0
   760
roker@529
   761
    assert(session);
roker@529
   762
    assert(fingerprint);
roker@529
   763
    assert(words);
roker@529
   764
    assert(wsize);
roker@529
   765
    assert(max_words >= 0);
vb@0
   766
vb@191
   767
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
   768
        return PEP_ILLEGAL_VALUE;
vb@191
   769
roker@529
   770
    *words = NULL;
roker@529
   771
    *wsize = 0;
vb@0
   772
vb@251
   773
    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
   774
    assert(buffer);
vb@0
   775
    if (buffer == NULL)
vb@0
   776
        return PEP_OUT_OF_MEMORY;
vb@125
   777
    dest = buffer;
vb@0
   778
roker@529
   779
    fsize = strlen(fingerprint);
vb@0
   780
roker@529
   781
    if (!lang || !lang[0])
roker@529
   782
        lang = "en";
vb@0
   783
roker@529
   784
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   785
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   786
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   787
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   788
    assert(lang[2] == 0);
vb@0
   789
roker@529
   790
    int n_words = 0;
roker@529
   791
    while (source < fingerprint + fsize) {
vb@939
   792
        PEP_STATUS _status;
roker@529
   793
        uint16_t value;
roker@529
   794
        char *word;
roker@529
   795
        size_t _wsize;
roker@529
   796
        int j;
vb@0
   797
vb@0
   798
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
roker@529
   799
            if (*source >= 'a' && *source <= 'f')
roker@529
   800
                value += (*source - 'a' + 10) << (3 - j++) * 4;
roker@529
   801
            else if (*source >= 'A' && *source <= 'F')
roker@529
   802
                value += (*source - 'A' + 10) << (3 - j++) * 4;
roker@529
   803
            else if (*source >= '0' && *source <= '9')
roker@529
   804
                value += (*source - '0') << (3 - j++) * 4;
roker@529
   805
            
roker@529
   806
            source++;
roker@529
   807
        }
vb@0
   808
roker@529
   809
        _status = trustword(session, value, lang, &word, &_wsize);
vb@0
   810
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   811
            free(buffer);
vb@0
   812
            return PEP_OUT_OF_MEMORY;
vb@0
   813
        }
roker@529
   814
        if (word == NULL) {
vb@0
   815
            free(buffer);
roker@529
   816
            return PEP_TRUSTWORD_NOT_FOUND;
vb@0
   817
        }
vb@0
   818
roker@529
   819
        if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
roker@529
   820
            strncpy(dest, word, _wsize);
vb@0
   821
            free(word);
roker@529
   822
            dest += _wsize;
roker@529
   823
        }
roker@529
   824
        else {
vb@0
   825
            free(word);
roker@529
   826
            break; // buffer full
vb@0
   827
        }
vb@0
   828
roker@529
   829
        if (source < fingerprint + fsize
vb@251
   830
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
roker@529
   831
            *dest++ = ' ';
vb@0
   832
roker@529
   833
        ++n_words;
roker@529
   834
        if (max_words && n_words >= max_words)
roker@529
   835
            break;
roker@529
   836
    }
vb@0
   837
roker@529
   838
    *words = buffer;
roker@529
   839
    *wsize = dest - buffer;
roker@529
   840
    return PEP_STATUS_OK;
vb@0
   841
}
vb@0
   842
vb@0
   843
pEp_identity *new_identity(
vb@0
   844
        const char *address, const char *fpr, const char *user_id,
vb@0
   845
        const char *username
vb@0
   846
    )
vb@0
   847
{
vb@0
   848
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   849
    assert(result);
vb@0
   850
    if (result) {
vb@0
   851
        if (address) {
vb@0
   852
            result->address = strdup(address);
vb@0
   853
            assert(result->address);
vb@0
   854
            if (result->address == NULL) {
vb@0
   855
                free(result);
vb@0
   856
                return NULL;
vb@0
   857
            }
vb@0
   858
        }
vb@0
   859
        if (fpr) {
vb@0
   860
            result->fpr = strdup(fpr);
vb@0
   861
            assert(result->fpr);
vb@0
   862
            if (result->fpr == NULL) {
vb@0
   863
                free_identity(result);
vb@0
   864
                return NULL;
vb@0
   865
            }
vb@0
   866
        }
vb@0
   867
        if (user_id) {
vb@0
   868
            result->user_id = strdup(user_id);
vb@0
   869
            assert(result->user_id);
vb@0
   870
            if (result->user_id == NULL) {
vb@0
   871
                free_identity(result);
vb@0
   872
                return NULL;
vb@0
   873
            }
vb@0
   874
        }
vb@0
   875
        if (username) {
vb@0
   876
            result->username = strdup(username);
vb@0
   877
            assert(result->username);
vb@0
   878
            if (result->username == NULL) {
vb@0
   879
                free_identity(result);
vb@0
   880
                return NULL;
vb@0
   881
            }
vb@0
   882
        }
vb@0
   883
    }
vb@0
   884
    return result;
vb@0
   885
}
vb@0
   886
vb@37
   887
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   888
{
vb@37
   889
    assert(src);
vb@37
   890
vb@951
   891
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id,
vb@951
   892
            src->username);
vb@37
   893
    assert(dup);
vb@37
   894
    if (dup == NULL)
vb@37
   895
        return NULL;
vb@37
   896
    
vb@37
   897
    dup->comm_type = src->comm_type;
vb@37
   898
    dup->lang[0] = src->lang[0];
vb@37
   899
    dup->lang[1] = src->lang[1];
vb@37
   900
    dup->lang[2] = 0;
vb@37
   901
    dup->me = src->me;
vb@930
   902
    dup->flags = src->flags;
vb@37
   903
vb@37
   904
    return dup;
vb@37
   905
}
vb@37
   906
vb@0
   907
void free_identity(pEp_identity *identity)
vb@0
   908
{
vb@0
   909
    if (identity) {
vb@0
   910
        free(identity->address);
vb@0
   911
        free(identity->fpr);
vb@0
   912
        free(identity->user_id);
vb@0
   913
        free(identity->username);
vb@0
   914
        free(identity);
vb@0
   915
    }
vb@0
   916
}
vb@0
   917
vb@0
   918
DYNAMIC_API PEP_STATUS get_identity(
Edouard@559
   919
        PEP_SESSION session,
Edouard@559
   920
        const char *address,
Edouard@559
   921
        const char *user_id,
vb@0
   922
        pEp_identity **identity
vb@0
   923
    )
vb@0
   924
{
roker@529
   925
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   926
    static pEp_identity *_identity;
vb@0
   927
roker@529
   928
    assert(session);
roker@529
   929
    assert(address);
vb@8
   930
    assert(address[0]);
vb@632
   931
    assert(identity);
vb@0
   932
vb@632
   933
    if (!(session && address && address[0] && identity))
vb@191
   934
        return PEP_ILLEGAL_VALUE;
vb@191
   935
vb@632
   936
    *identity = NULL;
vb@632
   937
vb@46
   938
    sqlite3_reset(session->get_identity);
vb@46
   939
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
Edouard@559
   940
    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
vb@0
   941
roker@879
   942
    const int result = sqlite3_step(session->get_identity);
roker@529
   943
    switch (result) {
roker@529
   944
    case SQLITE_ROW:
vb@0
   945
        _identity = new_identity(
vb@0
   946
                address,
vb@46
   947
                (const char *) sqlite3_column_text(session->get_identity, 0),
Edouard@559
   948
                user_id,
Edouard@559
   949
                (const char *) sqlite3_column_text(session->get_identity, 1)
vb@0
   950
                );
vb@0
   951
        assert(_identity);
vb@0
   952
        if (_identity == NULL)
vb@0
   953
            return PEP_OUT_OF_MEMORY;
vb@0
   954
vb@951
   955
        _identity->comm_type = (PEP_comm_type)
vb@951
   956
            sqlite3_column_int(session->get_identity, 2);
vb@951
   957
        const char* const _lang = (const char *)
vb@951
   958
            sqlite3_column_text(session->get_identity, 3);
vb@0
   959
        if (_lang && _lang[0]) {
roker@529
   960
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
roker@529
   961
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
roker@529
   962
            assert(_lang[2] == 0);
roker@529
   963
            _identity->lang[0] = _lang[0];
roker@529
   964
            _identity->lang[1] = _lang[1];
vb@0
   965
            _identity->lang[2] = 0;
roker@529
   966
        }
vb@951
   967
        _identity->flags = (unsigned int)
vb@951
   968
            sqlite3_column_int(session->get_identity, 4);
roker@529
   969
        *identity = _identity;
roker@529
   970
        break;
roker@529
   971
    default:
vb@0
   972
        status = PEP_CANNOT_FIND_IDENTITY;
roker@529
   973
        *identity = NULL;
roker@529
   974
    }
vb@0
   975
vb@46
   976
    sqlite3_reset(session->get_identity);
roker@529
   977
    return status;
vb@0
   978
}
vb@0
   979
vb@0
   980
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   981
        PEP_SESSION session, const pEp_identity *identity
vb@0
   982
    )
vb@0
   983
{
roker@529
   984
    int result;
vb@0
   985
roker@529
   986
    assert(session);
roker@529
   987
    assert(identity);
roker@529
   988
    assert(identity->address);
roker@529
   989
    assert(identity->fpr);
roker@529
   990
    assert(identity->user_id);
roker@529
   991
    assert(identity->username);
vb@0
   992
vb@191
   993
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   994
                identity->user_id && identity->username))
vb@191
   995
        return PEP_ILLEGAL_VALUE;
vb@191
   996
vb@515
   997
    bool listed;
vb@515
   998
    PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
vb@515
   999
    assert(status == PEP_STATUS_OK);
vb@515
  1000
    if (status != PEP_STATUS_OK)
vb@515
  1001
        return status;
vb@515
  1002
vb@515
  1003
    if (listed)
vb@515
  1004
        return PEP_KEY_BLACKLISTED;
vb@515
  1005
roker@529
  1006
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
  1007
vb@1079
  1008
    if (identity->lang[0]) {
vb@1079
  1009
        assert(identity->lang[0] >= 'a' && identity->lang[0] <= 'z');
vb@1079
  1010
        assert(identity->lang[1] >= 'a' && identity->lang[1] <= 'z');
vb@1079
  1011
        assert(identity->lang[2] == 0);
vb@1079
  1012
    }
vb@1079
  1013
roker@529
  1014
    sqlite3_reset(session->set_person);
vb@46
  1015
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
  1016
            SQLITE_STATIC);
vb@46
  1017
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
  1018
            SQLITE_STATIC);
roker@529
  1019
    if (identity->lang[0])
vb@1081
  1020
        sqlite3_bind_text(session->set_person, 3, identity->lang, 2,
vb@0
  1021
                SQLITE_STATIC);
roker@529
  1022
    else
roker@529
  1023
        sqlite3_bind_null(session->set_person, 3);
Edouard@641
  1024
    sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
Edouard@641
  1025
                      SQLITE_STATIC);
roker@529
  1026
    result = sqlite3_step(session->set_person);
roker@529
  1027
    sqlite3_reset(session->set_person);
roker@529
  1028
    if (result != SQLITE_DONE) {
roker@529
  1029
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1030
        return PEP_CANNOT_SET_PERSON;
roker@529
  1031
    }
vb@0
  1032
roker@529
  1033
    sqlite3_reset(session->set_pgp_keypair);
vb@46
  1034
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
  1035
            SQLITE_STATIC);
roker@529
  1036
    result = sqlite3_step(session->set_pgp_keypair);
roker@529
  1037
    sqlite3_reset(session->set_pgp_keypair);
roker@529
  1038
    if (result != SQLITE_DONE) {
roker@529
  1039
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1040
        return PEP_CANNOT_SET_PGP_KEYPAIR;
roker@529
  1041
    }
vb@0
  1042
roker@529
  1043
    sqlite3_reset(session->set_identity);
vb@46
  1044
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
  1045
            SQLITE_STATIC);
vb@46
  1046
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
  1047
            SQLITE_STATIC);
vb@46
  1048
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
  1049
            SQLITE_STATIC);
vb@932
  1050
    sqlite3_bind_int(session->set_identity, 4, identity->flags);
roker@529
  1051
    result = sqlite3_step(session->set_identity);
roker@529
  1052
    sqlite3_reset(session->set_identity);
roker@529
  1053
    if (result != SQLITE_DONE) {
roker@529
  1054
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1055
        return PEP_CANNOT_SET_IDENTITY;
roker@529
  1056
    }
vb@0
  1057
roker@529
  1058
    sqlite3_reset(session->set_trust);
vb@46
  1059
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
  1060
            SQLITE_STATIC);
vb@46
  1061
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
  1062
            SQLITE_STATIC);
roker@529
  1063
    sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
roker@529
  1064
    result = sqlite3_step(session->set_trust);
roker@529
  1065
    sqlite3_reset(session->set_trust);
roker@529
  1066
    if (result != SQLITE_DONE) {
roker@529
  1067
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
Edouard@584
  1068
        return PEP_CANNOT_SET_TRUST;
roker@529
  1069
    }
vb@0
  1070
vb@46
  1071
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
roker@529
  1072
    if (result == SQLITE_OK)
roker@529
  1073
        return PEP_STATUS_OK;
roker@529
  1074
    else
roker@529
  1075
        return PEP_COMMIT_FAILED;
vb@0
  1076
}
vb@0
  1077
vb@932
  1078
DYNAMIC_API PEP_STATUS set_identity_flags(
vb@934
  1079
        PEP_SESSION session,
vb@934
  1080
        pEp_identity *identity,
vb@934
  1081
        unsigned int flags
vb@932
  1082
    )
vb@932
  1083
{
vb@932
  1084
    int result;
vb@932
  1085
vb@932
  1086
    assert(session);
vb@932
  1087
    assert(identity);
vb@932
  1088
    assert(identity->address);
vb@932
  1089
    assert(identity->user_id);
vb@932
  1090
vb@932
  1091
    if (!(session && identity && identity->address && identity->user_id))
vb@932
  1092
        return PEP_ILLEGAL_VALUE;
vb@932
  1093
vb@932
  1094
    sqlite3_reset(session->set_identity_flags);
vb@934
  1095
    sqlite3_bind_int(session->set_identity_flags, 1, flags);
vb@932
  1096
    sqlite3_bind_text(session->set_identity_flags, 2, identity->address, -1,
vb@932
  1097
            SQLITE_STATIC);
vb@932
  1098
    sqlite3_bind_text(session->set_identity_flags, 3, identity->user_id, -1,
vb@932
  1099
            SQLITE_STATIC);
vb@932
  1100
    result = sqlite3_step(session->set_identity_flags);
vb@932
  1101
    sqlite3_reset(session->set_identity_flags);
vb@932
  1102
    if (result != SQLITE_DONE)
vb@932
  1103
        return PEP_CANNOT_SET_IDENTITY;
vb@932
  1104
vb@934
  1105
    identity->flags = flags;
vb@932
  1106
    return PEP_STATUS_OK;
vb@932
  1107
}
vb@932
  1108
vb@357
  1109
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
  1110
        PEP_SESSION session,
vb@357
  1111
        const char *fpr
vb@357
  1112
    )
vb@357
  1113
{
roker@529
  1114
    int result;
vb@357
  1115
roker@529
  1116
    assert(session);
vb@357
  1117
    assert(fpr && fpr[0]);
vb@357
  1118
vb@357
  1119
    if (!(session && fpr && fpr[0]))
vb@357
  1120
        return PEP_ILLEGAL_VALUE;
vb@357
  1121
roker@529
  1122
    sqlite3_reset(session->mark_compromized);
vb@357
  1123
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
  1124
            SQLITE_STATIC);
vb@357
  1125
    result = sqlite3_step(session->mark_compromized);
roker@529
  1126
    sqlite3_reset(session->mark_compromized);
vb@357
  1127
vb@357
  1128
    if (result != SQLITE_DONE)
Edouard@584
  1129
        return PEP_CANNOT_SET_TRUST;
vb@357
  1130
vb@357
  1131
    return PEP_STATUS_OK;
vb@357
  1132
}
vb@357
  1133
vb@0
  1134
void pEp_free(void *p)
vb@0
  1135
{
vb@0
  1136
    free(p);
vb@0
  1137
}
vb@0
  1138
vb@8
  1139
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
  1140
{
vb@8
  1141
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
  1142
    int result;
vb@8
  1143
vb@8
  1144
    assert(session);
vb@8
  1145
    assert(identity);
vb@8
  1146
    assert(identity->user_id);
vb@8
  1147
    assert(identity->user_id[0]);
vb@8
  1148
    assert(identity->fpr);
vb@8
  1149
    assert(identity->fpr[0]);
vb@8
  1150
vb@191
  1151
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
  1152
                identity->fpr && identity->fpr[0]))
vb@191
  1153
        return PEP_ILLEGAL_VALUE;
vb@191
  1154
vb@8
  1155
    identity->comm_type = PEP_ct_unknown;
vb@8
  1156
vb@46
  1157
    sqlite3_reset(session->get_trust);
vb@951
  1158
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1,
vb@951
  1159
            SQLITE_STATIC);
vb@46
  1160
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
  1161
vb@46
  1162
    result = sqlite3_step(session->get_trust);
vb@8
  1163
    switch (result) {
vb@8
  1164
    case SQLITE_ROW: {
vb@951
  1165
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust,
vb@951
  1166
                0);
vb@8
  1167
        identity->comm_type = comm_type;
vb@8
  1168
        break;
vb@8
  1169
    }
vb@8
  1170
 
vb@8
  1171
    default:
vb@8
  1172
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
  1173
    }
vb@8
  1174
vb@46
  1175
    sqlite3_reset(session->get_trust);
vb@8
  1176
    return status;
vb@9
  1177
}
vb@9
  1178
vb@251
  1179
DYNAMIC_API PEP_STATUS least_trust(
vb@251
  1180
        PEP_SESSION session,
vb@251
  1181
        const char *fpr,
vb@251
  1182
        PEP_comm_type *comm_type
vb@251
  1183
    )
vb@251
  1184
{
vb@251
  1185
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
  1186
    int result;
vb@251
  1187
vb@251
  1188
    assert(session);
vb@251
  1189
    assert(fpr);
vb@251
  1190
    assert(comm_type);
vb@251
  1191
vb@251
  1192
    if (!(session && fpr && comm_type))
vb@251
  1193
        return PEP_ILLEGAL_VALUE;
vb@251
  1194
vb@632
  1195
    *comm_type = PEP_ct_unknown;
vb@632
  1196
vb@251
  1197
    sqlite3_reset(session->least_trust);
vb@251
  1198
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
  1199
vb@251
  1200
    result = sqlite3_step(session->least_trust);
vb@251
  1201
    switch (result) {
vb@251
  1202
        case SQLITE_ROW: {
vb@259
  1203
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
  1204
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
  1205
            break;
vb@251
  1206
        }
vb@251
  1207
        default:
vb@251
  1208
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
  1209
    }
vb@251
  1210
vb@251
  1211
    sqlite3_reset(session->least_trust);
vb@251
  1212
    return status;
vb@251
  1213
}
vb@251
  1214
vb@24
  1215
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
  1216
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
  1217
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
  1218
    )
vb@24
  1219
{
vb@191
  1220
    assert(session);
vb@191
  1221
    assert(ctext);
vb@191
  1222
    assert(csize);
vb@191
  1223
    assert(ptext);
vb@191
  1224
    assert(psize);
vb@191
  1225
    assert(keylist);
vb@191
  1226
vb@939
  1227
    if (!(session && ctext && csize && ptext && psize && keylist))
vb@191
  1228
        return PEP_ILLEGAL_VALUE;
vb@191
  1229
vb@939
  1230
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(
vb@939
  1231
            session, ctext, csize, ptext, psize, keylist);
vb@24
  1232
}
vb@24
  1233
vb@24
  1234
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
  1235
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
  1236
    size_t psize, char **ctext, size_t *csize
vb@24
  1237
    )
vb@24
  1238
{
vb@191
  1239
    assert(session);
vb@191
  1240
    assert(keylist);
vb@191
  1241
    assert(ptext);
vb@191
  1242
    assert(psize);
vb@191
  1243
    assert(ctext);
vb@191
  1244
    assert(csize);
vb@191
  1245
vb@191
  1246
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
  1247
        return PEP_ILLEGAL_VALUE;
vb@191
  1248
vb@951
  1249
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session,
vb@951
  1250
            keylist, ptext, psize, ctext, csize);
vb@24
  1251
}
vb@24
  1252
vb@24
  1253
DYNAMIC_API PEP_STATUS verify_text(
vb@24
  1254
    PEP_SESSION session, const char *text, size_t size,
vb@24
  1255
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
  1256
    )
vb@24
  1257
{
vb@191
  1258
    assert(session);
vb@191
  1259
    assert(text);
vb@191
  1260
    assert(size);
vb@191
  1261
    assert(signature);
vb@191
  1262
    assert(sig_size);
vb@191
  1263
    assert(keylist);
vb@191
  1264
vb@191
  1265
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
  1266
        return PEP_ILLEGAL_VALUE;
vb@191
  1267
vb@951
  1268
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text,
vb@951
  1269
            size, signature, sig_size, keylist);
vb@24
  1270
}
vb@24
  1271
vb@24
  1272
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1273
{
vb@191
  1274
    assert(session);
vb@191
  1275
    assert(fpr);
vb@191
  1276
vb@191
  1277
    if (!(session && fpr))
vb@191
  1278
        return PEP_ILLEGAL_VALUE;
vb@191
  1279
vb@46
  1280
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
  1281
}
vb@24
  1282
vb@24
  1283
DYNAMIC_API PEP_STATUS export_key(
vb@46
  1284
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1285
    )
vb@24
  1286
{
vb@191
  1287
    assert(session);
vb@191
  1288
    assert(fpr);
vb@191
  1289
    assert(key_data);
vb@191
  1290
    assert(size);
vb@191
  1291
vb@191
  1292
    if (!(session && fpr && key_data && size))
vb@191
  1293
        return PEP_ILLEGAL_VALUE;
vb@191
  1294
vb@951
  1295
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr,
vb@1103
  1296
            key_data, size, false);
vb@1103
  1297
}
vb@1103
  1298
vb@1103
  1299
DYNAMIC_API PEP_STATUS export_secret_key(
vb@1103
  1300
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@1103
  1301
    )
vb@1103
  1302
{
vb@1103
  1303
    assert(session);
vb@1103
  1304
    assert(fpr);
vb@1103
  1305
    assert(key_data);
vb@1103
  1306
    assert(size);
vb@1103
  1307
vb@1103
  1308
    if (!(session && fpr && key_data && size))
vb@1103
  1309
        return PEP_ILLEGAL_VALUE;
vb@1103
  1310
vb@1103
  1311
    // don't accept key IDs but full fingerprints only
vb@1103
  1312
    if (strlen(fpr) < 16)
vb@1103
  1313
        return PEP_ILLEGAL_VALUE;
vb@1103
  1314
vb@1103
  1315
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr,
vb@1103
  1316
            key_data, size, true);
vb@24
  1317
}
vb@24
  1318
vb@24
  1319
DYNAMIC_API PEP_STATUS find_keys(
vb@46
  1320
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1321
    )
vb@24
  1322
{
vb@191
  1323
    assert(session);
vb@191
  1324
    assert(pattern);
vb@191
  1325
    assert(keylist);
vb@191
  1326
vb@191
  1327
    if (!(session && pattern && keylist))
vb@191
  1328
        return PEP_ILLEGAL_VALUE;
vb@191
  1329
vb@951
  1330
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern,
vb@951
  1331
            keylist);
vb@24
  1332
}
vb@24
  1333
krista@1005
  1334
vb@24
  1335
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
  1336
        PEP_SESSION session, pEp_identity *identity
vb@24
  1337
    )
vb@24
  1338
{
vb@191
  1339
    assert(session);
vb@191
  1340
    assert(identity);
vb@191
  1341
    assert(identity->address);
vb@298
  1342
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
  1343
    assert(identity->username);
vb@191
  1344
vb@298
  1345
    if (!(session && identity && identity->address &&
vb@951
  1346
            (identity->fpr == NULL || identity->fpr[0] == 0) &&
vb@951
  1347
            identity->username))
vb@191
  1348
        return PEP_ILLEGAL_VALUE;
vb@191
  1349
vb@944
  1350
    PEP_STATUS status =
vb@944
  1351
        session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session,
vb@944
  1352
                identity);
vb@944
  1353
    if (status != PEP_STATUS_OK)
vb@944
  1354
        return status;
vb@944
  1355
vb@944
  1356
    // if a state machine for keysync is in place, inject notify
vb@944
  1357
    if (session->sync_state != DeviceState_state_NONE)
vb@951
  1358
        status = fsm_DeviceState_inject(session, KeyGen, NULL, NULL);
vb@944
  1359
vb@944
  1360
    return status;
vb@24
  1361
}
vb@24
  1362
vb@9
  1363
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
  1364
        PEP_SESSION session,
vb@46
  1365
        const char *fpr,
vb@46
  1366
        PEP_comm_type *comm_type
vb@9
  1367
    )
vb@9
  1368
{
vb@191
  1369
    assert(session);
vb@191
  1370
    assert(fpr);
vb@191
  1371
    assert(comm_type);
vb@191
  1372
vb@191
  1373
    if (!(session && fpr && comm_type))
vb@191
  1374
        return PEP_ILLEGAL_VALUE;
vb@191
  1375
vb@951
  1376
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr,
vb@951
  1377
            comm_type);
vb@24
  1378
}
vb@9
  1379
Edouard@728
  1380
DYNAMIC_API PEP_STATUS import_key(
Edouard@728
  1381
        PEP_SESSION session,
Edouard@728
  1382
        const char *key_data,
Edouard@728
  1383
        size_t size,
Edouard@728
  1384
        identity_list **private_keys
Edouard@728
  1385
    )
vb@24
  1386
{
vb@191
  1387
    assert(session);
vb@191
  1388
    assert(key_data);
vb@191
  1389
vb@191
  1390
    if (!(session && key_data))
vb@191
  1391
        return PEP_ILLEGAL_VALUE;
vb@191
  1392
vb@951
  1393
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data,
vb@951
  1394
            size, private_keys);
vb@24
  1395
}
vb@9
  1396
vb@24
  1397
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1398
{
vb@191
  1399
    assert(session);
vb@191
  1400
    assert(pattern);
vb@191
  1401
vb@191
  1402
    if (!(session && pattern))
vb@191
  1403
        return PEP_ILLEGAL_VALUE;
vb@191
  1404
vb@46
  1405
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1406
}
vb@9
  1407
vb@24
  1408
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1409
{
vb@191
  1410
    assert(session);
vb@191
  1411
    assert(pattern);
vb@191
  1412
vb@191
  1413
    if (!(session && pattern))
vb@191
  1414
        return PEP_ILLEGAL_VALUE;
vb@191
  1415
vb@46
  1416
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1417
}
vb@196
  1418
vb@201
  1419
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1420
        PEP_SESSION session,
vb@201
  1421
        const char *fpr,
vb@201
  1422
        const timestamp *ts
vb@201
  1423
    )
vb@196
  1424
{
vb@196
  1425
    assert(session);
vb@200
  1426
    assert(fpr);
vb@196
  1427
vb@200
  1428
    if (!(session && fpr))
vb@196
  1429
        return PEP_ILLEGAL_VALUE;
vb@196
  1430
vb@201
  1431
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1432
}
vb@196
  1433
vb@211
  1434
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1435
        PEP_SESSION session,
vb@211
  1436
        const char *fpr,
vb@211
  1437
        const char *reason
vb@211
  1438
    )
vb@197
  1439
{
vb@197
  1440
    assert(session);
vb@200
  1441
    assert(fpr);
vb@197
  1442
vb@200
  1443
    if (!(session && fpr))
vb@197
  1444
        return PEP_ILLEGAL_VALUE;
vb@197
  1445
vb@214
  1446
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1447
            reason);
vb@197
  1448
}
vb@197
  1449
vb@214
  1450
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1451
        PEP_SESSION session,
vb@214
  1452
        const char *fpr,
Edouard@701
  1453
        const time_t when,
vb@214
  1454
        bool *expired
vb@214
  1455
    )
vb@214
  1456
{
vb@214
  1457
    assert(session);
vb@214
  1458
    assert(fpr);
vb@214
  1459
    assert(expired);
vb@214
  1460
vb@214
  1461
    if (!(session && fpr && expired))
vb@214
  1462
        return PEP_ILLEGAL_VALUE;
vb@214
  1463
vb@214
  1464
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
Edouard@701
  1465
            when, expired);
vb@214
  1466
}
vb@214
  1467
Edouard@694
  1468
DYNAMIC_API PEP_STATUS key_revoked(
vb@951
  1469
       PEP_SESSION session,
vb@951
  1470
       const char *fpr,
vb@951
  1471
       bool *revoked
vb@951
  1472
   )
Edouard@694
  1473
{
Edouard@694
  1474
    assert(session);
Edouard@694
  1475
    assert(fpr);
Edouard@694
  1476
    assert(revoked);
Edouard@694
  1477
    
Edouard@694
  1478
    if (!(session && fpr && revoked))
Edouard@694
  1479
        return PEP_ILLEGAL_VALUE;
Edouard@694
  1480
    
Edouard@694
  1481
    return session->cryptotech[PEP_crypt_OpenPGP].key_revoked(session, fpr,
vb@951
  1482
            revoked);
Edouard@694
  1483
}
Edouard@694
  1484
vb@451
  1485
static void _clean_log_value(char *text)
vb@451
  1486
{
vb@451
  1487
    if (text) {
vb@451
  1488
        for (char *c = text; *c; c++) {
vb@722
  1489
            if (*c < 32 && *c != '\n')
vb@451
  1490
                *c = 32;
vb@451
  1491
            else if (*c == '"')
vb@451
  1492
                *c = '\'';
vb@451
  1493
        }
vb@451
  1494
    }
vb@451
  1495
}
vb@451
  1496
vb@450
  1497
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1498
{
vb@454
  1499
    str2 = str2 ? str2 : "";
vb@450
  1500
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1501
    size_t len2 = strlen(str2);
vb@450
  1502
    size_t len = len1 + len2 + 3;
vb@450
  1503
    char * result = realloc(str1, len + 1);
vb@450
  1504
vb@450
  1505
    if (result) {
vb@450
  1506
        result[len1] = '"';
vb@450
  1507
        strcpy(result + len1 + 1, str2);
vb@450
  1508
        result[len - 2] = '"';
vb@450
  1509
        result[len - 1] = delim;
vb@450
  1510
        result[len] = 0;
vb@450
  1511
    }
vb@450
  1512
    else {
vb@450
  1513
        free(str1);
vb@450
  1514
    }
vb@450
  1515
vb@450
  1516
    return result;
vb@450
  1517
}
vb@450
  1518
vb@450
  1519
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1520
        PEP_SESSION session,
vb@450
  1521
        int maxlines,
vb@450
  1522
        char **logdata
vb@450
  1523
    )
vb@450
  1524
{
vb@450
  1525
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1526
    char *_logdata= NULL;
vb@450
  1527
vb@450
  1528
    assert(session);
vb@450
  1529
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1530
    assert(logdata);
vb@450
  1531
vb@450
  1532
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1533
            CRASHDUMP_MAX_LINES))
vb@450
  1534
        return PEP_ILLEGAL_VALUE;
vb@450
  1535
vb@632
  1536
    *logdata = NULL;
vb@632
  1537
vb@450
  1538
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1539
    const char *timestamp = NULL;
vb@458
  1540
    const char *title = NULL;
vb@458
  1541
    const char *entity = NULL;
vb@458
  1542
    const char *desc = NULL;
vb@458
  1543
    const char *comment = NULL;
vb@450
  1544
vb@450
  1545
    sqlite3_reset(session->crashdump);
vb@452
  1546
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1547
vb@450
  1548
    int result;
vb@450
  1549
vb@450
  1550
    do {
vb@450
  1551
        result = sqlite3_step(session->crashdump);
vb@450
  1552
        switch (result) {
vb@450
  1553
        case SQLITE_ROW:
vb@951
  1554
            timestamp = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1555
                    0);
vb@951
  1556
            title   = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1557
                    1);
vb@951
  1558
            entity  = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1559
                    2);
vb@951
  1560
            desc    = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1561
                    3);
vb@951
  1562
            comment = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1563
                    4);
vb@452
  1564
vb@452
  1565
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1566
            if (_logdata == NULL)
vb@452
  1567
                goto enomem;
vb@450
  1568
vb@450
  1569
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1570
            if (_logdata == NULL)
vb@450
  1571
                goto enomem;
vb@450
  1572
vb@450
  1573
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1574
            if (_logdata == NULL)
vb@450
  1575
                goto enomem;
vb@450
  1576
vb@450
  1577
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1578
            if (_logdata == NULL)
vb@450
  1579
                goto enomem;
vb@450
  1580
vb@450
  1581
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1582
            if (_logdata == NULL)
vb@450
  1583
                goto enomem;
vb@450
  1584
vb@687
  1585
            _clean_log_value(_logdata);
vb@450
  1586
            break;
vb@450
  1587
vb@450
  1588
        case SQLITE_DONE:
vb@450
  1589
            break;
vb@450
  1590
vb@450
  1591
        default:
vb@450
  1592
            status = PEP_UNKNOWN_ERROR;
vb@450
  1593
            result = SQLITE_DONE;
vb@450
  1594
        }
vb@450
  1595
    } while (result != SQLITE_DONE);
vb@450
  1596
vb@450
  1597
    sqlite3_reset(session->crashdump);
vb@450
  1598
    if (status == PEP_STATUS_OK)
vb@450
  1599
        *logdata = _logdata;
vb@450
  1600
vb@450
  1601
    goto the_end;
vb@450
  1602
vb@450
  1603
enomem:
vb@450
  1604
    status = PEP_OUT_OF_MEMORY;
vb@450
  1605
vb@450
  1606
the_end:
vb@450
  1607
    return status;
vb@450
  1608
}
vb@450
  1609
vb@458
  1610
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  1611
        PEP_SESSION session,
vb@458
  1612
        char **languages
vb@458
  1613
    )
vb@458
  1614
{
vb@458
  1615
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1616
    char *_languages= NULL;
vb@458
  1617
vb@458
  1618
    assert(session);
vb@458
  1619
    assert(languages);
vb@458
  1620
vb@458
  1621
    if (!(session && languages))
vb@458
  1622
        return PEP_ILLEGAL_VALUE;
vb@458
  1623
vb@632
  1624
    *languages = NULL;
vb@632
  1625
vb@458
  1626
    const char *lang = NULL;
vb@458
  1627
    const char *name = NULL;
vb@617
  1628
    const char *phrase = NULL;
vb@458
  1629
vb@458
  1630
    sqlite3_reset(session->languagelist);
vb@458
  1631
vb@458
  1632
    int result;
vb@458
  1633
vb@458
  1634
    do {
vb@458
  1635
        result = sqlite3_step(session->languagelist);
vb@458
  1636
        switch (result) {
vb@458
  1637
        case SQLITE_ROW:
vb@951
  1638
            lang = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  1639
                    0);
vb@951
  1640
            name = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  1641
                    1);
vb@951
  1642
            phrase = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  1643
                    2);
vb@458
  1644
vb@458
  1645
            _languages = _concat_string(_languages, lang, ',');
vb@458
  1646
            if (_languages == NULL)
vb@458
  1647
                goto enomem;
vb@458
  1648
vb@617
  1649
            _languages = _concat_string(_languages, name, ',');
vb@617
  1650
            if (_languages == NULL)
vb@617
  1651
                goto enomem;
vb@617
  1652
vb@617
  1653
            _languages = _concat_string(_languages, phrase, '\n');
vb@458
  1654
            if (_languages == NULL)
vb@458
  1655
                goto enomem;
vb@458
  1656
vb@458
  1657
            break;
vb@458
  1658
vb@458
  1659
        case SQLITE_DONE:
vb@458
  1660
            break;
vb@458
  1661
vb@458
  1662
        default:
vb@458
  1663
            status = PEP_UNKNOWN_ERROR;
vb@458
  1664
            result = SQLITE_DONE;
vb@458
  1665
        }
vb@458
  1666
    } while (result != SQLITE_DONE);
vb@458
  1667
vb@458
  1668
    sqlite3_reset(session->languagelist);
vb@458
  1669
    if (status == PEP_STATUS_OK)
vb@458
  1670
        *languages = _languages;
vb@458
  1671
vb@458
  1672
    goto the_end;
vb@458
  1673
vb@458
  1674
enomem:
vb@458
  1675
    status = PEP_OUT_OF_MEMORY;
vb@458
  1676
vb@458
  1677
the_end:
vb@458
  1678
    return status;
vb@458
  1679
}
vb@458
  1680
vb@458
  1681
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  1682
        PEP_SESSION session,
vb@458
  1683
        const char *lang,
vb@458
  1684
        int phrase_id,
vb@458
  1685
        char **phrase
vb@458
  1686
    )
vb@458
  1687
{
vb@458
  1688
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1689
vb@479
  1690
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@479
  1691
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  1692
        return PEP_ILLEGAL_VALUE;
vb@458
  1693
vb@632
  1694
    *phrase = NULL;
vb@632
  1695
vb@458
  1696
    sqlite3_reset(session->i18n_token);
roker@529
  1697
    sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
roker@529
  1698
    sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  1699
vb@458
  1700
    const char *_phrase = NULL;
vb@458
  1701
    int result;
vb@458
  1702
vb@458
  1703
    result = sqlite3_step(session->i18n_token);
vb@458
  1704
    switch (result) {
vb@458
  1705
    case SQLITE_ROW:
vb@458
  1706
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  1707
        break;
vb@458
  1708
vb@458
  1709
    case SQLITE_DONE:
vb@458
  1710
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  1711
        break;
vb@458
  1712
vb@458
  1713
    default:
vb@458
  1714
        status = PEP_UNKNOWN_ERROR;
vb@458
  1715
    }
vb@458
  1716
vb@458
  1717
    if (status == PEP_STATUS_OK) {
vb@458
  1718
        *phrase = strdup(_phrase);
vb@458
  1719
        if (*phrase == NULL)
vb@458
  1720
            goto enomem;
vb@458
  1721
    }
vb@459
  1722
vb@459
  1723
    sqlite3_reset(session->i18n_token);
vb@458
  1724
    goto the_end;
vb@458
  1725
vb@458
  1726
enomem:
vb@458
  1727
    status = PEP_OUT_OF_MEMORY;
vb@458
  1728
vb@458
  1729
the_end:
vb@458
  1730
    return status;
vb@458
  1731
}
vb@458
  1732
vb@1085
  1733
static PEP_STATUS _get_sequence_value(PEP_SESSION session, const char *name,
vb@1085
  1734
        int32_t *value)
vb@1085
  1735
{
vb@1086
  1736
    assert(session && name && value);
vb@1086
  1737
    if (!(session && name && value))
vb@1086
  1738
        return PEP_ILLEGAL_VALUE;
vb@1086
  1739
vb@1085
  1740
    PEP_STATUS status = PEP_STATUS_OK;
vb@1085
  1741
vb@1085
  1742
    sqlite3_reset(session->sequence_value2);
vb@1085
  1743
    sqlite3_bind_text(session->sequence_value2, 1, name, -1,
vb@1085
  1744
            SQLITE_STATIC);
vb@1085
  1745
    int result = sqlite3_step(session->sequence_value2);
vb@1085
  1746
    switch (result) {
vb@1085
  1747
        case SQLITE_ROW: {
vb@1085
  1748
            int32_t _value = (int32_t)
vb@1086
  1749
                    sqlite3_column_int(session->sequence_value2, 0);
vb@1086
  1750
            int _own = (int)
vb@1086
  1751
                    sqlite3_column_int(session->sequence_value2, 1);
vb@1085
  1752
            *value = _value;
vb@1086
  1753
            if (_own)
vb@1086
  1754
                status = PEP_OWN_SEQUENCE;
vb@1085
  1755
            break;
vb@1085
  1756
        }
vb@1091
  1757
        case SQLITE_DONE:
vb@1091
  1758
            status = PEP_RECORD_NOT_FOUND;
vb@1091
  1759
            break;
vb@1085
  1760
        default:
vb@1085
  1761
            status = PEP_UNKNOWN_ERROR;
vb@1085
  1762
    }
vb@1085
  1763
    sqlite3_reset(session->sequence_value2);
vb@1085
  1764
vb@1085
  1765
    return status;
vb@1085
  1766
}
vb@1085
  1767
vb@1086
  1768
static PEP_STATUS _increment_sequence_value(PEP_SESSION session,
vb@1086
  1769
        const char *name, int own)
vb@1085
  1770
{
vb@1086
  1771
    assert(session && name);
vb@1086
  1772
    if (!(session && name))
vb@1086
  1773
        return PEP_ILLEGAL_VALUE;
vb@1086
  1774
vb@1085
  1775
    sqlite3_reset(session->sequence_value1);
vb@1085
  1776
    sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
vb@1086
  1777
    sqlite3_bind_int(session->sequence_value1, 2, own);
vb@1085
  1778
    int result = sqlite3_step(session->sequence_value1);
vb@1085
  1779
    assert(result == SQLITE_DONE);
vb@1085
  1780
    sqlite3_reset(session->sequence_value1);
vb@1085
  1781
    if (result == SQLITE_DONE)
vb@1085
  1782
        return PEP_STATUS_OK;
vb@1085
  1783
    else
vb@1085
  1784
        return PEP_CANNOT_INCREASE_SEQUENCE;
vb@1085
  1785
}
vb@1085
  1786
vb@1087
  1787
static PEP_STATUS _set_sequence_value(PEP_SESSION session,
vb@1091
  1788
        const char *name, int32_t value, int own)
vb@1087
  1789
{
vb@1087
  1790
    assert(session && name && value > 0);
vb@1087
  1791
    if (!(session && name && value > 0))
vb@1087
  1792
        return PEP_ILLEGAL_VALUE;
vb@1087
  1793
vb@1087
  1794
    sqlite3_reset(session->sequence_value3);
vb@1087
  1795
    sqlite3_bind_text(session->sequence_value3, 1, name, -1, SQLITE_STATIC);
vb@1087
  1796
    sqlite3_bind_int(session->sequence_value3, 2, value);
vb@1091
  1797
    sqlite3_bind_int(session->sequence_value3, 3, own);
vb@1087
  1798
    int result = sqlite3_step(session->sequence_value3);
vb@1087
  1799
    assert(result == SQLITE_DONE);
vb@1087
  1800
    sqlite3_reset(session->sequence_value3);
vb@1087
  1801
    if (result == SQLITE_DONE)
vb@1087
  1802
        return PEP_STATUS_OK;
vb@1087
  1803
    else
vb@1087
  1804
        return PEP_CANNOT_SET_SEQUENCE_VALUE;
vb@1087
  1805
}
vb@1087
  1806
vb@632
  1807
DYNAMIC_API PEP_STATUS sequence_value(
vb@632
  1808
        PEP_SESSION session,
vb@1085
  1809
        char *name,
vb@652
  1810
        int32_t *value
vb@632
  1811
    )
vb@632
  1812
{
vb@632
  1813
    PEP_STATUS status = PEP_STATUS_OK;
vb@632
  1814
vb@632
  1815
    assert(session);
vb@1085
  1816
    assert(name && value && *value >= 0);
vb@632
  1817
vb@1085
  1818
    if (!(session && name && value && *value >= 0))
vb@632
  1819
        return PEP_ILLEGAL_VALUE;
vb@632
  1820
vb@1086
  1821
    int own = 0;
vb@1085
  1822
    if (!name[0]) {
vb@1085
  1823
        unsigned char uuid[16];
vb@1085
  1824
        uuid_generate_random(uuid);
vb@1085
  1825
        uuid_unparse_upper(uuid, name);
vb@1086
  1826
        own = 1;
vb@633
  1827
    }
vb@1091
  1828
    else {
vb@1091
  1829
        if (name == sync_uuid || strcmp(name, sync_uuid) == 0)
vb@1091
  1830
            own = 1;
vb@1091
  1831
    }
vb@1085
  1832
vb@1085
  1833
    if (*value) {
vb@1085
  1834
        int32_t old_value = 0;
vb@1085
  1835
        status = _get_sequence_value(session, name, &old_value);
vb@1091
  1836
        if (status != PEP_STATUS_OK && status != PEP_RECORD_NOT_FOUND)
vb@1085
  1837
            return status;
vb@1085
  1838
vb@1087
  1839
        if (old_value >= *value) {
vb@1085
  1840
            return PEP_SEQUENCE_VIOLATED;
vb@1087
  1841
        }
vb@1087
  1842
        else {
vb@1091
  1843
            status = _set_sequence_value(session, name, *value, own);
vb@1087
  1844
            return status;
vb@1087
  1845
        }
vb@632
  1846
    }
vb@1085
  1847
vb@1085
  1848
    assert(*value == 0);
vb@1086
  1849
    status = _increment_sequence_value(session, name, own);
vb@1086
  1850
    if (status == PEP_STATUS_OK) {
vb@1085
  1851
        status = _get_sequence_value(session, name, value);
vb@1086
  1852
        assert(*value < INT32_MAX);
vb@1086
  1853
        if (*value == INT32_MAX)
vb@1086
  1854
            return PEP_CANNOT_INCREASE_SEQUENCE;
vb@1086
  1855
    }
vb@632
  1856
    return status;
vb@632
  1857
}
vb@632
  1858
Edouard@693
  1859
DYNAMIC_API PEP_STATUS set_revoked(
Edouard@693
  1860
       PEP_SESSION session,
Edouard@693
  1861
       const char *revoked_fpr,
Edouard@693
  1862
       const char *replacement_fpr,
Edouard@693
  1863
       const uint64_t revocation_date
Edouard@693
  1864
    )
Edouard@693
  1865
{
Edouard@693
  1866
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  1867
    
Edouard@693
  1868
    assert(session &&
Edouard@693
  1869
           revoked_fpr && revoked_fpr[0] &&
Edouard@693
  1870
           replacement_fpr && replacement_fpr[0]
Edouard@693
  1871
          );
Edouard@693
  1872
    
Edouard@693
  1873
    if (!(session &&
Edouard@693
  1874
          revoked_fpr && revoked_fpr[0] &&
Edouard@693
  1875
          replacement_fpr && replacement_fpr[0]
Edouard@693
  1876
         ))
Edouard@693
  1877
        return PEP_ILLEGAL_VALUE;
Edouard@693
  1878
    
Edouard@693
  1879
    sqlite3_reset(session->set_revoked);
Edouard@693
  1880
    sqlite3_bind_text(session->set_revoked, 1, revoked_fpr, -1, SQLITE_STATIC);
vb@951
  1881
    sqlite3_bind_text(session->set_revoked, 2, replacement_fpr, -1,
vb@951
  1882
            SQLITE_STATIC);
Edouard@693
  1883
    sqlite3_bind_int64(session->set_revoked, 3, revocation_date);
Edouard@693
  1884
Edouard@693
  1885
    int result;
Edouard@693
  1886
    
Edouard@693
  1887
    result = sqlite3_step(session->set_revoked);
Edouard@693
  1888
    switch (result) {
Edouard@693
  1889
        case SQLITE_DONE:
Edouard@693
  1890
            status = PEP_STATUS_OK;
Edouard@693
  1891
            break;
Edouard@693
  1892
            
Edouard@693
  1893
        default:
Edouard@693
  1894
            status = PEP_UNKNOWN_ERROR;
Edouard@693
  1895
    }
Edouard@693
  1896
    
Edouard@693
  1897
    sqlite3_reset(session->set_revoked);
Edouard@693
  1898
    return status;
Edouard@693
  1899
}
Edouard@693
  1900
Edouard@693
  1901
DYNAMIC_API PEP_STATUS get_revoked(
Edouard@693
  1902
        PEP_SESSION session,
Edouard@693
  1903
        const char *fpr,
Edouard@693
  1904
        char **revoked_fpr,
Edouard@693
  1905
        uint64_t *revocation_date
Edouard@693
  1906
    )
Edouard@693
  1907
{
Edouard@693
  1908
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  1909
Edouard@693
  1910
    assert(session &&
Edouard@693
  1911
           revoked_fpr &&
Edouard@693
  1912
           fpr && fpr[0]
Edouard@693
  1913
          );
Edouard@693
  1914
    
Edouard@693
  1915
    if (!(session &&
Edouard@693
  1916
           revoked_fpr &&
Edouard@693
  1917
           fpr && fpr[0]
Edouard@693
  1918
          ))
Edouard@693
  1919
        return PEP_ILLEGAL_VALUE;
Edouard@693
  1920
Edouard@693
  1921
    *revoked_fpr = NULL;
Edouard@693
  1922
    *revocation_date = 0;
Edouard@693
  1923
Edouard@693
  1924
    sqlite3_reset(session->get_revoked);
Edouard@693
  1925
    sqlite3_bind_text(session->get_revoked, 1, fpr, -1, SQLITE_STATIC);
Edouard@693
  1926
Edouard@693
  1927
    int result;
Edouard@693
  1928
    
Edouard@693
  1929
    result = sqlite3_step(session->get_revoked);
Edouard@693
  1930
    switch (result) {
Edouard@693
  1931
        case SQLITE_ROW: {
vb@951
  1932
            *revoked_fpr = strdup((const char *)
vb@951
  1933
                    sqlite3_column_text(session->get_revoked, 0));
Edouard@693
  1934
            if(*revoked_fpr)
vb@951
  1935
                *revocation_date = sqlite3_column_int64(session->get_revoked,
vb@951
  1936
                        1);
Edouard@693
  1937
            else
Edouard@693
  1938
                status = PEP_OUT_OF_MEMORY;
Edouard@693
  1939
Edouard@693
  1940
            break;
Edouard@693
  1941
        }
Edouard@693
  1942
        default:
Edouard@693
  1943
            status = PEP_CANNOT_FIND_IDENTITY;
Edouard@693
  1944
    }
Edouard@693
  1945
Edouard@693
  1946
    sqlite3_reset(session->get_revoked);
Edouard@693
  1947
Edouard@693
  1948
    return status;
Edouard@693
  1949
}
Edouard@693
  1950
vb@958
  1951
PEP_STATUS key_created(
vb@958
  1952
        PEP_SESSION session,
vb@958
  1953
        const char *fpr,
vb@958
  1954
        time_t *created
vb@958
  1955
    )
vb@958
  1956
{
vb@958
  1957
    assert(session && fpr && created);
vb@958
  1958
    if (!(session && fpr && created))
vb@958
  1959
        return PEP_ILLEGAL_VALUE;
vb@958
  1960
vb@958
  1961
    return session->cryptotech[PEP_crypt_OpenPGP].key_created(session, fpr,
vb@958
  1962
            created);
vb@958
  1963
}
vb@958
  1964
krista@1011
  1965
DYNAMIC_API const char* get_engine_version() {
krista@1011
  1966
    return PEP_ENGINE_VERSION;
krista@1011
  1967
}
krista@1011
  1968
krista@1011
  1969
vb@482
  1970
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  1971
{
vb@482
  1972
    assert(session);
vb@482
  1973
vb@482
  1974
    if (!session)
vb@482
  1975
        return PEP_ILLEGAL_VALUE;
vb@482
  1976
fdik@494
  1977
    int int_result = sqlite3_exec(
fdik@494
  1978
        session->db,
fdik@494
  1979
        "delete from identity where address like '%@peptest.ch' ;",
fdik@494
  1980
        NULL,
fdik@494
  1981
        NULL,
fdik@494
  1982
        NULL
fdik@494
  1983
    );
fdik@494
  1984
    assert(int_result == SQLITE_OK);
vb@482
  1985
fdik@494
  1986
    return PEP_STATUS_OK;
vb@482
  1987
}
vb@482
  1988