src/pEpEngine.c
author Roker <roker@pep-project.org>
Thu, 14 Jul 2016 11:32:56 +0200
branchroker-linux
changeset 876 7bb8baa0d4ee
parent 734 bd9a3eb1d707
child 877 33ede5ddef27
permissions -rw-r--r--
tighter scope, more const
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;
Edouard@720
   417
#ifdef ANDROID
Edouard@720
   418
    _session->use_only_own_private_keys = true;
Edouard@720
   419
#elif TARGET_OS_IPHONE
Edouard@720
   420
    _session->use_only_own_private_keys = true;
Edouard@720
   421
#else
Edouard@720
   422
    _session->use_only_own_private_keys = false;
Edouard@720
   423
#endif
vb@464
   424
roker@529
   425
    *session = _session;
roker@529
   426
    return PEP_STATUS_OK;
vb@65
   427
vb@65
   428
enomem:
vb@65
   429
    status = PEP_OUT_OF_MEMORY;
vb@65
   430
vb@65
   431
pep_error:
vb@65
   432
    release(_session);
vb@65
   433
    return status;
vb@0
   434
}
vb@0
   435
vb@0
   436
DYNAMIC_API void release(PEP_SESSION session)
vb@0
   437
{
vb@62
   438
    bool out_last = false;
vb@62
   439
vb@62
   440
    assert(init_count >= 0);
roker@529
   441
    assert(session);
vb@0
   442
Edouard@385
   443
    if (!((init_count >= 0) && session))
vb@191
   444
        return;
vb@191
   445
vb@62
   446
    // a small race condition but still a race condition
vb@113
   447
    // mitigated by calling caveat (see documentation)
vb@62
   448
vb@62
   449
    if (init_count == 0)
vb@62
   450
        out_last = true;
vb@62
   451
    --init_count;
vb@62
   452
roker@529
   453
    if (session) {
roker@529
   454
        if (session->db) {
vb@517
   455
            if (session->log)
vb@517
   456
                sqlite3_finalize(session->log);
vb@233
   457
            if (session->trustword)
vb@233
   458
                sqlite3_finalize(session->trustword);
vb@65
   459
            if (session->get_identity)
vb@65
   460
                sqlite3_finalize(session->get_identity);
vb@65
   461
            if (session->set_person)
vb@65
   462
                sqlite3_finalize(session->set_person);
vb@65
   463
            if (session->set_pgp_keypair)
vb@65
   464
                sqlite3_finalize(session->set_pgp_keypair);
vb@517
   465
            if (session->set_identity)
vb@517
   466
                sqlite3_finalize(session->set_identity);
vb@65
   467
            if (session->set_trust)
vb@65
   468
                sqlite3_finalize(session->set_trust);
vb@65
   469
            if (session->get_trust)
vb@65
   470
                sqlite3_finalize(session->get_trust);
vb@251
   471
            if (session->least_trust)
vb@251
   472
                sqlite3_finalize(session->least_trust);
vb@517
   473
            if (session->mark_compromized)
vb@517
   474
                sqlite3_finalize(session->mark_compromized);
vb@517
   475
            if (session->crashdump)
vb@517
   476
                sqlite3_finalize(session->crashdump);
vb@517
   477
            if (session->languagelist)
vb@517
   478
                sqlite3_finalize(session->languagelist);
vb@517
   479
            if (session->i18n_token)
vb@517
   480
                sqlite3_finalize(session->i18n_token);
vb@517
   481
            if (session->blacklist_add)
vb@517
   482
                sqlite3_finalize(session->blacklist_add);
vb@517
   483
            if (session->blacklist_delete)
vb@517
   484
                sqlite3_finalize(session->blacklist_delete);
vb@517
   485
            if (session->blacklist_is_listed)
vb@517
   486
                sqlite3_finalize(session->blacklist_is_listed);
vb@517
   487
            if (session->blacklist_retrieve)
vb@517
   488
                sqlite3_finalize(session->blacklist_retrieve);
vb@26
   489
vb@65
   490
            if (session->db)
vb@65
   491
                sqlite3_close_v2(session->db);
vb@65
   492
            if (session->system_db)
vb@65
   493
                sqlite3_close_v2(session->system_db);
roker@529
   494
        }
vb@65
   495
vb@65
   496
        release_transport_system(session, out_last);
vb@65
   497
        release_cryptotech(session, out_last);
vb@65
   498
vb@65
   499
        free(session);
vb@62
   500
    }
vb@0
   501
}
vb@0
   502
vb@467
   503
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
   504
{
vb@464
   505
    assert(session);
vb@467
   506
    session->passive_mode = enable;
vb@464
   507
}
vb@464
   508
vb@467
   509
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
   510
{
vb@464
   511
    assert(session);
vb@467
   512
    session->unencrypted_subject = enable;
vb@464
   513
}
vb@464
   514
Edouard@720
   515
DYNAMIC_API void config_use_only_own_private_keys(PEP_SESSION session, bool enable)
Edouard@720
   516
{
Edouard@720
   517
    assert(session);
Edouard@720
   518
    session->use_only_own_private_keys = enable;
Edouard@720
   519
}
Edouard@720
   520
vb@0
   521
DYNAMIC_API PEP_STATUS log_event(
vb@450
   522
        PEP_SESSION session,
vb@451
   523
        const char *title,
vb@451
   524
        const char *entity,
vb@451
   525
        const char *description,
vb@451
   526
        const char *comment
vb@0
   527
    )
vb@0
   528
{
roker@529
   529
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   530
    int result;
vb@0
   531
roker@529
   532
    assert(session);
roker@529
   533
    assert(title);
roker@529
   534
    assert(entity);
vb@0
   535
vb@191
   536
    if (!(session && title && entity))
vb@191
   537
        return PEP_ILLEGAL_VALUE;
vb@191
   538
roker@529
   539
    sqlite3_reset(session->log);
roker@529
   540
    sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
roker@529
   541
    sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
roker@529
   542
    if (description)
vb@46
   543
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
roker@529
   544
    else
roker@529
   545
        sqlite3_bind_null(session->log, 3);
roker@529
   546
    if (comment)
roker@529
   547
        sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
roker@529
   548
    else
roker@529
   549
        sqlite3_bind_null(session->log, 4);
roker@529
   550
    do {
roker@529
   551
        result = sqlite3_step(session->log);
roker@529
   552
        assert(result == SQLITE_DONE || result == SQLITE_BUSY);
roker@529
   553
        if (result != SQLITE_DONE && result != SQLITE_BUSY)
roker@529
   554
            status = PEP_UNKNOWN_ERROR;
roker@529
   555
    } while (result == SQLITE_BUSY);
roker@529
   556
    sqlite3_reset(session->log);
vb@0
   557
roker@529
   558
    return status;
vb@0
   559
}
vb@0
   560
