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