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