vb@233
   561
DYNAMIC_API PEP_STATUS trustword(
vb@0
   562
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   563
            char **word, size_t *wsize
vb@0
   564
        )
vb@0
   565
{
roker@529
   566
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   567
    int result;
vb@0
   568
roker@529
   569
    assert(session);
roker@529
   570
    assert(word);
roker@529
   571
    assert(wsize);
vb@0
   572
vb@191
   573
    if (!(session && word && wsize))
vb@191
   574
        return PEP_ILLEGAL_VALUE;
vb@191
   575
roker@529
   576
    *word = NULL;
roker@529
   577
    *wsize = 0;
vb@0
   578
roker@529
   579
    if (lang == NULL)
roker@529
   580
        lang = "en";
vb@0
   581
roker@529
   582
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   583
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   584
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   585
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   586
    assert(lang[2] == 0);
vb@0
   587
roker@529
   588
    sqlite3_reset(session->trustword);
vb@233
   589
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
roker@529
   590
    sqlite3_bind_int(session->trustword, 2, value);
vb@0
   591
roker@529
   592
    result = sqlite3_step(session->trustword);
roker@529
   593
    if (result == SQLITE_ROW) {
vb@233
   594
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
   595
                    1));
roker@529
   596
        if (*word)
vb@233
   597
            *wsize = sqlite3_column_bytes(session->trustword, 1);
roker@529
   598
        else
Edouard@693
   599
            status = PEP_OUT_OF_MEMORY;
roker@529
   600
    } else
roker@529
   601
        status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   602
roker@529
   603
    sqlite3_reset(session->trustword);
roker@529
   604
    return status;
vb@0
   605
}
vb@0
   606
vb@233
   607
DYNAMIC_API PEP_STATUS trustwords(
vb@0
   608
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   609
        char **words, size_t *wsize, int max_words
vb@0
   610
    )
vb@0
   611
{
roker@529
   612
    const char *source = fingerprint;
roker@529
   613
    char *buffer;
roker@529
   614
    char *dest;
roker@529
   615
    size_t fsize;
vb@0
   616
    PEP_STATUS _status;
vb@0
   617
roker@529
   618
    assert(session);
roker@529
   619
    assert(fingerprint);
roker@529
   620
    assert(words);
roker@529
   621
    assert(wsize);
roker@529
   622
    assert(max_words >= 0);
vb@0
   623
vb@191
   624
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
   625
        return PEP_ILLEGAL_VALUE;
vb@191
   626
roker@529
   627
    *words = NULL;
roker@529
   628
    *wsize = 0;
vb@0
   629
vb@251
   630
    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
   631
    assert(buffer);
vb@0
   632
    if (buffer == NULL)
vb@0
   633
        return PEP_OUT_OF_MEMORY;
vb@125
   634
    dest = buffer;
vb@0
   635
roker@529
   636
    fsize = strlen(fingerprint);
vb@0
   637
roker@529
   638
    if (!lang || !lang[0])
roker@529
   639
        lang = "en";
vb@0
   640
roker@529
   641
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   642
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   643
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   644
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   645
    assert(lang[2] == 0);
vb@0
   646
roker@529
   647
    int n_words = 0;
roker@529
   648
    while (source < fingerprint + fsize) {
roker@529
   649
        uint16_t value;
roker@529
   650
        char *word;
roker@529
   651
        size_t _wsize;
roker@529
   652
        int j;
vb@0
   653
vb@0
   654
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
roker@529
   655
            if (*source >= 'a' && *source <= 'f')
roker@529
   656
                value += (*source - 'a' + 10) << (3 - j++) * 4;
roker@529
   657
            else if (*source >= 'A' && *source <= 'F')
roker@529
   658
                value += (*source - 'A' + 10) << (3 - j++) * 4;
roker@529
   659
            else if (*source >= '0' && *source <= '9')
roker@529
   660
                value += (*source - '0') << (3 - j++) * 4;
roker@529
   661
            
roker@529
   662
            source++;
roker@529
   663
        }
vb@0
   664
roker@529
   665
        _status = trustword(session, value, lang, &word, &_wsize);
vb@0
   666
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   667
            free(buffer);
vb@0
   668
            return PEP_OUT_OF_MEMORY;
vb@0
   669
        }
roker@529
   670
        if (word == NULL) {
vb@0
   671
            free(buffer);
roker@529
   672
            return PEP_TRUSTWORD_NOT_FOUND;
vb@0
   673
        }
vb@0
   674
roker@529
   675
        if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
roker@529
   676
            strncpy(dest, word, _wsize);
vb@0
   677
            free(word);
roker@529
   678
            dest += _wsize;
roker@529
   679
        }
roker@529
   680
        else {
vb@0
   681
            free(word);
roker@529
   682
            break; // buffer full
vb@0
   683
        }
vb@0
   684
roker@529
   685
        if (source < fingerprint + fsize
vb@251
   686
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
roker@529
   687
            *dest++ = ' ';
vb@0
   688
roker@529
   689
        ++n_words;
roker@529
   690
        if (max_words && n_words >= max_words)
roker@529
   691
            break;
roker@529
   692
    }
vb@0
   693
roker@529
   694
    *words = buffer;
roker@529
   695
    *wsize = dest - buffer;
roker@529
   696
    return PEP_STATUS_OK;
vb@0
   697
}
vb@0
   698
vb@0
   699
pEp_identity *new_identity(
vb@0
   700
        const char *address, const char *fpr, const char *user_id,
vb@0
   701
        const char *username
vb@0
   702
    )
