rendmid.c 8.0 KB

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