src/pEpEngine.c
author Volker Birk <vb@pep-project.org>
Thu, 12 May 2016 20:00:51 +0200
changeset 617 0caa732cf9a4
parent 591 a3dbbf610d32
child 619 f93107f46f92
permissions -rw-r--r--
adding third column with phrase
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@588
   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@617
   235
        sql_languagelist = "select i18n_language.lang, name, phrase from i18n_language join i18n_token using (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
        }
vb@0
   647
        if (fpr) {
vb@0
   648
            result->fpr = strdup(fpr);
vb@0
   649
            assert(result->fpr);
vb@0
   650
            if (result->fpr == NULL) {
vb@0
   651
                free_identity(result);
vb@0
   652
                return NULL;
vb@0
   653
            }
vb@0
   654
        }
vb@0
   655
        if (user_id) {
vb@0
   656
            result->user_id = strdup(user_id);
vb@0
   657
            assert(result->user_id);
vb@0
   658
            if (result->user_id == NULL) {
vb@0
   659
                free_identity(result);
vb@0
   660
                return NULL;
vb@0
   661
            }
vb@0
   662
        }
vb@0
   663
        if (username) {
vb@0
   664
            result->username = strdup(username);
vb@0
   665
            assert(result->username);
vb@0
   666
            if (result->username == NULL) {
vb@0
   667
                free_identity(result);
vb@0
   668
                return NULL;
vb@0
   669
            }
vb@0
   670
        }
vb@0
   671
    }
vb@0
   672
    return result;
vb@0
   673
}
vb@0
   674
vb@37
   675
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   676
{
vb@37
   677
    assert(src);
vb@37
   678
vb@37
   679
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
vb@37
   680
    assert(dup);
vb@37
   681
    if (dup == NULL)
vb@37
   682
        return NULL;
vb@37
   683
    
vb@37
   684
    dup->comm_type = src->comm_type;
vb@37
   685
    dup->lang[0] = src->lang[0];
vb@37
   686
    dup->lang[1] = src->lang[1];
vb@37
   687
    dup->lang[2] = 0;
vb@37
   688
    dup->me = src->me;
vb@37
   689
vb@37
   690
    return dup;
vb@37
   691
}
vb@37
   692
vb@0
   693
void free_identity(pEp_identity *identity)
vb@0
   694
{
vb@0
   695
    if (identity) {
vb@0
   696
        free(identity->address);
vb@0
   697
        free(identity->fpr);
vb@0
   698
        free(identity->user_id);
vb@0
   699
        free(identity->username);
vb@0
   700
        free(identity);
vb@0
   701
    }
vb@0
   702
}
vb@0
   703
vb@0
   704
DYNAMIC_API PEP_STATUS get_identity(
Edouard@559
   705
        PEP_SESSION session,
Edouard@559
   706
        const char *address,
Edouard@559
   707
        const char *user_id,
vb@0
   708
        pEp_identity **identity
vb@0
   709
    )
vb@0
   710
{
roker@529
   711
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   712
    static pEp_identity *_identity;
roker@529
   713
    int result;
roker@529
   714
    const char *_lang;
vb@0
   715
roker@529
   716
    assert(session);
roker@529
   717
    assert(address);
vb@8
   718
    assert(address[0]);
vb@0
   719
vb@191
   720
    if (!(session && address && address[0]))
vb@191
   721
        return PEP_ILLEGAL_VALUE;
vb@191
   722
vb@46
   723
    sqlite3_reset(session->get_identity);
vb@46
   724
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
Edouard@559
   725
    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
vb@0
   726
vb@46
   727
    result = sqlite3_step(session->get_identity);
roker@529
   728
    switch (result) {
roker@529
   729
    case SQLITE_ROW:
vb@0
   730
        _identity = new_identity(
vb@0
   731
                address,
vb@46
   732
                (const char *) sqlite3_column_text(session->get_identity, 0),
Edouard@559
   733
                user_id,
Edouard@559
   734
                (const char *) sqlite3_column_text(session->get_identity, 1)
vb@0
   735
                );
vb@0
   736
        assert(_identity);
vb@0
   737
        if (_identity == NULL)
vb@0
   738
            return PEP_OUT_OF_MEMORY;
vb@0
   739
Edouard@559
   740
        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 2);
Edouard@559
   741
        _lang = (const char *) sqlite3_column_text(session->get_identity, 3);
vb@0
   742
        if (_lang && _lang[0]) {
roker@529
   743
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
roker@529
   744
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
roker@529
   745
            assert(_lang[2] == 0);
roker@529
   746
            _identity->lang[0] = _lang[0];
roker@529
   747
            _identity->lang[1] = _lang[1];
vb@0
   748
            _identity->lang[2] = 0;
roker@529
   749
        }
roker@529
   750
        *identity = _identity;
roker@529
   751
        break;
roker@529
   752
    default:
vb@0
   753
        status = PEP_CANNOT_FIND_IDENTITY;
roker@529
   754
        *identity = NULL;
roker@529
   755
    }
