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