vb@0
   703
{
vb@0
   704
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   705
    assert(result);
vb@0
   706
    if (result) {
vb@0
   707
        if (address) {
vb@0
   708
            result->address = strdup(address);
vb@0
   709
            assert(result->address);
vb@0
   710
            if (result->address == NULL) {
vb@0
   711
                free(result);
vb@0
   712
                return NULL;
vb@0
   713
            }
vb@0
   714
        }
vb@0
   715
        if (fpr) {
vb@0
   716
            result->fpr = strdup(fpr);
vb@0
   717
            assert(result->fpr);
vb@0
   718
            if (result->fpr == NULL) {
vb@0
   719
                free_identity(result);
vb@0
   720
                return NULL;
vb@0
   721
            }
vb@0
   722
        }
vb@0
   723
        if (user_id) {
vb@0
   724
            result->user_id = strdup(user_id);
vb@0
   725
            assert(result->user_id);
vb@0
   726
            if (result->user_id == NULL) {
vb@0
   727
                free_identity(result);
vb@0
   728
                return NULL;
vb@0
   729
            }
vb@0
   730
        }
vb@0
   731
        if (username) {
vb@0
   732
            result->username = strdup(username);
vb@0
   733
            assert(result->username);
vb@0
   734
            if (result->username == NULL) {
vb@0
   735
                free_identity(result);
vb@0
   736
                return NULL;
vb@0
   737
            }
vb@0
   738
        }
vb@0
   739
    }
vb@0
   740
    return result;
vb@0
   741
}
vb@0
   742
vb@37
   743
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   744
{
vb@37
   745
    assert(src);
vb@37
   746
vb@37
   747
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
vb@37
   748
    assert(dup);
vb@37
   749
    if (dup == NULL)
vb@37
   750
        return NULL;
vb@37
   751
    
vb@37
   752
    dup->comm_type = src->comm_type;
vb@37
   753
    dup->lang[0] = src->lang[0];
vb@37
   754
    dup->lang[1] = src->lang[1];
vb@37
   755
    dup->lang[2] = 0;
vb@37
   756
    dup->me = src->me;
vb@37
   757
vb@37
   758
    return dup;
vb@37
   759
}
vb@37
   760
vb@0
   761
void free_identity(pEp_identity *identity)
vb@0
   762
{
vb@0
   763
    if (identity) {
vb@0
   764
        free(identity->address);
vb@0
   765
        free(identity->fpr);
vb@0
   766
        free(identity->user_id);
vb@0
   767
        free(identity->username);
vb@0
   768
        free(identity);
vb@0
   769
    }
vb@0
   770
}
vb@0
   771
vb@0
   772
DYNAMIC_API PEP_STATUS get_identity(
Edouard@559
   773
        PEP_SESSION session,
Edouard@559
   774
        const char *address,
Edouard@559
   775
        const char *user_id,
vb@0
   776
        pEp_identity **identity
vb@0
   777
    )
vb@0
   778
{
roker@529
   779
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   780
    static pEp_identity *_identity;
vb@0
   781
roker@529
   782
    assert(session);
roker@529
   783
    assert(address);
vb@8
   784
    assert(address[0]);
vb@632
   785
    assert(identity);
vb@0
   786
vb@632
   787
    if (!(session && address && address[0] && identity))
vb@191
   788
        return PEP_ILLEGAL_VALUE;
vb@191
   789
vb@632
   790
    *identity = NULL;
vb@632
   791
vb@46
   792
    sqlite3_reset(session->get_identity);
vb@46
   793
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
Edouard@559
   794
    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
vb@0
   795
roker@876
   796
    const int result = sqlite3_step(session->get_identity);
roker@529
   797
    switch (result) {
roker@529
   798
    case SQLITE_ROW:
vb@0
   799
        _identity = new_identity(
vb@0
   800
                address,
vb@46
   801
                (const char *) sqlite3_column_text(session->get_identity, 0),
Edouard@559
   802
                user_id,
Edouard@559
   803
                (const char *) sqlite3_column_text(session->get_identity, 1)
vb@0
   804
                );
vb@0
   805
        assert(_identity);
vb@0
   806
        if (_identity == NULL)
vb@0
   807
            return PEP_OUT_OF_MEMORY;
vb@0
   808
Edouard@559
   809
        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 2);
roker@876
   810
        const char* const _lang = (const char *) sqlite3_column_text(session->get_identity, 3);
vb@0
   811
        if (_lang && _lang[0]) {
roker@529
   812
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
roker@529
   813
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
roker@529
   814
            assert(_lang[2] == 0);
roker@529
   815
            _identity->lang[0] = _lang[0];
roker@529
   816
            _identity->lang[1] = _lang[1];
vb@0
   817
            _identity->lang[2] = 0;
roker@529
   818
        }
roker@529
   819
        *identity = _identity;
roker@529
   820
        break;
roker@529
   821
    default:
vb@0
   822
        status = PEP_CANNOT_FIND_IDENTITY;
roker@529
   823
        *identity = NULL;
roker@529
   824
    }
vb@0
   825
vb@46
   826
    sqlite3_reset(session->get_identity);
roker@529
   827
    return status;
vb@0
   828
}
vb@0
   829
vb@0
   830
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   831
        PEP_SESSION session, const pEp_identity *identity
vb@0
   832
    )
vb@0
   833
{
roker@529
   834
    int result;
vb@0
   835
roker@529
   836
    assert(session);
roker@529
   837
    assert(identity);
roker@529
   838
    assert(identity->address);
roker@529
   839
    assert(identity->fpr);
roker@529
   840
    assert(identity->user_id);
roker@529
   841
    assert(identity->username);
vb@0
   842
vb@191
   843
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   844
                identity->user_id && identity->username))
vb@191
   845
        return PEP_ILLEGAL_VALUE;
vb@191
   846
vb@515
   847
    bool listed;
vb@515
   848
    PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
vb@515
   849
    assert(status == PEP_STATUS_OK);
vb@515
   850
    if (status != PEP_STATUS_OK)
vb@515
   851
        return status;
vb@515
   852
vb@515
   853
    if (listed)
vb@515
   854
        return PEP_KEY_BLACKLISTED;
vb@515
   855
roker@529
   856
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   857
roker@529
   858
    sqlite3_reset(session->set_person);
vb@46
   859
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
   860
            SQLITE_STATIC);
vb@46
   861
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
   862
            SQLITE_STATIC);
roker@529
   863
    if (identity->lang[0])
vb@46
   864
        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
vb@0
   865
                SQLITE_STATIC);
roker@529
   866
    else
roker@529
   867
        sqlite3_bind_null(session->set_person, 3);
Edouard@641
   868
    sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
Edouard@641
   869
                      SQLITE_STATIC);
roker@529
   870
    result = sqlite3_step(session->set_person);
roker@529
   871
    sqlite3_reset(session->set_person);
roker@529
   872
    if (result != SQLITE_DONE) {
roker@529
   873
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   874
        return PEP_CANNOT_SET_PERSON;
roker@529
   875
    }
vb@0
   876
