rendmid.c 11 KB

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