src/pEpEngine.c
author Volker Birk <vb@pep-project.org>
Sat, 20 Feb 2016 14:43:28 +0100
changeset 450 ae04b9c42932
parent 444 a3a2a08b8d8e
child 451 495e53a4b441
permissions -rw-r--r--
adding get_crashdump_log()
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@450
   322
static void _clean_log_value(char *text)
vb@450
   323
{
vb@450
   324
    if (text) {
vb@450
   325
        for (char *c = text; *c; c++) {
vb@450
   326
            if (*c < 32)
vb@450
   327
                *c = 32;
vb@450
   328
            else if (*c == '"')
vb@450
   329
                *c = '\'';
vb@450
   330
        }
vb@450
   331
    }
vb@450
   332
}
vb@450
   333
vb@0
   334
DYNAMIC_API PEP_STATUS log_event(
vb@450
   335
        PEP_SESSION session,
vb@450
   336
        char *title,
vb@450
   337
        char *entity,
vb@450
   338
        char *description,
vb@450
   339
        char *comment
vb@0
   340
    )
vb@0
   341
{
vb@0
   342
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   343
	int result;
vb@0
   344
vb@46
   345
	assert(session);
vb@0
   346
	assert(title);
vb@0
   347
	assert(entity);
vb@0
   348
vb@191
   349
    if (!(session && title && entity))
vb@191
   350
        return PEP_ILLEGAL_VALUE;
vb@191
   351
vb@450
   352
    _clean_log_value(title);
vb@450
   353
    _clean_log_value(entity);
vb@450
   354
    _clean_log_value(description);
vb@450
   355
    _clean_log_value(comment);
vb@450
   356
vb@46
   357
	sqlite3_reset(session->log);
vb@46
   358
	sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
vb@46
   359
	sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
vb@0
   360
	if (description)
vb@46
   361
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
vb@0
   362
	else
vb@46
   363
		sqlite3_bind_null(session->log, 3);
vb@0
   364
	if (comment)
vb@46
   365
		sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
vb@0
   366
	else
vb@46
   367
		sqlite3_bind_null(session->log, 4);
vb@0
   368
	do {
vb@46
   369
		result = sqlite3_step(session->log);
vb@0
   370
		assert(result == SQLITE_DONE || result == SQLITE_BUSY);
vb@0
   371
		if (result != SQLITE_DONE && result != SQLITE_BUSY)
vb@0
   372
			status = PEP_UNKNOWN_ERROR;
vb@0
   373
	} while (result == SQLITE_BUSY);
vb@46
   374
	sqlite3_reset(session->log);
vb@0
   375
vb@0
   376
	return status;
vb@0
   377
}
vb@0
   378
vb@233
   379
DYNAMIC_API PEP_STATUS trustword(
vb@0
   380
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   381
            char **word, size_t *wsize
vb@0
   382
        )
vb@0
   383
{
vb@0
   384
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   385
	int result;
vb@0
   386
vb@46
   387
	assert(session);
vb@0
   388
	assert(word);
vb@0
   389
	assert(wsize);
vb@0
   390
vb@191
   391
    if (!(session && word && wsize))
vb@191
   392
        return PEP_ILLEGAL_VALUE;
vb@191
   393
vb@0
   394
	*word = NULL;
vb@0
   395
	*wsize = 0;
vb@0
   396
vb@0
   397
	if (lang == NULL)
vb@0
   398
		lang = "en";
vb@0
   399
vb@0
   400
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   401
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   402
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   403
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   404
	assert(lang[2] == 0);
vb@0
   405
vb@233
   406
	sqlite3_reset(session->trustword);
vb@233
   407
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
vb@233
   408
	sqlite3_bind_int(session->trustword, 2, value);
vb@0
   409
vb@233
   410
	result = sqlite3_step(session->trustword);
vb@0
   411
	if (result == SQLITE_ROW) {
vb@233
   412
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
   413
                    1));
vb@0
   414
		if (*word)
vb@233
   415
            *wsize = sqlite3_column_bytes(session->trustword, 1);
vb@0
   416
		else
