src/pEpEngine.c
author Edouard Tisserant
Tue, 10 May 2016 11:49:37 +0200
changeset 584 3f175e8668b3
parent 562 fe56206d4630
child 588 b7f954296900
permissions -rw-r--r--
Own keys (SQL and accessors functions)
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@0
     6
vb@221
     7
static int init_count = -1;
vb@62
     8
vb@0
     9
DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
vb@0
    10
{
vb@65
    11
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
    12
    int int_result;
roker@529
    13
    static const char *sql_log;
roker@529
    14
    static const char *sql_trustword;
roker@529
    15
    static const char *sql_get_identity;
roker@529
    16
    static const char *sql_set_person;
roker@529
    17
    static const char *sql_set_pgp_keypair;
roker@529
    18
    static const char *sql_set_identity;
roker@529
    19
    static const char *sql_set_trust;
vb@62
    20
    static const char *sql_get_trust;
vb@251
    21
    static const char *sql_least_trust;
vb@357
    22
    static const char *sql_mark_as_compromized;
vb@450
    23
    static const char *sql_crashdump;
vb@458
    24
    static const char *sql_languagelist;
vb@458
    25
    static const char *sql_i18n_token;
fdik@494
    26
fdik@494
    27
    // blacklist
fdik@494
    28
    static const char *sql_blacklist_add;
fdik@494
    29
    static const char *sql_blacklist_delete;
fdik@494
    30
    static const char *sql_blacklist_is_listed;
fdik@494
    31
    static const char *sql_blacklist_retrieve;
vb@450
    32
Edouard@584
    33
    // Own keys
Edouard@584
    34
    static const char *sql_own_key_add;
Edouard@584
    35
    static const char *sql_own_key_is_listed;
Edouard@584
    36
    static const char *sql_own_key_retrieve;
Edouard@584
    37
vb@62
    38
    bool in_first = false;
vb@8
    39
vb@62
    40
    assert(sqlite3_threadsafe());
vb@62
    41
    if (!sqlite3_threadsafe())
vb@62
    42
        return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
vb@62
    43
vb@62
    44
    // a little race condition - but still a race condition
vb@113
    45
    // mitigated by calling caveat (see documentation)
vb@62
    46
vb@62
    47
    ++init_count;
vb@67
    48
    if (init_count == 0)
vb@62
    49
        in_first = true;
vb@0
    50
roker@529
    51
    assert(session);
vb@191
    52
    if (session == NULL)
vb@191
    53
        return PEP_ILLEGAL_VALUE;
vb@191
    54
roker@529
    55
    *session = NULL;
vb@0
    56
vb@107
    57
    pEpSession *_session = calloc(1, sizeof(pEpSession));
roker@529
    58
    assert(_session);
roker@529
    59
    if (_session == NULL)
roker@529
    60
        goto enomem;
vb@62
    61
roker@529
    62
    _session->version = PEP_ENGINE_VERSION;
vb@0
    63
vb@0
    64
    assert(LOCAL_DB);
vb@0
    65
    if (LOCAL_DB == NULL) {
vb@65
    66
        status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    67
        goto pep_error;
vb@0
    68
    }
vb@0
    69
roker@529
    70
    int_result = sqlite3_open_v2(
roker@529
    71
            LOCAL_DB,
roker@529
    72
            &_session->db,
roker@529
    73
            SQLITE_OPEN_READWRITE
roker@529
    74
                | SQLITE_OPEN_CREATE
roker@529
    75
                | SQLITE_OPEN_FULLMUTEX
roker@529
    76
                | SQLITE_OPEN_PRIVATECACHE,
roker@529
    77
            NULL 
roker@529
    78
        );
vb@0
    79
roker@529
    80
    if (int_result != SQLITE_OK) {
roker@529
    81
        status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    82
        goto pep_error;
roker@529
    83
    }
vb@0
    84
roker@529
    85
    sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
vb@0
    86
vb@0
    87
    assert(SYSTEM_DB);
vb@0
    88
    if (SYSTEM_DB == NULL) {
roker@529
    89
        status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
    90
        goto pep_error;
vb@0
    91
    }
vb@0
    92
roker@529
    93
    int_result = sqlite3_open_v2(
roker@529
    94
            SYSTEM_DB, &_session->system_db,
roker@529
    95
            SQLITE_OPEN_READONLY
roker@529
    96
                | SQLITE_OPEN_FULLMUTEX
roker@529
    97
                | SQLITE_OPEN_SHAREDCACHE,
roker@529
    98
            NULL
roker@529
    99
        );
vb@0
   100
roker@529
   101
    if (int_result != SQLITE_OK) {
roker@529
   102
        status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
   103
        goto pep_error;
roker@529
   104
    }
vb@0
   105
roker@529
   106
    sqlite3_busy_timeout(_session->system_db, 1000);
vb@0
   107
vb@62
   108
    if (in_first) {
vb@62
   109
        int_result = sqlite3_exec(
vb@62
   110
            _session->db,
vb@456
   111
                "create table if not exists version_info (\n"
vb@456
   112
                "   id integer primary key,\n"
vb@456
   113
                "   timestamp integer default (datetime('now')) ,\n"
vb@456
   114
                "   version text,\n"
vb@456
   115
                "   comment text\n"
vb@456
   116
                ");\n"
vb@456
   117
                "create table if not exists log (\n"
vb@456
   118
                "   timestamp integer default (datetime('now')) ,\n"
vb@456
   119
                "   title text not null,\n"
vb@456
   120
                "   entity text not null,\n"
vb@456
   121
                "   description text,\n"
vb@456
   122
                "   comment text\n"
vb@456
   123
                ");\n"
vb@456
   124
                "create index if not exists log_timestamp on log (\n"
vb@456
   125
                "   timestamp\n"
vb@456
   126
                ");\n"
vb@456
   127
                "create table if not exists pgp_keypair (\n"
vb@456
   128
                "   fpr text primary key,\n"
vb@456
   129
                "   public_id text unique,\n"
vb@456
   130
                "   private_id text,\n"
vb@456
   131
                "   created integer,\n"
vb@456
   132
                "   expires integer,\n"
vb@456
   133
                "   comment text\n"
vb@456
   134
                ");\n"
vb@456
   135
                "create index if not exists pgp_keypair_expires on pgp_keypair (\n"
vb@456
   136
                "   expires\n"
vb@456
   137
                ");\n"
vb@456
   138
                "create table if not exists person (\n"
vb@456
   139
                "   id text primary key,\n"
vb@456
   140
                "   username text not null,\n"
vb@456
   141
                "   main_key_id text\n"
vb@456
   142
                "       references pgp_keypair (fpr)\n"
vb@456
   143
                "       on delete set null,\n"
vb@456
   144
                "   lang text,\n"
vb@456
   145
                "   comment text\n"
vb@456
   146
                ");\n"
vb@456
   147
                "create table if not exists identity (\n"
Edouard@559
   148
                "   address text,\n"
vb@456
   149
                "   user_id text\n"
vb@456
   150
                "       references person (id)\n"
vb@456
   151
                "       on delete cascade,\n"
vb@456
   152
                "   main_key_id text\n"
vb@456
   153
                "       references pgp_keypair (fpr)\n"
vb@456
   154
                "       on delete set null,\n"
Edouard@559
   155
                "   comment text,\n"
Edouard@559
   156
                "   primary key (address, user_id)\n"
vb@456
   157
                ");\n"
vb@456
   158
                "create table if not exists trust (\n"
vb@456
   159
                "   user_id text not null\n"
vb@456
   160
                "       references person (id)\n"
vb@456
   161
                "       on delete cascade,\n"
vb@456
   162
                "   pgp_keypair_fpr text not null\n"
vb@456
   163
                "       references pgp_keypair (fpr)\n"
vb@456
   164
                "       on delete cascade,\n"
vb@456
   165
                "   comm_type integer not null,\n"
vb@456
   166
                "   comment text\n"
vb@456
   167
                ");\n"
vb@456
   168
                "create unique index if not exists trust_index on trust (\n"
vb@456
   169
                "   user_id,\n"
vb@456
   170
                "   pgp_keypair_fpr\n"
vb@456
   171
                ");\n"
fdik@494
   172
                // blacklist
vb@456
   173
                "create table if not exists blacklist_keys (\n"
vb@456
   174
                "   fpr text primary key\n"
vb@456
   175
                ");\n"
Edouard@584
   176
                // Own keys
Edouard@584
   177
                "create table if not exists own_keys (\n"
Edouard@584
   178
                "   fpr text primary key\n"
Edouard@584
   179
                "       references pgp_keypair (fpr)\n"
Edouard@584
   180
                "       on delete cascade,\n"
Edouard@584
   181
                ");\n"
vb@456
   182
                ,
vb@62
   183
            NULL,
vb@62
   184
            NULL,
vb@62
   185
            NULL
vb@62
   186
        );
vb@62
   187
        assert(int_result == SQLITE_OK);
vb@0
   188
vb@62
   189
        int_result = sqlite3_exec(
vb@62
   190
            _session->db,
fdik@494
   191
            "insert or replace into version_info (id, version) values (1, '1.1');",
vb@62
   192
            NULL,
vb@62
   193
            NULL,
vb@62
   194
            NULL
vb@62
   195
        );
vb@62
   196
        assert(int_result == SQLITE_OK);
vb@0
   197
vb@62
   198
        sql_log = "insert into log (title, entity, description, comment)"
vb@62
   199
                  "values (?1, ?2, ?3, ?4);";
vb@62
   200
Edouard@584
   201
        sql_get_identity =  "select fpr, username, comm_type, lang"
vb@62
   202
                            "   from identity"
vb@62
   203
                            "   join person on id = identity.user_id"
vb@62
   204
                            "   join pgp_keypair on fpr = identity.main_key_id"
vb@62
   205
                            "   join trust on id = trust.user_id"
vb@62
   206
                            "       and pgp_keypair_fpr = identity.main_key_id"
Edouard@559
   207
                            "   where address = ?1 and identity.user_id = ?2;";
Edouard@559
   208
vb@233
   209
        sql_trustword = "select id, word from wordlist where lang = lower(?1) "
vb@62
   210
                       "and id = ?2 ;";
vb@62
   211
vb@62
   212
        sql_set_person = "insert or replace into person (id, username, lang) "
vb@62
   213
                         "values (?1, ?2, ?3) ;";
vb@62
   214
vb@62
   215
        sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr) "
Edouard@559
   216
                              "values (upper(replace(?1,' ',''))) ;";
vb@62
   217
vb@62
   218
        sql_set_identity = "insert or replace into identity (address, main_key_id, "
Edouard@559
   219
                           "user_id) values (?1, upper(replace(?2,' ','')), ?3) ;";
vb@62
   220
vb@62
   221
        sql_set_trust = "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
Edouard@559
   222
                        "values (?1, upper(replace(?2,' ','')), ?3) ;";
vb@62
   223
Edouard@559
   224
        sql_get_trust = "select comm_type from trust where user_id = ?1 "
Edouard@559
   225
                        "and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
vb@251
   226
Edouard@559
   227
        sql_least_trust = "select min(comm_type) from trust where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
vb@357
   228
vb@457
   229
        sql_mark_as_compromized = "update trust not indexed set comm_type = 15"
Edouard@559
   230
                                  " where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
vb@450
   231
vb@457
   232
        sql_crashdump = "select timestamp, title, entity, description, comment"
vb@457
   233
                        " from log order by timestamp desc limit ?1 ;";
vb@458
   234
vb@458
   235
        sql_languagelist = "select lang, name from i18n_language order by lang ;";
vb@458
   236
vb@461
   237
        sql_i18n_token = "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
vb@482
   238
fdik@494
   239
        // blacklist
fdik@494
   240
Edouard@559
   241
        sql_blacklist_add = "insert or replace into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
Edouard@559
   242
                            "delete from identity where main_key_id = upper(replace(?1,' ','')) ;"
Edouard@559
   243
                            "delete from pgp_keypair where fpr = upper(replace(?1,' ','')) ;";
vb@514
   244
Edouard@559
   245
        sql_blacklist_delete = "delete from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
vb@514
   246
Edouard@559
   247
        sql_blacklist_is_listed = "select count(*) from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
vb@514
   248
fdik@494
   249
        sql_blacklist_retrieve = "select * from blacklist_keys ;";
Edouard@584
   250
        
Edouard@584
   251
        // Own keys
Edouard@584
   252
        
Edouard@584
   253
        sql_own_key_add = "insert or replace into own_keys (fpr) "
Edouard@584
   254
                          "    values (upper(replace(?1,' ',''))) ;";
Edouard@584
   255
        
Edouard@584
   256
        sql_own_key_is_listed = "select count(*) from own_keys where fpr = upper(replace(?1,' ','')) ;";
Edouard@584
   257
Edouard@584
   258
        sql_own_key_retrieve = "select * from own_keys ;";
Edouard@584
   259
        
vb@62
   260
    }
