rendmid.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2013, 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 "relay.h"
  13. #include "rendmid.h"
  14. #include "rephist.h"
  15. /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
  16. * setting the circuit's purpose and service pk digest.
  17. */
  18. int
  19. rend_mid_establish_intro(or_circuit_t *circ, const uint8_t *request,
  20. size_t request_len)
  21. {
  22. crypto_pk_t *pk = NULL;
  23. char buf[DIGEST_LEN+9];
  24. char expected_digest[DIGEST_LEN];
  25. char pk_digest[DIGEST_LEN];
  26. size_t asn1len;
  27. or_circuit_t *c;
  28. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  29. int reason = END_CIRC_REASON_INTERNAL;
  30. log_info(LD_REND,
  31. "Received an ESTABLISH_INTRO request on circuit %u",
  32. (unsigned) circ->p_circ_id);
  33. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  34. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  35. "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
  36. reason = END_CIRC_REASON_TORPROTOCOL;
  37. goto err;
  38. }
  39. if (request_len < 2+DIGEST_LEN)
  40. goto truncated;
  41. /* First 2 bytes: length of asn1-encoded key. */
  42. asn1len = ntohs(get_uint16(request));
  43. /* Next asn1len bytes: asn1-encoded key. */
  44. if (request_len < 2+DIGEST_LEN+asn1len)
  45. goto truncated;
  46. pk = crypto_pk_asn1_decode((char*)(request+2), asn1len);
  47. if (!pk) {
  48. reason = END_CIRC_REASON_TORPROTOCOL;
  49. log_warn(LD_PROTOCOL, "Couldn't decode public key.");
  50. goto err;
  51. }
  52. /* Next 20 bytes: Hash of rend_circ_nonce | "INTRODUCE" */
  53. memcpy(buf, circ->rend_circ_nonce, DIGEST_LEN);
  54. memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
  55. if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
  56. log_warn(LD_BUG, "Internal error computing digest.");
  57. goto err;
  58. }
  59. if (tor_memneq(expected_digest, request+2+asn1len, DIGEST_LEN)) {
  60. log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
  61. reason = END_CIRC_REASON_TORPROTOCOL;
  62. goto err;
  63. }
  64. /* Rest of body: signature of previous data */
  65. note_crypto_pk_op(REND_MID);
  66. if (crypto_pk_public_checksig_digest(pk,
  67. (char*)request, 2+asn1len+DIGEST_LEN,
  68. (char*)(request+2+DIGEST_LEN+asn1len),
  69. request_len-(2+DIGEST_LEN+asn1len))<0) {
  70. log_warn(LD_PROTOCOL,
  71. "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
  72. reason = END_CIRC_REASON_TORPROTOCOL;
  73. goto err;
  74. }
  75. /* The request is valid. First, compute the hash of Bob's PK.*/
  76. if (crypto_pk_get_digest(pk, pk_digest)<0) {
  77. log_warn(LD_BUG, "Internal error: couldn't hash public key.");
  78. goto err;
  79. }
  80. crypto_pk_free(pk); /* don't need it anymore */
  81. pk = NULL; /* so we don't free it again if err */
  82. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  83. pk_digest, REND_SERVICE_ID_LEN);
  84. /* Close any other intro circuits with the same pk. */
  85. c = NULL;
  86. while ((c = circuit_get_intro_point((const uint8_t *)pk_digest))) {
  87. log_info(LD_REND, "Replacing old circuit for service %s",
  88. safe_str(serviceid));
  89. circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
  90. /* Now it's marked, and it won't be returned next time. */
  91. }
  92. /* Acknowledge the request. */
  93. if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  94. RELAY_COMMAND_INTRO_ESTABLISHED,
  95. "", 0, NULL)<0) {
  96. log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
  97. goto err;
  98. }
  99. /* Now, set up this circuit. */
  100. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
  101. circuit_set_intro_point_digest(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. if (pk) crypto_pk_free(pk);
  111. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  112. return -1;
  113. }
  114. /** Process an INTRODUCE1 cell by finding the corresponding introduction
  115. * circuit, and relaying the body of the INTRODUCE1 cell inside an
  116. * INTRODUCE2 cell.
  117. */
  118. int
  119. rend_mid_introduce(or_circuit_t *circ, const uint8_t *request,
  120. size_t request_len)
  121. {
  122. or_circuit_t *intro_circ;
  123. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  124. char nak_body[1];
  125. log_info(LD_REND, "Received an INTRODUCE1 request on circuit %u",
  126. (unsigned)circ->p_circ_id);
  127. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  128. log_warn(LD_PROTOCOL,
  129. "Rejecting INTRODUCE1 on non-OR or non-edge circuit %u.",
  130. (unsigned)circ->p_circ_id);
  131. goto err;
  132. }
  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 Bob's PK. */
  147. intro_circ = circuit_get_intro_point((const uint8_t*)request);
  148. if (!intro_circ) {
  149. log_info(LD_REND,
  150. "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
  151. "responding with nack.",
  152. safe_str(serviceid), (unsigned)circ->p_circ_id);
  153. goto err;
  154. }
  155. log_info(LD_REND,
  156. "Sending introduction request for service %s "
  157. "from circ %u to circ %u",
  158. safe_str(serviceid), (unsigned)circ->p_circ_id,
  159. (unsigned)intro_circ->p_circ_id);
  160. /* Great. Now we just relay the cell down the circuit. */
  161. if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
  162. RELAY_COMMAND_INTRODUCE2,
  163. (char*)request, request_len, NULL)) {
  164. log_warn(LD_GENERAL,
  165. "Unable to send INTRODUCE2 cell to Tor client.");
  166. goto err;
  167. }
  168. /* And sent an ack down Alice's circuit. Empty body means succeeded. */
  169. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  170. RELAY_COMMAND_INTRODUCE_ACK,
  171. NULL,0,NULL)) {
  172. log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
  173. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  174. return -1;
  175. }
  176. return 0;
  177. err:
  178. /* Send the client an NACK */
  179. nak_body[0] = 1;
  180. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  181. RELAY_COMMAND_INTRODUCE_ACK,
  182. nak_body, 1, NULL)) {
  183. log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
  184. /* Is this right? */
  185. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  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 || circ->base_.n_chan) {
  201. log_warn(LD_PROTOCOL,
  202. "Tried to establish rendezvous on non-OR or non-edge circuit.");
  203. goto err;
  204. }
  205. if (request_len != REND_COOKIE_LEN) {
  206. log_fn(LOG_PROTOCOL_WARN,
  207. LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
  208. goto err;
  209. }
  210. if (circuit_get_rendezvous(request)) {
  211. log_warn(LD_PROTOCOL,
  212. "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
  213. goto err;
  214. }
  215. /* Acknowledge the request. */
  216. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  217. RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
  218. "", 0, NULL)<0) {
  219. log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
  220. reason = END_CIRC_REASON_INTERNAL;
  221. goto err;
  222. }
  223. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_POINT_WAITING);
  224. circuit_set_rendezvous_cookie(circ, request);
  225. base16_encode(hexid,9,(char*)request,4);
  226. log_info(LD_REND,
  227. "Established rendezvous point on circuit %u for cookie %s",
  228. (unsigned)circ->p_circ_id, hexid);
  229. return 0;
  230. err:
  231. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  232. return -1;
  233. }
  234. /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
  235. * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
  236. * connecting the two circuits.
  237. */
  238. int
  239. rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
  240. size_t request_len)
  241. {
  242. or_circuit_t *rend_circ;
  243. char hexid[9];
  244. int reason = END_CIRC_REASON_INTERNAL;
  245. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
  246. log_info(LD_REND,
  247. "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
  248. (unsigned)circ->p_circ_id);
  249. reason = END_CIRC_REASON_TORPROTOCOL;
  250. goto err;
  251. }
  252. if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
  253. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  254. "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
  255. (int)request_len, (unsigned)circ->p_circ_id);
  256. reason = END_CIRC_REASON_TORPROTOCOL;
  257. goto err;
  258. }
  259. base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
  260. log_info(LD_REND,
  261. "Got request for rendezvous from circuit %u to cookie %s.",
  262. (unsigned)circ->p_circ_id, hexid);
  263. rend_circ = circuit_get_rendezvous(request);
  264. if (!rend_circ) {
  265. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  266. "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
  267. hexid);
  268. reason = END_CIRC_REASON_TORPROTOCOL;
  269. goto err;
  270. }
  271. /* Send the RENDEZVOUS2 cell to Alice. */
  272. if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
  273. RELAY_COMMAND_RENDEZVOUS2,
  274. (char*)(request+REND_COOKIE_LEN),
  275. request_len-REND_COOKIE_LEN, NULL)) {
  276. log_warn(LD_GENERAL,
  277. "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
  278. (unsigned)rend_circ->p_circ_id);
  279. goto err;
  280. }
  281. /* Join the circuits. */
  282. log_info(LD_REND,
  283. "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
  284. (unsigned)circ->p_circ_id, (unsigned)rend_circ->p_circ_id, hexid);
  285. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
  286. circuit_change_purpose(TO_CIRCUIT(rend_circ),
  287. CIRCUIT_PURPOSE_REND_ESTABLISHED);
  288. circuit_set_rendezvous_cookie(circ, NULL);
  289. rend_circ->rend_splice = circ;
  290. circ->rend_splice = rend_circ;
  291. return 0;
  292. err:
  293. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  294. return -1;
  295. }