hs_common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_common.c
  5. * \brief Contains code shared between different HS protocol version as well
  6. * as useful data structures and accessors used by other subsystems.
  7. * The rendcommon.c should only contains code relating to the v2
  8. * protocol.
  9. **/
  10. #define HS_COMMON_PRIVATE
  11. #include "or.h"
  12. #include "config.h"
  13. #include "networkstatus.h"
  14. #include "hs_cache.h"
  15. #include "hs_common.h"
  16. #include "hs_service.h"
  17. #include "rendcommon.h"
  18. #include "rendservice.h"
  19. #include "shared_random.h"
  20. /* Ed25519 Basepoint value. Taken from section 5 of
  21. * https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-03 */
  22. static const char *str_ed25519_basepoint =
  23. "(15112221349535400772501151409588531511"
  24. "454012693041857206046113283949847762202, "
  25. "463168356949264781694283940034751631413"
  26. "07993866256225615783033603165251855960)";
  27. /* Allocate and return a string containing the path to filename in directory.
  28. * This function will never return NULL. The caller must free this path. */
  29. char *
  30. hs_path_from_filename(const char *directory, const char *filename)
  31. {
  32. char *file_path = NULL;
  33. tor_assert(directory);
  34. tor_assert(filename);
  35. tor_asprintf(&file_path, "%s%s%s", directory, PATH_SEPARATOR, filename);
  36. return file_path;
  37. }
  38. /* Make sure that the directory for <b>service</b> is private, using the config
  39. * <b>username</b>.
  40. * If <b>create</b> is true:
  41. * - if the directory exists, change permissions if needed,
  42. * - if the directory does not exist, create it with the correct permissions.
  43. * If <b>create</b> is false:
  44. * - if the directory exists, check permissions,
  45. * - if the directory does not exist, check if we think we can create it.
  46. * Return 0 on success, -1 on failure. */
  47. int
  48. hs_check_service_private_dir(const char *username, const char *path,
  49. unsigned int dir_group_readable,
  50. unsigned int create)
  51. {
  52. cpd_check_t check_opts = CPD_NONE;
  53. tor_assert(path);
  54. if (create) {
  55. check_opts |= CPD_CREATE;
  56. } else {
  57. check_opts |= CPD_CHECK_MODE_ONLY;
  58. check_opts |= CPD_CHECK;
  59. }
  60. if (dir_group_readable) {
  61. check_opts |= CPD_GROUP_READ;
  62. }
  63. /* Check/create directory */
  64. if (check_private_dir(path, check_opts, username) < 0) {
  65. return -1;
  66. }
  67. return 0;
  68. }
  69. /** Get the default HS time period length in minutes from the consensus. */
  70. STATIC uint64_t
  71. get_time_period_length(void)
  72. {
  73. int32_t time_period_length = networkstatus_get_param(NULL, "hsdir-interval",
  74. HS_TIME_PERIOD_LENGTH_DEFAULT,
  75. HS_TIME_PERIOD_LENGTH_MIN,
  76. HS_TIME_PERIOD_LENGTH_MAX);
  77. /* Make sure it's a positive value. */
  78. tor_assert(time_period_length >= 0);
  79. /* uint64_t will always be able to contain a int32_t */
  80. return (uint64_t) time_period_length;
  81. }
  82. /** Get the HS time period number at time <b>now</b> */
  83. uint64_t
  84. hs_get_time_period_num(time_t now)
  85. {
  86. uint64_t time_period_num;
  87. uint64_t time_period_length = get_time_period_length();
  88. uint64_t minutes_since_epoch = now / 60;
  89. /* Now subtract half a day to fit the prop224 time period schedule (see
  90. * section [TIME-PERIODS]). */
  91. tor_assert(minutes_since_epoch > HS_TIME_PERIOD_ROTATION_OFFSET);
  92. minutes_since_epoch -= HS_TIME_PERIOD_ROTATION_OFFSET;
  93. /* Calculate the time period */
  94. time_period_num = minutes_since_epoch / time_period_length;
  95. return time_period_num;
  96. }
  97. /** Get the number of the _upcoming_ HS time period, given that the current
  98. * time is <b>now</b>. */
  99. uint64_t
  100. hs_get_next_time_period_num(time_t now)
  101. {
  102. return hs_get_time_period_num(now) + 1;
  103. }
  104. /* Create a new rend_data_t for a specific given <b>version</b>.
  105. * Return a pointer to the newly allocated data structure. */
  106. static rend_data_t *
  107. rend_data_alloc(uint32_t version)
  108. {
  109. rend_data_t *rend_data = NULL;
  110. switch (version) {
  111. case HS_VERSION_TWO:
  112. {
  113. rend_data_v2_t *v2 = tor_malloc_zero(sizeof(*v2));
  114. v2->base_.version = HS_VERSION_TWO;
  115. v2->base_.hsdirs_fp = smartlist_new();
  116. rend_data = &v2->base_;
  117. break;
  118. }
  119. default:
  120. tor_assert(0);
  121. break;
  122. }
  123. return rend_data;
  124. }
  125. /** Free all storage associated with <b>data</b> */
  126. void
  127. rend_data_free(rend_data_t *data)
  128. {
  129. if (!data) {
  130. return;
  131. }
  132. /* By using our allocation function, this should always be set. */
  133. tor_assert(data->hsdirs_fp);
  134. /* Cleanup the HSDir identity digest. */
  135. SMARTLIST_FOREACH(data->hsdirs_fp, char *, d, tor_free(d));
  136. smartlist_free(data->hsdirs_fp);
  137. /* Depending on the version, cleanup. */
  138. switch (data->version) {
  139. case HS_VERSION_TWO:
  140. {
  141. rend_data_v2_t *v2_data = TO_REND_DATA_V2(data);
  142. tor_free(v2_data);
  143. break;
  144. }
  145. default:
  146. tor_assert(0);
  147. }
  148. }
  149. /* Allocate and return a deep copy of <b>data</b>. */
  150. rend_data_t *
  151. rend_data_dup(const rend_data_t *data)
  152. {
  153. rend_data_t *data_dup = NULL;
  154. smartlist_t *hsdirs_fp = smartlist_new();
  155. tor_assert(data);
  156. tor_assert(data->hsdirs_fp);
  157. SMARTLIST_FOREACH(data->hsdirs_fp, char *, fp,
  158. smartlist_add(hsdirs_fp, tor_memdup(fp, DIGEST_LEN)));
  159. switch (data->version) {
  160. case HS_VERSION_TWO:
  161. {
  162. rend_data_v2_t *v2_data = tor_memdup(TO_REND_DATA_V2(data),
  163. sizeof(*v2_data));
  164. data_dup = &v2_data->base_;
  165. data_dup->hsdirs_fp = hsdirs_fp;
  166. break;
  167. }
  168. default:
  169. tor_assert(0);
  170. break;
  171. }
  172. return data_dup;
  173. }
  174. /* Compute the descriptor ID for each HS descriptor replica and save them. A
  175. * valid onion address must be present in the <b>rend_data</b>.
  176. *
  177. * Return 0 on success else -1. */
  178. static int
  179. compute_desc_id(rend_data_t *rend_data)
  180. {
  181. int ret = 0;
  182. unsigned replica;
  183. time_t now = time(NULL);
  184. tor_assert(rend_data);
  185. switch (rend_data->version) {
  186. case HS_VERSION_TWO:
  187. {
  188. rend_data_v2_t *v2_data = TO_REND_DATA_V2(rend_data);
  189. /* Compute descriptor ID for each replicas. */
  190. for (replica = 0; replica < ARRAY_LENGTH(v2_data->descriptor_id);
  191. replica++) {
  192. ret = rend_compute_v2_desc_id(v2_data->descriptor_id[replica],
  193. v2_data->onion_address,
  194. v2_data->descriptor_cookie,
  195. now, replica);
  196. if (ret < 0) {
  197. goto end;
  198. }
  199. }
  200. break;
  201. }
  202. default:
  203. tor_assert(0);
  204. }
  205. end:
  206. return ret;
  207. }
  208. /* Allocate and initialize a rend_data_t object for a service using the
  209. * provided arguments. All arguments are optional (can be NULL), except from
  210. * <b>onion_address</b> which MUST be set. The <b>pk_digest</b> is the hash of
  211. * the service private key. The <b>cookie</b> is the rendezvous cookie and
  212. * <b>auth_type</b> is which authentiation this service is configured with.
  213. *
  214. * Return a valid rend_data_t pointer. This only returns a version 2 object of
  215. * rend_data_t. */
  216. rend_data_t *
  217. rend_data_service_create(const char *onion_address, const char *pk_digest,
  218. const uint8_t *cookie, rend_auth_type_t auth_type)
  219. {
  220. /* Create a rend_data_t object for version 2. */
  221. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  222. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  223. /* We need at least one else the call is wrong. */
  224. tor_assert(onion_address != NULL);
  225. if (pk_digest) {
  226. memcpy(v2->rend_pk_digest, pk_digest, sizeof(v2->rend_pk_digest));
  227. }
  228. if (cookie) {
  229. memcpy(rend_data->rend_cookie, cookie, sizeof(rend_data->rend_cookie));
  230. }
  231. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  232. v2->auth_type = auth_type;
  233. return rend_data;
  234. }
  235. /* Allocate and initialize a rend_data_t object for a client request using the
  236. * given arguments. Either an onion address or a descriptor ID is needed. Both
  237. * can be given but in this case only the onion address will be used to make
  238. * the descriptor fetch. The <b>cookie</b> is the rendezvous cookie and
  239. * <b>auth_type</b> is which authentiation the service is configured with.
  240. *
  241. * Return a valid rend_data_t pointer or NULL on error meaning the
  242. * descriptor IDs couldn't be computed from the given data. */
  243. rend_data_t *
  244. rend_data_client_create(const char *onion_address, const char *desc_id,
  245. const char *cookie, rend_auth_type_t auth_type)
  246. {
  247. /* Create a rend_data_t object for version 2. */
  248. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  249. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  250. /* We need at least one else the call is wrong. */
  251. tor_assert(onion_address != NULL || desc_id != NULL);
  252. if (cookie) {
  253. memcpy(v2->descriptor_cookie, cookie, sizeof(v2->descriptor_cookie));
  254. }
  255. if (desc_id) {
  256. memcpy(v2->desc_id_fetch, desc_id, sizeof(v2->desc_id_fetch));
  257. }
  258. if (onion_address) {
  259. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  260. if (compute_desc_id(rend_data) < 0) {
  261. goto error;
  262. }
  263. }
  264. v2->auth_type = auth_type;
  265. return rend_data;
  266. error:
  267. rend_data_free(rend_data);
  268. return NULL;
  269. }
  270. /* Return the onion address from the rend data. Depending on the version,
  271. * the size of the address can vary but it's always NUL terminated. */
  272. const char *
  273. rend_data_get_address(const rend_data_t *rend_data)
  274. {
  275. tor_assert(rend_data);
  276. switch (rend_data->version) {
  277. case HS_VERSION_TWO:
  278. return TO_REND_DATA_V2(rend_data)->onion_address;
  279. default:
  280. /* We should always have a supported version. */
  281. tor_assert(0);
  282. }
  283. }
  284. /* Return the descriptor ID for a specific replica number from the rend
  285. * data. The returned data is a binary digest and depending on the version its
  286. * size can vary. The size of the descriptor ID is put in <b>len_out</b> if
  287. * non NULL. */
  288. const char *
  289. rend_data_get_desc_id(const rend_data_t *rend_data, uint8_t replica,
  290. size_t *len_out)
  291. {
  292. tor_assert(rend_data);
  293. switch (rend_data->version) {
  294. case HS_VERSION_TWO:
  295. tor_assert(replica < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS);
  296. if (len_out) {
  297. *len_out = DIGEST_LEN;
  298. }
  299. return TO_REND_DATA_V2(rend_data)->descriptor_id[replica];
  300. default:
  301. /* We should always have a supported version. */
  302. tor_assert(0);
  303. }
  304. }
  305. /* Return the public key digest using the given <b>rend_data</b>. The size of
  306. * the digest is put in <b>len_out</b> (if set) which can differ depending on
  307. * the version. */
  308. const uint8_t *
  309. rend_data_get_pk_digest(const rend_data_t *rend_data, size_t *len_out)
  310. {
  311. tor_assert(rend_data);
  312. switch (rend_data->version) {
  313. case HS_VERSION_TWO:
  314. {
  315. const rend_data_v2_t *v2_data = TO_REND_DATA_V2(rend_data);
  316. if (len_out) {
  317. *len_out = sizeof(v2_data->rend_pk_digest);
  318. }
  319. return (const uint8_t *) v2_data->rend_pk_digest;
  320. }
  321. default:
  322. /* We should always have a supported version. */
  323. tor_assert(0);
  324. }
  325. }
  326. /* Using the given time period number, compute the disaster shared random
  327. * value and put it in srv_out. It MUST be at least DIGEST256_LEN bytes. */
  328. static void
  329. get_disaster_srv(uint64_t time_period_num, uint8_t *srv_out)
  330. {
  331. crypto_digest_t *digest;
  332. tor_assert(srv_out);
  333. digest = crypto_digest256_new(DIGEST_SHA3_256);
  334. /* Setup payload: H("shared-random-disaster" | INT_8(period_num)) */
  335. crypto_digest_add_bytes(digest, HS_SRV_DISASTER_PREFIX,
  336. HS_SRV_DISASTER_PREFIX_LEN);
  337. crypto_digest_add_bytes(digest, (const char *) &time_period_num,
  338. sizeof(time_period_num));
  339. crypto_digest_get_digest(digest, (char *) srv_out, DIGEST256_LEN);
  340. crypto_digest_free(digest);
  341. }
  342. /* When creating a blinded key, we need a parameter which construction is as
  343. * follow: H(pubkey | [secret] | ed25519-basepoint | nonce).
  344. *
  345. * The nonce has a pre-defined format which uses the time period number
  346. * period_num and the start of the period in second start_time_period.
  347. *
  348. * The secret of size secret_len is optional meaning that it can be NULL and
  349. * thus will be ignored for the param construction.
  350. *
  351. * The result is put in param_out. */
  352. static void
  353. build_blinded_key_param(const ed25519_public_key_t *pubkey,
  354. const uint8_t *secret, size_t secret_len,
  355. uint64_t period_num, uint64_t start_time_period,
  356. uint8_t *param_out)
  357. {
  358. size_t offset = 0;
  359. uint8_t nonce[HS_KEYBLIND_NONCE_LEN];
  360. crypto_digest_t *digest;
  361. tor_assert(pubkey);
  362. tor_assert(param_out);
  363. /* Create the nonce N. The construction is as follow:
  364. * N = "key-blind" || INT_8(period_num) || INT_8(start_period_sec) */
  365. memcpy(nonce, HS_KEYBLIND_NONCE_PREFIX, HS_KEYBLIND_NONCE_PREFIX_LEN);
  366. offset += HS_KEYBLIND_NONCE_PREFIX_LEN;
  367. set_uint64(nonce + offset, period_num);
  368. offset += sizeof(uint64_t);
  369. set_uint64(nonce + offset, start_time_period);
  370. offset += sizeof(uint64_t);
  371. tor_assert(offset == HS_KEYBLIND_NONCE_LEN);
  372. /* Generate the parameter h and the construction is as follow:
  373. * h = H(pubkey | [secret] | ed25519-basepoint | nonce) */
  374. digest = crypto_digest256_new(DIGEST_SHA3_256);
  375. crypto_digest_add_bytes(digest, (char *) pubkey, ED25519_PUBKEY_LEN);
  376. /* Optional secret. */
  377. if (secret) {
  378. crypto_digest_add_bytes(digest, (char *) secret, secret_len);
  379. }
  380. crypto_digest_add_bytes(digest, str_ed25519_basepoint,
  381. strlen(str_ed25519_basepoint));
  382. crypto_digest_add_bytes(digest, (char *) nonce, sizeof(nonce));
  383. /* Extract digest and put it in the param. */
  384. crypto_digest_get_digest(digest, (char *) param_out, DIGEST256_LEN);
  385. crypto_digest_free(digest);
  386. }
  387. /* Using an ed25519 public key and version to build the checksum of an
  388. * address. Put in checksum_out. Format is:
  389. * SHA3-256(".onion checksum" || PUBKEY || VERSION)
  390. *
  391. * checksum_out must be large enough to receive 32 bytes (DIGEST256_LEN). */
  392. static void
  393. build_hs_checksum(const ed25519_public_key_t *key, uint8_t version,
  394. uint8_t *checksum_out)
  395. {
  396. size_t offset = 0;
  397. char data[HS_SERVICE_ADDR_CHECKSUM_INPUT_LEN];
  398. /* Build checksum data. */
  399. memcpy(data, HS_SERVICE_ADDR_CHECKSUM_PREFIX,
  400. HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN);
  401. offset += HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN;
  402. memcpy(data + offset, key->pubkey, ED25519_PUBKEY_LEN);
  403. offset += ED25519_PUBKEY_LEN;
  404. set_uint8(data + offset, version);
  405. offset += sizeof(version);
  406. tor_assert(offset == HS_SERVICE_ADDR_CHECKSUM_INPUT_LEN);
  407. /* Hash the data payload to create the checksum. */
  408. crypto_digest256((char *) checksum_out, data, sizeof(data),
  409. DIGEST_SHA3_256);
  410. }
  411. /* Using an ed25519 public key, checksum and version to build the binary
  412. * representation of a service address. Put in addr_out. Format is:
  413. * addr_out = PUBKEY || CHECKSUM || VERSION
  414. *
  415. * addr_out must be large enough to receive HS_SERVICE_ADDR_LEN bytes. */
  416. static void
  417. build_hs_address(const ed25519_public_key_t *key, const uint8_t *checksum,
  418. uint8_t version, char *addr_out)
  419. {
  420. size_t offset = 0;
  421. tor_assert(key);
  422. tor_assert(checksum);
  423. memcpy(addr_out, key->pubkey, ED25519_PUBKEY_LEN);
  424. offset += ED25519_PUBKEY_LEN;
  425. memcpy(addr_out + offset, checksum, HS_SERVICE_ADDR_CHECKSUM_LEN_USED);
  426. offset += HS_SERVICE_ADDR_CHECKSUM_LEN_USED;
  427. set_uint8(addr_out + offset, version);
  428. offset += sizeof(uint8_t);
  429. tor_assert(offset == HS_SERVICE_ADDR_LEN);
  430. }
  431. /* Helper for hs_parse_address(): Using a binary representation of a service
  432. * address, parse its content into the key_out, checksum_out and version_out.
  433. * Any out variable can be NULL in case the caller would want only one field.
  434. * checksum_out MUST at least be 2 bytes long. address must be at least
  435. * HS_SERVICE_ADDR_LEN bytes but doesn't need to be NUL terminated. */
  436. static void
  437. hs_parse_address_impl(const char *address, ed25519_public_key_t *key_out,
  438. uint8_t *checksum_out, uint8_t *version_out)
  439. {
  440. size_t offset = 0;
  441. tor_assert(address);
  442. if (key_out) {
  443. /* First is the key. */
  444. memcpy(key_out->pubkey, address, ED25519_PUBKEY_LEN);
  445. }
  446. offset += ED25519_PUBKEY_LEN;
  447. if (checksum_out) {
  448. /* Followed by a 2 bytes checksum. */
  449. memcpy(checksum_out, address + offset, HS_SERVICE_ADDR_CHECKSUM_LEN_USED);
  450. }
  451. offset += HS_SERVICE_ADDR_CHECKSUM_LEN_USED;
  452. if (version_out) {
  453. /* Finally, version value is 1 byte. */
  454. *version_out = get_uint8(address + offset);
  455. }
  456. offset += sizeof(uint8_t);
  457. /* Extra safety. */
  458. tor_assert(offset == HS_SERVICE_ADDR_LEN);
  459. }
  460. /* Using the given identity public key and a blinded public key, compute the
  461. * subcredential and put it in subcred_out. This can't fail. */
  462. void
  463. hs_get_subcredential(const ed25519_public_key_t *identity_pk,
  464. const ed25519_public_key_t *blinded_pk,
  465. uint8_t *subcred_out)
  466. {
  467. uint8_t credential[DIGEST256_LEN];
  468. crypto_digest_t *digest;
  469. tor_assert(identity_pk);
  470. tor_assert(blinded_pk);
  471. tor_assert(subcred_out);
  472. /* First, build the credential. Construction is as follow:
  473. * credential = H("credential" | public-identity-key) */
  474. digest = crypto_digest256_new(DIGEST_SHA3_256);
  475. crypto_digest_add_bytes(digest, HS_CREDENTIAL_PREFIX,
  476. HS_CREDENTIAL_PREFIX_LEN);
  477. crypto_digest_add_bytes(digest, (const char *) identity_pk->pubkey,
  478. ED25519_PUBKEY_LEN);
  479. crypto_digest_get_digest(digest, (char *) credential, DIGEST256_LEN);
  480. crypto_digest_free(digest);
  481. /* Now, compute the subcredential. Construction is as follow:
  482. * subcredential = H("subcredential" | credential | blinded-public-key). */
  483. digest = crypto_digest256_new(DIGEST_SHA3_256);
  484. crypto_digest_add_bytes(digest, HS_SUBCREDENTIAL_PREFIX,
  485. HS_SUBCREDENTIAL_PREFIX_LEN);
  486. crypto_digest_add_bytes(digest, (const char *) credential,
  487. sizeof(credential));
  488. crypto_digest_add_bytes(digest, (const char *) blinded_pk->pubkey,
  489. ED25519_PUBKEY_LEN);
  490. crypto_digest_get_digest(digest, (char *) subcred_out, DIGEST256_LEN);
  491. crypto_digest_free(digest);
  492. }
  493. /* Using a base32 representation of a service address, parse its content into
  494. * the key_out, checksum_out and version_out. Any out variable can be NULL in
  495. * case the caller would want only one field. checksum_out MUST at least be 2
  496. * bytes long.
  497. *
  498. * Return 0 if parsing went well; return -1 in case of error. */
  499. int
  500. hs_parse_address(const char *address, ed25519_public_key_t *key_out,
  501. uint8_t *checksum_out, uint8_t *version_out)
  502. {
  503. char decoded[HS_SERVICE_ADDR_LEN];
  504. tor_assert(address);
  505. /* Obvious length check. */
  506. if (strlen(address) != HS_SERVICE_ADDR_LEN_BASE32) {
  507. log_warn(LD_REND, "Service address %s has an invalid length. "
  508. "Expected %lu but got %lu.",
  509. escaped_safe_str(address),
  510. (unsigned long) HS_SERVICE_ADDR_LEN_BASE32,
  511. (unsigned long) strlen(address));
  512. goto invalid;
  513. }
  514. /* Decode address so we can extract needed fields. */
  515. if (base32_decode(decoded, sizeof(decoded), address, strlen(address)) < 0) {
  516. log_warn(LD_REND, "Service address %s can't be decoded.",
  517. escaped_safe_str(address));
  518. goto invalid;
  519. }
  520. /* Parse the decoded address into the fields we need. */
  521. hs_parse_address_impl(decoded, key_out, checksum_out, version_out);
  522. return 0;
  523. invalid:
  524. return -1;
  525. }
  526. /* Validate a given onion address. The length, the base32 decoding and
  527. * checksum are validated. Return 1 if valid else 0. */
  528. int
  529. hs_address_is_valid(const char *address)
  530. {
  531. uint8_t version;
  532. uint8_t checksum[HS_SERVICE_ADDR_CHECKSUM_LEN_USED];
  533. uint8_t target_checksum[DIGEST256_LEN];
  534. ed25519_public_key_t key;
  535. /* Parse the decoded address into the fields we need. */
  536. if (hs_parse_address(address, &key, checksum, &version) < 0) {
  537. goto invalid;
  538. }
  539. /* Get the checksum it's suppose to be and compare it with what we have
  540. * encoded in the address. */
  541. build_hs_checksum(&key, version, target_checksum);
  542. if (tor_memcmp(checksum, target_checksum, sizeof(checksum))) {
  543. log_warn(LD_REND, "Service address %s invalid checksum.",
  544. escaped_safe_str(address));
  545. goto invalid;
  546. }
  547. /* Valid address. */
  548. return 1;
  549. invalid:
  550. return 0;
  551. }
  552. /* Build a service address using an ed25519 public key and a given version.
  553. * The returned address is base32 encoded and put in addr_out. The caller MUST
  554. * make sure the addr_out is at least HS_SERVICE_ADDR_LEN_BASE32 + 1 long.
  555. *
  556. * Format is as follow:
  557. * base32(PUBKEY || CHECKSUM || VERSION)
  558. * CHECKSUM = H(".onion checksum" || PUBKEY || VERSION)
  559. * */
  560. void
  561. hs_build_address(const ed25519_public_key_t *key, uint8_t version,
  562. char *addr_out)
  563. {
  564. uint8_t checksum[DIGEST256_LEN];
  565. char address[HS_SERVICE_ADDR_LEN];
  566. tor_assert(key);
  567. tor_assert(addr_out);
  568. /* Get the checksum of the address. */
  569. build_hs_checksum(key, version, checksum);
  570. /* Get the binary address representation. */
  571. build_hs_address(key, checksum, version, address);
  572. /* Encode the address. addr_out will be NUL terminated after this. */
  573. base32_encode(addr_out, HS_SERVICE_ADDR_LEN_BASE32 + 1, address,
  574. sizeof(address));
  575. /* Validate what we just built. */
  576. tor_assert(hs_address_is_valid(addr_out));
  577. }
  578. /* Return a newly allocated copy of lspec. */
  579. link_specifier_t *
  580. hs_link_specifier_dup(const link_specifier_t *lspec)
  581. {
  582. link_specifier_t *dup = link_specifier_new();
  583. memcpy(dup, lspec, sizeof(*dup));
  584. /* The unrecognized field is a dynamic array so make sure to copy its
  585. * content and not the pointer. */
  586. link_specifier_setlen_un_unrecognized(
  587. dup, link_specifier_getlen_un_unrecognized(lspec));
  588. if (link_specifier_getlen_un_unrecognized(dup)) {
  589. memcpy(link_specifier_getarray_un_unrecognized(dup),
  590. link_specifier_getconstarray_un_unrecognized(lspec),
  591. link_specifier_getlen_un_unrecognized(dup));
  592. }
  593. return dup;
  594. }
  595. /* From a given ed25519 public key pk and an optional secret, compute a
  596. * blinded public key and put it in blinded_pk_out. This is only useful to
  597. * the client side because the client only has access to the identity public
  598. * key of the service. */
  599. void
  600. hs_build_blinded_pubkey(const ed25519_public_key_t *pk,
  601. const uint8_t *secret, size_t secret_len,
  602. uint64_t time_period_num,
  603. ed25519_public_key_t *blinded_pk_out)
  604. {
  605. /* Our blinding key API requires a 32 bytes parameter. */
  606. uint8_t param[DIGEST256_LEN];
  607. tor_assert(pk);
  608. tor_assert(blinded_pk_out);
  609. tor_assert(!tor_mem_is_zero((char *) pk, ED25519_PUBKEY_LEN));
  610. build_blinded_key_param(pk, secret, secret_len,
  611. time_period_num, get_time_period_length(), param);
  612. ed25519_public_blind(blinded_pk_out, pk, param);
  613. }
  614. /* From a given ed25519 keypair kp and an optional secret, compute a blinded
  615. * keypair for the current time period and put it in blinded_kp_out. This is
  616. * only useful by the service side because the client doesn't have access to
  617. * the identity secret key. */
  618. void
  619. hs_build_blinded_keypair(const ed25519_keypair_t *kp,
  620. const uint8_t *secret, size_t secret_len,
  621. uint64_t time_period_num,
  622. ed25519_keypair_t *blinded_kp_out)
  623. {
  624. /* Our blinding key API requires a 32 bytes parameter. */
  625. uint8_t param[DIGEST256_LEN];
  626. tor_assert(kp);
  627. tor_assert(blinded_kp_out);
  628. /* Extra safety. A zeroed key is bad. */
  629. tor_assert(!tor_mem_is_zero((char *) &kp->pubkey, ED25519_PUBKEY_LEN));
  630. tor_assert(!tor_mem_is_zero((char *) &kp->seckey, ED25519_SECKEY_LEN));
  631. build_blinded_key_param(&kp->pubkey, secret, secret_len,
  632. time_period_num, get_time_period_length(), param);
  633. ed25519_keypair_blind(blinded_kp_out, kp, param);
  634. }
  635. /* Return true if overlap mode is active given the date in consensus. If
  636. * consensus is NULL, then we use the latest live consensus we can find. */
  637. int
  638. hs_overlap_mode_is_active(const networkstatus_t *consensus, time_t now)
  639. {
  640. struct tm valid_after_tm;
  641. if (!consensus) {
  642. consensus = networkstatus_get_live_consensus(now);
  643. if (!consensus) {
  644. return 0;
  645. }
  646. }
  647. /* XXX: Futur commits will change this to a slot system so it can be
  648. * fine tuned better for testing networks in terms of timings. */
  649. /* From the spec: "Specifically, when a hidden service fetches a consensus
  650. * with "valid-after" between 00:00UTC and 12:00UTC, it goes into
  651. * "descriptor overlap" mode." */
  652. tor_gmtime_r(&consensus->valid_after, &valid_after_tm);
  653. if (valid_after_tm.tm_hour > 0 && valid_after_tm.tm_hour < 12) {
  654. return 1;
  655. }
  656. return 0;
  657. }
  658. /* Return 1 if any virtual port in ports needs a circuit with good uptime.
  659. * Else return 0. */
  660. int
  661. hs_service_requires_uptime_circ(const smartlist_t *ports)
  662. {
  663. tor_assert(ports);
  664. SMARTLIST_FOREACH_BEGIN(ports, rend_service_port_config_t *, p) {
  665. if (smartlist_contains_int_as_string(get_options()->LongLivedPorts,
  666. p->virtual_port)) {
  667. return 1;
  668. }
  669. } SMARTLIST_FOREACH_END(p);
  670. return 0;
  671. }
  672. /* Build hs_index which is used to find the responsible hsdirs. This index
  673. * value is used to select the responsible HSDir where their hsdir_index is
  674. * closest to this value.
  675. * SHA3-256("store-at-idx" | blinded_public_key |
  676. * INT_8(replicanum) | INT_8(period_num) )
  677. *
  678. * hs_index_out must be large enough to receive DIGEST256_LEN bytes. */
  679. void
  680. hs_build_hs_index(uint64_t replica, const ed25519_public_key_t *blinded_pk,
  681. uint64_t period_num, uint8_t *hs_index_out)
  682. {
  683. crypto_digest_t *digest;
  684. tor_assert(blinded_pk);
  685. tor_assert(hs_index_out);
  686. /* Build hs_index. See construction at top of function comment. */
  687. digest = crypto_digest256_new(DIGEST_SHA3_256);
  688. crypto_digest_add_bytes(digest, HS_INDEX_PREFIX, HS_INDEX_PREFIX_LEN);
  689. crypto_digest_add_bytes(digest, (const char *) blinded_pk->pubkey,
  690. ED25519_PUBKEY_LEN);
  691. crypto_digest_add_bytes(digest, (const char *) &replica, sizeof(replica));
  692. crypto_digest_add_bytes(digest, (const char *) &period_num,
  693. sizeof(period_num));
  694. crypto_digest_get_digest(digest, (char *) hs_index_out, DIGEST256_LEN);
  695. crypto_digest_free(digest);
  696. }
  697. /* Build hsdir_index which is used to find the responsible hsdirs. This is the
  698. * index value that is compare to the hs_index when selecting an HSDir.
  699. * SHA3-256("node-idx" | node_identity |
  700. * shared_random_value | INT_8(period_num) )
  701. *
  702. * hsdir_index_out must be large enough to receive DIGEST256_LEN bytes. */
  703. void
  704. hs_build_hsdir_index(const ed25519_public_key_t *identity_pk,
  705. const uint8_t *srv_value, uint64_t period_num,
  706. uint8_t *hsdir_index_out)
  707. {
  708. crypto_digest_t *digest;
  709. tor_assert(identity_pk);
  710. tor_assert(srv_value);
  711. tor_assert(hsdir_index_out);
  712. /* Build hsdir_index. See construction at top of function comment. */
  713. digest = crypto_digest256_new(DIGEST_SHA3_256);
  714. crypto_digest_add_bytes(digest, HSDIR_INDEX_PREFIX, HSDIR_INDEX_PREFIX_LEN);
  715. crypto_digest_add_bytes(digest, (const char *) identity_pk->pubkey,
  716. ED25519_PUBKEY_LEN);
  717. crypto_digest_add_bytes(digest, (const char *) srv_value, DIGEST256_LEN);
  718. crypto_digest_add_bytes(digest, (const char *) &period_num,
  719. sizeof(period_num));
  720. crypto_digest_get_digest(digest, (char *) hsdir_index_out, DIGEST256_LEN);
  721. crypto_digest_free(digest);
  722. }
  723. /* Return a newly allocated buffer containing the current shared random value
  724. * or if not present, a disaster value is computed using the given time period
  725. * number. This function can't fail. */
  726. uint8_t *
  727. hs_get_current_srv(uint64_t time_period_num)
  728. {
  729. uint8_t *sr_value = tor_malloc_zero(DIGEST256_LEN);
  730. const sr_srv_t *current_srv = sr_get_current();
  731. if (current_srv) {
  732. memcpy(sr_value, current_srv->value, sizeof(current_srv->value));
  733. } else {
  734. /* Disaster mode. */
  735. get_disaster_srv(time_period_num, sr_value);
  736. }
  737. return sr_value;
  738. }
  739. /* Return a newly allocated buffer containing the previous shared random
  740. * value or if not present, a disaster value is computed using the given time
  741. * period number. This function can't fail. */
  742. uint8_t *
  743. hs_get_previous_srv(uint64_t time_period_num)
  744. {
  745. uint8_t *sr_value = tor_malloc_zero(DIGEST256_LEN);
  746. const sr_srv_t *previous_srv = sr_get_previous();
  747. if (previous_srv) {
  748. memcpy(sr_value, previous_srv->value, sizeof(previous_srv->value));
  749. } else {
  750. /* Disaster mode. */
  751. get_disaster_srv(time_period_num, sr_value);
  752. }
  753. return sr_value;
  754. }
  755. /* Initialize the entire HS subsytem. This is called in tor_init() before any
  756. * torrc options are loaded. Only for >= v3. */
  757. void
  758. hs_init(void)
  759. {
  760. hs_circuitmap_init();
  761. hs_service_init();
  762. hs_cache_init();
  763. }
  764. /* Release and cleanup all memory of the HS subsystem (all version). This is
  765. * called by tor_free_all(). */
  766. void
  767. hs_free_all(void)
  768. {
  769. hs_circuitmap_free_all();
  770. hs_service_free_all();
  771. hs_cache_free_all();
  772. }