src/pEpEngine.c
author Volker Birk <vb@pep.foundation>
Mon, 01 Aug 2016 12:03:00 +0200
branchkeysync
changeset 958 cef602982909
parent 955 060272dfaaeb
child 986 8f85f70bdd75
permissions -rw-r--r--
key_created() added to determine creation date of a key
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@928
   136
                "create table 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@62
   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@928
   255
                NULL,
vb@928
   256
                NULL,
vb@928
   257
                NULL
vb@928
   258
            );
vb@928
   259
            assert(int_result == SQLITE_OK);
vb@928
   260
        }
vb@928
   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@944
   269
                NULL,
vb@944
   270
                NULL,
vb@944
   271
                NULL
vb@944
   272
            );
vb@944
   273
            assert(int_result == SQLITE_OK);
vb@944
   274
        }
vb@944
   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@955
   363
        sql_own_identities_retrieve =  "select address, fpr, username, '" PEP_OWN_USERID "',"
vb@955
   364
                            "   comm_type, 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@65
   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@0
   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@62
   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) {
roker@529
   555
        if (session->db) {
vb@65
   556
            if (session->log)
vb@65
   557
                sqlite3_finalize(session->log);
vb@517
   558
            if (session->trustword)
vb@517
   559
                sqlite3_finalize(session->trustword);
vb@65
   560
            if (session->get_identity)
vb@65
   561
                sqlite3_finalize(session->get_identity);
vb@65
   562
            if (session->set_person)
vb@65
   563
                sqlite3_finalize(session->set_person);
vb@65
   564
            if (session->set_pgp_keypair)
vb@65
   565
                sqlite3_finalize(session->set_pgp_keypair);
vb@517
   566
            if (session->set_identity)
vb@517
   567
                sqlite3_finalize(session->set_identity);
vb@932
   568
            if (session->set_identity_flags)
vb@932
   569
                sqlite3_finalize(session->set_identity_flags);
vb@65
   570
            if (session->set_trust)
vb@65
   571
                sqlite3_finalize(session->set_trust);
vb@65
   572
            if (session->get_trust)
vb@65
   573
                sqlite3_finalize(session->get_trust);
vb@251
   574
            if (session->least_trust)
vb@251
   575
                sqlite3_finalize(session->least_trust);
vb@517
   576
            if (session->mark_compromized)
vb@517
   577
                sqlite3_finalize(session->mark_compromized);
vb@517
   578
            if (session->crashdump)
vb@517
   579
                sqlite3_finalize(session->crashdump);
vb@517
   580
            if (session->languagelist)
vb@517
   581
                sqlite3_finalize(session->languagelist);
vb@517
   582
            if (session->i18n_token)
vb@517
   583
                sqlite3_finalize(session->i18n_token);
vb@517
   584
            if (session->blacklist_add)
vb@517
   585
                sqlite3_finalize(session->blacklist_add);
vb@517
   586
            if (session->blacklist_delete)
vb@517
   587
                sqlite3_finalize(session->blacklist_delete);
vb@517
   588
            if (session->blacklist_is_listed)
vb@517
   589
                sqlite3_finalize(session->blacklist_is_listed);
vb@517
   590
            if (session->blacklist_retrieve)
vb@517
   591
                sqlite3_finalize(session->blacklist_retrieve);
vb@26
   592
vb@65
   593
            if (session->db)
vb@65
   594
                sqlite3_close_v2(session->db);
vb@65
   595
            if (session->system_db)
vb@65
   596
                sqlite3_close_v2(session->system_db);
roker@529
   597
        }
vb@28
   598
vb@65
   599
        release_transport_system(session, out_last);
vb@65
   600
        release_cryptotech(session, out_last);
vb@62
   601
vb@65
   602
        free(session);
vb@65
   603
    }
vb@0
   604
}
vb@0
   605
vb@467
   606
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
   607
{
vb@464
   608
    assert(session);
vb@467
   609
    session->passive_mode = enable;
vb@464
   610
}
vb@464
   611
vb@467
   612
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
   613
{
vb@464
   614
    assert(session);
vb@467
   615
    session->unencrypted_subject = enable;
vb@464
   616
}
vb@464
   617
vb@951
   618
DYNAMIC_API void config_use_only_own_private_keys(PEP_SESSION session,
vb@951
   619
        bool enable)
Edouard@720
   620
{
Edouard@720
   621
    assert(session);
Edouard@720
   622
    session->use_only_own_private_keys = enable;
Edouard@720
   623
}
Edouard@720
   624
vb@0
   625
DYNAMIC_API PEP_STATUS log_event(
vb@450
   626
        PEP_SESSION session,
vb@451
   627
        const char *title,
vb@451
   628
        const char *entity,
vb@451
   629
        const char *description,
vb@451
   630
        const char *comment
vb@0
   631
    )
vb@0
   632
{
roker@529
   633
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   634
    int result;
vb@0
   635
roker@529
   636
    assert(session);
roker@529
   637
    assert(title);
roker@529
   638
    assert(entity);
vb@0
   639
vb@191
   640
    if (!(session && title && entity))
vb@191
   641
        return PEP_ILLEGAL_VALUE;
vb@191
   642
roker@529
   643
    sqlite3_reset(session->log);
roker@529
   644
    sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
roker@529
   645
    sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
roker@529
   646
    if (description)
vb@46
   647
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
roker@529
   648
    else
roker@529
   649
        sqlite3_bind_null(session->log, 3);
roker@529
   650
    if (comment)
roker@529
   651
        sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
roker@529
   652
    else
roker@529
   653
        sqlite3_bind_null(session->log, 4);
roker@529
   654
    do {
roker@529
   655
        result = sqlite3_step(session->log);
roker@529
   656
        assert(result == SQLITE_DONE || result == SQLITE_BUSY);
roker@529
   657
        if (result != SQLITE_DONE && result != SQLITE_BUSY)
roker@529
   658
            status = PEP_UNKNOWN_ERROR;
roker@529
   659
    } while (result == SQLITE_BUSY);
roker@529
   660
    sqlite3_reset(session->log);
vb@0
   661
roker@529
   662
    return status;
vb@0
   663
}
vb@0
   664
vb@233
   665
DYNAMIC_API PEP_STATUS trustword(
vb@0
   666
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   667
            char **word, size_t *wsize
vb@0
   668
        )
