p≡p engine fork for my own dirty testing of stuff
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.

849 lines
22 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
ENGINE-866 feature branch merge (squashed commit) of functionality to set the sticky bit for manually imported keys, to query for that bit in the trust database, and prevention of automatic reset of sticky keys by key reset when devices leave a device group. Squashed commit of the following: commit c64d850dc4bfe5a9dfd54aa94eea08a75ff69191 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:29:32 2021 +0100 ENGINE-866: doc'd bit getter function commit ad725b5b7c742300a6a182ad8b058db23dbc3cfb Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:23:49 2021 +0100 ENGINE-866: Key reset tested on mixed sticky and not sticky keys and does what it should. commit 0ffbdde7b598c7c3fff5d797e732dec07685f9be Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:13:53 2021 +0100 ENGINE-866: Add boolean for whether to set the sticky bit or not with set_own_imported_key. the adapter should filter this out for apps, I guess, according to Volker commit 23fec59a9a4ede0682a9ebcb9a61e78456e7d8d4 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 14:53:19 2021 +0100 ENGINE-866: Test and use the sticky bit commit 562239fda874623c40893c382a8f82df9e002ef5 Author: Krista Bennett <krista@pep.foundation> Date: Thu Feb 25 16:47:47 2021 +0100 ENGINE-866: moved bit from key to trust, created set_own_imported_key to replace set_own_key FOR MAIL APPS (does NOT replace it for key reset, as the new function can generate a passphrase error, whereas set_own_key cannot), and did an initial test to ensure the setter/getter functions work on the DB. commit 594133cfdee966adbaa66c62133ede1ca917bca0 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 11:16:21 2021 +0100 Commented out the or'd identity.flags / pgp_keypair.flags in the sql code for the get_identity functions; we've never HAD a pgp_keypair flag before, so it never hurt before, but at this point, we're going to introduce them, and I don't want trouble. If fdik wants them or'd, fine, we'll have to change the values in the keyflags to be disjoint from the identity flags so they can coexist, but for now, they are out. commit 99831445b3e22e1386aa0f86414fdb6939e5ebaf Merge: 8ba53ece d1664cf5 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 10:15:53 2021 +0100 Merge branch 'master' into ENGINE-866 commit 8ba53ece06773168a9188373d1be5f13d99b2f6e Merge: 168e2cf9 c52f4d39 Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 20:06:08 2021 +0100 Merged in engine_sql changes commit 168e2cf9578b12157b98da8b26e598f0a1448d9e Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 19:03:35 2021 +0100 ENGINE-866: Added sticky bit in database for manually set keys
9 months ago
7 years ago
3 years ago
7 years ago
ENGINE-866 feature branch merge (squashed commit) of functionality to set the sticky bit for manually imported keys, to query for that bit in the trust database, and prevention of automatic reset of sticky keys by key reset when devices leave a device group. Squashed commit of the following: commit c64d850dc4bfe5a9dfd54aa94eea08a75ff69191 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:29:32 2021 +0100 ENGINE-866: doc'd bit getter function commit ad725b5b7c742300a6a182ad8b058db23dbc3cfb Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:23:49 2021 +0100 ENGINE-866: Key reset tested on mixed sticky and not sticky keys and does what it should. commit 0ffbdde7b598c7c3fff5d797e732dec07685f9be Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:13:53 2021 +0100 ENGINE-866: Add boolean for whether to set the sticky bit or not with set_own_imported_key. the adapter should filter this out for apps, I guess, according to Volker commit 23fec59a9a4ede0682a9ebcb9a61e78456e7d8d4 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 14:53:19 2021 +0100 ENGINE-866: Test and use the sticky bit commit 562239fda874623c40893c382a8f82df9e002ef5 Author: Krista Bennett <krista@pep.foundation> Date: Thu Feb 25 16:47:47 2021 +0100 ENGINE-866: moved bit from key to trust, created set_own_imported_key to replace set_own_key FOR MAIL APPS (does NOT replace it for key reset, as the new function can generate a passphrase error, whereas set_own_key cannot), and did an initial test to ensure the setter/getter functions work on the DB. commit 594133cfdee966adbaa66c62133ede1ca917bca0 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 11:16:21 2021 +0100 Commented out the or'd identity.flags / pgp_keypair.flags in the sql code for the get_identity functions; we've never HAD a pgp_keypair flag before, so it never hurt before, but at this point, we're going to introduce them, and I don't want trouble. If fdik wants them or'd, fine, we'll have to change the values in the keyflags to be disjoint from the identity flags so they can coexist, but for now, they are out. commit 99831445b3e22e1386aa0f86414fdb6939e5ebaf Merge: 8ba53ece d1664cf5 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 10:15:53 2021 +0100 Merge branch 'master' into ENGINE-866 commit 8ba53ece06773168a9188373d1be5f13d99b2f6e Merge: 168e2cf9 c52f4d39 Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 20:06:08 2021 +0100 Merged in engine_sql changes commit 168e2cf9578b12157b98da8b26e598f0a1448d9e Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 19:03:35 2021 +0100 ENGINE-866: Added sticky bit in database for manually set keys
9 months 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
9 months ago
9 months ago
9 months ago
7 years ago
9 months ago
9 months ago
9 months ago
9 months ago
3 years ago
3 years ago
3 years ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months 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. // maximum attachment size to import as key 25MB, maximum of 20 attachments
  9. #define MAX_KEY_SIZE (25 * 1024 * 1024)
  10. #define MAX_KEYS_TO_IMPORT 20
  11. #define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
  12. // this is 20 trustwords with 79 chars max
  13. #define MAX_TRUSTWORDS_SPACE (20 * 80)
  14. // XML parameters string
  15. #define PARMS_MAX 32768
  16. // maximum busy wait time in ms
  17. #define BUSY_WAIT_TIME 5000
  18. // default keyserver
  19. #ifndef DEFAULT_KEYSERVER
  20. #define DEFAULT_KEYSERVER "hkps://keys.openpgp.org"
  21. #endif
  22. // crashdump constants
  23. #ifndef CRASHDUMP_DEFAULT_LINES
  24. #define CRASHDUMP_DEFAULT_LINES 100
  25. #endif
  26. #define CRASHDUMP_MAX_LINES 32767
  27. // p≡p full string, NUL-terminated
  28. #ifndef PEP_SUBJ_STRING
  29. #define PEP_SUBJ_STRING {0x70,0xE2,0x89,0xA1,0x70,0x00}
  30. #define PEP_SUBJ_BYTELEN 5
  31. #endif
  32. #ifndef PEP_SUBJ_KEY
  33. #define PEP_SUBJ_KEY "Subject: "
  34. #define PEP_SUBJ_KEY_LC "subject: "
  35. #define PEP_SUBJ_KEY_LEN 9
  36. #endif
  37. #ifndef PEP_MSG_WRAP_KEY
  38. #define PEP_MSG_WRAP_KEY "pEp-Wrapped-Message-Info: "
  39. #define PEP_MSG_WRAP_KEY_LC "pep-wrapped-message-info: "
  40. #define PEP_MSG_WRAP_KEY_LEN 26
  41. #endif
  42. #ifndef X_PEP_MSG_WRAP_KEY
  43. #define X_PEP_MSG_WRAP_KEY "X-pEp-Wrapped-Message-Info"
  44. #endif
  45. #ifndef X_PEP_SNDR_FPR_KEY
  46. #define X_PEP_SNDR_FPR_KEY "X-pEp-Sender-FPR"
  47. #endif
  48. #ifndef X_PEP_MSG_VER_KEY
  49. #define X_PEP_MSG_VER_KEY "X-pEp-Message-Version"
  50. #endif
  51. #define VER_1_0 "1.0"
  52. #define VER_2_0 "2.0"
  53. #define VER_2_1 "2.1"
  54. #define VER_2_2 "2.2"
  55. #include "platform.h"
  56. #ifdef WIN32
  57. #define KEYS_DB windoze_keys_db()
  58. #define LOCAL_DB windoze_local_db()
  59. #define SYSTEM_DB windoze_system_db()
  60. #else // UNIX
  61. #define _POSIX_C_SOURCE 200809L
  62. #include <dlfcn.h>
  63. #ifdef NDEBUG
  64. #define LOCAL_DB unix_local_db()
  65. #else
  66. #define LOCAL_DB unix_local_db(false)
  67. #define LOCAL_DB_RESET unix_local_db(true)
  68. #endif
  69. #ifdef ANDROID
  70. #define SYSTEM_DB android_system_db()
  71. #else
  72. #define SYSTEM_DB unix_system_db()
  73. #endif
  74. #endif
  75. #include <locale.h>
  76. #include <stdlib.h>
  77. #include <string.h>
  78. #include <assert.h>
  79. #include <stdio.h>
  80. #include <ctype.h>
  81. #include <math.h>
  82. #ifdef SQLITE3_FROM_OS
  83. #include <sqlite3.h>
  84. #else
  85. #include "sqlite3.h"
  86. #endif
  87. #include "pEpEngine.h"
  88. #include "key_reset.h"
  89. #include "pEpEngine_internal.h"
  90. #include "key_reset_internal.h"
  91. #include "group_internal.h"
  92. #include "keymanagement_internal.h"
  93. #include "message_api_internal.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 "../asn.1/Distribution.h"
  102. #include "../asn.1/Sync.h"
  103. #include "keymanagement.h"
  104. #include "cryptotech.h"
  105. #include "transport.h"
  106. #include "sync_api.h"
  107. #include "Sync_func.h"
  108. #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
  109. struct _pEpSession;
  110. typedef struct _pEpSession pEpSession;
  111. /**
  112. * @struct _pEpSession
  113. *
  114. * @brief TODO
  115. *
  116. */
  117. struct _pEpSession {
  118. const char *version;
  119. messageToSend_t messageToSend;
  120. #if defined(USE_SEQUOIA)
  121. sqlite3 *key_db;
  122. struct {
  123. sqlite3_stmt *begin_transaction;
  124. sqlite3_stmt *commit_transaction;
  125. sqlite3_stmt *rollback_transaction;
  126. sqlite3_stmt *cert_find;
  127. sqlite3_stmt *tsk_find;
  128. sqlite3_stmt *cert_find_by_keyid;
  129. sqlite3_stmt *tsk_find_by_keyid;
  130. sqlite3_stmt *cert_find_by_email;
  131. sqlite3_stmt *tsk_find_by_email;
  132. sqlite3_stmt *cert_all;
  133. sqlite3_stmt *tsk_all;
  134. sqlite3_stmt *cert_save_insert_primary;
  135. sqlite3_stmt *cert_save_insert_subkeys;
  136. sqlite3_stmt *cert_save_insert_userids;
  137. sqlite3_stmt *delete_keypair;
  138. } sq_sql;
  139. pgp_policy_t policy;
  140. #endif
  141. PEP_cryptotech_t *cryptotech;
  142. PEP_CIPHER_SUITE cipher_suite;
  143. char* curr_passphrase;
  144. bool new_key_pass_enable;
  145. char* generation_passphrase;
  146. PEP_transport_t *transports;
  147. sqlite3 *db;
  148. sqlite3 *system_db;
  149. sqlite3_stmt *log;
  150. sqlite3_stmt *trustword;
  151. sqlite3_stmt *get_identity;
  152. sqlite3_stmt *get_identity_without_trust_check;
  153. sqlite3_stmt *get_identities_by_address;
  154. sqlite3_stmt *get_identities_by_userid;
  155. sqlite3_stmt *get_identities_by_main_key_id;
  156. sqlite3_stmt *replace_identities_fpr;
  157. sqlite3_stmt *replace_main_user_fpr;
  158. sqlite3_stmt *replace_main_user_fpr_if_equal;
  159. sqlite3_stmt *get_main_user_fpr;
  160. sqlite3_stmt *set_default_identity_fpr;
  161. sqlite3_stmt *get_default_identity_fpr;
  162. sqlite3_stmt *refresh_userid_default_key;
  163. sqlite3_stmt *delete_key;
  164. sqlite3_stmt *remove_fpr_as_identity_default;
  165. sqlite3_stmt *remove_fpr_as_user_default;
  166. sqlite3_stmt *set_person;
  167. sqlite3_stmt *update_person;
  168. sqlite3_stmt *delete_person;
  169. sqlite3_stmt *exists_person;
  170. sqlite3_stmt *set_as_pEp_user;
  171. sqlite3_stmt *is_pEp_user;
  172. sqlite3_stmt *upgrade_pEp_version_by_user_id;
  173. sqlite3_stmt *add_into_social_graph;
  174. sqlite3_stmt *get_own_address_binding_from_contact;
  175. sqlite3_stmt *set_revoke_contact_as_notified;
  176. sqlite3_stmt *get_contacted_ids_from_revoke_fpr;
  177. sqlite3_stmt *was_id_for_revoke_contacted;
  178. sqlite3_stmt *has_id_contacted_address;
  179. sqlite3_stmt *get_last_contacted;
  180. // sqlite3_stmt *set_device_group;
  181. // sqlite3_stmt *get_device_group;
  182. sqlite3_stmt *set_pgp_keypair;
  183. sqlite3_stmt *set_pgp_keypair_flags;
  184. sqlite3_stmt *unset_pgp_keypair_flags;
  185. sqlite3_stmt *set_identity_entry;
  186. sqlite3_stmt *update_identity_entry;
  187. sqlite3_stmt *exists_identity_entry;
  188. sqlite3_stmt *force_set_identity_username;
  189. sqlite3_stmt *set_identity_flags;
  190. sqlite3_stmt *unset_identity_flags;
  191. sqlite3_stmt *set_ident_enc_format;
  192. sqlite3_stmt *set_pEp_version;
  193. sqlite3_stmt *clear_trust_info;
  194. sqlite3_stmt *set_trust;
  195. sqlite3_stmt *update_trust;
  196. sqlite3_stmt *exists_trust_entry;
  197. sqlite3_stmt *update_trust_to_pEp;
  198. sqlite3_stmt *update_trust_for_fpr;
  199. sqlite3_stmt *get_trust;
  200. sqlite3_stmt *get_trust_by_userid;
  201. sqlite3_stmt *least_trust;
  202. sqlite3_stmt *update_key_sticky_bit_for_user;
  203. sqlite3_stmt *is_key_sticky_for_user;
  204. sqlite3_stmt *mark_compromised;
  205. sqlite3_stmt *reset_trust;
  206. sqlite3_stmt *crashdump;
  207. sqlite3_stmt *languagelist;
  208. sqlite3_stmt *i18n_token;
  209. sqlite3_stmt *replace_userid;
  210. // blacklist
  211. sqlite3_stmt *blacklist_add;
  212. sqlite3_stmt *blacklist_delete;
  213. sqlite3_stmt *blacklist_is_listed;
  214. sqlite3_stmt *blacklist_retrieve;
  215. // Keys
  216. sqlite3_stmt *own_key_is_listed;
  217. sqlite3_stmt *is_own_address;
  218. sqlite3_stmt *own_identities_retrieve;
  219. sqlite3_stmt *own_keys_retrieve;
  220. sqlite3_stmt *key_identities_retrieve;
  221. sqlite3_stmt *get_user_default_key;
  222. sqlite3_stmt *get_all_keys_for_user;
  223. sqlite3_stmt *get_default_own_userid;
  224. // groups
  225. sqlite3_stmt *create_group;
  226. sqlite3_stmt *enable_group;
  227. sqlite3_stmt *disable_group;
  228. sqlite3_stmt *exists_group_entry;
  229. sqlite3_stmt *group_add_member;
  230. sqlite3_stmt *group_delete_member;
  231. sqlite3_stmt *group_join;
  232. sqlite3_stmt *leave_group;
  233. sqlite3_stmt *set_group_member_status;
  234. sqlite3_stmt *get_all_members;
  235. sqlite3_stmt *get_active_members;
  236. sqlite3_stmt *get_active_groups;
  237. sqlite3_stmt *get_all_groups;
  238. sqlite3_stmt *add_own_membership_entry;
  239. sqlite3_stmt *get_own_membership_status;
  240. sqlite3_stmt *retrieve_own_membership_info_for_group_and_ident;
  241. sqlite3_stmt *retrieve_own_membership_info_for_group;
  242. sqlite3_stmt *get_group_manager;
  243. sqlite3_stmt *is_invited_group_member;
  244. sqlite3_stmt *is_active_group_member;
  245. sqlite3_stmt *is_group_active;
  246. // sqlite3_stmt *set_own_key;
  247. // sequence value
  248. sqlite3_stmt *sequence_value1;
  249. sqlite3_stmt *sequence_value2;
  250. // revoked keys
  251. sqlite3_stmt *set_revoked;
  252. sqlite3_stmt *get_revoked;
  253. sqlite3_stmt *get_replacement_fpr;
  254. // mistrusted
  255. sqlite3_stmt* add_mistrusted_key;
  256. sqlite3_stmt* is_mistrusted_key;
  257. sqlite3_stmt* delete_mistrusted_key;
  258. // aliases
  259. sqlite3_stmt *get_userid_alias_default;
  260. sqlite3_stmt *add_userid_alias;
  261. // callbacks
  262. examine_identity_t examine_identity;
  263. void *examine_management;
  264. notifyHandshake_t notifyHandshake;
  265. inject_sync_event_t inject_sync_event;
  266. retrieve_next_sync_event_t retrieve_next_sync_event;
  267. ensure_passphrase_t ensure_passphrase;
  268. // pEp Sync
  269. void *sync_management;
  270. void *sync_obj;
  271. struct Sync_state_s sync_state;
  272. // void* sync_state_payload;
  273. // char sync_uuid[37];
  274. // time_t LastCannotDecrypt;
  275. // time_t LastUpdateRequest;
  276. // runtime config
  277. bool passive_mode;
  278. bool unencrypted_subject;
  279. bool service_log;
  280. #ifndef NDEBUG
  281. int debug_color;
  282. #endif
  283. };
  284. /**
  285. * <!-- init_transport_system() -->
  286. *
  287. * @brief TODO
  288. *
  289. * @param[in] session session handle
  290. * @param[in] in_first bool
  291. *
  292. * @retval PEP_STATUS_OK
  293. */
  294. PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
  295. /**
  296. * <!-- release_transport_system() -->
  297. *
  298. * @brief TODO
  299. *
  300. * @param[in] session session handle
  301. * @param[in] out_last bool
  302. *
  303. */
  304. void release_transport_system(PEP_SESSION session, bool out_last);
  305. /**
  306. * @internal
  307. *
  308. * <!-- encrypt_only() -->
  309. *
  310. * @brief TODO
  311. *
  312. * @param[in] session session handle
  313. * @param[in] keylist const stringlist_t*
  314. * @param[in] ptext const char*
  315. * @param[in] psize size_t
  316. * @param[in] ctext char**
  317. * @param[in] csize size_t*
  318. *
  319. * @warning NOT to be exposed to the outside!!!!!
  320. */
  321. PEP_STATUS encrypt_only(
  322. PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  323. size_t psize, char **ctext, size_t *csize
  324. );
  325. /**
  326. * <!-- decorate_message() -->
  327. *
  328. * @brief TODO
  329. *
  330. * @param[in] msg message*
  331. * @param[in] rating PEP_rating
  332. * @param[in] keylist stringlist_t*
  333. * @param[in] add_version bool
  334. * @param[in] clobber bool
  335. *
  336. */
  337. void decorate_message(
  338. PEP_SESSION session,
  339. message *msg,
  340. PEP_rating rating,
  341. stringlist_t *keylist,
  342. bool add_version,
  343. bool clobber);
  344. #if defined(NDEBUG) || defined(NOLOG)
  345. #define DEBUG_LOG(TITLE, ENTITY, DESC)
  346. #else
  347. #ifdef ANDROID
  348. #include <android/log.h>
  349. #define LOG_MORE(...) __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
  350. #else
  351. #include <stdio.h>
  352. #define LOG_MORE(...) fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
  353. #endif
  354. #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
  355. log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
  356. LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
  357. }
  358. #endif
  359. /**
  360. * @enum normalize_hex_res_t
  361. *
  362. * @brief TODO
  363. *
  364. */
  365. typedef enum _normalize_hex_rest_t {
  366. accept_hex,
  367. ignore_hex,
  368. reject_hex
  369. } normalize_hex_res_t;
  370. /**
  371. * <!-- _normalize_hex() -->
  372. *
  373. * @brief TODO
  374. *
  375. * @param[in] hex char*
  376. *
  377. * @retval accept_hex
  378. * @retval irgnore_hex
  379. * @retval reject_hex
  380. */
  381. static inline normalize_hex_res_t _normalize_hex(char *hex)
  382. {
  383. if (*hex >= '0' && *hex <= '9')
  384. return accept_hex;
  385. if (*hex >= 'A' && *hex <= 'F') {
  386. *hex += 'a' - 'A';
  387. return accept_hex;
  388. }
  389. if (*hex >= 'a' && *hex <= 'f')
  390. return accept_hex;
  391. if (*hex == ' ')
  392. return ignore_hex;
  393. return reject_hex;
  394. }
  395. // Space tolerant and case insensitive fingerprint string compare
  396. /**
  397. * <!-- _compare_fprs() -->
  398. *
  399. * @brief TODO
  400. *
  401. * @param[in] fpra const char*
  402. * @param[in] fpras size_t
  403. * @param[in] fprb const char*
  404. * @param[in] fprbs size_t
  405. * @param[in] comparison int*
  406. *
  407. * @retval PEP_STATUS_OK
  408. * @retval PEP_ILLEGAL_VALUE illegal parameter values
  409. * @retval PEP_TRUSTWORDS_FPR_WRONG_LENGTH
  410. */
  411. static inline PEP_STATUS _compare_fprs(
  412. const char* fpra,
  413. size_t fpras,
  414. const char* fprb,
  415. size_t fprbs,
  416. int* comparison)
  417. {
  418. size_t ai = 0;
  419. size_t bi = 0;
  420. size_t significant = 0;
  421. int _comparison = 0;
  422. const int _FULL_FINGERPRINT_LENGTH = 40;
  423. // First compare every non-ignored chars until an end is reached
  424. while(ai < fpras && bi < fprbs)
  425. {
  426. char fprac = fpra[ai];
  427. char fprbc = fprb[bi];
  428. normalize_hex_res_t fprah = _normalize_hex(&fprac);
  429. normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
  430. if(fprah == reject_hex || fprbh == reject_hex)
  431. return PEP_ILLEGAL_VALUE;
  432. if ( fprah == ignore_hex )
  433. {
  434. ai++;
  435. }
  436. else if ( fprbh == ignore_hex )
  437. {
  438. bi++;
  439. }
  440. else
  441. {
  442. if(fprac != fprbc && _comparison == 0 )
  443. {
  444. _comparison = fprac > fprbc ? 1 : -1;
  445. }
  446. significant++;
  447. ai++;
  448. bi++;
  449. }
  450. }
  451. // Bail out if we didn't got enough significnt chars
  452. if (significant != _FULL_FINGERPRINT_LENGTH )
  453. return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
  454. // Then purge remaining chars, all must be ignored chars
  455. while ( ai < fpras )
  456. {
  457. char fprac = fpra[ai];
  458. normalize_hex_res_t fprah = _normalize_hex(&fprac);
  459. if( fprah == reject_hex )
  460. return PEP_ILLEGAL_VALUE;
  461. if ( fprah != ignore_hex )
  462. return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
  463. ai++;
  464. }
  465. while ( bi < fprbs )
  466. {
  467. char fprbc = fprb[bi];
  468. normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
  469. if( fprbh == reject_hex )
  470. return PEP_ILLEGAL_VALUE;
  471. if ( fprbh != ignore_hex )
  472. return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
  473. bi++;
  474. }
  475. *comparison = _comparison;
  476. return PEP_STATUS_OK;
  477. }
  478. /**
  479. * <!-- _same_fpr() -->
  480. *
  481. * @brief TODO
  482. *
  483. * @param[in] fpra const char*
  484. * @param[in] fpras size_t
  485. * @param[in] fprb const char*
  486. * @param[in] fprbs size_t
  487. *
  488. * @retval 0 on equal fingerprints
  489. * @retval non-zero if not equal
  490. */
  491. static inline int _same_fpr(
  492. const char* fpra,
  493. size_t fpras,
  494. const char* fprb,
  495. size_t fprbs
  496. )
  497. {
  498. // illegal values are ignored, and considered not same.
  499. int comparison = 1;
  500. _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
  501. return comparison == 0;
  502. }
  503. // size is the length of the bytestr that's coming in. This is really only intended
  504. // for comparing two full strings. If charstr's length is different from bytestr_size,
  505. // we'll return a non-zero value.
  506. /**
  507. * @internal
  508. *
  509. * <!-- _unsigned_signed_strcmp() -->
  510. *
  511. * @brief Compare an unsigned sequence of bytes with the input string.
  512. * This is really only intended for comparing two full strings.
  513. * If charstr's length is different from bytestr_size,
  514. * we'll return a non-zero value.
  515. *
  516. * @param[in] bytestr byte string (unsigned char data)
  517. * @param[in] charstr character string (NUL-terminated)
  518. * @param[in] bytestr_size length of byte string passed in
  519. *
  520. * @retval 0 if equal
  521. * @retval non-zero if not equal
  522. *
  523. */
  524. static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
  525. int charstr_len = strlen(charstr);
  526. if (charstr_len != bytestr_size)
  527. return -1; // we don't actually care except that it's non-zero
  528. return memcmp(bytestr, charstr, bytestr_size);
  529. }
  530. // This is just a horrible example of C type madness. UTF-8 made me do it.
  531. /**
  532. * <!-- _pEp_subj_copy() -->
  533. *
  534. * @brief TODO
  535. *
  536. *
  537. */
  538. static inline char* _pEp_subj_copy() {
  539. #ifndef WIN32
  540. unsigned char pEpstr[] = PEP_SUBJ_STRING;
  541. void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
  542. memcpy(retval, pEpstr, PEP_SUBJ_BYTELEN);
  543. return (char*)retval;
  544. #else
  545. return strdup("pEp");
  546. #endif
  547. }
  548. /**
  549. * <!-- is_me() -->
  550. *
  551. * @brief TODO
  552. *
  553. * @param[in] session session handle
  554. * @param[in] test_ident const pEp_identity*
  555. *
  556. * @retval true
  557. * @retval false
  558. */
  559. static inline bool is_me(PEP_SESSION session, const pEp_identity* test_ident) {
  560. bool retval = false;
  561. if (test_ident && test_ident->user_id) {
  562. char* def_id = NULL;
  563. get_default_own_userid(session, &def_id);
  564. if (test_ident->me ||
  565. (def_id && strcmp(def_id, test_ident->user_id) == 0)) {
  566. retval = true;
  567. }
  568. free(def_id);
  569. }
  570. return retval;
  571. }
  572. /**
  573. * <!-- pEp_version_numeric() -->
  574. *
  575. * @brief
  576. *
  577. * @param[in] version_str const char*
  578. *
  579. * @retval float version number
  580. * @retval 0 on failure
  581. */
  582. static inline float pEp_version_numeric(const char* version_str) {
  583. float retval = 0;
  584. if (!version_str || sscanf(version_str, "%f", &retval) != 1)
  585. return 0;
  586. return retval;
  587. }
  588. /**
  589. * <!-- pEp_version_major_minor() -->
  590. *
  591. * @brief get major and minor numbers as integers from version string
  592. *
  593. * @param[in] version_str const char*
  594. * @param[out] major unsigned int*
  595. * @param[out] minor unsigned int*
  596. *
  597. */
  598. static inline void pEp_version_major_minor(const char* version_str, unsigned int* major, unsigned int* minor) {
  599. if (!major || !minor)
  600. return;
  601. if (!version_str || sscanf(version_str, "%u.%u", major, minor) != 2) {
  602. *major = 0;
  603. *minor = 0;
  604. }
  605. return;
  606. }
  607. /**
  608. * <!-- compare_versions() -->
  609. *
  610. * @brief compares two versions by major and minor version numbers
  611. *
  612. * @param[in] first_maj unsigned int
  613. * @param[in] first_min unsigned int
  614. * @param[in] second_maj unsigned int
  615. * @param[in] second_min unsigned int
  616. *
  617. * @retval 1 when first is higher version
  618. * @retval -1 when first is lower version
  619. * @retval 0 when versions are equal
  620. */
  621. static inline int compare_versions(unsigned int first_maj, unsigned int first_min,
  622. unsigned int second_maj, unsigned int second_min) {
  623. if (first_maj > second_maj)
  624. return 1;
  625. if (first_maj < second_maj)
  626. return -1;
  627. if (first_min > second_min)
  628. return 1;
  629. if (first_min < second_min)
  630. return -1;
  631. return 0;
  632. }
  633. /**
  634. * <!-- set_min_version() -->
  635. *
  636. * @brief determine the smaler version from two versions
  637. *
  638. * @param[in] first_maj unsigned int
  639. * @param[in] first_minor unsigned int
  640. * @param[in] second_maj unsigned int
  641. * @param[in] second_minor unsigned int
  642. * @param[out] result_maj unsigned int*
  643. * @param[out] result_minor unsigned int*
  644. *
  645. */
  646. static inline void set_min_version(unsigned int first_maj, unsigned int first_minor,
  647. unsigned int second_maj, unsigned int second_minor,
  648. unsigned int* result_maj, unsigned int* result_minor) {
  649. int result = compare_versions(first_maj, first_minor, second_maj, second_minor);
  650. if (result < 0) {
  651. *result_maj = first_maj;
  652. *result_minor = first_minor;
  653. }
  654. else {
  655. *result_maj = second_maj;
  656. *result_minor = second_minor;
  657. }
  658. }
  659. /**
  660. * <!-- set_max_version() -->
  661. *
  662. * @brief determine the greater version out of two versions
  663. *
  664. * @param[in] first_maj unsigned int
  665. * @param[in] first_minor unsigned int
  666. * @param[in] second_maj unsigned int
  667. * @param[in] second_minor unsigned int
  668. * @param[out] result_maj unsigned int*
  669. * @param[out] result_minor unsigned int*
  670. *
  671. */
  672. static inline void set_max_version(unsigned int first_maj, unsigned int first_minor,
  673. unsigned int second_maj, unsigned int second_minor,
  674. unsigned int* result_maj, unsigned int* result_minor) {
  675. int result = compare_versions(first_maj, first_minor, second_maj, second_minor);
  676. if (result > 0) {
  677. *result_maj = first_maj;
  678. *result_minor = first_minor;
  679. }
  680. else {
  681. *result_maj = second_maj;
  682. *result_minor = second_minor;
  683. }
  684. }
  685. #ifndef EMPTYSTR
  686. #define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
  687. #endif
  688. #ifndef PASS_ERROR
  689. #define PASS_ERROR(ST) (ST == PEP_PASSPHRASE_REQUIRED || ST == PEP_WRONG_PASSPHRASE || ST == PEP_PASSPHRASE_FOR_NEW_KEYS_REQUIRED)
  690. #endif
  691. #ifndef IS_PGP_CT
  692. #define IS_PGP_CT(CT) (((CT) | PEP_ct_confirmed) == PEP_ct_OpenPGP)
  693. #endif
  694. #ifndef _MIN
  695. #define _MIN(A, B) ((B) > (A) ? (A) : (B))
  696. #endif
  697. #ifndef _MAX
  698. #define _MAX(A, B) ((B) > (A) ? (B) : (A))
  699. #endif
  700. // These are globals used in generating message IDs and should only be
  701. // computed once, as they're either really constants or OS-dependent
  702. extern int _pEp_rand_max_bits;
  703. extern double _pEp_log2_36;
  704. /**
  705. * <!-- _init_globals() -->
  706. *
  707. * @internal
  708. *
  709. * @brief TODO
  710. *
  711. * Please leave _patch_asn1_codec COMMENTED OUT unless you're working
  712. * in a branch or patching the asn1 is a solution
  713. */
  714. static inline void _init_globals() {
  715. _pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
  716. _pEp_log2_36 = log2(36);
  717. }
  718. // spinlock implementation
  719. /**
  720. * <!-- Sqlite3_step() -->
  721. *
  722. * @brief TODO
  723. *
  724. * @param[in] stmt sqlite3_stmt*
  725. *
  726. */
  727. static inline int Sqlite3_step(sqlite3_stmt* stmt)
  728. {
  729. int rc;
  730. do {
  731. rc = sqlite3_step(stmt);
  732. } while (rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
  733. return rc;
  734. }
  735. /**
  736. * @internal
  737. *
  738. * <!-- _add_auto_consume() -->
  739. *
  740. * @brief TODO
  741. *
  742. * @param[in] *msg message
  743. *
  744. */
  745. static inline void _add_auto_consume(message* msg) {
  746. add_opt_field(msg, "pEp-auto-consume", "yes");
  747. msg->in_reply_to = stringlist_add(msg->in_reply_to, "pEp-auto-consume@pEp.foundation");
  748. }
  749. #endif