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