roker@529
   877
    sqlite3_reset(session->set_pgp_keypair);
vb@46
   878
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   879
            SQLITE_STATIC);
roker@529
   880
    result = sqlite3_step(session->set_pgp_keypair);
roker@529
   881
    sqlite3_reset(session->set_pgp_keypair);
roker@529
   882
    if (result != SQLITE_DONE) {
roker@529
   883
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   884
        return PEP_CANNOT_SET_PGP_KEYPAIR;
roker@529
   885
    }
vb@0
   886
roker@529
   887
    sqlite3_reset(session->set_identity);
vb@46
   888
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
   889
            SQLITE_STATIC);
vb@46
   890
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
   891
            SQLITE_STATIC);
vb@46
   892
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
   893
            SQLITE_STATIC);
roker@529
   894
    result = sqlite3_step(session->set_identity);
roker@529
   895
    sqlite3_reset(session->set_identity);
roker@529
   896
    if (result != SQLITE_DONE) {
roker@529
   897
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   898
        return PEP_CANNOT_SET_IDENTITY;
roker@529
   899
    }
vb@0
   900
roker@529
   901
    sqlite3_reset(session->set_trust);
vb@46
   902
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
   903
            SQLITE_STATIC);
vb@46
   904
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
   905
            SQLITE_STATIC);
roker@529
   906
    sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
roker@529
   907
    result = sqlite3_step(session->set_trust);
roker@529
   908
    sqlite3_reset(session->set_trust);
roker@529
   909
    if (result != SQLITE_DONE) {
roker@529
   910
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
Edouard@584
   911
        return PEP_CANNOT_SET_TRUST;
roker@529
   912
    }
vb@0
   913
vb@46
   914
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
roker@529
   915
    if (result == SQLITE_OK)
roker@529
   916
        return PEP_STATUS_OK;
roker@529
   917
    else
roker@529
   918
        return PEP_COMMIT_FAILED;
vb@0
   919
}
vb@0
   920
vb@357
   921
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
   922
        PEP_SESSION session,
vb@357
   923
        const char *fpr
vb@357
   924
    )
vb@357
   925
{
roker@529
   926
    int result;
vb@357
   927
roker@529
   928
    assert(session);
vb@357
   929
    assert(fpr && fpr[0]);
vb@357
   930
vb@357
   931
    if (!(session && fpr && fpr[0]))
vb@357
   932
        return PEP_ILLEGAL_VALUE;
vb@357
   933
roker@529
   934
    sqlite3_reset(session->mark_compromized);
vb@357
   935
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
   936
            SQLITE_STATIC);
vb@357
   937
    result = sqlite3_step(session->mark_compromized);
roker@529
   938
    sqlite3_reset(session->mark_compromized);
vb@357
   939
vb@357
   940
    if (result != SQLITE_DONE)
Edouard@584
   941
        return PEP_CANNOT_SET_TRUST;
vb@357
   942
vb@357
   943
    return PEP_STATUS_OK;
vb@357
   944
}
vb@357
   945
vb@0
   946
void pEp_free(void *p)
vb@0
   947
{
vb@0
   948
    free(p);
vb@0
   949
}
vb@0
   950
vb@8
   951
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
   952
{
vb@8
   953
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
   954
    int result;
vb@8
   955
vb@8
   956
    assert(session);
vb@8
   957
    assert(identity);
vb@8
   958
    assert(identity->user_id);
vb@8
   959
    assert(identity->user_id[0]);
vb@8
   960
    assert(identity->fpr);
vb@8
   961
    assert(identity->fpr[0]);
vb@8
   962
vb@191
   963
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
   964
                identity->fpr && identity->fpr[0]))
vb@191
   965
        return PEP_ILLEGAL_VALUE;
vb@191
   966
vb@8
   967
    identity->comm_type = PEP_ct_unknown;
vb@8
   968
vb@46
   969
    sqlite3_reset(session->get_trust);
vb@46
   970
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
vb@46
   971
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
   972
vb@46
   973
    result = sqlite3_step(session->get_trust);
vb@8
   974
    switch (result) {
vb@8
   975
    case SQLITE_ROW: {
Edouard@559
   976
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 0);
vb@8
   977
        identity->comm_type = comm_type;
vb@8
   978
        break;
vb@8
   979
    }
vb@8
   980
 
vb@8
   981
    default:
vb@8
   982
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
   983
    }
vb@8
   984
vb@46
   985
    sqlite3_reset(session->get_trust);
vb@8
   986
    return status;
vb@9
   987
}
vb@9
   988
vb@251
   989
DYNAMIC_API PEP_STATUS least_trust(
vb@251
   990
        PEP_SESSION session,
vb@251
   991
        const char *fpr,
vb@251
   992
        PEP_comm_type *comm_type
vb@251
   993
    )
vb@251
   994
{
vb@251
   995
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
   996
    int result;
vb@251
   997
vb@251
   998
    assert(session);
vb@251
   999
    assert(fpr);
vb@251
  1000
    assert(comm_type);
vb@251
  1001
vb@251
  1002
    if (!(session && fpr && comm_type))
vb@251
  1003
        return PEP_ILLEGAL_VALUE;
vb@251
  1004
vb@632
  1005
    *comm_type = PEP_ct_unknown;
vb@632
  1006
vb@251
  1007
    sqlite3_reset(session->least_trust);
vb@251
  1008
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
  1009
vb@251
  1010
    result = sqlite3_step(session->least_trust);
vb@251
  1011
    switch (result) {
vb@251
  1012
        case SQLITE_ROW: {
vb@259
  1013
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
  1014
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
  1015
            break;
vb@251
  1016
        }
vb@251
  1017
        default:
vb@251
  1018
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
  1019
    }
vb@251
  1020
vb@251
  1021
    sqlite3_reset(session->least_trust);
vb@251
  1022
    return status;
vb@251
  1023
}
vb@251
  1024
vb@24
  1025
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
  1026
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
  1027
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
  1028
    )
vb@24
  1029
{
vb@191
  1030
    assert(session);
vb@191
  1031
    assert(ctext);
vb@191
  1032
    assert(csize);
vb@191
  1033
    assert(ptext);
vb@191
  1034
    assert(psize);
vb@191
  1035
    assert(keylist);
vb@191
  1036
vb@191
  1037
    if (!(session && ctext && csize && ptext && psize && keylist && keylist))
vb@191
  1038
        return PEP_ILLEGAL_VALUE;
vb@191
  1039
vb@46
  1040
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
vb@24
  1041
}
vb@24
  1042
