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