rendcommon.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file rendcommon.c
  6. * \brief Rendezvous implementation: shared code between
  7. * introducers, services, clients, and rendezvous points.
  8. **/
  9. #include "or.h"
  10. #include "circuitbuild.h"
  11. #include "config.h"
  12. #include "control.h"
  13. #include "rendclient.h"
  14. #include "rendcommon.h"
  15. #include "rendmid.h"
  16. #include "rendservice.h"
  17. #include "rephist.h"
  18. #include "router.h"
  19. #include "routerlist.h"
  20. #include "routerparse.h"
  21. #include "networkstatus.h"
  22. /** Return 0 if one and two are the same service ids, else -1 or 1 */
  23. int
  24. rend_cmp_service_ids(const char *one, const char *two)
  25. {
  26. return strcasecmp(one,two);
  27. }
  28. /** Free the storage held by the service descriptor <b>desc</b>.
  29. */
  30. void
  31. rend_service_descriptor_free(rend_service_descriptor_t *desc)
  32. {
  33. if (!desc)
  34. return;
  35. if (desc->pk)
  36. crypto_pk_free(desc->pk);
  37. if (desc->intro_nodes) {
  38. SMARTLIST_FOREACH(desc->intro_nodes, rend_intro_point_t *, intro,
  39. rend_intro_point_free(intro););
  40. smartlist_free(desc->intro_nodes);
  41. }
  42. if (desc->successful_uploads) {
  43. SMARTLIST_FOREACH(desc->successful_uploads, char *, c, tor_free(c););
  44. smartlist_free(desc->successful_uploads);
  45. }
  46. tor_free(desc);
  47. }
  48. /** Length of the descriptor cookie that is used for versioned hidden
  49. * service descriptors. */
  50. #define REND_DESC_COOKIE_LEN 16
  51. /** Length of the replica number that is used to determine the secret ID
  52. * part of versioned hidden service descriptors. */
  53. #define REND_REPLICA_LEN 1
  54. /** Compute the descriptor ID for <b>service_id</b> of length
  55. * <b>REND_SERVICE_ID_LEN</b> and <b>secret_id_part</b> of length
  56. * <b>DIGEST_LEN</b>, and write it to <b>descriptor_id_out</b> of length
  57. * <b>DIGEST_LEN</b>. */
  58. void
  59. rend_get_descriptor_id_bytes(char *descriptor_id_out,
  60. const char *service_id,
  61. const char *secret_id_part)
  62. {
  63. crypto_digest_t *digest = crypto_digest_new();
  64. crypto_digest_add_bytes(digest, service_id, REND_SERVICE_ID_LEN);
  65. crypto_digest_add_bytes(digest, secret_id_part, DIGEST_LEN);
  66. crypto_digest_get_digest(digest, descriptor_id_out, DIGEST_LEN);
  67. crypto_digest_free(digest);
  68. }
  69. /** Compute the secret ID part for time_period,
  70. * a <b>descriptor_cookie</b> of length
  71. * <b>REND_DESC_COOKIE_LEN</b> which may also be <b>NULL</b> if no
  72. * descriptor_cookie shall be used, and <b>replica</b>, and write it to
  73. * <b>secret_id_part</b> of length DIGEST_LEN. */
  74. static void
  75. get_secret_id_part_bytes(char *secret_id_part, uint32_t time_period,
  76. const char *descriptor_cookie, uint8_t replica)
  77. {
  78. crypto_digest_t *digest = crypto_digest_new();
  79. time_period = htonl(time_period);
  80. crypto_digest_add_bytes(digest, (char*)&time_period, sizeof(uint32_t));
  81. if (descriptor_cookie) {
  82. crypto_digest_add_bytes(digest, descriptor_cookie,
  83. REND_DESC_COOKIE_LEN);
  84. }
  85. crypto_digest_add_bytes(digest, (const char *)&replica, REND_REPLICA_LEN);
  86. crypto_digest_get_digest(digest, secret_id_part, DIGEST_LEN);
  87. crypto_digest_free(digest);
  88. }
  89. /** Return the time period for time <b>now</b> plus a potentially
  90. * intended <b>deviation</b> of one or more periods, based on the first byte
  91. * of <b>service_id</b>. */
  92. static uint32_t
  93. get_time_period(time_t now, uint8_t deviation, const char *service_id)
  94. {
  95. /* The time period is the number of REND_TIME_PERIOD_V2_DESC_VALIDITY
  96. * intervals that have passed since the epoch, offset slightly so that
  97. * each service's time periods start and end at a fraction of that
  98. * period based on their first byte. */
  99. return (uint32_t)
  100. (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
  101. / REND_TIME_PERIOD_V2_DESC_VALIDITY + deviation;
  102. }
  103. /** Compute the time in seconds that a descriptor that is generated
  104. * <b>now</b> for <b>service_id</b> will be valid. */
  105. static uint32_t
  106. get_seconds_valid(time_t now, const char *service_id)
  107. {
  108. uint32_t result = REND_TIME_PERIOD_V2_DESC_VALIDITY -
  109. ((uint32_t)
  110. (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
  111. % REND_TIME_PERIOD_V2_DESC_VALIDITY);
  112. return result;
  113. }
  114. /** Compute the binary <b>desc_id_out</b> (DIGEST_LEN bytes long) for a given
  115. * base32-encoded <b>service_id</b> and optional unencoded
  116. * <b>descriptor_cookie</b> of length REND_DESC_COOKIE_LEN,
  117. * at time <b>now</b> for replica number
  118. * <b>replica</b>. <b>desc_id</b> needs to have <b>DIGEST_LEN</b> bytes
  119. * free. Return 0 for success, -1 otherwise. */
  120. int
  121. rend_compute_v2_desc_id(char *desc_id_out, const char *service_id,
  122. const char *descriptor_cookie, time_t now,
  123. uint8_t replica)
  124. {
  125. char service_id_binary[REND_SERVICE_ID_LEN];
  126. char secret_id_part[DIGEST_LEN];
  127. uint32_t time_period;
  128. if (!service_id ||
  129. strlen(service_id) != REND_SERVICE_ID_LEN_BASE32) {
  130. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  131. "Illegal service ID: %s",
  132. safe_str(service_id));
  133. return -1;
  134. }
  135. if (replica >= REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS) {
  136. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  137. "Replica number out of range: %d", replica);
  138. return -1;
  139. }
  140. /* Convert service ID to binary. */
  141. if (base32_decode(service_id_binary, REND_SERVICE_ID_LEN,
  142. service_id, REND_SERVICE_ID_LEN_BASE32) < 0) {
  143. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  144. "Illegal characters in service ID: %s",
  145. safe_str_client(service_id));
  146. return -1;
  147. }
  148. /* Calculate current time-period. */
  149. time_period = get_time_period(now, 0, service_id_binary);
  150. /* Calculate secret-id-part = h(time-period | desc-cookie | replica). */
  151. get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
  152. replica);
  153. /* Calculate descriptor ID: H(permanent-id | secret-id-part) */
  154. rend_get_descriptor_id_bytes(desc_id_out, service_id_binary, secret_id_part);
  155. return 0;
  156. }
  157. /** Encode the introduction points in <b>desc</b> and write the result to a
  158. * newly allocated string pointed to by <b>encoded</b>. Return 0 for
  159. * success, -1 otherwise. */
  160. static int
  161. rend_encode_v2_intro_points(char **encoded, rend_service_descriptor_t *desc)
  162. {
  163. size_t unenc_len;
  164. char *unenc = NULL;
  165. size_t unenc_written = 0;
  166. int i;
  167. int r = -1;
  168. /* Assemble unencrypted list of introduction points. */
  169. unenc_len = smartlist_len(desc->intro_nodes) * 1000; /* too long, but ok. */
  170. unenc = tor_malloc_zero(unenc_len);
  171. for (i = 0; i < smartlist_len(desc->intro_nodes); i++) {
  172. char id_base32[REND_INTRO_POINT_ID_LEN_BASE32 + 1];
  173. char *onion_key = NULL;
  174. size_t onion_key_len;
  175. crypto_pk_t *intro_key;
  176. char *service_key = NULL;
  177. char *address = NULL;
  178. size_t service_key_len;
  179. int res;
  180. rend_intro_point_t *intro = smartlist_get(desc->intro_nodes, i);
  181. /* Obtain extend info with introduction point details. */
  182. extend_info_t *info = intro->extend_info;
  183. /* Encode introduction point ID. */
  184. base32_encode(id_base32, sizeof(id_base32),
  185. info->identity_digest, DIGEST_LEN);
  186. /* Encode onion key. */
  187. if (crypto_pk_write_public_key_to_string(info->onion_key, &onion_key,
  188. &onion_key_len) < 0) {
  189. log_warn(LD_REND, "Could not write onion key.");
  190. goto done;
  191. }
  192. /* Encode intro key. */
  193. intro_key = intro->intro_key;
  194. if (!intro_key ||
  195. crypto_pk_write_public_key_to_string(intro_key, &service_key,
  196. &service_key_len) < 0) {
  197. log_warn(LD_REND, "Could not write intro key.");
  198. tor_free(onion_key);
  199. goto done;
  200. }
  201. /* Assemble everything for this introduction point. */
  202. address = tor_addr_to_str_dup(&info->addr);
  203. res = tor_snprintf(unenc + unenc_written, unenc_len - unenc_written,
  204. "introduction-point %s\n"
  205. "ip-address %s\n"
  206. "onion-port %d\n"
  207. "onion-key\n%s"
  208. "service-key\n%s",
  209. id_base32,
  210. address,
  211. info->port,
  212. onion_key,
  213. service_key);
  214. tor_free(address);
  215. tor_free(onion_key);
  216. tor_free(service_key);
  217. if (res < 0) {
  218. log_warn(LD_REND, "Not enough space for writing introduction point "
  219. "string.");
  220. goto done;
  221. }
  222. /* Update total number of written bytes for unencrypted intro points. */
  223. unenc_written += res;
  224. }
  225. /* Finalize unencrypted introduction points. */
  226. if (unenc_len < unenc_written + 2) {
  227. log_warn(LD_REND, "Not enough space for finalizing introduction point "
  228. "string.");
  229. goto done;
  230. }
  231. unenc[unenc_written++] = '\n';
  232. unenc[unenc_written++] = 0;
  233. *encoded = unenc;
  234. r = 0;
  235. done:
  236. if (r<0)
  237. tor_free(unenc);
  238. return r;
  239. }
  240. /** Encrypt the encoded introduction points in <b>encoded</b> using
  241. * authorization type 'basic' with <b>client_cookies</b> and write the
  242. * result to a newly allocated string pointed to by <b>encrypted_out</b> of
  243. * length <b>encrypted_len_out</b>. Return 0 for success, -1 otherwise. */
  244. static int
  245. rend_encrypt_v2_intro_points_basic(char **encrypted_out,
  246. size_t *encrypted_len_out,
  247. const char *encoded,
  248. smartlist_t *client_cookies)
  249. {
  250. int r = -1, i, pos, enclen, client_blocks;
  251. size_t len, client_entries_len;
  252. char *enc = NULL, iv[CIPHER_IV_LEN], *client_part = NULL,
  253. session_key[CIPHER_KEY_LEN];
  254. smartlist_t *encrypted_session_keys = NULL;
  255. crypto_digest_t *digest;
  256. crypto_cipher_t *cipher;
  257. tor_assert(encoded);
  258. tor_assert(client_cookies && smartlist_len(client_cookies) > 0);
  259. /* Generate session key. */
  260. crypto_rand(session_key, CIPHER_KEY_LEN);
  261. /* Determine length of encrypted introduction points including session
  262. * keys. */
  263. client_blocks = 1 + ((smartlist_len(client_cookies) - 1) /
  264. REND_BASIC_AUTH_CLIENT_MULTIPLE);
  265. client_entries_len = client_blocks * REND_BASIC_AUTH_CLIENT_MULTIPLE *
  266. REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
  267. len = 2 + client_entries_len + CIPHER_IV_LEN + strlen(encoded);
  268. if (client_blocks >= 256) {
  269. log_warn(LD_REND, "Too many clients in introduction point string.");
  270. goto done;
  271. }
  272. enc = tor_malloc_zero(len);
  273. enc[0] = 0x01; /* type of authorization. */
  274. enc[1] = (uint8_t)client_blocks;
  275. /* Encrypt with random session key. */
  276. enclen = crypto_cipher_encrypt_with_iv(session_key,
  277. enc + 2 + client_entries_len,
  278. CIPHER_IV_LEN + strlen(encoded), encoded, strlen(encoded));
  279. if (enclen < 0) {
  280. log_warn(LD_REND, "Could not encrypt introduction point string.");
  281. goto done;
  282. }
  283. memcpy(iv, enc + 2 + client_entries_len, CIPHER_IV_LEN);
  284. /* Encrypt session key for cookies, determine client IDs, and put both
  285. * in a smartlist. */
  286. encrypted_session_keys = smartlist_new();
  287. SMARTLIST_FOREACH_BEGIN(client_cookies, const char *, cookie) {
  288. client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  289. /* Encrypt session key. */
  290. cipher = crypto_cipher_new(cookie);
  291. if (crypto_cipher_encrypt(cipher, client_part +
  292. REND_BASIC_AUTH_CLIENT_ID_LEN,
  293. session_key, CIPHER_KEY_LEN) < 0) {
  294. log_warn(LD_REND, "Could not encrypt session key for client.");
  295. crypto_cipher_free(cipher);
  296. tor_free(client_part);
  297. goto done;
  298. }
  299. crypto_cipher_free(cipher);
  300. /* Determine client ID. */
  301. digest = crypto_digest_new();
  302. crypto_digest_add_bytes(digest, cookie, REND_DESC_COOKIE_LEN);
  303. crypto_digest_add_bytes(digest, iv, CIPHER_IV_LEN);
  304. crypto_digest_get_digest(digest, client_part,
  305. REND_BASIC_AUTH_CLIENT_ID_LEN);
  306. crypto_digest_free(digest);
  307. /* Put both together. */
  308. smartlist_add(encrypted_session_keys, client_part);
  309. } SMARTLIST_FOREACH_END(cookie);
  310. /* Add some fake client IDs and encrypted session keys. */
  311. for (i = (smartlist_len(client_cookies) - 1) %
  312. REND_BASIC_AUTH_CLIENT_MULTIPLE;
  313. i < REND_BASIC_AUTH_CLIENT_MULTIPLE - 1; i++) {
  314. client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  315. crypto_rand(client_part, REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  316. smartlist_add(encrypted_session_keys, client_part);
  317. }
  318. /* Sort smartlist and put elements in result in order. */
  319. smartlist_sort_digests(encrypted_session_keys);
  320. pos = 2;
  321. SMARTLIST_FOREACH(encrypted_session_keys, const char *, entry, {
  322. memcpy(enc + pos, entry, REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  323. pos += REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
  324. });
  325. *encrypted_out = enc;
  326. *encrypted_len_out = len;
  327. enc = NULL; /* prevent free. */
  328. r = 0;
  329. done:
  330. tor_free(enc);
  331. if (encrypted_session_keys) {
  332. SMARTLIST_FOREACH(encrypted_session_keys, char *, d, tor_free(d););
  333. smartlist_free(encrypted_session_keys);
  334. }
  335. return r;
  336. }
  337. /** Encrypt the encoded introduction points in <b>encoded</b> using
  338. * authorization type 'stealth' with <b>descriptor_cookie</b> of length
  339. * REND_DESC_COOKIE_LEN and write the result to a newly allocated string
  340. * pointed to by <b>encrypted_out</b> of length <b>encrypted_len_out</b>.
  341. * Return 0 for success, -1 otherwise. */
  342. static int
  343. rend_encrypt_v2_intro_points_stealth(char **encrypted_out,
  344. size_t *encrypted_len_out,
  345. const char *encoded,
  346. const char *descriptor_cookie)
  347. {
  348. int r = -1, enclen;
  349. char *enc;
  350. tor_assert(encoded);
  351. tor_assert(descriptor_cookie);
  352. enc = tor_malloc_zero(1 + CIPHER_IV_LEN + strlen(encoded));
  353. enc[0] = 0x02; /* Auth type */
  354. enclen = crypto_cipher_encrypt_with_iv(descriptor_cookie,
  355. enc + 1,
  356. CIPHER_IV_LEN+strlen(encoded),
  357. encoded, strlen(encoded));
  358. if (enclen < 0) {
  359. log_warn(LD_REND, "Could not encrypt introduction point string.");
  360. goto done;
  361. }
  362. *encrypted_out = enc;
  363. *encrypted_len_out = enclen;
  364. enc = NULL; /* prevent free */
  365. r = 0;
  366. done:
  367. tor_free(enc);
  368. return r;
  369. }
  370. /** Attempt to parse the given <b>desc_str</b> and return true if this
  371. * succeeds, false otherwise. */
  372. static int
  373. rend_desc_v2_is_parsable(rend_encoded_v2_service_descriptor_t *desc)
  374. {
  375. rend_service_descriptor_t *test_parsed = NULL;
  376. char test_desc_id[DIGEST_LEN];
  377. char *test_intro_content = NULL;
  378. size_t test_intro_size;
  379. size_t test_encoded_size;
  380. const char *test_next;
  381. int res = rend_parse_v2_service_descriptor(&test_parsed, test_desc_id,
  382. &test_intro_content,
  383. &test_intro_size,
  384. &test_encoded_size,
  385. &test_next, desc->desc_str, 1);
  386. rend_service_descriptor_free(test_parsed);
  387. tor_free(test_intro_content);
  388. return (res >= 0);
  389. }
  390. /** Free the storage held by an encoded v2 service descriptor. */
  391. void
  392. rend_encoded_v2_service_descriptor_free(
  393. rend_encoded_v2_service_descriptor_t *desc)
  394. {
  395. if (!desc)
  396. return;
  397. tor_free(desc->desc_str);
  398. tor_free(desc);
  399. }
  400. /** Free the storage held by an introduction point info. */
  401. void
  402. rend_intro_point_free(rend_intro_point_t *intro)
  403. {
  404. if (!intro)
  405. return;
  406. extend_info_free(intro->extend_info);
  407. crypto_pk_free(intro->intro_key);
  408. if (intro->accepted_intro_rsa_parts != NULL) {
  409. replaycache_free(intro->accepted_intro_rsa_parts);
  410. }
  411. tor_free(intro);
  412. }
  413. /** Encode a set of rend_encoded_v2_service_descriptor_t's for <b>desc</b>
  414. * at time <b>now</b> using <b>service_key</b>, depending on
  415. * <b>auth_type</b> a <b>descriptor_cookie</b> and a list of
  416. * <b>client_cookies</b> (which are both <b>NULL</b> if no client
  417. * authorization is performed), and <b>period</b> (e.g. 0 for the current
  418. * period, 1 for the next period, etc.) and add them to the existing list
  419. * <b>descs_out</b>; return the number of seconds that the descriptors will
  420. * be found by clients, or -1 if the encoding was not successful. */
  421. int
  422. rend_encode_v2_descriptors(smartlist_t *descs_out,
  423. rend_service_descriptor_t *desc, time_t now,
  424. uint8_t period, rend_auth_type_t auth_type,
  425. crypto_pk_t *client_key,
  426. smartlist_t *client_cookies)
  427. {
  428. char service_id[DIGEST_LEN];
  429. char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
  430. uint32_t time_period;
  431. char *ipos_base64 = NULL, *ipos = NULL, *ipos_encrypted = NULL,
  432. *descriptor_cookie = NULL;
  433. size_t ipos_len = 0, ipos_encrypted_len = 0;
  434. int k;
  435. uint32_t seconds_valid;
  436. crypto_pk_t *service_key;
  437. if (!desc) {
  438. log_warn(LD_BUG, "Could not encode v2 descriptor: No desc given.");
  439. return -1;
  440. }
  441. service_key = (auth_type == REND_STEALTH_AUTH) ? client_key : desc->pk;
  442. tor_assert(service_key);
  443. if (auth_type == REND_STEALTH_AUTH) {
  444. descriptor_cookie = smartlist_get(client_cookies, 0);
  445. tor_assert(descriptor_cookie);
  446. }
  447. /* Obtain service_id from public key. */
  448. if (crypto_pk_get_digest(service_key, service_id) < 0) {
  449. log_warn(LD_BUG, "Couldn't compute service key digest.");
  450. return -1;
  451. }
  452. /* Calculate current time-period. */
  453. time_period = get_time_period(now, period, service_id);
  454. /* Determine how many seconds the descriptor will be valid. */
  455. seconds_valid = period * REND_TIME_PERIOD_V2_DESC_VALIDITY +
  456. get_seconds_valid(now, service_id);
  457. /* Assemble, possibly encrypt, and encode introduction points. */
  458. if (smartlist_len(desc->intro_nodes) > 0) {
  459. if (rend_encode_v2_intro_points(&ipos, desc) < 0) {
  460. log_warn(LD_REND, "Encoding of introduction points did not succeed.");
  461. return -1;
  462. }
  463. switch (auth_type) {
  464. case REND_NO_AUTH:
  465. ipos_len = strlen(ipos);
  466. break;
  467. case REND_BASIC_AUTH:
  468. if (rend_encrypt_v2_intro_points_basic(&ipos_encrypted,
  469. &ipos_encrypted_len, ipos,
  470. client_cookies) < 0) {
  471. log_warn(LD_REND, "Encrypting of introduction points did not "
  472. "succeed.");
  473. tor_free(ipos);
  474. return -1;
  475. }
  476. tor_free(ipos);
  477. ipos = ipos_encrypted;
  478. ipos_len = ipos_encrypted_len;
  479. break;
  480. case REND_STEALTH_AUTH:
  481. if (rend_encrypt_v2_intro_points_stealth(&ipos_encrypted,
  482. &ipos_encrypted_len, ipos,
  483. descriptor_cookie) < 0) {
  484. log_warn(LD_REND, "Encrypting of introduction points did not "
  485. "succeed.");
  486. tor_free(ipos);
  487. return -1;
  488. }
  489. tor_free(ipos);
  490. ipos = ipos_encrypted;
  491. ipos_len = ipos_encrypted_len;
  492. break;
  493. default:
  494. log_warn(LD_REND|LD_BUG, "Unrecognized authorization type %d",
  495. (int)auth_type);
  496. tor_free(ipos);
  497. return -1;
  498. }
  499. /* Base64-encode introduction points. */
  500. ipos_base64 = tor_calloc(ipos_len, 2);
  501. if (base64_encode(ipos_base64, ipos_len * 2, ipos, ipos_len,
  502. BASE64_ENCODE_MULTILINE)<0) {
  503. log_warn(LD_REND, "Could not encode introduction point string to "
  504. "base64. length=%d", (int)ipos_len);
  505. tor_free(ipos_base64);
  506. tor_free(ipos);
  507. return -1;
  508. }
  509. tor_free(ipos);
  510. }
  511. /* Encode REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS descriptors. */
  512. for (k = 0; k < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; k++) {
  513. char secret_id_part[DIGEST_LEN];
  514. char secret_id_part_base32[REND_SECRET_ID_PART_LEN_BASE32 + 1];
  515. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  516. char *permanent_key = NULL;
  517. size_t permanent_key_len;
  518. char published[ISO_TIME_LEN+1];
  519. int i;
  520. char protocol_versions_string[16]; /* max len: "0,1,2,3,4,5,6,7\0" */
  521. size_t protocol_versions_written;
  522. size_t desc_len;
  523. char *desc_str = NULL;
  524. int result = 0;
  525. size_t written = 0;
  526. char desc_digest[DIGEST_LEN];
  527. rend_encoded_v2_service_descriptor_t *enc =
  528. tor_malloc_zero(sizeof(rend_encoded_v2_service_descriptor_t));
  529. /* Calculate secret-id-part = h(time-period | cookie | replica). */
  530. get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
  531. k);
  532. base32_encode(secret_id_part_base32, sizeof(secret_id_part_base32),
  533. secret_id_part, DIGEST_LEN);
  534. /* Calculate descriptor ID. */
  535. rend_get_descriptor_id_bytes(enc->desc_id, service_id, secret_id_part);
  536. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  537. enc->desc_id, DIGEST_LEN);
  538. /* PEM-encode the public key */
  539. if (crypto_pk_write_public_key_to_string(service_key, &permanent_key,
  540. &permanent_key_len) < 0) {
  541. log_warn(LD_BUG, "Could not write public key to string.");
  542. rend_encoded_v2_service_descriptor_free(enc);
  543. goto err;
  544. }
  545. /* Encode timestamp. */
  546. format_iso_time(published, desc->timestamp);
  547. /* Write protocol-versions bitmask to comma-separated value string. */
  548. protocol_versions_written = 0;
  549. for (i = 0; i < 8; i++) {
  550. if (desc->protocols & 1 << i) {
  551. tor_snprintf(protocol_versions_string + protocol_versions_written,
  552. 16 - protocol_versions_written, "%d,", i);
  553. protocol_versions_written += 2;
  554. }
  555. }
  556. if (protocol_versions_written)
  557. protocol_versions_string[protocol_versions_written - 1] = '\0';
  558. else
  559. protocol_versions_string[0]= '\0';
  560. /* Assemble complete descriptor. */
  561. desc_len = 2000 + smartlist_len(desc->intro_nodes) * 1000; /* far too long,
  562. but okay.*/
  563. enc->desc_str = desc_str = tor_malloc_zero(desc_len);
  564. result = tor_snprintf(desc_str, desc_len,
  565. "rendezvous-service-descriptor %s\n"
  566. "version 2\n"
  567. "permanent-key\n%s"
  568. "secret-id-part %s\n"
  569. "publication-time %s\n"
  570. "protocol-versions %s\n",
  571. desc_id_base32,
  572. permanent_key,
  573. secret_id_part_base32,
  574. published,
  575. protocol_versions_string);
  576. tor_free(permanent_key);
  577. if (result < 0) {
  578. log_warn(LD_BUG, "Descriptor ran out of room.");
  579. rend_encoded_v2_service_descriptor_free(enc);
  580. goto err;
  581. }
  582. written = result;
  583. /* Add introduction points. */
  584. if (ipos_base64) {
  585. result = tor_snprintf(desc_str + written, desc_len - written,
  586. "introduction-points\n"
  587. "-----BEGIN MESSAGE-----\n%s"
  588. "-----END MESSAGE-----\n",
  589. ipos_base64);
  590. if (result < 0) {
  591. log_warn(LD_BUG, "could not write introduction points.");
  592. rend_encoded_v2_service_descriptor_free(enc);
  593. goto err;
  594. }
  595. written += result;
  596. }
  597. /* Add signature. */
  598. strlcpy(desc_str + written, "signature\n", desc_len - written);
  599. written += strlen(desc_str + written);
  600. if (crypto_digest(desc_digest, desc_str, written) < 0) {
  601. log_warn(LD_BUG, "could not create digest.");
  602. rend_encoded_v2_service_descriptor_free(enc);
  603. goto err;
  604. }
  605. if (router_append_dirobj_signature(desc_str + written,
  606. desc_len - written,
  607. desc_digest, DIGEST_LEN,
  608. service_key) < 0) {
  609. log_warn(LD_BUG, "Couldn't sign desc.");
  610. rend_encoded_v2_service_descriptor_free(enc);
  611. goto err;
  612. }
  613. written += strlen(desc_str+written);
  614. if (written+2 > desc_len) {
  615. log_warn(LD_BUG, "Could not finish desc.");
  616. rend_encoded_v2_service_descriptor_free(enc);
  617. goto err;
  618. }
  619. desc_str[written++] = 0;
  620. /* Check if we can parse our own descriptor. */
  621. if (!rend_desc_v2_is_parsable(enc)) {
  622. log_warn(LD_BUG, "Could not parse my own descriptor: %s", desc_str);
  623. rend_encoded_v2_service_descriptor_free(enc);
  624. goto err;
  625. }
  626. smartlist_add(descs_out, enc);
  627. /* Add the uploaded descriptor to the local service's descriptor cache */
  628. rend_cache_store_v2_desc_as_service(enc->desc_str);
  629. base32_encode(service_id_base32, sizeof(service_id_base32),
  630. service_id, REND_SERVICE_ID_LEN);
  631. control_event_hs_descriptor_created(service_id_base32, desc_id_base32, k);
  632. }
  633. log_info(LD_REND, "Successfully encoded a v2 descriptor and "
  634. "confirmed that it is parsable.");
  635. goto done;
  636. err:
  637. SMARTLIST_FOREACH(descs_out, rend_encoded_v2_service_descriptor_t *, d,
  638. rend_encoded_v2_service_descriptor_free(d););
  639. smartlist_clear(descs_out);
  640. seconds_valid = -1;
  641. done:
  642. tor_free(ipos_base64);
  643. return seconds_valid;
  644. }
  645. /** Sets <b>out</b> to the first 10 bytes of the digest of <b>pk</b>,
  646. * base32 encoded. NUL-terminates out. (We use this string to
  647. * identify services in directory requests and .onion URLs.)
  648. */
  649. int
  650. rend_get_service_id(crypto_pk_t *pk, char *out)
  651. {
  652. char buf[DIGEST_LEN];
  653. tor_assert(pk);
  654. if (crypto_pk_get_digest(pk, buf) < 0)
  655. return -1;
  656. base32_encode(out, REND_SERVICE_ID_LEN_BASE32+1, buf, REND_SERVICE_ID_LEN);
  657. return 0;
  658. }
  659. /** Return true iff <b>query</b> is a syntactically valid service ID (as
  660. * generated by rend_get_service_id). */
  661. int
  662. rend_valid_service_id(const char *query)
  663. {
  664. if (strlen(query) != REND_SERVICE_ID_LEN_BASE32)
  665. return 0;
  666. if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN_BASE32)
  667. return 0;
  668. return 1;
  669. }
  670. /** Return true iff <b>query</b> is a syntactically valid descriptor ID.
  671. * (as generated by rend_get_descriptor_id_bytes). */
  672. int
  673. rend_valid_descriptor_id(const char *query)
  674. {
  675. if (strlen(query) != REND_DESC_ID_V2_LEN_BASE32) {
  676. goto invalid;
  677. }
  678. if (strspn(query, BASE32_CHARS) != REND_DESC_ID_V2_LEN_BASE32) {
  679. goto invalid;
  680. }
  681. return 1;
  682. invalid:
  683. return 0;
  684. }
  685. /** Return true iff <b>client_name</b> is a syntactically valid name
  686. * for rendezvous client authentication. */
  687. int
  688. rend_valid_client_name(const char *client_name)
  689. {
  690. size_t len = strlen(client_name);
  691. if (len < 1 || len > REND_CLIENTNAME_MAX_LEN) {
  692. return 0;
  693. }
  694. if (strspn(client_name, REND_LEGAL_CLIENTNAME_CHARACTERS) != len) {
  695. return 0;
  696. }
  697. return 1;
  698. }
  699. /** Called when we get a rendezvous-related relay cell on circuit
  700. * <b>circ</b>. Dispatch on rendezvous relay command. */
  701. void
  702. rend_process_relay_cell(circuit_t *circ, const crypt_path_t *layer_hint,
  703. int command, size_t length,
  704. const uint8_t *payload)
  705. {
  706. or_circuit_t *or_circ = NULL;
  707. origin_circuit_t *origin_circ = NULL;
  708. int r = -2;
  709. if (CIRCUIT_IS_ORIGIN(circ)) {
  710. origin_circ = TO_ORIGIN_CIRCUIT(circ);
  711. if (!layer_hint || layer_hint != origin_circ->cpath->prev) {
  712. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  713. "Relay cell (rend purpose %d) from wrong hop on origin circ",
  714. command);
  715. origin_circ = NULL;
  716. }
  717. } else {
  718. or_circ = TO_OR_CIRCUIT(circ);
  719. }
  720. switch (command) {
  721. case RELAY_COMMAND_ESTABLISH_INTRO:
  722. if (or_circ)
  723. r = rend_mid_establish_intro(or_circ,payload,length);
  724. break;
  725. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  726. if (or_circ)
  727. r = rend_mid_establish_rendezvous(or_circ,payload,length);
  728. break;
  729. case RELAY_COMMAND_INTRODUCE1:
  730. if (or_circ)
  731. r = rend_mid_introduce(or_circ,payload,length);
  732. break;
  733. case RELAY_COMMAND_INTRODUCE2:
  734. if (origin_circ)
  735. r = rend_service_receive_introduction(origin_circ,payload,length);
  736. break;
  737. case RELAY_COMMAND_INTRODUCE_ACK:
  738. if (origin_circ)
  739. r = rend_client_introduction_acked(origin_circ,payload,length);
  740. break;
  741. case RELAY_COMMAND_RENDEZVOUS1:
  742. if (or_circ)
  743. r = rend_mid_rendezvous(or_circ,payload,length);
  744. break;
  745. case RELAY_COMMAND_RENDEZVOUS2:
  746. if (origin_circ)
  747. r = rend_client_receive_rendezvous(origin_circ,payload,length);
  748. break;
  749. case RELAY_COMMAND_INTRO_ESTABLISHED:
  750. if (origin_circ)
  751. r = rend_service_intro_established(origin_circ,payload,length);
  752. break;
  753. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  754. if (origin_circ)
  755. r = rend_client_rendezvous_acked(origin_circ,payload,length);
  756. break;
  757. default:
  758. tor_fragile_assert();
  759. }
  760. if (r == -2)
  761. log_info(LD_PROTOCOL, "Dropping cell (type %d) for wrong circuit type.",
  762. command);
  763. }
  764. /** Allocate and return a new rend_data_t with the same
  765. * contents as <b>query</b>. */
  766. rend_data_t *
  767. rend_data_dup(const rend_data_t *data)
  768. {
  769. rend_data_t *data_dup;
  770. tor_assert(data);
  771. data_dup = tor_memdup(data, sizeof(rend_data_t));
  772. data_dup->hsdirs_fp = smartlist_new();
  773. SMARTLIST_FOREACH(data->hsdirs_fp, char *, fp,
  774. smartlist_add(data_dup->hsdirs_fp,
  775. tor_memdup(fp, DIGEST_LEN)));
  776. return data_dup;
  777. }
  778. /** Compute descriptor ID for each replicas and save them. A valid onion
  779. * address must be present in the <b>rend_data</b>.
  780. *
  781. * Return 0 on success else -1. */
  782. static int
  783. compute_desc_id(rend_data_t *rend_data)
  784. {
  785. int ret = 0;
  786. unsigned replica;
  787. time_t now = time(NULL);
  788. tor_assert(rend_data);
  789. /* Compute descriptor ID for each replicas. */
  790. for (replica = 0; replica < ARRAY_LENGTH(rend_data->descriptor_id);
  791. replica++) {
  792. ret = rend_compute_v2_desc_id(rend_data->descriptor_id[replica],
  793. rend_data->onion_address,
  794. rend_data->descriptor_cookie,
  795. now, replica);
  796. if (ret < 0) {
  797. goto end;
  798. }
  799. }
  800. end:
  801. return ret;
  802. }
  803. /** Allocate and initialize a rend_data_t object for a service using the
  804. * given arguments. Only the <b>onion_address</b> is not optional.
  805. *
  806. * Return a valid rend_data_t pointer. */
  807. rend_data_t *
  808. rend_data_service_create(const char *onion_address, const char *pk_digest,
  809. const uint8_t *cookie, rend_auth_type_t auth_type)
  810. {
  811. rend_data_t *rend_data = tor_malloc_zero(sizeof(*rend_data));
  812. /* We need at least one else the call is wrong. */
  813. tor_assert(onion_address != NULL);
  814. if (pk_digest) {
  815. memcpy(rend_data->rend_pk_digest, pk_digest,
  816. sizeof(rend_data->rend_pk_digest));
  817. }
  818. if (cookie) {
  819. memcpy(rend_data->rend_cookie, cookie,
  820. sizeof(rend_data->rend_cookie));
  821. }
  822. strlcpy(rend_data->onion_address, onion_address,
  823. sizeof(rend_data->onion_address));
  824. rend_data->auth_type = auth_type;
  825. /* Won't be used but still need to initialize it for rend_data dup and
  826. * free. */
  827. rend_data->hsdirs_fp = smartlist_new();
  828. return rend_data;
  829. }
  830. /** Allocate and initialize a rend_data_t object for a client request using
  831. * the given arguments. Either an onion address or a descriptor ID is
  832. * needed. Both can be given but only the onion address will be used to make
  833. * the descriptor fetch.
  834. *
  835. * Return a valid rend_data_t pointer or NULL on error meaning the
  836. * descriptor IDs couldn't be computed from the given data. */
  837. rend_data_t *
  838. rend_data_client_create(const char *onion_address, const char *desc_id,
  839. const char *cookie, rend_auth_type_t auth_type)
  840. {
  841. rend_data_t *rend_data = tor_malloc_zero(sizeof(*rend_data));
  842. /* We need at least one else the call is wrong. */
  843. tor_assert(onion_address != NULL || desc_id != NULL);
  844. if (cookie) {
  845. memcpy(rend_data->descriptor_cookie, cookie,
  846. sizeof(rend_data->descriptor_cookie));
  847. }
  848. if (desc_id) {
  849. memcpy(rend_data->desc_id_fetch, desc_id,
  850. sizeof(rend_data->desc_id_fetch));
  851. }
  852. if (onion_address) {
  853. strlcpy(rend_data->onion_address, onion_address,
  854. sizeof(rend_data->onion_address));
  855. if (compute_desc_id(rend_data) < 0) {
  856. goto error;
  857. }
  858. }
  859. rend_data->auth_type = auth_type;
  860. rend_data->hsdirs_fp = smartlist_new();
  861. return rend_data;
  862. error:
  863. rend_data_free(rend_data);
  864. return NULL;
  865. }
  866. /** Determine the routers that are responsible for <b>id</b> (binary) and
  867. * add pointers to those routers' routerstatus_t to <b>responsible_dirs</b>.
  868. * Return -1 if we're returning an empty smartlist, else return 0.
  869. */
  870. int
  871. hid_serv_get_responsible_directories(smartlist_t *responsible_dirs,
  872. const char *id)
  873. {
  874. int start, found, n_added = 0, i;
  875. networkstatus_t *c = networkstatus_get_latest_consensus();
  876. if (!c || !smartlist_len(c->routerstatus_list)) {
  877. log_warn(LD_REND, "We don't have a consensus, so we can't perform v2 "
  878. "rendezvous operations.");
  879. return -1;
  880. }
  881. tor_assert(id);
  882. start = networkstatus_vote_find_entry_idx(c, id, &found);
  883. if (start == smartlist_len(c->routerstatus_list)) start = 0;
  884. i = start;
  885. do {
  886. routerstatus_t *r = smartlist_get(c->routerstatus_list, i);
  887. if (r->is_hs_dir) {
  888. smartlist_add(responsible_dirs, r);
  889. if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
  890. return 0;
  891. }
  892. if (++i == smartlist_len(c->routerstatus_list))
  893. i = 0;
  894. } while (i != start);
  895. /* Even though we don't have the desired number of hidden service
  896. * directories, be happy if we got any. */
  897. return smartlist_len(responsible_dirs) ? 0 : -1;
  898. }
  899. /* Length of the 'extended' auth cookie used to encode auth type before
  900. * base64 encoding. */
  901. #define REND_DESC_COOKIE_LEN_EXT (REND_DESC_COOKIE_LEN + 1)
  902. /* Length of the zero-padded auth cookie when base64 encoded. These two
  903. * padding bytes always (A=) are stripped off of the returned cookie. */
  904. #define REND_DESC_COOKIE_LEN_EXT_BASE64 (REND_DESC_COOKIE_LEN_BASE64 + 2)
  905. /** Encode a client authorization descriptor cookie.
  906. * The result of this function is suitable for use in the HidServAuth
  907. * option. The trailing padding characters are removed, and the
  908. * auth type is encoded into the cookie.
  909. *
  910. * Returns a new base64-encoded cookie. This function cannot fail.
  911. * The caller is responsible for freeing the returned value.
  912. */
  913. char *
  914. rend_auth_encode_cookie(const uint8_t *cookie_in, rend_auth_type_t auth_type)
  915. {
  916. uint8_t extended_cookie[REND_DESC_COOKIE_LEN_EXT];
  917. char *cookie_out = tor_malloc_zero(REND_DESC_COOKIE_LEN_EXT_BASE64 + 1);
  918. int re;
  919. tor_assert(cookie_in);
  920. memcpy(extended_cookie, cookie_in, REND_DESC_COOKIE_LEN);
  921. extended_cookie[REND_DESC_COOKIE_LEN] = ((int)auth_type - 1) << 4;
  922. re = base64_encode(cookie_out, REND_DESC_COOKIE_LEN_EXT_BASE64 + 1,
  923. (const char *) extended_cookie, REND_DESC_COOKIE_LEN_EXT,
  924. 0);
  925. tor_assert(re == REND_DESC_COOKIE_LEN_EXT_BASE64);
  926. /* Remove the trailing 'A='. Auth type is encoded in the high bits
  927. * of the last byte, so the last base64 character will always be zero
  928. * (A). This is subtly different behavior from base64_encode_nopad. */
  929. cookie_out[REND_DESC_COOKIE_LEN_BASE64] = '\0';
  930. memwipe(extended_cookie, 0, sizeof(extended_cookie));
  931. return cookie_out;
  932. }
  933. /** Decode a base64-encoded client authorization descriptor cookie.
  934. * The descriptor_cookie can be truncated to REND_DESC_COOKIE_LEN_BASE64
  935. * characters (as given to clients), or may include the two padding
  936. * characters (as stored by the service).
  937. *
  938. * The result is stored in REND_DESC_COOKIE_LEN bytes of cookie_out.
  939. * The rend_auth_type_t decoded from the cookie is stored in the
  940. * optional auth_type_out parameter.
  941. *
  942. * Return 0 on success, or -1 on error. The caller is responsible for
  943. * freeing the returned err_msg.
  944. */
  945. int
  946. rend_auth_decode_cookie(const char *cookie_in, uint8_t *cookie_out,
  947. rend_auth_type_t *auth_type_out, char **err_msg_out)
  948. {
  949. uint8_t descriptor_cookie_decoded[REND_DESC_COOKIE_LEN_EXT + 1] = { 0 };
  950. char descriptor_cookie_base64ext[REND_DESC_COOKIE_LEN_EXT_BASE64 + 1];
  951. const char *descriptor_cookie = cookie_in;
  952. char *err_msg = NULL;
  953. int auth_type_val = 0;
  954. int res = -1;
  955. int decoded_len;
  956. size_t len = strlen(descriptor_cookie);
  957. if (len == REND_DESC_COOKIE_LEN_BASE64) {
  958. /* Add a trailing zero byte to make base64-decoding happy. */
  959. tor_snprintf(descriptor_cookie_base64ext,
  960. sizeof(descriptor_cookie_base64ext),
  961. "%sA=", descriptor_cookie);
  962. descriptor_cookie = descriptor_cookie_base64ext;
  963. } else if (len != REND_DESC_COOKIE_LEN_EXT_BASE64) {
  964. tor_asprintf(&err_msg, "Authorization cookie has wrong length: %s",
  965. escaped(cookie_in));
  966. goto err;
  967. }
  968. decoded_len = base64_decode((char *) descriptor_cookie_decoded,
  969. sizeof(descriptor_cookie_decoded),
  970. descriptor_cookie,
  971. REND_DESC_COOKIE_LEN_EXT_BASE64);
  972. if (decoded_len != REND_DESC_COOKIE_LEN &&
  973. decoded_len != REND_DESC_COOKIE_LEN_EXT) {
  974. tor_asprintf(&err_msg, "Authorization cookie has invalid characters: %s",
  975. escaped(cookie_in));
  976. goto err;
  977. }
  978. if (auth_type_out) {
  979. auth_type_val = (descriptor_cookie_decoded[REND_DESC_COOKIE_LEN] >> 4) + 1;
  980. if (auth_type_val < 1 || auth_type_val > 2) {
  981. tor_asprintf(&err_msg, "Authorization cookie type is unknown: %s",
  982. escaped(cookie_in));
  983. goto err;
  984. }
  985. *auth_type_out = auth_type_val == 1 ? REND_BASIC_AUTH : REND_STEALTH_AUTH;
  986. }
  987. memcpy(cookie_out, descriptor_cookie_decoded, REND_DESC_COOKIE_LEN);
  988. res = 0;
  989. err:
  990. if (err_msg_out) {
  991. *err_msg_out = err_msg;
  992. } else {
  993. tor_free(err_msg);
  994. }
  995. memwipe(descriptor_cookie_decoded, 0, sizeof(descriptor_cookie_decoded));
  996. memwipe(descriptor_cookie_base64ext, 0, sizeof(descriptor_cookie_base64ext));
  997. return res;
  998. }
  999. /* Is this a rend client or server that allows direct (non-anonymous)
  1000. * connections?
  1001. * Clients must be specifically compiled and configured in this mode.
  1002. * Onion services can be configured to start in this mode.
  1003. * Prefer rend_client_allow_non_anonymous_connection() or
  1004. * rend_service_allow_non_anonymous_connection() whenever possible, so that
  1005. * checks are specific to Single Onion Services or Tor2web. */
  1006. int
  1007. rend_allow_non_anonymous_connection(const or_options_t* options)
  1008. {
  1009. return (rend_client_allow_non_anonymous_connection(options)
  1010. || rend_service_allow_non_anonymous_connection(options));
  1011. }
  1012. /* Is this a rend client or server in non-anonymous mode?
  1013. * Clients must be specifically compiled in this mode.
  1014. * Onion services can be configured to start in this mode.
  1015. * Prefer rend_client_non_anonymous_mode_enabled() or
  1016. * rend_service_non_anonymous_mode_enabled() whenever possible, so that checks
  1017. * are specific to Single Onion Services or Tor2web. */
  1018. int
  1019. rend_non_anonymous_mode_enabled(const or_options_t *options)
  1020. {
  1021. return (rend_client_non_anonymous_mode_enabled(options)
  1022. || rend_service_non_anonymous_mode_enabled(options));
  1023. }
  1024. /* Make sure that tor only builds one-hop circuits when they would not
  1025. * compromise user anonymity.
  1026. *
  1027. * One-hop circuits are permitted in Tor2web or Single Onion modes.
  1028. *
  1029. * Tor2web or Single Onion modes are also allowed to make multi-hop circuits.
  1030. * For example, single onion HSDir circuits are 3-hop to prevent denial of
  1031. * service.
  1032. */
  1033. void
  1034. assert_circ_anonymity_ok(origin_circuit_t *circ,
  1035. const or_options_t *options)
  1036. {
  1037. tor_assert(options);
  1038. tor_assert(circ);
  1039. tor_assert(circ->build_state);
  1040. if (circ->build_state->onehop_tunnel) {
  1041. tor_assert(rend_allow_non_anonymous_connection(options));
  1042. }
  1043. }