rendclient.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2011, 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 "relay.h"
  18. #include "rendclient.h"
  19. #include "rendcommon.h"
  20. #include "rephist.h"
  21. #include "routerlist.h"
  22. static extend_info_t *rend_client_get_random_intro_impl(
  23. const rend_cache_entry_t *rend_query,
  24. const int strict, const int warnings);
  25. /** Called when we've established a circuit to an introduction point:
  26. * send the introduction request. */
  27. void
  28. rend_client_introcirc_has_opened(origin_circuit_t *circ)
  29. {
  30. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  31. tor_assert(circ->cpath);
  32. log_info(LD_REND,"introcirc is open");
  33. connection_ap_attach_pending();
  34. }
  35. /** Send the establish-rendezvous cell along a rendezvous circuit. if
  36. * it fails, mark the circ for close and return -1. else return 0.
  37. */
  38. static int
  39. rend_client_send_establish_rendezvous(origin_circuit_t *circ)
  40. {
  41. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  42. tor_assert(circ->rend_data);
  43. log_info(LD_REND, "Sending an ESTABLISH_RENDEZVOUS cell");
  44. if (crypto_rand(circ->rend_data->rend_cookie, REND_COOKIE_LEN) < 0) {
  45. log_warn(LD_BUG, "Internal error: Couldn't produce random cookie.");
  46. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  47. return -1;
  48. }
  49. if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  50. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  51. circ->rend_data->rend_cookie,
  52. REND_COOKIE_LEN,
  53. circ->cpath->prev)<0) {
  54. /* circ is already marked for close */
  55. log_warn(LD_GENERAL, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  56. return -1;
  57. }
  58. return 0;
  59. }
  60. /** Extend the introduction circuit <b>circ</b> to another valid
  61. * introduction point for the hidden service it is trying to connect
  62. * to, or mark it and launch a new circuit if we can't extend it.
  63. * Return 0 on success. Return -1 and mark the introduction
  64. * circuit on failure.
  65. *
  66. * On failure, the caller is responsible for marking the associated
  67. * rendezvous circuit for close. */
  68. static int
  69. rend_client_reextend_intro_circuit(origin_circuit_t *circ)
  70. {
  71. extend_info_t *extend_info;
  72. int result;
  73. extend_info = rend_client_get_random_intro(circ->rend_data);
  74. if (!extend_info) {
  75. log_warn(LD_REND,
  76. "No usable introduction points left for %s. Closing.",
  77. safe_str_client(circ->rend_data->onion_address));
  78. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  79. return -1;
  80. }
  81. if (circ->remaining_relay_early_cells) {
  82. log_info(LD_REND,
  83. "Re-extending circ %d, this time to %s.",
  84. circ->_base.n_circ_id, extend_info->nickname);
  85. result = circuit_extend_to_new_exit(circ, extend_info);
  86. } else {
  87. log_info(LD_REND,
  88. "Building a new introduction circuit, this time to %s.",
  89. extend_info->nickname);
  90. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  91. if (!circuit_launch_by_extend_info(CIRCUIT_PURPOSE_C_INTRODUCING,
  92. extend_info,
  93. CIRCLAUNCH_IS_INTERNAL)) {
  94. log_warn(LD_REND, "Building introduction circuit failed.");
  95. result = -1;
  96. } else {
  97. result = 0;
  98. }
  99. }
  100. extend_info_free(extend_info);
  101. return result;
  102. }
  103. /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
  104. * down introcirc if possible.
  105. */
  106. int
  107. rend_client_send_introduction(origin_circuit_t *introcirc,
  108. origin_circuit_t *rendcirc)
  109. {
  110. size_t payload_len;
  111. int r, v3_shift = 0;
  112. char payload[RELAY_PAYLOAD_SIZE];
  113. char tmp[RELAY_PAYLOAD_SIZE];
  114. rend_cache_entry_t *entry;
  115. crypt_path_t *cpath;
  116. off_t dh_offset;
  117. crypto_pk_env_t *intro_key = NULL;
  118. tor_assert(introcirc->_base.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  119. tor_assert(rendcirc->_base.purpose == CIRCUIT_PURPOSE_C_REND_READY);
  120. tor_assert(introcirc->rend_data);
  121. tor_assert(rendcirc->rend_data);
  122. tor_assert(!rend_cmp_service_ids(introcirc->rend_data->onion_address,
  123. rendcirc->rend_data->onion_address));
  124. if (rend_cache_lookup_entry(introcirc->rend_data->onion_address, -1,
  125. &entry) < 1) {
  126. log_info(LD_REND,
  127. "query %s didn't have valid rend desc in cache. "
  128. "Refetching descriptor.",
  129. safe_str_client(introcirc->rend_data->onion_address));
  130. rend_client_refetch_v2_renddesc(introcirc->rend_data);
  131. {
  132. connection_t *conn;
  133. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  134. AP_CONN_STATE_CIRCUIT_WAIT,
  135. introcirc->rend_data->onion_address))) {
  136. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  137. }
  138. }
  139. return -1;
  140. }
  141. /* first 20 bytes of payload are the hash of Bob's pk */
  142. intro_key = NULL;
  143. SMARTLIST_FOREACH(entry->parsed->intro_nodes, rend_intro_point_t *,
  144. intro, {
  145. if (tor_memeq(introcirc->build_state->chosen_exit->identity_digest,
  146. intro->extend_info->identity_digest, DIGEST_LEN)) {
  147. intro_key = intro->intro_key;
  148. break;
  149. }
  150. });
  151. if (!intro_key) {
  152. log_info(LD_REND, "Could not find intro key for %s at %s; we "
  153. "have a v2 rend desc with %d intro points. "
  154. "Trying a different intro point...",
  155. safe_str_client(introcirc->rend_data->onion_address),
  156. introcirc->build_state->chosen_exit->nickname,
  157. smartlist_len(entry->parsed->intro_nodes));
  158. if (rend_client_reextend_intro_circuit(introcirc)) {
  159. goto perm_err;
  160. } else {
  161. return -1;
  162. }
  163. }
  164. if (crypto_pk_get_digest(intro_key, payload)<0) {
  165. log_warn(LD_BUG, "Internal error: couldn't hash public key.");
  166. goto perm_err;
  167. }
  168. /* Initialize the pending_final_cpath and start the DH handshake. */
  169. cpath = rendcirc->build_state->pending_final_cpath;
  170. if (!cpath) {
  171. cpath = rendcirc->build_state->pending_final_cpath =
  172. tor_malloc_zero(sizeof(crypt_path_t));
  173. cpath->magic = CRYPT_PATH_MAGIC;
  174. if (!(cpath->dh_handshake_state = crypto_dh_new(DH_TYPE_REND))) {
  175. log_warn(LD_BUG, "Internal error: couldn't allocate DH.");
  176. goto perm_err;
  177. }
  178. if (crypto_dh_generate_public(cpath->dh_handshake_state)<0) {
  179. log_warn(LD_BUG, "Internal error: couldn't generate g^x.");
  180. goto perm_err;
  181. }
  182. }
  183. /* If version is 3, write (optional) auth data and timestamp. */
  184. if (entry->parsed->protocols & (1<<3)) {
  185. tmp[0] = 3; /* version 3 of the cell format */
  186. tmp[1] = (uint8_t)introcirc->rend_data->auth_type; /* auth type, if any */
  187. v3_shift = 1;
  188. if (introcirc->rend_data->auth_type != REND_NO_AUTH) {
  189. set_uint16(tmp+2, htons(REND_DESC_COOKIE_LEN));
  190. memcpy(tmp+4, introcirc->rend_data->descriptor_cookie,
  191. REND_DESC_COOKIE_LEN);
  192. v3_shift += 2+REND_DESC_COOKIE_LEN;
  193. }
  194. set_uint32(tmp+v3_shift+1, htonl((uint32_t)time(NULL)));
  195. v3_shift += 4;
  196. } /* if version 2 only write version number */
  197. else if (entry->parsed->protocols & (1<<2)) {
  198. tmp[0] = 2; /* version 2 of the cell format */
  199. }
  200. /* write the remaining items into tmp */
  201. if (entry->parsed->protocols & (1<<3) || entry->parsed->protocols & (1<<2)) {
  202. /* version 2 format */
  203. extend_info_t *extend_info = rendcirc->build_state->chosen_exit;
  204. int klen;
  205. /* nul pads */
  206. set_uint32(tmp+v3_shift+1, tor_addr_to_ipv4h(&extend_info->addr));
  207. set_uint16(tmp+v3_shift+5, htons(extend_info->port));
  208. memcpy(tmp+v3_shift+7, extend_info->identity_digest, DIGEST_LEN);
  209. klen = crypto_pk_asn1_encode(extend_info->onion_key,
  210. tmp+v3_shift+7+DIGEST_LEN+2,
  211. sizeof(tmp)-(v3_shift+7+DIGEST_LEN+2));
  212. set_uint16(tmp+v3_shift+7+DIGEST_LEN, htons(klen));
  213. memcpy(tmp+v3_shift+7+DIGEST_LEN+2+klen, rendcirc->rend_data->rend_cookie,
  214. REND_COOKIE_LEN);
  215. dh_offset = v3_shift+7+DIGEST_LEN+2+klen+REND_COOKIE_LEN;
  216. } else {
  217. /* Version 0. */
  218. strncpy(tmp, rendcirc->build_state->chosen_exit->nickname,
  219. (MAX_NICKNAME_LEN+1)); /* nul pads */
  220. memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_data->rend_cookie,
  221. REND_COOKIE_LEN);
  222. dh_offset = MAX_NICKNAME_LEN+1+REND_COOKIE_LEN;
  223. }
  224. if (crypto_dh_get_public(cpath->dh_handshake_state, tmp+dh_offset,
  225. DH_KEY_LEN)<0) {
  226. log_warn(LD_BUG, "Internal error: couldn't extract g^x.");
  227. goto perm_err;
  228. }
  229. note_crypto_pk_op(REND_CLIENT);
  230. /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
  231. * to avoid buffer overflows? */
  232. r = crypto_pk_public_hybrid_encrypt(intro_key, payload+DIGEST_LEN,
  233. sizeof(payload)-DIGEST_LEN,
  234. tmp,
  235. (int)(dh_offset+DH_KEY_LEN),
  236. PK_PKCS1_OAEP_PADDING, 0);
  237. if (r<0) {
  238. log_warn(LD_BUG,"Internal error: hybrid pk encrypt failed.");
  239. goto perm_err;
  240. }
  241. payload_len = DIGEST_LEN + r;
  242. tor_assert(payload_len <= RELAY_PAYLOAD_SIZE); /* we overran something */
  243. log_info(LD_REND, "Sending an INTRODUCE1 cell");
  244. if (relay_send_command_from_edge(0, TO_CIRCUIT(introcirc),
  245. RELAY_COMMAND_INTRODUCE1,
  246. payload, payload_len,
  247. introcirc->cpath->prev)<0) {
  248. /* introcirc is already marked for close. leave rendcirc alone. */
  249. log_warn(LD_BUG, "Couldn't send INTRODUCE1 cell");
  250. return -2;
  251. }
  252. /* Now, we wait for an ACK or NAK on this circuit. */
  253. introcirc->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
  254. return 0;
  255. perm_err:
  256. if (!introcirc->_base.marked_for_close)
  257. circuit_mark_for_close(TO_CIRCUIT(introcirc), END_CIRC_REASON_INTERNAL);
  258. circuit_mark_for_close(TO_CIRCUIT(rendcirc), END_CIRC_REASON_INTERNAL);
  259. return -2;
  260. }
  261. /** Called when a rendezvous circuit is open; sends a establish
  262. * rendezvous circuit as appropriate. */
  263. void
  264. rend_client_rendcirc_has_opened(origin_circuit_t *circ)
  265. {
  266. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  267. log_info(LD_REND,"rendcirc is open");
  268. /* generate a rendezvous cookie, store it in circ */
  269. if (rend_client_send_establish_rendezvous(circ) < 0) {
  270. return;
  271. }
  272. }
  273. /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
  274. */
  275. int
  276. rend_client_introduction_acked(origin_circuit_t *circ,
  277. const uint8_t *request, size_t request_len)
  278. {
  279. origin_circuit_t *rendcirc;
  280. (void) request; // XXXX Use this.
  281. if (circ->_base.purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  282. log_warn(LD_PROTOCOL,
  283. "Received REND_INTRODUCE_ACK on unexpected circuit %d.",
  284. circ->_base.n_circ_id);
  285. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  286. return -1;
  287. }
  288. tor_assert(circ->build_state->chosen_exit);
  289. tor_assert(circ->rend_data);
  290. if (request_len == 0) {
  291. /* It's an ACK; the introduction point relayed our introduction request. */
  292. /* Locate the rend circ which is waiting to hear about this ack,
  293. * and tell it.
  294. */
  295. log_info(LD_REND,"Received ack. Telling rend circ...");
  296. rendcirc = circuit_get_by_rend_query_and_purpose(
  297. circ->rend_data->onion_address, CIRCUIT_PURPOSE_C_REND_READY);
  298. if (rendcirc) { /* remember the ack */
  299. rendcirc->_base.purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  300. } else {
  301. log_info(LD_REND,"...Found no rend circ. Dropping on the floor.");
  302. }
  303. /* close the circuit: we won't need it anymore. */
  304. circ->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACKED;
  305. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  306. } else {
  307. /* It's a NAK; the introduction point didn't relay our request. */
  308. circ->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  309. /* Remove this intro point from the set of viable introduction
  310. * points. If any remain, extend to a new one and try again.
  311. * If none remain, refetch the service descriptor.
  312. */
  313. log_info(LD_REND, "Got nack for %s from %s...",
  314. safe_str_client(circ->rend_data->onion_address),
  315. circ->build_state->chosen_exit->nickname);
  316. if (rend_client_remove_intro_point(circ->build_state->chosen_exit,
  317. circ->rend_data) > 0) {
  318. /* There are introduction points left. Re-extend the circuit to
  319. * another intro point and try again. */
  320. int result = rend_client_reextend_intro_circuit(circ);
  321. /* XXXX If that call failed, should we close the rend circuit,
  322. * too? */
  323. return result;
  324. }
  325. }
  326. return 0;
  327. }
  328. /** The period for which a hidden service directory cannot be queried for
  329. * the same descriptor ID again. */
  330. #define REND_HID_SERV_DIR_REQUERY_PERIOD (15 * 60)
  331. /** Contains the last request times to hidden service directories for
  332. * certain queries; keys are strings consisting of base32-encoded
  333. * hidden service directory identities and base32-encoded descriptor IDs;
  334. * values are pointers to timestamps of the last requests. */
  335. static strmap_t *last_hid_serv_requests = NULL;
  336. /** Look up the last request time to hidden service directory <b>hs_dir</b>
  337. * for descriptor ID <b>desc_id_base32</b>. If <b>set</b> is non-zero,
  338. * assign the current time <b>now</b> and return that. Otherwise, return
  339. * the most recent request time, or 0 if no such request has been sent
  340. * before. */
  341. static time_t
  342. lookup_last_hid_serv_request(routerstatus_t *hs_dir,
  343. const char *desc_id_base32, time_t now, int set)
  344. {
  345. char hsdir_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  346. char hsdir_desc_comb_id[2 * REND_DESC_ID_V2_LEN_BASE32 + 1];
  347. time_t *last_request_ptr;
  348. base32_encode(hsdir_id_base32, sizeof(hsdir_id_base32),
  349. hs_dir->identity_digest, DIGEST_LEN);
  350. tor_snprintf(hsdir_desc_comb_id, sizeof(hsdir_desc_comb_id), "%s%s",
  351. hsdir_id_base32, desc_id_base32);
  352. if (set) {
  353. time_t *oldptr;
  354. last_request_ptr = tor_malloc_zero(sizeof(time_t));
  355. *last_request_ptr = now;
  356. oldptr = strmap_set(last_hid_serv_requests, hsdir_desc_comb_id,
  357. last_request_ptr);
  358. tor_free(oldptr);
  359. } else
  360. last_request_ptr = strmap_get_lc(last_hid_serv_requests,
  361. hsdir_desc_comb_id);
  362. return (last_request_ptr) ? *last_request_ptr : 0;
  363. }
  364. /** Clean the history of request times to hidden service directories, so that
  365. * it does not contain requests older than REND_HID_SERV_DIR_REQUERY_PERIOD
  366. * seconds any more. */
  367. static void
  368. directory_clean_last_hid_serv_requests(void)
  369. {
  370. strmap_iter_t *iter;
  371. time_t cutoff = time(NULL) - REND_HID_SERV_DIR_REQUERY_PERIOD;
  372. if (!last_hid_serv_requests)
  373. last_hid_serv_requests = strmap_new();
  374. for (iter = strmap_iter_init(last_hid_serv_requests);
  375. !strmap_iter_done(iter); ) {
  376. const char *key;
  377. void *val;
  378. time_t *ent;
  379. strmap_iter_get(iter, &key, &val);
  380. ent = (time_t *) val;
  381. if (*ent < cutoff) {
  382. iter = strmap_iter_next_rmv(last_hid_serv_requests, iter);
  383. tor_free(ent);
  384. } else {
  385. iter = strmap_iter_next(last_hid_serv_requests, iter);
  386. }
  387. }
  388. }
  389. /** Determine the responsible hidden service directories for <b>desc_id</b>
  390. * and fetch the descriptor belonging to that ID from one of them. Only
  391. * send a request to hidden service directories that we did not try within
  392. * the last REND_HID_SERV_DIR_REQUERY_PERIOD seconds; on success, return 1,
  393. * in the case that no hidden service directory is left to ask for the
  394. * descriptor, return 0, and in case of a failure -1. <b>query</b> is only
  395. * passed for pretty log statements. */
  396. static int
  397. directory_get_from_hs_dir(const char *desc_id, const rend_data_t *rend_query)
  398. {
  399. smartlist_t *responsible_dirs = smartlist_create();
  400. routerstatus_t *hs_dir;
  401. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  402. time_t now = time(NULL);
  403. char descriptor_cookie_base64[3*REND_DESC_COOKIE_LEN_BASE64];
  404. tor_assert(desc_id);
  405. tor_assert(rend_query);
  406. /* Determine responsible dirs. Even if we can't get all we want,
  407. * work with the ones we have. If it's empty, we'll notice below. */
  408. hid_serv_get_responsible_directories(responsible_dirs, desc_id);
  409. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  410. desc_id, DIGEST_LEN);
  411. /* Only select those hidden service directories to which we did not send
  412. * a request recently and for which we have a router descriptor here. */
  413. directory_clean_last_hid_serv_requests(); /* Clean request history first. */
  414. SMARTLIST_FOREACH(responsible_dirs, routerstatus_t *, dir, {
  415. if (lookup_last_hid_serv_request(dir, desc_id_base32, 0, 0) +
  416. REND_HID_SERV_DIR_REQUERY_PERIOD >= now ||
  417. !router_get_by_digest(dir->identity_digest))
  418. SMARTLIST_DEL_CURRENT(responsible_dirs, dir);
  419. });
  420. hs_dir = smartlist_choose(responsible_dirs);
  421. smartlist_free(responsible_dirs);
  422. if (!hs_dir) {
  423. log_info(LD_REND, "Could not pick one of the responsible hidden "
  424. "service directories, because we requested them all "
  425. "recently without success.");
  426. return 0;
  427. }
  428. /* Remember, that we are requesting a descriptor from this hidden service
  429. * directory now. */
  430. lookup_last_hid_serv_request(hs_dir, desc_id_base32, now, 1);
  431. /* Encode descriptor cookie for logging purposes. */
  432. if (rend_query->auth_type != REND_NO_AUTH) {
  433. if (base64_encode(descriptor_cookie_base64,
  434. sizeof(descriptor_cookie_base64),
  435. rend_query->descriptor_cookie, REND_DESC_COOKIE_LEN)<0) {
  436. log_warn(LD_BUG, "Could not base64-encode descriptor cookie.");
  437. return 0;
  438. }
  439. /* Remove == signs and newline. */
  440. descriptor_cookie_base64[strlen(descriptor_cookie_base64)-3] = '\0';
  441. } else {
  442. strlcpy(descriptor_cookie_base64, "(none)",
  443. sizeof(descriptor_cookie_base64));
  444. }
  445. /* Send fetch request. (Pass query and possibly descriptor cookie so that
  446. * they can be written to the directory connection and be referred to when
  447. * the response arrives. */
  448. directory_initiate_command_routerstatus_rend(hs_dir,
  449. DIR_PURPOSE_FETCH_RENDDESC_V2,
  450. ROUTER_PURPOSE_GENERAL,
  451. 1, desc_id_base32, NULL, 0, 0,
  452. rend_query);
  453. log_info(LD_REND, "Sending fetch request for v2 descriptor for "
  454. "service '%s' with descriptor ID '%s', auth type %d, "
  455. "and descriptor cookie '%s' to hidden service "
  456. "directory '%s' on port %d.",
  457. rend_query->onion_address, desc_id_base32,
  458. rend_query->auth_type,
  459. (rend_query->auth_type == REND_NO_AUTH ? "[none]" :
  460. escaped_safe_str_client(descriptor_cookie_base64)),
  461. hs_dir->nickname, hs_dir->dir_port);
  462. return 1;
  463. }
  464. /** Unless we already have a descriptor for <b>rend_query</b> with at least
  465. * one (possibly) working introduction point in it, start a connection to a
  466. * hidden service directory to fetch a v2 rendezvous service descriptor. */
  467. void
  468. rend_client_refetch_v2_renddesc(const rend_data_t *rend_query)
  469. {
  470. char descriptor_id[DIGEST_LEN];
  471. int replicas_left_to_try[REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS];
  472. int i, tries_left;
  473. rend_cache_entry_t *e = NULL;
  474. tor_assert(rend_query);
  475. /* Are we configured to fetch descriptors? */
  476. if (!get_options()->FetchHidServDescriptors) {
  477. log_warn(LD_REND, "We received an onion address for a v2 rendezvous "
  478. "service descriptor, but are not fetching service descriptors.");
  479. return;
  480. }
  481. /* Before fetching, check if we already have the descriptor here. */
  482. if (rend_cache_lookup_entry(rend_query->onion_address, -1, &e) > 0) {
  483. log_info(LD_REND, "We would fetch a v2 rendezvous descriptor, but we "
  484. "already have that descriptor here. Not fetching.");
  485. return;
  486. }
  487. log_debug(LD_REND, "Fetching v2 rendezvous descriptor for service %s",
  488. safe_str_client(rend_query->onion_address));
  489. /* Randomly iterate over the replicas until a descriptor can be fetched
  490. * from one of the consecutive nodes, or no options are left. */
  491. tries_left = REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS;
  492. for (i = 0; i < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; i++)
  493. replicas_left_to_try[i] = i;
  494. while (tries_left > 0) {
  495. int rand = crypto_rand_int(tries_left);
  496. int chosen_replica = replicas_left_to_try[rand];
  497. replicas_left_to_try[rand] = replicas_left_to_try[--tries_left];
  498. if (rend_compute_v2_desc_id(descriptor_id, rend_query->onion_address,
  499. rend_query->auth_type == REND_STEALTH_AUTH ?
  500. rend_query->descriptor_cookie : NULL,
  501. time(NULL), chosen_replica) < 0) {
  502. log_warn(LD_REND, "Internal error: Computing v2 rendezvous "
  503. "descriptor ID did not succeed.");
  504. return;
  505. }
  506. if (directory_get_from_hs_dir(descriptor_id, rend_query) != 0)
  507. return; /* either success or failure, but we're done */
  508. }
  509. /* If we come here, there are no hidden service directories left. */
  510. log_info(LD_REND, "Could not pick one of the responsible hidden "
  511. "service directories to fetch descriptors, because "
  512. "we already tried them all unsuccessfully.");
  513. /* Close pending connections. */
  514. rend_client_desc_trynow(rend_query->onion_address);
  515. return;
  516. }
  517. /** Cancel all rendezvous descriptor fetches currently in progress.
  518. */
  519. void
  520. rend_client_cancel_descriptor_fetches(void)
  521. {
  522. smartlist_t *connection_array = get_connection_array();
  523. SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
  524. if (conn->type == CONN_TYPE_DIR &&
  525. (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC ||
  526. conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2)) {
  527. /* It's a rendezvous descriptor fetch in progress -- cancel it
  528. * by marking the connection for close.
  529. *
  530. * Even if this connection has already reached EOF, this is
  531. * enough to make sure that if the descriptor hasn't been
  532. * processed yet, it won't be. See the end of
  533. * connection_handle_read; connection_reached_eof (indirectly)
  534. * processes whatever response the connection received. */
  535. const rend_data_t *rd = (TO_DIR_CONN(conn))->rend_data;
  536. if (!rd) {
  537. log_warn(LD_BUG | LD_REND,
  538. "Marking for close dir conn fetching rendezvous "
  539. "descriptor for unknown service!");
  540. } else {
  541. log_debug(LD_REND, "Marking for close dir conn fetching "
  542. "rendezvous descriptor for service %s",
  543. safe_str(rd->onion_address));
  544. }
  545. connection_mark_for_close(conn);
  546. }
  547. } SMARTLIST_FOREACH_END(conn);
  548. }
  549. /** Remove failed_intro from ent. If ent now has no intro points, or
  550. * service is unrecognized, then launch a new renddesc fetch.
  551. *
  552. * Return -1 if error, 0 if no intro points remain or service
  553. * unrecognized, 1 if recognized and some intro points remain.
  554. */
  555. int
  556. rend_client_remove_intro_point(extend_info_t *failed_intro,
  557. const rend_data_t *rend_query)
  558. {
  559. int i, r;
  560. rend_cache_entry_t *ent;
  561. connection_t *conn;
  562. r = rend_cache_lookup_entry(rend_query->onion_address, -1, &ent);
  563. if (r<0) {
  564. log_warn(LD_BUG, "Malformed service ID %s.",
  565. escaped_safe_str_client(rend_query->onion_address));
  566. return -1;
  567. }
  568. if (r==0) {
  569. log_info(LD_REND, "Unknown service %s. Re-fetching descriptor.",
  570. escaped_safe_str_client(rend_query->onion_address));
  571. rend_client_refetch_v2_renddesc(rend_query);
  572. return 0;
  573. }
  574. for (i = 0; i < smartlist_len(ent->parsed->intro_nodes); i++) {
  575. rend_intro_point_t *intro = smartlist_get(ent->parsed->intro_nodes, i);
  576. if (tor_memeq(failed_intro->identity_digest,
  577. intro->extend_info->identity_digest, DIGEST_LEN)) {
  578. rend_intro_point_free(intro);
  579. smartlist_del(ent->parsed->intro_nodes, i);
  580. break;
  581. }
  582. }
  583. if (! rend_client_any_intro_points_usable(ent)) {
  584. log_info(LD_REND,
  585. "No more intro points remain for %s. Re-fetching descriptor.",
  586. escaped_safe_str_client(rend_query->onion_address));
  587. rend_client_refetch_v2_renddesc(rend_query);
  588. /* move all pending streams back to renddesc_wait */
  589. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  590. AP_CONN_STATE_CIRCUIT_WAIT,
  591. rend_query->onion_address))) {
  592. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  593. }
  594. return 0;
  595. }
  596. log_info(LD_REND,"%d options left for %s.",
  597. smartlist_len(ent->parsed->intro_nodes),
  598. escaped_safe_str_client(rend_query->onion_address));
  599. return 1;
  600. }
  601. /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  602. * the circuit to C_REND_READY.
  603. */
  604. int
  605. rend_client_rendezvous_acked(origin_circuit_t *circ, const uint8_t *request,
  606. size_t request_len)
  607. {
  608. (void) request;
  609. (void) request_len;
  610. /* we just got an ack for our establish-rendezvous. switch purposes. */
  611. if (circ->_base.purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  612. log_warn(LD_PROTOCOL,"Got a rendezvous ack when we weren't expecting one. "
  613. "Closing circ.");
  614. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  615. return -1;
  616. }
  617. log_info(LD_REND,"Got rendezvous ack. This circuit is now ready for "
  618. "rendezvous.");
  619. circ->_base.purpose = CIRCUIT_PURPOSE_C_REND_READY;
  620. /* XXXX023 This is a pretty brute-force approach. It'd be better to
  621. * attach only the connections that are waiting on this circuit, rather
  622. * than trying to attach them all. See comments bug 743. */
  623. /* If we already have the introduction circuit built, make sure we send
  624. * the INTRODUCE cell _now_ */
  625. connection_ap_attach_pending();
  626. return 0;
  627. }
  628. /** Bob sent us a rendezvous cell; join the circuits. */
  629. int
  630. rend_client_receive_rendezvous(origin_circuit_t *circ, const uint8_t *request,
  631. size_t request_len)
  632. {
  633. crypt_path_t *hop;
  634. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  635. if ((circ->_base.purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  636. circ->_base.purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
  637. || !circ->build_state->pending_final_cpath) {
  638. log_warn(LD_PROTOCOL,"Got rendezvous2 cell from hidden service, but not "
  639. "expecting it. Closing.");
  640. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  641. return -1;
  642. }
  643. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  644. log_warn(LD_PROTOCOL,"Incorrect length (%d) on RENDEZVOUS2 cell.",
  645. (int)request_len);
  646. goto err;
  647. }
  648. log_info(LD_REND,"Got RENDEZVOUS2 cell from hidden service.");
  649. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  650. tor_assert(circ->build_state);
  651. tor_assert(circ->build_state->pending_final_cpath);
  652. hop = circ->build_state->pending_final_cpath;
  653. tor_assert(hop->dh_handshake_state);
  654. if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN,
  655. hop->dh_handshake_state, (char*)request,
  656. DH_KEY_LEN,
  657. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  658. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  659. goto err;
  660. }
  661. /* ... and set up cpath. */
  662. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  663. goto err;
  664. /* Check whether the digest is right... */
  665. if (tor_memneq(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  666. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  667. goto err;
  668. }
  669. crypto_dh_free(hop->dh_handshake_state);
  670. hop->dh_handshake_state = NULL;
  671. /* All is well. Extend the circuit. */
  672. circ->_base.purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  673. hop->state = CPATH_STATE_OPEN;
  674. /* set the windows to default. these are the windows
  675. * that alice thinks bob has.
  676. */
  677. hop->package_window = circuit_initial_package_window();
  678. hop->deliver_window = CIRCWINDOW_START;
  679. onion_append_to_cpath(&circ->cpath, hop);
  680. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  681. /* XXXX023 This is a pretty brute-force approach. It'd be better to
  682. * attach only the connections that are waiting on this circuit, rather
  683. * than trying to attach them all. See comments bug 743. */
  684. connection_ap_attach_pending();
  685. memset(keys, 0, sizeof(keys));
  686. return 0;
  687. err:
  688. memset(keys, 0, sizeof(keys));
  689. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  690. return -1;
  691. }
  692. /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that are
  693. * waiting on <b>query</b>. If there's a working cache entry here with at
  694. * least one intro point, move them to the next state. */
  695. void
  696. rend_client_desc_trynow(const char *query)
  697. {
  698. edge_connection_t *conn;
  699. rend_cache_entry_t *entry;
  700. time_t now = time(NULL);
  701. smartlist_t *conns = get_connection_array();
  702. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, _conn) {
  703. if (_conn->type != CONN_TYPE_AP ||
  704. _conn->state != AP_CONN_STATE_RENDDESC_WAIT ||
  705. _conn->marked_for_close)
  706. continue;
  707. conn = TO_EDGE_CONN(_conn);
  708. if (!conn->rend_data)
  709. continue;
  710. if (rend_cmp_service_ids(query, conn->rend_data->onion_address))
  711. continue;
  712. assert_connection_ok(TO_CONN(conn), now);
  713. if (rend_cache_lookup_entry(conn->rend_data->onion_address, -1,
  714. &entry) == 1 &&
  715. rend_client_any_intro_points_usable(entry)) {
  716. /* either this fetch worked, or it failed but there was a
  717. * valid entry from before which we should reuse */
  718. log_info(LD_REND,"Rend desc is usable. Launching circuits.");
  719. conn->_base.state = AP_CONN_STATE_CIRCUIT_WAIT;
  720. /* restart their timeout values, so they get a fair shake at
  721. * connecting to the hidden service. */
  722. conn->_base.timestamp_created = now;
  723. conn->_base.timestamp_lastread = now;
  724. conn->_base.timestamp_lastwritten = now;
  725. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  726. /* it will never work */
  727. log_warn(LD_REND,"Rendezvous attempt failed. Closing.");
  728. if (!conn->_base.marked_for_close)
  729. connection_mark_unattached_ap(conn, END_STREAM_REASON_CANT_ATTACH);
  730. }
  731. } else { /* 404, or fetch didn't get that far */
  732. log_notice(LD_REND,"Closing stream for '%s.onion': hidden service is "
  733. "unavailable (try again later).",
  734. safe_str_client(query));
  735. connection_mark_unattached_ap(conn, END_STREAM_REASON_RESOLVEFAILED);
  736. }
  737. } SMARTLIST_FOREACH_END(_conn);
  738. }
  739. /** Return a newly allocated extend_info_t* for a randomly chosen introduction
  740. * point for the named hidden service. Return NULL if all introduction points
  741. * have been tried and failed.
  742. */
  743. extend_info_t *
  744. rend_client_get_random_intro(const rend_data_t *rend_query)
  745. {
  746. extend_info_t *result;
  747. rend_cache_entry_t *entry;
  748. if (rend_cache_lookup_entry(rend_query->onion_address, -1, &entry) < 1) {
  749. log_warn(LD_REND,
  750. "Query '%s' didn't have valid rend desc in cache. Failing.",
  751. safe_str_client(rend_query->onion_address));
  752. return NULL;
  753. }
  754. /* See if we can get a node that complies with ExcludeNodes */
  755. if ((result = rend_client_get_random_intro_impl(entry, 1, 1)))
  756. return result;
  757. /* If not, and StrictNodes is not set, see if we can return any old node
  758. */
  759. if (!get_options()->StrictNodes)
  760. return rend_client_get_random_intro_impl(entry, 0, 1);
  761. return NULL;
  762. }
  763. /** As rend_client_get_random_intro, except assume that StrictNodes is set
  764. * iff <b>strict</b> is true. If <b>warnings</b> is false, don't complain
  765. * to the user when we're out of nodes, even if StrictNodes is true.
  766. */
  767. static extend_info_t *
  768. rend_client_get_random_intro_impl(const rend_cache_entry_t *entry,
  769. const int strict,
  770. const int warnings)
  771. {
  772. int i;
  773. rend_intro_point_t *intro;
  774. routerinfo_t *router;
  775. or_options_t *options = get_options();
  776. smartlist_t *usable_nodes;
  777. int n_excluded = 0;
  778. /* We'll keep a separate list of the usable nodes. If this becomes empty,
  779. * no nodes are usable. */
  780. usable_nodes = smartlist_create();
  781. smartlist_add_all(usable_nodes, entry->parsed->intro_nodes);
  782. again:
  783. if (smartlist_len(usable_nodes) == 0) {
  784. if (n_excluded && get_options()->StrictNodes && warnings) {
  785. /* We only want to warn if StrictNodes is really set. Otherwise
  786. * we're just about to retry anyways.
  787. */
  788. log_warn(LD_REND, "All introduction points for hidden service are "
  789. "at excluded relays, and StrictNodes is set. Skipping.");
  790. }
  791. smartlist_free(usable_nodes);
  792. return NULL;
  793. }
  794. i = crypto_rand_int(smartlist_len(usable_nodes));
  795. intro = smartlist_get(usable_nodes, i);
  796. /* Do we need to look up the router or is the extend info complete? */
  797. if (!intro->extend_info->onion_key) {
  798. if (tor_digest_is_zero(intro->extend_info->identity_digest))
  799. router = router_get_by_hexdigest(intro->extend_info->nickname);
  800. else
  801. router = router_get_by_digest(intro->extend_info->identity_digest);
  802. if (!router) {
  803. log_info(LD_REND, "Unknown router with nickname '%s'; trying another.",
  804. intro->extend_info->nickname);
  805. smartlist_del(usable_nodes, i);
  806. goto again;
  807. }
  808. extend_info_free(intro->extend_info);
  809. intro->extend_info = extend_info_from_router(router);
  810. }
  811. /* Check if we should refuse to talk to this router. */
  812. if (options->ExcludeNodes && strict &&
  813. routerset_contains_extendinfo(options->ExcludeNodes,
  814. intro->extend_info)) {
  815. n_excluded++;
  816. smartlist_del(usable_nodes, i);
  817. goto again;
  818. }
  819. smartlist_free(usable_nodes);
  820. return extend_info_dup(intro->extend_info);
  821. }
  822. /** Return true iff any introduction points still listed in <b>entry</b> are
  823. * usable. */
  824. int
  825. rend_client_any_intro_points_usable(const rend_cache_entry_t *entry)
  826. {
  827. return rend_client_get_random_intro_impl(
  828. entry, get_options()->StrictNodes, 0) != NULL;
  829. }
  830. /** Client-side authorizations for hidden services; map of onion address to
  831. * rend_service_authorization_t*. */
  832. static strmap_t *auth_hid_servs = NULL;
  833. /** Look up the client-side authorization for the hidden service with
  834. * <b>onion_address</b>. Return NULL if no authorization is available for
  835. * that address. */
  836. rend_service_authorization_t*
  837. rend_client_lookup_service_authorization(const char *onion_address)
  838. {
  839. tor_assert(onion_address);
  840. if (!auth_hid_servs) return NULL;
  841. return strmap_get(auth_hid_servs, onion_address);
  842. }
  843. /** Helper: Free storage held by rend_service_authorization_t. */
  844. static void
  845. rend_service_authorization_free(rend_service_authorization_t *auth)
  846. {
  847. tor_free(auth);
  848. }
  849. /** Helper for strmap_free. */
  850. static void
  851. rend_service_authorization_strmap_item_free(void *service_auth)
  852. {
  853. rend_service_authorization_free(service_auth);
  854. }
  855. /** Release all the storage held in auth_hid_servs.
  856. */
  857. void
  858. rend_service_authorization_free_all(void)
  859. {
  860. if (!auth_hid_servs) {
  861. return;
  862. }
  863. strmap_free(auth_hid_servs, rend_service_authorization_strmap_item_free);
  864. auth_hid_servs = NULL;
  865. }
  866. /** Parse <b>config_line</b> as a client-side authorization for a hidden
  867. * service and add it to the local map of hidden service authorizations.
  868. * Return 0 for success and -1 for failure. */
  869. int
  870. rend_parse_service_authorization(or_options_t *options, int validate_only)
  871. {
  872. config_line_t *line;
  873. int res = -1;
  874. strmap_t *parsed = strmap_new();
  875. smartlist_t *sl = smartlist_create();
  876. rend_service_authorization_t *auth = NULL;
  877. for (line = options->HidServAuth; line; line = line->next) {
  878. char *onion_address, *descriptor_cookie;
  879. char descriptor_cookie_tmp[REND_DESC_COOKIE_LEN+2];
  880. char descriptor_cookie_base64ext[REND_DESC_COOKIE_LEN_BASE64+2+1];
  881. int auth_type_val = 0;
  882. auth = NULL;
  883. SMARTLIST_FOREACH(sl, char *, c, tor_free(c););
  884. smartlist_clear(sl);
  885. smartlist_split_string(sl, line->value, " ",
  886. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  887. if (smartlist_len(sl) < 2) {
  888. log_warn(LD_CONFIG, "Configuration line does not consist of "
  889. "\"onion-address authorization-cookie [service-name]\": "
  890. "'%s'", line->value);
  891. goto err;
  892. }
  893. auth = tor_malloc_zero(sizeof(rend_service_authorization_t));
  894. /* Parse onion address. */
  895. onion_address = smartlist_get(sl, 0);
  896. if (strlen(onion_address) != REND_SERVICE_ADDRESS_LEN ||
  897. strcmpend(onion_address, ".onion")) {
  898. log_warn(LD_CONFIG, "Onion address has wrong format: '%s'",
  899. onion_address);
  900. goto err;
  901. }
  902. strlcpy(auth->onion_address, onion_address, REND_SERVICE_ID_LEN_BASE32+1);
  903. if (!rend_valid_service_id(auth->onion_address)) {
  904. log_warn(LD_CONFIG, "Onion address has wrong format: '%s'",
  905. onion_address);
  906. goto err;
  907. }
  908. /* Parse descriptor cookie. */
  909. descriptor_cookie = smartlist_get(sl, 1);
  910. if (strlen(descriptor_cookie) != REND_DESC_COOKIE_LEN_BASE64) {
  911. log_warn(LD_CONFIG, "Authorization cookie has wrong length: '%s'",
  912. descriptor_cookie);
  913. goto err;
  914. }
  915. /* Add trailing zero bytes (AA) to make base64-decoding happy. */
  916. tor_snprintf(descriptor_cookie_base64ext,
  917. REND_DESC_COOKIE_LEN_BASE64+2+1,
  918. "%sAA", descriptor_cookie);
  919. if (base64_decode(descriptor_cookie_tmp, sizeof(descriptor_cookie_tmp),
  920. descriptor_cookie_base64ext,
  921. strlen(descriptor_cookie_base64ext)) < 0) {
  922. log_warn(LD_CONFIG, "Decoding authorization cookie failed: '%s'",
  923. descriptor_cookie);
  924. goto err;
  925. }
  926. auth_type_val = (descriptor_cookie_tmp[16] >> 4) + 1;
  927. if (auth_type_val < 1 || auth_type_val > 2) {
  928. log_warn(LD_CONFIG, "Authorization cookie has unknown authorization "
  929. "type encoded.");
  930. goto err;
  931. }
  932. auth->auth_type = auth_type_val == 1 ? REND_BASIC_AUTH : REND_STEALTH_AUTH;
  933. memcpy(auth->descriptor_cookie, descriptor_cookie_tmp,
  934. REND_DESC_COOKIE_LEN);
  935. if (strmap_get(parsed, auth->onion_address)) {
  936. log_warn(LD_CONFIG, "Duplicate authorization for the same hidden "
  937. "service.");
  938. goto err;
  939. }
  940. strmap_set(parsed, auth->onion_address, auth);
  941. auth = NULL;
  942. }
  943. res = 0;
  944. goto done;
  945. err:
  946. res = -1;
  947. done:
  948. rend_service_authorization_free(auth);
  949. SMARTLIST_FOREACH(sl, char *, c, tor_free(c););
  950. smartlist_free(sl);
  951. if (!validate_only && res == 0) {
  952. rend_service_authorization_free_all();
  953. auth_hid_servs = parsed;
  954. } else {
  955. strmap_free(parsed, rend_service_authorization_strmap_item_free);
  956. }
  957. return res;
  958. }