rendmid.c 9.0 KB

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