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