rendmid.c 10 KB

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