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