rendclient.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /* Copyright 2004 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char rendclient_c_id[] = "$Id$";
  5. /**
  6. * \file rendclient.c
  7. * \brief Client code to access location-hidden services.
  8. **/
  9. #include "or.h"
  10. /** Called when we've established a circuit to an introduction point:
  11. * send the introduction request. */
  12. void
  13. rend_client_introcirc_has_opened(circuit_t *circ)
  14. {
  15. tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  16. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  17. tor_assert(circ->cpath);
  18. log_fn(LOG_INFO,"introcirc is open");
  19. connection_ap_attach_pending();
  20. }
  21. /** Send the establish-rendezvous cell along a rendezvous circuit. if
  22. * it fails, mark the circ for close and return -1. else return 0.
  23. */
  24. static int
  25. rend_client_send_establish_rendezvous(circuit_t *circ)
  26. {
  27. tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  28. log_fn(LOG_INFO, "Sending an ESTABLISH_RENDEZVOUS cell");
  29. if (crypto_rand(circ->rend_cookie, REND_COOKIE_LEN) < 0) {
  30. log_fn(LOG_WARN, "Couldn't get random cookie");
  31. circuit_mark_for_close(circ);
  32. return -1;
  33. }
  34. if (connection_edge_send_command(NULL,circ,
  35. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  36. circ->rend_cookie, REND_COOKIE_LEN,
  37. circ->cpath->prev)<0) {
  38. /* circ is already marked for close */
  39. log_fn(LOG_WARN, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
  45. * down introcirc if possible.
  46. */
  47. int
  48. rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc) {
  49. size_t payload_len;
  50. int r;
  51. char payload[RELAY_PAYLOAD_SIZE];
  52. char tmp[1+(MAX_HEX_NICKNAME_LEN+1)+REND_COOKIE_LEN+DH_KEY_LEN];
  53. rend_cache_entry_t *entry;
  54. crypt_path_t *cpath;
  55. tor_assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  56. tor_assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
  57. tor_assert(!rend_cmp_service_ids(introcirc->rend_query, rendcirc->rend_query));
  58. if (rend_cache_lookup_entry(introcirc->rend_query, &entry) < 1) {
  59. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.",
  60. introcirc->rend_query);
  61. goto err;
  62. }
  63. /* first 20 bytes of payload are the hash of bob's pk */
  64. if (crypto_pk_get_digest(entry->parsed->pk, payload)<0) {
  65. log_fn(LOG_WARN, "Couldn't hash public key.");
  66. goto err;
  67. }
  68. /* Initialize the pending_final_cpath and start the DH handshake. */
  69. cpath = rendcirc->build_state->pending_final_cpath;
  70. if (!cpath) {
  71. cpath = rendcirc->build_state->pending_final_cpath =
  72. tor_malloc_zero(sizeof(crypt_path_t));
  73. if (!(cpath->handshake_state = crypto_dh_new())) {
  74. log_fn(LOG_WARN, "Couldn't allocate DH");
  75. goto err;
  76. }
  77. if (crypto_dh_generate_public(cpath->handshake_state)<0) {
  78. log_fn(LOG_WARN, "Couldn't generate g^x");
  79. goto err;
  80. }
  81. }
  82. /* write the remaining items into tmp */
  83. #if 0
  84. tmp[0] = 1; /* version 1 of the cell format */
  85. /* nul pads */
  86. strncpy(tmp+1, rendcirc->build_state->chosen_exit_name, (MAX_HEX_NICKNAME_LEN+1));
  87. memcpy(tmp+1+MAX_HEX_NICKNAME_LEN+1, rendcirc->rend_cookie, REND_COOKIE_LEN);
  88. #else
  89. strncpy(tmp, rendcirc->build_state->chosen_exit_name, (MAX_NICKNAME_LEN+1)); /* nul pads */
  90. memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_cookie, REND_COOKIE_LEN);
  91. #endif
  92. if (crypto_dh_get_public(cpath->handshake_state,
  93. #if 0
  94. tmp+1+MAX_HEX_NICKNAME_LEN+1+REND_COOKIE_LEN,
  95. #else
  96. tmp+MAX_NICKNAME_LEN+1+REND_COOKIE_LEN,
  97. #endif
  98. DH_KEY_LEN)<0) {
  99. log_fn(LOG_WARN, "Couldn't extract g^x");
  100. goto err;
  101. }
  102. /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
  103. * to avoid buffer overflows? */
  104. r = crypto_pk_public_hybrid_encrypt(entry->parsed->pk, payload+DIGEST_LEN, tmp,
  105. #if 0
  106. 1+MAX_HEX_NICKNAME_LEN+1+REND_COOKIE_LEN+DH_KEY_LEN,
  107. #else
  108. MAX_NICKNAME_LEN+1+REND_COOKIE_LEN+DH_KEY_LEN,
  109. #endif
  110. PK_PKCS1_OAEP_PADDING, 0);
  111. if (r<0) {
  112. log_fn(LOG_WARN,"hybrid pk encrypt failed.");
  113. goto err;
  114. }
  115. tor_assert(DIGEST_LEN + r <= RELAY_PAYLOAD_SIZE); /* we overran something */
  116. payload_len = DIGEST_LEN + r;
  117. if (connection_edge_send_command(NULL, introcirc,
  118. RELAY_COMMAND_INTRODUCE1,
  119. payload, payload_len,
  120. introcirc->cpath->prev)<0) {
  121. /* introcirc is already marked for close. leave rendcirc alone. */
  122. log_fn(LOG_WARN, "Couldn't send INTRODUCE1 cell");
  123. return -1;
  124. }
  125. /* Now, we wait for an ACK or NAK on this circuit. */
  126. introcirc->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
  127. return 0;
  128. err:
  129. circuit_mark_for_close(introcirc);
  130. circuit_mark_for_close(rendcirc);
  131. return -1;
  132. }
  133. /** Called when a rendezvous circuit is open; sends a establish
  134. * rendezvous circuit as appropriate. */
  135. void
  136. rend_client_rendcirc_has_opened(circuit_t *circ)
  137. {
  138. tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  139. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  140. log_fn(LOG_INFO,"rendcirc is open");
  141. /* generate a rendezvous cookie, store it in circ */
  142. if (rend_client_send_establish_rendezvous(circ) < 0) {
  143. return;
  144. }
  145. }
  146. /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
  147. */
  148. int
  149. rend_client_introduction_acked(circuit_t *circ,
  150. const char *request, size_t request_len)
  151. {
  152. char *nickname;
  153. circuit_t *rendcirc;
  154. if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  155. log_fn(LOG_WARN, "Received REND_INTRODUCE_ACK on unexpected circuit %d",
  156. circ->n_circ_id);
  157. circuit_mark_for_close(circ);
  158. return -1;
  159. }
  160. tor_assert(circ->build_state->chosen_exit_name);
  161. if (request_len == 0) {
  162. /* It's an ACK; the introduction point relayed our introduction request. */
  163. /* Locate the rend circ which is waiting to hear about this ack,
  164. * and tell it.
  165. */
  166. log_fn(LOG_INFO,"Received ack. Telling rend circ...");
  167. rendcirc = circuit_get_by_rend_query_and_purpose(
  168. circ->rend_query, CIRCUIT_PURPOSE_C_REND_READY);
  169. if (rendcirc) { /* remember the ack */
  170. rendcirc->purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  171. } else {
  172. log_fn(LOG_INFO,"...Found no rend circ. Dropping on the floor.");
  173. }
  174. /* close the circuit: we won't need it anymore. */
  175. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACKED;
  176. circuit_mark_for_close(circ);
  177. } else {
  178. /* It's a NAK; the introduction point didn't relay our request. */
  179. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  180. /* Remove this intro point from the set of viable introduction
  181. * points. If any remain, extend to a new one and try again.
  182. * If none remain, refetch the service descriptor.
  183. */
  184. if (rend_client_remove_intro_point(circ->build_state->chosen_exit_name,
  185. circ->rend_query) > 0) {
  186. /* There are introduction points left. re-extend the circuit to
  187. * another intro point and try again. */
  188. routerinfo_t *r;
  189. nickname = rend_client_get_random_intro(circ->rend_query);
  190. tor_assert(nickname);
  191. log_fn(LOG_INFO,"Got nack for %s from %s, extending to %s.",
  192. circ->rend_query, circ->build_state->chosen_exit_name, nickname);
  193. if (!(r = router_get_by_nickname(nickname))) {
  194. log_fn(LOG_WARN, "Advertised intro point '%s' for %s is not known. Closing.",
  195. nickname, circ->rend_query);
  196. tor_free(nickname);
  197. circuit_mark_for_close(circ);
  198. return -1;
  199. }
  200. log_fn(LOG_INFO, "Chose new intro point %s for %s (circ %d)",
  201. nickname, circ->rend_query, circ->n_circ_id);
  202. tor_free(nickname);
  203. return circuit_extend_to_new_exit(circ, r);
  204. }
  205. }
  206. return 0;
  207. }
  208. /** If we are not currently fetching a rendezvous service descriptor
  209. * for the service ID <b>query</b>, start a directory connection to fetch a
  210. * new one.
  211. */
  212. void
  213. rend_client_refetch_renddesc(const char *query)
  214. {
  215. if (connection_get_by_type_state_rendquery(CONN_TYPE_DIR, 0, query)) {
  216. log_fn(LOG_INFO,"Would fetch a new renddesc here (for %s), but one is already in progress.", query);
  217. } else {
  218. /* not one already; initiate a dir rend desc lookup */
  219. directory_get_from_dirserver(DIR_PURPOSE_FETCH_RENDDESC, query, 1);
  220. }
  221. }
  222. /** remove failed_intro from ent. if ent now has no intro points, or
  223. * service is unrecognized, then launch a new renddesc fetch.
  224. *
  225. * Return -1 if error, 0 if no intro points remain or service
  226. * unrecognized, 1 if recognized and some intro points remain.
  227. */
  228. int
  229. rend_client_remove_intro_point(char *failed_intro, const char *query)
  230. {
  231. int i, r;
  232. rend_cache_entry_t *ent;
  233. connection_t *conn;
  234. r = rend_cache_lookup_entry(query, &ent);
  235. if (r<0) {
  236. log_fn(LOG_WARN, "Malformed service ID '%s'", query);
  237. return -1;
  238. }
  239. if (r==0) {
  240. log_fn(LOG_INFO, "Unknown service %s. Re-fetching descriptor.", query);
  241. rend_client_refetch_renddesc(query);
  242. return 0;
  243. }
  244. for (i=0; i < ent->parsed->n_intro_points; ++i) {
  245. if (!strcasecmp(ent->parsed->intro_points[i], failed_intro)) {
  246. tor_free(ent->parsed->intro_points[i]);
  247. ent->parsed->intro_points[i] =
  248. ent->parsed->intro_points[--ent->parsed->n_intro_points];
  249. break;
  250. }
  251. }
  252. if (!ent->parsed->n_intro_points) {
  253. log_fn(LOG_INFO,"No more intro points remain for %s. Re-fetching descriptor.", query);
  254. rend_client_refetch_renddesc(query);
  255. /* move all pending streams back to renddesc_wait */
  256. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  257. AP_CONN_STATE_CIRCUIT_WAIT, query))) {
  258. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  259. }
  260. return 0;
  261. }
  262. log_fn(LOG_INFO,"%d options left for %s.", ent->parsed->n_intro_points, query);
  263. return 1;
  264. }
  265. /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  266. * the circuit to C_REND_READY.
  267. */
  268. int
  269. rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len)
  270. {
  271. /* we just got an ack for our establish-rendezvous. switch purposes. */
  272. if (circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  273. log_fn(LOG_WARN,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
  274. circuit_mark_for_close(circ);
  275. return -1;
  276. }
  277. log_fn(LOG_INFO,"Got rendezvous ack. This circuit is now ready for rendezvous.");
  278. circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
  279. return 0;
  280. }
  281. /** Bob sent us a rendezvous cell; join the circuits. */
  282. int
  283. rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len)
  284. {
  285. crypt_path_t *hop;
  286. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  287. if ((circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  288. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
  289. || !circ->build_state->pending_final_cpath) {
  290. log_fn(LOG_WARN,"Got rendezvous2 cell from Bob, but not expecting it. Closing.");
  291. circuit_mark_for_close(circ);
  292. return -1;
  293. }
  294. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  295. log_fn(LOG_WARN,"Incorrect length (%d) on RENDEZVOUS2 cell.",(int)request_len);
  296. goto err;
  297. }
  298. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  299. tor_assert(circ->build_state);
  300. tor_assert(circ->build_state->pending_final_cpath);
  301. hop = circ->build_state->pending_final_cpath;
  302. tor_assert(hop->handshake_state);
  303. if (crypto_dh_compute_secret(hop->handshake_state, request, DH_KEY_LEN,
  304. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  305. log_fn(LOG_WARN, "Couldn't complete DH handshake");
  306. goto err;
  307. }
  308. /* ... and set up cpath. */
  309. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  310. goto err;
  311. /* Check whether the digest is right... */
  312. if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  313. log_fn(LOG_WARN, "Incorrect digest of key material");
  314. goto err;
  315. }
  316. crypto_dh_free(hop->handshake_state);
  317. hop->handshake_state = NULL;
  318. /* All is well. Extend the circuit. */
  319. circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  320. hop->state = CPATH_STATE_OPEN;
  321. /* set the windows to default. these are the windows
  322. * that alice thinks bob has.
  323. */
  324. hop->package_window = CIRCWINDOW_START;
  325. hop->deliver_window = CIRCWINDOW_START;
  326. onion_append_to_cpath(&circ->cpath, hop);
  327. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  328. return 0;
  329. err:
  330. circuit_mark_for_close(circ);
  331. return -1;
  332. }
  333. /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  334. * are waiting on query. If there's a working cache entry here
  335. * with at least one intro point, move them to the next state;
  336. * else fail them.
  337. */
  338. void rend_client_desc_here(char *query) {
  339. connection_t *conn;
  340. rend_cache_entry_t *entry;
  341. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  342. AP_CONN_STATE_RENDDESC_WAIT, query))) {
  343. if (rend_cache_lookup_entry(conn->rend_query, &entry) == 1 &&
  344. entry->parsed->n_intro_points > 0) {
  345. /* either this fetch worked, or it failed but there was a
  346. * valid entry from before which we should reuse */
  347. log_fn(LOG_INFO,"Rend desc is usable. Launching circuits.");
  348. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  349. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  350. /* it will never work */
  351. log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
  352. conn->has_sent_end = 1;
  353. connection_mark_for_close(conn);
  354. }
  355. tor_assert(conn->state != AP_CONN_STATE_RENDDESC_WAIT); /* avoid loop */
  356. } else { /* 404, or fetch didn't get that far */
  357. log_fn(LOG_NOTICE,"Closing stream for '%s.onion': hidden service is unavailable (try again later).", query);
  358. conn->has_sent_end = 1;
  359. connection_mark_for_close(conn);
  360. }
  361. }
  362. }
  363. /** strdup a nickname for a random introduction
  364. * point of query. return NULL if error.
  365. */
  366. char *rend_client_get_random_intro(char *query) {
  367. int i;
  368. smartlist_t *sl;
  369. char *choice;
  370. char *nickname;
  371. rend_cache_entry_t *entry;
  372. if (rend_cache_lookup_entry(query, &entry) < 1) {
  373. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.", query);
  374. return NULL;
  375. }
  376. sl = smartlist_create();
  377. /* add the intro point nicknames */
  378. for (i=0;i<entry->parsed->n_intro_points;i++)
  379. smartlist_add(sl,entry->parsed->intro_points[i]);
  380. choice = smartlist_choose(sl);
  381. if (!choice) {
  382. smartlist_free(sl);
  383. return NULL;
  384. }
  385. nickname = tor_strdup(choice);
  386. smartlist_free(sl);
  387. return nickname;
  388. }