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