rendmid.c 9.0 KB

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