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