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