rendmid.c 12 KB

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