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