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