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