vb@24
  1043
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
  1044
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
  1045
    size_t psize, char **ctext, size_t *csize
vb@24
  1046
    )
vb@24
  1047
{
vb@191
  1048
    assert(session);
vb@191
  1049
    assert(keylist);
vb@191
  1050
    assert(ptext);
vb@191
  1051
    assert(psize);
vb@191
  1052
    assert(ctext);
vb@191
  1053
    assert(csize);
vb@191
  1054
vb@191
  1055
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
  1056
        return PEP_ILLEGAL_VALUE;
vb@191
  1057
vb@46
  1058
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
vb@24
  1059
}
vb@24
  1060
vb@24
  1061
DYNAMIC_API PEP_STATUS verify_text(
vb@24
  1062
    PEP_SESSION session, const char *text, size_t size,
vb@24
  1063
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
  1064
    )
vb@24
  1065
{
vb@191
  1066
    assert(session);
vb@191
  1067
    assert(text);
vb@191
  1068
    assert(size);
vb@191
  1069
    assert(signature);
vb@191
  1070
    assert(sig_size);
vb@191
  1071
    assert(keylist);
vb@191
  1072
vb@191
  1073
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
  1074
        return PEP_ILLEGAL_VALUE;
vb@191
  1075
vb@46
  1076
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
vb@24
  1077
}
vb@24
  1078
vb@24
  1079
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1080
{
vb@191
  1081
    assert(session);
vb@191
  1082
    assert(fpr);
vb@191
  1083
vb@191
  1084
    if (!(session && fpr))
vb@191
  1085
        return PEP_ILLEGAL_VALUE;
vb@191
  1086
vb@46
  1087
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
  1088
}
vb@24
  1089
vb@24
  1090
DYNAMIC_API PEP_STATUS export_key(
vb@46
  1091
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1092
    )
vb@24
  1093
{
vb@191
  1094
    assert(session);
vb@191
  1095
    assert(fpr);
vb@191
  1096
    assert(key_data);
vb@191
  1097
    assert(size);
vb@191
  1098
vb@191
  1099
    if (!(session && fpr && key_data && size))
vb@191
  1100
        return PEP_ILLEGAL_VALUE;
vb@191
  1101
vb@46
  1102
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
vb@24
  1103
}
vb@24
  1104
vb@24
  1105
DYNAMIC_API PEP_STATUS find_keys(
vb@46
  1106
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1107
    )
vb@24
  1108
{
vb@191
  1109
    assert(session);
vb@191
  1110
    assert(pattern);
vb@191
  1111
    assert(keylist);
vb@191
  1112
vb@191
  1113
    if (!(session && pattern && keylist))
vb@191
  1114
        return PEP_ILLEGAL_VALUE;
vb@191
  1115
vb@46
  1116
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
vb@24
  1117
}
vb@24
  1118
vb@24
  1119
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
  1120
        PEP_SESSION session, pEp_identity *identity
vb@24
  1121
    )
vb@24
  1122
{
vb@191
  1123
    assert(session);
vb@191
  1124
    assert(identity);
vb@191
  1125
    assert(identity->address);
vb@298
  1126
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
  1127
    assert(identity->username);
vb@191
  1128
vb@298
  1129
    if (!(session && identity && identity->address &&
vb@298
  1130
        (identity->fpr == NULL || identity->fpr[0] == 0) && identity->username))
vb@191
  1131
        return PEP_ILLEGAL_VALUE;
vb@191
  1132
vb@46
  1133
    return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
vb@24
  1134
}
vb@24
  1135
vb@9
  1136
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
  1137
        PEP_SESSION session,
vb@46
  1138
        const char *fpr,
vb@46
  1139
        PEP_comm_type *comm_type
vb@9
  1140
    )
vb@9
  1141
{
vb@191
  1142
    assert(session);
vb@191
  1143
    assert(fpr);
vb@191
  1144
    assert(comm_type);
vb@191
  1145
vb@191
  1146
    if (!(session && fpr && comm_type))
vb@191
  1147
        return PEP_ILLEGAL_VALUE;
vb@191
  1148
vb@46
  1149
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
vb@24
  1150
}
vb@9
  1151
Edouard@728
  1152
DYNAMIC_API PEP_STATUS import_key(
Edouard@728
  1153
        PEP_SESSION session,
Edouard@728
  1154
        const char *key_data,
Edouard@728
  1155
        size_t size,
Edouard@728
  1156
        identity_list **private_keys
Edouard@728
  1157
    )
vb@24
  1158
{
vb@191
  1159
    assert(session);
vb@191
  1160
    assert(key_data);
vb@191
  1161
vb@191
  1162
    if (!(session && key_data))
vb@191
  1163
        return PEP_ILLEGAL_VALUE;
vb@191
  1164
Edouard@728
  1165
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size, private_keys);
vb@24
  1166
}
vb@9
  1167
vb@24
  1168
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1169
{
vb@191
  1170
    assert(session);
vb@191
  1171
    assert(pattern);
vb@191
  1172
vb@191
  1173
    if (!(session && pattern))
vb@191
  1174
        return PEP_ILLEGAL_VALUE;
vb@191
  1175
vb@46
  1176
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1177
}
vb@9
  1178
vb@24
  1179
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1180
{
vb@191
  1181
    assert(session);
vb@191
  1182
    assert(pattern);
vb@191
  1183
vb@191
  1184
    if (!(session && pattern))
vb@191
  1185
        return PEP_ILLEGAL_VALUE;
vb@191
  1186
vb@46
  1187
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1188
}
vb@196
  1189
vb@201
  1190
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1191
        PEP_SESSION session,
vb@201
  1192
        const char *fpr,
vb@201
  1193
        const timestamp *ts
vb@201
  1194
    )
vb@196
  1195
{
vb@196
  1196
    assert(session);
vb@200
  1197
    assert(fpr);
vb@196
  1198
vb@200
  1199
    if (!(session && fpr))
vb@196
  1200
        return PEP_ILLEGAL_VALUE;
vb@196
  1201
vb@201
  1202
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1203
}
vb@196
  1204
vb@211
  1205
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1206
        PEP_SESSION session,
vb@211
  1207
        const char *fpr,
vb@211
  1208
        const char *reason
vb@211
  1209
    )
vb@197
  1210
{
vb@197
  1211
    assert(session);
vb@200
  1212
    assert(fpr);
vb@197
  1213
vb@200
  1214
    if (!(session && fpr))
vb@197
  1215
        return PEP_ILLEGAL_VALUE;
vb@197
  1216
vb@214
  1217
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1218
            reason);