vb@251
   417
			status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   418
	} else
vb@251
   419
		status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   420
vb@233
   421
	sqlite3_reset(session->trustword);
vb@0
   422
	return status;
vb@0
   423
}
vb@0
   424
vb@233
   425
DYNAMIC_API PEP_STATUS trustwords(
vb@0
   426
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   427
        char **words, size_t *wsize, int max_words
vb@0
   428
    )
vb@0
   429
{
vb@0
   430
	const char *source = fingerprint;
vb@109
   431
	char *buffer;
vb@125
   432
	char *dest;
vb@0
   433
	size_t fsize;
vb@0
   434
    PEP_STATUS _status;
vb@0
   435
vb@0
   436
	assert(session);
vb@0
   437
	assert(fingerprint);
vb@0
   438
	assert(words);
vb@0
   439
	assert(wsize);
vb@0
   440
	assert(max_words >= 0);
vb@0
   441
vb@191
   442
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
   443
        return PEP_ILLEGAL_VALUE;
vb@191
   444
vb@0
   445
	*words = NULL;
vb@0
   446
	*wsize = 0;
vb@0
   447
vb@251
   448
    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
   449
    assert(buffer);
vb@0
   450
    if (buffer == NULL)
vb@0
   451
        return PEP_OUT_OF_MEMORY;
vb@125
   452
    dest = buffer;
vb@0
   453
vb@0
   454
	fsize = strlen(fingerprint);
vb@0
   455
vb@339
   456
	if (!lang || !lang[0])
vb@0
   457
		lang = "en";
vb@0
   458
vb@0
   459
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   460
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   461
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   462
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   463
	assert(lang[2] == 0);
vb@0
   464
vb@0
   465
	int n_words = 0;
vb@0
   466
	while (source < fingerprint + fsize) {
vb@0
   467
		uint16_t value;
vb@0
   468
		char *word;
vb@0
   469
		size_t _wsize;
vb@0
   470
		int j;
vb@0
   471
vb@0
   472
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
vb@0
   473
			if (*source >= 'a' && *source <= 'f')
vb@0
   474
				value += (*source - 'a' + 10) << (3 - j++) * 4;
vb@0
   475
			else if (*source >= 'A' && *source <= 'F')
vb@0
   476
				value += (*source - 'A' + 10) << (3 - j++) * 4;
vb@0
   477
			else if (*source >= '0' && *source <= '9')
vb@0
   478
				value += (*source - '0') << (3 - j++) * 4;
vb@0
   479
			
vb@0
   480
			source++;
vb@0
   481
		}
vb@0
   482
vb@233
   483
		_status = trustword(session, value, lang, &word, &_wsize);
vb@0
   484
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   485
            free(buffer);
vb@0
   486
            return PEP_OUT_OF_MEMORY;
vb@0
   487
        }
vb@0
   488
		if (word == NULL) {
vb@0
   489
            free(buffer);
vb@251
   490
			return PEP_TRUSTWORD_NOT_FOUND;
vb@0
   491
        }
vb@0
   492
vb@251
   493
		if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
vb@0
   494
			strncpy(dest, word, _wsize);
vb@0
   495
            free(word);
vb@0
   496
			dest += _wsize;
vb@0
   497
		}
vb@0
   498
		else {
vb@0
   499
            free(word);
vb@0
   500
			break; // buffer full
vb@0
   501
        }
vb@0
   502
vb@0
   503
		if (source < fingerprint + fsize
vb@251
   504
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
vb@0
   505
			*dest++ = ' ';
vb@0
   506
vb@0
   507
		++n_words;
vb@0
   508
		if (max_words && n_words >= max_words)
vb@0
   509
			break;
vb@0
   510
	}
vb@0
   511
vb@0
   512
	*words = buffer;
vb@0
   513
	*wsize = dest - buffer;
vb@0
   514
	return PEP_STATUS_OK;
vb@0
   515
}
vb@0
   516
vb@0
   517
pEp_identity *new_identity(
vb@0
   518
        const char *address, const char *fpr, const char *user_id,
vb@0
   519
        const char *username
vb@0
   520
    )