vb@0
   669
{
roker@529
   670
    PEP_STATUS status = PEP_STATUS_OK;
vb@0
   671
roker@529
   672
    assert(session);
roker@529
   673
    assert(word);
roker@529
   674
    assert(wsize);
vb@0
   675
vb@191
   676
    if (!(session && word && wsize))
vb@191
   677
        return PEP_ILLEGAL_VALUE;
vb@191
   678
roker@529
   679
    *word = NULL;
roker@529
   680
    *wsize = 0;
vb@0
   681
roker@529
   682
    if (lang == NULL)
roker@529
   683
        lang = "en";
vb@0
   684
roker@529
   685
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   686
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   687
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   688
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   689
    assert(lang[2] == 0);
vb@0
   690
roker@529
   691
    sqlite3_reset(session->trustword);
vb@233
   692
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
roker@529
   693
    sqlite3_bind_int(session->trustword, 2, value);
vb@0
   694
roker@880
   695
    const int result = sqlite3_step(session->trustword);
roker@529
   696
    if (result == SQLITE_ROW) {
vb@233
   697
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
   698
                    1));
roker@529
   699
        if (*word)
vb@233
   700
            *wsize = sqlite3_column_bytes(session->trustword, 1);
roker@529
   701
        else
Edouard@693
   702
            status = PEP_OUT_OF_MEMORY;
roker@529
   703
    } else
roker@529
   704
        status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   705
roker@529
   706
    sqlite3_reset(session->trustword);
roker@529
   707
    return status;
vb@0
   708
}
vb@0
   709
vb@233
   710
DYNAMIC_API PEP_STATUS trustwords(
vb@0
   711
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   712
        char **words, size_t *wsize, int max_words
vb@0
   713
    )
vb@0
   714
{
roker@529
   715
    const char *source = fingerprint;
roker@529
   716
    char *buffer;
roker@529
   717
    char *dest;
roker@529
   718
    size_t fsize;
vb@0
   719
roker@529
   720
    assert(session);
roker@529
   721
    assert(fingerprint);
roker@529
   722
    assert(words);
roker@529
   723
    assert(wsize);
roker@529
   724
    assert(max_words >= 0);
vb@0
   725
vb@191
   726
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
   727
        return PEP_ILLEGAL_VALUE;
vb@191
   728
roker@529
   729
    *words = NULL;
roker@529
   730
    *wsize = 0;
vb@0
   731
vb@251
   732
    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
   733
    assert(buffer);
vb@0
   734
    if (buffer == NULL)
vb@0
   735
        return PEP_OUT_OF_MEMORY;
vb@125
   736
    dest = buffer;
vb@0
   737
roker@529
   738
    fsize = strlen(fingerprint);
vb@0
   739
roker@529
   740
    if (!lang || !lang[0])
roker@529
   741
        lang = "en";
vb@0
   742
roker@529
   743
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   744
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   745
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   746
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   747
    assert(lang[2] == 0);
vb@0
   748
roker@529
   749
    int n_words = 0;
roker@529
   750
    while (source < fingerprint + fsize) {
vb@939
   751
        PEP_STATUS _status;
roker@529
   752
        uint16_t value;
roker@529
   753
        char *word;
roker@529
   754
        size_t _wsize;
roker@529
   755
        int j;
vb@0
   756
vb@0
   757
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
roker@529
   758
            if (*source >= 'a' && *source <= 'f')
roker@529
   759
                value += (*source - 'a' + 10) << (3 - j++) * 4;
roker@529
   760
            else if (*source >= 'A' && *source <= 'F')
roker@529
   761
                value += (*source - 'A' + 10) << (3 - j++) * 4;
roker@529
   762
            else if (*source >= '0' && *source <= '9')
roker@529
   763
                value += (*source - '0') << (3 - j++) * 4;
roker@529
   764
            
roker@529
   765
            source++;
roker@529
   766
        }
vb@0
   767
roker@529
   768
        _status = trustword(session, value, lang, &word, &_wsize);
vb@0
   769
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   770
            free(buffer);
vb@0
   771
            return PEP_OUT_OF_MEMORY;
vb@0
   772
        }
roker@529
   773
        if (word == NULL) {
vb@0
   774
            free(buffer);
roker@529
   775
            return PEP_TRUSTWORD_NOT_FOUND;
vb@0
   776
        }
vb@0
   777
roker@529
   778
        if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
roker@529
   779
            strncpy(dest, word, _wsize);
vb@0
   780
            free(word);
roker@529
   781
            dest += _wsize;
roker@529
   782
        }
roker@529
   783
        else {
vb@0
   784
            free(word);
roker@529
   785
            break; // buffer full
vb@0
   786
        }
vb@0
   787
roker@529
   788
        if (source < fingerprint + fsize
vb@251
   789
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
roker@529
   790
            *dest++ = ' ';
vb@0
   791
roker@529
   792
        ++n_words;
roker@529
   793
        if (max_words && n_words >= max_words)
roker@529
   794
            break;
roker@529
   795
    }
vb@0
   796
roker@529
   797
    *words = buffer;
roker@529
   798
    *wsize = dest - buffer;
roker@529
   799
    return PEP_STATUS_OK;
vb@0
   800
}
vb@0
   801
vb@0
   802
pEp_identity *new_identity(
vb@0
   803
        const char *address, const char *fpr, const char *user_id,
vb@0
   804
        const char *username
vb@0
   805
    )
vb@0
   806
{
vb@0
   807
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   808
    assert(result);
vb@0
   809
    if (result) {
vb@0
   810
        if (address) {
vb@0
   811
            result->address = strdup(address);
vb@0
   812
            assert(result->address);
vb@0
   813
            if (result->address == NULL) {
vb@0
   814
                free(result);
vb@0
   815
                return NULL;
vb@0
   816
            }
vb@0
   817
        }
vb@0
   818
        if (fpr) {
vb@0
   819
            result->fpr = strdup(fpr);
vb@0
   820
            assert(result->fpr);
vb@0
   821
            if (result->fpr == NULL) {
vb@0
   822
                free_identity(result);
vb@0
   823
                return NULL;
vb@0
   824
            }
vb@0
   825
        }
vb@0
   826
        if (user_id) {
vb@0
   827
            result->user_id = strdup(user_id);
vb@0
   828
            assert(result->user_id);
vb@0
   829
            if (result->user_id == NULL) {
vb@0
   830
                free_identity(result);
vb@0
   831
                return NULL;
vb@0
   832
            }
vb@0
   833
        }
vb@0
   834
        if (username) {
vb@0
   835
            result->username = strdup(username);
vb@0
   836
            assert(result->username);
vb@0
   837
            if (result->username == NULL) {
vb@0
   838
                free_identity(result);
vb@0
   839
                return NULL;
vb@0
   840
            }
vb@0
   841
        }
vb@0
   842
    }
vb@0
   843
    return result;
vb@0
   844
}
vb@0
   845
vb@37
   846
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   847
{
vb@37
   848
    assert(src);
vb@37
   849
vb@951
   850
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id,
vb@951
   851
            src->username);
vb@37
   852
    assert(dup);
vb@37
   853
    if (dup == NULL)
vb@37
   854
        return NULL;
vb@37
   855
    
vb@37
   856
    dup->comm_type = src->comm_type;
vb@37
   857
    dup->lang[0] = src->lang[0];
vb@37
   858
    dup->lang[1] = src->lang[1];