vb@62
   261
Edouard@417
   262
    int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
vb@0
   263
            &_session->log, NULL);
roker@529
   264
    assert(int_result == SQLITE_OK);
vb@0
   265
vb@233
   266
    int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
Edouard@417
   267
            (int)strlen(sql_trustword), &_session->trustword, NULL);
roker@529
   268
    assert(int_result == SQLITE_OK);
vb@0
   269
vb@0
   270
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
Edouard@417
   271
            (int)strlen(sql_get_identity), &_session->get_identity, NULL);
roker@529
   272
    assert(int_result == SQLITE_OK);
vb@0
   273
vb@0
   274
    int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
Edouard@417
   275
            (int)strlen(sql_set_person), &_session->set_person, NULL);
vb@0
   276
    assert(int_result == SQLITE_OK);
vb@62
   277
vb@0
   278
    int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
Edouard@417
   279
            (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair, NULL);
roker@529
   280
    assert(int_result == SQLITE_OK);
vb@62
   281
vb@0
   282
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity,
Edouard@417
   283
            (int)strlen(sql_set_identity), &_session->set_identity, NULL);
roker@529
   284
    assert(int_result == SQLITE_OK);
vb@62
   285
vb@0
   286
    int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
Edouard@417
   287
            (int)strlen(sql_set_trust), &_session->set_trust, NULL);
roker@529
   288
    assert(int_result == SQLITE_OK);
vb@62
   289
vb@8
   290
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
Edouard@417
   291
            (int)strlen(sql_get_trust), &_session->get_trust, NULL);
vb@8
   292
    assert(int_result == SQLITE_OK);
vb@0
   293
vb@251
   294
    int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
Edouard@417
   295
            (int)strlen(sql_least_trust), &_session->least_trust, NULL);
vb@251
   296
    assert(int_result == SQLITE_OK);
vb@251
   297
vb@357
   298
    int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromized,
