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