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