vb@0
   521
{
vb@0
   522
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   523
    assert(result);
vb@0
   524
    if (result) {
vb@0
   525
        if (address) {
vb@0
   526
            result->address = strdup(address);
vb@0
   527
            assert(result->address);
vb@0
   528
            if (result->address == NULL) {
vb@0
   529
                free(result);
vb@0
   530
                return NULL;
vb@0
   531
            }
vb@0
   532
            result->address_size = strlen(address);
vb@0
   533
        }
vb@0
   534
        if (fpr) {
vb@0
   535
            result->fpr = strdup(fpr);
vb@0
   536
            assert(result->fpr);
vb@0
   537
            if (result->fpr == NULL) {
vb@0
   538
                free_identity(result);
vb@0
   539
                return NULL;
vb@0
   540
            }
vb@0
   541
            result->fpr_size = strlen(fpr);
vb@0
   542
        }
vb@0
   543
        if (user_id) {
vb@0
   544
            result->user_id = strdup(user_id);
vb@0
   545
            assert(result->user_id);
vb@0
   546
            if (result->user_id == NULL) {
vb@0
   547
                free_identity(result);
vb@0
   548
                return NULL;
vb@0
   549
            }
vb@0
   550
            result->user_id_size = strlen(user_id);
vb@0
   551
        }
vb@0
   552
        if (username) {
vb@0
   553
            result->username = strdup(username);
vb@0
   554
            assert(result->username);
vb@0
   555
            if (result->username == NULL) {
vb@0
   556
                free_identity(result);
vb@0
   557
                return NULL;
vb@0
   558
            }
vb@0
   559
            result->username_size = strlen(username);
vb@0
   560
        }
vb@0
   561
        result->struct_size = sizeof(pEp_identity);
vb@0
   562
    }
vb@0
   563
    return result;
vb@0
   564
}
vb@0
   565
vb@37
   566
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   567
{
vb@37
   568
    assert(src);
vb@37
   569
vb@37
   570
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
vb@37
   571
    assert(dup);
vb@37
   572
    if (dup == NULL)
vb@37
   573
        return NULL;
vb@37
   574
    
vb@37
   575
    dup->comm_type = src->comm_type;
vb@37
   576
    dup->lang[0] = src->lang[0];
vb@37
   577
    dup->lang[1] = src->lang[1];
vb@37
   578
    dup->lang[2] = 0;
vb@37
   579
    dup->me = src->me;
vb@37
   580
vb@37
   581
    return dup;
vb@37
   582
}
vb@37
   583
vb@0
   584
void free_identity(pEp_identity *identity)
vb@0
   585
{
vb@0
   586
    if (identity) {
vb@0
   587
        free(identity->address);
vb@0
   588
        free(identity->fpr);
vb@0
   589
        free(identity->user_id);
vb@0
   590
        free(identity->username);
vb@0
   591
        free(identity);
vb@0
   592
    }
vb@0
   593
}
vb@0
   594
vb@0
   595
DYNAMIC_API PEP_STATUS get_identity(
vb@0
   596
        PEP_SESSION session, const char *address,
vb@0
   597
        pEp_identity **identity
vb@0
   598
    )
