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