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.

738 lines
18 KiB

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