hs_common.c 24 KB

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