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