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