rendmid.c 12 KB

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