crypt_path.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2019, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file crypt_path.c
  6. *
  7. * \brief Functions dealing with layered circuit encryption. This file aims to
  8. * provide an API around the crypt_path_t structure which holds crypto
  9. * information about a specific hop of a circuit.
  10. **/
  11. #define CRYPT_PATH_PRIVATE
  12. #include "core/or/or.h"
  13. #include "core/or/crypt_path.h"
  14. #include "core/crypto/relay_crypto.h"
  15. #include "core/crypto/onion_crypto.h"
  16. #include "core/or/circuitbuild.h"
  17. #include "core/or/circuitlist.h"
  18. #include "lib/crypt_ops/crypto_dh.h"
  19. #include "lib/crypt_ops/crypto_util.h"
  20. #include "core/or/crypt_path_st.h"
  21. #include "core/or/cell_st.h"
  22. /** Initialize and return a minimal crypt_path_t */
  23. crypt_path_t *
  24. crypt_path_new(void)
  25. {
  26. crypt_path_t *cpath = tor_malloc_zero(sizeof(crypt_path_t));
  27. cpath->magic = CRYPT_PATH_MAGIC;
  28. cpath->private = tor_malloc_zero(sizeof(struct crypt_path_private_t));
  29. return cpath;
  30. }
  31. /** Add <b>new_hop</b> to the end of the doubly-linked-list <b>head_ptr</b>.
  32. * This function is used to extend cpath by another hop.
  33. */
  34. void
  35. cpath_extend_linked_list(crypt_path_t **head_ptr, crypt_path_t *new_hop)
  36. {
  37. if (*head_ptr) {
  38. new_hop->next = (*head_ptr);
  39. new_hop->prev = (*head_ptr)->prev;
  40. (*head_ptr)->prev->next = new_hop;
  41. (*head_ptr)->prev = new_hop;
  42. } else {
  43. *head_ptr = new_hop;
  44. new_hop->prev = new_hop->next = new_hop;
  45. }
  46. }
  47. /** Create a new hop, annotate it with information about its
  48. * corresponding router <b>choice</b>, and append it to the
  49. * end of the cpath <b>head_ptr</b>. */
  50. int
  51. cpath_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
  52. {
  53. crypt_path_t *hop = crypt_path_new();
  54. /* link hop into the cpath, at the end. */
  55. cpath_extend_linked_list(head_ptr, hop);
  56. hop->state = CPATH_STATE_CLOSED;
  57. hop->extend_info = extend_info_dup(choice);
  58. hop->package_window = circuit_initial_package_window();
  59. hop->deliver_window = CIRCWINDOW_START;
  60. return 0;
  61. }
  62. /** Verify that cpath <b>cp</b> has all of its invariants
  63. * correct. Trigger an assert if anything is invalid.
  64. */
  65. void
  66. cpath_assert_ok(const crypt_path_t *cp)
  67. {
  68. const crypt_path_t *start = cp;
  69. do {
  70. cpath_assert_layer_ok(cp);
  71. /* layers must be in sequence of: "open* awaiting? closed*" */
  72. if (cp != start) {
  73. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  74. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  75. } else if (cp->state == CPATH_STATE_OPEN) {
  76. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  77. }
  78. }
  79. cp = cp->next;
  80. tor_assert(cp);
  81. } while (cp != start);
  82. }
  83. /** Verify that cpath layer <b>cp</b> has all of its invariants
  84. * correct. Trigger an assert if anything is invalid.
  85. */
  86. void
  87. cpath_assert_layer_ok(const crypt_path_t *cp)
  88. {
  89. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  90. // tor_assert(cp->port);
  91. tor_assert(cp);
  92. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  93. tor_assert(cp->private);
  94. switch (cp->state)
  95. {
  96. case CPATH_STATE_OPEN:
  97. relay_crypto_assert_ok(&cp->private->crypto);
  98. /* fall through */
  99. case CPATH_STATE_CLOSED:
  100. /*XXXX Assert that there's no handshake_state either. */
  101. tor_assert(!cp->rend_dh_handshake_state);
  102. break;
  103. case CPATH_STATE_AWAITING_KEYS:
  104. /* tor_assert(cp->dh_handshake_state); */
  105. break;
  106. default:
  107. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  108. tor_assert(0);
  109. }
  110. tor_assert(cp->package_window >= 0);
  111. tor_assert(cp->deliver_window >= 0);
  112. }
  113. /** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in key_data.
  114. *
  115. * If <b>is_hs_v3</b> is set, this cpath will be used for next gen hidden
  116. * service circuits and <b>key_data</b> must be at least
  117. * HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN bytes in length.
  118. *
  119. * If <b>is_hs_v3</b> is not set, key_data must contain CPATH_KEY_MATERIAL_LEN
  120. * bytes, which are used as follows:
  121. * - 20 to initialize f_digest
  122. * - 20 to initialize b_digest
  123. * - 16 to key f_crypto
  124. * - 16 to key b_crypto
  125. *
  126. * (If 'reverse' is true, then f_XX and b_XX are swapped.)
  127. *
  128. * Return 0 if init was successful, else -1 if it failed.
  129. */
  130. int
  131. cpath_init_circuit_crypto(crypt_path_t *cpath,
  132. const char *key_data, size_t key_data_len,
  133. int reverse, int is_hs_v3)
  134. {
  135. tor_assert(cpath);
  136. tor_assert(cpath->private);
  137. return relay_crypto_init(&cpath->private->crypto, key_data, key_data_len,
  138. reverse, is_hs_v3);
  139. }
  140. /** Deallocate space associated with the cpath node <b>victim</b>. */
  141. void
  142. cpath_free(crypt_path_t *victim)
  143. {
  144. if (!victim || BUG(!victim->private))
  145. return;
  146. relay_crypto_clear(&victim->private->crypto);
  147. onion_handshake_state_release(&victim->handshake_state);
  148. crypto_dh_free(victim->rend_dh_handshake_state);
  149. extend_info_free(victim->extend_info);
  150. tor_free(victim->private);
  151. memwipe(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  152. tor_free(victim);
  153. }
  154. /********************** cpath crypto API *******************************/
  155. /** Encrypt or decrypt <b>payload</b> using the crypto of <b>cpath</b>. Actual
  156. * operation decided by <b>is_decrypt</b>. */
  157. void
  158. cpath_crypt_cell(const crypt_path_t *cpath, uint8_t *payload, bool is_decrypt)
  159. {
  160. tor_assert(cpath);
  161. tor_assert(cpath->private);
  162. if (is_decrypt) {
  163. relay_crypt_one_payload(cpath->private->crypto.b_crypto, payload);
  164. } else {
  165. relay_crypt_one_payload(cpath->private->crypto.f_crypto, payload);
  166. }
  167. }
  168. /** Getter for the incoming digest of <b>cpath</b>. */
  169. struct crypto_digest_t *
  170. cpath_get_incoming_digest(const crypt_path_t *cpath)
  171. {
  172. tor_assert(cpath);
  173. tor_assert(cpath->private);
  174. return cpath->private->crypto.b_digest;
  175. }
  176. /** Set the right integrity digest on the outgoing <b>cell</b> based on the
  177. * cell payload and update the forward digest of <b>cpath</b>. */
  178. void
  179. cpath_set_cell_forward_digest(crypt_path_t *cpath, cell_t *cell)
  180. {
  181. tor_assert(cpath);
  182. tor_assert(cpath->private);
  183. relay_set_digest(cpath->private->crypto.f_digest, cell);
  184. }
  185. /************ other cpath functions ***************************/
  186. /** Return the first non-open hop in cpath, or return NULL if all
  187. * hops are open. */
  188. crypt_path_t *
  189. cpath_get_next_non_open_hop(crypt_path_t *cpath)
  190. {
  191. crypt_path_t *hop = cpath;
  192. do {
  193. if (hop->state != CPATH_STATE_OPEN)
  194. return hop;
  195. hop = hop->next;
  196. } while (hop != cpath);
  197. return NULL;
  198. }
  199. #ifdef TOR_UNIT_TESTS
  200. /** Unittest helper function: Count number of hops in cpath linked list. */
  201. unsigned int
  202. cpath_get_n_hops(crypt_path_t **head_ptr)
  203. {
  204. unsigned int n_hops = 0;
  205. crypt_path_t *tmp;
  206. if (!*head_ptr) {
  207. return 0;
  208. }
  209. tmp = *head_ptr;
  210. do {
  211. n_hops++;
  212. tmp = tmp->next;
  213. } while (tmp != *head_ptr);
  214. return n_hops;
  215. }
  216. #endif /* defined(TOR_UNIT_TESTS) */