vb@37
   859
    dup->lang[2] = 0;
vb@37
   860
    dup->me = src->me;
vb@930
   861
    dup->flags = src->flags;
vb@37
   862
vb@37
   863
    return dup;
vb@37
   864
}
vb@37
   865
vb@0
   866
void free_identity(pEp_identity *identity)
vb@0
   867
{
vb@0
   868
    if (identity) {
vb@0
   869
        free(identity->address);
vb@0
   870
        free(identity->fpr);
vb@0
   871
        free(identity->user_id);
vb@0
   872
        free(identity->username);
vb@0
   873
        free(identity);
vb@0
   874
    }
vb@0
   875
}
vb@0
   876
vb@0
   877
DYNAMIC_API PEP_STATUS get_identity(
Edouard@559
   878
        PEP_SESSION session,
Edouard@559
   879
        const char *address,
Edouard@559
   880
        const char *user_id,
vb@0
   881
        pEp_identity **identity
vb@0
   882
    )
vb@0
   883
{
roker@529
   884
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   885
    static pEp_identity *_identity;
vb@0
   886
roker@529
   887
    assert(session);
roker@529
   888
    assert(address);
vb@8
   889
    assert(address[0]);
vb@632
   890
    assert(identity);
vb@0
   891
vb@632
   892
    if (!(session && address && address[0] && identity))
vb@191
   893
        return PEP_ILLEGAL_VALUE;
vb@191
   894
vb@632
   895
    *identity = NULL;
vb@632
   896
vb@46
   897
    sqlite3_reset(session->get_identity);
vb@46
   898
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
Edouard@559
   899
    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
vb@0
   900
roker@879
   901
    const int result = sqlite3_step(session->get_identity);
roker@529
   902
    switch (result) {
roker@529
   903
    case SQLITE_ROW:
vb@0
   904
        _identity = new_identity(
vb@0
   905
                address,
vb@46
   906
                (const char *) sqlite3_column_text(session->get_identity, 0),
Edouard@559
   907
                user_id,
Edouard@559
   908
                (const char *) sqlite3_column_text(session->get_identity, 1)
vb@0
   909
                );
vb@0
   910
        assert(_identity);
vb@0
   911
        if (_identity == NULL)
vb@0
   912
            return PEP_OUT_OF_MEMORY;
vb@0
   913
vb@951
   914
        _identity->comm_type = (PEP_comm_type)
vb@951
   915
            sqlite3_column_int(session->get_identity, 2);
vb@951
   916
        const char* const _lang = (const char *)
vb@951
   917
            sqlite3_column_text(session->get_identity, 3);
vb@0
   918
        if (_lang && _lang[0]) {
roker@529
   919
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
roker@529
   920
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
roker@529
   921
            assert(_lang[2] == 0);
roker@529
   922
            _identity->lang[0] = _lang[0];
roker@529
   923
            _identity->lang[1] = _lang[1];
vb@0
   924
            _identity->lang[2] = 0;
roker@529
   925
        }
vb@951
   926
        _identity->flags = (unsigned int)
vb@951
   927
            sqlite3_column_int(session->get_identity, 4);
roker@529
   928
        *identity = _identity;
roker@529
   929
        break;
roker@529
   930
    default:
vb@0
   931
        status = PEP_CANNOT_FIND_IDENTITY;
roker@529
   932
        *identity = NULL;
roker@529
   933
    }
vb@0
   934
vb@46
   935
    sqlite3_reset(session->get_identity);
roker@529
   936
    return status;
vb@0
   937
}
vb@0
   938
vb@0
   939
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   940
        PEP_SESSION session, const pEp_identity *identity
vb@0
   941
    )
vb@0
   942
{
roker@529
   943
    int result;
vb@0
   944
roker@529
   945
    assert(session);
roker@529
   946
    assert(identity);
roker@529
   947
    assert(identity->address);
roker@529
   948
    assert(identity->fpr);
roker@529
   949
    assert(identity->user_id);
roker@529
   950
    assert(identity->username);
vb@0
   951
vb@191
   952
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   953
                identity->user_id && identity->username))
vb@191
   954
        return PEP_ILLEGAL_VALUE;
vb@191
   955
vb@515
   956
    bool listed;
vb@515
   957
    PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
vb@515
   958
    assert(status == PEP_STATUS_OK);
vb@515
   959
    if (status != PEP_STATUS_OK)
vb@515
   960
        return status;
vb@515
   961
vb@515
   962
    if (listed)
vb@515
   963
        return PEP_KEY_BLACKLISTED;
vb@515
   964
roker@529
   965
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   966
roker@529
   967
    sqlite3_reset(session->set_person);
vb@46
   968
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
   969
            SQLITE_STATIC);
vb@46
   970
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
   971
            SQLITE_STATIC);
roker@529
   972
    if (identity->lang[0])
vb@46
   973
        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
vb@0
   974
                SQLITE_STATIC);
roker@529
   975
    else
roker@529
   976
        sqlite3_bind_null(session->set_person, 3);
Edouard@641
   977
    sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
Edouard@641
   978
                      SQLITE_STATIC);
roker@529
   979
    result = sqlite3_step(session->set_person);
roker@529
   980
    sqlite3_reset(session->set_person);
roker@529
   981
    if (result != SQLITE_DONE) {
roker@529
   982
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   983
        return PEP_CANNOT_SET_PERSON;
roker@529
   984
    }
vb@0
   985
roker@529
   986
    sqlite3_reset(session->set_pgp_keypair);
vb@46
   987
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   988
            SQLITE_STATIC);
roker@529
   989
    result = sqlite3_step(session->set_pgp_keypair);
roker@529
   990
    sqlite3_reset(session->set_pgp_keypair);
roker@529
   991
    if (result != SQLITE_DONE) {
roker@529
   992
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   993
        return PEP_CANNOT_SET_PGP_KEYPAIR;
roker@529
   994
    }
vb@0
   995
roker@529
   996
    sqlite3_reset(session->set_identity);
vb@46
   997
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
   998
            SQLITE_STATIC);
vb@46
   999
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
  1000
            SQLITE_STATIC);
vb@46
  1001
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
  1002
            SQLITE_STATIC);
vb@932
  1003
    sqlite3_bind_int(session->set_identity, 4, identity->flags);
roker@529
  1004
    result = sqlite3_step(session->set_identity);
roker@529
  1005
    sqlite3_reset(session->set_identity);
roker@529
  1006
    if (result != SQLITE_DONE) {
roker@529
  1007
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1008
        return PEP_CANNOT_SET_IDENTITY;
roker@529
  1009
    }
vb@0
  1010
roker@529
  1011
    sqlite3_reset(session->set_trust);
vb@46
  1012
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
  1013
            SQLITE_STATIC);
vb@46
  1014
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
  1015
            SQLITE_STATIC);
roker@529
  1016
    sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
