rendmid.c 11 KB

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