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