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