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