roker@529
  1017
    result = sqlite3_step(session->set_trust);
roker@529
  1018
    sqlite3_reset(session->set_trust);
roker@529
  1019
    if (result != SQLITE_DONE) {
roker@529
  1020
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
Edouard@584
  1021
        return PEP_CANNOT_SET_TRUST;
roker@529
  1022
    }
vb@0
  1023
vb@46
  1024
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
roker@529
  1025
    if (result == SQLITE_OK)
roker@529
  1026
        return PEP_STATUS_OK;
roker@529
  1027
    else
roker@529
  1028
        return PEP_COMMIT_FAILED;
vb@0
  1029
}
vb@0
  1030
vb@932
  1031
DYNAMIC_API PEP_STATUS set_identity_flags(
vb@934
  1032
        PEP_SESSION session,
vb@934
  1033
        pEp_identity *identity,
vb@934
  1034
        unsigned int flags
vb@932
  1035
    )
vb@932
  1036
{
vb@932
  1037
    int result;
vb@932
  1038
vb@932
  1039
    assert(session);
vb@932
  1040
    assert(identity);
vb@932
  1041
    assert(identity->address);
vb@932
  1042
    assert(identity->user_id);
vb@932
  1043
vb@932
  1044
    if (!(session && identity && identity->address && identity->user_id))
vb@932
  1045
        return PEP_ILLEGAL_VALUE;
vb@932
  1046
vb@932
  1047
    sqlite3_reset(session->set_identity_flags);
vb@934
  1048
    sqlite3_bind_int(session->set_identity_flags, 1, flags);
vb@932
  1049
    sqlite3_bind_text(session->set_identity_flags, 2, identity->address, -1,
vb@932
  1050
            SQLITE_STATIC);
vb@932
  1051
    sqlite3_bind_text(session->set_identity_flags, 3, identity->user_id, -1,
vb@932
  1052
            SQLITE_STATIC);
vb@932
  1053
    result = sqlite3_step(session->set_identity_flags);
vb@932
  1054
    sqlite3_reset(session->set_identity_flags);
vb@932
  1055
    if (result != SQLITE_DONE)
vb@932
  1056
        return PEP_CANNOT_SET_IDENTITY;
vb@932
  1057
vb@934
  1058
    identity->flags = flags;
vb@932
  1059
    return PEP_STATUS_OK;
vb@932
  1060
}
vb@932
  1061
vb@357
  1062
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
  1063
        PEP_SESSION session,
vb@357
  1064
        const char *fpr
vb@357
  1065
    )
vb@357
  1066
{
roker@529
  1067
    int result;
vb@357
  1068
roker@529
  1069
    assert(session);
vb@357
  1070
    assert(fpr && fpr[0]);
vb@357
  1071
vb@357
  1072
    if (!(session && fpr && fpr[0]))
vb@357
  1073
        return PEP_ILLEGAL_VALUE;
vb@357
  1074
roker@529
  1075
    sqlite3_reset(session->mark_compromized);
vb@357
  1076
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
  1077
            SQLITE_STATIC);
vb@357
  1078
    result = sqlite3_step(session->mark_compromized);
roker@529
  1079
    sqlite3_reset(session->mark_compromized);
vb@357
  1080
vb@357
  1081
    if (result != SQLITE_DONE)
Edouard@584
  1082
        return PEP_CANNOT_SET_TRUST;
vb@357
  1083
vb@357
  1084
    return PEP_STATUS_OK;
vb@357
  1085
}
vb@357
  1086
vb@0
  1087
void pEp_free(void *p)
vb@0
  1088
{
vb@0
  1089
    free(p);
vb@0
  1090
}
vb@0
  1091
vb@8
  1092
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
  1093
{
vb@8
  1094
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
  1095
    int result;
vb@8
  1096
vb@8
  1097
    assert(session);
vb@8
  1098
    assert(identity);
vb@8
  1099
    assert(identity->user_id);
vb@8
  1100
    assert(identity->user_id[0]);
vb@8
  1101
    assert(identity->fpr);
vb@8
  1102
    assert(identity->fpr[0]);
vb@8
  1103
vb@191
  1104
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
  1105
                identity->fpr && identity->fpr[0]))
vb@191
  1106
        return PEP_ILLEGAL_VALUE;
vb@191
  1107
vb@8
  1108
    identity->comm_type = PEP_ct_unknown;
vb@8
  1109
vb@46
  1110
    sqlite3_reset(session->get_trust);
vb@951
  1111
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1,
vb@951
  1112
            SQLITE_STATIC);
vb@46
  1113
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
  1114
vb@46
  1115
    result = sqlite3_step(session->get_trust);
vb@8
  1116
    switch (result) {
vb@8
  1117
    case SQLITE_ROW: {
vb@951
  1118
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust,
vb@951
  1119
                0);
vb@8
  1120
        identity->comm_type = comm_type;
vb@8
  1121
        break;
vb@8
  1122
    }
vb@8
  1123
 
vb@8
  1124
    default:
vb@8
  1125
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
  1126
    }
vb@8
  1127
vb@46
  1128
    sqlite3_reset(session->get_trust);
vb@8
  1129
    return status;
vb@9
  1130
}
vb@9
  1131
vb@251
  1132
DYNAMIC_API PEP_STATUS least_trust(
vb@251
  1133
        PEP_SESSION session,
vb@251
  1134
        const char *fpr,
vb@251
  1135
        PEP_comm_type *comm_type
vb@251
  1136
    )
vb@251
  1137
{
vb@251
  1138
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
  1139
    int result;
vb@251
  1140
vb@251
  1141
    assert(session);
vb@251
  1142
    assert(fpr);
vb@251
  1143
    assert(comm_type);
vb@251
  1144
vb@251
  1145
    if (!(session && fpr && comm_type))
vb@251
  1146
        return PEP_ILLEGAL_VALUE;
vb@251
  1147
vb@632
  1148
    *comm_type = PEP_ct_unknown;
vb@632
  1149
vb@251
  1150
    sqlite3_reset(session->least_trust);
vb@251
  1151
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
  1152
vb@251
  1153
    result = sqlite3_step(session->least_trust);
vb@251
  1154
    switch (result) {
vb@251
  1155
        case SQLITE_ROW: {
vb@259
  1156
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
  1157
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
  1158
            break;
vb@251
  1159
        }
vb@251
  1160
        default:
vb@251
  1161
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
  1162
    }
vb@251
  1163
vb@251
  1164
    sqlite3_reset(session->least_trust);
vb@251
  1165
    return status;
vb@251
  1166
}
vb@251
  1167
vb@24
  1168
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
  1169
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
  1170
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
  1171
    )
