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