rendmid.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 (relay_send_command_from_edge(0, 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. log_info(LD_REND, "Received an INTRODUCE1 request on circuit %d",
  119. circ->p_circ_id);
  120. if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
  121. log_warn(LD_PROTOCOL,
  122. "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d.",
  123. circ->p_circ_id);
  124. goto err;
  125. }
  126. /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
  127. * obsolete; however, there isn't much reason to do so, and we're going
  128. * to revise this protocol anyway.
  129. */
  130. if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
  131. DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
  132. log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %d; "
  133. "responding with nack.",
  134. circ->p_circ_id);
  135. goto err;
  136. }
  137. base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request,10);
  138. /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
  139. intro_circ = circuit_get_intro_point(request);
  140. if (!intro_circ) {
  141. log_info(LD_REND,
  142. "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; "
  143. "responding with nack.",
  144. safe_str(serviceid), circ->p_circ_id);
  145. goto err;
  146. }
  147. log_info(LD_REND,
  148. "Sending introduction request for service %s "
  149. "from circ %d to circ %d",
  150. safe_str(serviceid), circ->p_circ_id,
  151. intro_circ->p_circ_id);
  152. /* Great. Now we just relay the cell down the circuit. */
  153. if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
  154. RELAY_COMMAND_INTRODUCE2,
  155. request, request_len, NULL)) {
  156. log_warn(LD_GENERAL,
  157. "Unable to send INTRODUCE2 cell to Tor client.");
  158. goto err;
  159. }
  160. /* And sent an ack down Alice's circuit. Empty body means succeeded. */
  161. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  162. RELAY_COMMAND_INTRODUCE_ACK,
  163. NULL,0,NULL)) {
  164. log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
  165. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  166. return -1;
  167. }
  168. return 0;
  169. err:
  170. /* Send the client an NACK */
  171. nak_body[0] = 1;
  172. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  173. RELAY_COMMAND_INTRODUCE_ACK,
  174. nak_body, 1, NULL)) {
  175. log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
  176. /* Is this right? */
  177. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  178. }
  179. return -1;
  180. }
  181. /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
  182. * rendezvous cookie.
  183. */
  184. int
  185. rend_mid_establish_rendezvous(or_circuit_t *circ, const char *request,
  186. size_t request_len)
  187. {
  188. char hexid[9];
  189. int reason = END_CIRC_REASON_TORPROTOCOL;
  190. log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %d",
  191. circ->p_circ_id);
  192. if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
  193. log_warn(LD_PROTOCOL,
  194. "Tried to establish rendezvous on non-OR or non-edge circuit.");
  195. goto err;
  196. }
  197. if (request_len != REND_COOKIE_LEN) {
  198. log_warn(LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
  199. goto err;
  200. }
  201. if (circuit_get_rendezvous(request)) {
  202. log_warn(LD_PROTOCOL,
  203. "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
  204. goto err;
  205. }
  206. /* Acknowledge the request. */
  207. if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
  208. RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
  209. "", 0, NULL)<0) {
  210. log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
  211. reason = END_CIRC_REASON_INTERNAL;
  212. goto err;
  213. }
  214. circ->_base.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
  215. memcpy(circ->rend_token, request, REND_COOKIE_LEN);
  216. base16_encode(hexid,9,request,4);
  217. log_info(LD_REND,
  218. "Established rendezvous point on circuit %d for cookie %s",
  219. circ->p_circ_id, hexid);
  220. return 0;
  221. err:
  222. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  223. return -1;
  224. }
  225. /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
  226. * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
  227. * connecting the two circuits.
  228. */
  229. int
  230. rend_mid_rendezvous(or_circuit_t *circ, const char *request,
  231. size_t request_len)
  232. {
  233. or_circuit_t *rend_circ;
  234. char hexid[9];
  235. int reason = END_CIRC_REASON_INTERNAL;
  236. base16_encode(hexid,9,request,request_len<4?request_len:4);
  237. if (request_len>=4) {
  238. log_info(LD_REND,
  239. "Got request for rendezvous from circuit %d to cookie %s.",
  240. circ->p_circ_id, hexid);
  241. }
  242. if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
  243. log_info(LD_REND,
  244. "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
  245. circ->p_circ_id);
  246. reason = END_CIRC_REASON_TORPROTOCOL;
  247. goto err;
  248. }
  249. if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
  250. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  251. "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d.",
  252. (int)request_len, circ->p_circ_id);
  253. reason = END_CIRC_REASON_TORPROTOCOL;
  254. goto err;
  255. }
  256. rend_circ = circuit_get_rendezvous(request);
  257. if (!rend_circ) {
  258. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  259. "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
  260. hexid);
  261. reason = END_CIRC_REASON_TORPROTOCOL;
  262. goto err;
  263. }
  264. /* Send the RENDEZVOUS2 cell to Alice. */
  265. if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
  266. RELAY_COMMAND_RENDEZVOUS2,
  267. request+REND_COOKIE_LEN,
  268. request_len-REND_COOKIE_LEN, NULL)) {
  269. log_warn(LD_GENERAL,
  270. "Unable to send RENDEZVOUS2 cell to client on circuit %d.",
  271. rend_circ->p_circ_id);
  272. goto err;
  273. }
  274. /* Join the circuits. */
  275. log_info(LD_REND,
  276. "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
  277. circ->p_circ_id, rend_circ->p_circ_id, hexid);
  278. circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
  279. rend_circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
  280. memset(circ->rend_token, 0, REND_COOKIE_LEN);
  281. rend_circ->rend_splice = circ;
  282. circ->rend_splice = rend_circ;
  283. return 0;
  284. err:
  285. circuit_mark_for_close(TO_CIRCUIT(circ), reason);
  286. return -1;
  287. }