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