vb@0
   756
vb@46
   757
    sqlite3_reset(session->get_identity);
roker@529
   758
    return status;
vb@0
   759
}
vb@0
   760
vb@0
   761
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   762
        PEP_SESSION session, const pEp_identity *identity
vb@0
   763
    )
vb@0
   764
{
roker@529
   765
    int result;
vb@0
   766
roker@529
   767
    assert(session);
roker@529
   768
    assert(identity);
roker@529
   769
    assert(identity->address);
roker@529
   770
    assert(identity->fpr);
roker@529
   771
    assert(identity->user_id);
roker@529
   772
    assert(identity->username);
vb@0
   773
vb@191
   774
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   775
                identity->user_id && identity->username))
vb@191
   776
        return PEP_ILLEGAL_VALUE;
vb@191
   777
vb@515
   778
    bool listed;
vb@515
   779
    PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
vb@515
   780
    assert(status == PEP_STATUS_OK);
vb@515
   781
    if (status != PEP_STATUS_OK)
vb@515
   782
        return status;
vb@515
   783
vb@515
   784
    if (listed)
vb@515
   785
        return PEP_KEY_BLACKLISTED;
vb@515
   786
roker@529
   787
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   788
roker@529
   789
    sqlite3_reset(session->set_person);
vb@46
   790
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
   791
            SQLITE_STATIC);
vb@46
   792
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
   793
            SQLITE_STATIC);
roker@529
   794
    if (identity->lang[0])
vb@46
   795
        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
vb@0
   796
                SQLITE_STATIC);
roker@529
   797
    else
roker@529
   798
        sqlite3_bind_null(session->set_person, 3);
roker@529
   799
    result = sqlite3_step(session->set_person);
roker@529
   800
    sqlite3_reset(session->set_person);
roker@529
   801
    if (result != SQLITE_DONE) {
roker@529
   802
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   803
        return PEP_CANNOT_SET_PERSON;
roker@529
   804
    }
vb@0
   805
roker@529
   806
    sqlite3_reset(session->set_pgp_keypair);
vb@46
   807
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   808
            SQLITE_STATIC);
roker@529
   809
    result = sqlite3_step(session->set_pgp_keypair);
roker@529
   810
    sqlite3_reset(session->set_pgp_keypair);
roker@529
   811
    if (result != SQLITE_DONE) {
roker@529
   812
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   813
        return PEP_CANNOT_SET_PGP_KEYPAIR;
roker@529
   814
    }
vb@0
   815
roker@529
   816
    sqlite3_reset(session->set_identity);
vb@46
   817
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
   818
            SQLITE_STATIC);
vb@46
   819
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
   820
            SQLITE_STATIC);
vb@46
   821
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
   822
            SQLITE_STATIC);
roker@529
   823
    result = sqlite3_step(session->set_identity);
roker@529
   824
    sqlite3_reset(session->set_identity);
roker@529
   825
    if (result != SQLITE_DONE) {
roker@529
   826
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   827
        return PEP_CANNOT_SET_IDENTITY;
roker@529
   828
    }
vb@0
   829
roker@529
   830
    sqlite3_reset(session->set_trust);
vb@46
   831
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
   832
            SQLITE_STATIC);
vb@46
   833
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
   834
            SQLITE_STATIC);
roker@529
   835
    sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
roker@529
   836
    result = sqlite3_step(session->set_trust);
roker@529
   837
    sqlite3_reset(session->set_trust);
roker@529
   838
    if (result != SQLITE_DONE) {
roker@529
   839
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
Edouard@584
   840
        return PEP_CANNOT_SET_TRUST;
roker@529
   841
    }
vb@0
   842
vb@46
   843
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
roker@529
   844
    if (result == SQLITE_OK)
roker@529
   845
        return PEP_STATUS_OK;
roker@529
   846
    else