vb@24
  1172
{
vb@191
  1173
    assert(session);
vb@191
  1174
    assert(ctext);
vb@191
  1175
    assert(csize);
vb@191
  1176
    assert(ptext);
vb@191
  1177
    assert(psize);
vb@191
  1178
    assert(keylist);
vb@191
  1179
vb@939
  1180
    if (!(session && ctext && csize && ptext && psize && keylist))
vb@191
  1181
        return PEP_ILLEGAL_VALUE;
vb@191
  1182
vb@939
  1183
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(
vb@939
  1184
            session, ctext, csize, ptext, psize, keylist);
vb@24
  1185
}
vb@24
  1186
vb@24
  1187
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
  1188
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
  1189
    size_t psize, char **ctext, size_t *csize
vb@24
  1190
    )
vb@24
  1191
{
vb@191
  1192
    assert(session);
vb@191
  1193
    assert(keylist);
vb@191
  1194
    assert(ptext);
vb@191
  1195
    assert(psize);
vb@191
  1196
    assert(ctext);
vb@191
  1197
    assert(csize);
vb@191
  1198
vb@191
  1199
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
  1200
        return PEP_ILLEGAL_VALUE;
vb@191
  1201
vb@951
  1202
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session,
vb@951
  1203
            keylist, ptext, psize, ctext, csize);
vb@24
  1204
}
vb@24
  1205
vb@24
  1206
DYNAMIC_API PEP_STATUS verify_text(
vb@24
  1207
    PEP_SESSION session, const char *text, size_t size,
vb@24
  1208
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
  1209
    )
vb@24
  1210
{
vb@191
  1211
    assert(session);
vb@191
  1212
    assert(text);
vb@191
  1213
    assert(size);
vb@191
  1214
    assert(signature);
vb@191
  1215
    assert(sig_size);
vb@191
  1216
    assert(keylist);
vb@191
  1217
vb@191
  1218
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
  1219
        return PEP_ILLEGAL_VALUE;
vb@191
  1220
vb@951
  1221
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text,
vb@951
  1222
            size, signature, sig_size, keylist);
vb@24
  1223
}
vb@24
  1224
vb@24
  1225
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1226
{
vb@191
  1227
    assert(session);
vb@191
  1228
    assert(fpr);
vb@191
  1229
vb@191
  1230
    if (!(session && fpr))
vb@191
  1231
        return PEP_ILLEGAL_VALUE;
vb@191
  1232
vb@46
  1233
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
  1234
}
vb@24
  1235
vb@24
  1236
DYNAMIC_API PEP_STATUS export_key(
vb@46
  1237
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1238
    )
vb@24
  1239
{
vb@191
  1240
    assert(session);
vb@191
  1241
    assert(fpr);
vb@191
  1242
    assert(key_data);
vb@191
  1243
    assert(size);
vb@191
  1244
vb@191
  1245
    if (!(session && fpr && key_data && size))
vb@191
  1246
        return PEP_ILLEGAL_VALUE;
vb@191
  1247
vb@951
  1248
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr,
vb@951
  1249
            key_data, size);
vb@24
  1250
}
vb@24
  1251
vb@24
  1252
DYNAMIC_API PEP_STATUS find_keys(
vb@46
  1253
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1254
    )
vb@24
  1255
{
vb@191
  1256
    assert(session);
vb@191
  1257
    assert(pattern);
vb@191
  1258
    assert(keylist);
vb@191
  1259
vb@191
  1260
    if (!(session && pattern && keylist))
vb@191
  1261
        return PEP_ILLEGAL_VALUE;
vb@191
  1262
vb@951
  1263
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern,
vb@951
  1264
            keylist);
vb@24
  1265
}
vb@24
  1266
vb@24
  1267
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
  1268
        PEP_SESSION session, pEp_identity *identity
vb@24
  1269
    )
vb@24
  1270
{
vb@191
  1271
    assert(session);
vb@191
  1272
    assert(identity);
vb@191
  1273
    assert(identity->address);
vb@298
  1274
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
  1275
    assert(identity->username);
vb@191
  1276
vb@298
  1277
    if (!(session && identity && identity->address &&
vb@951
  1278
            (identity->fpr == NULL || identity->fpr[0] == 0) &&
vb@951
  1279
            identity->username))
vb@191
  1280
        return PEP_ILLEGAL_VALUE;
vb@191
  1281
vb@944
  1282
    PEP_STATUS status =
vb@944
  1283
        session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session,
vb@944
  1284
                identity);
vb@944
  1285
    if (status != PEP_STATUS_OK)
vb@944
  1286
        return status;
vb@944
  1287
vb@944
  1288
    // if a state machine for keysync is in place, inject notify
vb@944
  1289
    if (session->sync_state != DeviceState_state_NONE)
vb@951
  1290
        status = fsm_DeviceState_inject(session, KeyGen, NULL, NULL);
vb@944
  1291
vb@944
  1292
    return status;
vb@24
  1293
}
vb@24
  1294
vb@9
  1295
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
  1296
        PEP_SESSION session,
vb@46
  1297
        const char *fpr,
vb@46
  1298
        PEP_comm_type *comm_type
vb@9
  1299
    )
vb@9
  1300
{
vb@191
  1301
    assert(session);
vb@191
  1302
    assert(fpr);
vb@191
  1303
    assert(comm_type);
vb@191
  1304
vb@191
  1305
    if (!(session && fpr && comm_type))
vb@191
  1306
        return PEP_ILLEGAL_VALUE;
vb@191
  1307
vb@951
  1308
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr,
vb@951
  1309
            comm_type);
vb@24
  1310
}
vb@9
  1311
Edouard@728
  1312
DYNAMIC_API PEP_STATUS import_key(
Edouard@728
  1313
        PEP_SESSION session,
Edouard@728
  1314
        const char *key_data,
Edouard@728
  1315
        size_t size,
Edouard@728
  1316
        identity_list **private_keys
Edouard@728
  1317
    )
vb@24
  1318
{
vb@191
  1319
    assert(session);
vb@191
  1320
    assert(key_data);
vb@191
  1321
vb@191
  1322
    if (!(session && key_data))
vb@191
  1323
        return PEP_ILLEGAL_VALUE;
vb@191
  1324
vb@951
  1325
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data,
vb@951
  1326
            size, private_keys);
vb@24
  1327
}
vb@9
  1328
vb@24
  1329
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1330
{
vb@191
  1331
    assert(session);
vb@191
  1332
    assert(pattern);
vb@191
  1333
vb@191
  1334
    if (!(session && pattern))
vb@191
  1335
        return PEP_ILLEGAL_VALUE;
vb@191
  1336
vb@46
  1337
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1338
}
vb@9
  1339
vb@24
  1340
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1341
{
vb@191
  1342
    assert(session);
vb@191
  1343
    assert(pattern);
vb@191
  1344
vb@191
  1345
    if (!(session && pattern))
vb@191
  1346
        return PEP_ILLEGAL_VALUE;
vb@191
  1347
vb@46
  1348
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1349
}
vb@196
  1350
vb@201
  1351
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1352
        PEP_SESSION session,
