rendmid.c 9.9 KB

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