hs_cell.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. Return 0 on success, -1 on failure. (Right now, failure is only
  432. * possible if there is a bug.) */
  433. static int
  434. build_establish_intro_dos_extension(const hs_service_config_t *service_config,
  435. trn_cell_extension_t *extensions)
  436. {
  437. ssize_t ret;
  438. size_t dos_ext_encoded_len;
  439. uint8_t *field_array;
  440. trn_cell_extension_field_t *field = NULL;
  441. trn_cell_extension_dos_t *dos_ext = NULL;
  442. tor_assert(service_config);
  443. tor_assert(extensions);
  444. /* We are creating a cell extension field of the type DoS. */
  445. field = trn_cell_extension_field_new();
  446. trn_cell_extension_field_set_field_type(field,
  447. TRUNNEL_CELL_EXTENSION_TYPE_DOS);
  448. /* Build DoS extension field. We will put in two parameters. */
  449. dos_ext = trn_cell_extension_dos_new();
  450. trn_cell_extension_dos_set_n_params(dos_ext, 2);
  451. /* Build DoS parameter INTRO2 rate per second. */
  452. build_establish_intro_dos_param(dos_ext,
  453. TRUNNEL_DOS_PARAM_TYPE_INTRO2_RATE_PER_SEC,
  454. service_config->intro_dos_rate_per_sec);
  455. /* Build DoS parameter INTRO2 burst per second. */
  456. build_establish_intro_dos_param(dos_ext,
  457. TRUNNEL_DOS_PARAM_TYPE_INTRO2_BURST_PER_SEC,
  458. service_config->intro_dos_burst_per_sec);
  459. /* Set the field with the encoded DoS extension. */
  460. ret = trn_cell_extension_dos_encoded_len(dos_ext);
  461. if (BUG(ret <= 0)) {
  462. goto err;
  463. }
  464. dos_ext_encoded_len = ret;
  465. /* Set length field and the field array size length. */
  466. trn_cell_extension_field_set_field_len(field, dos_ext_encoded_len);
  467. trn_cell_extension_field_setlen_field(field, dos_ext_encoded_len);
  468. /* Encode the DoS extension into the cell extension field. */
  469. field_array = trn_cell_extension_field_getarray_field(field);
  470. ret = trn_cell_extension_dos_encode(field_array,
  471. trn_cell_extension_field_getlen_field(field), dos_ext);
  472. if (BUG(ret <= 0)) {
  473. goto err;
  474. }
  475. tor_assert(ret == (ssize_t) dos_ext_encoded_len);
  476. /* Finally, encode field into the cell extension. */
  477. trn_cell_extension_add_fields(extensions, field);
  478. /* We've just add an extension field to the cell extensions so increment the
  479. * total number. */
  480. trn_cell_extension_set_num(extensions,
  481. trn_cell_extension_get_num(extensions) + 1);
  482. /* Cleanup. DoS extension has been encoded at this point. */
  483. trn_cell_extension_dos_free(dos_ext);
  484. return 0;
  485. err:
  486. trn_cell_extension_field_free(field);
  487. trn_cell_extension_dos_free(dos_ext);
  488. return -1;
  489. }
  490. /* ========== */
  491. /* Public API */
  492. /* ========== */
  493. /* Allocate and build all the ESTABLISH_INTRO cell extension. The given
  494. * extensions pointer is always set to a valid cell extension object. */
  495. STATIC trn_cell_extension_t *
  496. build_establish_intro_extensions(const hs_service_config_t *service_config,
  497. const hs_service_intro_point_t *ip)
  498. {
  499. int ret;
  500. trn_cell_extension_t *extensions;
  501. tor_assert(service_config);
  502. tor_assert(ip);
  503. extensions = trn_cell_extension_new();
  504. trn_cell_extension_set_num(extensions, 0);
  505. /* If the defense has been enabled service side (by the operator with a
  506. * torrc option) and the intro point does support it. */
  507. if (service_config->has_dos_defense_enabled &&
  508. ip->support_intro2_dos_defense) {
  509. /* This function takes care to increment the number of extensions. */
  510. ret = build_establish_intro_dos_extension(service_config, extensions);
  511. if (ret < 0) {
  512. /* Return no extensions on error. */
  513. goto end;
  514. }
  515. }
  516. end:
  517. return extensions;
  518. }
  519. /* Build an ESTABLISH_INTRO cell with the given circuit nonce and intro point
  520. * object. The encoded cell is put in cell_out that MUST at least be of the
  521. * size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on success else
  522. * a negative value and cell_out is untouched. This function also supports
  523. * legacy cell creation. */
  524. ssize_t
  525. hs_cell_build_establish_intro(const char *circ_nonce,
  526. const hs_service_config_t *service_config,
  527. const hs_service_intro_point_t *ip,
  528. uint8_t *cell_out)
  529. {
  530. ssize_t cell_len = -1;
  531. uint16_t sig_len = ED25519_SIG_LEN;
  532. trn_cell_establish_intro_t *cell = NULL;
  533. trn_cell_extension_t *extensions;
  534. tor_assert(circ_nonce);
  535. tor_assert(service_config);
  536. tor_assert(ip);
  537. /* Quickly handle the legacy IP. */
  538. if (ip->base.is_only_legacy) {
  539. tor_assert(ip->legacy_key);
  540. cell_len = build_legacy_establish_intro(circ_nonce, ip->legacy_key,
  541. cell_out);
  542. tor_assert(cell_len <= RELAY_PAYLOAD_SIZE);
  543. /* Success or not we are done here. */
  544. goto done;
  545. }
  546. /* Build the extensions, if any. */
  547. extensions = build_establish_intro_extensions(service_config, ip);
  548. /* Set extension data. None used here. */
  549. cell = trn_cell_establish_intro_new();
  550. trn_cell_establish_intro_set_extensions(cell, extensions);
  551. /* Set signature size. Array is then allocated in the cell. We need to do
  552. * this early so we can use trunnel API to get the signature length. */
  553. trn_cell_establish_intro_set_sig_len(cell, sig_len);
  554. trn_cell_establish_intro_setlen_sig(cell, sig_len);
  555. /* Set AUTH_KEY_TYPE: 2 means ed25519 */
  556. trn_cell_establish_intro_set_auth_key_type(cell,
  557. TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519);
  558. /* Set AUTH_KEY and AUTH_KEY_LEN field. Must also set byte-length of
  559. * AUTH_KEY to match */
  560. {
  561. uint16_t auth_key_len = ED25519_PUBKEY_LEN;
  562. trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
  563. trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
  564. /* We do this call _after_ setting the length because it's reallocated at
  565. * that point only. */
  566. uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
  567. memcpy(auth_key_ptr, ip->auth_key_kp.pubkey.pubkey, auth_key_len);
  568. }
  569. /* Calculate HANDSHAKE_AUTH field (MAC). */
  570. {
  571. ssize_t tmp_cell_enc_len = 0;
  572. ssize_t tmp_cell_mac_offset =
  573. sig_len + sizeof(cell->sig_len) +
  574. trn_cell_establish_intro_getlen_handshake_mac(cell);
  575. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0};
  576. uint8_t mac[TRUNNEL_SHA3_256_LEN], *handshake_ptr;
  577. /* We first encode the current fields we have in the cell so we can
  578. * compute the MAC using the raw bytes. */
  579. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  580. sizeof(tmp_cell_enc),
  581. cell);
  582. if (BUG(tmp_cell_enc_len < 0)) {
  583. goto done;
  584. }
  585. /* Sanity check. */
  586. tor_assert(tmp_cell_enc_len > tmp_cell_mac_offset);
  587. /* Circuit nonce is always DIGEST_LEN according to tor-spec.txt. */
  588. crypto_mac_sha3_256(mac, sizeof(mac),
  589. (uint8_t *) circ_nonce, DIGEST_LEN,
  590. tmp_cell_enc, tmp_cell_enc_len - tmp_cell_mac_offset);
  591. handshake_ptr = trn_cell_establish_intro_getarray_handshake_mac(cell);
  592. memcpy(handshake_ptr, mac, sizeof(mac));
  593. memwipe(mac, 0, sizeof(mac));
  594. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  595. }
  596. /* Calculate the cell signature SIG. */
  597. {
  598. ssize_t tmp_cell_enc_len = 0;
  599. ssize_t tmp_cell_sig_offset = (sig_len + sizeof(cell->sig_len));
  600. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0}, *sig_ptr;
  601. ed25519_signature_t sig;
  602. /* We first encode the current fields we have in the cell so we can
  603. * compute the signature from the raw bytes of the cell. */
  604. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  605. sizeof(tmp_cell_enc),
  606. cell);
  607. if (BUG(tmp_cell_enc_len < 0)) {
  608. goto done;
  609. }
  610. if (ed25519_sign_prefixed(&sig, tmp_cell_enc,
  611. tmp_cell_enc_len - tmp_cell_sig_offset,
  612. ESTABLISH_INTRO_SIG_PREFIX, &ip->auth_key_kp)) {
  613. log_warn(LD_BUG, "Unable to make signature for ESTABLISH_INTRO cell.");
  614. goto done;
  615. }
  616. /* Copy the signature into the cell. */
  617. sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
  618. memcpy(sig_ptr, sig.sig, sig_len);
  619. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  620. }
  621. /* Encode the cell. Can't be bigger than a standard cell. */
  622. cell_len = trn_cell_establish_intro_encode(cell_out, RELAY_PAYLOAD_SIZE,
  623. cell);
  624. done:
  625. trn_cell_establish_intro_free(cell);
  626. return cell_len;
  627. }
  628. /* Parse the INTRO_ESTABLISHED cell in the payload of size payload_len. If we
  629. * are successful at parsing it, return the length of the parsed cell else a
  630. * negative value on error. */
  631. ssize_t
  632. hs_cell_parse_intro_established(const uint8_t *payload, size_t payload_len)
  633. {
  634. ssize_t ret;
  635. trn_cell_intro_established_t *cell = NULL;
  636. tor_assert(payload);
  637. /* Try to parse the payload into a cell making sure we do actually have a
  638. * valid cell. */
  639. ret = trn_cell_intro_established_parse(&cell, payload, payload_len);
  640. if (ret >= 0) {
  641. /* On success, we do not keep the cell, we just notify the caller that it
  642. * was successfully parsed. */
  643. trn_cell_intro_established_free(cell);
  644. }
  645. return ret;
  646. }
  647. /* Parse the INTRODUCE2 cell using data which contains everything we need to
  648. * do so and contains the destination buffers of information we extract and
  649. * compute from the cell. Return 0 on success else a negative value. The
  650. * service and circ are only used for logging purposes. */
  651. ssize_t
  652. hs_cell_parse_introduce2(hs_cell_introduce2_data_t *data,
  653. const origin_circuit_t *circ,
  654. const hs_service_t *service)
  655. {
  656. int ret = -1;
  657. time_t elapsed;
  658. uint8_t *decrypted = NULL;
  659. size_t encrypted_section_len;
  660. const uint8_t *encrypted_section;
  661. trn_cell_introduce1_t *cell = NULL;
  662. trn_cell_introduce_encrypted_t *enc_cell = NULL;
  663. hs_ntor_intro_cell_keys_t *intro_keys = NULL;
  664. tor_assert(data);
  665. tor_assert(circ);
  666. tor_assert(service);
  667. /* Parse the cell into a decoded data structure pointed by cell_ptr. */
  668. if (parse_introduce2_cell(service, circ, data->payload, data->payload_len,
  669. &cell) < 0) {
  670. goto done;
  671. }
  672. log_info(LD_REND, "Received a decodable INTRODUCE2 cell on circuit %u "
  673. "for service %s. Decoding encrypted section...",
  674. TO_CIRCUIT(circ)->n_circ_id,
  675. safe_str_client(service->onion_address));
  676. encrypted_section = trn_cell_introduce1_getconstarray_encrypted(cell);
  677. encrypted_section_len = trn_cell_introduce1_getlen_encrypted(cell);
  678. /* Encrypted section must at least contain the CLIENT_PK and MAC which is
  679. * defined in section 3.3.2 of the specification. */
  680. if (encrypted_section_len < (CURVE25519_PUBKEY_LEN + DIGEST256_LEN)) {
  681. log_info(LD_REND, "Invalid INTRODUCE2 encrypted section length "
  682. "for service %s. Dropping cell.",
  683. safe_str_client(service->onion_address));
  684. goto done;
  685. }
  686. /* Check our replay cache for this introduction point. */
  687. if (replaycache_add_test_and_elapsed(data->replay_cache, encrypted_section,
  688. encrypted_section_len, &elapsed)) {
  689. log_warn(LD_REND, "Possible replay detected! An INTRODUCE2 cell with the"
  690. "same ENCRYPTED section was seen %ld seconds ago. "
  691. "Dropping cell.", (long int) elapsed);
  692. goto done;
  693. }
  694. /* Build the key material out of the key material found in the cell. */
  695. intro_keys = get_introduce2_key_material(data->auth_pk, data->enc_kp,
  696. data->subcredential,
  697. encrypted_section,
  698. &data->client_pk);
  699. if (intro_keys == NULL) {
  700. log_info(LD_REND, "Invalid INTRODUCE2 encrypted data. Unable to "
  701. "compute key material on circuit %u for service %s",
  702. TO_CIRCUIT(circ)->n_circ_id,
  703. safe_str_client(service->onion_address));
  704. goto done;
  705. }
  706. /* Validate MAC from the cell and our computed key material. The MAC field
  707. * in the cell is at the end of the encrypted section. */
  708. {
  709. uint8_t mac[DIGEST256_LEN];
  710. /* The MAC field is at the very end of the ENCRYPTED section. */
  711. size_t mac_offset = encrypted_section_len - sizeof(mac);
  712. /* Compute the MAC. Use the entire encoded payload with a length up to the
  713. * ENCRYPTED section. */
  714. compute_introduce_mac(data->payload,
  715. data->payload_len - encrypted_section_len,
  716. encrypted_section, encrypted_section_len,
  717. intro_keys->mac_key, sizeof(intro_keys->mac_key),
  718. mac, sizeof(mac));
  719. if (tor_memcmp(mac, encrypted_section + mac_offset, sizeof(mac))) {
  720. log_info(LD_REND, "Invalid MAC validation for INTRODUCE2 cell on "
  721. "circuit %u for service %s",
  722. TO_CIRCUIT(circ)->n_circ_id,
  723. safe_str_client(service->onion_address));
  724. goto done;
  725. }
  726. }
  727. {
  728. /* The ENCRYPTED_DATA section starts just after the CLIENT_PK. */
  729. const uint8_t *encrypted_data =
  730. encrypted_section + sizeof(data->client_pk);
  731. /* It's symmetric encryption so it's correct to use the ENCRYPTED length
  732. * for decryption. Computes the length of ENCRYPTED_DATA meaning removing
  733. * the CLIENT_PK and MAC length. */
  734. size_t encrypted_data_len =
  735. encrypted_section_len - (sizeof(data->client_pk) + DIGEST256_LEN);
  736. /* This decrypts the ENCRYPTED_DATA section of the cell. */
  737. decrypted = decrypt_introduce2(intro_keys->enc_key,
  738. encrypted_data, encrypted_data_len);
  739. if (decrypted == NULL) {
  740. log_info(LD_REND, "Unable to decrypt the ENCRYPTED section of an "
  741. "INTRODUCE2 cell on circuit %u for service %s",
  742. TO_CIRCUIT(circ)->n_circ_id,
  743. safe_str_client(service->onion_address));
  744. goto done;
  745. }
  746. /* Parse this blob into an encrypted cell structure so we can then extract
  747. * the data we need out of it. */
  748. enc_cell = parse_introduce2_encrypted(decrypted, encrypted_data_len,
  749. circ, service);
  750. memwipe(decrypted, 0, encrypted_data_len);
  751. if (enc_cell == NULL) {
  752. goto done;
  753. }
  754. }
  755. /* XXX: Implement client authorization checks. */
  756. /* Extract onion key and rendezvous cookie from the cell used for the
  757. * rendezvous point circuit e2e encryption. */
  758. memcpy(data->onion_pk.public_key,
  759. trn_cell_introduce_encrypted_getconstarray_onion_key(enc_cell),
  760. CURVE25519_PUBKEY_LEN);
  761. memcpy(data->rendezvous_cookie,
  762. trn_cell_introduce_encrypted_getconstarray_rend_cookie(enc_cell),
  763. sizeof(data->rendezvous_cookie));
  764. /* Extract rendezvous link specifiers. */
  765. for (size_t idx = 0;
  766. idx < trn_cell_introduce_encrypted_get_nspec(enc_cell); idx++) {
  767. link_specifier_t *lspec =
  768. trn_cell_introduce_encrypted_get_nspecs(enc_cell, idx);
  769. if (BUG(!lspec)) {
  770. goto done;
  771. }
  772. link_specifier_t *lspec_dup = link_specifier_dup(lspec);
  773. if (BUG(!lspec_dup)) {
  774. goto done;
  775. }
  776. smartlist_add(data->link_specifiers, lspec_dup);
  777. }
  778. /* Success. */
  779. ret = 0;
  780. log_info(LD_REND, "Valid INTRODUCE2 cell. Launching rendezvous circuit.");
  781. done:
  782. if (intro_keys) {
  783. memwipe(intro_keys, 0, sizeof(hs_ntor_intro_cell_keys_t));
  784. tor_free(intro_keys);
  785. }
  786. tor_free(decrypted);
  787. trn_cell_introduce_encrypted_free(enc_cell);
  788. trn_cell_introduce1_free(cell);
  789. return ret;
  790. }
  791. /* Build a RENDEZVOUS1 cell with the given rendezvous cookie and handshake
  792. * info. The encoded cell is put in cell_out and the length of the data is
  793. * returned. This can't fail. */
  794. ssize_t
  795. hs_cell_build_rendezvous1(const uint8_t *rendezvous_cookie,
  796. size_t rendezvous_cookie_len,
  797. const uint8_t *rendezvous_handshake_info,
  798. size_t rendezvous_handshake_info_len,
  799. uint8_t *cell_out)
  800. {
  801. ssize_t cell_len;
  802. trn_cell_rendezvous1_t *cell;
  803. tor_assert(rendezvous_cookie);
  804. tor_assert(rendezvous_handshake_info);
  805. tor_assert(cell_out);
  806. cell = trn_cell_rendezvous1_new();
  807. /* Set the RENDEZVOUS_COOKIE. */
  808. memcpy(trn_cell_rendezvous1_getarray_rendezvous_cookie(cell),
  809. rendezvous_cookie, rendezvous_cookie_len);
  810. /* Set the HANDSHAKE_INFO. */
  811. trn_cell_rendezvous1_setlen_handshake_info(cell,
  812. rendezvous_handshake_info_len);
  813. memcpy(trn_cell_rendezvous1_getarray_handshake_info(cell),
  814. rendezvous_handshake_info, rendezvous_handshake_info_len);
  815. /* Encoding. */
  816. cell_len = trn_cell_rendezvous1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
  817. tor_assert(cell_len > 0);
  818. trn_cell_rendezvous1_free(cell);
  819. return cell_len;
  820. }
  821. /* Build an INTRODUCE1 cell from the given data. The encoded cell is put in
  822. * cell_out which must be of at least size RELAY_PAYLOAD_SIZE. On success, the
  823. * encoded length is returned else a negative value and the content of
  824. * cell_out should be ignored. */
  825. ssize_t
  826. hs_cell_build_introduce1(const hs_cell_introduce1_data_t *data,
  827. uint8_t *cell_out)
  828. {
  829. ssize_t cell_len;
  830. trn_cell_introduce1_t *cell;
  831. trn_cell_extension_t *ext;
  832. tor_assert(data);
  833. tor_assert(cell_out);
  834. cell = trn_cell_introduce1_new();
  835. tor_assert(cell);
  836. /* Set extension data. None are used. */
  837. ext = trn_cell_extension_new();
  838. tor_assert(ext);
  839. trn_cell_extension_set_num(ext, 0);
  840. trn_cell_introduce1_set_extensions(cell, ext);
  841. /* Set the legacy ID field. */
  842. introduce1_set_legacy_id(cell, data);
  843. /* Set the authentication key. */
  844. introduce1_set_auth_key(cell, data);
  845. /* Set the encrypted section. This will set, encrypt and encode the
  846. * ENCRYPTED section in the cell. After this, we'll be ready to encode. */
  847. introduce1_set_encrypted(cell, data);
  848. /* Final encoding. */
  849. cell_len = trn_cell_introduce1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
  850. trn_cell_introduce1_free(cell);
  851. return cell_len;
  852. }
  853. /* Build an ESTABLISH_RENDEZVOUS cell from the given rendezvous_cookie. The
  854. * encoded cell is put in cell_out which must be of at least
  855. * RELAY_PAYLOAD_SIZE. On success, the encoded length is returned and the
  856. * caller should clear up the content of the cell.
  857. *
  858. * This function can't fail. */
  859. ssize_t
  860. hs_cell_build_establish_rendezvous(const uint8_t *rendezvous_cookie,
  861. uint8_t *cell_out)
  862. {
  863. tor_assert(rendezvous_cookie);
  864. tor_assert(cell_out);
  865. memcpy(cell_out, rendezvous_cookie, HS_REND_COOKIE_LEN);
  866. return HS_REND_COOKIE_LEN;
  867. }
  868. /* Handle an INTRODUCE_ACK cell encoded in payload of length payload_len.
  869. * Return the status code on success else a negative value if the cell as not
  870. * decodable. */
  871. int
  872. hs_cell_parse_introduce_ack(const uint8_t *payload, size_t payload_len)
  873. {
  874. int ret = -1;
  875. trn_cell_introduce_ack_t *cell = NULL;
  876. tor_assert(payload);
  877. /* If it is a legacy IP, rend-spec.txt specifies that a ACK is 0 byte and a
  878. * NACK is 1 byte. We can't use the legacy function for this so we have to
  879. * do a special case. */
  880. if (payload_len <= 1) {
  881. if (payload_len == 0) {
  882. ret = TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS;
  883. } else {
  884. ret = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
  885. }
  886. goto end;
  887. }
  888. if (trn_cell_introduce_ack_parse(&cell, payload, payload_len) < 0) {
  889. log_info(LD_REND, "Invalid INTRODUCE_ACK cell. Unable to parse it.");
  890. goto end;
  891. }
  892. ret = trn_cell_introduce_ack_get_status(cell);
  893. end:
  894. trn_cell_introduce_ack_free(cell);
  895. return ret;
  896. }
  897. /* Handle a RENDEZVOUS2 cell encoded in payload of length payload_len. On
  898. * success, handshake_info contains the data in the HANDSHAKE_INFO field, and
  899. * 0 is returned. On error, a negative value is returned. */
  900. int
  901. hs_cell_parse_rendezvous2(const uint8_t *payload, size_t payload_len,
  902. uint8_t *handshake_info, size_t handshake_info_len)
  903. {
  904. int ret = -1;
  905. trn_cell_rendezvous2_t *cell = NULL;
  906. tor_assert(payload);
  907. tor_assert(handshake_info);
  908. if (trn_cell_rendezvous2_parse(&cell, payload, payload_len) < 0) {
  909. log_info(LD_REND, "Invalid RENDEZVOUS2 cell. Unable to parse it.");
  910. goto end;
  911. }
  912. /* Static size, we should never have an issue with this else we messed up
  913. * our code flow. */
  914. tor_assert(trn_cell_rendezvous2_getlen_handshake_info(cell) ==
  915. handshake_info_len);
  916. memcpy(handshake_info,
  917. trn_cell_rendezvous2_getconstarray_handshake_info(cell),
  918. handshake_info_len);
  919. ret = 0;
  920. end:
  921. trn_cell_rendezvous2_free(cell);
  922. return ret;
  923. }
  924. /* Clear the given INTRODUCE1 data structure data. */
  925. void
  926. hs_cell_introduce1_data_clear(hs_cell_introduce1_data_t *data)
  927. {
  928. if (data == NULL) {
  929. return;
  930. }
  931. /* Object in this list have been moved to the cell object when building it
  932. * so they've been freed earlier. We do that in order to avoid duplicating
  933. * them leading to more memory and CPU time being used for nothing. */
  934. smartlist_free(data->link_specifiers);
  935. /* The data object has no ownership of any members. */
  936. memwipe(data, 0, sizeof(hs_cell_introduce1_data_t));
  937. }