hs_cell.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /* Copyright (c) 2017-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_cell.c
  5. * \brief Hidden service API for cell creation and handling.
  6. **/
  7. #include "core/or/or.h"
  8. #include "app/config/config.h"
  9. #include "lib/crypt_ops/crypto_util.h"
  10. #include "feature/rend/rendservice.h"
  11. #include "feature/hs_common/replaycache.h"
  12. #include "feature/hs/hs_cell.h"
  13. #include "core/crypto/hs_ntor.h"
  14. #include "core/or/origin_circuit_st.h"
  15. /* Trunnel. */
  16. #include "trunnel/ed25519_cert.h"
  17. #include "trunnel/hs/cell_common.h"
  18. #include "trunnel/hs/cell_establish_intro.h"
  19. #include "trunnel/hs/cell_introduce1.h"
  20. #include "trunnel/hs/cell_rendezvous.h"
  21. /* Compute the MAC of an INTRODUCE cell in mac_out. The encoded_cell param is
  22. * the cell content up to the ENCRYPTED section of length encoded_cell_len.
  23. * The encrypted param is the start of the ENCRYPTED section of length
  24. * encrypted_len. The mac_key is the key needed for the computation of the MAC
  25. * derived from the ntor handshake of length mac_key_len.
  26. *
  27. * The length mac_out_len must be at least DIGEST256_LEN. */
  28. static void
  29. compute_introduce_mac(const uint8_t *encoded_cell, size_t encoded_cell_len,
  30. const uint8_t *encrypted, size_t encrypted_len,
  31. const uint8_t *mac_key, size_t mac_key_len,
  32. uint8_t *mac_out, size_t mac_out_len)
  33. {
  34. size_t offset = 0;
  35. size_t mac_msg_len;
  36. uint8_t mac_msg[RELAY_PAYLOAD_SIZE] = {0};
  37. tor_assert(encoded_cell);
  38. tor_assert(encrypted);
  39. tor_assert(mac_key);
  40. tor_assert(mac_out);
  41. tor_assert(mac_out_len >= DIGEST256_LEN);
  42. /* Compute the size of the message which is basically the entire cell until
  43. * the MAC field of course. */
  44. mac_msg_len = encoded_cell_len + (encrypted_len - DIGEST256_LEN);
  45. tor_assert(mac_msg_len <= sizeof(mac_msg));
  46. /* First, put the encoded cell in the msg. */
  47. memcpy(mac_msg, encoded_cell, encoded_cell_len);
  48. offset += encoded_cell_len;
  49. /* Second, put the CLIENT_PK + ENCRYPTED_DATA but ommit the MAC field (which
  50. * is junk at this point). */
  51. memcpy(mac_msg + offset, encrypted, (encrypted_len - DIGEST256_LEN));
  52. offset += (encrypted_len - DIGEST256_LEN);
  53. tor_assert(offset == mac_msg_len);
  54. crypto_mac_sha3_256(mac_out, mac_out_len,
  55. mac_key, mac_key_len,
  56. mac_msg, mac_msg_len);
  57. memwipe(mac_msg, 0, sizeof(mac_msg));
  58. }
  59. /* From a set of keys, subcredential and the ENCRYPTED section of an
  60. * INTRODUCE2 cell, return a newly allocated intro cell keys structure.
  61. * Finally, the client public key is copied in client_pk. On error, return
  62. * NULL. */
  63. static hs_ntor_intro_cell_keys_t *
  64. get_introduce2_key_material(const ed25519_public_key_t *auth_key,
  65. const curve25519_keypair_t *enc_key,
  66. const uint8_t *subcredential,
  67. const uint8_t *encrypted_section,
  68. curve25519_public_key_t *client_pk)
  69. {
  70. hs_ntor_intro_cell_keys_t *keys;
  71. tor_assert(auth_key);
  72. tor_assert(enc_key);
  73. tor_assert(subcredential);
  74. tor_assert(encrypted_section);
  75. tor_assert(client_pk);
  76. keys = tor_malloc_zero(sizeof(*keys));
  77. /* First bytes of the ENCRYPTED section are the client public key. */
  78. memcpy(client_pk->public_key, encrypted_section, CURVE25519_PUBKEY_LEN);
  79. if (hs_ntor_service_get_introduce1_keys(auth_key, enc_key, client_pk,
  80. subcredential, keys) < 0) {
  81. /* Don't rely on the caller to wipe this on error. */
  82. memwipe(client_pk, 0, sizeof(curve25519_public_key_t));
  83. tor_free(keys);
  84. keys = NULL;
  85. }
  86. return keys;
  87. }
  88. /* Using the given encryption key, decrypt the encrypted_section of length
  89. * encrypted_section_len of an INTRODUCE2 cell and return a newly allocated
  90. * buffer containing the decrypted data. On decryption failure, NULL is
  91. * returned. */
  92. static uint8_t *
  93. decrypt_introduce2(const uint8_t *enc_key, const uint8_t *encrypted_section,
  94. size_t encrypted_section_len)
  95. {
  96. uint8_t *decrypted = NULL;
  97. crypto_cipher_t *cipher = NULL;
  98. tor_assert(enc_key);
  99. tor_assert(encrypted_section);
  100. /* Decrypt ENCRYPTED section. */
  101. cipher = crypto_cipher_new_with_bits((char *) enc_key,
  102. CURVE25519_PUBKEY_LEN * 8);
  103. tor_assert(cipher);
  104. /* This is symmetric encryption so can't be bigger than the encrypted
  105. * section length. */
  106. decrypted = tor_malloc_zero(encrypted_section_len);
  107. if (crypto_cipher_decrypt(cipher, (char *) decrypted,
  108. (const char *) encrypted_section,
  109. encrypted_section_len) < 0) {
  110. tor_free(decrypted);
  111. decrypted = NULL;
  112. goto done;
  113. }
  114. done:
  115. crypto_cipher_free(cipher);
  116. return decrypted;
  117. }
  118. /* Given a pointer to the decrypted data of the ENCRYPTED section of an
  119. * INTRODUCE2 cell of length decrypted_len, parse and validate the cell
  120. * content. Return a newly allocated cell structure or NULL on error. The
  121. * circuit and service object are only used for logging purposes. */
  122. static trn_cell_introduce_encrypted_t *
  123. parse_introduce2_encrypted(const uint8_t *decrypted_data,
  124. size_t decrypted_len, const origin_circuit_t *circ,
  125. const hs_service_t *service)
  126. {
  127. trn_cell_introduce_encrypted_t *enc_cell = NULL;
  128. tor_assert(decrypted_data);
  129. tor_assert(circ);
  130. tor_assert(service);
  131. if (trn_cell_introduce_encrypted_parse(&enc_cell, decrypted_data,
  132. decrypted_len) < 0) {
  133. log_info(LD_REND, "Unable to parse the decrypted ENCRYPTED section of "
  134. "the INTRODUCE2 cell on circuit %u for service %s",
  135. TO_CIRCUIT(circ)->n_circ_id,
  136. safe_str_client(service->onion_address));
  137. goto err;
  138. }
  139. if (trn_cell_introduce_encrypted_get_onion_key_type(enc_cell) !=
  140. TRUNNEL_HS_INTRO_ONION_KEY_TYPE_NTOR) {
  141. log_info(LD_REND, "INTRODUCE2 onion key type is invalid. Got %u but "
  142. "expected %u on circuit %u for service %s",
  143. trn_cell_introduce_encrypted_get_onion_key_type(enc_cell),
  144. TRUNNEL_HS_INTRO_ONION_KEY_TYPE_NTOR,
  145. TO_CIRCUIT(circ)->n_circ_id,
  146. safe_str_client(service->onion_address));
  147. goto err;
  148. }
  149. if (trn_cell_introduce_encrypted_getlen_onion_key(enc_cell) !=
  150. CURVE25519_PUBKEY_LEN) {
  151. log_info(LD_REND, "INTRODUCE2 onion key length is invalid. Got %u but "
  152. "expected %d on circuit %u for service %s",
  153. (unsigned)trn_cell_introduce_encrypted_getlen_onion_key(enc_cell),
  154. CURVE25519_PUBKEY_LEN, TO_CIRCUIT(circ)->n_circ_id,
  155. safe_str_client(service->onion_address));
  156. goto err;
  157. }
  158. /* XXX: Validate NSPEC field as well. */
  159. return enc_cell;
  160. err:
  161. trn_cell_introduce_encrypted_free(enc_cell);
  162. return NULL;
  163. }
  164. /* Build a legacy ESTABLISH_INTRO cell with the given circuit nonce and RSA
  165. * encryption key. The encoded cell is put in cell_out that MUST at least be
  166. * of the size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on
  167. * success else a negative value and cell_out is untouched. */
  168. static ssize_t
  169. build_legacy_establish_intro(const char *circ_nonce, crypto_pk_t *enc_key,
  170. uint8_t *cell_out)
  171. {
  172. ssize_t cell_len;
  173. tor_assert(circ_nonce);
  174. tor_assert(enc_key);
  175. tor_assert(cell_out);
  176. memwipe(cell_out, 0, RELAY_PAYLOAD_SIZE);
  177. cell_len = rend_service_encode_establish_intro_cell((char*)cell_out,
  178. RELAY_PAYLOAD_SIZE,
  179. enc_key, circ_nonce);
  180. return cell_len;
  181. }
  182. /* Parse an INTRODUCE2 cell from payload of size payload_len for the given
  183. * service and circuit which are used only for logging purposes. The resulting
  184. * parsed cell is put in cell_ptr_out.
  185. *
  186. * This function only parses prop224 INTRODUCE2 cells even when the intro point
  187. * is a legacy intro point. That's because intro points don't actually care
  188. * about the contents of the introduce cell. Legacy INTRODUCE cells are only
  189. * used by the legacy system now.
  190. *
  191. * Return 0 on success else a negative value and cell_ptr_out is untouched. */
  192. static int
  193. parse_introduce2_cell(const hs_service_t *service,
  194. const origin_circuit_t *circ, const uint8_t *payload,
  195. size_t payload_len,
  196. trn_cell_introduce1_t **cell_ptr_out)
  197. {
  198. trn_cell_introduce1_t *cell = NULL;
  199. tor_assert(service);
  200. tor_assert(circ);
  201. tor_assert(payload);
  202. tor_assert(cell_ptr_out);
  203. /* Parse the cell so we can start cell validation. */
  204. if (trn_cell_introduce1_parse(&cell, payload, payload_len) < 0) {
  205. log_info(LD_PROTOCOL, "Unable to parse INTRODUCE2 cell on circuit %u "
  206. "for service %s",
  207. TO_CIRCUIT(circ)->n_circ_id,
  208. safe_str_client(service->onion_address));
  209. goto err;
  210. }
  211. /* Success. */
  212. *cell_ptr_out = cell;
  213. return 0;
  214. err:
  215. return -1;
  216. }
  217. /* Set the onion public key onion_pk in cell, the encrypted section of an
  218. * INTRODUCE1 cell. */
  219. static void
  220. introduce1_set_encrypted_onion_key(trn_cell_introduce_encrypted_t *cell,
  221. const uint8_t *onion_pk)
  222. {
  223. tor_assert(cell);
  224. tor_assert(onion_pk);
  225. /* There is only one possible key type for a non legacy cell. */
  226. trn_cell_introduce_encrypted_set_onion_key_type(cell,
  227. TRUNNEL_HS_INTRO_ONION_KEY_TYPE_NTOR);
  228. trn_cell_introduce_encrypted_set_onion_key_len(cell, CURVE25519_PUBKEY_LEN);
  229. trn_cell_introduce_encrypted_setlen_onion_key(cell, CURVE25519_PUBKEY_LEN);
  230. memcpy(trn_cell_introduce_encrypted_getarray_onion_key(cell), onion_pk,
  231. trn_cell_introduce_encrypted_getlen_onion_key(cell));
  232. }
  233. /* Set the link specifiers in lspecs in cell, the encrypted section of an
  234. * INTRODUCE1 cell. */
  235. static void
  236. introduce1_set_encrypted_link_spec(trn_cell_introduce_encrypted_t *cell,
  237. const smartlist_t *lspecs)
  238. {
  239. tor_assert(cell);
  240. tor_assert(lspecs);
  241. tor_assert(smartlist_len(lspecs) > 0);
  242. tor_assert(smartlist_len(lspecs) <= UINT8_MAX);
  243. uint8_t lspecs_num = (uint8_t) smartlist_len(lspecs);
  244. trn_cell_introduce_encrypted_set_nspec(cell, lspecs_num);
  245. /* We aren't duplicating the link specifiers object here which means that
  246. * the ownership goes to the trn_cell_introduce_encrypted_t cell and those
  247. * object will be freed when the cell is. */
  248. SMARTLIST_FOREACH(lspecs, link_specifier_t *, ls,
  249. trn_cell_introduce_encrypted_add_nspecs(cell, ls));
  250. }
  251. /* Set padding in the enc_cell only if needed that is the total length of both
  252. * sections are below the mininum required for an INTRODUCE1 cell. */
  253. static void
  254. introduce1_set_encrypted_padding(const trn_cell_introduce1_t *cell,
  255. trn_cell_introduce_encrypted_t *enc_cell)
  256. {
  257. tor_assert(cell);
  258. tor_assert(enc_cell);
  259. /* This is the length we expect to have once encoded of the whole cell. */
  260. ssize_t full_len = trn_cell_introduce1_encoded_len(cell) +
  261. trn_cell_introduce_encrypted_encoded_len(enc_cell);
  262. tor_assert(full_len > 0);
  263. if (full_len < HS_CELL_INTRODUCE1_MIN_SIZE) {
  264. size_t padding = HS_CELL_INTRODUCE1_MIN_SIZE - full_len;
  265. trn_cell_introduce_encrypted_setlen_pad(enc_cell, padding);
  266. memset(trn_cell_introduce_encrypted_getarray_pad(enc_cell), 0,
  267. trn_cell_introduce_encrypted_getlen_pad(enc_cell));
  268. }
  269. }
  270. /* Encrypt the ENCRYPTED payload and encode it in the cell using the enc_cell
  271. * and the INTRODUCE1 data.
  272. *
  273. * This can't fail but it is very important that the caller sets every field
  274. * in data so the computation of the INTRODUCE1 keys doesn't fail. */
  275. static void
  276. introduce1_encrypt_and_encode(trn_cell_introduce1_t *cell,
  277. const trn_cell_introduce_encrypted_t *enc_cell,
  278. const hs_cell_introduce1_data_t *data)
  279. {
  280. size_t offset = 0;
  281. ssize_t encrypted_len;
  282. ssize_t encoded_cell_len, encoded_enc_cell_len;
  283. uint8_t encoded_cell[RELAY_PAYLOAD_SIZE] = {0};
  284. uint8_t encoded_enc_cell[RELAY_PAYLOAD_SIZE] = {0};
  285. uint8_t *encrypted = NULL;
  286. uint8_t mac[DIGEST256_LEN];
  287. crypto_cipher_t *cipher = NULL;
  288. hs_ntor_intro_cell_keys_t keys;
  289. tor_assert(cell);
  290. tor_assert(enc_cell);
  291. tor_assert(data);
  292. /* Encode the cells up to now of what we have to we can perform the MAC
  293. * computation on it. */
  294. encoded_cell_len = trn_cell_introduce1_encode(encoded_cell,
  295. sizeof(encoded_cell), cell);
  296. /* We have a much more serious issue if this isn't true. */
  297. tor_assert(encoded_cell_len > 0);
  298. encoded_enc_cell_len =
  299. trn_cell_introduce_encrypted_encode(encoded_enc_cell,
  300. sizeof(encoded_enc_cell), enc_cell);
  301. /* We have a much more serious issue if this isn't true. */
  302. tor_assert(encoded_enc_cell_len > 0);
  303. /* Get the key material for the encryption. */
  304. if (hs_ntor_client_get_introduce1_keys(data->auth_pk, data->enc_pk,
  305. data->client_kp,
  306. data->subcredential, &keys) < 0) {
  307. tor_assert_unreached();
  308. }
  309. /* Prepare cipher with the encryption key just computed. */
  310. cipher = crypto_cipher_new_with_bits((const char *) keys.enc_key,
  311. sizeof(keys.enc_key) * 8);
  312. tor_assert(cipher);
  313. /* Compute the length of the ENCRYPTED section which is the CLIENT_PK,
  314. * ENCRYPTED_DATA and MAC length. */
  315. encrypted_len = sizeof(data->client_kp->pubkey) + encoded_enc_cell_len +
  316. sizeof(mac);
  317. tor_assert(encrypted_len < RELAY_PAYLOAD_SIZE);
  318. encrypted = tor_malloc_zero(encrypted_len);
  319. /* Put the CLIENT_PK first. */
  320. memcpy(encrypted, data->client_kp->pubkey.public_key,
  321. sizeof(data->client_kp->pubkey.public_key));
  322. offset += sizeof(data->client_kp->pubkey.public_key);
  323. /* Then encrypt and set the ENCRYPTED_DATA. This can't fail. */
  324. crypto_cipher_encrypt(cipher, (char *) encrypted + offset,
  325. (const char *) encoded_enc_cell, encoded_enc_cell_len);
  326. crypto_cipher_free(cipher);
  327. offset += encoded_enc_cell_len;
  328. /* Compute MAC from the above and put it in the buffer. This function will
  329. * make the adjustment to the encrypted_len to omit the MAC length. */
  330. compute_introduce_mac(encoded_cell, encoded_cell_len,
  331. encrypted, encrypted_len,
  332. keys.mac_key, sizeof(keys.mac_key),
  333. mac, sizeof(mac));
  334. memcpy(encrypted + offset, mac, sizeof(mac));
  335. offset += sizeof(mac);
  336. tor_assert(offset == (size_t) encrypted_len);
  337. /* Set the ENCRYPTED section in the cell. */
  338. trn_cell_introduce1_setlen_encrypted(cell, encrypted_len);
  339. memcpy(trn_cell_introduce1_getarray_encrypted(cell),
  340. encrypted, encrypted_len);
  341. /* Cleanup. */
  342. memwipe(&keys, 0, sizeof(keys));
  343. memwipe(mac, 0, sizeof(mac));
  344. memwipe(encrypted, 0, sizeof(encrypted_len));
  345. memwipe(encoded_enc_cell, 0, sizeof(encoded_enc_cell));
  346. tor_free(encrypted);
  347. }
  348. /* Using the INTRODUCE1 data, setup the ENCRYPTED section in cell. This means
  349. * set it, encrypt it and encode it. */
  350. static void
  351. introduce1_set_encrypted(trn_cell_introduce1_t *cell,
  352. const hs_cell_introduce1_data_t *data)
  353. {
  354. trn_cell_introduce_encrypted_t *enc_cell;
  355. trn_cell_extension_t *ext;
  356. tor_assert(cell);
  357. tor_assert(data);
  358. enc_cell = trn_cell_introduce_encrypted_new();
  359. tor_assert(enc_cell);
  360. /* Set extension data. None are used. */
  361. ext = trn_cell_extension_new();
  362. tor_assert(ext);
  363. trn_cell_extension_set_num(ext, 0);
  364. trn_cell_introduce_encrypted_set_extensions(enc_cell, ext);
  365. /* Set the rendezvous cookie. */
  366. memcpy(trn_cell_introduce_encrypted_getarray_rend_cookie(enc_cell),
  367. data->rendezvous_cookie, REND_COOKIE_LEN);
  368. /* Set the onion public key. */
  369. introduce1_set_encrypted_onion_key(enc_cell, data->onion_pk->public_key);
  370. /* Set the link specifiers. */
  371. introduce1_set_encrypted_link_spec(enc_cell, data->link_specifiers);
  372. /* Set padding. */
  373. introduce1_set_encrypted_padding(cell, enc_cell);
  374. /* Encrypt and encode it in the cell. */
  375. introduce1_encrypt_and_encode(cell, enc_cell, data);
  376. /* Cleanup. */
  377. trn_cell_introduce_encrypted_free(enc_cell);
  378. }
  379. /* Set the authentication key in the INTRODUCE1 cell from the given data. */
  380. static void
  381. introduce1_set_auth_key(trn_cell_introduce1_t *cell,
  382. const hs_cell_introduce1_data_t *data)
  383. {
  384. tor_assert(cell);
  385. tor_assert(data);
  386. /* There is only one possible type for a non legacy cell. */
  387. trn_cell_introduce1_set_auth_key_type(cell,
  388. TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519);
  389. trn_cell_introduce1_set_auth_key_len(cell, ED25519_PUBKEY_LEN);
  390. trn_cell_introduce1_setlen_auth_key(cell, ED25519_PUBKEY_LEN);
  391. memcpy(trn_cell_introduce1_getarray_auth_key(cell),
  392. data->auth_pk->pubkey, trn_cell_introduce1_getlen_auth_key(cell));
  393. }
  394. /* Set the legacy ID field in the INTRODUCE1 cell from the given data. */
  395. static void
  396. introduce1_set_legacy_id(trn_cell_introduce1_t *cell,
  397. const hs_cell_introduce1_data_t *data)
  398. {
  399. tor_assert(cell);
  400. tor_assert(data);
  401. if (data->is_legacy) {
  402. uint8_t digest[DIGEST_LEN];
  403. if (BUG(crypto_pk_get_digest(data->legacy_key, (char *) digest) < 0)) {
  404. return;
  405. }
  406. memcpy(trn_cell_introduce1_getarray_legacy_key_id(cell),
  407. digest, trn_cell_introduce1_getlen_legacy_key_id(cell));
  408. } else {
  409. /* We have to zeroed the LEGACY_KEY_ID field. */
  410. memset(trn_cell_introduce1_getarray_legacy_key_id(cell), 0,
  411. trn_cell_introduce1_getlen_legacy_key_id(cell));
  412. }
  413. }
  414. /* Build and add to the given DoS cell extension the given parameter type and
  415. * value. */
  416. static void
  417. build_establish_intro_dos_param(trn_cell_extension_dos_t *dos_ext,
  418. uint8_t param_type, uint64_t param_value)
  419. {
  420. trn_cell_extension_dos_param_t *dos_param =
  421. trn_cell_extension_dos_param_new();
  422. /* Extra safety. We should never send an unknown parameter type. */
  423. tor_assert(param_type == TRUNNEL_DOS_PARAM_TYPE_INTRO2_RATE_PER_SEC ||
  424. param_type == TRUNNEL_DOS_PARAM_TYPE_INTRO2_BURST_PER_SEC);
  425. trn_cell_extension_dos_param_set_type(dos_param, param_type);
  426. trn_cell_extension_dos_param_set_value(dos_param, param_value);
  427. trn_cell_extension_dos_add_params(dos_ext, dos_param);
  428. /* Not freeing the trunnel object because it is now owned by dos_ext. */
  429. }
  430. /* Build the DoS defense cell extension and put it in the given extensions
  431. * object. This can't fail. */
  432. static int
  433. build_establish_intro_dos_extension(const hs_service_config_t *service_config,
  434. trn_cell_extension_t *extensions)
  435. {
  436. ssize_t ret;
  437. size_t dos_ext_encoded_len;
  438. uint8_t *field_array;
  439. trn_cell_extension_field_t *field;
  440. trn_cell_extension_dos_t *dos_ext;
  441. tor_assert(service_config);
  442. tor_assert(extensions);
  443. /* We are creating a cell extension field of the type DoS. */
  444. field = trn_cell_extension_field_new();
  445. trn_cell_extension_field_set_field_type(field,
  446. TRUNNEL_CELL_EXTENSION_TYPE_DOS);
  447. /* Build DoS extension field. We will put in two parameters. */
  448. dos_ext = trn_cell_extension_dos_new();
  449. trn_cell_extension_dos_set_n_params(dos_ext, 2);
  450. /* Build DoS parameter INTRO2 rate per second. */
  451. build_establish_intro_dos_param(dos_ext,
  452. TRUNNEL_DOS_PARAM_TYPE_INTRO2_RATE_PER_SEC,
  453. service_config->intro_dos_rate_per_sec);
  454. /* Build DoS parameter INTRO2 burst per second. */
  455. build_establish_intro_dos_param(dos_ext,
  456. TRUNNEL_DOS_PARAM_TYPE_INTRO2_BURST_PER_SEC,
  457. service_config->intro_dos_burst_per_sec);
  458. /* Set the field with the encoded DoS extension. */
  459. ret = trn_cell_extension_dos_encoded_len(dos_ext);
  460. if (BUG(ret <= 0)) {
  461. return -1;
  462. }
  463. dos_ext_encoded_len = ret;
  464. /* Set length field and the field array size length. */
  465. trn_cell_extension_field_set_field_len(field, dos_ext_encoded_len);
  466. trn_cell_extension_field_setlen_field(field, dos_ext_encoded_len);
  467. /* Encode the DoS extension into the cell extension field. */
  468. field_array = trn_cell_extension_field_getarray_field(field);
  469. ret = trn_cell_extension_dos_encode(field_array,
  470. trn_cell_extension_field_getlen_field(field), dos_ext);
  471. if (BUG(ret <= 0)) {
  472. return -1;
  473. }
  474. tor_assert(ret == (ssize_t) dos_ext_encoded_len);
  475. /* Finally, encode field into the cell extension. */
  476. trn_cell_extension_add_fields(extensions, field);
  477. /* We've just add an extension field to the cell extensions so increment the
  478. * total number. */
  479. trn_cell_extension_set_num(extensions,
  480. trn_cell_extension_get_num(extensions) + 1);
  481. /* Cleanup. DoS extension has been encoded at this point. */
  482. trn_cell_extension_dos_free(dos_ext);
  483. return 0;
  484. }
  485. /* ========== */
  486. /* Public API */
  487. /* ========== */
  488. /* Allocate and build all the ESTABLISH_INTRO cell extension. The given
  489. * extensions pointer is always set to a valid cell extension object. */
  490. STATIC trn_cell_extension_t *
  491. build_establish_intro_extensions(const hs_service_config_t *service_config,
  492. const hs_service_intro_point_t *ip)
  493. {
  494. int ret;
  495. trn_cell_extension_t *extensions;
  496. tor_assert(service_config);
  497. tor_assert(ip);
  498. extensions = trn_cell_extension_new();
  499. trn_cell_extension_set_num(extensions, 0);
  500. /* If the defense has been enabled service side (by the operator with a
  501. * torrc option) and the intro point does support it. */
  502. if (service_config->has_dos_defense_enabled &&
  503. ip->support_intro2_dos_defense) {
  504. /* This function takes care to increment the number of extensions. */
  505. ret = build_establish_intro_dos_extension(service_config, extensions);
  506. if (ret < 0) {
  507. /* Return no extensions on error. */
  508. goto end;
  509. }
  510. }
  511. end:
  512. return extensions;
  513. }
  514. /* Build an ESTABLISH_INTRO cell with the given circuit nonce and intro point
  515. * object. The encoded cell is put in cell_out that MUST at least be of the
  516. * size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on success else
  517. * a negative value and cell_out is untouched. This function also supports
  518. * legacy cell creation. */
  519. ssize_t
  520. hs_cell_build_establish_intro(const char *circ_nonce,
  521. const hs_service_config_t *service_config,
  522. const hs_service_intro_point_t *ip,
  523. uint8_t *cell_out)
  524. {
  525. ssize_t cell_len = -1;
  526. uint16_t sig_len = ED25519_SIG_LEN;
  527. trn_cell_establish_intro_t *cell = NULL;
  528. trn_cell_extension_t *extensions;
  529. tor_assert(circ_nonce);
  530. tor_assert(service_config);
  531. tor_assert(ip);
  532. /* Quickly handle the legacy IP. */
  533. if (ip->base.is_only_legacy) {
  534. tor_assert(ip->legacy_key);
  535. cell_len = build_legacy_establish_intro(circ_nonce, ip->legacy_key,
  536. cell_out);
  537. tor_assert(cell_len <= RELAY_PAYLOAD_SIZE);
  538. /* Success or not we are done here. */
  539. goto done;
  540. }
  541. /* Build the extensions, if any. */
  542. extensions = build_establish_intro_extensions(service_config, ip);
  543. /* Set extension data. None used here. */
  544. cell = trn_cell_establish_intro_new();
  545. trn_cell_establish_intro_set_extensions(cell, extensions);
  546. /* Set signature size. Array is then allocated in the cell. We need to do
  547. * this early so we can use trunnel API to get the signature length. */
  548. trn_cell_establish_intro_set_sig_len(cell, sig_len);
  549. trn_cell_establish_intro_setlen_sig(cell, sig_len);
  550. /* Set AUTH_KEY_TYPE: 2 means ed25519 */
  551. trn_cell_establish_intro_set_auth_key_type(cell,
  552. TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519);
  553. /* Set AUTH_KEY and AUTH_KEY_LEN field. Must also set byte-length of
  554. * AUTH_KEY to match */
  555. {
  556. uint16_t auth_key_len = ED25519_PUBKEY_LEN;
  557. trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
  558. trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
  559. /* We do this call _after_ setting the length because it's reallocated at
  560. * that point only. */
  561. uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
  562. memcpy(auth_key_ptr, ip->auth_key_kp.pubkey.pubkey, auth_key_len);
  563. }
  564. /* Calculate HANDSHAKE_AUTH field (MAC). */
  565. {
  566. ssize_t tmp_cell_enc_len = 0;
  567. ssize_t tmp_cell_mac_offset =
  568. sig_len + sizeof(cell->sig_len) +
  569. trn_cell_establish_intro_getlen_handshake_mac(cell);
  570. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0};
  571. uint8_t mac[TRUNNEL_SHA3_256_LEN], *handshake_ptr;
  572. /* We first encode the current fields we have in the cell so we can
  573. * compute the MAC using the raw bytes. */
  574. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  575. sizeof(tmp_cell_enc),
  576. cell);
  577. if (BUG(tmp_cell_enc_len < 0)) {
  578. goto done;
  579. }
  580. /* Sanity check. */
  581. tor_assert(tmp_cell_enc_len > tmp_cell_mac_offset);
  582. /* Circuit nonce is always DIGEST_LEN according to tor-spec.txt. */
  583. crypto_mac_sha3_256(mac, sizeof(mac),
  584. (uint8_t *) circ_nonce, DIGEST_LEN,
  585. tmp_cell_enc, tmp_cell_enc_len - tmp_cell_mac_offset);
  586. handshake_ptr = trn_cell_establish_intro_getarray_handshake_mac(cell);
  587. memcpy(handshake_ptr, mac, sizeof(mac));
  588. memwipe(mac, 0, sizeof(mac));
  589. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  590. }
  591. /* Calculate the cell signature SIG. */
  592. {
  593. ssize_t tmp_cell_enc_len = 0;
  594. ssize_t tmp_cell_sig_offset = (sig_len + sizeof(cell->sig_len));
  595. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0}, *sig_ptr;
  596. ed25519_signature_t sig;
  597. /* We first encode the current fields we have in the cell so we can
  598. * compute the signature from the raw bytes of the cell. */
  599. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  600. sizeof(tmp_cell_enc),
  601. cell);
  602. if (BUG(tmp_cell_enc_len < 0)) {
  603. goto done;
  604. }
  605. if (ed25519_sign_prefixed(&sig, tmp_cell_enc,
  606. tmp_cell_enc_len - tmp_cell_sig_offset,
  607. ESTABLISH_INTRO_SIG_PREFIX, &ip->auth_key_kp)) {
  608. log_warn(LD_BUG, "Unable to make signature for ESTABLISH_INTRO cell.");
  609. goto done;
  610. }
  611. /* Copy the signature into the cell. */
  612. sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
  613. memcpy(sig_ptr, sig.sig, sig_len);
  614. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  615. }
  616. /* Encode the cell. Can't be bigger than a standard cell. */
  617. cell_len = trn_cell_establish_intro_encode(cell_out, RELAY_PAYLOAD_SIZE,
  618. cell);
  619. done:
  620. trn_cell_establish_intro_free(cell);
  621. return cell_len;
  622. }
  623. /* Parse the INTRO_ESTABLISHED cell in the payload of size payload_len. If we
  624. * are successful at parsing it, return the length of the parsed cell else a
  625. * negative value on error. */
  626. ssize_t
  627. hs_cell_parse_intro_established(const uint8_t *payload, size_t payload_len)
  628. {
  629. ssize_t ret;
  630. trn_cell_intro_established_t *cell = NULL;
  631. tor_assert(payload);
  632. /* Try to parse the payload into a cell making sure we do actually have a
  633. * valid cell. */
  634. ret = trn_cell_intro_established_parse(&cell, payload, payload_len);
  635. if (ret >= 0) {
  636. /* On success, we do not keep the cell, we just notify the caller that it
  637. * was successfully parsed. */
  638. trn_cell_intro_established_free(cell);
  639. }
  640. return ret;
  641. }
  642. /* Parse the INTRODUCE2 cell using data which contains everything we need to
  643. * do so and contains the destination buffers of information we extract and
  644. * compute from the cell. Return 0 on success else a negative value. The
  645. * service and circ are only used for logging purposes. */
  646. ssize_t
  647. hs_cell_parse_introduce2(hs_cell_introduce2_data_t *data,
  648. const origin_circuit_t *circ,
  649. const hs_service_t *service)
  650. {
  651. int ret = -1;
  652. time_t elapsed;
  653. uint8_t *decrypted = NULL;
  654. size_t encrypted_section_len;
  655. const uint8_t *encrypted_section;
  656. trn_cell_introduce1_t *cell = NULL;
  657. trn_cell_introduce_encrypted_t *enc_cell = NULL;
  658. hs_ntor_intro_cell_keys_t *intro_keys = NULL;
  659. tor_assert(data);
  660. tor_assert(circ);
  661. tor_assert(service);
  662. /* Parse the cell into a decoded data structure pointed by cell_ptr. */
  663. if (parse_introduce2_cell(service, circ, data->payload, data->payload_len,
  664. &cell) < 0) {
  665. goto done;
  666. }
  667. log_info(LD_REND, "Received a decodable INTRODUCE2 cell on circuit %u "
  668. "for service %s. Decoding encrypted section...",
  669. TO_CIRCUIT(circ)->n_circ_id,
  670. safe_str_client(service->onion_address));
  671. encrypted_section = trn_cell_introduce1_getconstarray_encrypted(cell);
  672. encrypted_section_len = trn_cell_introduce1_getlen_encrypted(cell);
  673. /* Encrypted section must at least contain the CLIENT_PK and MAC which is
  674. * defined in section 3.3.2 of the specification. */
  675. if (encrypted_section_len < (CURVE25519_PUBKEY_LEN + DIGEST256_LEN)) {
  676. log_info(LD_REND, "Invalid INTRODUCE2 encrypted section length "
  677. "for service %s. Dropping cell.",
  678. safe_str_client(service->onion_address));
  679. goto done;
  680. }
  681. /* Check our replay cache for this introduction point. */
  682. if (replaycache_add_test_and_elapsed(data->replay_cache, encrypted_section,
  683. encrypted_section_len, &elapsed)) {
  684. log_warn(LD_REND, "Possible replay detected! An INTRODUCE2 cell with the"
  685. "same ENCRYPTED section was seen %ld seconds ago. "
  686. "Dropping cell.", (long int) elapsed);
  687. goto done;
  688. }
  689. /* Build the key material out of the key material found in the cell. */
  690. intro_keys = get_introduce2_key_material(data->auth_pk, data->enc_kp,
  691. data->subcredential,
  692. encrypted_section,
  693. &data->client_pk);
  694. if (intro_keys == NULL) {
  695. log_info(LD_REND, "Invalid INTRODUCE2 encrypted data. Unable to "
  696. "compute key material on circuit %u for service %s",
  697. TO_CIRCUIT(circ)->n_circ_id,
  698. safe_str_client(service->onion_address));
  699. goto done;
  700. }
  701. /* Validate MAC from the cell and our computed key material. The MAC field
  702. * in the cell is at the end of the encrypted section. */
  703. {
  704. uint8_t mac[DIGEST256_LEN];
  705. /* The MAC field is at the very end of the ENCRYPTED section. */
  706. size_t mac_offset = encrypted_section_len - sizeof(mac);
  707. /* Compute the MAC. Use the entire encoded payload with a length up to the
  708. * ENCRYPTED section. */
  709. compute_introduce_mac(data->payload,
  710. data->payload_len - encrypted_section_len,
  711. encrypted_section, encrypted_section_len,
  712. intro_keys->mac_key, sizeof(intro_keys->mac_key),
  713. mac, sizeof(mac));
  714. if (tor_memcmp(mac, encrypted_section + mac_offset, sizeof(mac))) {
  715. log_info(LD_REND, "Invalid MAC validation for INTRODUCE2 cell on "
  716. "circuit %u for service %s",
  717. TO_CIRCUIT(circ)->n_circ_id,
  718. safe_str_client(service->onion_address));
  719. goto done;
  720. }
  721. }
  722. {
  723. /* The ENCRYPTED_DATA section starts just after the CLIENT_PK. */
  724. const uint8_t *encrypted_data =
  725. encrypted_section + sizeof(data->client_pk);
  726. /* It's symmetric encryption so it's correct to use the ENCRYPTED length
  727. * for decryption. Computes the length of ENCRYPTED_DATA meaning removing
  728. * the CLIENT_PK and MAC length. */
  729. size_t encrypted_data_len =
  730. encrypted_section_len - (sizeof(data->client_pk) + DIGEST256_LEN);
  731. /* This decrypts the ENCRYPTED_DATA section of the cell. */
  732. decrypted = decrypt_introduce2(intro_keys->enc_key,
  733. encrypted_data, encrypted_data_len);
  734. if (decrypted == NULL) {
  735. log_info(LD_REND, "Unable to decrypt the ENCRYPTED section of an "
  736. "INTRODUCE2 cell on circuit %u for service %s",
  737. TO_CIRCUIT(circ)->n_circ_id,
  738. safe_str_client(service->onion_address));
  739. goto done;
  740. }
  741. /* Parse this blob into an encrypted cell structure so we can then extract
  742. * the data we need out of it. */
  743. enc_cell = parse_introduce2_encrypted(decrypted, encrypted_data_len,
  744. circ, service);
  745. memwipe(decrypted, 0, encrypted_data_len);
  746. if (enc_cell == NULL) {
  747. goto done;
  748. }
  749. }
  750. /* XXX: Implement client authorization checks. */
  751. /* Extract onion key and rendezvous cookie from the cell used for the
  752. * rendezvous point circuit e2e encryption. */
  753. memcpy(data->onion_pk.public_key,
  754. trn_cell_introduce_encrypted_getconstarray_onion_key(enc_cell),
  755. CURVE25519_PUBKEY_LEN);
  756. memcpy(data->rendezvous_cookie,
  757. trn_cell_introduce_encrypted_getconstarray_rend_cookie(enc_cell),
  758. sizeof(data->rendezvous_cookie));
  759. /* Extract rendezvous link specifiers. */
  760. for (size_t idx = 0;
  761. idx < trn_cell_introduce_encrypted_get_nspec(enc_cell); idx++) {
  762. link_specifier_t *lspec =
  763. trn_cell_introduce_encrypted_get_nspecs(enc_cell, idx);
  764. if (BUG(!lspec)) {
  765. goto done;
  766. }
  767. link_specifier_t *lspec_dup = link_specifier_dup(lspec);
  768. if (BUG(!lspec_dup)) {
  769. goto done;
  770. }
  771. smartlist_add(data->link_specifiers, lspec_dup);
  772. }
  773. /* Success. */
  774. ret = 0;
  775. log_info(LD_REND, "Valid INTRODUCE2 cell. Launching rendezvous circuit.");
  776. done:
  777. if (intro_keys) {
  778. memwipe(intro_keys, 0, sizeof(hs_ntor_intro_cell_keys_t));
  779. tor_free(intro_keys);
  780. }
  781. tor_free(decrypted);
  782. trn_cell_introduce_encrypted_free(enc_cell);
  783. trn_cell_introduce1_free(cell);
  784. return ret;
  785. }
  786. /* Build a RENDEZVOUS1 cell with the given rendezvous cookie and handshake
  787. * info. The encoded cell is put in cell_out and the length of the data is
  788. * returned. This can't fail. */
  789. ssize_t
  790. hs_cell_build_rendezvous1(const uint8_t *rendezvous_cookie,
  791. size_t rendezvous_cookie_len,
  792. const uint8_t *rendezvous_handshake_info,
  793. size_t rendezvous_handshake_info_len,
  794. uint8_t *cell_out)
  795. {
  796. ssize_t cell_len;
  797. trn_cell_rendezvous1_t *cell;
  798. tor_assert(rendezvous_cookie);
  799. tor_assert(rendezvous_handshake_info);
  800. tor_assert(cell_out);
  801. cell = trn_cell_rendezvous1_new();
  802. /* Set the RENDEZVOUS_COOKIE. */
  803. memcpy(trn_cell_rendezvous1_getarray_rendezvous_cookie(cell),
  804. rendezvous_cookie, rendezvous_cookie_len);
  805. /* Set the HANDSHAKE_INFO. */
  806. trn_cell_rendezvous1_setlen_handshake_info(cell,
  807. rendezvous_handshake_info_len);
  808. memcpy(trn_cell_rendezvous1_getarray_handshake_info(cell),
  809. rendezvous_handshake_info, rendezvous_handshake_info_len);
  810. /* Encoding. */
  811. cell_len = trn_cell_rendezvous1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
  812. tor_assert(cell_len > 0);
  813. trn_cell_rendezvous1_free(cell);
  814. return cell_len;
  815. }
  816. /* Build an INTRODUCE1 cell from the given data. The encoded cell is put in
  817. * cell_out which must be of at least size RELAY_PAYLOAD_SIZE. On success, the
  818. * encoded length is returned else a negative value and the content of
  819. * cell_out should be ignored. */
  820. ssize_t
  821. hs_cell_build_introduce1(const hs_cell_introduce1_data_t *data,
  822. uint8_t *cell_out)
  823. {
  824. ssize_t cell_len;
  825. trn_cell_introduce1_t *cell;
  826. trn_cell_extension_t *ext;
  827. tor_assert(data);
  828. tor_assert(cell_out);
  829. cell = trn_cell_introduce1_new();
  830. tor_assert(cell);
  831. /* Set extension data. None are used. */
  832. ext = trn_cell_extension_new();
  833. tor_assert(ext);
  834. trn_cell_extension_set_num(ext, 0);
  835. trn_cell_introduce1_set_extensions(cell, ext);
  836. /* Set the legacy ID field. */
  837. introduce1_set_legacy_id(cell, data);
  838. /* Set the authentication key. */
  839. introduce1_set_auth_key(cell, data);
  840. /* Set the encrypted section. This will set, encrypt and encode the
  841. * ENCRYPTED section in the cell. After this, we'll be ready to encode. */
  842. introduce1_set_encrypted(cell, data);
  843. /* Final encoding. */
  844. cell_len = trn_cell_introduce1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
  845. trn_cell_introduce1_free(cell);
  846. return cell_len;
  847. }
  848. /* Build an ESTABLISH_RENDEZVOUS cell from the given rendezvous_cookie. The
  849. * encoded cell is put in cell_out which must be of at least
  850. * RELAY_PAYLOAD_SIZE. On success, the encoded length is returned and the
  851. * caller should clear up the content of the cell.
  852. *
  853. * This function can't fail. */
  854. ssize_t
  855. hs_cell_build_establish_rendezvous(const uint8_t *rendezvous_cookie,
  856. uint8_t *cell_out)
  857. {
  858. tor_assert(rendezvous_cookie);
  859. tor_assert(cell_out);
  860. memcpy(cell_out, rendezvous_cookie, HS_REND_COOKIE_LEN);
  861. return HS_REND_COOKIE_LEN;
  862. }
  863. /* Handle an INTRODUCE_ACK cell encoded in payload of length payload_len.
  864. * Return the status code on success else a negative value if the cell as not
  865. * decodable. */
  866. int
  867. hs_cell_parse_introduce_ack(const uint8_t *payload, size_t payload_len)
  868. {
  869. int ret = -1;
  870. trn_cell_introduce_ack_t *cell = NULL;
  871. tor_assert(payload);
  872. /* If it is a legacy IP, rend-spec.txt specifies that a ACK is 0 byte and a
  873. * NACK is 1 byte. We can't use the legacy function for this so we have to
  874. * do a special case. */
  875. if (payload_len <= 1) {
  876. if (payload_len == 0) {
  877. ret = TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS;
  878. } else {
  879. ret = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
  880. }
  881. goto end;
  882. }
  883. if (trn_cell_introduce_ack_parse(&cell, payload, payload_len) < 0) {
  884. log_info(LD_REND, "Invalid INTRODUCE_ACK cell. Unable to parse it.");
  885. goto end;
  886. }
  887. ret = trn_cell_introduce_ack_get_status(cell);
  888. end:
  889. trn_cell_introduce_ack_free(cell);
  890. return ret;
  891. }
  892. /* Handle a RENDEZVOUS2 cell encoded in payload of length payload_len. On
  893. * success, handshake_info contains the data in the HANDSHAKE_INFO field, and
  894. * 0 is returned. On error, a negative value is returned. */
  895. int
  896. hs_cell_parse_rendezvous2(const uint8_t *payload, size_t payload_len,
  897. uint8_t *handshake_info, size_t handshake_info_len)
  898. {
  899. int ret = -1;
  900. trn_cell_rendezvous2_t *cell = NULL;
  901. tor_assert(payload);
  902. tor_assert(handshake_info);
  903. if (trn_cell_rendezvous2_parse(&cell, payload, payload_len) < 0) {
  904. log_info(LD_REND, "Invalid RENDEZVOUS2 cell. Unable to parse it.");
  905. goto end;
  906. }
  907. /* Static size, we should never have an issue with this else we messed up
  908. * our code flow. */
  909. tor_assert(trn_cell_rendezvous2_getlen_handshake_info(cell) ==
  910. handshake_info_len);
  911. memcpy(handshake_info,
  912. trn_cell_rendezvous2_getconstarray_handshake_info(cell),
  913. handshake_info_len);
  914. ret = 0;
  915. end:
  916. trn_cell_rendezvous2_free(cell);
  917. return ret;
  918. }
  919. /* Clear the given INTRODUCE1 data structure data. */
  920. void
  921. hs_cell_introduce1_data_clear(hs_cell_introduce1_data_t *data)
  922. {
  923. if (data == NULL) {
  924. return;
  925. }
  926. /* Object in this list have been moved to the cell object when building it
  927. * so they've been freed earlier. We do that in order to avoid duplicating
  928. * them leading to more memory and CPU time being used for nothing. */
  929. smartlist_free(data->link_specifiers);
  930. /* The data object has no ownership of any members. */
  931. memwipe(data, 0, sizeof(hs_cell_introduce1_data_t));
  932. }