rendmid.c 11 KB

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