Edouard@417
   299
            (int)strlen(sql_mark_as_compromized), &_session->mark_compromized, NULL);
vb@357
   300
    assert(int_result == SQLITE_OK);
vb@357
   301
vb@453
   302
    int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
vb@453
   303
            (int)strlen(sql_crashdump), &_session->crashdump, NULL);
vb@453
   304
    assert(int_result == SQLITE_OK);
vb@453
   305
vb@458
   306
    int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
vb@458
   307
            (int)strlen(sql_languagelist), &_session->languagelist, NULL);
roker@529
   308
    assert(int_result == SQLITE_OK);
vb@458
   309
vb@458
   310
    int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
vb@458
   311
            (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
roker@529
   312
    assert(int_result == SQLITE_OK);
vb@458
   313
fdik@494
   314
    // blacklist
fdik@494
   315
fdik@494
   316
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
fdik@494
   317
            (int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
fdik@494
   318
    assert(int_result == SQLITE_OK);
fdik@494
   319
fdik@494
   320
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
fdik@494
   321
            (int)strlen(sql_blacklist_delete), &_session->blacklist_delete, NULL);
fdik@494
   322
    assert(int_result == SQLITE_OK);
fdik@494
   323
fdik@494
   324
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
fdik@494
   325
            (int)strlen(sql_blacklist_is_listed), &_session->blacklist_is_listed, NULL);
fdik@494
   326
    assert(int_result == SQLITE_OK);
fdik@494
   327
fdik@494
   328
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
fdik@494
   329
            (int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve, NULL);
vb@482
   330
    assert(int_result == SQLITE_OK);
vb@482
   331
Edouard@584
   332
    // Own keys
Edouard@584
   333
    
Edouard@584
   334
    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_add,
Edouard@584
   335
                                    (int)strlen(sql_own_key_add), &_session->own_key_add, NULL);
Edouard@584
   336
    assert(int_result == SQLITE_OK);
Edouard@584
   337
    
Edouard@584
   338
    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
Edouard@584
   339
                                    (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed, NULL);
Edouard@584
   340
    assert(int_result == SQLITE_OK);
Edouard@584
   341
    
Edouard@584
   342
    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_retrieve,
Edouard@584
   343
                                    (int)strlen(sql_own_key_retrieve), &_session->own_key_retrieve, NULL);
Edouard@584
   344
    assert(int_result == SQLITE_OK);
Edouard@584
   345
    
vb@65
   346
    status = init_cryptotech(_session, in_first);
vb@65
   347
    if (status != PEP_STATUS_OK)
vb@65
   348
        goto pep_error;
vb@0
   349
vb@65
   350
    status = init_transport_system(_session, in_first);
vb@65
   351
    if (status != PEP_STATUS_OK)
vb@65
   352
        goto pep_error;
vb@62
   353
vb@65
   354
    status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
vb@65
   355
    if (status != PEP_STATUS_OK)
vb@65
   356
        goto pep_error;
vb@65
   357
vb@464
   358
    // runtime config
vb@464
   359
vb@464
   360
    _session->passive_mode = false;
vb@464
   361
    _session->unencrypted_subject = false;
vb@464
   362
roker@529
   363
    *session = _session;
roker@529
   364
    return PEP_STATUS_OK;
vb@65
   365
vb@65
   366
enomem:
vb@65
   367
    status = PEP_OUT_OF_MEMORY;
vb@65
   368
vb@65
   369
pep_error:
vb@65
   370
    release(_session);
vb@65
   371
    return status;
vb@0
   372
}
vb@0
   373
vb@0
   374
DYNAMIC_API void release(PEP_SESSION session)
vb@0
   375
{
vb@62
   376
    bool out_last = false;
vb@62
   377
vb@62
   378
    assert(init_count >= 0);
roker@529
   379
    assert(session);
vb@0
   380
Edouard@385
   381
    if (!((init_count >= 0) && session))
vb@191
   382
        return;
vb@191
   383
vb@62
   384
    // a small race condition but still a race condition
vb@113
   385
    // mitigated by calling caveat (see documentation)
vb@62
   386
vb@62
   387
    if (init_count == 0)
vb@62
   388
        out_last = true;
vb@62
   389
    --init_count;
vb@62
   390
roker@529
   391
    if (session) {
roker@529
   392
        if (session->db) {
vb@517
   393
            if (session->log)
vb@517
   394
                sqlite3_finalize(session->log);
vb@233
   395
            if (session->trustword)
vb@233
   396
                sqlite3_finalize(session->trustword);
vb@65
   397
            if (session->get_identity)
vb@65
   398
                sqlite3_finalize(session->get_identity);
vb@65
   399
            if (session->set_person)
vb@65
   400
                sqlite3_finalize(session->set_person);
vb@65
   401
            if (session->set_pgp_keypair)
vb@65
   402
                sqlite3_finalize(session->set_pgp_keypair);
vb@517
   403
            if (session->set_identity)
vb@517
   404
                sqlite3_finalize(session->set_identity);
vb@65
   405
            if (session->set_trust)
vb@65
   406
                sqlite3_finalize(session->set_trust);
vb@65
   407
            if (session->get_trust)
vb@65
   408
                sqlite3_finalize(session->get_trust);
vb@251
   409
            if (session->least_trust)
vb@251
   410
                sqlite3_finalize(session->least_trust);
vb@517
   411
            if (session->mark_compromized)
vb@517
   412
                sqlite3_finalize(session->mark_compromized);
vb@517
   413
            if (session->crashdump)
vb@517
   414
                sqlite3_finalize(session->crashdump);
vb@517
   415
            if (session->languagelist)
vb@517
   416
                sqlite3_finalize(session->languagelist);
vb@517
   417
            if (session->i18n_token)
vb@517
   418
                sqlite3_finalize(session->i18n_token);
vb@517
   419
            if (session->blacklist_add)
vb@517
   420
                sqlite3_finalize(session->blacklist_add);
vb@517
   421
            if (session->blacklist_delete)
vb@517
   422
                sqlite3_finalize(session->blacklist_delete);
vb@517
   423
            if (session->blacklist_is_listed)
vb@517
   424
                sqlite3_finalize(session->blacklist_is_listed);
vb@517
   425
            if (session->blacklist_retrieve)
vb@517
   426
                sqlite3_finalize(session->blacklist_retrieve);
vb@26
   427
vb@65
   428
            if (session->db)
vb@65
   429
                sqlite3_close_v2(session->db);
vb@65
   430
            if (session->system_db)
vb@65
   431
                sqlite3_close_v2(session->system_db);
roker@529
   432
        }
vb@65
   433
vb@65
   434
        release_transport_system(session, out_last);
vb@65
   435
        release_cryptotech(session, out_last);
vb@65
   436
vb@65
   437
        free(session);
vb@62
   438
    }
vb@0
   439
}
vb@0
   440
vb@467
   441
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
   442
{
vb@464
   443
    assert(session);
vb@467
   444
    session->passive_mode = enable;
vb@464
   445
}
vb@464
   446
vb@467
   447
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
   448
{
vb@464
   449
    assert(session);
vb@467
   450
    session->unencrypted_subject = enable;
vb@464
   451
}
vb@464
   452
vb@0
   453
DYNAMIC_API PEP_STATUS log_event(
vb@450
   454
        PEP_SESSION session,
vb@451
   455
        const char *title,
vb@451
   456
        const char *entity,
vb@451
   457
        const char *description,
vb@451
   458
        const char *comment
vb@0
   459
    )
