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