vb@197
  1219
}
vb@197
  1220
vb@214
  1221
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1222
        PEP_SESSION session,
vb@214
  1223
        const char *fpr,
Edouard@701
  1224
        const time_t when,
vb@214
  1225
        bool *expired
vb@214
  1226
    )
vb@214
  1227
{
vb@214
  1228
    assert(session);
vb@214
  1229
    assert(fpr);
vb@214
  1230
    assert(expired);
vb@214
  1231
vb@214
  1232
    if (!(session && fpr && expired))
vb@214
  1233
        return PEP_ILLEGAL_VALUE;
vb@214
  1234
vb@214
  1235
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
Edouard@701
  1236
            when, expired);
vb@214
  1237
}
vb@214
  1238
Edouard@694
  1239
DYNAMIC_API PEP_STATUS key_revoked(
Edouard@694
  1240
                                   PEP_SESSION session,
Edouard@694
  1241
                                   const char *fpr,
Edouard@694
  1242
                                   bool *revoked
Edouard@694
  1243
                                   )
Edouard@694
  1244
{
Edouard@694
  1245
    assert(session);
Edouard@694
  1246
    assert(fpr);
Edouard@694
  1247
    assert(revoked);
Edouard@694
  1248
    
Edouard@694
  1249
    if (!(session && fpr && revoked))
Edouard@694
  1250
        return PEP_ILLEGAL_VALUE;
Edouard@694
  1251
    
Edouard@694
  1252
    return session->cryptotech[PEP_crypt_OpenPGP].key_revoked(session, fpr,
Edouard@694
  1253
                                                              revoked);
Edouard@694
  1254
}
Edouard@694
  1255
vb@451
  1256
static void _clean_log_value(char *text)
vb@451
  1257
{
vb@451
  1258
    if (text) {
vb@451
  1259
        for (char *c = text; *c; c++) {
vb@722
  1260
            if (*c < 32 && *c != '\n')
vb@451
  1261
                *c = 32;
vb@451
  1262
            else if (*c == '"')
vb@451
  1263
                *c = '\'';
vb@451
  1264
        }
vb@451
  1265
    }
vb@451
  1266
}
vb@451
  1267
vb@450
  1268
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1269
{
vb@454
  1270
    str2 = str2 ? str2 : "";
vb@450
  1271
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1272
    size_t len2 = strlen(str2);
vb@450
  1273
    size_t len = len1 + len2 + 3;
vb@450
  1274
    char * result = realloc(str1, len + 1);
vb@450
  1275
vb@450
  1276
    if (result) {
vb@450
  1277
        result[len1] = '"';
vb@450
  1278
        strcpy(result + len1 + 1, str2);
vb@450
  1279
        result[len - 2] = '"';
vb@450
  1280
        result[len - 1] = delim;
vb@450
  1281
        result[len] = 0;
vb@450
  1282
    }
vb@450
  1283
    else {
vb@450
  1284
        free(str1);
vb@450
  1285
    }
vb@450
  1286
vb@450
  1287
    return result;
vb@450
  1288
}
vb@450
  1289
vb@450
  1290
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1291
        PEP_SESSION session,
vb@450
  1292
        int maxlines,
vb@450
  1293
        char **logdata
vb@450
  1294
    )
vb@450
  1295
{
vb@450
  1296
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1297
    char *_logdata= NULL;
vb@450
  1298
vb@450
  1299
    assert(session);
vb@450
  1300
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1301
    assert(logdata);
vb@450
  1302
vb@450
  1303
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1304
            CRASHDUMP_MAX_LINES))
vb@450
  1305
        return PEP_ILLEGAL_VALUE;
vb@450
  1306
vb@632
  1307
    *logdata = NULL;
vb@632
  1308
vb@450
  1309
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1310
    const char *timestamp = NULL;
vb@458
  1311
    const char *title = NULL;
vb@458
  1312
    const char *entity = NULL;
vb@458
  1313
    const char *desc = NULL;
vb@458
  1314
    const char *comment = NULL;
vb@450
  1315
vb@450
  1316
    sqlite3_reset(session->crashdump);
vb@452
  1317
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1318
vb@450
  1319
    int result;
vb@450
  1320
vb@450
  1321
    do {
vb@450
  1322
        result = sqlite3_step(session->crashdump);
vb@450
  1323
        switch (result) {
vb@450
  1324
        case SQLITE_ROW:
vb@452
  1325
            timestamp = (const char *) sqlite3_column_text(session->crashdump, 0);
vb@452
  1326
            title   = (const char *) sqlite3_column_text(session->crashdump, 1);
vb@452
  1327
            entity  = (const char *) sqlite3_column_text(session->crashdump, 2);
vb@452
  1328
            desc    = (const char *) sqlite3_column_text(session->crashdump, 3);
vb@452
  1329
            comment = (const char *) sqlite3_column_text(session->crashdump, 4);
vb@452
  1330
vb@452
  1331
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1332
            if (_logdata == NULL)
vb@452
  1333
                goto enomem;
vb@450
  1334
vb@450
  1335
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1336
            if (_logdata == NULL)
vb@450
  1337
                goto enomem;
vb@450
  1338
vb@450
  1339
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1340
            if (_logdata == NULL)
vb@450
  1341
                goto enomem;
vb@450
  1342
vb@450
  1343
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1344
            if (_logdata == NULL)
vb@450
  1345
                goto enomem;
vb@450
  1346
vb@450
  1347
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1348
            if (_logdata == NULL)
vb@450
  1349
                goto enomem;
vb@450
  1350
vb@687
  1351
            _clean_log_value(_logdata);
vb@450
  1352
            break;
vb@450
  1353
vb@450
  1354
        case SQLITE_DONE:
vb@450
  1355
            break;
vb@450
  1356
vb@450
  1357
        default:
vb@450
  1358
            status = PEP_UNKNOWN_ERROR;
vb@450
  1359
            result = SQLITE_DONE;
vb@450
  1360
        }
vb@450
  1361
    } while (result != SQLITE_DONE);
vb@450
  1362
vb@450
  1363
    sqlite3_reset(session->crashdump);
vb@450
  1364
    if (status == PEP_STATUS_OK)
vb@450
  1365
        *logdata = _logdata;
vb@450
  1366
vb@450
  1367
    goto the_end;
vb@450
  1368
vb@450
  1369