vb@0
   599
{
vb@0
   600
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   601
	static pEp_identity *_identity;
vb@0
   602
	int result;
vb@0
   603
	const char *_lang;
vb@0
   604
vb@0
   605
	assert(session);
vb@0
   606
	assert(address);
vb@8
   607
    assert(address[0]);
vb@0
   608
vb@191
   609
    if (!(session && address && address[0]))
vb@191
   610
        return PEP_ILLEGAL_VALUE;
vb@191
   611
vb@46
   612
    sqlite3_reset(session->get_identity);
vb@46
   613
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
vb@0
   614
vb@46
   615
    result = sqlite3_step(session->get_identity);
vb@0
   616
	switch (result) {
vb@0
   617
	case SQLITE_ROW:
vb@0
   618
        _identity = new_identity(
vb@0
   619
                address,
vb@46
   620
                (const char *) sqlite3_column_text(session->get_identity, 0),
vb@46
   621
                (const char *) sqlite3_column_text(session->get_identity, 1),
vb@46
   622
                (const char *) sqlite3_column_text(session->get_identity, 2)
vb@0
   623
                );
vb@0
   624
        assert(_identity);
vb@0
   625
        if (_identity == NULL)
vb@0
   626
            return PEP_OUT_OF_MEMORY;
vb@0
   627
vb@46
   628
        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 3);
vb@46
   629
        _lang = (const char *) sqlite3_column_text(session->get_identity, 4);
vb@0
   630
        if (_lang && _lang[0]) {
vb@0
   631
			assert(_lang[0] >= 'a' && _lang[0] <= 'z');
vb@0
   632
			assert(_lang[1] >= 'a' && _lang[1] <= 'z');
vb@0
   633
			assert(_lang[2] == 0);
vb@0
   634
			_identity->lang[0] = _lang[0];
vb@0
   635
			_identity->lang[1] = _lang[1];
vb@0
   636
            _identity->lang[2] = 0;
vb@0
   637
		}
vb@0
   638
		*identity = _identity;
vb@0
   639
		break;
vb@0
   640
	default:
vb@0
   641
        status = PEP_CANNOT_FIND_IDENTITY;
vb@0
   642
		*identity = NULL;
vb@0
   643
	}
vb@0
   644
vb@46
   645
    sqlite3_reset(session->get_identity);
vb@0
   646
	return status;
vb@0
   647
}
vb@0
   648
vb@0
   649
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   650
        PEP_SESSION session, const pEp_identity *identity
vb@0
   651
    )
vb@0
   652
{
vb@0
   653
	int result;
vb@0
   654
vb@0
   655
	assert(session);
vb@0
   656
	assert(identity);
vb@0
   657
	assert(identity->address);
vb@0
   658
	assert(identity->fpr);
vb@0
   659
	assert(identity->user_id);
vb@0
   660
	assert(identity->username);
vb@0
   661
vb@191
   662
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   663
                identity->user_id && identity->username))
vb@191
   664
        return PEP_ILLEGAL_VALUE;
vb@191
   665
vb@46
   666
	sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   667
vb@46
   668
	sqlite3_reset(session->set_person);
vb@46
   669
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
   670
            SQLITE_STATIC);
vb@46
   671
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
   672
            SQLITE_STATIC);
vb@0
   673
	if (identity->lang[0])
vb@46
   674
        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
vb@0
   675
                SQLITE_STATIC);
vb@0
   676
	else
vb@46
   677
		sqlite3_bind_null(session->set_person, 3);
vb@46
   678
	result = sqlite3_step(session->set_person);
vb@46
   679
	sqlite3_reset(session->set_person);
vb@0
   680
	if (result != SQLITE_DONE) {
vb@46
   681
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   682
		return PEP_CANNOT_SET_PERSON;
vb@0
   683
	}
vb@0
   684
vb@46
   685
	sqlite3_reset(session->set_pgp_keypair);
vb@46
   686
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   687
            SQLITE_STATIC);
vb@46
   688
	result = sqlite3_step(session->set_pgp_keypair);
vb@46
   689
	sqlite3_reset(session->set_pgp_keypair);
vb@0
   690
	if (result != SQLITE_DONE) {
vb@46
   691
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   692
		return PEP_CANNOT_SET_PGP_KEYPAIR;
vb@0
   693
	}
vb@0
   694
vb@46
   695
	sqlite3_reset(session->set_identity);
vb@46
   696
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
   697
            SQLITE_STATIC);
vb@46
   698
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
   699
            SQLITE_STATIC);
vb@46
   700
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
   701
            SQLITE_STATIC);
vb@46
   702
	result = sqlite3_step(session->set_identity);
vb@46
   703
	sqlite3_reset(session->set_identity);
vb@0
   704
	if (result != SQLITE_DONE) {
vb@46
   705
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   706
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   707
	}
vb@0
   708
vb@46
   709
	sqlite3_reset(session->set_trust);
vb@46
   710
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
   711
            SQLITE_STATIC);
vb@46
   712
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
   713
            SQLITE_STATIC);