vb@201
  1353
        const char *fpr,
vb@201
  1354
        const timestamp *ts
vb@201
  1355
    )
vb@196
  1356
{
vb@196
  1357
    assert(session);
vb@200
  1358
    assert(fpr);
vb@196
  1359
vb@200
  1360
    if (!(session && fpr))
vb@196
  1361
        return PEP_ILLEGAL_VALUE;
vb@196
  1362
vb@201
  1363
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1364
}
vb@196
  1365
vb@211
  1366
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1367
        PEP_SESSION session,
vb@211
  1368
        const char *fpr,
vb@211
  1369
        const char *reason
vb@211
  1370
    )
vb@197
  1371
{
vb@197
  1372
    assert(session);
vb@200
  1373
    assert(fpr);
vb@197
  1374
vb@200
  1375
    if (!(session && fpr))
vb@197
  1376
        return PEP_ILLEGAL_VALUE;
vb@197
  1377
vb@214
  1378
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1379
            reason);
vb@197
  1380
}
vb@197
  1381
vb@214
  1382
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1383
        PEP_SESSION session,
vb@214
  1384
        const char *fpr,
Edouard@701
  1385
        const time_t when,
vb@214
  1386
        bool *expired
vb@214
  1387
    )
vb@214
  1388
{
vb@214
  1389
    assert(session);
vb@214
  1390
    assert(fpr);
vb@214
  1391
    assert(expired);
vb@214
  1392
vb@214
  1393
    if (!(session && fpr && expired))
vb@214
  1394
        return PEP_ILLEGAL_VALUE;
vb@214
  1395
vb@214
  1396
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
Edouard@701
  1397
            when, expired);
vb@214
  1398
}
vb@214
  1399
Edouard@694
  1400
DYNAMIC_API PEP_STATUS key_revoked(
vb@951
  1401
       PEP_SESSION session,
vb@951
  1402
       const char *fpr,
vb@951
  1403
       bool *revoked
vb@951
  1404
   )
Edouard@694
  1405
{
Edouard@694
  1406
    assert(session);
Edouard@694
  1407
    assert(fpr);
Edouard@694
  1408
    assert(revoked);
Edouard@694
  1409
    
Edouard@694
  1410
    if (!(session && fpr && revoked))
Edouard@694
  1411
        return PEP_ILLEGAL_VALUE;
Edouard@694
  1412
    
Edouard@694
  1413
    return session->cryptotech[PEP_crypt_OpenPGP].key_revoked(session, fpr,
vb@951
  1414
            revoked);
Edouard@694
  1415
}
Edouard@694
  1416
vb@451
  1417
static void _clean_log_value(char *text)
vb@451
  1418
{
vb@451
  1419
    if (text) {
vb@451
  1420
        for (char *c = text; *c; c++) {
vb@722
  1421
            if (*c < 32 && *c != '\n')
vb@451
  1422
                *c = 32;
vb@451
  1423
            else if (*c == '"')
vb@451
  1424
                *c = '\'';
vb@451
  1425
        }
vb@451
  1426
    }
vb@451
  1427
}
vb@451
  1428
vb@450
  1429
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1430
{
vb@454
  1431
    str2 = str2 ? str2 : "";
vb@450
  1432
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1433
    size_t len2 = strlen(str2);
vb@450
  1434
    size_t len = len1 + len2 + 3;
vb@450
  1435
    char * result = realloc(str1, len + 1);
vb@450
  1436
vb@450
  1437
    if (result) {
vb@450
  1438
        result[len1] = '"';
vb@450
  1439
        strcpy(result + len1 + 1, str2);
vb@450
  1440
        result[len - 2] = '"';
vb@450
  1441
        result[len - 1] = delim;
vb@450
  1442
        result[len] = 0;
vb@450
  1443
    }
vb@450
  1444
    else {
vb@450
  1445
        free(str1);
vb@450
  1446
    }
vb@450
  1447
vb@450
  1448
    return result;
vb@450
  1449
}
vb@450
  1450
vb@450
  1451
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1452
        PEP_SESSION session,
vb@450
  1453
        int maxlines,
vb@450
  1454
        char **logdata
vb@450
  1455
    )
vb@450
  1456
{
vb@450
  1457
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1458
    char *_logdata= NULL;
vb@450
  1459
vb@450
  1460
    assert(session);
vb@450
  1461
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1462
    assert(logdata);
vb@450
  1463
vb@450
  1464
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1465
            CRASHDUMP_MAX_LINES))
vb@450
  1466
        return PEP_ILLEGAL_VALUE;
vb@450
  1467
vb@632
  1468
    *logdata = NULL;
vb@632
  1469
vb@450
  1470
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1471
    const char *timestamp = NULL;
vb@458
  1472
    const char *title = NULL;
vb@458
  1473
    const char *entity = NULL;
vb@458
  1474
    const char *desc = NULL;
vb@458
  1475
    const char *comment = NULL;
vb@450
  1476
vb@450
  1477
    sqlite3_reset(session->crashdump);
vb@452
  1478
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1479
vb@450
  1480
    int result;
vb@450
  1481
vb@450
  1482
    do {
vb@450
  1483
        result = sqlite3_step(session->crashdump);
vb@450
  1484
        switch (result) {
vb@450
  1485
        case SQLITE_ROW:
vb@951
  1486
            timestamp = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1487
                    0);
vb@951
  1488
            title   = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1489
                    1);
vb@951
  1490
            entity  = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1491
                    2);
vb@951
  1492
            desc    = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1493
                    3);
vb@951
  1494
            comment = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1495
                    4);
vb@452
  1496
vb@452
  1497
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1498
            if (_logdata == NULL)
vb@452
  1499
                goto enomem;
vb@450
  1500
vb@450
  1501
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1502
            if (_logdata == NULL)
vb@450
  1503
                goto enomem;
vb@450
  1504
vb@450
  1505
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1506
            if (_logdata == NULL)
vb@450
  1507
                goto enomem;
vb@450
  1508
vb@450
  1509
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1510
            if (_logdata == NULL)
vb@450
  1511
                goto enomem;
vb@450
  1512
vb@450
  1513
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1514
            if (_logdata == NULL)
vb@450
  1515
                goto enomem;
vb@450
  1516
vb@687
  1517
            _clean_log_value(_logdata);
vb@450
  1518
            break;
vb@450
  1519
vb@450
  1520
        case SQLITE_DONE:
vb@450
  1521
            break;
vb@450
  1522
vb@450
  1523
        default:
vb@450
  1524
            status = PEP_UNKNOWN_ERROR;
vb@450
  1525
            result = SQLITE_DONE;
vb@450
  1526
        }
vb@450
  1527
    } while (result != SQLITE_DONE);
vb@450
  1528
vb@450
  1529
    sqlite3_reset(session->crashdump);
vb@450
  1530
    if (status == PEP_STATUS_OK)
