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