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