vb@0
   460
{
roker@529
   461
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   462
    int result;
vb@0
   463
roker@529
   464
    assert(session);
roker@529
   465
    assert(title);
roker@529
   466
    assert(entity);
vb@0
   467
vb@191
   468
    if (!(session && title && entity))
vb@191
   469
        return PEP_ILLEGAL_VALUE;
vb@191
   470
roker@529
   471
    sqlite3_reset(session->log);
roker@529
   472
    sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
roker@529
   473
    sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
roker@529
   474
    if (description)
vb@46
   475
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
roker@529
   476
    else
roker@529
   477
        sqlite3_bind_null(session->log, 3);
roker@529
   478
    if (comment)
roker@529
   479
        sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
roker@529
   480
    else
roker@529
   481
        sqlite3_bind_null(session->log, 4);
roker@529
   482
    do {
roker@529
   483
        result = sqlite3_step(session->log);
roker@529
   484
        assert(result == SQLITE_DONE || result == SQLITE_BUSY);
roker@529
   485
        if (result != SQLITE_DONE && result != SQLITE_BUSY)
roker@529
   486
            status = PEP_UNKNOWN_ERROR;
roker@529
   487
    } while (result == SQLITE_BUSY);
roker@529
   488
    sqlite3_reset(session->log);
vb@0
   489
roker@529
   490
    return status;
vb@0
   491
}
vb@0
   492
vb@233
   493
DYNAMIC_API PEP_STATUS trustword(
vb@0
   494
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   495
            char **word, size_t *wsize
vb@0
   496
        )
vb@0
   497
{
roker@529
   498
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   499
    int result;
vb@0
   500
roker@529
   501
    assert(session);
roker@529
   502
    assert(word);
roker@529
   503
    assert(wsize);
vb@0
   504
vb@191
   505
    if (!(session && word && wsize))
vb@191
   506
        return PEP_ILLEGAL_VALUE;
vb@191
   507
roker@529
   508
    *word = NULL;
roker@529
   509
    *wsize = 0;
vb@0
   510
roker@529
   511
    if (lang == NULL)
roker@529
   512
        lang = "en";
vb@0
   513
roker@529
   514
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   515
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   516
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   517
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   518
    assert(lang[2] == 0);
vb@0
   519
roker@529
   520
    sqlite3_reset(session->trustword);
vb@233
   521
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
roker@529
   522
    sqlite3_bind_int(session->trustword, 2, value);
vb@0
   523
roker@529
   524
    result = sqlite3_step(session->trustword);
roker@529
   525
    if (result == SQLITE_ROW) {
vb@233
   526
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
   527
                    1));
roker@529
   528
        if (*word)
vb@233
   529
            *wsize = sqlite3_column_bytes(session->trustword, 1);
roker@529
   530
        else
roker@529
   531
            status = PEP_TRUSTWORD_NOT_FOUND;
roker@529
   532
    } else
roker@529
   533
        status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   534
roker@529
   535
    sqlite3_reset(session->trustword);
roker@529
   536
    return status;
vb@0
   537
}
vb@0
   538
vb@233
   539
DYNAMIC_API PEP_STATUS trustwords(
vb@0
   540
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   541
        char **words, size_t *wsize, int max_words
vb@0
   542
    )
vb@0
   543
{
roker@529
   544
    const char *source = fingerprint;
roker@529
   545
    char *buffer;
roker@529
   546
    char *dest;
roker@529
   547
    size_t fsize;
vb@0
   548
    PEP_STATUS _status;
vb@0
   549
roker@529
   550
    assert(session);
roker@529
   551
    assert(fingerprint);
roker@529
   552
    assert(words);
roker@529
   553
    assert(wsize);
roker@529
   554
    assert(max_words >= 0);
vb@0
   555
vb@191
   556
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
   557
        return PEP_ILLEGAL_VALUE;
vb@191
   558
roker@529
   559
    *words = NULL;
roker@529
   560
    *wsize = 0;
vb@0
   561
vb@251
   562
    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
   563
    assert(buffer);
vb@0
   564
    if (buffer == NULL)
vb@0
   565
        return PEP_OUT_OF_MEMORY;
vb@125
   566
    dest = buffer;
vb@0
   567
roker@529
   568
    fsize = strlen(fingerprint);
vb@0
   569
roker@529
   570
    if (!lang || !lang[0])
roker@529
   571
        lang = "en";
vb@0
   572
roker@529
   573
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   574
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   575
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   576
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   577
    assert(lang[2] == 0);
vb@0
   578
roker@529
   579
    int n_words = 0;
roker@529
   580
    while (source < fingerprint + fsize) {
roker@529
   581
        uint16_t value;
roker@529
   582
        char *word;
roker@529
   583
        size_t _wsize;
roker@529
   584
        int j;
vb@0
   585
vb@0
   586
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
roker@529
   587
            if (*source >= 'a' && *source <= 'f')
roker@529
   588
                value += (*source - 'a' + 10) << (3 - j++) * 4;
roker@529
   589
            else if (*source >= 'A' && *source <= 'F')
roker@529
   590
                value += (*source - 'A' + 10) << (3 - j++) * 4;
roker@529
   591
            else if (*source >= '0' && *source <= '9')
roker@529
   592
                value += (*source - '0') << (3 - j++) * 4;
roker@529
   593
            
roker@529
   594
            source++;
roker@529
   595
        }
vb@0
   596
roker@529
   597
        _status = trustword(session, value, lang, &word, &_wsize);
vb@0
   598
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   599
            free(buffer);
vb@0
   600
            return PEP_OUT_OF_MEMORY;
vb@0
   601
        }
roker@529
   602
        if (word == NULL) {
vb@0
   603
            free(buffer);
roker@529
   604
            return PEP_TRUSTWORD_NOT_FOUND;
vb@0
   605
        }
vb@0
   606
roker@529
   607
        if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
roker@529
   608
            strncpy(dest, word, _wsize);
vb@0
   609
            free(word);
roker@529
   610
            dest += _wsize;
roker@529
   611
        }
roker@529
   612
        else {
vb@0
   613
            free(word);
roker@529
   614
            break; // buffer full
vb@0
   615
        }
vb@0
   616
roker@529
   617
        if (source < fingerprint + fsize
vb@251
   618
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
roker@529
   619
            *dest++ = ' ';
vb@0
   620
roker@529
   621
        ++n_words;
roker@529
   622
        if (max_words && n_words >= max_words)
roker@529
   623
            break;
roker@529
   624
    }
vb@0
   625
roker@529
   626
    *words = buffer;
roker@529
   627
    *wsize = dest - buffer;
roker@529
   628
    return PEP_STATUS_OK;
vb@0
   629
}
vb@0
   630
vb@0
   631
pEp_identity *new_identity(
vb@0
   632
        const char *address, const char *fpr, const char *user_id,
vb@0
   633
        const char *username
vb@0
   634
    )
