src/pEpEngine.c
author Edouard Tisserant <edouard@pep-project.org>
Tue, 15 Nov 2016 15:56:18 +0100
branchENGINE-140-reloaded
changeset 1394 8659157b681f
parent 1393 ee4bf9e19635
child 1395 c999b8c26dd1
permissions -rw-r--r--
ENGINE-140 #comment Added own_keys table, linking pgp_keypair to identity in case user_id = PEP_OWN_USERID.
Added related set_own_key and own_keys_retieve.
Updated set_identity, set_identity_flags and added unset_identity_flags, all doing bitwise logic to untouched keep flags accross calls.
Removed keys_retrieve_by_flag and PEP_kpf_own_key, now replaced by own_keys table.
Removed duplicated code seting _DDL_USER_VERSION in manageent DB.
Removed exists_empty_fpr_entry update_fprless_identity, and corresponding logic in set_identity()

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