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