rendmid.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "channel.h"
  10. #include "circuitlist.h"
  11. #include "circuituse.h"
  12. #include "config.h"
  13. #include "dos.h"
  14. #include "relay.h"
  15. #include "rendmid.h"
  16. #include "rephist.h"
  17. /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
  18. * setting the circuit's purpose and service pk digest.
  19. */
  20. int
  21. rend_mid_establish_intro(or_circuit_t *circ, const uint8_t *request,
  22. size_t request_len)
  23. {
  24. crypto_pk_t *pk = NULL;
  25. char buf[DIGEST_LEN+9];
  26. char expected_digest[DIGEST_LEN];
  27. char pk_digest[DIGEST_LEN];
  28. size_t asn1len;
  29. or_circuit_t *c;
  30. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  31. int reason = END_CIRC_REASON_INTERNAL;
  32. log_info(LD_REND,
  33. "Received an ESTABLISH_INTRO request on circuit %u",
  34. (unsigned) circ->p_circ_id);
  35. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  36. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  37. "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
  38. reason = END_CIRC_REASON_TORPROTOCOL;
  39. goto err;
  40. }
  41. if (request_len < 2+DIGEST_LEN)
  42. goto truncated;
  43. /* First 2 bytes: length of asn1-encoded key. */
  44. asn1len = ntohs(get_uint16(request));
  45. /* Next asn1len bytes: asn1-encoded key. */
  46. if (request_len < 2+DIGEST_LEN+asn1len)
  47. goto truncated;
  48. pk = crypto_pk_asn1_decode((char*)(request+2), asn1len);
  49. if (!pk) {
  50. reason = END_CIRC_REASON_TORPROTOCOL;
  51. log_warn(LD_PROTOCOL, "Couldn't decode public key.");
  52. goto err;
  53. }
  54. /* Next 20 bytes: Hash of rend_circ_nonce | "INTRODUCE" */
  55. memcpy(buf, circ->rend_circ_nonce, DIGEST_LEN);
  56. memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
  57. if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
  58. log_warn(LD_BUG, "Internal error computing digest.");
  59. goto err;
  60. }
  61. if (tor_memneq(expected_digest, request+2+asn1len, DIGEST_LEN)) {
  62. log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
  63. reason = END_CIRC_REASON_TORPROTOCOL;
  64. goto err;
  65. }
  66. /* Rest of body: signature of previous data */
  67. note_crypto_pk_op(REND_MID);
  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 = circuit_get_intro_point((const uint8_t *)pk_digest))) {
  89. log_info(LD_REND, "Replacing old circuit for service %s",
  90. safe_str(serviceid));
  91. circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
  92. /* Now it's marked, and it won't be returned next time. */
  93. }
  94. /* Acknowledge the request. */
  95. if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  96. RELAY_COMMAND_INTRO_ESTABLISHED,
  97. "", 0, NULL)<0) {
  98. log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
  99. goto err;
  100. }
  101. /* Now, set up this circuit. */
  102. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
  103. circuit_set_intro_point_digest(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. if (pk) crypto_pk_free(pk);
  113. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  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(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. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  130. log_warn(LD_PROTOCOL,
  131. "Rejecting INTRODUCE1 on non-OR or non-edge circuit %u.",
  132. (unsigned)circ->p_circ_id);
  133. goto err;
  134. }
  135. /* We have already done an introduction on this circuit but we just
  136. received a request for another one. We block it since this might
  137. be an attempt to DoS a hidden service (#15515). */
  138. if (circ->already_received_introduce1) {
  139. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  140. "Blocking multiple introductions on the same circuit. "
  141. "Someone might be trying to attack a hidden service through "
  142. "this relay.");
  143. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  144. return -1;
  145. }
  146. circ->already_received_introduce1 = 1;
  147. /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
  148. * obsolete; however, there isn't much reason to do so, and we're going
  149. * to revise this protocol anyway.
  150. */
  151. if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
  152. DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
  153. log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %u; "
  154. "responding with nack.",
  155. (unsigned)circ->p_circ_id);
  156. goto err;
  157. }
  158. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  159. (char*)request, REND_SERVICE_ID_LEN);
  160. /* The first 20 bytes are all we look at: they have a hash of the service's
  161. * PK. */
  162. intro_circ = circuit_get_intro_point((const uint8_t*)request);
  163. if (!intro_circ) {
  164. log_info(LD_REND,
  165. "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
  166. "responding with nack.",
  167. safe_str(serviceid), (unsigned)circ->p_circ_id);
  168. goto err;
  169. }
  170. log_info(LD_REND,
  171. "Sending introduction request for service %s "
  172. "from circ %u to circ %u",
  173. safe_str(serviceid), (unsigned)circ->p_circ_id,
  174. (unsigned)intro_circ->p_circ_id);
  175. /* Great. Now we just relay the cell down the circuit. */
  176. if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
  177. RELAY_COMMAND_INTRODUCE2,
  178. (char*)request, request_len, NULL)) {
  179. log_warn(LD_GENERAL,
  180. "Unable to send INTRODUCE2 cell to Tor client.");
  181. goto err;
  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. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  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. /* Is this right? */
  200. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  201. }
  202. return -1;
  203. }
  204. /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
  205. * rendezvous cookie.
  206. */
  207. int
  208. rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
  209. size_t request_len)
  210. {
  211. char hexid[9];
  212. int reason = END_CIRC_REASON_TORPROTOCOL;
  213. log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
  214. (unsigned)circ->p_circ_id);
  215. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
  216. log_warn(LD_PROTOCOL,
  217. "Tried to establish rendezvous on non-OR circuit with purpose %s",
  218. circuit_purpose_to_string(circ->base_.purpose));
  219. goto err;
  220. }
  221. /* Check if we are configured to accept established rendezvous cells from
  222. * client or in other words tor2web clients. */
  223. if (channel_is_client(circ->p_chan) &&
  224. dos_should_refuse_single_hop_client()) {
  225. /* Note it down for the heartbeat log purposes. */
  226. dos_note_refuse_single_hop_client();
  227. /* Silent drop so the client has to time out before moving on. */
  228. return 0;
  229. }
  230. if (circ->base_.n_chan) {
  231. log_warn(LD_PROTOCOL,
  232. "Tried to establish rendezvous on non-edge circuit");
  233. goto err;
  234. }
  235. if (request_len != REND_COOKIE_LEN) {
  236. log_fn(LOG_PROTOCOL_WARN,
  237. LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
  238. goto err;
  239. }
  240. if (circuit_get_rendezvous(request)) {
  241. log_warn(LD_PROTOCOL,
  242. "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
  243. goto err;
  244. }
  245. /* Acknowledge the request. */
  246. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  247. RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
  248. "", 0, NULL)<0) {
  249. log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
  250. reason = END_CIRC_REASON_INTERNAL;
  251. goto err;
  252. }
  253. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_POINT_WAITING);
  254. circuit_set_rendezvous_cookie(circ, request);
  255. base16_encode(hexid,9,(char*)request,4);
  256. log_info(LD_REND,
  257. "Established rendezvous point on circuit %u for cookie %s",
  258. (unsigned)circ->p_circ_id, hexid);
  259. return 0;
  260. err:
  261. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  262. return -1;
  263. }
  264. /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
  265. * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
  266. * connecting the two circuits.
  267. */
  268. int
  269. rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
  270. size_t request_len)
  271. {
  272. const or_options_t *options = get_options();
  273. or_circuit_t *rend_circ;
  274. char hexid[9];
  275. int reason = END_CIRC_REASON_INTERNAL;
  276. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  277. log_info(LD_REND,
  278. "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
  279. (unsigned)circ->p_circ_id);
  280. reason = END_CIRC_REASON_TORPROTOCOL;
  281. goto err;
  282. }
  283. if (request_len < REND_COOKIE_LEN) {
  284. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  285. "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
  286. (int)request_len, (unsigned)circ->p_circ_id);
  287. reason = END_CIRC_REASON_TORPROTOCOL;
  288. goto err;
  289. }
  290. base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
  291. log_info(LD_REND,
  292. "Got request for rendezvous from circuit %u to cookie %s.",
  293. (unsigned)circ->p_circ_id, hexid);
  294. rend_circ = circuit_get_rendezvous(request);
  295. if (!rend_circ) {
  296. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  297. "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
  298. hexid);
  299. reason = END_CIRC_REASON_TORPROTOCOL;
  300. goto err;
  301. }
  302. /* Statistics: Mark this circuit as an RP circuit so that we collect
  303. stats from it. */
  304. if (options->HiddenServiceStatistics) {
  305. circ->circuit_carries_hs_traffic_stats = 1;
  306. }
  307. /* Send the RENDEZVOUS2 cell to the client. */
  308. if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
  309. RELAY_COMMAND_RENDEZVOUS2,
  310. (char*)(request+REND_COOKIE_LEN),
  311. request_len-REND_COOKIE_LEN, NULL)) {
  312. log_warn(LD_GENERAL,
  313. "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
  314. (unsigned)rend_circ->p_circ_id);
  315. goto err;
  316. }
  317. /* Join the circuits. */
  318. log_info(LD_REND,
  319. "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
  320. (unsigned)circ->p_circ_id, (unsigned)rend_circ->p_circ_id, hexid);
  321. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
  322. circuit_change_purpose(TO_CIRCUIT(rend_circ),
  323. CIRCUIT_PURPOSE_REND_ESTABLISHED);
  324. circuit_set_rendezvous_cookie(circ, NULL);
  325. rend_circ->rend_splice = circ;
  326. circ->rend_splice = rend_circ;
  327. return 0;
  328. err:
  329. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  330. return -1;
  331. }