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