enomem:
vb@450
  1370
    status = PEP_OUT_OF_MEMORY;
vb@450
  1371
vb@450
  1372
the_end:
vb@450
  1373
    return status;
vb@450
  1374
}
vb@450
  1375
vb@458
  1376
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  1377
        PEP_SESSION session,
vb@458
  1378
        char **languages
vb@458
  1379
    )
vb@458
  1380
{
vb@458
  1381
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1382
    char *_languages= NULL;
vb@458
  1383
vb@458
  1384
    assert(session);
vb@458
  1385
    assert(languages);
vb@458
  1386
vb@458
  1387
    if (!(session && languages))
vb@458
  1388
        return PEP_ILLEGAL_VALUE;
vb@458
  1389
vb@632
  1390
    *languages = NULL;
vb@632
  1391
vb@458
  1392
    const char *lang = NULL;
vb@458
  1393
    const char *name = NULL;
vb@617
  1394
    const char *phrase = NULL;
vb@458
  1395
vb@458
  1396
    sqlite3_reset(session->languagelist);
vb@458
  1397
vb@458
  1398
    int result;
vb@458
  1399
vb@458
  1400
    do {
vb@458
  1401
        result = sqlite3_step(session->languagelist);
vb@458
  1402
        switch (result) {
vb@458
  1403
        case SQLITE_ROW:
vb@458
  1404
            lang = (const char *) sqlite3_column_text(session->languagelist, 0);
vb@458
  1405
            name = (const char *) sqlite3_column_text(session->languagelist, 1);
vb@617
  1406
            phrase = (const char *) sqlite3_column_text(session->languagelist, 2);
vb@458
  1407
vb@458
  1408
            _languages = _concat_string(_languages, lang, ',');
vb@458
  1409
            if (_languages == NULL)
vb@458
  1410
                goto enomem;
vb@458
  1411
vb@617
  1412
            _languages = _concat_string(_languages, name, ',');
vb@617
  1413
            if (_languages == NULL)
vb@617
  1414
                goto enomem;
vb@617
  1415
vb@617
  1416
            _languages = _concat_string(_languages, phrase, '\n');
vb@458
  1417
            if (_languages == NULL)
vb@458
  1418
                goto enomem;
vb@458
  1419
vb@458
  1420
            break;
vb@458
  1421
vb@458
  1422
        case SQLITE_DONE:
vb@458
  1423
            break;
vb@458
  1424
vb@458
  1425
        default:
vb@458
  1426
            status = PEP_UNKNOWN_ERROR;
vb@458
  1427
            result = SQLITE_DONE;
vb@458
  1428
        }
vb@458
  1429
    } while (result != SQLITE_DONE);
vb@458
  1430
vb@458
  1431
    sqlite3_reset(session->languagelist);
vb@458
  1432
    if (status == PEP_STATUS_OK)
vb@458
  1433
        *languages = _languages;
vb@458
  1434
vb@458
  1435
    goto the_end;
vb@458
  1436
vb@458
  1437
enomem:
vb@458
  1438
    status = PEP_OUT_OF_MEMORY;
vb@458
  1439
vb@458
  1440
the_end:
vb@458
  1441
    return status;
vb@458
  1442
}
vb@458
  1443
vb@458
  1444
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  1445
        PEP_SESSION session,
vb@458
  1446
        const char *lang,
vb@458
  1447
        int phrase_id,
vb@458
  1448
        char **phrase
vb@458
  1449
    )
vb@458
  1450
{
vb@458
  1451
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1452
vb@479
  1453
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@479
  1454
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  1455
        return PEP_ILLEGAL_VALUE;
vb@458
  1456
vb@632
  1457
    *phrase = NULL;
vb@632
  1458
vb@458
  1459
    sqlite3_reset(session->i18n_token);
roker@529
  1460
    sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
roker@529
  1461
    sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  1462
vb@458
  1463
    const char *_phrase = NULL;
vb@458
  1464
    int result;
vb@458
  1465
vb@458
  1466
    result = sqlite3_step(session->i18n_token);
vb@458
  1467
    switch (result) {
vb@458
  1468
    case SQLITE_ROW:
vb@458
  1469
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  1470
        break;
vb@458
  1471
vb@458
  1472
    case SQLITE_DONE:
vb@458
  1473
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  1474
        break;
vb@458
  1475
vb@458
  1476
    default:
vb@458
  1477
        status = PEP_UNKNOWN_ERROR;
vb@458
  1478
    }
vb@458
  1479
vb@458
  1480
    if (status == PEP_STATUS_OK) {
vb@458
  1481
        *phrase = strdup(_phrase);
vb@458
  1482
        if (*phrase == NULL)
vb@458
  1483
            goto enomem;
vb@458
  1484
    }
vb@459
  1485
vb@459
  1486
    sqlite3_reset(session->i18n_token);
vb@458
  1487
    goto the_end;
vb@458
  1488
vb@458
  1489
enomem:
vb@458
  1490
    status = PEP_OUT_OF_MEMORY;
vb@458
  1491
vb@458
  1492
the_end:
vb@458
  1493
    return status;
vb@458
  1494
}
vb@458
  1495
vb@632
  1496
DYNAMIC_API PEP_STATUS sequence_value(
vb@632
  1497
        PEP_SESSION session,
vb@632
  1498
        const char *name,
vb@652
  1499
        int32_t *value
vb@632
  1500
    )
vb@632
  1501
{
vb@632
  1502
    PEP_STATUS status = PEP_STATUS_OK;
vb@632
  1503
    int result;
vb@632
  1504
vb@632
  1505
    assert(session);
vb@632
  1506
    assert(name);
vb@632
  1507
    assert(value);
vb@632
  1508
vb@632
  1509
    if (!(session && name && value))
vb@632
  1510
        return PEP_ILLEGAL_VALUE;
vb@632
  1511
vb@632
  1512
    *value = 0;
vb@632
  1513
vb@633
  1514
    sqlite3_reset(session->sequence_value1);
vb@633
  1515
    sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
vb@633
  1516
    result = sqlite3_step(session->sequence_value1);
vb@633
  1517
    assert(result == SQLITE_DONE);
vb@633
  1518
    sqlite3_reset(session->sequence_value1);
vb@633
  1519
    if (result != SQLITE_DONE) {
vb@633
  1520
        status = PEP_UNKNOWN_ERROR;
vb@633
  1521
    }
vb@633
  1522
    else {
vb@633
  1523
        sqlite3_reset(session->sequence_value2);
vb@633
  1524
        sqlite3_bind_text(session->sequence_value2, 1, name, -1, SQLITE_STATIC);
vb@633
  1525
        result = sqlite3_step(session->sequence_value2);
vb@633
  1526
        switch (result) {
vb@633
  1527
            case SQLITE_ROW: {
vb@652
  1528
                int32_t _value = (int32_t)
vb@633
  1529
                        sqlite3_column_int64(session->sequence_value2, 0);
vb@633
  1530
                *value = _value;
vb@633
  1531
                break;
vb@633
  1532
            }
vb@633
  1533
            default:
vb@633
  1534
                status = PEP_UNKNOWN_ERROR;
vb@632
  1535
        }
vb@633
  1536
        sqlite3_reset(session->sequence_value2);
vb@632
  1537
    }
vb@632
  1538
    return status;
vb@632
  1539
}
vb@632
  1540