vb@0
   635
{
vb@0
   636
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   637
    assert(result);
vb@0
   638
    if (result) {
vb@0
   639
        if (address) {
vb@0
   640
            result->address = strdup(address);
vb@0
   641
            assert(result->address);
vb@0
   642
            if (result->address == NULL) {
vb@0
   643
                free(result);
vb@0
   644
                return NULL;
vb@0
   645
            }
vb@0
   646
            result->address_size = strlen(address);
vb@0
   647
        }
vb@0
   648
        if (fpr) {
vb@0
   649
            result->fpr = strdup(fpr);
vb@0
   650
            assert(result->fpr);
vb@0
   651
            if (result->fpr == NULL) {
vb@0
   652
                free_identity(result);
vb@0
   653
                return NULL;
vb@0
   654
            }
vb@0
   655
            result->fpr_size = strlen(fpr);
vb@0
   656
        }
vb@0
   657
        if (user_id) {
vb@0
   658
            result->user_id = strdup(user_id);
vb@0
   659
            assert(result->user_id);
vb@0
   660
            if (result->user_id == NULL) {
vb@0
   661
                free_identity(result);
vb@0
   662
                return NULL;
vb@0
   663
            }
vb@0
   664
            result->user_id_size = strlen(user_id);
vb@0
   665
        }
vb@0
   666
        if (username) {
vb@0
   667
            result->username = strdup(username);
vb@0
   668
            assert(result->username);
vb@0
   669
            if (result->username == NULL) {
vb@0
   670
                free_identity(result);
vb@0
   671
                return NULL;
vb@0
   672
            }
vb@0
   673
            result->username_size = strlen(username);
vb@0
   674
        }
vb@0
   675
        result->struct_size = sizeof(pEp_identity);
vb@0
   676
    }
vb@0
   677
    return result;
vb@0
   678
}
vb@0
   679
vb@37
   680
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   681
{
vb@37
   682
    assert(src);
vb@37
   683
vb@37
   684
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
vb@37
   685
    assert(dup);
vb@37
   686
    if (dup == NULL)
vb@37
   687
        return NULL;
vb@37
   688
    
vb@37
   689
    dup->comm_type = src->comm_type;
vb@37
   690
    dup->lang[0] = src->lang[0];
vb@37
   691
    dup->lang[1] = src->lang[1];
vb@37
   692
    dup->lang[2] = 0;
vb@37
   693
    dup->me = src->me;
vb@37
   694
vb@37
   695
    return dup;
vb@37
   696
}
vb@37
   697
vb@0
   698
void free_identity(pEp_identity *identity)
vb@0
   699
{
vb@0
   700
    if (identity) {
vb@0
   701
        free(identity->address);
vb@0
   702
        free(identity->fpr);
vb@0
   703
        free(identity->user_id);
vb@0
   704
        free(identity->username);
vb@0
   705
        free(identity);
vb@0
   706
    }
vb@0
   707
}
vb@0
   708
vb@0
   709
DYNAMIC_API PEP_STATUS get_identity(
Edouard@559
   710
        PEP_SESSION session,
Edouard@559
   711
        const char *address,
Edouard@559
   712
        const char *user_id,
vb@0
   713
        pEp_identity **identity
vb@0
   714
    )
vb@0
   715
{
roker@529
   716
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   717
    static pEp_identity *_identity;
roker@529
   718
    int result;
roker@529
   719
    const char *_lang;
vb@0
   720
roker@529
   721
    assert(session);
roker@529
   722
    assert(address);
vb@8
   723
    assert(address[0]);
vb@0
   724
vb@191
   725
    if (!(session && address && address[0]))
vb@191
   726
        return PEP_ILLEGAL_VALUE;
vb@191
   727
vb@46
   728
    sqlite3_reset(session->get_identity);
vb@46
   729
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
Edouard@559
   730
    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
vb@0
   731
vb@46
   732
    result = sqlite3_step(session->get_identity);
roker@529
   733
    switch (result) {
roker@529
   734
    case SQLITE_ROW:
vb@0
   735
        _identity = new_identity(
vb@0
   736
                address,
vb@46
   737
                (const char *) sqlite3_column_text(session->get_identity, 0),
Edouard@559
   738
                user_id,
Edouard@559
   739
                (const char *) sqlite3_column_text(session->get_identity, 1)
vb@0
   740
                );
vb@0
   741
        assert(_identity);
vb@0
   742
        if (_identity == NULL)
vb@0
   743
            return PEP_OUT_OF_MEMORY;
vb@0
   744
Edouard@559
   745
        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 2);
Edouard@559
   746
        _lang = (const char *) sqlite3_column_text(session->get_identity, 3);
vb@0
   747
        if (_lang && _lang[0]) {
roker@529
   748
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
roker@529
   749
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
roker@529
   750
            assert(_lang[2] == 0);
roker@529
   751
            _identity->lang[0] = _lang[0];
roker@529
   752
            _identity->lang[1] = _lang[1];
vb@0
   753
            _identity->lang[2] = 0;
roker@529
   754
        }
roker@529
   755
        *identity = _identity;
roker@529
   756
        break;
roker@529
   757
    default:
vb@0
   758
        status = PEP_CANNOT_FIND_IDENTITY;
roker@529
   759
        *identity = NULL;
roker@529
   760
    }
vb@0
   761
vb@46
   762
    sqlite3_reset(session->get_identity);
roker@529
   763
    return status;
vb@0
   764
}
vb@0
   765
vb@0
   766
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   767
        PEP_SESSION session, const pEp_identity *identity
vb@0
   768
    )
vb@0
   769
{
roker@529
   770
    int result;
vb@0
   771
roker@529
   772
    assert(session);
roker@529
   773
    assert(identity);
roker@529
   774
    assert(identity->address);
roker@529
   775
    assert(identity->fpr);
roker@529
   776
    assert(identity->user_id);
roker@529
   777
    assert(identity->username);
vb@0
   778
vb@191
   779
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   780
                identity->user_id && identity->username))
vb@191
   781
        return PEP_ILLEGAL_VALUE;
vb@191
   782
vb@515
   783
    bool listed;
vb@515
   784
    PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
vb@515
   785
    assert(status == PEP_STATUS_OK);
vb@515
   786
    if (status != PEP_STATUS_OK)
vb@515
   787
        return status;
vb@515
   788
vb@515
   789
    if (listed)
vb@515
   790
        return PEP_KEY_BLACKLISTED;
vb@515
   791
roker@529
   792
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   793
roker@529
   794
    sqlite3_reset(session->set_person);
vb@46
   795
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
   796
            SQLITE_STATIC);
vb@46
   797
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
   798
            SQLITE_STATIC);
roker@529
   799
    if (identity->lang[0])
vb@46
   800
        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
vb@0
   801
                SQLITE_STATIC);
roker@529
   802
    else
roker@529
   803
        sqlite3_bind_null(session->set_person, 3);
roker@529
   804
    result = sqlite3_step(session->set_person);
roker@529
   805
    sqlite3_reset(session->set_person);
roker@529
   806
    if (result != SQLITE_DONE) {
roker@529
   807
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   808
        return PEP_CANNOT_SET_PERSON;
roker@529
   809
    }
vb@0
   810
roker@529
   811
    sqlite3_reset(session->set_pgp_keypair);
vb@46
   812
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   813
            SQLITE_STATIC);
roker@529
   814
    result = sqlite3_step(session->set_pgp_keypair);
roker@529
   815
    sqlite3_reset(session->set_pgp_keypair);
roker@529
   816
    if (result != SQLITE_DONE) {
roker@529
   817
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   818
        return PEP_CANNOT_SET_PGP_KEYPAIR;
roker@529
   819
    }
vb@0
   820
roker@529
   821
    sqlite3_reset(session->set_identity);
vb@46
   822
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
   823
            SQLITE_STATIC);
vb@46
   824
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
   825
            SQLITE_STATIC);
vb@46
   826
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
   827
            SQLITE_STATIC);
roker@529
   828
    result = sqlite3_step(session->set_identity);
roker@529
   829
    sqlite3_reset(session->set_identity);
