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