p≡p engine
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.

781 lines
20 KiB

7 years ago
7 years ago
2 years ago
7 years ago
7 years ago
7 years ago
7 years ago
2 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
3 years ago
3 years ago
7 years ago
7 years ago
7 years ago
7 years ago
3 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
3 years ago
7 years ago
7 years ago
3 years ago
7 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
3 years ago
3 years ago
6 years ago
6 years ago
2 years ago
7 years ago
7 years ago
3 years ago
3 years ago
3 years ago
  1. /**
  2. * @file pEp_internal.h
  3. * @brief pEp internal structs, functions, defines, and values
  4. * @license GNU General Public License 3.0 - see LICENSE.txt
  5. */
  6. #ifndef PEP_INTERNAL_H
  7. #define PEP_INTERNAL_H
  8. #if defined __has_include
  9. # if __has_include ("commit_hash.h")
  10. # include "commit_hash.h"
  11. # else
  12. # define PEP_CURRENT_COMMIT_HASH="DUMMY_COMMIT_HASH_ERROR"
  13. # endif
  14. #else
  15. // Well, we tried. Better hope the git setup is correct.
  16. // IF NOT, or you aren't using git, please copy the commit_hash.h file from
  17. // templates/ into src/ if this include fails.
  18. # include "commit_hash.h"
  19. #endif
  20. #include "commit_hash.h" // We need this everywhere. So.
  21. // maximum attachment size to import as key 25MB, maximum of 20 attachments
  22. #define MAX_KEY_SIZE (25 * 1024 * 1024)
  23. #define MAX_KEYS_TO_IMPORT 20
  24. #define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
  25. // this is 20 trustwords with 79 chars max
  26. #define MAX_TRUSTWORDS_SPACE (20 * 80)
  27. // XML parameters string
  28. #define PARMS_MAX 32768
  29. // maximum busy wait time in ms
  30. #define BUSY_WAIT_TIME 5000
  31. // default keyserver
  32. #ifndef DEFAULT_KEYSERVER
  33. #define DEFAULT_KEYSERVER "hkps://keys.openpgp.org"
  34. #endif
  35. // crashdump constants
  36. #ifndef CRASHDUMP_DEFAULT_LINES
  37. #define CRASHDUMP_DEFAULT_LINES 100
  38. #endif
  39. #define CRASHDUMP_MAX_LINES 32767
  40. // p≡p full string, NUL-terminated
  41. #ifndef PEP_SUBJ_STRING
  42. #define PEP_SUBJ_STRING {0x70,0xE2,0x89,0xA1,0x70,0x00}
  43. #define PEP_SUBJ_BYTELEN 5
  44. #endif
  45. #ifndef PEP_SUBJ_KEY
  46. #define PEP_SUBJ_KEY "Subject: "
  47. #define PEP_SUBJ_KEY_LC "subject: "
  48. #define PEP_SUBJ_KEY_LEN 9
  49. #endif
  50. #ifndef PEP_MSG_WRAP_KEY
  51. #define PEP_MSG_WRAP_KEY "pEp-Wrapped-Message-Info: "
  52. #define PEP_MSG_WRAP_KEY_LC "pep-wrapped-message-info: "
  53. #define PEP_MSG_WRAP_KEY_LEN 26
  54. #endif
  55. #ifndef X_PEP_MSG_WRAP_KEY
  56. #define X_PEP_MSG_WRAP_KEY "X-pEp-Wrapped-Message-Info"
  57. #endif
  58. #ifndef X_PEP_SNDR_FPR_KEY
  59. #define X_PEP_SNDR_FPR_KEY "X-pEp-Sender-FPR"
  60. #endif
  61. #include "platform.h"
  62. #ifdef WIN32
  63. #define KEYS_DB windoze_keys_db()
  64. #define LOCAL_DB windoze_local_db()
  65. #define SYSTEM_DB windoze_system_db()
  66. #else // UNIX
  67. #define _POSIX_C_SOURCE 200809L
  68. #include <dlfcn.h>
  69. #ifdef NDEBUG
  70. #define LOCAL_DB unix_local_db()
  71. #else
  72. #define LOCAL_DB unix_local_db(false)
  73. #define LOCAL_DB_RESET unix_local_db(true)
  74. #endif
  75. #ifdef ANDROID
  76. #define SYSTEM_DB android_system_db()
  77. #else
  78. #define SYSTEM_DB unix_system_db()
  79. #endif
  80. #endif
  81. #include <locale.h>
  82. #include <stdlib.h>
  83. #include <string.h>
  84. #include <assert.h>
  85. #include <stdio.h>
  86. #include <ctype.h>
  87. #include <math.h>
  88. #ifdef SQLITE3_FROM_OS
  89. #include <sqlite3.h>
  90. #else
  91. #include "sqlite3.h"
  92. #endif
  93. #include "pEpEngine.h"
  94. // If not specified, build for Sequoia
  95. #ifndef USE_SEQUOIA
  96. #define USE_SEQUOIA
  97. #endif
  98. #if defined(USE_SEQUOIA)
  99. #include "pgp_sequoia_internal.h"
  100. #endif
  101. #include "keymanagement.h"
  102. #include "cryptotech.h"
  103. #include "transport.h"
  104. #include "sync_api.h"
  105. #include "Sync_func.h"
  106. #include "key_reset.h"
  107. #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
  108. struct _pEpSession;
  109. typedef struct _pEpSession pEpSession;
  110. /**
  111. * @struct _pEpSession
  112. *
  113. * @brief TODO
  114. *
  115. */
  116. struct _pEpSession {
  117. const char *version;
  118. messageToSend_t messageToSend;
  119. #if defined(USE_SEQUOIA)
  120. sqlite3 *key_db;
  121. struct {
  122. sqlite3_stmt *begin_transaction;
  123. sqlite3_stmt *commit_transaction;
  124. sqlite3_stmt *rollback_transaction;
  125. sqlite3_stmt *cert_find;
  126. sqlite3_stmt *tsk_find;
  127. sqlite3_stmt *cert_find_by_keyid;
  128. sqlite3_stmt *tsk_find_by_keyid;
  129. sqlite3_stmt *cert_find_by_email;
  130. sqlite3_stmt *tsk_find_by_email;
  131. sqlite3_stmt *cert_all;
  132. sqlite3_stmt *tsk_all;
  133. sqlite3_stmt *cert_save_insert_primary;
  134. sqlite3_stmt *cert_save_insert_subkeys;
  135. sqlite3_stmt *cert_save_insert_userids;
  136. sqlite3_stmt *delete_keypair;
  137. } sq_sql;
  138. pgp_policy_t policy;
  139. #endif
  140. PEP_cryptotech_t *cryptotech;
  141. PEP_CIPHER_SUITE cipher_suite;
  142. char* curr_passphrase;
  143. bool new_key_pass_enable;
  144. char* generation_passphrase;
  145. PEP_transport_t *transports;
  146. sqlite3 *db;
  147. sqlite3 *system_db;
  148. sqlite3_stmt *log;
  149. sqlite3_stmt *trustword;
  150. sqlite3_stmt *get_identity;
  151. sqlite3_stmt *get_identity_without_trust_check;
  152. sqlite3_stmt *get_identities_by_address;
  153. sqlite3_stmt *get_identities_by_userid;
  154. sqlite3_stmt *get_identities_by_main_key_id;
  155. sqlite3_stmt *replace_identities_fpr;
  156. sqlite3_stmt *replace_main_user_fpr;
  157. sqlite3_stmt *replace_main_user_fpr_if_equal;
  158. sqlite3_stmt *get_main_user_fpr;
  159. sqlite3_stmt *refresh_userid_default_key;
  160. sqlite3_stmt *delete_key;
  161. sqlite3_stmt *remove_fpr_as_identity_default;
  162. sqlite3_stmt *remove_fpr_as_user_default;
  163. sqlite3_stmt *set_person;
  164. sqlite3_stmt *update_person;
  165. sqlite3_stmt *delete_person;
  166. sqlite3_stmt *exists_person;
  167. sqlite3_stmt *set_as_pEp_user;
  168. sqlite3_stmt *is_pEp_user;
  169. sqlite3_stmt *upgrade_pEp_version_by_user_id;
  170. sqlite3_stmt *add_into_social_graph;
  171. sqlite3_stmt *get_own_address_binding_from_contact;
  172. sqlite3_stmt *set_revoke_contact_as_notified;
  173. sqlite3_stmt *get_contacted_ids_from_revoke_fpr;
  174. sqlite3_stmt *was_id_for_revoke_contacted;
  175. sqlite3_stmt *has_id_contacted_address;
  176. sqlite3_stmt *get_last_contacted;
  177. // sqlite3_stmt *set_device_group;
  178. // sqlite3_stmt *get_device_group;
  179. sqlite3_stmt *set_pgp_keypair;
  180. sqlite3_stmt *set_identity_entry;
  181. sqlite3_stmt *update_identity_entry;
  182. sqlite3_stmt *exists_identity_entry;
  183. sqlite3_stmt *set_identity_flags;
  184. sqlite3_stmt *unset_identity_flags;
  185. sqlite3_stmt *set_ident_enc_format;
  186. sqlite3_stmt *set_pEp_version;
  187. sqlite3_stmt *clear_trust_info;
  188. sqlite3_stmt *set_trust;
  189. sqlite3_stmt *update_trust;
  190. sqlite3_stmt *exists_trust_entry;
  191. sqlite3_stmt *update_trust_to_pEp;
  192. sqlite3_stmt *update_trust_for_fpr;
  193. sqlite3_stmt *get_trust;
  194. sqlite3_stmt *get_trust_by_userid;
  195. sqlite3_stmt *least_trust;
  196. sqlite3_stmt *mark_compromised;
  197. sqlite3_stmt *reset_trust;
  198. sqlite3_stmt *crashdump;
  199. sqlite3_stmt *languagelist;
  200. sqlite3_stmt *i18n_token;
  201. sqlite3_stmt *replace_userid;
  202. // blacklist
  203. sqlite3_stmt *blacklist_add;
  204. sqlite3_stmt *blacklist_delete;
  205. sqlite3_stmt *blacklist_is_listed;
  206. sqlite3_stmt *blacklist_retrieve;
  207. // Keys
  208. sqlite3_stmt *own_key_is_listed;
  209. sqlite3_stmt *is_own_address;
  210. sqlite3_stmt *own_identities_retrieve;
  211. sqlite3_stmt *own_keys_retrieve;
  212. sqlite3_stmt *key_identities_retrieve;
  213. sqlite3_stmt *get_user_default_key;
  214. sqlite3_stmt *get_all_keys_for_user;
  215. sqlite3_stmt *get_default_own_userid;
  216. // sqlite3_stmt *set_own_key;
  217. // sequence value
  218. sqlite3_stmt *sequence_value1;
  219. sqlite3_stmt *sequence_value2;
  220. // revoked keys
  221. sqlite3_stmt *set_revoked;
  222. sqlite3_stmt *get_revoked;
  223. sqlite3_stmt *get_replacement_fpr;
  224. // mistrusted
  225. sqlite3_stmt* add_mistrusted_key;
  226. sqlite3_stmt* is_mistrusted_key;
  227. sqlite3_stmt* delete_mistrusted_key;
  228. // aliases
  229. sqlite3_stmt *get_userid_alias_default;
  230. sqlite3_stmt *add_userid_alias;
  231. // callbacks
  232. examine_identity_t examine_identity;
  233. void *examine_management;
  234. notifyHandshake_t notifyHandshake;
  235. inject_sync_event_t inject_sync_event;
  236. retrieve_next_sync_event_t retrieve_next_sync_event;
  237. ensure_passphrase_t ensure_passphrase;
  238. // pEp Sync
  239. void *sync_management;
  240. void *sync_obj;
  241. struct Sync_state_s sync_state;
  242. // void* sync_state_payload;
  243. // char sync_uuid[37];
  244. // time_t LastCannotDecrypt;
  245. // time_t LastUpdateRequest;
  246. // runtime config
  247. bool passive_mode;
  248. bool unencrypted_subject;
  249. bool service_log;
  250. #ifndef NDEBUG
  251. int debug_color;
  252. #endif
  253. };
  254. /**
  255. * <!-- init_transport_system() -->
  256. *
  257. * @brief TODO
  258. *
  259. * @param[in] session PEP_SESSION
  260. * @param[in] in_first bool
  261. *
  262. */
  263. PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
  264. /**
  265. * <!-- release_transport_system() -->
  266. *
  267. * @brief TODO
  268. *
  269. * @param[in] session PEP_SESSION
  270. * @param[in] out_last bool
  271. *
  272. */
  273. void release_transport_system(PEP_SESSION session, bool out_last);
  274. /**
  275. * @internal
  276. *
  277. * <!-- encrypt_only() -->
  278. *
  279. * @brief TODO
  280. *
  281. * @param[in] session PEP_SESSION
  282. * @param[in] keylist const stringlist_t*
  283. * @param[in] ptext const char*
  284. * @param[in] psize size_t
  285. * @param[in] ctext char**
  286. * @param[in] csize size_t*
  287. *
  288. * @warning NOT to be exposed to the outside!!!!!
  289. */
  290. PEP_STATUS encrypt_only(
  291. PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  292. size_t psize, char **ctext, size_t *csize
  293. );
  294. /**
  295. * <!-- decorate_message() -->
  296. *
  297. * @brief TODO
  298. *
  299. * @param[in] msg message*
  300. * @param[in] rating PEP_rating
  301. * @param[in] keylist stringlist_t*
  302. * @param[in] add_version bool
  303. * @param[in] clobber bool
  304. *
  305. */
  306. void decorate_message(
  307. PEP_SESSION session,
  308. message *msg,
  309. PEP_rating rating,
  310. stringlist_t *keylist,
  311. bool add_version,
  312. bool clobber);
  313. #if defined(NDEBUG) || defined(NOLOG)
  314. #define DEBUG_LOG(TITLE, ENTITY, DESC)
  315. #else
  316. #ifdef ANDROID
  317. #include <android/log.h>
  318. #define LOG_MORE(...) __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
  319. #else
  320. #include <stdio.h>
  321. #define LOG_MORE(...) fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
  322. #endif
  323. #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
  324. log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
  325. LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
  326. }
  327. #endif
  328. /**
  329. * @enum normalize_hex_res_t
  330. *
  331. * @brief TODO
  332. *
  333. */
  334. typedef enum _normalize_hex_rest_t {
  335. accept_hex,
  336. ignore_hex,
  337. reject_hex
  338. } normalize_hex_res_t;
  339. /**
  340. * <!-- _normalize_hex() -->
  341. *
  342. * @brief TODO
  343. *
  344. * @param[in] hex char*
  345. *
  346. */
  347. static inline normalize_hex_res_t _normalize_hex(char *hex)
  348. {
  349. if (*hex >= '0' && *hex <= '9')
  350. return accept_hex;
  351. if (*hex >= 'A' && *hex <= 'F') {
  352. *hex += 'a' - 'A';
  353. return accept_hex;
  354. }
  355. if (*hex >= 'a' && *hex <= 'f')
  356. return accept_hex;
  357. if (*hex == ' ')
  358. return ignore_hex;
  359. return reject_hex;
  360. }
  361. // Space tolerant and case insensitive fingerprint string compare
  362. /**
  363. * <!-- _compare_fprs() -->
  364. *
  365. * @brief TODO
  366. *
  367. * @param[in] fpra const char*
  368. * @param[in] fpras size_t
  369. * @param[in] fprb const char*
  370. * @param[in] fprbs size_t
  371. * @param[in] comparison int*
  372. *
  373. */
  374. static inline PEP_STATUS _compare_fprs(
  375. const char* fpra,
  376. size_t fpras,
  377. const char* fprb,
  378. size_t fprbs,
  379. int* comparison)
  380. {
  381. size_t ai = 0;
  382. size_t bi = 0;
  383. size_t significant = 0;
  384. int _comparison = 0;
  385. const int _FULL_FINGERPRINT_LENGTH = 40;
  386. // First compare every non-ignored chars until an end is reached
  387. while(ai < fpras && bi < fprbs)
  388. {
  389. char fprac = fpra[ai];
  390. char fprbc = fprb[bi];
  391. normalize_hex_res_t fprah = _normalize_hex(&fprac);
  392. normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
  393. if(fprah == reject_hex || fprbh == reject_hex)
  394. return PEP_ILLEGAL_VALUE;
  395. if ( fprah == ignore_hex )
  396. {
  397. ai++;
  398. }
  399. else if ( fprbh == ignore_hex )
  400. {
  401. bi++;
  402. }
  403. else
  404. {
  405. if(fprac != fprbc && _comparison == 0 )
  406. {
  407. _comparison = fprac > fprbc ? 1 : -1;
  408. }
  409. significant++;
  410. ai++;
  411. bi++;
  412. }
  413. }
  414. // Bail out if we didn't got enough significnt chars
  415. if (significant != _FULL_FINGERPRINT_LENGTH )
  416. return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
  417. // Then purge remaining chars, all must be ignored chars
  418. while ( ai < fpras )
  419. {
  420. char fprac = fpra[ai];
  421. normalize_hex_res_t fprah = _normalize_hex(&fprac);
  422. if( fprah == reject_hex )
  423. return PEP_ILLEGAL_VALUE;
  424. if ( fprah != ignore_hex )
  425. return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
  426. ai++;
  427. }
  428. while ( bi < fprbs )
  429. {
  430. char fprbc = fprb[bi];
  431. normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
  432. if( fprbh == reject_hex )
  433. return PEP_ILLEGAL_VALUE;
  434. if ( fprbh != ignore_hex )
  435. return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
  436. bi++;
  437. }
  438. *comparison = _comparison;
  439. return PEP_STATUS_OK;
  440. }
  441. /**
  442. * <!-- _same_fpr() -->
  443. *
  444. * @brief TODO
  445. *
  446. * @param[in] fpra const char*
  447. * @param[in] fpras size_t
  448. * @param[in] fprb const char*
  449. * @param[in] fprbs size_t
  450. *
  451. */
  452. static inline int _same_fpr(
  453. const char* fpra,
  454. size_t fpras,
  455. const char* fprb,
  456. size_t fprbs
  457. )
  458. {
  459. // illegal values are ignored, and considered not same.
  460. int comparison = 1;
  461. _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
  462. return comparison == 0;
  463. }
  464. // size is the length of the bytestr that's coming in. This is really only intended
  465. // for comparing two full strings. If charstr's length is different from bytestr_size,
  466. // we'll return a non-zero value.
  467. /**
  468. * @internal
  469. *
  470. * <!-- _unsigned_signed_strcmp() -->
  471. *
  472. * @brief Compare an unsigned sequence of bytes with the input string.
  473. * This is really only intended for comparing two full strings.
  474. * If charstr's length is different from bytestr_size,
  475. * we'll return a non-zero value.
  476. *
  477. * @param[in] bytestr byte string (unsigned char data)
  478. * @param[in] charstr character string (NUL-terminated)
  479. * @param[in] bytestr_size length of byte string passed in
  480. *
  481. * @retval 0 if equal
  482. * @retval non-zero if not equal
  483. *
  484. */
  485. static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
  486. int charstr_len = strlen(charstr);
  487. if (charstr_len != bytestr_size)
  488. return -1; // we don't actually care except that it's non-zero
  489. return memcmp(bytestr, charstr, bytestr_size);
  490. }
  491. // This is just a horrible example of C type madness. UTF-8 made me do it.
  492. /**
  493. * <!-- _pEp_subj_copy() -->
  494. *
  495. * @brief TODO
  496. *
  497. *
  498. */
  499. static inline char* _pEp_subj_copy() {
  500. #ifndef WIN32
  501. unsigned char pEpstr[] = PEP_SUBJ_STRING;
  502. void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
  503. memcpy(retval, pEpstr, PEP_SUBJ_BYTELEN);
  504. return (char*)retval;
  505. #else
  506. return strdup("pEp");
  507. #endif
  508. }
  509. /**
  510. * <!-- is_me() -->
  511. *
  512. * @brief TODO
  513. *
  514. * @param[in] session PEP_SESSION
  515. * @param[in] test_ident const pEp_identity*
  516. *
  517. */
  518. static inline bool is_me(PEP_SESSION session, const pEp_identity* test_ident) {
  519. bool retval = false;
  520. if (test_ident && test_ident->user_id) {
  521. char* def_id = NULL;
  522. get_default_own_userid(session, &def_id);
  523. if (test_ident->me ||
  524. (def_id && strcmp(def_id, test_ident->user_id) == 0)) {
  525. retval = true;
  526. }
  527. free(def_id);
  528. }
  529. return retval;
  530. }
  531. /**
  532. * <!-- pEp_version_numeric() -->
  533. *
  534. * @brief TODO
  535. *
  536. * @param[in] version_str const char*
  537. *
  538. */
  539. static inline float pEp_version_numeric(const char* version_str) {
  540. float retval = 0;
  541. if (!version_str || sscanf(version_str, "%f", &retval) != 1)
  542. return 0;
  543. return retval;
  544. }
  545. /**
  546. * <!-- pEp_version_major_minor() -->
  547. *
  548. * @brief TODO
  549. *
  550. * @param[in] version_str const char*
  551. * @param[out] major unsigned int*
  552. * @param[out] minor unsigned int*
  553. *
  554. */
  555. static inline void pEp_version_major_minor(const char* version_str, unsigned int* major, unsigned int* minor) {
  556. if (!major || !minor)
  557. return;
  558. if (!version_str || sscanf(version_str, "%u.%u", major, minor) != 2) {
  559. *major = 0;
  560. *minor = 0;
  561. }
  562. return;
  563. }
  564. /**
  565. * <!-- compare_versions() -->
  566. *
  567. * @brief TODO
  568. *
  569. * @param[in] first_maj unsigned int
  570. * @param[in] first_min unsigned int
  571. * @param[in] second_maj unsigned int
  572. * @param[in] second_min unsigned int
  573. *
  574. */
  575. static inline int compare_versions(unsigned int first_maj, unsigned int first_min,
  576. unsigned int second_maj, unsigned int second_min) {
  577. if (first_maj > second_maj)
  578. return 1;
  579. if (first_maj < second_maj)
  580. return -1;
  581. if (first_min > second_min)
  582. return 1;
  583. if (first_min < second_min)
  584. return -1;
  585. return 0;
  586. }
  587. /**
  588. * <!-- set_min_version() -->
  589. *
  590. * @brief TODO
  591. *
  592. * @param[in] first_maj unsigned int
  593. * @param[in] first_minor unsigned int
  594. * @param[in] second_maj unsigned int
  595. * @param[in] second_minor unsigned int
  596. * @param[in] result_maj unsigned int*
  597. * @param[in] result_minor unsigned int*
  598. *
  599. */
  600. static inline void set_min_version(unsigned int first_maj, unsigned int first_minor,
  601. unsigned int second_maj, unsigned int second_minor,
  602. unsigned int* result_maj, unsigned int* result_minor) {
  603. int result = compare_versions(first_maj, first_minor, second_maj, second_minor);
  604. if (result < 0) {
  605. *result_maj = first_maj;
  606. *result_minor = first_minor;
  607. }
  608. else {
  609. *result_maj = second_maj;
  610. *result_minor = second_minor;
  611. }
  612. }
  613. /**
  614. * <!-- set_max_version() -->
  615. *
  616. * @brief TODO
  617. *
  618. * @param[in] first_maj unsigned int
  619. * @param[in] first_minor unsigned int
  620. * @param[in] second_maj unsigned int
  621. * @param[in] second_minor unsigned int
  622. * @param[out] result_maj unsigned int*
  623. * @param[out] result_minor unsigned int*
  624. *
  625. */
  626. static inline void set_max_version(unsigned int first_maj, unsigned int first_minor,
  627. unsigned int second_maj, unsigned int second_minor,
  628. unsigned int* result_maj, unsigned int* result_minor) {
  629. int result = compare_versions(first_maj, first_minor, second_maj, second_minor);
  630. if (result > 0) {
  631. *result_maj = first_maj;
  632. *result_minor = first_minor;
  633. }
  634. else {
  635. *result_maj = second_maj;
  636. *result_minor = second_minor;
  637. }
  638. }
  639. #ifndef EMPTYSTR
  640. #define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
  641. #endif
  642. #ifndef PASS_ERROR
  643. #define PASS_ERROR(ST) (ST == PEP_PASSPHRASE_REQUIRED || ST == PEP_WRONG_PASSPHRASE || ST == PEP_PASSPHRASE_FOR_NEW_KEYS_REQUIRED)
  644. #endif
  645. #ifndef IS_PGP_CT
  646. #define IS_PGP_CT(CT) (((CT) | PEP_ct_confirmed) == PEP_ct_OpenPGP)
  647. #endif
  648. #ifndef _MIN
  649. #define _MIN(A, B) ((B) > (A) ? (A) : (B))
  650. #endif
  651. #ifndef _MAX
  652. #define _MAX(A, B) ((B) > (A) ? (B) : (A))
  653. #endif
  654. // These are globals used in generating message IDs and should only be
  655. // computed once, as they're either really constants or OS-dependent
  656. extern int _pEp_rand_max_bits;
  657. extern double _pEp_log2_36;
  658. /**
  659. * <!-- _init_globals() -->
  660. *
  661. * @brief TODO
  662. *
  663. *
  664. */
  665. static inline void _init_globals() {
  666. _pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
  667. _pEp_log2_36 = log2(36);
  668. }
  669. // spinlock implementation
  670. /**
  671. * <!-- Sqlite3_step() -->
  672. *
  673. * @brief TODO
  674. *
  675. * @param[in] stmt sqlite3_stmt*
  676. *
  677. */
  678. static inline int Sqlite3_step(sqlite3_stmt* stmt)
  679. {
  680. int rc;
  681. do {
  682. rc = sqlite3_step(stmt);
  683. } while (rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
  684. return rc;
  685. }
  686. #endif