rendmid.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file rendmid.c
  6. * \brief Implement introductions points and rendezvous points.
  7. **/
  8. #include "core/or/or.h"
  9. #include "core/or/channel.h"
  10. #include "core/or/circuitlist.h"
  11. #include "core/or/circuituse.h"
  12. #include "app/config/config.h"
  13. #include "lib/crypt_ops/crypto_cipher.h"
  14. #include "core/or/dos.h"
  15. #include "core/or/relay.h"
  16. #include "feature/rend/rendmid.h"
  17. #include "feature/stats/rephist.h"
  18. #include "feature/hs/hs_circuitmap.h"
  19. #include "feature/hs/hs_intropoint.h"
  20. #include "core/or/or_circuit_st.h"
  21. /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
  22. * setting the circuit's purpose and service pk digest.
  23. */
  24. int
  25. rend_mid_establish_intro_legacy(or_circuit_t *circ, const uint8_t *request,
  26. size_t request_len)
  27. {
  28. crypto_pk_t *pk = NULL;
  29. char buf[DIGEST_LEN+9];
  30. char expected_digest[DIGEST_LEN];
  31. char pk_digest[DIGEST_LEN];
  32. size_t asn1len;
  33. or_circuit_t *c;
  34. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  35. int reason = END_CIRC_REASON_INTERNAL;
  36. log_info(LD_REND,
  37. "Received a legacy ESTABLISH_INTRO request on circuit %u",
  38. (unsigned) circ->p_circ_id);
  39. if (!hs_intro_circuit_is_suitable_for_establish_intro(circ)) {
  40. reason = END_CIRC_REASON_TORPROTOCOL;
  41. goto err;
  42. }
  43. if (request_len < 2+DIGEST_LEN)
  44. goto truncated;
  45. /* First 2 bytes: length of asn1-encoded key. */
  46. asn1len = ntohs(get_uint16(request));
  47. /* Next asn1len bytes: asn1-encoded key. */
  48. if (request_len < 2+DIGEST_LEN+asn1len)
  49. goto truncated;
  50. pk = crypto_pk_asn1_decode((char*)(request+2), asn1len);
  51. if (!pk) {
  52. reason = END_CIRC_REASON_TORPROTOCOL;
  53. log_warn(LD_PROTOCOL, "Couldn't decode public key.");
  54. goto err;
  55. }
  56. /* Next 20 bytes: Hash of rend_circ_nonce | "INTRODUCE" */
  57. memcpy(buf, circ->rend_circ_nonce, DIGEST_LEN);
  58. memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
  59. if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
  60. log_warn(LD_BUG, "Internal error computing digest.");
  61. goto err;
  62. }
  63. if (tor_memneq(expected_digest, request+2+asn1len, DIGEST_LEN)) {
  64. log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
  65. reason = END_CIRC_REASON_TORPROTOCOL;
  66. goto err;
  67. }
  68. /* Rest of body: signature of previous data */
  69. if (crypto_pk_public_checksig_digest(pk,
  70. (char*)request, 2+asn1len+DIGEST_LEN,
  71. (char*)(request+2+DIGEST_LEN+asn1len),
  72. request_len-(2+DIGEST_LEN+asn1len))<0) {
  73. log_warn(LD_PROTOCOL,
  74. "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
  75. reason = END_CIRC_REASON_TORPROTOCOL;
  76. goto err;
  77. }
  78. /* The request is valid. First, compute the hash of the service's PK.*/
  79. if (crypto_pk_get_digest(pk, pk_digest)<0) {
  80. log_warn(LD_BUG, "Internal error: couldn't hash public key.");
  81. goto err;
  82. }
  83. crypto_pk_free(pk); /* don't need it anymore */
  84. pk = NULL; /* so we don't free it again if err */
  85. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  86. pk_digest, REND_SERVICE_ID_LEN);
  87. /* Close any other intro circuits with the same pk. */
  88. c = NULL;
  89. while ((c = hs_circuitmap_get_intro_circ_v2_relay_side(
  90. (const uint8_t *)pk_digest))) {
  91. log_info(LD_REND, "Replacing old circuit for service %s",
  92. safe_str(serviceid));
  93. circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
  94. /* Now it's marked, and it won't be returned next time. */
  95. }
  96. /* Acknowledge the request. */
  97. if (hs_intro_send_intro_established_cell(circ) < 0) {
  98. log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
  99. goto err_no_close;
  100. }
  101. /* Now, set up this circuit. */
  102. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
  103. hs_circuitmap_register_intro_circ_v2_relay_side(circ, (uint8_t *)pk_digest);
  104. log_info(LD_REND,
  105. "Established introduction point on circuit %u for service %s",
  106. (unsigned) circ->p_circ_id, safe_str(serviceid));
  107. return 0;
  108. truncated:
  109. log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
  110. reason = END_CIRC_REASON_TORPROTOCOL;
  111. err:
  112. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  113. err_no_close:
  114. if (pk) crypto_pk_free(pk);
  115. return -1;
  116. }
  117. /** Process an INTRODUCE1 cell by finding the corresponding introduction
  118. * circuit, and relaying the body of the INTRODUCE1 cell inside an
  119. * INTRODUCE2 cell.
  120. */
  121. int
  122. rend_mid_introduce_legacy(or_circuit_t *circ, const uint8_t *request,
  123. size_t request_len)
  124. {
  125. or_circuit_t *intro_circ;
  126. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  127. char nak_body[1];
  128. log_info(LD_REND, "Received an INTRODUCE1 request on circuit %u",
  129. (unsigned)circ->p_circ_id);
  130. /* At this point, we know that the circuit is valid for an INTRODUCE1
  131. * because the validation has been made before calling this function. */
  132. tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_OR);
  133. tor_assert(!circ->base_.n_chan);
  134. /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
  135. * obsolete; however, there isn't much reason to do so, and we're going
  136. * to revise this protocol anyway.
  137. */
  138. if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
  139. DH1024_KEY_LEN+CIPHER_KEY_LEN+
  140. PKCS1_OAEP_PADDING_OVERHEAD)) {
  141. log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %u; "
  142. "responding with nack.",
  143. (unsigned)circ->p_circ_id);
  144. goto err;
  145. }
  146. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  147. (char*)request, REND_SERVICE_ID_LEN);
  148. /* The first 20 bytes are all we look at: they have a hash of the service's
  149. * PK. */
  150. intro_circ = hs_circuitmap_get_intro_circ_v2_relay_side(
  151. (const uint8_t*)request);
  152. if (!intro_circ) {
  153. log_info(LD_REND,
  154. "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
  155. "responding with nack.",
  156. safe_str(serviceid), (unsigned)circ->p_circ_id);
  157. goto err;
  158. }
  159. log_info(LD_REND,
  160. "Sending introduction request for service %s "
  161. "from circ %u to circ %u",
  162. safe_str(serviceid), (unsigned)circ->p_circ_id,
  163. (unsigned)intro_circ->p_circ_id);
  164. /* Great. Now we just relay the cell down the circuit. */
  165. if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
  166. RELAY_COMMAND_INTRODUCE2,
  167. (char*)request, request_len, NULL)) {
  168. log_warn(LD_GENERAL,
  169. "Unable to send INTRODUCE2 cell to Tor client.");
  170. /* Stop right now, the circuit has been closed. */
  171. return -1;
  172. }
  173. /* And send an ack down the client's circuit. Empty body means succeeded. */
  174. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  175. RELAY_COMMAND_INTRODUCE_ACK,
  176. NULL,0,NULL)) {
  177. log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
  178. /* Stop right now, the circuit has been closed. */
  179. return -1;
  180. }
  181. return 0;
  182. err:
  183. /* Send the client a NACK */
  184. nak_body[0] = 1;
  185. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  186. RELAY_COMMAND_INTRODUCE_ACK,
  187. nak_body, 1, NULL)) {
  188. log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
  189. }
  190. return -1;
  191. }
  192. /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
  193. * rendezvous cookie.
  194. */
  195. int
  196. rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
  197. size_t request_len)
  198. {
  199. char hexid[9];
  200. int reason = END_CIRC_REASON_TORPROTOCOL;
  201. log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
  202. (unsigned)circ->p_circ_id);
  203. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
  204. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  205. "Tried to establish rendezvous on non-OR circuit with purpose %s",
  206. circuit_purpose_to_string(circ->base_.purpose));
  207. goto err;
  208. }
  209. /* Check if we are configured to defend ourselves from clients that
  210. * attempt to establish rendezvous points directly to us. */
  211. if (channel_is_client(circ->p_chan) &&
  212. dos_should_refuse_single_hop_client()) {
  213. /* Note it down for the heartbeat log purposes. */
  214. dos_note_refuse_single_hop_client();
  215. /* Silent drop so the client has to time out before moving on. */
  216. return 0;
  217. }
  218. if (circ->base_.n_chan) {
  219. log_warn(LD_PROTOCOL,
  220. "Tried to establish rendezvous on non-edge circuit");
  221. goto err;
  222. }
  223. if (request_len != REND_COOKIE_LEN) {
  224. log_fn(LOG_PROTOCOL_WARN,
  225. LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
  226. goto err;
  227. }
  228. if (hs_circuitmap_get_rend_circ_relay_side(request)) {
  229. log_warn(LD_PROTOCOL,
  230. "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
  231. goto err;
  232. }
  233. /* Acknowledge the request. */
  234. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  235. RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
  236. "", 0, NULL)<0) {
  237. log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
  238. /* Stop right now, the circuit has been closed. */
  239. return -1;
  240. }
  241. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_POINT_WAITING);
  242. hs_circuitmap_register_rend_circ_relay_side(circ, request);
  243. base16_encode(hexid,9,(char*)request,4);
  244. log_info(LD_REND,
  245. "Established rendezvous point on circuit %u for cookie %s",
  246. (unsigned)circ->p_circ_id, hexid);
  247. return 0;
  248. err:
  249. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  250. return -1;
  251. }
  252. /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
  253. * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
  254. * connecting the two circuits.
  255. */
  256. int
  257. rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
  258. size_t request_len)
  259. {
  260. const or_options_t *options = get_options();
  261. or_circuit_t *rend_circ;
  262. char hexid[9];
  263. int reason = END_CIRC_REASON_INTERNAL;
  264. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  265. log_info(LD_REND,
  266. "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
  267. (unsigned)circ->p_circ_id);
  268. reason = END_CIRC_REASON_TORPROTOCOL;
  269. goto err;
  270. }
  271. if (request_len < REND_COOKIE_LEN) {
  272. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  273. "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
  274. (int)request_len, (unsigned)circ->p_circ_id);
  275. reason = END_CIRC_REASON_TORPROTOCOL;
  276. goto err;
  277. }
  278. base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
  279. log_info(LD_REND,
  280. "Got request for rendezvous from circuit %u to cookie %s.",
  281. (unsigned)circ->p_circ_id, hexid);
  282. rend_circ = hs_circuitmap_get_rend_circ_relay_side(request);
  283. if (!rend_circ) {
  284. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  285. "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
  286. hexid);
  287. reason = END_CIRC_REASON_TORPROTOCOL;
  288. goto err;
  289. }
  290. /* Statistics: Mark this circuit as an RP circuit so that we collect
  291. stats from it. */
  292. if (options->HiddenServiceStatistics) {
  293. circ->circuit_carries_hs_traffic_stats = 1;
  294. }
  295. /* Send the RENDEZVOUS2 cell to the client. */
  296. if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
  297. RELAY_COMMAND_RENDEZVOUS2,
  298. (char*)(request+REND_COOKIE_LEN),
  299. request_len-REND_COOKIE_LEN, NULL)) {
  300. log_warn(LD_GENERAL,
  301. "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
  302. (unsigned)rend_circ->p_circ_id);
  303. /* Stop right now, the circuit has been closed. */
  304. return -1;
  305. }
  306. /* Join the circuits. */
  307. log_info(LD_REND,
  308. "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
  309. (unsigned)circ->p_circ_id, (unsigned)rend_circ->p_circ_id, hexid);
  310. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
  311. circuit_change_purpose(TO_CIRCUIT(rend_circ),
  312. CIRCUIT_PURPOSE_REND_ESTABLISHED);
  313. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  314. rend_circ->rend_splice = circ;
  315. circ->rend_splice = rend_circ;
  316. return 0;
  317. err:
  318. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  319. return -1;
  320. }