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