rendmid.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2016, 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 "circuitlist.h"
  10. #include "circuituse.h"
  11. #include "config.h"
  12. #include "crypto.h"
  13. #include "relay.h"
  14. #include "rendmid.h"
  15. #include "rephist.h"
  16. #include "hs_circuitmap.h"
  17. #include "hs_intropoint.h"
  18. /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
  19. * setting the circuit's purpose and service pk digest.
  20. */
  21. int
  22. rend_mid_establish_intro_legacy(or_circuit_t *circ, const uint8_t *request,
  23. size_t request_len)
  24. {
  25. crypto_pk_t *pk = NULL;
  26. char buf[DIGEST_LEN+9];
  27. char expected_digest[DIGEST_LEN];
  28. char pk_digest[DIGEST_LEN];
  29. size_t asn1len;
  30. or_circuit_t *c;
  31. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  32. int reason = END_CIRC_REASON_INTERNAL;
  33. log_info(LD_REND,
  34. "Received an ESTABLISH_INTRO request on circuit %u",
  35. (unsigned) circ->p_circ_id);
  36. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  37. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  38. "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
  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. note_crypto_pk_op(REND_MID);
  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((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 (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  97. RELAY_COMMAND_INTRO_ESTABLISHED,
  98. "", 0, NULL)<0) {
  99. log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
  100. goto err_no_close;
  101. }
  102. /* Now, set up this circuit. */
  103. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
  104. hs_circuitmap_register_intro_circ_v2(circ, (uint8_t *)pk_digest);
  105. log_info(LD_REND,
  106. "Established introduction point on circuit %u for service %s",
  107. (unsigned) circ->p_circ_id, safe_str(serviceid));
  108. return 0;
  109. truncated:
  110. log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
  111. reason = END_CIRC_REASON_TORPROTOCOL;
  112. err:
  113. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  114. err_no_close:
  115. if (pk) crypto_pk_free(pk);
  116. return -1;
  117. }
  118. /** Process an INTRODUCE1 cell by finding the corresponding introduction
  119. * circuit, and relaying the body of the INTRODUCE1 cell inside an
  120. * INTRODUCE2 cell.
  121. */
  122. int
  123. rend_mid_introduce(or_circuit_t *circ, const uint8_t *request,
  124. size_t request_len)
  125. {
  126. or_circuit_t *intro_circ;
  127. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  128. char nak_body[1];
  129. log_info(LD_REND, "Received an INTRODUCE1 request on circuit %u",
  130. (unsigned)circ->p_circ_id);
  131. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  132. log_warn(LD_PROTOCOL,
  133. "Rejecting INTRODUCE1 on non-OR or non-edge circuit %u.",
  134. (unsigned)circ->p_circ_id);
  135. goto err;
  136. }
  137. /* We have already done an introduction on this circuit but we just
  138. received a request for another one. We block it since this might
  139. be an attempt to DoS a hidden service (#15515). */
  140. if (circ->already_received_introduce1) {
  141. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  142. "Blocking multiple introductions on the same circuit. "
  143. "Someone might be trying to attack a hidden service through "
  144. "this relay.");
  145. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  146. return -1;
  147. }
  148. circ->already_received_introduce1 = 1;
  149. /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
  150. * obsolete; however, there isn't much reason to do so, and we're going
  151. * to revise this protocol anyway.
  152. */
  153. if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
  154. DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
  155. log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %u; "
  156. "responding with nack.",
  157. (unsigned)circ->p_circ_id);
  158. goto err;
  159. }
  160. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  161. (char*)request, REND_SERVICE_ID_LEN);
  162. /* The first 20 bytes are all we look at: they have a hash of the service's
  163. * PK. */
  164. intro_circ = hs_circuitmap_get_intro_circ_v2((const uint8_t*)request);
  165. if (!intro_circ) {
  166. log_info(LD_REND,
  167. "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
  168. "responding with nack.",
  169. safe_str(serviceid), (unsigned)circ->p_circ_id);
  170. goto err;
  171. }
  172. log_info(LD_REND,
  173. "Sending introduction request for service %s "
  174. "from circ %u to circ %u",
  175. safe_str(serviceid), (unsigned)circ->p_circ_id,
  176. (unsigned)intro_circ->p_circ_id);
  177. /* Great. Now we just relay the cell down the circuit. */
  178. if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
  179. RELAY_COMMAND_INTRODUCE2,
  180. (char*)request, request_len, NULL)) {
  181. log_warn(LD_GENERAL,
  182. "Unable to send INTRODUCE2 cell to Tor client.");
  183. /* Stop right now, the circuit has been closed. */
  184. return -1;
  185. }
  186. /* And send an ack down the client's circuit. Empty body means succeeded. */
  187. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  188. RELAY_COMMAND_INTRODUCE_ACK,
  189. NULL,0,NULL)) {
  190. log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
  191. /* Stop right now, the circuit has been closed. */
  192. return -1;
  193. }
  194. return 0;
  195. err:
  196. /* Send the client a NACK */
  197. nak_body[0] = 1;
  198. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  199. RELAY_COMMAND_INTRODUCE_ACK,
  200. nak_body, 1, NULL)) {
  201. log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
  202. }
  203. return -1;
  204. }
  205. /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
  206. * rendezvous cookie.
  207. */
  208. int
  209. rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
  210. size_t request_len)
  211. {
  212. char hexid[9];
  213. int reason = END_CIRC_REASON_TORPROTOCOL;
  214. log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
  215. (unsigned)circ->p_circ_id);
  216. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
  217. log_warn(LD_PROTOCOL,
  218. "Tried to establish rendezvous on non-OR circuit with purpose %s",
  219. circuit_purpose_to_string(circ->base_.purpose));
  220. goto err;
  221. }
  222. if (circ->base_.n_chan) {
  223. log_warn(LD_PROTOCOL,
  224. "Tried to establish rendezvous on non-edge circuit");
  225. goto err;
  226. }
  227. if (request_len != REND_COOKIE_LEN) {
  228. log_fn(LOG_PROTOCOL_WARN,
  229. LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
  230. goto err;
  231. }
  232. if (hs_circuitmap_get_rend_circ(request)) {
  233. log_warn(LD_PROTOCOL,
  234. "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
  235. goto err;
  236. }
  237. /* Acknowledge the request. */
  238. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  239. RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
  240. "", 0, NULL)<0) {
  241. log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
  242. /* Stop right now, the circuit has been closed. */
  243. return -1;
  244. }
  245. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_POINT_WAITING);
  246. hs_circuitmap_register_rend_circ(circ, request);
  247. base16_encode(hexid,9,(char*)request,4);
  248. log_info(LD_REND,
  249. "Established rendezvous point on circuit %u for cookie %s",
  250. (unsigned)circ->p_circ_id, hexid);
  251. return 0;
  252. err:
  253. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  254. return -1;
  255. }
  256. /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
  257. * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
  258. * connecting the two circuits.
  259. */
  260. int
  261. rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
  262. size_t request_len)
  263. {
  264. const or_options_t *options = get_options();
  265. or_circuit_t *rend_circ;
  266. char hexid[9];
  267. int reason = END_CIRC_REASON_INTERNAL;
  268. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  269. log_info(LD_REND,
  270. "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
  271. (unsigned)circ->p_circ_id);
  272. reason = END_CIRC_REASON_TORPROTOCOL;
  273. goto err;
  274. }
  275. if (request_len < REND_COOKIE_LEN) {
  276. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  277. "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
  278. (int)request_len, (unsigned)circ->p_circ_id);
  279. reason = END_CIRC_REASON_TORPROTOCOL;
  280. goto err;
  281. }
  282. base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
  283. log_info(LD_REND,
  284. "Got request for rendezvous from circuit %u to cookie %s.",
  285. (unsigned)circ->p_circ_id, hexid);
  286. rend_circ = hs_circuitmap_get_rend_circ(request);
  287. if (!rend_circ) {
  288. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  289. "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
  290. hexid);
  291. reason = END_CIRC_REASON_TORPROTOCOL;
  292. goto err;
  293. }
  294. /* Statistics: Mark this circuit as an RP circuit so that we collect
  295. stats from it. */
  296. if (options->HiddenServiceStatistics) {
  297. circ->circuit_carries_hs_traffic_stats = 1;
  298. }
  299. /* Send the RENDEZVOUS2 cell to the client. */
  300. if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
  301. RELAY_COMMAND_RENDEZVOUS2,
  302. (char*)(request+REND_COOKIE_LEN),
  303. request_len-REND_COOKIE_LEN, NULL)) {
  304. log_warn(LD_GENERAL,
  305. "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
  306. (unsigned)rend_circ->p_circ_id);
  307. /* Stop right now, the circuit has been closed. */
  308. return -1;
  309. }
  310. /* Join the circuits. */
  311. log_info(LD_REND,
  312. "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
  313. (unsigned)circ->p_circ_id, (unsigned)rend_circ->p_circ_id, hexid);
  314. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
  315. circuit_change_purpose(TO_CIRCUIT(rend_circ),
  316. CIRCUIT_PURPOSE_REND_ESTABLISHED);
  317. hs_circuitmap_remove_circuit(circ);
  318. rend_circ->rend_splice = circ;
  319. circ->rend_splice = rend_circ;
  320. return 0;
  321. err:
  322. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  323. return -1;
  324. }