roker@529
   847
        return PEP_COMMIT_FAILED;
vb@0
   848
}
vb@0
   849
vb@357
   850
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
   851
        PEP_SESSION session,
vb@357
   852
        const char *fpr
vb@357
   853
    )
vb@357
   854
{
roker@529
   855
    int result;
vb@357
   856
roker@529
   857
    assert(session);
vb@357
   858
    assert(fpr && fpr[0]);
vb@357
   859
vb@357
   860
    if (!(session && fpr && fpr[0]))
vb@357
   861
        return PEP_ILLEGAL_VALUE;
vb@357
   862
roker@529
   863
    sqlite3_reset(session->mark_compromized);
vb@357
   864
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
   865
            SQLITE_STATIC);
vb@357
   866
    result = sqlite3_step(session->mark_compromized);
roker@529
   867
    sqlite3_reset(session->mark_compromized);
vb@357
   868
vb@357
   869
    if (result != SQLITE_DONE)
Edouard@584
   870
        return PEP_CANNOT_SET_TRUST;
vb@357
   871
vb@357
   872
    return PEP_STATUS_OK;
vb@357
   873
}
vb@357
   874
vb@0
   875
void pEp_free(void *p)
vb@0
   876
{
vb@0
   877
    free(p);
vb@0
   878
}
vb@0
   879
vb@8
   880
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
   881
{
vb@8
   882
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
   883
    int result;
vb@8
   884
vb@8
   885
    assert(session);
vb@8
   886
    assert(identity);
vb@8
   887
    assert(identity->user_id);
vb@8
   888
    assert(identity->user_id[0]);
vb@8
   889
    assert(identity->fpr);
vb@8
   890
    assert(identity->fpr[0]);
vb@8
   891
vb@191
   892
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
   893
                identity->fpr && identity->fpr[0]))
vb@191
   894
        return PEP_ILLEGAL_VALUE;
vb@191
   895
vb@8
   896
    identity->comm_type = PEP_ct_unknown;
vb@8
   897
vb@46
   898
    sqlite3_reset(session->get_trust);
vb@46
   899
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
vb@46
   900
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
   901
vb@46
   902
    result = sqlite3_step(session->get_trust);
vb@8
   903
    switch (result) {
vb@8
   904
    case SQLITE_ROW: {
Edouard@559
   905
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 0);
vb@8
   906
        identity->comm_type = comm_type;
vb@8
   907
        break;
vb@8
   908
    }
vb@8
   909
 
vb@8
   910
    default:
vb@8
   911
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
   912
    }
vb@8
   913
vb@46
   914
    sqlite3_reset(session->get_trust);
vb@8
   915
    return status;
vb@9
   916
}
vb@9
   917
vb@251
   918
DYNAMIC_API PEP_STATUS least_trust(
vb@251
   919
        PEP_SESSION session,
vb@251
   920
        const char *fpr,
vb@251
   921
        PEP_comm_type *comm_type
vb@251
   922
    )
vb@251
   923
{
vb@251
   924
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
   925
    int result;
vb@251
   926
vb@251
   927
    assert(session);
vb@251
   928
    assert(fpr);
vb@251
   929
    assert(comm_type);
vb@251
   930
vb@259
   931
    *comm_type = PEP_ct_unknown;
vb@259
   932
vb@251
   933
    if (!(session && fpr && comm_type))
vb@251
   934
        return PEP_ILLEGAL_VALUE;
vb@251
   935
vb@251
   936
    sqlite3_reset(session->least_trust);
vb@251
   937
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
   938
vb@251
   939
    result = sqlite3_step(session->least_trust);
vb@251
   940
    switch (result) {
vb@251
   941
        case SQLITE_ROW: {
vb@259
   942
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
   943
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
   944
            break;
vb@251
   945
        }
vb@251
   946
        default:
vb@251
   947
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
   948
    }
vb@251
   949
vb@251
   950
    sqlite3_reset(session->least_trust);
vb@251
   951
    return status;
vb@251
   952
}
vb@251
   953
vb@24
   954
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
   955
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
   956
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   957
    )
vb@24
   958
{
vb@191
   959
    assert(session);
vb@191
   960
    assert(ctext);
vb@191
   961
    assert(csize);
vb@191
   962
    assert(ptext);
vb@191
   963
    assert(psize);
vb@191
   964
    assert(keylist);
vb@191
   965
vb@191
   966
    if (!(session && ctext && csize && ptext && psize && keylist && keylist))
vb@191
   967
        return PEP_ILLEGAL_VALUE;
vb@191
   968
vb@46
   969
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
vb@24
   970
}
vb@24
   971