roker@529
   830
    if (result != SQLITE_DONE) {
roker@529
   831
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   832
        return PEP_CANNOT_SET_IDENTITY;
roker@529
   833
    }
vb@0
   834
roker@529
   835
    sqlite3_reset(session->set_trust);
vb@46
   836
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
   837
            SQLITE_STATIC);
vb@46
   838
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
   839
            SQLITE_STATIC);
roker@529
   840
    sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
roker@529
   841
    result = sqlite3_step(session->set_trust);
roker@529
   842
    sqlite3_reset(session->set_trust);
roker@529
   843
    if (result != SQLITE_DONE) {
roker@529
   844
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
Edouard@584
   845
        return PEP_CANNOT_SET_TRUST;
roker@529
   846
    }
vb@0
   847
vb@46
   848
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
roker@529
   849
    if (result == SQLITE_OK)
roker@529
   850
        return PEP_STATUS_OK;
roker@529
   851
    else
roker@529
   852
        return PEP_COMMIT_FAILED;
vb@0
   853
}
vb@0
   854
vb@357
   855
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
   856
        PEP_SESSION session,
vb@357
   857
        const char *fpr
vb@357
   858
    )
vb@357
   859
{
roker@529
   860
    int result;
vb@357
   861
roker@529
   862
    assert(session);
vb@357
   863
    assert(fpr && fpr[0]);
vb@357
   864
vb@357
   865
    if (!(session && fpr && fpr[0]))
vb@357
   866
        return PEP_ILLEGAL_VALUE;
vb@357
   867
roker@529
   868
    sqlite3_reset(session->mark_compromized);
vb@357
   869
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
   870
            SQLITE_STATIC);
vb@357
   871
    result = sqlite3_step(session->mark_compromized);
roker@529
   872
    sqlite3_reset(session->mark_compromized);
vb@357
   873
vb@357
   874
    if (result != SQLITE_DONE)
Edouard@584
   875
        return PEP_CANNOT_SET_TRUST;
vb@357
   876
vb@357
   877
    return PEP_STATUS_OK;
vb@357
   878
}
vb@357
   879
vb@0
   880
void pEp_free(void *p)
vb@0
   881
{
vb@0
   882
    free(p);
vb@0
   883
}
vb@0
   884
vb@8
   885
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
   886
{
vb@8
   887
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
   888
    int result;
vb@8
   889
vb@8
   890
    assert(session);
vb@8
   891
    assert(identity);
vb@8
   892
    assert(identity->user_id);
vb@8
   893
    assert(identity->user_id[0]);
vb@8
   894
    assert(identity->fpr);
vb@8
   895
    assert(identity->fpr[0]);
vb@8
   896
vb@191
   897
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
   898
                identity->fpr && identity->fpr[0]))
vb@191
   899
        return PEP_ILLEGAL_VALUE;
vb@191
   900
vb@8
   901
    identity->comm_type = PEP_ct_unknown;
vb@8
   902
vb@46
   903
    sqlite3_reset(session->get_trust);
vb@46
   904
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
vb@46
   905
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
   906
vb@46
   907
    result = sqlite3_step(session->get_trust);
vb@8
   908
    switch (result) {
vb@8
   909
    case SQLITE_ROW: {
Edouard@559
   910
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 0);
vb@8
   911
        identity->comm_type = comm_type;
vb@8
   912
        break;
vb@8
   913
    }
vb@8
   914
 
vb@8
   915
    default:
vb@8
   916
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
   917
    }
vb@8
   918
vb@46
   919
    sqlite3_reset(session->get_trust);
vb@8
   920
    return status;
vb@9
   921
}
vb@9
   922
vb@251
   923
DYNAMIC_API PEP_STATUS least_trust(
vb@251
   924
        PEP_SESSION session,
vb@251
   925
        const char *fpr,
vb@251
   926
        PEP_comm_type *comm_type
vb@251
   927
    )
vb@251
   928
{
vb@251
   929
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
   930
    int result;
vb@251
   931
vb@251
   932
    assert(session);
vb@251
   933
    assert(fpr);
vb@251
   934
    assert(comm_type);
vb@251
   935
vb@259
   936
    *comm_type = PEP_ct_unknown;
vb@259
   937
vb@251
   938
    if (!(session && fpr && comm_type))
vb@251
   939
        return PEP_ILLEGAL_VALUE;
vb@251
   940
vb@251
   941
    sqlite3_reset(session->least_trust);
vb@251
   942
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
   943
vb@251
   944
    result = sqlite3_step(session->least_trust);
vb@251
   945
    switch (result) {
vb@251
   946
        case SQLITE_ROW: {
vb@259
   947
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
   948
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
   949
            break;
vb@251
   950
        }
vb@251
   951
        default:
vb@251
   952
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
   953
    }
vb@251
   954
vb@251
   955
    sqlite3_reset(session->least_trust);
vb@251
   956
    return status;
vb@251
   957
}
vb@251
   958
vb@24
   959
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
   960
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
   961
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   962
    )
vb@24
   963
{
vb@191
   964
    assert(session);
vb@191
   965
    assert(ctext);
vb@191
   966
    assert(csize);
vb@191
   967
    assert(ptext);
vb@191
   968
    assert(psize);
vb@191
   969
    assert(keylist);
vb@191
   970
vb@191
   971
    if (!(session && ctext && csize && ptext && psize && keylist && keylist))
vb@191
   972
        return PEP_ILLEGAL_VALUE;
vb@191
   973
vb@46
   974
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
vb@24
   975
}
vb@24
   976
vb@24
   977
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
   978
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
   979
    size_t psize, char **ctext, size_t *csize
vb@24
   980
    )
vb@24
   981
{
vb@191
   982
    assert(session);
vb@191
   983
    assert(keylist);
vb@191
   984
    assert(ptext);
vb@191
   985
    assert(psize);
vb@191
   986
    assert(ctext);
vb@191
   987
    assert(csize);
vb@191
   988
vb@191
   989
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
   990
        return PEP_ILLEGAL_VALUE;
vb@191
   991
vb@46
   992
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
vb@24
   993
}
vb@24
   994
vb@24
   995
DYNAMIC_API PEP_STATUS verify_text(
vb@24
   996
    PEP_SESSION session, const char *text, size_t size,
vb@24
   997
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   998
    )
vb@24
   999
{
vb@191
  1000
    assert(session);
vb@191
  1001
    assert(text);
vb@191
  1002
    assert(size);
vb@191
  1003
    assert(signature);
vb@191
  1004
    assert(sig_size);
vb@191
  1005
    assert(keylist);
vb@191
  1006
vb@191
  1007
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
  1008
        return PEP_ILLEGAL_VALUE;
vb@191
  1009
vb@46
  1010
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
vb@24
  1011
}
vb@24
  1012
vb@24
  1013
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1014
{
vb@191
  1015
    assert(session);
vb@191
  1016
    assert(fpr);
vb@191
  1017
vb@191
  1018
    if (!(session && fpr))
vb@191
  1019
        return PEP_ILLEGAL_VALUE;
vb@191
  1020
vb@46
  1021
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
  1022
}
vb@24
  1023
vb@24
  1024
DYNAMIC_API PEP_STATUS export_key(
vb@46
  1025
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1026
    )
