rendclient.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file rendclient.c
  6. * \brief Client code to access location-hidden services.
  7. **/
  8. #include "or.h"
  9. #include "circuitbuild.h"
  10. #include "circuitlist.h"
  11. #include "circuituse.h"
  12. #include "config.h"
  13. #include "connection.h"
  14. #include "connection_edge.h"
  15. #include "directory.h"
  16. #include "main.h"
  17. #include "nodelist.h"
  18. #include "relay.h"
  19. #include "rendclient.h"
  20. #include "rendcommon.h"
  21. #include "rephist.h"
  22. #include "router.h"
  23. #include "routerlist.h"
  24. #include "routerset.h"
  25. static extend_info_t *rend_client_get_random_intro_impl(
  26. const rend_cache_entry_t *rend_query,
  27. const int strict, const int warnings);
  28. /** Purge all potentially remotely-detectable state held in the hidden
  29. * service client code. Called on SIGNAL NEWNYM. */
  30. void
  31. rend_client_purge_state(void)
  32. {
  33. rend_cache_purge();
  34. rend_client_cancel_descriptor_fetches();
  35. rend_client_purge_last_hid_serv_requests();
  36. }
  37. /** Called when we've established a circuit to an introduction point:
  38. * send the introduction request. */
  39. void
  40. rend_client_introcirc_has_opened(origin_circuit_t *circ)
  41. {
  42. tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  43. tor_assert(circ->cpath);
  44. log_info(LD_REND,"introcirc is open");
  45. connection_ap_attach_pending();
  46. }
  47. /** Send the establish-rendezvous cell along a rendezvous circuit. if
  48. * it fails, mark the circ for close and return -1. else return 0.
  49. */
  50. static int
  51. rend_client_send_establish_rendezvous(origin_circuit_t *circ)
  52. {
  53. tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  54. tor_assert(circ->rend_data);
  55. log_info(LD_REND, "Sending an ESTABLISH_RENDEZVOUS cell");
  56. if (crypto_rand(circ->rend_data->rend_cookie, REND_COOKIE_LEN) < 0) {
  57. log_warn(LD_BUG, "Internal error: Couldn't produce random cookie.");
  58. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  59. return -1;
  60. }
  61. /* Set timestamp_dirty, because circuit_expire_building expects it,
  62. * and the rend cookie also means we've used the circ. */
  63. circ->base_.timestamp_dirty = time(NULL);
  64. /* We've attempted to use this circuit. Probe it if we fail */
  65. pathbias_count_use_attempt(circ);
  66. if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  67. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  68. circ->rend_data->rend_cookie,
  69. REND_COOKIE_LEN,
  70. circ->cpath->prev)<0) {
  71. /* circ is already marked for close */
  72. log_warn(LD_GENERAL, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. /** Extend the introduction circuit <b>circ</b> to another valid
  78. * introduction point for the hidden service it is trying to connect
  79. * to, or mark it and launch a new circuit if we can't extend it.
  80. * Return 0 on success or possible success. Return -1 and mark the
  81. * introduction circuit for close on permanent failure.
  82. *
  83. * On failure, the caller is responsible for marking the associated
  84. * rendezvous circuit for close. */
  85. static int
  86. rend_client_reextend_intro_circuit(origin_circuit_t *circ)
  87. {
  88. extend_info_t *extend_info;
  89. int result;
  90. extend_info = rend_client_get_random_intro(circ->rend_data);
  91. if (!extend_info) {
  92. log_warn(LD_REND,
  93. "No usable introduction points left for %s. Closing.",
  94. safe_str_client(circ->rend_data->onion_address));
  95. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  96. return -1;
  97. }
  98. // XXX: should we not re-extend if hs_circ_has_timed_out?
  99. if (circ->remaining_relay_early_cells) {
  100. log_info(LD_REND,
  101. "Re-extending circ %u, this time to %s.",
  102. (unsigned)circ->base_.n_circ_id,
  103. safe_str_client(extend_info_describe(extend_info)));
  104. result = circuit_extend_to_new_exit(circ, extend_info);
  105. } else {
  106. log_info(LD_REND,
  107. "Closing intro circ %u (out of RELAY_EARLY cells).",
  108. (unsigned)circ->base_.n_circ_id);
  109. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  110. /* connection_ap_handshake_attach_circuit will launch a new intro circ. */
  111. result = 0;
  112. }
  113. extend_info_free(extend_info);
  114. return result;
  115. }
  116. /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
  117. * down introcirc if possible.
  118. */
  119. int
  120. rend_client_send_introduction(origin_circuit_t *introcirc,
  121. origin_circuit_t *rendcirc)
  122. {
  123. size_t payload_len;
  124. int r, v3_shift = 0;
  125. char payload[RELAY_PAYLOAD_SIZE];
  126. char tmp[RELAY_PAYLOAD_SIZE];
  127. rend_cache_entry_t *entry;
  128. crypt_path_t *cpath;
  129. off_t dh_offset;
  130. crypto_pk_t *intro_key = NULL;
  131. int status = 0;
  132. tor_assert(introcirc->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  133. tor_assert(rendcirc->base_.purpose == CIRCUIT_PURPOSE_C_REND_READY);
  134. tor_assert(introcirc->rend_data);
  135. tor_assert(rendcirc->rend_data);
  136. tor_assert(!rend_cmp_service_ids(introcirc->rend_data->onion_address,
  137. rendcirc->rend_data->onion_address));
  138. #ifndef NON_ANONYMOUS_MODE_ENABLED
  139. tor_assert(!(introcirc->build_state->onehop_tunnel));
  140. tor_assert(!(rendcirc->build_state->onehop_tunnel));
  141. #endif
  142. if (rend_cache_lookup_entry(introcirc->rend_data->onion_address, -1,
  143. &entry) < 1) {
  144. log_info(LD_REND,
  145. "query %s didn't have valid rend desc in cache. "
  146. "Refetching descriptor.",
  147. safe_str_client(introcirc->rend_data->onion_address));
  148. rend_client_refetch_v2_renddesc(introcirc->rend_data);
  149. {
  150. connection_t *conn;
  151. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  152. AP_CONN_STATE_CIRCUIT_WAIT,
  153. introcirc->rend_data->onion_address))) {
  154. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  155. }
  156. }
  157. status = -1;
  158. goto cleanup;
  159. }
  160. /* first 20 bytes of payload are the hash of Bob's pk */
  161. intro_key = NULL;
  162. SMARTLIST_FOREACH(entry->parsed->intro_nodes, rend_intro_point_t *,
  163. intro, {
  164. if (tor_memeq(introcirc->build_state->chosen_exit->identity_digest,
  165. intro->extend_info->identity_digest, DIGEST_LEN)) {
  166. intro_key = intro->intro_key;
  167. break;
  168. }
  169. });
  170. if (!intro_key) {
  171. log_info(LD_REND, "Could not find intro key for %s at %s; we "
  172. "have a v2 rend desc with %d intro points. "
  173. "Trying a different intro point...",
  174. safe_str_client(introcirc->rend_data->onion_address),
  175. safe_str_client(extend_info_describe(
  176. introcirc->build_state->chosen_exit)),
  177. smartlist_len(entry->parsed->intro_nodes));
  178. if (rend_client_reextend_intro_circuit(introcirc)) {
  179. status = -2;
  180. goto perm_err;
  181. } else {
  182. status = -1;
  183. goto cleanup;
  184. }
  185. }
  186. if (crypto_pk_get_digest(intro_key, payload)<0) {
  187. log_warn(LD_BUG, "Internal error: couldn't hash public key.");
  188. status = -2;
  189. goto perm_err;
  190. }
  191. /* Initialize the pending_final_cpath and start the DH handshake. */
  192. cpath = rendcirc->build_state->pending_final_cpath;
  193. if (!cpath) {
  194. cpath = rendcirc->build_state->pending_final_cpath =
  195. tor_malloc_zero(sizeof(crypt_path_t));
  196. cpath->magic = CRYPT_PATH_MAGIC;
  197. if (!(cpath->rend_dh_handshake_state = crypto_dh_new(DH_TYPE_REND))) {
  198. log_warn(LD_BUG, "Internal error: couldn't allocate DH.");
  199. status = -2;
  200. goto perm_err;
  201. }
  202. if (crypto_dh_generate_public(cpath->rend_dh_handshake_state)<0) {
  203. log_warn(LD_BUG, "Internal error: couldn't generate g^x.");
  204. status = -2;
  205. goto perm_err;
  206. }
  207. }
  208. /* If version is 3, write (optional) auth data and timestamp. */
  209. if (entry->parsed->protocols & (1<<3)) {
  210. tmp[0] = 3; /* version 3 of the cell format */
  211. tmp[1] = (uint8_t)introcirc->rend_data->auth_type; /* auth type, if any */
  212. v3_shift = 1;
  213. if (introcirc->rend_data->auth_type != REND_NO_AUTH) {
  214. set_uint16(tmp+2, htons(REND_DESC_COOKIE_LEN));
  215. memcpy(tmp+4, introcirc->rend_data->descriptor_cookie,
  216. REND_DESC_COOKIE_LEN);
  217. v3_shift += 2+REND_DESC_COOKIE_LEN;
  218. }
  219. set_uint32(tmp+v3_shift+1, htonl((uint32_t)time(NULL)));
  220. v3_shift += 4;
  221. } /* if version 2 only write version number */
  222. else if (entry->parsed->protocols & (1<<2)) {
  223. tmp[0] = 2; /* version 2 of the cell format */
  224. }
  225. /* write the remaining items into tmp */
  226. if (entry->parsed->protocols & (1<<3) || entry->parsed->protocols & (1<<2)) {
  227. /* version 2 format */
  228. extend_info_t *extend_info = rendcirc->build_state->chosen_exit;
  229. int klen;
  230. /* nul pads */
  231. set_uint32(tmp+v3_shift+1, tor_addr_to_ipv4h(&extend_info->addr));
  232. set_uint16(tmp+v3_shift+5, htons(extend_info->port));
  233. memcpy(tmp+v3_shift+7, extend_info->identity_digest, DIGEST_LEN);
  234. klen = crypto_pk_asn1_encode(extend_info->onion_key,
  235. tmp+v3_shift+7+DIGEST_LEN+2,
  236. sizeof(tmp)-(v3_shift+7+DIGEST_LEN+2));
  237. set_uint16(tmp+v3_shift+7+DIGEST_LEN, htons(klen));
  238. memcpy(tmp+v3_shift+7+DIGEST_LEN+2+klen, rendcirc->rend_data->rend_cookie,
  239. REND_COOKIE_LEN);
  240. dh_offset = v3_shift+7+DIGEST_LEN+2+klen+REND_COOKIE_LEN;
  241. } else {
  242. /* Version 0. */
  243. strncpy(tmp, rendcirc->build_state->chosen_exit->nickname,
  244. (MAX_NICKNAME_LEN+1)); /* nul pads */
  245. memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_data->rend_cookie,
  246. REND_COOKIE_LEN);
  247. dh_offset = MAX_NICKNAME_LEN+1+REND_COOKIE_LEN;
  248. }
  249. if (crypto_dh_get_public(cpath->rend_dh_handshake_state, tmp+dh_offset,
  250. DH_KEY_LEN)<0) {
  251. log_warn(LD_BUG, "Internal error: couldn't extract g^x.");
  252. status = -2;
  253. goto perm_err;
  254. }
  255. note_crypto_pk_op(REND_CLIENT);
  256. /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
  257. * to avoid buffer overflows? */
  258. r = crypto_pk_public_hybrid_encrypt(intro_key, payload+DIGEST_LEN,
  259. sizeof(payload)-DIGEST_LEN,
  260. tmp,
  261. (int)(dh_offset+DH_KEY_LEN),
  262. PK_PKCS1_OAEP_PADDING, 0);
  263. if (r<0) {
  264. log_warn(LD_BUG,"Internal error: hybrid pk encrypt failed.");
  265. status = -2;
  266. goto perm_err;
  267. }
  268. payload_len = DIGEST_LEN + r;
  269. tor_assert(payload_len <= RELAY_PAYLOAD_SIZE); /* we overran something */
  270. /* Copy the rendezvous cookie from rendcirc to introcirc, so that
  271. * when introcirc gets an ack, we can change the state of the right
  272. * rendezvous circuit. */
  273. memcpy(introcirc->rend_data->rend_cookie, rendcirc->rend_data->rend_cookie,
  274. REND_COOKIE_LEN);
  275. log_info(LD_REND, "Sending an INTRODUCE1 cell");
  276. if (relay_send_command_from_edge(0, TO_CIRCUIT(introcirc),
  277. RELAY_COMMAND_INTRODUCE1,
  278. payload, payload_len,
  279. introcirc->cpath->prev)<0) {
  280. /* introcirc is already marked for close. leave rendcirc alone. */
  281. log_warn(LD_BUG, "Couldn't send INTRODUCE1 cell");
  282. status = -2;
  283. goto cleanup;
  284. }
  285. /* Now, we wait for an ACK or NAK on this circuit. */
  286. circuit_change_purpose(TO_CIRCUIT(introcirc),
  287. CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT);
  288. /* Set timestamp_dirty, because circuit_expire_building expects it
  289. * to specify when a circuit entered the _C_INTRODUCE_ACK_WAIT
  290. * state. */
  291. introcirc->base_.timestamp_dirty = time(NULL);
  292. pathbias_count_use_attempt(introcirc);
  293. goto cleanup;
  294. perm_err:
  295. if (!introcirc->base_.marked_for_close)
  296. circuit_mark_for_close(TO_CIRCUIT(introcirc), END_CIRC_REASON_INTERNAL);
  297. circuit_mark_for_close(TO_CIRCUIT(rendcirc), END_CIRC_REASON_INTERNAL);
  298. cleanup:
  299. memwipe(payload, 0, sizeof(payload));
  300. memwipe(tmp, 0, sizeof(tmp));
  301. return status;
  302. }
  303. /** Called when a rendezvous circuit is open; sends a establish
  304. * rendezvous circuit as appropriate. */
  305. void
  306. rend_client_rendcirc_has_opened(origin_circuit_t *circ)
  307. {
  308. tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  309. log_info(LD_REND,"rendcirc is open");
  310. /* generate a rendezvous cookie, store it in circ */
  311. if (rend_client_send_establish_rendezvous(circ) < 0) {
  312. return;
  313. }
  314. }
  315. /**
  316. * Called to close other intro circuits we launched in parallel
  317. * due to timeout.
  318. */
  319. static void
  320. rend_client_close_other_intros(const char *onion_address)
  321. {
  322. circuit_t *c;
  323. /* abort parallel intro circs, if any */
  324. for (c = circuit_get_global_list_(); c; c = c->next) {
  325. if ((c->purpose == CIRCUIT_PURPOSE_C_INTRODUCING ||
  326. c->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) &&
  327. !c->marked_for_close && CIRCUIT_IS_ORIGIN(c)) {
  328. origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(c);
  329. if (oc->rend_data &&
  330. !rend_cmp_service_ids(onion_address,
  331. oc->rend_data->onion_address)) {
  332. log_info(LD_REND|LD_CIRC, "Closing introduction circuit %d that we "
  333. "built in parallel (Purpose %d).", oc->global_identifier,
  334. c->purpose);
  335. circuit_mark_for_close(c, END_CIRC_REASON_TIMEOUT);
  336. }
  337. }
  338. }
  339. }
  340. /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
  341. */
  342. int
  343. rend_client_introduction_acked(origin_circuit_t *circ,
  344. const uint8_t *request, size_t request_len)
  345. {
  346. origin_circuit_t *rendcirc;
  347. (void) request; // XXXX Use this.
  348. if (circ->base_.purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  349. log_warn(LD_PROTOCOL,
  350. "Received REND_INTRODUCE_ACK on unexpected circuit %u.",
  351. (unsigned)circ->base_.n_circ_id);
  352. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  353. return -1;
  354. }
  355. tor_assert(circ->build_state->chosen_exit);
  356. #ifndef NON_ANONYMOUS_MODE_ENABLED
  357. tor_assert(!(circ->build_state->onehop_tunnel));
  358. #endif
  359. tor_assert(circ->rend_data);
  360. /* For path bias: This circuit was used successfully. Valid
  361. * nacks and acks count. */
  362. pathbias_mark_use_success(circ);
  363. if (request_len == 0) {
  364. /* It's an ACK; the introduction point relayed our introduction request. */
  365. /* Locate the rend circ which is waiting to hear about this ack,
  366. * and tell it.
  367. */
  368. log_info(LD_REND,"Received ack. Telling rend circ...");
  369. rendcirc = circuit_get_ready_rend_circ_by_rend_data(circ->rend_data);
  370. if (rendcirc) { /* remember the ack */
  371. #ifndef NON_ANONYMOUS_MODE_ENABLED
  372. tor_assert(!(rendcirc->build_state->onehop_tunnel));
  373. #endif
  374. circuit_change_purpose(TO_CIRCUIT(rendcirc),
  375. CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED);
  376. /* Set timestamp_dirty, because circuit_expire_building expects
  377. * it to specify when a circuit entered the
  378. * _C_REND_READY_INTRO_ACKED state. */
  379. rendcirc->base_.timestamp_dirty = time(NULL);
  380. } else {
  381. log_info(LD_REND,"...Found no rend circ. Dropping on the floor.");
  382. }
  383. /* close the circuit: we won't need it anymore. */
  384. circuit_change_purpose(TO_CIRCUIT(circ),
  385. CIRCUIT_PURPOSE_C_INTRODUCE_ACKED);
  386. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  387. /* close any other intros launched in parallel */
  388. rend_client_close_other_intros(circ->rend_data->onion_address);
  389. } else {
  390. /* It's a NAK; the introduction point didn't relay our request. */
  391. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_INTRODUCING);
  392. /* Remove this intro point from the set of viable introduction
  393. * points. If any remain, extend to a new one and try again.
  394. * If none remain, refetch the service descriptor.
  395. */
  396. log_info(LD_REND, "Got nack for %s from %s...",
  397. safe_str_client(circ->rend_data->onion_address),
  398. safe_str_client(extend_info_describe(circ->build_state->chosen_exit)));
  399. if (rend_client_report_intro_point_failure(circ->build_state->chosen_exit,
  400. circ->rend_data,
  401. INTRO_POINT_FAILURE_GENERIC)>0) {
  402. /* There are introduction points left. Re-extend the circuit to
  403. * another intro point and try again. */
  404. int result = rend_client_reextend_intro_circuit(circ);
  405. /* XXXX If that call failed, should we close the rend circuit,
  406. * too? */
  407. return result;
  408. }
  409. }
  410. return 0;
  411. }
  412. /** The period for which a hidden service directory cannot be queried for
  413. * the same descriptor ID again. */
  414. #define REND_HID_SERV_DIR_REQUERY_PERIOD (15 * 60)
  415. /** Contains the last request times to hidden service directories for
  416. * certain queries; each key is a string consisting of the
  417. * concatenation of a base32-encoded HS directory identity digest, a
  418. * base32-encoded HS descriptor ID, and a hidden service address
  419. * (without the ".onion" part); each value is a pointer to a time_t
  420. * holding the time of the last request for that descriptor ID to that
  421. * HS directory. */
  422. static strmap_t *last_hid_serv_requests_ = NULL;
  423. /** Returns last_hid_serv_requests_, initializing it to a new strmap if
  424. * necessary. */
  425. static strmap_t *
  426. get_last_hid_serv_requests(void)
  427. {
  428. if (!last_hid_serv_requests_)
  429. last_hid_serv_requests_ = strmap_new();
  430. return last_hid_serv_requests_;
  431. }
  432. #define LAST_HID_SERV_REQUEST_KEY_LEN (REND_DESC_ID_V2_LEN_BASE32 + \
  433. REND_DESC_ID_V2_LEN_BASE32 + \
  434. REND_SERVICE_ID_LEN_BASE32)
  435. /** Look up the last request time to hidden service directory <b>hs_dir</b>
  436. * for descriptor ID <b>desc_id_base32</b> for the service specified in
  437. * <b>rend_query</b>. If <b>set</b> is non-zero,
  438. * assign the current time <b>now</b> and return that. Otherwise, return
  439. * the most recent request time, or 0 if no such request has been sent
  440. * before. */
  441. static time_t
  442. lookup_last_hid_serv_request(routerstatus_t *hs_dir,
  443. const char *desc_id_base32,
  444. const rend_data_t *rend_query,
  445. time_t now, int set)
  446. {
  447. char hsdir_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  448. char hsdir_desc_comb_id[LAST_HID_SERV_REQUEST_KEY_LEN + 1];
  449. time_t *last_request_ptr;
  450. strmap_t *last_hid_serv_requests = get_last_hid_serv_requests();
  451. base32_encode(hsdir_id_base32, sizeof(hsdir_id_base32),
  452. hs_dir->identity_digest, DIGEST_LEN);
  453. tor_snprintf(hsdir_desc_comb_id, sizeof(hsdir_desc_comb_id), "%s%s%s",
  454. hsdir_id_base32,
  455. desc_id_base32,
  456. rend_query->onion_address);
  457. /* XXX023 tor_assert(strlen(hsdir_desc_comb_id) ==
  458. LAST_HID_SERV_REQUEST_KEY_LEN); */
  459. if (set) {
  460. time_t *oldptr;
  461. last_request_ptr = tor_malloc_zero(sizeof(time_t));
  462. *last_request_ptr = now;
  463. oldptr = strmap_set(last_hid_serv_requests, hsdir_desc_comb_id,
  464. last_request_ptr);
  465. tor_free(oldptr);
  466. } else
  467. last_request_ptr = strmap_get_lc(last_hid_serv_requests,
  468. hsdir_desc_comb_id);
  469. return (last_request_ptr) ? *last_request_ptr : 0;
  470. }
  471. /** Clean the history of request times to hidden service directories, so that
  472. * it does not contain requests older than REND_HID_SERV_DIR_REQUERY_PERIOD
  473. * seconds any more. */
  474. static void
  475. directory_clean_last_hid_serv_requests(time_t now)
  476. {
  477. strmap_iter_t *iter;
  478. time_t cutoff = now - REND_HID_SERV_DIR_REQUERY_PERIOD;
  479. strmap_t *last_hid_serv_requests = get_last_hid_serv_requests();
  480. for (iter = strmap_iter_init(last_hid_serv_requests);
  481. !strmap_iter_done(iter); ) {
  482. const char *key;
  483. void *val;
  484. time_t *ent;
  485. strmap_iter_get(iter, &key, &val);
  486. ent = (time_t *) val;
  487. if (*ent < cutoff) {
  488. iter = strmap_iter_next_rmv(last_hid_serv_requests, iter);
  489. tor_free(ent);
  490. } else {
  491. iter = strmap_iter_next(last_hid_serv_requests, iter);
  492. }
  493. }
  494. }
  495. /** Remove all requests related to the hidden service named
  496. * <b>onion_address</b> from the history of times of requests to
  497. * hidden service directories. */
  498. static void
  499. purge_hid_serv_from_last_hid_serv_requests(const char *onion_address)
  500. {
  501. strmap_iter_t *iter;
  502. strmap_t *last_hid_serv_requests = get_last_hid_serv_requests();
  503. /* XXX023 tor_assert(strlen(onion_address) == REND_SERVICE_ID_LEN_BASE32); */
  504. for (iter = strmap_iter_init(last_hid_serv_requests);
  505. !strmap_iter_done(iter); ) {
  506. const char *key;
  507. void *val;
  508. strmap_iter_get(iter, &key, &val);
  509. /* XXX023 tor_assert(strlen(key) == LAST_HID_SERV_REQUEST_KEY_LEN); */
  510. if (tor_memeq(key + LAST_HID_SERV_REQUEST_KEY_LEN -
  511. REND_SERVICE_ID_LEN_BASE32,
  512. onion_address,
  513. REND_SERVICE_ID_LEN_BASE32)) {
  514. iter = strmap_iter_next_rmv(last_hid_serv_requests, iter);
  515. tor_free(val);
  516. } else {
  517. iter = strmap_iter_next(last_hid_serv_requests, iter);
  518. }
  519. }
  520. }
  521. /** Purge the history of request times to hidden service directories,
  522. * so that future lookups of an HS descriptor will not fail because we
  523. * accessed all of the HSDir relays responsible for the descriptor
  524. * recently. */
  525. void
  526. rend_client_purge_last_hid_serv_requests(void)
  527. {
  528. /* Don't create the table if it doesn't exist yet (and it may very
  529. * well not exist if the user hasn't accessed any HSes)... */
  530. strmap_t *old_last_hid_serv_requests = last_hid_serv_requests_;
  531. /* ... and let get_last_hid_serv_requests re-create it for us if
  532. * necessary. */
  533. last_hid_serv_requests_ = NULL;
  534. if (old_last_hid_serv_requests != NULL) {
  535. log_info(LD_REND, "Purging client last-HS-desc-request-time table");
  536. strmap_free(old_last_hid_serv_requests, tor_free_);
  537. }
  538. }
  539. /** Determine the responsible hidden service directories for <b>desc_id</b>
  540. * and fetch the descriptor with that ID from one of them. Only
  541. * send a request to a hidden service directory that we have not yet tried
  542. * during this attempt to connect to this hidden service; on success, return 1,
  543. * in the case that no hidden service directory is left to ask for the
  544. * descriptor, return 0, and in case of a failure -1. */
  545. static int
  546. directory_get_from_hs_dir(const char *desc_id, const rend_data_t *rend_query)
  547. {
  548. smartlist_t *responsible_dirs = smartlist_new();
  549. routerstatus_t *hs_dir;
  550. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  551. time_t now = time(NULL);
  552. char descriptor_cookie_base64[3*REND_DESC_COOKIE_LEN_BASE64];
  553. int tor2web_mode = get_options()->Tor2webMode;
  554. tor_assert(desc_id);
  555. tor_assert(rend_query);
  556. /* Determine responsible dirs. Even if we can't get all we want,
  557. * work with the ones we have. If it's empty, we'll notice below. */
  558. hid_serv_get_responsible_directories(responsible_dirs, desc_id);
  559. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  560. desc_id, DIGEST_LEN);
  561. /* Only select those hidden service directories to which we did not send
  562. * a request recently and for which we have a router descriptor here. */
  563. /* Clean request history first. */
  564. directory_clean_last_hid_serv_requests(now);
  565. SMARTLIST_FOREACH(responsible_dirs, routerstatus_t *, dir, {
  566. time_t last = lookup_last_hid_serv_request(
  567. dir, desc_id_base32, rend_query, 0, 0);
  568. const node_t *node = node_get_by_id(dir->identity_digest);
  569. if (last + REND_HID_SERV_DIR_REQUERY_PERIOD >= now ||
  570. !node || !node_has_descriptor(node))
  571. SMARTLIST_DEL_CURRENT(responsible_dirs, dir);
  572. });
  573. hs_dir = smartlist_choose(responsible_dirs);
  574. smartlist_free(responsible_dirs);
  575. if (!hs_dir) {
  576. log_info(LD_REND, "Could not pick one of the responsible hidden "
  577. "service directories, because we requested them all "
  578. "recently without success.");
  579. return 0;
  580. }
  581. /* Remember that we are requesting a descriptor from this hidden service
  582. * directory now. */
  583. lookup_last_hid_serv_request(hs_dir, desc_id_base32, rend_query, now, 1);
  584. /* Encode descriptor cookie for logging purposes. */
  585. if (rend_query->auth_type != REND_NO_AUTH) {
  586. if (base64_encode(descriptor_cookie_base64,
  587. sizeof(descriptor_cookie_base64),
  588. rend_query->descriptor_cookie, REND_DESC_COOKIE_LEN)<0) {
  589. log_warn(LD_BUG, "Could not base64-encode descriptor cookie.");
  590. return 0;
  591. }
  592. /* Remove == signs and newline. */
  593. descriptor_cookie_base64[strlen(descriptor_cookie_base64)-3] = '\0';
  594. } else {
  595. strlcpy(descriptor_cookie_base64, "(none)",
  596. sizeof(descriptor_cookie_base64));
  597. }
  598. /* Send fetch request. (Pass query and possibly descriptor cookie so that
  599. * they can be written to the directory connection and be referred to when
  600. * the response arrives. */
  601. directory_initiate_command_routerstatus_rend(hs_dir,
  602. DIR_PURPOSE_FETCH_RENDDESC_V2,
  603. ROUTER_PURPOSE_GENERAL,
  604. tor2web_mode?DIRIND_ONEHOP:DIRIND_ANONYMOUS,
  605. desc_id_base32,
  606. NULL, 0, 0,
  607. rend_query);
  608. log_info(LD_REND, "Sending fetch request for v2 descriptor for "
  609. "service '%s' with descriptor ID '%s', auth type %d, "
  610. "and descriptor cookie '%s' to hidden service "
  611. "directory %s",
  612. rend_query->onion_address, desc_id_base32,
  613. rend_query->auth_type,
  614. (rend_query->auth_type == REND_NO_AUTH ? "[none]" :
  615. escaped_safe_str_client(descriptor_cookie_base64)),
  616. routerstatus_describe(hs_dir));
  617. return 1;
  618. }
  619. /** Unless we already have a descriptor for <b>rend_query</b> with at least
  620. * one (possibly) working introduction point in it, start a connection to a
  621. * hidden service directory to fetch a v2 rendezvous service descriptor. */
  622. void
  623. rend_client_refetch_v2_renddesc(const rend_data_t *rend_query)
  624. {
  625. char descriptor_id[DIGEST_LEN];
  626. int replicas_left_to_try[REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS];
  627. int i, tries_left;
  628. rend_cache_entry_t *e = NULL;
  629. tor_assert(rend_query);
  630. /* Are we configured to fetch descriptors? */
  631. if (!get_options()->FetchHidServDescriptors) {
  632. log_warn(LD_REND, "We received an onion address for a v2 rendezvous "
  633. "service descriptor, but are not fetching service descriptors.");
  634. return;
  635. }
  636. /* Before fetching, check if we already have a usable descriptor here. */
  637. if (rend_cache_lookup_entry(rend_query->onion_address, -1, &e) > 0 &&
  638. rend_client_any_intro_points_usable(e)) {
  639. log_info(LD_REND, "We would fetch a v2 rendezvous descriptor, but we "
  640. "already have a usable descriptor here. Not fetching.");
  641. return;
  642. }
  643. log_debug(LD_REND, "Fetching v2 rendezvous descriptor for service %s",
  644. safe_str_client(rend_query->onion_address));
  645. /* Randomly iterate over the replicas until a descriptor can be fetched
  646. * from one of the consecutive nodes, or no options are left. */
  647. tries_left = REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS;
  648. for (i = 0; i < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; i++)
  649. replicas_left_to_try[i] = i;
  650. while (tries_left > 0) {
  651. int rand = crypto_rand_int(tries_left);
  652. int chosen_replica = replicas_left_to_try[rand];
  653. replicas_left_to_try[rand] = replicas_left_to_try[--tries_left];
  654. if (rend_compute_v2_desc_id(descriptor_id, rend_query->onion_address,
  655. rend_query->auth_type == REND_STEALTH_AUTH ?
  656. rend_query->descriptor_cookie : NULL,
  657. time(NULL), chosen_replica) < 0) {
  658. log_warn(LD_REND, "Internal error: Computing v2 rendezvous "
  659. "descriptor ID did not succeed.");
  660. /*
  661. * Hmm, can this write anything to descriptor_id and still fail?
  662. * Let's clear it just to be safe.
  663. *
  664. * From here on, any returns should goto done which clears
  665. * descriptor_id so we don't leave key-derived material on the stack.
  666. */
  667. goto done;
  668. }
  669. if (directory_get_from_hs_dir(descriptor_id, rend_query) != 0)
  670. goto done; /* either success or failure, but we're done */
  671. }
  672. /* If we come here, there are no hidden service directories left. */
  673. log_info(LD_REND, "Could not pick one of the responsible hidden "
  674. "service directories to fetch descriptors, because "
  675. "we already tried them all unsuccessfully.");
  676. /* Close pending connections. */
  677. rend_client_desc_trynow(rend_query->onion_address);
  678. done:
  679. memwipe(descriptor_id, 0, sizeof(descriptor_id));
  680. return;
  681. }
  682. /** Cancel all rendezvous descriptor fetches currently in progress.
  683. */
  684. void
  685. rend_client_cancel_descriptor_fetches(void)
  686. {
  687. smartlist_t *connection_array = get_connection_array();
  688. SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
  689. if (conn->type == CONN_TYPE_DIR &&
  690. (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC ||
  691. conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2)) {
  692. /* It's a rendezvous descriptor fetch in progress -- cancel it
  693. * by marking the connection for close.
  694. *
  695. * Even if this connection has already reached EOF, this is
  696. * enough to make sure that if the descriptor hasn't been
  697. * processed yet, it won't be. See the end of
  698. * connection_handle_read; connection_reached_eof (indirectly)
  699. * processes whatever response the connection received. */
  700. const rend_data_t *rd = (TO_DIR_CONN(conn))->rend_data;
  701. if (!rd) {
  702. log_warn(LD_BUG | LD_REND,
  703. "Marking for close dir conn fetching rendezvous "
  704. "descriptor for unknown service!");
  705. } else {
  706. log_debug(LD_REND, "Marking for close dir conn fetching "
  707. "rendezvous descriptor for service %s",
  708. safe_str(rd->onion_address));
  709. }
  710. connection_mark_for_close(conn);
  711. }
  712. } SMARTLIST_FOREACH_END(conn);
  713. }
  714. /** Mark <b>failed_intro</b> as a failed introduction point for the
  715. * hidden service specified by <b>rend_query</b>. If the HS now has no
  716. * usable intro points, or we do not have an HS descriptor for it,
  717. * then launch a new renddesc fetch.
  718. *
  719. * If <b>failure_type</b> is INTRO_POINT_FAILURE_GENERIC, remove the
  720. * intro point from (our parsed copy of) the HS descriptor.
  721. *
  722. * If <b>failure_type</b> is INTRO_POINT_FAILURE_TIMEOUT, mark the
  723. * intro point as 'timed out'; it will not be retried until the
  724. * current hidden service connection attempt has ended or it has
  725. * appeared in a newly fetched rendezvous descriptor.
  726. *
  727. * If <b>failure_type</b> is INTRO_POINT_FAILURE_UNREACHABLE,
  728. * increment the intro point's reachability-failure count; if it has
  729. * now failed MAX_INTRO_POINT_REACHABILITY_FAILURES or more times,
  730. * remove the intro point from (our parsed copy of) the HS descriptor.
  731. *
  732. * Return -1 if error, 0 if no usable intro points remain or service
  733. * unrecognized, 1 if recognized and some intro points remain.
  734. */
  735. int
  736. rend_client_report_intro_point_failure(extend_info_t *failed_intro,
  737. const rend_data_t *rend_query,
  738. unsigned int failure_type)
  739. {
  740. int i, r;
  741. rend_cache_entry_t *ent;
  742. connection_t *conn;
  743. r = rend_cache_lookup_entry(rend_query->onion_address, -1, &ent);
  744. if (r<0) {
  745. log_warn(LD_BUG, "Malformed service ID %s.",
  746. escaped_safe_str_client(rend_query->onion_address));
  747. return -1;
  748. }
  749. if (r==0) {
  750. log_info(LD_REND, "Unknown service %s. Re-fetching descriptor.",
  751. escaped_safe_str_client(rend_query->onion_address));
  752. rend_client_refetch_v2_renddesc(rend_query);
  753. return 0;
  754. }
  755. for (i = 0; i < smartlist_len(ent->parsed->intro_nodes); i++) {
  756. rend_intro_point_t *intro = smartlist_get(ent->parsed->intro_nodes, i);
  757. if (tor_memeq(failed_intro->identity_digest,
  758. intro->extend_info->identity_digest, DIGEST_LEN)) {
  759. switch (failure_type) {
  760. default:
  761. log_warn(LD_BUG, "Unknown failure type %u. Removing intro point.",
  762. failure_type);
  763. tor_fragile_assert();
  764. /* fall through */
  765. case INTRO_POINT_FAILURE_GENERIC:
  766. rend_intro_point_free(intro);
  767. smartlist_del(ent->parsed->intro_nodes, i);
  768. break;
  769. case INTRO_POINT_FAILURE_TIMEOUT:
  770. intro->timed_out = 1;
  771. break;
  772. case INTRO_POINT_FAILURE_UNREACHABLE:
  773. ++(intro->unreachable_count);
  774. {
  775. int zap_intro_point =
  776. intro->unreachable_count >= MAX_INTRO_POINT_REACHABILITY_FAILURES;
  777. log_info(LD_REND, "Failed to reach this intro point %u times.%s",
  778. intro->unreachable_count,
  779. zap_intro_point ? " Removing from descriptor.": "");
  780. if (zap_intro_point) {
  781. rend_intro_point_free(intro);
  782. smartlist_del(ent->parsed->intro_nodes, i);
  783. }
  784. }
  785. break;
  786. }
  787. break;
  788. }
  789. }
  790. if (! rend_client_any_intro_points_usable(ent)) {
  791. log_info(LD_REND,
  792. "No more intro points remain for %s. Re-fetching descriptor.",
  793. escaped_safe_str_client(rend_query->onion_address));
  794. rend_client_refetch_v2_renddesc(rend_query);
  795. /* move all pending streams back to renddesc_wait */
  796. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  797. AP_CONN_STATE_CIRCUIT_WAIT,
  798. rend_query->onion_address))) {
  799. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  800. }
  801. return 0;
  802. }
  803. log_info(LD_REND,"%d options left for %s.",
  804. smartlist_len(ent->parsed->intro_nodes),
  805. escaped_safe_str_client(rend_query->onion_address));
  806. return 1;
  807. }
  808. /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  809. * the circuit to C_REND_READY.
  810. */
  811. int
  812. rend_client_rendezvous_acked(origin_circuit_t *circ, const uint8_t *request,
  813. size_t request_len)
  814. {
  815. (void) request;
  816. (void) request_len;
  817. /* we just got an ack for our establish-rendezvous. switch purposes. */
  818. if (circ->base_.purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  819. log_warn(LD_PROTOCOL,"Got a rendezvous ack when we weren't expecting one. "
  820. "Closing circ.");
  821. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  822. return -1;
  823. }
  824. log_info(LD_REND,"Got rendezvous ack. This circuit is now ready for "
  825. "rendezvous.");
  826. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_REND_READY);
  827. /* Set timestamp_dirty, because circuit_expire_building expects it
  828. * to specify when a circuit entered the _C_REND_READY state. */
  829. circ->base_.timestamp_dirty = time(NULL);
  830. /* From a path bias point of view, this circuit is now successfully used.
  831. * Waiting any longer opens us up to attacks from Bob. He could induce
  832. * Alice to attempt to connect to his hidden service and never reply
  833. * to her rend requests */
  834. pathbias_mark_use_success(circ);
  835. /* XXXX This is a pretty brute-force approach. It'd be better to
  836. * attach only the connections that are waiting on this circuit, rather
  837. * than trying to attach them all. See comments bug 743. */
  838. /* If we already have the introduction circuit built, make sure we send
  839. * the INTRODUCE cell _now_ */
  840. connection_ap_attach_pending();
  841. return 0;
  842. }
  843. /** Bob sent us a rendezvous cell; join the circuits. */
  844. int
  845. rend_client_receive_rendezvous(origin_circuit_t *circ, const uint8_t *request,
  846. size_t request_len)
  847. {
  848. crypt_path_t *hop;
  849. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  850. if ((circ->base_.purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  851. circ->base_.purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
  852. || !circ->build_state->pending_final_cpath) {
  853. log_warn(LD_PROTOCOL,"Got rendezvous2 cell from hidden service, but not "
  854. "expecting it. Closing.");
  855. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  856. return -1;
  857. }
  858. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  859. log_warn(LD_PROTOCOL,"Incorrect length (%d) on RENDEZVOUS2 cell.",
  860. (int)request_len);
  861. goto err;
  862. }
  863. log_info(LD_REND,"Got RENDEZVOUS2 cell from hidden service.");
  864. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  865. tor_assert(circ->build_state);
  866. tor_assert(circ->build_state->pending_final_cpath);
  867. hop = circ->build_state->pending_final_cpath;
  868. tor_assert(hop->rend_dh_handshake_state);
  869. if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN,
  870. hop->rend_dh_handshake_state, (char*)request,
  871. DH_KEY_LEN,
  872. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  873. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  874. goto err;
  875. }
  876. /* ... and set up cpath. */
  877. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  878. goto err;
  879. /* Check whether the digest is right... */
  880. if (tor_memneq(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  881. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  882. goto err;
  883. }
  884. crypto_dh_free(hop->rend_dh_handshake_state);
  885. hop->rend_dh_handshake_state = NULL;
  886. /* All is well. Extend the circuit. */
  887. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_REND_JOINED);
  888. hop->state = CPATH_STATE_OPEN;
  889. /* set the windows to default. these are the windows
  890. * that alice thinks bob has.
  891. */
  892. hop->package_window = circuit_initial_package_window();
  893. hop->deliver_window = CIRCWINDOW_START;
  894. /* Now that this circuit has finished connecting to its destination,
  895. * make sure circuit_get_open_circ_or_launch is willing to return it
  896. * so we can actually use it. */
  897. circ->hs_circ_has_timed_out = 0;
  898. onion_append_to_cpath(&circ->cpath, hop);
  899. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  900. circuit_try_attaching_streams(circ);
  901. memwipe(keys, 0, sizeof(keys));
  902. return 0;
  903. err:
  904. memwipe(keys, 0, sizeof(keys));
  905. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  906. return -1;
  907. }
  908. /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that are
  909. * waiting on <b>query</b>. If there's a working cache entry here with at
  910. * least one intro point, move them to the next state. */
  911. void
  912. rend_client_desc_trynow(const char *query)
  913. {
  914. entry_connection_t *conn;
  915. rend_cache_entry_t *entry;
  916. const rend_data_t *rend_data;
  917. time_t now = time(NULL);
  918. smartlist_t *conns = get_connection_array();
  919. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
  920. if (base_conn->type != CONN_TYPE_AP ||
  921. base_conn->state != AP_CONN_STATE_RENDDESC_WAIT ||
  922. base_conn->marked_for_close)
  923. continue;
  924. conn = TO_ENTRY_CONN(base_conn);
  925. rend_data = ENTRY_TO_EDGE_CONN(conn)->rend_data;
  926. if (!rend_data)
  927. continue;
  928. if (rend_cmp_service_ids(query, rend_data->onion_address))
  929. continue;
  930. assert_connection_ok(base_conn, now);
  931. if (rend_cache_lookup_entry(rend_data->onion_address, -1,
  932. &entry) == 1 &&
  933. rend_client_any_intro_points_usable(entry)) {
  934. /* either this fetch worked, or it failed but there was a
  935. * valid entry from before which we should reuse */
  936. log_info(LD_REND,"Rend desc is usable. Launching circuits.");
  937. base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  938. /* restart their timeout values, so they get a fair shake at
  939. * connecting to the hidden service. */
  940. base_conn->timestamp_created = now;
  941. base_conn->timestamp_lastread = now;
  942. base_conn->timestamp_lastwritten = now;
  943. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  944. /* it will never work */
  945. log_warn(LD_REND,"Rendezvous attempt failed. Closing.");
  946. if (!base_conn->marked_for_close)
  947. connection_mark_unattached_ap(conn, END_STREAM_REASON_CANT_ATTACH);
  948. }
  949. } else { /* 404, or fetch didn't get that far */
  950. log_notice(LD_REND,"Closing stream for '%s.onion': hidden service is "
  951. "unavailable (try again later).",
  952. safe_str_client(query));
  953. connection_mark_unattached_ap(conn, END_STREAM_REASON_RESOLVEFAILED);
  954. rend_client_note_connection_attempt_ended(query);
  955. }
  956. } SMARTLIST_FOREACH_END(base_conn);
  957. }
  958. /** Clear temporary state used only during an attempt to connect to
  959. * the hidden service named <b>onion_address</b>. Called when a
  960. * connection attempt has ended; may be called occasionally at other
  961. * times, and should be reasonably harmless. */
  962. void
  963. rend_client_note_connection_attempt_ended(const char *onion_address)
  964. {
  965. rend_cache_entry_t *cache_entry = NULL;
  966. rend_cache_lookup_entry(onion_address, -1, &cache_entry);
  967. log_info(LD_REND, "Connection attempt for %s has ended; "
  968. "cleaning up temporary state.",
  969. safe_str_client(onion_address));
  970. /* Clear the timed_out flag on all remaining intro points for this HS. */
  971. if (cache_entry != NULL) {
  972. SMARTLIST_FOREACH(cache_entry->parsed->intro_nodes,
  973. rend_intro_point_t *, ip,
  974. ip->timed_out = 0; );
  975. }
  976. /* Remove the HS's entries in last_hid_serv_requests. */
  977. purge_hid_serv_from_last_hid_serv_requests(onion_address);
  978. }
  979. /** Return a newly allocated extend_info_t* for a randomly chosen introduction
  980. * point for the named hidden service. Return NULL if all introduction points
  981. * have been tried and failed.
  982. */
  983. extend_info_t *
  984. rend_client_get_random_intro(const rend_data_t *rend_query)
  985. {
  986. extend_info_t *result;
  987. rend_cache_entry_t *entry;
  988. if (rend_cache_lookup_entry(rend_query->onion_address, -1, &entry) < 1) {
  989. log_warn(LD_REND,
  990. "Query '%s' didn't have valid rend desc in cache. Failing.",
  991. safe_str_client(rend_query->onion_address));
  992. return NULL;
  993. }
  994. /* See if we can get a node that complies with ExcludeNodes */
  995. if ((result = rend_client_get_random_intro_impl(entry, 1, 1)))
  996. return result;
  997. /* If not, and StrictNodes is not set, see if we can return any old node
  998. */
  999. if (!get_options()->StrictNodes)
  1000. return rend_client_get_random_intro_impl(entry, 0, 1);
  1001. return NULL;
  1002. }
  1003. /** As rend_client_get_random_intro, except assume that StrictNodes is set
  1004. * iff <b>strict</b> is true. If <b>warnings</b> is false, don't complain
  1005. * to the user when we're out of nodes, even if StrictNodes is true.
  1006. */
  1007. static extend_info_t *
  1008. rend_client_get_random_intro_impl(const rend_cache_entry_t *entry,
  1009. const int strict,
  1010. const int warnings)
  1011. {
  1012. int i;
  1013. rend_intro_point_t *intro;
  1014. const or_options_t *options = get_options();
  1015. smartlist_t *usable_nodes;
  1016. int n_excluded = 0;
  1017. /* We'll keep a separate list of the usable nodes. If this becomes empty,
  1018. * no nodes are usable. */
  1019. usable_nodes = smartlist_new();
  1020. smartlist_add_all(usable_nodes, entry->parsed->intro_nodes);
  1021. /* Remove the intro points that have timed out during this HS
  1022. * connection attempt from our list of usable nodes. */
  1023. SMARTLIST_FOREACH(usable_nodes, rend_intro_point_t *, ip,
  1024. if (ip->timed_out) {
  1025. SMARTLIST_DEL_CURRENT(usable_nodes, ip);
  1026. });
  1027. again:
  1028. if (smartlist_len(usable_nodes) == 0) {
  1029. if (n_excluded && get_options()->StrictNodes && warnings) {
  1030. /* We only want to warn if StrictNodes is really set. Otherwise
  1031. * we're just about to retry anyways.
  1032. */
  1033. log_warn(LD_REND, "All introduction points for hidden service are "
  1034. "at excluded relays, and StrictNodes is set. Skipping.");
  1035. }
  1036. smartlist_free(usable_nodes);
  1037. return NULL;
  1038. }
  1039. i = crypto_rand_int(smartlist_len(usable_nodes));
  1040. intro = smartlist_get(usable_nodes, i);
  1041. /* Do we need to look up the router or is the extend info complete? */
  1042. if (!intro->extend_info->onion_key) {
  1043. const node_t *node;
  1044. extend_info_t *new_extend_info;
  1045. if (tor_digest_is_zero(intro->extend_info->identity_digest))
  1046. node = node_get_by_hex_id(intro->extend_info->nickname);
  1047. else
  1048. node = node_get_by_id(intro->extend_info->identity_digest);
  1049. if (!node) {
  1050. log_info(LD_REND, "Unknown router with nickname '%s'; trying another.",
  1051. intro->extend_info->nickname);
  1052. smartlist_del(usable_nodes, i);
  1053. goto again;
  1054. }
  1055. new_extend_info = extend_info_from_node(node, 0);
  1056. if (!new_extend_info) {
  1057. log_info(LD_REND, "We don't have a descriptor for the intro-point relay "
  1058. "'%s'; trying another.",
  1059. extend_info_describe(intro->extend_info));
  1060. smartlist_del(usable_nodes, i);
  1061. goto again;
  1062. } else {
  1063. extend_info_free(intro->extend_info);
  1064. intro->extend_info = new_extend_info;
  1065. }
  1066. tor_assert(intro->extend_info != NULL);
  1067. }
  1068. /* Check if we should refuse to talk to this router. */
  1069. if (strict &&
  1070. routerset_contains_extendinfo(options->ExcludeNodes,
  1071. intro->extend_info)) {
  1072. n_excluded++;
  1073. smartlist_del(usable_nodes, i);
  1074. goto again;
  1075. }
  1076. smartlist_free(usable_nodes);
  1077. return extend_info_dup(intro->extend_info);
  1078. }
  1079. /** Return true iff any introduction points still listed in <b>entry</b> are
  1080. * usable. */
  1081. int
  1082. rend_client_any_intro_points_usable(const rend_cache_entry_t *entry)
  1083. {
  1084. extend_info_t *extend_info =
  1085. rend_client_get_random_intro_impl(entry, get_options()->StrictNodes, 0);
  1086. int rv = (extend_info != NULL);
  1087. extend_info_free(extend_info);
  1088. return rv;
  1089. }
  1090. /** Client-side authorizations for hidden services; map of onion address to
  1091. * rend_service_authorization_t*. */
  1092. static strmap_t *auth_hid_servs = NULL;
  1093. /** Look up the client-side authorization for the hidden service with
  1094. * <b>onion_address</b>. Return NULL if no authorization is available for
  1095. * that address. */
  1096. rend_service_authorization_t*
  1097. rend_client_lookup_service_authorization(const char *onion_address)
  1098. {
  1099. tor_assert(onion_address);
  1100. if (!auth_hid_servs) return NULL;
  1101. return strmap_get(auth_hid_servs, onion_address);
  1102. }
  1103. /** Helper: Free storage held by rend_service_authorization_t. */
  1104. static void
  1105. rend_service_authorization_free(rend_service_authorization_t *auth)
  1106. {
  1107. tor_free(auth);
  1108. }
  1109. /** Helper for strmap_free. */
  1110. static void
  1111. rend_service_authorization_strmap_item_free(void *service_auth)
  1112. {
  1113. rend_service_authorization_free(service_auth);
  1114. }
  1115. /** Release all the storage held in auth_hid_servs.
  1116. */
  1117. void
  1118. rend_service_authorization_free_all(void)
  1119. {
  1120. if (!auth_hid_servs) {
  1121. return;
  1122. }
  1123. strmap_free(auth_hid_servs, rend_service_authorization_strmap_item_free);
  1124. auth_hid_servs = NULL;
  1125. }
  1126. /** Parse <b>config_line</b> as a client-side authorization for a hidden
  1127. * service and add it to the local map of hidden service authorizations.
  1128. * Return 0 for success and -1 for failure. */
  1129. int
  1130. rend_parse_service_authorization(const or_options_t *options,
  1131. int validate_only)
  1132. {
  1133. config_line_t *line;
  1134. int res = -1;
  1135. strmap_t *parsed = strmap_new();
  1136. smartlist_t *sl = smartlist_new();
  1137. rend_service_authorization_t *auth = NULL;
  1138. char descriptor_cookie_tmp[REND_DESC_COOKIE_LEN+2];
  1139. char descriptor_cookie_base64ext[REND_DESC_COOKIE_LEN_BASE64+2+1];
  1140. for (line = options->HidServAuth; line; line = line->next) {
  1141. char *onion_address, *descriptor_cookie;
  1142. int auth_type_val = 0;
  1143. auth = NULL;
  1144. SMARTLIST_FOREACH(sl, char *, c, tor_free(c););
  1145. smartlist_clear(sl);
  1146. smartlist_split_string(sl, line->value, " ",
  1147. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  1148. if (smartlist_len(sl) < 2) {
  1149. log_warn(LD_CONFIG, "Configuration line does not consist of "
  1150. "\"onion-address authorization-cookie [service-name]\": "
  1151. "'%s'", line->value);
  1152. goto err;
  1153. }
  1154. auth = tor_malloc_zero(sizeof(rend_service_authorization_t));
  1155. /* Parse onion address. */
  1156. onion_address = smartlist_get(sl, 0);
  1157. if (strlen(onion_address) != REND_SERVICE_ADDRESS_LEN ||
  1158. strcmpend(onion_address, ".onion")) {
  1159. log_warn(LD_CONFIG, "Onion address has wrong format: '%s'",
  1160. onion_address);
  1161. goto err;
  1162. }
  1163. strlcpy(auth->onion_address, onion_address, REND_SERVICE_ID_LEN_BASE32+1);
  1164. if (!rend_valid_service_id(auth->onion_address)) {
  1165. log_warn(LD_CONFIG, "Onion address has wrong format: '%s'",
  1166. onion_address);
  1167. goto err;
  1168. }
  1169. /* Parse descriptor cookie. */
  1170. descriptor_cookie = smartlist_get(sl, 1);
  1171. if (strlen(descriptor_cookie) != REND_DESC_COOKIE_LEN_BASE64) {
  1172. log_warn(LD_CONFIG, "Authorization cookie has wrong length: '%s'",
  1173. descriptor_cookie);
  1174. goto err;
  1175. }
  1176. /* Add trailing zero bytes (AA) to make base64-decoding happy. */
  1177. tor_snprintf(descriptor_cookie_base64ext,
  1178. REND_DESC_COOKIE_LEN_BASE64+2+1,
  1179. "%sAA", descriptor_cookie);
  1180. if (base64_decode(descriptor_cookie_tmp, sizeof(descriptor_cookie_tmp),
  1181. descriptor_cookie_base64ext,
  1182. strlen(descriptor_cookie_base64ext)) < 0) {
  1183. log_warn(LD_CONFIG, "Decoding authorization cookie failed: '%s'",
  1184. descriptor_cookie);
  1185. goto err;
  1186. }
  1187. auth_type_val = (((uint8_t)descriptor_cookie_tmp[16]) >> 4) + 1;
  1188. if (auth_type_val < 1 || auth_type_val > 2) {
  1189. log_warn(LD_CONFIG, "Authorization cookie has unknown authorization "
  1190. "type encoded.");
  1191. goto err;
  1192. }
  1193. auth->auth_type = auth_type_val == 1 ? REND_BASIC_AUTH : REND_STEALTH_AUTH;
  1194. memcpy(auth->descriptor_cookie, descriptor_cookie_tmp,
  1195. REND_DESC_COOKIE_LEN);
  1196. if (strmap_get(parsed, auth->onion_address)) {
  1197. log_warn(LD_CONFIG, "Duplicate authorization for the same hidden "
  1198. "service.");
  1199. goto err;
  1200. }
  1201. strmap_set(parsed, auth->onion_address, auth);
  1202. auth = NULL;
  1203. }
  1204. res = 0;
  1205. goto done;
  1206. err:
  1207. res = -1;
  1208. done:
  1209. rend_service_authorization_free(auth);
  1210. SMARTLIST_FOREACH(sl, char *, c, tor_free(c););
  1211. smartlist_free(sl);
  1212. if (!validate_only && res == 0) {
  1213. rend_service_authorization_free_all();
  1214. auth_hid_servs = parsed;
  1215. } else {
  1216. strmap_free(parsed, rend_service_authorization_strmap_item_free);
  1217. }
  1218. memwipe(descriptor_cookie_tmp, 0, sizeof(descriptor_cookie_tmp));
  1219. memwipe(descriptor_cookie_base64ext, 0, sizeof(descriptor_cookie_base64ext));
  1220. return res;
  1221. }