vb@46
   714
	sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
vb@46
   715
	result = sqlite3_step(session->set_trust);
vb@46
   716
	sqlite3_reset(session->set_trust);
vb@0
   717
	if (result != SQLITE_DONE) {
vb@46
   718
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   719
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   720
	}
vb@0
   721
vb@46
   722
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
vb@0
   723
	if (result == SQLITE_OK)
vb@0
   724
		return PEP_STATUS_OK;
vb@0
   725
	else
vb@0
   726
		return PEP_COMMIT_FAILED;
vb@0
   727
}
vb@0
   728
vb@357
   729
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
   730
        PEP_SESSION session,
vb@357
   731
        const char *fpr
vb@357
   732
    )
vb@357
   733
{
vb@357
   734
	int result;
vb@357
   735
vb@357
   736
	assert(session);
vb@357
   737
    assert(fpr && fpr[0]);
vb@357
   738
vb@357
   739
    if (!(session && fpr && fpr[0]))
vb@357
   740
        return PEP_ILLEGAL_VALUE;
vb@357
   741
vb@357
   742
	sqlite3_reset(session->mark_compromized);
vb@357
   743
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
   744
            SQLITE_STATIC);
vb@357
   745
    result = sqlite3_step(session->mark_compromized);
vb@357
   746
	sqlite3_reset(session->mark_compromized);
vb@357
   747
vb@357
   748
    if (result != SQLITE_DONE)
vb@357
   749
        return PEP_CANNOT_SET_IDENTITY;
vb@357
   750
vb@357
   751
    return PEP_STATUS_OK;
vb@357
   752
}
vb@357
   753
vb@0
   754
void pEp_free(void *p)
vb@0
   755
{
vb@0
   756
    free(p);
vb@0
   757
}
vb@0
   758
vb@8
   759
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
   760
{
vb@8
   761
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
   762
    int result;
vb@8
   763
vb@8
   764
    assert(session);
vb@8
   765
    assert(identity);
vb@8
   766
    assert(identity->user_id);
vb@8
   767
    assert(identity->user_id[0]);
vb@8
   768
    assert(identity->fpr);
vb@8
   769
    assert(identity->fpr[0]);
vb@8
   770
vb@191
   771
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
   772
                identity->fpr && identity->fpr[0]))
vb@191
   773
        return PEP_ILLEGAL_VALUE;
vb@191
   774
vb@8
   775
    identity->comm_type = PEP_ct_unknown;
vb@8
   776
vb@46
   777
    sqlite3_reset(session->get_trust);
vb@46
   778
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
vb@46
   779
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
   780
vb@46
   781
    result = sqlite3_step(session->get_trust);
vb@8
   782
    switch (result) {
vb@8
   783
    case SQLITE_ROW: {
vb@259
   784
        const char * user_id = (const char *) sqlite3_column_text(session->get_trust, 0);
vb@259
   785
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 1);
vb@8
   786
vb@8
   787
        if (strcmp(user_id, identity->user_id) != 0) {
vb@8
   788
            free(identity->user_id);
vb@8
   789
            identity->user_id = strdup(user_id);
vb@8
   790
            assert(identity->user_id);
vb@8
   791
            if (identity->user_id == NULL)
vb@8
   792
                return PEP_OUT_OF_MEMORY;
vb@8
   793
        }
vb@8
   794
        identity->comm_type = comm_type;
vb@8
   795
        break;
vb@8
   796
    }
vb@8
   797
 
vb@8
   798
    default:
vb@8
   799
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
   800
    }
vb@8
   801
vb@46
   802
    sqlite3_reset(session->get_trust);
vb@8
   803
    return status;
vb@9
   804
}
vb@9
   805
vb@251
   806
DYNAMIC_API PEP_STATUS least_trust(
vb@251
   807
        PEP_SESSION session,
vb@251
   808
        const char *fpr,
vb@251
   809
        PEP_comm_type *comm_type
vb@251
   810
    )
