rendmid.c 11 KB

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