rendmid.c 9.3 KB

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