vb@24
  1027
{
vb@191
  1028
    assert(session);
vb@191
  1029
    assert(fpr);
vb@191
  1030
    assert(key_data);
vb@191
  1031
    assert(size);
vb@191
  1032
vb@191
  1033
    if (!(session && fpr && key_data && size))
vb@191
  1034
        return PEP_ILLEGAL_VALUE;
vb@191
  1035
vb@46
  1036
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
vb@24
  1037
}
vb@24
  1038
vb@24
  1039
DYNAMIC_API PEP_STATUS find_keys(
vb@46
  1040
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1041
    )
vb@24
  1042
{
vb@191
  1043
    assert(session);
vb@191
  1044
    assert(pattern);
vb@191
  1045
    assert(keylist);
vb@191
  1046
vb@191
  1047
    if (!(session && pattern && keylist))
vb@191
  1048
        return PEP_ILLEGAL_VALUE;
vb@191
  1049
vb@46
  1050
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
vb@24
  1051
}
vb@24
  1052
vb@24
  1053
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
  1054
        PEP_SESSION session, pEp_identity *identity
vb@24
  1055
    )
vb@24
  1056
{
vb@191
  1057
    assert(session);
vb@191
  1058
    assert(identity);
vb@191
  1059
    assert(identity->address);
vb@298
  1060
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
  1061
    assert(identity->username);
vb@191
  1062
vb@298
  1063
    if (!(session && identity && identity->address &&
vb@298
  1064
        (identity->fpr == NULL || identity->fpr[0] == 0) && identity->username))
vb@191
  1065
        return PEP_ILLEGAL_VALUE;
vb@191
  1066
vb@46
  1067
    return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
vb@24
  1068
}
vb@24
  1069
vb@9
  1070
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
  1071
        PEP_SESSION session,
vb@46
  1072
        const char *fpr,
vb@46
  1073
        PEP_comm_type *comm_type
vb@9
  1074
    )
vb@9
  1075
{
vb@191
  1076
    assert(session);
vb@191
  1077
    assert(fpr);
vb@191
  1078
    assert(comm_type);
vb@191
  1079
vb@191
  1080
    if (!(session && fpr && comm_type))
vb@191
  1081
        return PEP_ILLEGAL_VALUE;
vb@191
  1082
vb@46
  1083
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
vb@24
  1084
}
vb@9
  1085
vb@24
  1086
DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
vb@24
  1087
{
vb@191
  1088
    assert(session);
vb@191
  1089
    assert(key_data);
vb@191
  1090
vb@191
  1091
    if (!(session && key_data))
vb@191
  1092
        return PEP_ILLEGAL_VALUE;
vb@191
  1093
vb@46
  1094
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size);
vb@24
  1095
}
vb@9
  1096
vb@24
  1097
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1098
{
vb@191
  1099
    assert(session);
vb@191
  1100
    assert(pattern);
vb@191
  1101
vb@191
  1102
    if (!(session && pattern))
vb@191
  1103
        return PEP_ILLEGAL_VALUE;
vb@191
  1104
vb@46
  1105
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1106
}
vb@9
  1107
vb@24
  1108
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1109
{
vb@191
  1110
    assert(session);
vb@191
  1111
    assert(pattern);
vb@191
  1112
vb@191
  1113
    if (!(session && pattern))
vb@191
  1114
        return PEP_ILLEGAL_VALUE;
vb@191
  1115
vb@46
  1116
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1117
}
vb@196
  1118
vb@201
  1119
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1120
        PEP_SESSION session,
vb@201
  1121
        const char *fpr,
vb@201
  1122
        const timestamp *ts
vb@201
  1123
    )
vb@196
  1124
{
vb@196
  1125
    assert(session);
vb@200
  1126
    assert(fpr);
vb@196
  1127
vb@200
  1128
    if (!(session && fpr))
vb@196
  1129
        return PEP_ILLEGAL_VALUE;
vb@196
  1130
vb@201
  1131
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1132
}
vb@196
  1133
vb@211
  1134
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1135
        PEP_SESSION session,
vb@211
  1136
        const char *fpr,
vb@211
  1137
        const char *reason
vb@211
  1138
    )
vb@197
  1139
{
vb@197
  1140
    assert(session);
vb@200
  1141
    assert(fpr);
vb@197
  1142
vb@200
  1143
    if (!(session && fpr))
vb@197
  1144
        return PEP_ILLEGAL_VALUE;
vb@197
  1145
vb@214
  1146
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1147
            reason);
vb@197
  1148
}
vb@197
  1149
vb@214
  1150
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1151
        PEP_SESSION session,
vb@214
  1152
        const char *fpr,
vb@214
  1153
        bool *expired
vb@214
  1154
    )
vb@214
  1155
{
vb@214
  1156
    assert(session);
vb@214
  1157
    assert(fpr);
vb@214
  1158
    assert(expired);
vb@214
  1159
vb@214
  1160
    if (!(session && fpr && expired))
vb@214
  1161
        return PEP_ILLEGAL_VALUE;
vb@214
  1162
vb@214
  1163
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
vb@214
  1164
            expired);
vb@214
  1165
}
vb@214
  1166
vb@451
  1167
static void _clean_log_value(char *text)
vb@451
  1168
{
vb@451
  1169
    if (text) {
vb@451
  1170
        for (char *c = text; *c; c++) {
vb@451
  1171
            if (*c < 32)
vb@451
  1172
                *c = 32;
vb@451
  1173
            else if (*c == '"')
vb@451
  1174
                *c = '\'';
vb@451
  1175
        }
vb@451
  1176
    }
vb@451
  1177
}
vb@451
  1178
vb@450
  1179
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1180
{
vb@454
  1181
    str2 = str2 ? str2 : "";
vb@450
  1182
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1183
    size_t len2 = strlen(str2);
vb@450
  1184
    size_t len = len1 + len2 + 3;
vb@450
  1185
    char * result = realloc(str1, len + 1);
vb@450
  1186
vb@450
  1187
    if (result) {
vb@450
  1188
        result[len1] = '"';
vb@450
  1189
        strcpy(result + len1 + 1, str2);
vb@450
  1190
        _clean_log_value(result + len1 + 1);
vb@450
  1191
        result[len - 2] = '"';
vb@450
  1192
        result[len - 1] = delim;
vb@450
  1193
        result[len] = 0;
vb@450
  1194
    }
vb@450
  1195
    else {
vb@450
  1196
        free(str1);
vb@450
  1197
    }
vb@450
  1198
vb@450
  1199
    return result;
vb@450
  1200
}
vb@450
  1201
vb@450
  1202
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1203
        PEP_SESSION session,
vb@450
  1204
        int maxlines,
vb@450
  1205
        char **logdata
vb@450
  1206
    )
vb@450
  1207
{
vb@450
  1208
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1209
    char *_logdata= NULL;
vb@450
  1210
vb@450
  1211
    assert(session);
vb@450
  1212
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1213
    assert(logdata);
vb@450
  1214
vb@450
  1215
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1216
            CRASHDUMP_MAX_LINES))
vb@450
  1217
        return PEP_ILLEGAL_VALUE;
vb@450
  1218
vb@450
  1219
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1220
    const char *timestamp = NULL;
vb@458
  1221
    const char *title = NULL;
vb@458
  1222
    const char *entity = NULL;
vb@458
  1223
    const char *desc = NULL;
vb@458
  1224
    const char *comment = NULL;
vb@450
  1225
vb@450
  1226
    sqlite3_reset(session->crashdump);
vb@452
  1227
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1228
vb@450
  1229
    int result;
vb@450
  1230
