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