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