vb@450
  1231
    do {
vb@450
  1232
        result = sqlite3_step(session->crashdump);
vb@450
  1233
        switch (result) {
vb@450
  1234
        case SQLITE_ROW:
vb@452
  1235
            timestamp = (const char *) sqlite3_column_text(session->crashdump, 0);
vb@452
  1236
            title   = (const char *) sqlite3_column_text(session->crashdump, 1);
vb@452
  1237
            entity  = (const char *) sqlite3_column_text(session->crashdump, 2);
vb@452
  1238
            desc    = (const char *) sqlite3_column_text(session->crashdump, 3);
vb@452
  1239
            comment = (const char *) sqlite3_column_text(session->crashdump, 4);
vb@452
  1240
vb@452
  1241
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1242
            if (_logdata == NULL)
vb@452
  1243
                goto enomem;
vb@450
  1244
vb@450
  1245
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1246
            if (_logdata == NULL)
vb@450
  1247
                goto enomem;
vb@450
  1248
vb@450
  1249
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1250
            if (_logdata == NULL)
vb@450
  1251
                goto enomem;
vb@450
  1252
vb@450
  1253
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1254
            if (_logdata == NULL)
vb@450
  1255
                goto enomem;
vb@450
  1256
vb@450
  1257
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1258
            if (_logdata == NULL)
vb@450
  1259
                goto enomem;
vb@450
  1260
vb@450
  1261
            break;
vb@450
  1262
vb@450
  1263
        case SQLITE_DONE:
vb@450
  1264
            break;
vb@450
  1265
vb@450
  1266
        default:
vb@450
  1267
            status = PEP_UNKNOWN_ERROR;
vb@450
  1268
            result = SQLITE_DONE;
vb@450
  1269
        }
vb@450
  1270
    } while (result != SQLITE_DONE);
vb@450
  1271
vb@450
  1272
    sqlite3_reset(session->crashdump);
vb@450
  1273
    if (status == PEP_STATUS_OK)
vb@450
  1274
        *logdata = _logdata;
vb@450
  1275
vb@450
  1276
    goto the_end;
vb@450
  1277
vb@450
  1278
enomem:
vb@450
  1279
    status = PEP_OUT_OF_MEMORY;
vb@450
  1280
vb@450
  1281
the_end:
vb@450
  1282
    return status;
vb@450
  1283
}
vb@450
  1284
vb@458
  1285
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  1286
        PEP_SESSION session,
vb@458
  1287
        char **languages
vb@458
  1288
    )
vb@458
  1289
{
vb@458
  1290
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1291
    char *_languages= NULL;
vb@458
  1292
vb@458
  1293
    assert(session);
vb@458
  1294
    assert(languages);
vb@458
  1295
vb@458
  1296
    if (!(session && languages))
vb@458
  1297
        return PEP_ILLEGAL_VALUE;
vb@458
  1298
vb@458
  1299
    const char *lang = NULL;
vb@458
  1300
    const char *name = NULL;
vb@458
  1301
vb@458
  1302
    sqlite3_reset(session->languagelist);
vb@458
  1303
vb@458
  1304
    int result;
vb@458
  1305
vb@458
  1306
    do {
vb@458
  1307
        result = sqlite3_step(session->languagelist);
vb@458
  1308
        switch (result) {
vb@458
  1309
        case SQLITE_ROW:
vb@458
  1310
            lang = (const char *) sqlite3_column_text(session->languagelist, 0);
vb@458
  1311
            name = (const char *) sqlite3_column_text(session->languagelist, 1);
vb@458
  1312
vb@458
  1313
            _languages = _concat_string(_languages, lang, ',');
vb@458
  1314
            if (_languages == NULL)
vb@458
  1315
                goto enomem;
vb@458
  1316
vb@458
  1317
            _languages = _concat_string(_languages, name, '\n');
vb@458
  1318
            if (_languages == NULL)
vb@458
  1319
                goto enomem;
vb@458
  1320
vb@458
  1321
            break;
vb@458
  1322
vb@458
  1323
        case SQLITE_DONE:
vb@458
  1324
            break;
vb@458
  1325
vb@458
  1326
        default:
vb@458
  1327
            status = PEP_UNKNOWN_ERROR;
vb@458
  1328
            result = SQLITE_DONE;
vb@458
  1329
        }
vb@458
  1330
    } while (result != SQLITE_DONE);
vb@458
  1331
vb@458
  1332
    sqlite3_reset(session->languagelist);
vb@458
  1333
    if (status == PEP_STATUS_OK)
vb@458
  1334
        *languages = _languages;
vb@458
  1335
vb@458
  1336
    goto the_end;
vb@458
  1337
vb@458
  1338
enomem:
vb@458
  1339
    status = PEP_OUT_OF_MEMORY;
vb@458
  1340
vb@458
  1341
the_end:
vb@458
  1342
    return status;
vb@458
  1343
}
vb@458
  1344
vb@458
  1345
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  1346
        PEP_SESSION session,
vb@458
  1347
        const char *lang,
vb@458
  1348
        int phrase_id,
vb@458
  1349
        char **phrase
vb@458
  1350
    )
vb@458
  1351
{
vb@458
  1352
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1353
    *phrase = NULL;
vb@458
  1354
vb@479
  1355
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@458
  1356
vb@479
  1357
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  1358
        return PEP_ILLEGAL_VALUE;
vb@458
  1359
vb@458
  1360
    sqlite3_reset(session->i18n_token);
roker@529
  1361
    sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
roker@529
  1362
    sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  1363
vb@458
  1364
    const char *_phrase = NULL;
vb@458
  1365
    int result;
vb@458
  1366
vb@458
  1367
    result = sqlite3_step(session->i18n_token);
vb@458
  1368
    switch (result) {
vb@458
  1369
    case SQLITE_ROW:
vb@458
  1370
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  1371
        break;
vb@458
  1372
vb@458
  1373
    case SQLITE_DONE:
vb@458
  1374
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  1375
        break;
vb@458
  1376
vb@458
  1377
    default:
vb@458
  1378
        status = PEP_UNKNOWN_ERROR;
vb@458
  1379
    }
vb@458
  1380
vb@458
  1381
    if (status == PEP_STATUS_OK) {
vb@458
  1382
        *phrase = strdup(_phrase);
vb@458
  1383
        if (*phrase == NULL)
vb@458
  1384
            goto enomem;
vb@458
  1385
    }
vb@459
  1386
vb@459
  1387
    sqlite3_reset(session->i18n_token);
vb@458
  1388
    goto the_end;
vb@458
  1389
vb@458
  1390
enomem:
vb@458
  1391
    status = PEP_OUT_OF_MEMORY;
vb@458
  1392
vb@458
  1393
the_end:
vb@458
  1394
    return status;
vb@458
  1395
}
vb@458
  1396
vb@482
  1397
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  1398
{
vb@482
  1399
    assert(session);
vb@482
  1400
vb@482
  1401
    if (!session)
vb@482
  1402
        return PEP_ILLEGAL_VALUE;
vb@482
  1403
fdik@494
  1404
    int int_result = sqlite3_exec(
fdik@494
  1405
        session->db,
fdik@494
  1406
        "delete from identity where address like '%@peptest.ch' ;",
fdik@494
  1407
        NULL,
fdik@494
  1408
        NULL,
fdik@494
  1409
        NULL
fdik@494
  1410
    );
fdik@494
  1411
    assert(int_result == SQLITE_OK);
vb@482
  1412
fdik@494
  1413
    return PEP_STATUS_OK;
vb@482
  1414
}
vb@482
  1415