vb@251
   811
{
vb@251
   812
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
   813
    int result;
vb@251
   814
vb@251
   815
    assert(session);
vb@251
   816
    assert(fpr);
vb@251
   817
    assert(comm_type);
vb@251
   818
vb@259
   819
    *comm_type = PEP_ct_unknown;
vb@259
   820
vb@251
   821
    if (!(session && fpr && comm_type))
vb@251
   822
        return PEP_ILLEGAL_VALUE;
vb@251
   823
vb@251
   824
    sqlite3_reset(session->least_trust);
vb@251
   825
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
   826
vb@251
   827
    result = sqlite3_step(session->least_trust);
vb@251
   828
    switch (result) {
vb@251
   829
        case SQLITE_ROW: {
vb@259
   830
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
   831
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
   832
            break;
vb@251
   833
        }
vb@251
   834
        default:
vb@251
   835
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
   836
    }
vb@251
   837
vb@251
   838
    sqlite3_reset(session->least_trust);
vb@251
   839
    return status;
vb@251
   840
}
vb@251
   841
vb@24
   842
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
   843
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
   844
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   845
    )
vb@24
   846
{
vb@191
   847
    assert(session);
vb@191
   848
    assert(ctext);
vb@191
   849
    assert(csize);
vb@191
   850
    assert(ptext);
vb@191
   851
    assert(psize);
vb@191
   852
    assert(keylist);
vb@191
   853
vb@191
   854
    if (!(session && ctext && csize && ptext && psize && keylist && keylist))
vb@191
   855
        return PEP_ILLEGAL_VALUE;
vb@191
   856
vb@46
   857
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
vb@24
   858
}
vb@24
   859
vb@24
   860
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
   861
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
   862
    size_t psize, char **ctext, size_t *csize
vb@24
   863
    )
vb@24
   864
{
vb@191
   865
    assert(session);
vb@191
   866
    assert(keylist);
vb@191
   867
    assert(ptext);
vb@191
   868
    assert(psize);
vb@191
   869
    assert(ctext);
vb@191
   870
    assert(csize);
vb@191
   871
vb@191
   872
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
   873
        return PEP_ILLEGAL_VALUE;
vb@191
   874
vb@46
   875
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
vb@24
   876
}
vb@24
   877
vb@24
   878
DYNAMIC_API PEP_STATUS verify_text(
vb@24
   879
    PEP_SESSION session, const char *text, size_t size,
vb@24
   880
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   881
    )
vb@24
   882
{
vb@191
   883
    assert(session);
vb@191
   884
    assert(text);
vb@191
   885
    assert(size);
vb@191
   886
    assert(signature);
vb@191
   887
    assert(sig_size);
vb@191
   888
    assert(keylist);
vb@191
   889
vb@191
   890
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
   891
        return PEP_ILLEGAL_VALUE;
vb@191
   892
vb@46
   893
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
vb@24
   894
}
vb@24
   895
vb@24
   896
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
   897
{
vb@191
   898
    assert(session);
vb@191
   899
    assert(fpr);
vb@191
   900
vb@191
   901
    if (!(session && fpr))
vb@191
   902
        return PEP_ILLEGAL_VALUE;
vb@191
   903
vb@46
   904
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
   905
}
vb@24
   906
vb@24
   907
DYNAMIC_API PEP_STATUS export_key(
vb@46
   908
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
   909
    )
vb@24
   910
{
vb@191
   911
    assert(session);
vb@191
   912
    assert(fpr);
vb@191
   913
    assert(key_data);
vb@191
   914
    assert(size);
vb@191
   915
vb@191
   916
    if (!(session && fpr && key_data && size))
vb@191
   917
        return PEP_ILLEGAL_VALUE;
vb@191
   918
vb@46
   919
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
vb@24
   920
}
vb@24
   921
vb@24
   922
DYNAMIC_API PEP_STATUS find_keys(
vb@46
   923
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
   924
    )
vb@24
   925
{
vb@191
   926
    assert(session);
vb@191
   927
    assert(pattern);
vb@191
   928
    assert(keylist);
vb@191
   929
vb@191
   930
    if (!(session && pattern && keylist))
vb@191
   931
        return PEP_ILLEGAL_VALUE;
vb@191
   932
vb@46
   933
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
vb@24
   934
}
vb@24
   935
