rendmid.c 8.1 KB

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