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