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