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