hs_common.c 24 KB

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