Edouard@693
  1541
DYNAMIC_API PEP_STATUS set_revoked(
Edouard@693
  1542
       PEP_SESSION session,
Edouard@693
  1543
       const char *revoked_fpr,
Edouard@693
  1544
       const char *replacement_fpr,
Edouard@693
  1545
       const uint64_t revocation_date
Edouard@693
  1546
    )
Edouard@693
  1547
{
Edouard@693
  1548
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  1549
    
Edouard@693
  1550
    assert(session &&
Edouard@693
  1551
           revoked_fpr && revoked_fpr[0] &&
Edouard@693
  1552
           replacement_fpr && replacement_fpr[0]
Edouard@693
  1553
          );
Edouard@693
  1554
    
Edouard@693
  1555
    if (!(session &&
Edouard@693
  1556
          revoked_fpr && revoked_fpr[0] &&
Edouard@693
  1557
          replacement_fpr && replacement_fpr[0]
Edouard@693
  1558
         ))
Edouard@693
  1559
        return PEP_ILLEGAL_VALUE;
Edouard@693
  1560
    
Edouard@693
  1561
    sqlite3_reset(session->set_revoked);
Edouard@693
  1562
    sqlite3_bind_text(session->set_revoked, 1, revoked_fpr, -1, SQLITE_STATIC);
Edouard@693
  1563
    sqlite3_bind_text(session->set_revoked, 2, replacement_fpr, -1, SQLITE_STATIC);
Edouard@693
  1564
    sqlite3_bind_int64(session->set_revoked, 3, revocation_date);
Edouard@693
  1565
Edouard@693
  1566
    int result;
Edouard@693
  1567
    
Edouard@693
  1568
    result = sqlite3_step(session->set_revoked);
Edouard@693
  1569
    switch (result) {
Edouard@693
  1570
        case SQLITE_DONE:
Edouard@693
  1571
            status = PEP_STATUS_OK;
Edouard@693
  1572
            break;
Edouard@693
  1573
            
Edouard@693
  1574
        default:
Edouard@693
  1575
            status = PEP_UNKNOWN_ERROR;
Edouard@693
  1576
    }
Edouard@693
  1577
    
Edouard@693
  1578
    sqlite3_reset(session->set_revoked);
Edouard@693
  1579
    return status;
Edouard@693
  1580
}
Edouard@693
  1581
Edouard@693
  1582
DYNAMIC_API PEP_STATUS get_revoked(
Edouard@693
  1583
        PEP_SESSION session,
Edouard@693
  1584
        const char *fpr,
Edouard@693
  1585
        char **revoked_fpr,
Edouard@693
  1586
        uint64_t *revocation_date
Edouard@693
  1587
    )
Edouard@693
  1588
{
Edouard@693
  1589
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  1590
Edouard@693
  1591
    assert(session &&
Edouard@693
  1592
           revoked_fpr &&
Edouard@693
  1593
           fpr && fpr[0]
Edouard@693
  1594
          );
Edouard@693
  1595
    
Edouard@693
  1596
    if (!(session &&
Edouard@693
  1597
           revoked_fpr &&
Edouard@693
  1598
           fpr && fpr[0]
Edouard@693
  1599
          ))
Edouard@693
  1600
        return PEP_ILLEGAL_VALUE;
Edouard@693
  1601
Edouard@693
  1602
    *revoked_fpr = NULL;
Edouard@693
  1603
    *revocation_date = 0;
Edouard@693
  1604
Edouard@693
  1605
    sqlite3_reset(session->get_revoked);
Edouard@693
  1606
    sqlite3_bind_text(session->get_revoked, 1, fpr, -1, SQLITE_STATIC);
Edouard@693
  1607
Edouard@693
  1608
    int result;
Edouard@693
  1609
    
Edouard@693
  1610
    result = sqlite3_step(session->get_revoked);
Edouard@693
  1611
    switch (result) {
Edouard@693
  1612
        case SQLITE_ROW: {
Edouard@693
  1613
            *revoked_fpr = strdup((const char *) sqlite3_column_text(session->get_revoked, 0));
Edouard@693
  1614
            if(*revoked_fpr)
Edouard@693
  1615
                *revocation_date = sqlite3_column_int64(session->get_revoked, 1);
Edouard@693
  1616
            else
Edouard@693
  1617
                status = PEP_OUT_OF_MEMORY;
Edouard@693
  1618
Edouard@693
  1619
            break;
Edouard@693
  1620
        }
Edouard@693
  1621
        default:
Edouard@693
  1622
            status = PEP_CANNOT_FIND_IDENTITY;
Edouard@693
  1623
    }
Edouard@693
  1624
Edouard@693
  1625
    sqlite3_reset(session->get_revoked);
Edouard@693
  1626
Edouard@693
  1627
    return status;
Edouard@693
  1628
}
Edouard@693
  1629
vb@482
  1630
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  1631
{
vb@482
  1632
    assert(session);
vb@482
  1633
vb@482
  1634
    if (!session)
vb@482
  1635
        return PEP_ILLEGAL_VALUE;
vb@482
  1636
fdik@494
  1637
    int int_result = sqlite3_exec(
fdik@494
  1638
        session->db,
fdik@494
  1639
        "delete from identity where address like '%@peptest.ch' ;",
fdik@494
  1640
        NULL,
fdik@494
  1641
        NULL,
fdik@494
  1642
        NULL
fdik@494
  1643
    );
fdik@494
  1644
    assert(int_result == SQLITE_OK);
vb@482
  1645
fdik@494
  1646
    return PEP_STATUS_OK;
vb@482
  1647
}
vb@482
  1648