vb@450
  1531
        *logdata = _logdata;
vb@450
  1532
vb@450
  1533
    goto the_end;
vb@450
  1534
vb@450
  1535
enomem:
vb@450
  1536
    status = PEP_OUT_OF_MEMORY;
vb@450
  1537
vb@450
  1538
the_end:
vb@450
  1539
    return status;
vb@450
  1540
}
vb@450
  1541
vb@458
  1542
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  1543
        PEP_SESSION session,
vb@458
  1544
        char **languages
vb@458
  1545
    )
vb@458
  1546
{
vb@458
  1547
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1548
    char *_languages= NULL;
vb@458
  1549
vb@458
  1550
    assert(session);
vb@458
  1551
    assert(languages);
vb@458
  1552
vb@458
  1553
    if (!(session && languages))
vb@458
  1554
        return PEP_ILLEGAL_VALUE;
vb@458
  1555
vb@632
  1556
    *languages = NULL;
vb@632
  1557
vb@458
  1558
    const char *lang = NULL;
vb@458
  1559
    const char *name = NULL;
vb@617
  1560
    const char *phrase = NULL;
vb@458
  1561
vb@458
  1562
    sqlite3_reset(session->languagelist);
vb@458
  1563
vb@458
  1564
    int result;
vb@458
  1565
vb@458
  1566
    do {
vb@458
  1567
        result = sqlite3_step(session->languagelist);
vb@458
  1568
        switch (result) {
vb@458
  1569
        case SQLITE_ROW:
vb@951
  1570
            lang = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  1571
                    0);
vb@951
  1572
            name = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  1573
                    1);
vb@951
  1574
            phrase = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  1575
                    2);
vb@458
  1576
vb@458
  1577
            _languages = _concat_string(_languages, lang, ',');
vb@458
  1578
            if (_languages == NULL)
vb@458
  1579
                goto enomem;
vb@458
  1580
vb@617
  1581
            _languages = _concat_string(_languages, name, ',');
vb@617
  1582
            if (_languages == NULL)
vb@617
  1583
                goto enomem;
vb@617
  1584
vb@617
  1585
            _languages = _concat_string(_languages, phrase, '\n');
vb@458
  1586
            if (_languages == NULL)
vb@458
  1587
                goto enomem;
vb@458
  1588
vb@458
  1589
            break;
vb@458
  1590
vb@458
  1591
        case SQLITE_DONE:
vb@458
  1592
            break;
vb@458
  1593
vb@458
  1594
        default:
vb@458
  1595
            status = PEP_UNKNOWN_ERROR;
vb@458
  1596
            result = SQLITE_DONE;
vb@458
  1597
        }
vb@458
  1598
    } while (result != SQLITE_DONE);
vb@458
  1599
vb@458
  1600
    sqlite3_reset(session->languagelist);
vb@458
  1601
    if (status == PEP_STATUS_OK)
vb@458
  1602
        *languages = _languages;
vb@458
  1603
vb@458
  1604
    goto the_end;
vb@458
  1605
vb@458
  1606
enomem:
vb@458
  1607
    status = PEP_OUT_OF_MEMORY;
vb@458
  1608
vb@458
  1609
the_end:
vb@458
  1610
    return status;
vb@458
  1611
}
vb@458
  1612
vb@458
  1613
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  1614
        PEP_SESSION session,
vb@458
  1615
        const char *lang,
vb@458
  1616
        int phrase_id,
vb@458
  1617
        char **phrase
vb@458
  1618
    )
vb@458
  1619
{
vb@458
  1620
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1621
vb@479
  1622
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@479
  1623
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  1624
        return PEP_ILLEGAL_VALUE;
vb@458
  1625
vb@632
  1626
    *phrase = NULL;
vb@632
  1627
vb@458
  1628
    sqlite3_reset(session->i18n_token);
roker@529
  1629
    sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
roker@529
  1630
    sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  1631
vb@458
  1632
    const char *_phrase = NULL;
vb@458
  1633
    int result;
vb@458
  1634
vb@458
  1635
    result = sqlite3_step(session->i18n_token);
vb@458
  1636
    switch (result) {
vb@458
  1637
    case SQLITE_ROW:
vb@458
  1638
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  1639
        break;
vb@458
  1640
vb@458
  1641
    case SQLITE_DONE:
vb@458
  1642
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  1643
        break;
vb@458
  1644
vb@458
  1645
    default:
vb@458
  1646
        status = PEP_UNKNOWN_ERROR;
vb@458
  1647
    }
vb@458
  1648
vb@458
  1649
    if (status == PEP_STATUS_OK) {
vb@458
  1650
        *phrase = strdup(_phrase);
vb@458
  1651
        if (*phrase == NULL)
vb@458
  1652
            goto enomem;
vb@458
  1653
    }
vb@459
  1654
vb@459
  1655
    sqlite3_reset(session->i18n_token);
vb@458
  1656
    goto the_end;
vb@458
  1657
vb@458
  1658
enomem:
vb@458
  1659
    status = PEP_OUT_OF_MEMORY;
vb@458
  1660
vb@458
  1661
the_end:
vb@458
  1662
    return status;
vb@458
  1663
}
vb@458
  1664
vb@632
  1665
DYNAMIC_API PEP_STATUS sequence_value(
vb@632
  1666
        PEP_SESSION session,
vb@632
  1667
        const char *name,
vb@652
  1668
        int32_t *value
vb@632
  1669
    )
vb@632
  1670
{
vb@632
  1671
    PEP_STATUS status = PEP_STATUS_OK;
vb@632
  1672
    int result;
vb@632
  1673
vb@632
  1674
    assert(session);
vb@632
  1675
    assert(name);
vb@632
  1676
    assert(value);
vb@632
  1677
vb@632
  1678
    if (!(session && name && value))
vb@632
  1679
        return PEP_ILLEGAL_VALUE;
vb@632
  1680
vb@632
  1681
    *value = 0;
vb@632
  1682
vb@633
  1683
    sqlite3_reset(session->sequence_value1);
vb@633
  1684
    sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
vb@633
  1685
    result = sqlite3_step(session->sequence_value1);
vb@633
  1686
    assert(result == SQLITE_DONE);
vb@633
  1687
    sqlite3_reset(session->sequence_value1);
vb@633
  1688
    if (result != SQLITE_DONE) {
vb@633
  1689
        status = PEP_UNKNOWN_ERROR;
vb@633
  1690
    }
vb@633
  1691
    else {
vb@633
  1692
        sqlite3_reset(session->sequence_value2);
vb@951
  1693
        sqlite3_bind_text(session->sequence_value2, 1, name, -1,
vb@951
  1694
                SQLITE_STATIC);
vb@633
  1695
        result = sqlite3_step(session->sequence_value2);
vb@633
  1696
        switch (result) {
vb@633
  1697
            case SQLITE_ROW: {
vb@652
  1698
                int32_t _value = (int32_t)
vb@633
  1699
                        sqlite3_column_int64(session->sequence_value2, 0);
vb@633
  1700
                *value = _value;
vb@633
  1701
                break;
vb@633
  1702
            }
vb@633
  1703
            default:
vb@633
  1704
                status = PEP_UNKNOWN_ERROR;
vb@632
  1705
        }
vb@633
  1706
        sqlite3_reset(session->sequence_value2);
vb@632
  1707
    }
vb@632
  1708
    return status;
vb@632
  1709
}
vb@632
  1710
