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