rendclient.c 39 KB

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