rendclient.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* Copyright 2004 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /* send the introduce cell */
  6. void
  7. rend_client_introcirc_is_open(circuit_t *circ)
  8. {
  9. assert(circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  10. assert(circ->cpath);
  11. log_fn(LOG_INFO,"introcirc is open");
  12. connection_ap_attach_pending();
  13. }
  14. /* send the establish-rendezvous cell. if it fails, mark
  15. * the circ for close and return -1. else return 0.
  16. */
  17. int
  18. rend_client_send_establish_rendezvous(circuit_t *circ)
  19. {
  20. assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  21. log_fn(LOG_INFO, "Sending an ESTABLISH_RENDEZVOUS cell");
  22. if (crypto_rand(REND_COOKIE_LEN, circ->rend_cookie)<0) {
  23. log_fn(LOG_WARN, "Couldn't get random cookie");
  24. circuit_mark_for_close(circ);
  25. return -1;
  26. }
  27. if (connection_edge_send_command(NULL,circ,
  28. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  29. circ->rend_cookie, REND_COOKIE_LEN,
  30. circ->cpath->prev)<0) {
  31. /* circ is already marked for close */
  32. log_fn(LOG_WARN, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  33. return -1;
  34. }
  35. return 0;
  36. }
  37. #define LEN_REND_INTRODUCE1 (20+20+20+16+128+42)
  38. int
  39. rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc) {
  40. const char *descp;
  41. int desc_len;
  42. char payload[LEN_REND_INTRODUCE1];
  43. char tmp[20+20+128];
  44. rend_service_descriptor_t *parsed=NULL;
  45. crypt_path_t *cpath;
  46. assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  47. assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
  48. assert(!rend_cmp_service_ids(introcirc->rend_query, rendcirc->rend_query));
  49. if(rend_cache_lookup(introcirc->rend_query, &descp, &desc_len) < 1) {
  50. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.",
  51. introcirc->rend_query);
  52. goto err;
  53. }
  54. parsed = rend_parse_service_descriptor(descp,desc_len);
  55. if (!parsed) {
  56. log_fn(LOG_WARN,"Couldn't parse service descriptor");
  57. goto err;
  58. }
  59. /* first 20 bytes of payload are the hash of bob's pk */
  60. if (crypto_pk_get_digest(parsed->pk, payload)<0) {
  61. log_fn(LOG_WARN, "Couldn't hash public key.");
  62. goto err;
  63. }
  64. /* Initialize the pending_final_cpath and start the DH handshake. */
  65. cpath = rendcirc->build_state->pending_final_cpath =
  66. tor_malloc_zero(sizeof(crypt_path_t));
  67. if (!(cpath->handshake_state = crypto_dh_new())) {
  68. log_fn(LOG_WARN, "Couldn't allocate DH");
  69. goto err;
  70. }
  71. if (crypto_dh_generate_public(cpath->handshake_state)<0) {
  72. log_fn(LOG_WARN, "Couldn't generate g^x");
  73. goto err;
  74. }
  75. /* write the remaining items into tmp */
  76. strncpy(tmp, rendcirc->build_state->chosen_exit, 20); /* nul pads */
  77. memcpy(tmp+20, rendcirc->rend_cookie, 20);
  78. if (crypto_dh_get_public(cpath->handshake_state, tmp+40, 128)<0) {
  79. log_fn(LOG_WARN, "Couldn't extract g^x");
  80. goto err;
  81. }
  82. if(crypto_pk_public_hybrid_encrypt(parsed->pk, tmp,
  83. 20+20+128, payload+20,
  84. PK_PKCS1_OAEP_PADDING) < 0) {
  85. log_fn(LOG_WARN,"hybrid pk encrypt failed.");
  86. goto err;
  87. }
  88. rend_service_descriptor_free(parsed);
  89. if (connection_edge_send_command(NULL, introcirc,
  90. RELAY_COMMAND_INTRODUCE1,
  91. payload, LEN_REND_INTRODUCE1,
  92. introcirc->cpath->prev)<0) {
  93. /* introcirc is already marked for close. leave rendcirc alone. */
  94. log_fn(LOG_WARN, "Couldn't send INTRODUCE1 cell");
  95. return -1;
  96. }
  97. /* we don't need it anymore, plus it's been used. send the destroy. */
  98. circuit_mark_for_close(introcirc);
  99. return 0;
  100. err:
  101. if(parsed)
  102. rend_service_descriptor_free(parsed);
  103. circuit_mark_for_close(introcirc);
  104. circuit_mark_for_close(rendcirc);
  105. return -1;
  106. }
  107. /* send the rendezvous cell */
  108. void
  109. rend_client_rendcirc_is_open(circuit_t *circ)
  110. {
  111. assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  112. assert(circ->cpath);
  113. log_fn(LOG_INFO,"rendcirc is open");
  114. /* generate a rendezvous cookie, store it in circ */
  115. if (rend_client_send_establish_rendezvous(circ) < 0) {
  116. return;
  117. }
  118. connection_ap_attach_pending();
  119. }
  120. int
  121. rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len)
  122. {
  123. /* we just got an ack for our establish-rendezvous. switch purposes. */
  124. if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  125. log_fn(LOG_WARN,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
  126. circuit_mark_for_close(circ);
  127. return -1;
  128. }
  129. log_fn(LOG_INFO,"Got rendezvous ack. This circuit is now ready for rendezvous.");
  130. circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
  131. return 0;
  132. }
  133. /* bob sent us a rendezvous cell, join the circs. */
  134. int
  135. rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len)
  136. {
  137. connection_t *apconn;
  138. if(circ->purpose != CIRCUIT_PURPOSE_C_REND_READY ||
  139. !circ->build_state->pending_final_cpath) {
  140. log_fn(LOG_WARN,"Got rendezvous2 cell from Bob, but not expecting it. Closing.");
  141. circuit_mark_for_close(circ);
  142. return -1;
  143. }
  144. /* XXX
  145. * take 'request' and 'circ->build_state->pending_final_cpath'
  146. * and do the right thing to circ
  147. */
  148. circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  149. for(apconn = circ->p_streams; apconn; apconn = apconn->next_stream) {
  150. if(connection_ap_handshake_send_begin(apconn, circ) < 0)
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. /* Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  156. * are waiting on query. If success==1, move them to the next state.
  157. * If success==0, fail them.
  158. */
  159. void rend_client_desc_fetched(char *query, int success) {
  160. connection_t **carray;
  161. connection_t *conn;
  162. int n, i;
  163. get_connection_array(&carray, &n);
  164. for (i = 0; i < n; ++i) {
  165. conn = carray[i];
  166. if (conn->type != CONN_TYPE_AP ||
  167. conn->state != AP_CONN_STATE_RENDDESC_WAIT)
  168. continue;
  169. if (rend_cmp_service_ids(conn->rend_query, query))
  170. continue;
  171. /* great, this guy was waiting */
  172. if(success) {
  173. log_fn(LOG_INFO,"Rend desc retrieved. Launching circuits.");
  174. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  175. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  176. /* it will never work */
  177. log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
  178. connection_mark_for_close(conn,0);
  179. }
  180. } else { /* 404 */
  181. log_fn(LOG_WARN,"service id '%s' not found. Closing conn.", query);
  182. connection_mark_for_close(conn,0);
  183. }
  184. }
  185. }
  186. int rend_cmp_service_ids(char *one, char *two) {
  187. return strcasecmp(one,two);
  188. }
  189. /* strdup a nickname for a random introduction
  190. * point of query. return NULL if error.
  191. */
  192. char *rend_client_get_random_intro(char *query) {
  193. const char *descp;
  194. int desc_len;
  195. int i;
  196. smartlist_t *sl;
  197. rend_service_descriptor_t *parsed;
  198. char *choice;
  199. char *nickname;
  200. if(rend_cache_lookup(query, &descp, &desc_len) < 1) {
  201. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.", query);
  202. return NULL;
  203. }
  204. parsed = rend_parse_service_descriptor(descp,desc_len);
  205. if (!parsed) {
  206. log_fn(LOG_WARN,"Couldn't parse service descriptor");
  207. return NULL;
  208. }
  209. sl = smartlist_create();
  210. /* add the intro point nicknames */
  211. for(i=0;i<parsed->n_intro_points;i++)
  212. smartlist_add(sl,parsed->intro_points[i]);
  213. choice = smartlist_choose(sl);
  214. nickname = tor_strdup(choice);
  215. smartlist_free(sl);
  216. rend_service_descriptor_free(parsed);
  217. return nickname;
  218. }
  219. /* If address is of the form "y.onion" with a well-formed handle y,
  220. * then put a '\0' after y, lower-case it, and return 0.
  221. * Else return -1 and change nothing.
  222. */
  223. int rend_parse_rendezvous_address(char *address) {
  224. char *s;
  225. char query[REND_SERVICE_ID_LEN+1];
  226. s = strrchr(address,'.');
  227. if(!s) return -1; /* no dot */
  228. if (strcasecmp(s+1,"onion"))
  229. return -1; /* not .onion */
  230. *s = 0; /* null terminate it */
  231. if(strlcpy(query, address, REND_SERVICE_ID_LEN+1) >= REND_SERVICE_ID_LEN+1)
  232. goto failed;
  233. tor_strlower(query);
  234. if(rend_valid_service_id(query)) {
  235. tor_strlower(address);
  236. return 0; /* success */
  237. }
  238. failed:
  239. /* otherwise, return to previous state and return -1 */
  240. *s = '.';
  241. return -1;
  242. }
  243. /*
  244. Local Variables:
  245. mode:c
  246. indent-tabs-mode:nil
  247. c-basic-offset:2
  248. End:
  249. */