You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

827 lines
24 KiB

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