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