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