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