rendclient.c 15 KB

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