vb@24
   936
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
   937
        PEP_SESSION session, pEp_identity *identity
vb@24
   938
    )
vb@24
   939
{
vb@191
   940
    assert(session);
vb@191
   941
    assert(identity);
vb@191
   942
    assert(identity->address);
vb@298
   943
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
   944
    assert(identity->username);
vb@191
   945
vb@298
   946
    if (!(session && identity && identity->address &&
vb@298
   947
        (identity->fpr == NULL || identity->fpr[0] == 0) && identity->username))
vb@191
   948
        return PEP_ILLEGAL_VALUE;
vb@191
   949
vb@46
   950
    return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
vb@24
   951
}
vb@24
   952
vb@9
   953
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
   954
        PEP_SESSION session,
vb@46
   955
        const char *fpr,
vb@46
   956
        PEP_comm_type *comm_type
vb@9
   957
    )
vb@9
   958
{
vb@191
   959
    assert(session);
vb@191
   960
    assert(fpr);
vb@191
   961
    assert(comm_type);
vb@191
   962
vb@191
   963
    if (!(session && fpr && comm_type))
vb@191
   964
        return PEP_ILLEGAL_VALUE;
vb@191
   965
vb@46
   966
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
vb@24
   967
}
vb@9
   968
vb@24
   969
DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
vb@24
   970
{
vb@191
   971
    assert(session);
vb@191
   972
    assert(key_data);
vb@191
   973
vb@191
   974
    if (!(session && key_data))
vb@191
   975
        return PEP_ILLEGAL_VALUE;
vb@191
   976
vb@46
   977
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size);
vb@24
   978
}
vb@9
   979
vb@24
   980
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
   981
{
vb@191
   982
    assert(session);
vb@191
   983
    assert(pattern);
vb@191
   984
vb@191
   985
    if (!(session && pattern))
vb@191
   986
        return PEP_ILLEGAL_VALUE;
vb@191
   987
vb@46
   988
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
   989
}
vb@9
   990
vb@24
   991
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
   992
{
vb@191
   993
    assert(session);
vb@191
   994
    assert(pattern);
vb@191
   995
vb@191
   996
    if (!(session && pattern))
vb@191
   997
        return PEP_ILLEGAL_VALUE;
vb@191
   998
vb@46
   999
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1000
}
vb@196
  1001
vb@201
  1002
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1003
        PEP_SESSION session,
vb@201
  1004
        const char *fpr,
vb@201
  1005
        const timestamp *ts
vb@201
  1006
    )
vb@196
  1007
{
vb@196
  1008
    assert(session);
vb@200
  1009
    assert(fpr);
vb@196
  1010
vb@200
  1011
    if (!(session && fpr))
vb@196
  1012
        return PEP_ILLEGAL_VALUE;
vb@196
  1013
vb@201
  1014
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1015
}
vb@196
  1016
vb@211
  1017
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1018
        PEP_SESSION session,
vb@211
  1019
        const char *fpr,
vb@211
  1020
        const char *reason
vb@211
  1021
    )
vb@197
  1022
{
vb@197
  1023
    assert(session);
vb@200
  1024
    assert(fpr);
vb@197
  1025
vb@200
  1026
    if (!(session && fpr))
vb@197
  1027
        return PEP_ILLEGAL_VALUE;
vb@197
  1028
vb@214
  1029
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1030
            reason);
vb@197
  1031
}
vb@197
  1032
vb@214
  1033
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1034
        PEP_SESSION session,
vb@214
  1035
        const char *fpr,
vb@214
  1036
        bool *expired
vb@214
  1037
    )
vb@214
  1038
{
vb@214
  1039
    assert(session);
vb@214
  1040
    assert(fpr);
vb@214
  1041
    assert(expired);
vb@214
  1042
vb@214
  1043
    if (!(session && fpr && expired))
vb@214
  1044
        return PEP_ILLEGAL_VALUE;
vb@214
  1045
vb@214
  1046
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
vb@214
  1047
            expired);
