rendmid.c 13 KB

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