rendclient.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. /* Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
  38. * down introcirc if possible.
  39. */
  40. int
  41. rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc) {
  42. const char *descp;
  43. int desc_len, payload_len, r;
  44. char payload[RELAY_PAYLOAD_SIZE];
  45. char tmp[(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+DH_KEY_LEN];
  46. rend_service_descriptor_t *parsed=NULL;
  47. crypt_path_t *cpath;
  48. assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  49. assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
  50. assert(!rend_cmp_service_ids(introcirc->rend_query, rendcirc->rend_query));
  51. if(rend_cache_lookup(introcirc->rend_query, &descp, &desc_len) < 1) {
  52. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.",
  53. introcirc->rend_query);
  54. goto err;
  55. }
  56. parsed = rend_parse_service_descriptor(descp,desc_len);
  57. if (!parsed) {
  58. log_fn(LOG_WARN,"Couldn't parse service descriptor");
  59. goto err;
  60. }
  61. /* first 20 bytes of payload are the hash of bob's pk */
  62. if (crypto_pk_get_digest(parsed->pk, payload)<0) {
  63. log_fn(LOG_WARN, "Couldn't hash public key.");
  64. goto err;
  65. }
  66. /* Initialize the pending_final_cpath and start the DH handshake. */
  67. cpath = rendcirc->build_state->pending_final_cpath =
  68. tor_malloc_zero(sizeof(crypt_path_t));
  69. if (!(cpath->handshake_state = crypto_dh_new())) {
  70. log_fn(LOG_WARN, "Couldn't allocate DH");
  71. goto err;
  72. }
  73. if (crypto_dh_generate_public(cpath->handshake_state)<0) {
  74. log_fn(LOG_WARN, "Couldn't generate g^x");
  75. goto err;
  76. }
  77. /* write the remaining items into tmp */
  78. strncpy(tmp, rendcirc->build_state->chosen_exit, (MAX_NICKNAME_LEN+1)); /* nul pads */
  79. memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_cookie, REND_COOKIE_LEN);
  80. if (crypto_dh_get_public(cpath->handshake_state,
  81. tmp+MAX_NICKNAME_LEN+1+REND_COOKIE_LEN,
  82. DH_KEY_LEN)<0) {
  83. log_fn(LOG_WARN, "Couldn't extract g^x");
  84. goto err;
  85. }
  86. r = crypto_pk_public_hybrid_encrypt(parsed->pk, tmp,
  87. MAX_NICKNAME_LEN+1+REND_COOKIE_LEN+DH_KEY_LEN,
  88. payload+DIGEST_LEN,
  89. PK_PKCS1_OAEP_PADDING);
  90. if (r<0) {
  91. log_fn(LOG_WARN,"hybrid pk encrypt failed.");
  92. goto err;
  93. }
  94. payload_len = DIGEST_LEN + r;
  95. rend_service_descriptor_free(parsed);
  96. if (connection_edge_send_command(NULL, introcirc,
  97. RELAY_COMMAND_INTRODUCE1,
  98. payload, payload_len,
  99. introcirc->cpath->prev)<0) {
  100. /* introcirc is already marked for close. leave rendcirc alone. */
  101. log_fn(LOG_WARN, "Couldn't send INTRODUCE1 cell");
  102. return -1;
  103. }
  104. /* we don't need it anymore, plus it's been used. send the destroy. */
  105. circuit_mark_for_close(introcirc);
  106. return 0;
  107. err:
  108. if(parsed)
  109. rend_service_descriptor_free(parsed);
  110. circuit_mark_for_close(introcirc);
  111. circuit_mark_for_close(rendcirc);
  112. return -1;
  113. }
  114. /* send the rendezvous cell */
  115. void
  116. rend_client_rendcirc_is_open(circuit_t *circ)
  117. {
  118. assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  119. assert(circ->cpath);
  120. log_fn(LOG_INFO,"rendcirc is open");
  121. /* generate a rendezvous cookie, store it in circ */
  122. if (rend_client_send_establish_rendezvous(circ) < 0) {
  123. return;
  124. }
  125. connection_ap_attach_pending();
  126. }
  127. /* Called when we recieve a RENDEZVOUS_ESTABLISHED cell; changes the state of
  128. * the circuit to C_REND_READY.
  129. */
  130. int
  131. rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len)
  132. {
  133. /* we just got an ack for our establish-rendezvous. switch purposes. */
  134. if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  135. log_fn(LOG_WARN,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
  136. circuit_mark_for_close(circ);
  137. return -1;
  138. }
  139. log_fn(LOG_INFO,"Got rendezvous ack. This circuit is now ready for rendezvous.");
  140. circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
  141. return 0;
  142. }
  143. /* bob sent us a rendezvous cell, join the circs. */
  144. int
  145. rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len)
  146. {
  147. connection_t *apconn;
  148. crypt_path_t *hop;
  149. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  150. if(circ->purpose != CIRCUIT_PURPOSE_C_REND_READY ||
  151. !circ->build_state->pending_final_cpath) {
  152. log_fn(LOG_WARN,"Got rendezvous2 cell from Bob, but not expecting it. Closing.");
  153. circuit_mark_for_close(circ);
  154. return -1;
  155. }
  156. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  157. log_fn(LOG_WARN,"Incorrect length (%d) on RENDEZVOUS2 cell.",request_len);
  158. goto err;
  159. }
  160. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  161. assert(circ->build_state && circ->build_state->pending_final_cpath);
  162. hop = circ->build_state->pending_final_cpath;
  163. assert(hop->handshake_state);
  164. if (crypto_dh_compute_secret(hop->handshake_state, request, DH_KEY_LEN,
  165. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  166. log_fn(LOG_WARN, "Couldn't complete DH handshake");
  167. goto err;
  168. }
  169. /* ... and set up cpath. */
  170. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  171. goto err;
  172. /* Check whether the digest is right... */
  173. if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  174. log_fn(LOG_WARN, "Incorrect digest of key material");
  175. goto err;
  176. }
  177. /* All is well. Extend the circuit. */
  178. circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  179. hop->state = CPATH_STATE_OPEN;
  180. onion_append_to_cpath(&circ->cpath, hop);
  181. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  182. for(apconn = circ->p_streams; apconn; apconn = apconn->next_stream) {
  183. apconn->cpath_layer = circ->cpath->prev;
  184. /* now the last hop is different. be sure to send all the way. */
  185. if(connection_ap_handshake_send_begin(apconn, circ) < 0)
  186. return -1;
  187. }
  188. return 0;
  189. err:
  190. circuit_mark_for_close(circ);
  191. return -1;
  192. }
  193. /* Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  194. * are waiting on query. If success==1, move them to the next state.
  195. * If success==0, fail them.
  196. */
  197. void rend_client_desc_fetched(char *query, int success) {
  198. connection_t **carray;
  199. connection_t *conn;
  200. int n, i;
  201. get_connection_array(&carray, &n);
  202. for (i = 0; i < n; ++i) {
  203. conn = carray[i];
  204. if (conn->type != CONN_TYPE_AP ||
  205. conn->state != AP_CONN_STATE_RENDDESC_WAIT)
  206. continue;
  207. if (rend_cmp_service_ids(conn->rend_query, query))
  208. continue;
  209. /* great, this guy was waiting */
  210. if(success) {
  211. log_fn(LOG_INFO,"Rend desc retrieved. Launching circuits.");
  212. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  213. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  214. /* it will never work */
  215. log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
  216. connection_mark_for_close(conn,0);
  217. }
  218. } else { /* 404 */
  219. log_fn(LOG_WARN,"service id '%s' not found. Closing conn.", query);
  220. connection_mark_for_close(conn,0);
  221. }
  222. }
  223. }
  224. int rend_cmp_service_ids(char *one, char *two) {
  225. return strcasecmp(one,two);
  226. }
  227. /* strdup a nickname for a random introduction
  228. * point of query. return NULL if error.
  229. */
  230. char *rend_client_get_random_intro(char *query) {
  231. const char *descp;
  232. int desc_len;
  233. int i;
  234. smartlist_t *sl;
  235. rend_service_descriptor_t *parsed;
  236. char *choice;
  237. char *nickname;
  238. if(rend_cache_lookup(query, &descp, &desc_len) < 1) {
  239. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.", query);
  240. return NULL;
  241. }
  242. parsed = rend_parse_service_descriptor(descp,desc_len);
  243. if (!parsed) {
  244. log_fn(LOG_WARN,"Couldn't parse service descriptor");
  245. return NULL;
  246. }
  247. sl = smartlist_create();
  248. /* add the intro point nicknames */
  249. for(i=0;i<parsed->n_intro_points;i++)
  250. smartlist_add(sl,parsed->intro_points[i]);
  251. choice = smartlist_choose(sl);
  252. nickname = tor_strdup(choice);
  253. smartlist_free(sl);
  254. rend_service_descriptor_free(parsed);
  255. return nickname;
  256. }
  257. /* If address is of the form "y.onion" with a well-formed handle y,
  258. * then put a '\0' after y, lower-case it, and return 0.
  259. * Else return -1 and change nothing.
  260. */
  261. int rend_parse_rendezvous_address(char *address) {
  262. char *s;
  263. char query[REND_SERVICE_ID_LEN+1];
  264. s = strrchr(address,'.');
  265. if(!s) return -1; /* no dot */
  266. if (strcasecmp(s+1,"onion"))
  267. return -1; /* not .onion */
  268. *s = 0; /* null terminate it */
  269. if(strlcpy(query, address, REND_SERVICE_ID_LEN+1) >= REND_SERVICE_ID_LEN+1)
  270. goto failed;
  271. tor_strlower(query);
  272. if(rend_valid_service_id(query)) {
  273. tor_strlower(address);
  274. return 0; /* success */
  275. }
  276. failed:
  277. /* otherwise, return to previous state and return -1 */
  278. *s = '.';
  279. return -1;
  280. }
  281. /*
  282. Local Variables:
  283. mode:c
  284. indent-tabs-mode:nil
  285. c-basic-offset:2
  286. End:
  287. */