vb@24
   972
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
   973
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
   974
    size_t psize, char **ctext, size_t *csize
vb@24
   975
    )
vb@24
   976
{
vb@191
   977
    assert(session);
vb@191
   978
    assert(keylist);
vb@191
   979
    assert(ptext);
vb@191
   980
    assert(psize);
vb@191
   981
    assert(ctext);
vb@191
   982
    assert(csize);
vb@191
   983
vb@191
   984
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
   985
        return PEP_ILLEGAL_VALUE;
vb@191
   986
vb@46
   987
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
vb@24
   988
}
vb@24
   989
vb@24
   990
DYNAMIC_API PEP_STATUS verify_text(
vb@24
   991
    PEP_SESSION session, const char *text, size_t size,
vb@24
   992
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   993
    )
vb@24
   994
{
vb@191
   995
    assert(session);
vb@191
   996
    assert(text);
vb@191
   997
    assert(size);
vb@191
   998
    assert(signature);
vb@191
   999
    assert(sig_size);
vb@191
  1000
    assert(keylist);
vb@191
  1001
vb@191
  1002
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
  1003
        return PEP_ILLEGAL_VALUE;
vb@191
  1004
vb@46
  1005
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
vb@24
  1006
}
vb@24
  1007
vb@24
  1008
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1009
{
vb@191
  1010
    assert(session);
vb@191
  1011
    assert(fpr);
vb@191
  1012
vb@191
  1013
    if (!(session && fpr))
vb@191
  1014
        return PEP_ILLEGAL_VALUE;
vb@191
  1015
vb@46
  1016
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
  1017
}
vb@24
  1018
vb@24
  1019
DYNAMIC_API PEP_STATUS export_key(
vb@46
  1020
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1021
    )
vb@24
  1022
{
vb@191
  1023
    assert(session);
vb@191
  1024
    assert(fpr);
vb@191
  1025
    assert(key_data);
vb@191
  1026
    assert(size);
vb@191
  1027
vb@191
  1028
    if (!(session && fpr && key_data && size))
vb@191
  1029
        return PEP_ILLEGAL_VALUE;
vb@191
  1030
vb@46
  1031
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
vb@24
  1032
}
vb@24
  1033
vb@24
  1034
DYNAMIC_API PEP_STATUS find_keys(
vb@46
  1035
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1036
    )
vb@24
  1037
{
vb@191
  1038
    assert(session);
vb@191
  1039
    assert(pattern);
vb@191
  1040
    assert(keylist);
vb@191
  1041
vb@191
  1042
    if (!(session && pattern && keylist))
vb@191
  1043
        return PEP_ILLEGAL_VALUE;
vb@191
  1044
vb@46
  1045
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
vb@24
  1046
}
vb@24
  1047
vb@24
  1048
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
  1049
        PEP_SESSION session, pEp_identity *identity
vb@24
  1050
    )
vb@24
  1051
{
vb@191
  1052
    assert(session);
vb@191
  1053
    assert(identity);
vb@191
  1054
    assert(identity->address);
vb@298
  1055
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
  1056
    assert(identity->username);
vb@191
  1057
vb@298
  1058
    if (!(session && identity && identity->address &&
vb@298
  1059
        (identity->fpr == NULL || identity->fpr[0] == 0) && identity->username))
vb@191
  1060
        return PEP_ILLEGAL_VALUE;
vb@191
  1061
vb@46
  1062
    return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
vb@24
  1063
}
vb@24
  1064
vb@9
  1065
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
  1066
        PEP_SESSION session,
vb@46
  1067
        const char *fpr,
vb@46
  1068
        PEP_comm_type *comm_type
vb@9
  1069
    )
vb@9
  1070
{
vb@191
  1071
    assert(session);
vb@191
  1072
    assert(fpr);
vb@191
  1073
    assert(comm_type);
vb@191
  1074
vb@191
  1075
    if (!(session && fpr && comm_type))
vb@191
  1076
        return PEP_ILLEGAL_VALUE;
vb@191
  1077
vb@46
  1078
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
vb@24
  1079
}
vb@9
  1080
vb@24
  1081
DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
vb@24
  1082
{
vb@191
  1083
    assert(session);
vb@191
  1084
    assert(key_data);
vb@191
  1085
vb@191
  1086
    if (!(session && key_data))
vb@191
  1087
        return PEP_ILLEGAL_VALUE;
vb@191
  1088
vb@46
  1089
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size);
vb@24
  1090
}
vb@9
  1091
vb@24
  1092
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1093
{
vb@191
  1094
    assert(session);
vb@191
  1095
    assert(pattern);
vb@191
  1096
vb@191
  1097
    if (!(session && pattern))
vb@191
  1098
        return PEP_ILLEGAL_VALUE;
vb@191
  1099
vb@46
  1100
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1101
}
vb@9
  1102
vb@24
  1103
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1104
{
vb@191
  1105
    assert(session);
vb@191
  1106
    assert(pattern);
vb@191
  1107
vb@191
  1108
    if (!(session && pattern))
vb@191
  1109
        return PEP_ILLEGAL_VALUE;
vb@191
  1110
vb@46
  1111
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1112
}
vb@196
  1113
vb@201
  1114
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1115
        PEP_SESSION session,
vb@201
  1116
        const char *fpr,
vb@201
  1117
        const timestamp *ts
vb@201
  1118
    )
vb@196
  1119
{
vb@196
  1120
    assert(session);
vb@200
  1121
    assert(fpr);
vb@196
  1122
vb@200
  1123
    if (!(session && fpr))
vb@196
  1124
        return PEP_ILLEGAL_VALUE;
vb@196
  1125
vb@201
  1126
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1127
}
vb@196
  1128
vb@211
  1129
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1130
        PEP_SESSION session,
vb@211
  1131
        const char *fpr,
vb@211
  1132
        const char *reason
vb@211
  1133
    )
vb@197
  1134
{
vb@197
  1135
    assert(session);
vb@200
  1136
    assert(fpr);
vb@197
  1137
vb@200
  1138
    if (!(session && fpr))
vb@197
  1139
        return PEP_ILLEGAL_VALUE;
vb@197
  1140
vb@214
  1141
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1142
            reason);
vb@197
  1143
}
vb@197
  1144
vb@214
  1145
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1146
        PEP_SESSION session,
vb@214
  1147
        const char *fpr,
vb@214
  1148
        bool *expired
vb@214
  1149
    )
vb@214
  1150
{
vb@214
  1151
    assert(session);
vb@214
  1152
    assert(fpr);
vb@214
  1153
    assert(expired);
vb@214
  1154
vb@214
  1155
    if (!(session && fpr && expired))
vb@214
  1156
        return PEP_ILLEGAL_VALUE;
vb@214
  1157
vb@214
  1158
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
vb@214
  1159
            expired);
vb@214
  1160
}
vb@214
  1161
vb@451
  1162
static void _clean_log_value(char *text)
vb@451
  1163
{
vb@451
  1164
    if (text) {
vb@451
  1165
        for (char *c = text; *c; c++) {
vb@451
  1166
            if (*c < 32)
vb@451
  1167
                *c = 32;
vb@451
  1168
            else if (*c == '"')
vb@451
  1169
                *c = '\'';
vb@451
  1170
        }
vb@451
  1171
    }
vb@451
  1172
}
vb@451
  1173
vb@450
  1174
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1175
{
vb@454
  1176
    str2 = str2 ? str2 : "";
vb@450
  1177
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1178
    size_t len2 = strlen(str2);
vb@450
  1179
    size_t len = len1 + len2 + 3;
vb@450
  1180
    char * result = realloc(str1, len + 1);
vb@450
  1181
vb@450
  1182
    if (result) {
vb@450
  1183
        result[len1] = '"';
vb@450
  1184
        strcpy(result + len1 + 1, str2);
vb@450
  1185
        _clean_log_value(result + len1 + 1);
vb@450
  1186
        result[len - 2] = '"';
vb@450
  1187
        result[len - 1] = delim;
vb@450
  1188
        result[len] = 0;
vb@450
  1189
    }
vb@450
  1190
    else {
vb@450
  1191
        free(str1);
vb@450
  1192
    }
vb@450
  1193
vb@450
  1194
    return result;
vb@450
  1195
}
vb@450
  1196
vb@450
  1197
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1198
        PEP_SESSION session,
vb@450
  1199
        int maxlines,
vb@450
  1200
        char **logdata
vb@450
  1201
    )