vb@214
  1048
}
vb@214
  1049
vb@450
  1050
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1051
{
vb@450
  1052
    assert(str2);
vb@450
  1053
vb@450
  1054
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1055
    size_t len2 = strlen(str2);
vb@450
  1056
    size_t len = len1 + len2 + 3;
vb@450
  1057
    char * result = realloc(str1, len + 1);
vb@450
  1058
vb@450
  1059
    if (result) {
vb@450
  1060
        result[len1] = '"';
vb@450
  1061
        strcpy(result + len1 + 1, str2);
vb@450
  1062
        _clean_log_value(result + len1 + 1);
vb@450
  1063
        result[len - 2] = '"';
vb@450
  1064
        result[len - 1] = delim;
vb@450
  1065
        result[len] = 0;
vb@450
  1066
    }
vb@450
  1067
    else {
vb@450
  1068
        free(str1);
vb@450
  1069
    }
vb@450
  1070
vb@450
  1071
    return result;
vb@450
  1072
}
vb@450
  1073
vb@450
  1074
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1075
        PEP_SESSION session,
vb@450
  1076
        int maxlines,
vb@450
  1077
        char **logdata
vb@450
  1078
    )
vb@450
  1079
{
vb@450
  1080
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1081
    char *_logdata= NULL;
vb@450
  1082
vb@450
  1083
    assert(session);
vb@450
  1084
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1085
    assert(logdata);
vb@450
  1086
vb@450
  1087
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1088
            CRASHDUMP_MAX_LINES))
vb@450
  1089
        return PEP_ILLEGAL_VALUE;
vb@450
  1090
vb@450
  1091
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@450
  1092
    const char *title;
vb@450
  1093
    const char *entity;
vb@450
  1094
    const char *desc;
vb@450
  1095
    const char *comment;
vb@450
  1096
vb@450
  1097
    sqlite3_reset(session->crashdump);
vb@450
  1098
	sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1099
vb@450
  1100
    int result;
vb@450
  1101
vb@450
  1102
    do {
vb@450
  1103
        result = sqlite3_step(session->crashdump);
vb@450
  1104
        switch (result) {
vb@450
  1105
        case SQLITE_ROW:
vb@450
  1106
            title   = (const char *) sqlite3_column_text(session->crashdump, 0);
vb@450
  1107
            entity  = (const char *) sqlite3_column_text(session->crashdump, 1);
vb@450
  1108
            desc    = (const char *) sqlite3_column_text(session->crashdump, 2);
vb@450
  1109
            comment = (const char *) sqlite3_column_text(session->crashdump, 3);
vb@450
  1110
vb@450
  1111
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1112
            if (_logdata == NULL)
vb@450
  1113
                goto enomem;
vb@450
  1114
vb@450
  1115
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1116
            if (_logdata == NULL)
vb@450
  1117
                goto enomem;
vb@450
  1118
vb@450
  1119
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1120
            if (_logdata == NULL)
vb@450
  1121
                goto enomem;
vb@450
  1122
vb@450
  1123
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1124
            if (_logdata == NULL)
vb@450
  1125
                goto enomem;
vb@450
  1126
vb@450
  1127
            break;
vb@450
  1128
vb@450
  1129
        case SQLITE_DONE:
vb@450
  1130
            break;
vb@450
  1131
vb@450
  1132
        default:
vb@450
  1133
            status = PEP_UNKNOWN_ERROR;
vb@450
  1134
            result = SQLITE_DONE;
vb@450
  1135
        }
vb@450
  1136
    } while (result != SQLITE_DONE);
vb@450
  1137
vb@450
  1138
    sqlite3_reset(session->crashdump);
vb@450
  1139
    if (status == PEP_STATUS_OK)
vb@450
  1140
        *logdata = _logdata;
vb@450
  1141
vb@450
  1142
    goto the_end;
vb@450
  1143
vb@450
  1144
enomem:
vb@450
  1145
    status = PEP_OUT_OF_MEMORY;
vb@450
  1146
vb@450
  1147
the_end:
vb@450
  1148
    return status;
vb@450
  1149
}
vb@450
  1150