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