vb@450
  1202
{
vb@450
  1203
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1204
    char *_logdata= NULL;
vb@450
  1205
vb@450
  1206
    assert(session);
vb@450
  1207
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1208
    assert(logdata);
vb@450
  1209
vb@450
  1210
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1211
            CRASHDUMP_MAX_LINES))
vb@450
  1212
        return PEP_ILLEGAL_VALUE;
vb@450
  1213
vb@450
  1214
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1215
    const char *timestamp = NULL;
vb@458
  1216
    const char *title = NULL;
vb@458
  1217
    const char *entity = NULL;
vb@458
  1218
    const char *desc = NULL;
vb@458
  1219
    const char *comment = NULL;
vb@450
  1220
vb@450
  1221
    sqlite3_reset(session->crashdump);
vb@452
  1222
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1223
vb@450
  1224
    int result;
vb@450
  1225
vb@450
  1226
    do {
vb@450
  1227
        result = sqlite3_step(session->crashdump);
vb@450
  1228
        switch (result) {
vb@450
  1229
        case SQLITE_ROW:
vb@452
  1230
            timestamp = (const char *) sqlite3_column_text(session->crashdump, 0);
vb@452
  1231
            title   = (const char *) sqlite3_column_text(session->crashdump, 1);
vb@452
  1232
            entity  = (const char *) sqlite3_column_text(session->crashdump, 2);
vb@452
  1233
            desc    = (const char *) sqlite3_column_text(session->crashdump, 3);
vb@452
  1234
            comment = (const char *) sqlite3_column_text(session->crashdump, 4);
vb@452
  1235
vb@452
  1236
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1237
            if (_logdata == NULL)
vb@452
  1238
                goto enomem;
vb@450
  1239
vb@450
  1240
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1241
            if (_logdata == NULL)
vb@450
  1242
                goto enomem;
vb@450
  1243
vb@450
  1244
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1245
            if (_logdata == NULL)
vb@450
  1246
                goto enomem;
vb@450
  1247
vb@450
  1248
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1249
            if (_logdata == NULL)
vb@450
  1250
                goto enomem;
vb@450
  1251
vb@450
  1252
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1253
            if (_logdata == NULL)
vb@450
  1254
                goto enomem;
vb@450
  1255
vb@450
  1256
            break;
vb@450
  1257
vb@450
  1258
        case SQLITE_DONE:
vb@450
  1259
            break;
vb@450
  1260
vb@450
  1261
        default:
vb@450
  1262
            status = PEP_UNKNOWN_ERROR;
vb@450
  1263
            result = SQLITE_DONE;
vb@450
  1264
        }
vb@450
  1265
    } while (result != SQLITE_DONE);
vb@450
  1266
vb@450
  1267
    sqlite3_reset(session->crashdump);
vb@450
  1268
    if (status == PEP_STATUS_OK)
vb@450
  1269
        *logdata = _logdata;
vb@450
  1270
vb@450
  1271
    goto the_end;
vb@450
  1272
vb@450
  1273
enomem:
vb@450
  1274
    status = PEP_OUT_OF_MEMORY;
vb@450
  1275
vb@450
  1276
the_end:
vb@450
  1277
    return status;
vb@450
  1278
}
vb@450
  1279
vb@458
  1280
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  1281
        PEP_SESSION session,
vb@458
  1282
        char **languages
vb@458
  1283
    )
