rendclient.c 15 KB

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