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