vb@458
  1284
{
vb@458
  1285
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1286
    char *_languages= NULL;
vb@458
  1287
vb@458
  1288
    assert(session);
vb@458
  1289
    assert(languages);
vb@458
  1290
vb@458
  1291
    if (!(session && languages))
vb@458
  1292
        return PEP_ILLEGAL_VALUE;
vb@458
  1293
vb@458
  1294
    const char *lang = NULL;
vb@458
  1295
    const char *name = NULL;
vb@617
  1296
    const char *phrase = NULL;
vb@458
  1297
vb@458
  1298
    sqlite3_reset(session->languagelist);
vb@458
  1299
vb@458
  1300
    int result;
vb@458
  1301
vb@458
  1302
    do {
vb@458
  1303
        result = sqlite3_step(session->languagelist);
vb@458
  1304
        switch (result) {
vb@458
  1305
        case SQLITE_ROW:
vb@458
  1306
            lang = (const char *) sqlite3_column_text(session->languagelist, 0);
vb@458
  1307
            name = (const char *) sqlite3_column_text(session->languagelist, 1);
vb@617
  1308
            phrase = (const char *) sqlite3_column_text(session->languagelist, 2);
vb@458
  1309
vb@458
  1310
            _languages = _concat_string(_languages, lang, ',');
vb@458
  1311
            if (_languages == NULL)
vb@458
  1312
                goto enomem;
vb@458
  1313
vb@617
  1314
            _languages = _concat_string(_languages, name, ',');
vb@617
  1315
            if (_languages == NULL)
vb@617
  1316
                goto enomem;
vb@617
  1317
vb@617
  1318
            _languages = _concat_string(_languages, phrase, '\n');
vb@458
  1319
            if (_languages == NULL)
vb@458
  1320
                goto enomem;
vb@458
  1321
vb@458
  1322
            break;
vb@458
  1323
vb@458
  1324
        case SQLITE_DONE:
vb@458
  1325
            break;
vb@458
  1326
vb@458
  1327
        default:
vb@458
  1328
            status = PEP_UNKNOWN_ERROR;
vb@458
  1329
            result = SQLITE_DONE;
vb@458
  1330
        }
vb@458
  1331
    } while (result != SQLITE_DONE);
vb@458
  1332
vb@458
  1333
    sqlite3_reset(session->languagelist);
vb@458
  1334
    if (status == PEP_STATUS_OK)
vb@458
  1335
        *languages = _languages;
vb@458
  1336
vb@458
  1337
    goto the_end;
vb@458
  1338
vb@458
  1339
enomem:
vb@458
  1340
    status = PEP_OUT_OF_MEMORY;
vb@458
  1341
vb@458
  1342
the_end:
vb@458
  1343
    return status;
vb@458
  1344
}
vb@458
  1345
vb@458
  1346
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  1347
        PEP_SESSION session,
vb@458
  1348
        const char *lang,
vb@458
  1349
        int phrase_id,
vb@458
  1350
        char **phrase
vb@458
  1351
    )
vb@458
  1352
{
vb@458
  1353
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1354
    *phrase = NULL;
vb@458
  1355
vb@479
  1356
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@458
  1357
vb@479
  1358
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  1359
        return PEP_ILLEGAL_VALUE;
vb@458
  1360
vb@458
  1361
    sqlite3_reset(session->i18n_token);
roker@529
  1362
    sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
roker@529
  1363
    sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  1364
vb@458
  1365
    const char *_phrase = NULL;
vb@458
  1366
    int result;
vb@458
  1367
vb@458
  1368
    result = sqlite3_step(session->i18n_token);
vb@458
  1369
    switch (result) {
vb@458
  1370
    case SQLITE_ROW:
vb@458
  1371
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  1372
        break;
vb@458
  1373
vb@458
  1374
    case SQLITE_DONE:
vb@458
  1375
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  1376
        break;
vb@458
  1377
vb@458
  1378
    default:
vb@458
  1379
        status = PEP_UNKNOWN_ERROR;
vb@458
  1380
    }
vb@458
  1381
vb@458
  1382
    if (status == PEP_STATUS_OK) {
vb@458
  1383
        *phrase = strdup(_phrase);
vb@458
  1384
        if (*phrase == NULL)
vb@458
  1385
            goto enomem;
vb@458
  1386
    }
vb@459
  1387
vb@459
  1388
    sqlite3_reset(session->i18n_token);
vb@458
  1389
    goto the_end;
vb@458
  1390
vb@458
  1391
enomem:
vb@458
  1392
    status = PEP_OUT_OF_MEMORY;
vb@458
  1393
vb@458
  1394
the_end:
vb@458
  1395
    return status;
vb@458
  1396
}
vb@458
  1397
vb@482
  1398
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  1399
{
vb@482
  1400
    assert(session);
vb@482
  1401
vb@482
  1402
    if (!session)
vb@482
  1403
        return PEP_ILLEGAL_VALUE;
vb@482
  1404
fdik@494
  1405
    int int_result = sqlite3_exec(
fdik@494
  1406
        session->db,
fdik@494
  1407
        "delete from identity where address like '%@peptest.ch' ;",
fdik@494
  1408
        NULL,
fdik@494
  1409
        NULL,
fdik@494
  1410
        NULL
fdik@494
  1411
    );
fdik@494
  1412
    assert(int_result == SQLITE_OK);
vb@482
  1413
fdik@494
  1414
    return PEP_STATUS_OK;
vb@482
  1415
}
vb@482
  1416