Edouard@693
  1711
DYNAMIC_API PEP_STATUS set_revoked(
Edouard@693
  1712
       PEP_SESSION session,
Edouard@693
  1713
       const char *revoked_fpr,
Edouard@693
  1714
       const char *replacement_fpr,
Edouard@693
  1715
       const uint64_t revocation_date
Edouard@693
  1716
    )
Edouard@693
  1717
{
Edouard@693
  1718
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  1719
    
Edouard@693
  1720
    assert(session &&
Edouard@693
  1721
           revoked_fpr && revoked_fpr[0] &&
Edouard@693
  1722
           replacement_fpr && replacement_fpr[0]
Edouard@693
  1723
          );
Edouard@693
  1724
    
Edouard@693
  1725
    if (!(session &&
Edouard@693
  1726
          revoked_fpr && revoked_fpr[0] &&
Edouard@693
  1727
          replacement_fpr && replacement_fpr[0]
Edouard@693
  1728
         ))
Edouard@693
  1729
        return PEP_ILLEGAL_VALUE;
Edouard@693
  1730
    
Edouard@693
  1731
    sqlite3_reset(session->set_revoked);
Edouard@693
  1732
    sqlite3_bind_text(session->set_revoked, 1, revoked_fpr, -1, SQLITE_STATIC);
vb@951
  1733
    sqlite3_bind_text(session->set_revoked, 2, replacement_fpr, -1,
vb@951
  1734
            SQLITE_STATIC);
Edouard@693
  1735
    sqlite3_bind_int64(session->set_revoked, 3, revocation_date);
Edouard@693
  1736
Edouard@693
  1737
    int result;
Edouard@693
  1738
    
Edouard@693
  1739
    result = sqlite3_step(session->set_revoked);
Edouard@693
  1740
    switch (result) {
Edouard@693
  1741
        case SQLITE_DONE:
Edouard@693
  1742
            status = PEP_STATUS_OK;
Edouard@693
  1743
            break;
Edouard@693
  1744
            
Edouard@693
  1745
        default:
Edouard@693
  1746
            status = PEP_UNKNOWN_ERROR;
Edouard@693
  1747
    }
Edouard@693
  1748
    
Edouard@693
  1749
    sqlite3_reset(session->set_revoked);
Edouard@693
  1750
    return status;
Edouard@693
  1751
}
Edouard@693
  1752
Edouard@693
  1753
DYNAMIC_API PEP_STATUS get_revoked(
Edouard@693
  1754
        PEP_SESSION session,
Edouard@693
  1755
        const char *fpr,
Edouard@693
  1756
        char **revoked_fpr,
Edouard@693
  1757
        uint64_t *revocation_date
Edouard@693
  1758
    )
Edouard@693
  1759
{
Edouard@693
  1760
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  1761
Edouard@693
  1762
    assert(session &&
Edouard@693
  1763
           revoked_fpr &&
Edouard@693
  1764
           fpr && fpr[0]
Edouard@693
  1765
          );
Edouard@693
  1766
    
Edouard@693
  1767
    if (!(session &&
Edouard@693
  1768
           revoked_fpr &&
Edouard@693
  1769
           fpr && fpr[0]
Edouard@693
  1770
          ))
Edouard@693
  1771
        return PEP_ILLEGAL_VALUE;
Edouard@693
  1772
Edouard@693
  1773
    *revoked_fpr = NULL;
Edouard@693
  1774
    *revocation_date = 0;
Edouard@693
  1775
Edouard@693
  1776
    sqlite3_reset(session->get_revoked);
Edouard@693
  1777
    sqlite3_bind_text(session->get_revoked, 1, fpr, -1, SQLITE_STATIC);
Edouard@693
  1778
Edouard@693
  1779
    int result;
Edouard@693
  1780
    
Edouard@693
  1781
    result = sqlite3_step(session->get_revoked);
Edouard@693
  1782
    switch (result) {
Edouard@693
  1783
        case SQLITE_ROW: {
vb@951
  1784
            *revoked_fpr = strdup((const char *)
vb@951
  1785
                    sqlite3_column_text(session->get_revoked, 0));
Edouard@693
  1786
            if(*revoked_fpr)
vb@951
  1787
                *revocation_date = sqlite3_column_int64(session->get_revoked,
vb@951
  1788
                        1);
Edouard@693
  1789
            else
Edouard@693
  1790
                status = PEP_OUT_OF_MEMORY;
Edouard@693
  1791
Edouard@693
  1792
            break;
Edouard@693
  1793
        }
Edouard@693
  1794
        default:
Edouard@693
  1795
            status = PEP_CANNOT_FIND_IDENTITY;
Edouard@693
  1796
    }
Edouard@693
  1797
Edouard@693
  1798
    sqlite3_reset(session->get_revoked);
Edouard@693
  1799
Edouard@693
  1800
    return status;
Edouard@693
  1801
}
Edouard@693
  1802
vb@958
  1803
PEP_STATUS key_created(
vb@958
  1804
        PEP_SESSION session,
vb@958
  1805
        const char *fpr,
vb@958
  1806
        time_t *created
vb@958
  1807
    )
vb@958
  1808
{
vb@958
  1809
    assert(session && fpr && created);
vb@958
  1810
    if (!(session && fpr && created))
vb@958
  1811
        return PEP_ILLEGAL_VALUE;
vb@958
  1812
vb@958
  1813
    return session->cryptotech[PEP_crypt_OpenPGP].key_created(session, fpr,
vb@958
  1814
            created);
vb@958
  1815
}
vb@958
  1816
vb@482
  1817
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  1818
{
vb@482
  1819
    assert(session);
vb@482
  1820
vb@482
  1821
    if (!session)
vb@482
  1822
        return PEP_ILLEGAL_VALUE;
vb@482
  1823
fdik@494
  1824
    int int_result = sqlite3_exec(
fdik@494
  1825
        session->db,
fdik@494
  1826
        "delete from identity where address like '%@peptest.ch' ;",
fdik@494
  1827
        NULL,
fdik@494
  1828
        NULL,
fdik@494
  1829
        NULL
fdik@494
  1830
    );
fdik@494
  1831
    assert(int_result == SQLITE_OK);
vb@482
  1832
fdik@494
  1833
    return PEP_STATUS_OK;
vb@482
  1834
}
vb@482
  1835