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