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