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