onion_tap.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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-2016, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file onion_tap.c
  8. * \brief Functions to implement the original Tor circuit extension handshake
  9. * (a.k.a TAP).
  10. *
  11. * The "TAP" handshake is the first one that was widely used in Tor: It
  12. * combines RSA1024-OAEP and AES128-CTR to perform a hybrid encryption over
  13. * the first message DH1024 key exchange. (The RSA-encrypted part of the
  14. * encryption is authenticated; the AES-encrypted part isn't. This was
  15. * not a smart choice.)
  16. *
  17. * We didn't call it "TAP" ourselves -- Ian Goldberg named it in "On the
  18. * Security of the Tor Authentication Protocol". (Spoiler: it's secure, but
  19. * its security is kind of fragile and implementation dependent. Never modify
  20. * this implementation without reading and understanding that paper at least.)
  21. *
  22. * We have deprecated TAP since the ntor handshake came into general use. It
  23. * is still used for hidden service IP and RP connections, however.
  24. *
  25. * This handshake, like the other circuit-extension handshakes, is
  26. * invoked from onion.c.
  27. **/
  28. #include "or.h"
  29. #include "config.h"
  30. #include "onion_tap.h"
  31. #include "rephist.h"
  32. /*----------------------------------------------------------------------*/
  33. /** Given a router's 128 byte public key,
  34. * stores the following in onion_skin_out:
  35. * - [42 bytes] OAEP padding
  36. * - [16 bytes] Symmetric key for encrypting blob past RSA
  37. * - [70 bytes] g^x part 1 (inside the RSA)
  38. * - [58 bytes] g^x part 2 (symmetrically encrypted)
  39. *
  40. * Stores the DH private key into handshake_state_out for later completion
  41. * of the handshake.
  42. *
  43. * The meeting point/cookies and auth are zeroed out for now.
  44. */
  45. int
  46. onion_skin_TAP_create(crypto_pk_t *dest_router_key,
  47. crypto_dh_t **handshake_state_out,
  48. char *onion_skin_out) /* TAP_ONIONSKIN_CHALLENGE_LEN bytes */
  49. {
  50. char challenge[DH_KEY_LEN];
  51. crypto_dh_t *dh = NULL;
  52. int dhbytes, pkbytes;
  53. tor_assert(dest_router_key);
  54. tor_assert(handshake_state_out);
  55. tor_assert(onion_skin_out);
  56. *handshake_state_out = NULL;
  57. memset(onion_skin_out, 0, TAP_ONIONSKIN_CHALLENGE_LEN);
  58. if (!(dh = crypto_dh_new(DH_TYPE_CIRCUIT)))
  59. goto err;
  60. dhbytes = crypto_dh_get_bytes(dh);
  61. pkbytes = (int) crypto_pk_keysize(dest_router_key);
  62. tor_assert(dhbytes == 128);
  63. tor_assert(pkbytes == 128);
  64. if (crypto_dh_get_public(dh, challenge, dhbytes))
  65. goto err;
  66. note_crypto_pk_op(ENC_ONIONSKIN);
  67. /* set meeting point, meeting cookie, etc here. Leave zero for now. */
  68. if (crypto_pk_public_hybrid_encrypt(dest_router_key, onion_skin_out,
  69. TAP_ONIONSKIN_CHALLENGE_LEN,
  70. challenge, DH_KEY_LEN,
  71. PK_PKCS1_OAEP_PADDING, 1)<0)
  72. goto err;
  73. memwipe(challenge, 0, sizeof(challenge));
  74. *handshake_state_out = dh;
  75. return 0;
  76. err:
  77. /* LCOV_EXCL_START
  78. * We only get here if RSA encryption fails or DH keygen fails. Those
  79. * shouldn't be possible. */
  80. memwipe(challenge, 0, sizeof(challenge));
  81. if (dh) crypto_dh_free(dh);
  82. return -1;
  83. /* LCOV_EXCL_STOP */
  84. }
  85. /** Given an encrypted DH public key as generated by onion_skin_create,
  86. * and the private key for this onion router, generate the reply (128-byte
  87. * DH plus the first 20 bytes of shared key material), and store the
  88. * next key_out_len bytes of key material in key_out.
  89. */
  90. int
  91. onion_skin_TAP_server_handshake(
  92. /*TAP_ONIONSKIN_CHALLENGE_LEN*/
  93. const char *onion_skin,
  94. crypto_pk_t *private_key,
  95. crypto_pk_t *prev_private_key,
  96. /*TAP_ONIONSKIN_REPLY_LEN*/
  97. char *handshake_reply_out,
  98. char *key_out,
  99. size_t key_out_len)
  100. {
  101. char challenge[TAP_ONIONSKIN_CHALLENGE_LEN];
  102. crypto_dh_t *dh = NULL;
  103. ssize_t len;
  104. char *key_material=NULL;
  105. size_t key_material_len=0;
  106. int i;
  107. crypto_pk_t *k;
  108. len = -1;
  109. for (i=0;i<2;++i) {
  110. k = i==0?private_key:prev_private_key;
  111. if (!k)
  112. break;
  113. note_crypto_pk_op(DEC_ONIONSKIN);
  114. len = crypto_pk_private_hybrid_decrypt(k, challenge,
  115. TAP_ONIONSKIN_CHALLENGE_LEN,
  116. onion_skin,
  117. TAP_ONIONSKIN_CHALLENGE_LEN,
  118. PK_PKCS1_OAEP_PADDING,0);
  119. if (len>0)
  120. break;
  121. }
  122. if (len<0) {
  123. log_info(LD_PROTOCOL,
  124. "Couldn't decrypt onionskin: client may be using old onion key");
  125. goto err;
  126. } else if (len != DH_KEY_LEN) {
  127. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  128. "Unexpected onionskin length after decryption: %ld",
  129. (long)len);
  130. goto err;
  131. }
  132. dh = crypto_dh_new(DH_TYPE_CIRCUIT);
  133. if (!dh) {
  134. /* LCOV_EXCL_START
  135. * Failure to allocate a DH key should be impossible.
  136. */
  137. log_warn(LD_BUG, "Couldn't allocate DH key");
  138. goto err;
  139. /* LCOV_EXCL_STOP */
  140. }
  141. if (crypto_dh_get_public(dh, handshake_reply_out, DH_KEY_LEN)) {
  142. /* LCOV_EXCL_START
  143. * This can only fail if the length of the key we just allocated is too
  144. * big. That should be impossible. */
  145. log_info(LD_GENERAL, "crypto_dh_get_public failed.");
  146. goto err;
  147. /* LCOV_EXCP_STOP */
  148. }
  149. key_material_len = DIGEST_LEN+key_out_len;
  150. key_material = tor_malloc(key_material_len);
  151. len = crypto_dh_compute_secret(LOG_PROTOCOL_WARN, dh, challenge,
  152. DH_KEY_LEN, key_material,
  153. key_material_len);
  154. if (len < 0) {
  155. log_info(LD_GENERAL, "crypto_dh_compute_secret failed.");
  156. goto err;
  157. }
  158. /* send back H(K|0) as proof that we learned K. */
  159. memcpy(handshake_reply_out+DH_KEY_LEN, key_material, DIGEST_LEN);
  160. /* use the rest of the key material for our shared keys, digests, etc */
  161. memcpy(key_out, key_material+DIGEST_LEN, key_out_len);
  162. memwipe(challenge, 0, sizeof(challenge));
  163. memwipe(key_material, 0, key_material_len);
  164. tor_free(key_material);
  165. crypto_dh_free(dh);
  166. return 0;
  167. err:
  168. memwipe(challenge, 0, sizeof(challenge));
  169. if (key_material) {
  170. memwipe(key_material, 0, key_material_len);
  171. tor_free(key_material);
  172. }
  173. if (dh) crypto_dh_free(dh);
  174. return -1;
  175. }
  176. /** Finish the client side of the DH handshake.
  177. * Given the 128 byte DH reply + 20 byte hash as generated by
  178. * onion_skin_server_handshake and the handshake state generated by
  179. * onion_skin_create, verify H(K) with the first 20 bytes of shared
  180. * key material, then generate key_out_len more bytes of shared key
  181. * material and store them in key_out.
  182. *
  183. * After the invocation, call crypto_dh_free on handshake_state.
  184. */
  185. int
  186. onion_skin_TAP_client_handshake(crypto_dh_t *handshake_state,
  187. const char *handshake_reply, /* TAP_ONIONSKIN_REPLY_LEN bytes */
  188. char *key_out,
  189. size_t key_out_len,
  190. const char **msg_out)
  191. {
  192. ssize_t len;
  193. char *key_material=NULL;
  194. size_t key_material_len;
  195. tor_assert(crypto_dh_get_bytes(handshake_state) == DH_KEY_LEN);
  196. key_material_len = DIGEST_LEN + key_out_len;
  197. key_material = tor_malloc(key_material_len);
  198. len = crypto_dh_compute_secret(LOG_PROTOCOL_WARN, handshake_state,
  199. handshake_reply, DH_KEY_LEN, key_material,
  200. key_material_len);
  201. if (len < 0) {
  202. if (msg_out)
  203. *msg_out = "DH computation failed.";
  204. goto err;
  205. }
  206. if (tor_memneq(key_material, handshake_reply+DH_KEY_LEN, DIGEST_LEN)) {
  207. /* H(K) does *not* match. Something fishy. */
  208. if (msg_out)
  209. *msg_out = "Digest DOES NOT MATCH on onion handshake. Bug or attack.";
  210. goto err;
  211. }
  212. /* use the rest of the key material for our shared keys, digests, etc */
  213. memcpy(key_out, key_material+DIGEST_LEN, key_out_len);
  214. memwipe(key_material, 0, key_material_len);
  215. tor_free(key_material);
  216. return 0;
  217. err:
  218. memwipe(key_material, 0, key_material_len);
  219. tor_free(key_material);
  220. return -1;
  221. }