rendcommon.c 37 KB

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