onion_crypto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file onion_crypto.c
  8. * \brief Functions to handle different kinds of circuit extension crypto.
  9. *
  10. * In this module, we provide a set of abstractions to create a uniform
  11. * interface over the three circuit extension handshakes that Tor has used
  12. * over the years (TAP, CREATE_FAST, and ntor). These handshakes are
  13. * implemented in onion_tap.c, onion_fast.c, and onion_ntor.c respectively.
  14. *
  15. * All[*] of these handshakes follow a similar pattern: a client, knowing
  16. * some key from the relay it wants to extend through, generates the
  17. * first part of a handshake. A relay receives that handshake, and sends
  18. * a reply. Once the client handles the reply, it knows that it is
  19. * talking to the right relay, and it shares some freshly negotiated key
  20. * material with that relay.
  21. *
  22. * We sometimes call the client's part of the handshake an "onionskin".
  23. * We do this because historically, Onion Routing used a multi-layer
  24. * structure called an "onion" to construct circuits. Each layer of the
  25. * onion contained key material chosen by the client, the identity of
  26. * the next relay in the circuit, and a smaller onion, encrypted with
  27. * the key of the next relay. When we changed Tor to use a telescoping
  28. * circuit extension design, it corresponded to sending each layer of the
  29. * onion separately -- as a series of onionskins.
  30. **/
  31. #include "core/or/or.h"
  32. #include "core/or/circuitbuild.h"
  33. #include "core/crypto/onion_crypto.h"
  34. #include "core/crypto/onion_fast.h"
  35. #include "core/crypto/onion_ntor.h"
  36. #include "core/crypto/onion_tap.h"
  37. #include "feature/relay/router.h"
  38. #include "lib/crypt_ops/crypto_dh.h"
  39. #include "lib/crypt_ops/crypto_util.h"
  40. #include "core/or/crypt_path_st.h"
  41. #include "core/or/extend_info_st.h"
  42. /** Return a new server_onion_keys_t object with all of the keys
  43. * and other info we might need to do onion handshakes. (We make a copy of
  44. * our keys for each cpuworker to avoid race conditions with the main thread,
  45. * and to avoid locking) */
  46. server_onion_keys_t *
  47. server_onion_keys_new(void)
  48. {
  49. server_onion_keys_t *keys = tor_malloc_zero(sizeof(server_onion_keys_t));
  50. memcpy(keys->my_identity, router_get_my_id_digest(), DIGEST_LEN);
  51. dup_onion_keys(&keys->onion_key, &keys->last_onion_key);
  52. keys->curve25519_key_map = construct_ntor_key_map();
  53. keys->junk_keypair = tor_malloc_zero(sizeof(curve25519_keypair_t));
  54. curve25519_keypair_generate(keys->junk_keypair, 0);
  55. return keys;
  56. }
  57. /** Release all storage held in <b>keys</b>. */
  58. void
  59. server_onion_keys_free_(server_onion_keys_t *keys)
  60. {
  61. if (! keys)
  62. return;
  63. crypto_pk_free(keys->onion_key);
  64. crypto_pk_free(keys->last_onion_key);
  65. ntor_key_map_free(keys->curve25519_key_map);
  66. tor_free(keys->junk_keypair);
  67. memwipe(keys, 0, sizeof(server_onion_keys_t));
  68. tor_free(keys);
  69. }
  70. /** Release whatever storage is held in <b>state</b>, depending on its
  71. * type, and clear its pointer. */
  72. void
  73. onion_handshake_state_release(onion_handshake_state_t *state)
  74. {
  75. switch (state->tag) {
  76. case ONION_HANDSHAKE_TYPE_TAP:
  77. crypto_dh_free(state->u.tap);
  78. state->u.tap = NULL;
  79. break;
  80. case ONION_HANDSHAKE_TYPE_FAST:
  81. fast_handshake_state_free(state->u.fast);
  82. state->u.fast = NULL;
  83. break;
  84. case ONION_HANDSHAKE_TYPE_NTOR:
  85. ntor_handshake_state_free(state->u.ntor);
  86. state->u.ntor = NULL;
  87. break;
  88. default:
  89. /* LCOV_EXCL_START
  90. * This state should not even exist. */
  91. log_warn(LD_BUG, "called with unknown handshake state type %d",
  92. (int)state->tag);
  93. tor_fragile_assert();
  94. /* LCOV_EXCL_STOP */
  95. }
  96. }
  97. /** Perform the first step of a circuit-creation handshake of type <b>type</b>
  98. * (one of ONION_HANDSHAKE_TYPE_*): generate the initial "onion skin" in
  99. * <b>onion_skin_out</b>, and store any state information in <b>state_out</b>.
  100. * Return -1 on failure, and the length of the onionskin on acceptance.
  101. */
  102. int
  103. onion_skin_create(int type,
  104. const extend_info_t *node,
  105. onion_handshake_state_t *state_out,
  106. uint8_t *onion_skin_out)
  107. {
  108. int r = -1;
  109. switch (type) {
  110. case ONION_HANDSHAKE_TYPE_TAP:
  111. if (!node->onion_key)
  112. return -1;
  113. if (onion_skin_TAP_create(node->onion_key,
  114. &state_out->u.tap,
  115. (char*)onion_skin_out) < 0)
  116. return -1;
  117. r = TAP_ONIONSKIN_CHALLENGE_LEN;
  118. break;
  119. case ONION_HANDSHAKE_TYPE_FAST:
  120. if (fast_onionskin_create(&state_out->u.fast, onion_skin_out) < 0)
  121. return -1;
  122. r = CREATE_FAST_LEN;
  123. break;
  124. case ONION_HANDSHAKE_TYPE_NTOR:
  125. if (!extend_info_supports_ntor(node))
  126. return -1;
  127. if (onion_skin_ntor_create((const uint8_t*)node->identity_digest,
  128. &node->curve25519_onion_key,
  129. &state_out->u.ntor,
  130. onion_skin_out) < 0)
  131. return -1;
  132. r = NTOR_ONIONSKIN_LEN;
  133. break;
  134. default:
  135. /* LCOV_EXCL_START
  136. * We should never try to create an impossible handshake type. */
  137. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  138. tor_fragile_assert();
  139. r = -1;
  140. /* LCOV_EXCL_STOP */
  141. }
  142. if (r > 0)
  143. state_out->tag = (uint16_t) type;
  144. return r;
  145. }
  146. /* This is the maximum value for keys_out_len passed to
  147. * onion_skin_server_handshake, plus 16. We can make it bigger if needed:
  148. * It just defines how many bytes to stack-allocate. */
  149. #define MAX_KEYS_TMP_LEN 128
  150. /** Perform the second (server-side) step of a circuit-creation handshake of
  151. * type <b>type</b>, responding to the client request in <b>onion_skin</b>
  152. * using the keys in <b>keys</b>. On success, write our response into
  153. * <b>reply_out</b>, generate <b>keys_out_len</b> bytes worth of key material
  154. * in <b>keys_out_len</b>, a hidden service nonce to <b>rend_nonce_out</b>,
  155. * and return the length of the reply. On failure, return -1.
  156. */
  157. int
  158. onion_skin_server_handshake(int type,
  159. const uint8_t *onion_skin, size_t onionskin_len,
  160. const server_onion_keys_t *keys,
  161. uint8_t *reply_out,
  162. uint8_t *keys_out, size_t keys_out_len,
  163. uint8_t *rend_nonce_out)
  164. {
  165. int r = -1;
  166. switch (type) {
  167. case ONION_HANDSHAKE_TYPE_TAP:
  168. if (onionskin_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  169. return -1;
  170. if (onion_skin_TAP_server_handshake((const char*)onion_skin,
  171. keys->onion_key, keys->last_onion_key,
  172. (char*)reply_out,
  173. (char*)keys_out, keys_out_len)<0)
  174. return -1;
  175. r = TAP_ONIONSKIN_REPLY_LEN;
  176. memcpy(rend_nonce_out, reply_out+DH1024_KEY_LEN, DIGEST_LEN);
  177. break;
  178. case ONION_HANDSHAKE_TYPE_FAST:
  179. if (onionskin_len != CREATE_FAST_LEN)
  180. return -1;
  181. if (fast_server_handshake(onion_skin, reply_out, keys_out, keys_out_len)<0)
  182. return -1;
  183. r = CREATED_FAST_LEN;
  184. memcpy(rend_nonce_out, reply_out+DIGEST_LEN, DIGEST_LEN);
  185. break;
  186. case ONION_HANDSHAKE_TYPE_NTOR:
  187. if (onionskin_len < NTOR_ONIONSKIN_LEN)
  188. return -1;
  189. {
  190. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  191. tor_assert(keys_tmp_len <= MAX_KEYS_TMP_LEN);
  192. uint8_t keys_tmp[MAX_KEYS_TMP_LEN];
  193. if (onion_skin_ntor_server_handshake(
  194. onion_skin, keys->curve25519_key_map,
  195. keys->junk_keypair,
  196. keys->my_identity,
  197. reply_out, keys_tmp, keys_tmp_len)<0) {
  198. /* no need to memwipe here, since the output will never be used */
  199. return -1;
  200. }
  201. memcpy(keys_out, keys_tmp, keys_out_len);
  202. memcpy(rend_nonce_out, keys_tmp+keys_out_len, DIGEST_LEN);
  203. memwipe(keys_tmp, 0, sizeof(keys_tmp));
  204. r = NTOR_REPLY_LEN;
  205. }
  206. break;
  207. default:
  208. /* LCOV_EXCL_START
  209. * We should have rejected this far before this point */
  210. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  211. tor_fragile_assert();
  212. return -1;
  213. /* LCOV_EXCL_STOP */
  214. }
  215. return r;
  216. }
  217. /** Perform the final (client-side) step of a circuit-creation handshake of
  218. * type <b>type</b>, using our state in <b>handshake_state</b> and the
  219. * server's response in <b>reply</b>. On success, generate <b>keys_out_len</b>
  220. * bytes worth of key material in <b>keys_out_len</b>, set
  221. * <b>rend_authenticator_out</b> to the "KH" field that can be used to
  222. * establish introduction points at this hop, and return 0. On failure,
  223. * return -1, and set *msg_out to an error message if this is worth
  224. * complaining to the user about. */
  225. int
  226. onion_skin_client_handshake(int type,
  227. const onion_handshake_state_t *handshake_state,
  228. const uint8_t *reply, size_t reply_len,
  229. uint8_t *keys_out, size_t keys_out_len,
  230. uint8_t *rend_authenticator_out,
  231. const char **msg_out)
  232. {
  233. if (handshake_state->tag != type)
  234. return -1;
  235. switch (type) {
  236. case ONION_HANDSHAKE_TYPE_TAP:
  237. if (reply_len != TAP_ONIONSKIN_REPLY_LEN) {
  238. if (msg_out)
  239. *msg_out = "TAP reply was not of the correct length.";
  240. return -1;
  241. }
  242. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  243. (const char*)reply,
  244. (char *)keys_out, keys_out_len,
  245. msg_out) < 0)
  246. return -1;
  247. memcpy(rend_authenticator_out, reply+DH1024_KEY_LEN, DIGEST_LEN);
  248. return 0;
  249. case ONION_HANDSHAKE_TYPE_FAST:
  250. if (reply_len != CREATED_FAST_LEN) {
  251. if (msg_out)
  252. *msg_out = "TAP reply was not of the correct length.";
  253. return -1;
  254. }
  255. if (fast_client_handshake(handshake_state->u.fast, reply,
  256. keys_out, keys_out_len, msg_out) < 0)
  257. return -1;
  258. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  259. return 0;
  260. case ONION_HANDSHAKE_TYPE_NTOR:
  261. if (reply_len < NTOR_REPLY_LEN) {
  262. if (msg_out)
  263. *msg_out = "ntor reply was not of the correct length.";
  264. return -1;
  265. }
  266. {
  267. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  268. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  269. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  270. reply,
  271. keys_tmp, keys_tmp_len, msg_out) < 0) {
  272. tor_free(keys_tmp);
  273. return -1;
  274. }
  275. memcpy(keys_out, keys_tmp, keys_out_len);
  276. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  277. memwipe(keys_tmp, 0, keys_tmp_len);
  278. tor_free(keys_tmp);
  279. }
  280. return 0;
  281. default:
  282. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  283. tor_fragile_assert();
  284. return -1;
  285. }
  286. }