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