rendmid.c 9.2 KB

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