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