hs_cell.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /* Copyright (c) 2017, 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 "or.h"
  8. #include "config.h"
  9. #include "rendservice.h"
  10. #include "replaycache.h"
  11. #include "hs_cell.h"
  12. #include "hs_ntor.h"
  13. /* Trunnel. */
  14. #include "ed25519_cert.h"
  15. #include "hs/cell_common.h"
  16. #include "hs/cell_establish_intro.h"
  17. #include "hs/cell_introduce1.h"
  18. #include "hs/cell_rendezvous.h"
  19. /* Compute the MAC of an INTRODUCE cell in mac_out. The encoded_cell param is
  20. * the cell content up to the ENCRYPTED section of length encoded_cell_len.
  21. * The encrypted param is the start of the ENCRYPTED section of length
  22. * encrypted_len. The mac_key is the key needed for the computation of the MAC
  23. * derived from the ntor handshake of length mac_key_len.
  24. *
  25. * The length mac_out_len must be at least DIGEST256_LEN. */
  26. static void
  27. compute_introduce_mac(const uint8_t *encoded_cell, size_t encoded_cell_len,
  28. const uint8_t *encrypted, size_t encrypted_len,
  29. const uint8_t *mac_key, size_t mac_key_len,
  30. uint8_t *mac_out, size_t mac_out_len)
  31. {
  32. size_t offset = 0;
  33. size_t mac_msg_len;
  34. uint8_t mac_msg[RELAY_PAYLOAD_SIZE] = {0};
  35. tor_assert(encoded_cell);
  36. tor_assert(encrypted);
  37. tor_assert(mac_key);
  38. tor_assert(mac_out);
  39. tor_assert(mac_out_len >= DIGEST256_LEN);
  40. /* Compute the size of the message which is basically the entire cell until
  41. * the MAC field of course. */
  42. mac_msg_len = encoded_cell_len + (encrypted_len - DIGEST256_LEN);
  43. tor_assert(mac_msg_len <= sizeof(mac_msg));
  44. /* First, put the encoded cell in the msg. */
  45. memcpy(mac_msg, encoded_cell, encoded_cell_len);
  46. offset += encoded_cell_len;
  47. /* Second, put the CLIENT_PK + ENCRYPTED_DATA but ommit the MAC field (which
  48. * is junk at this point). */
  49. memcpy(mac_msg + offset, encrypted, (encrypted_len - DIGEST256_LEN));
  50. offset += (encrypted_len - DIGEST256_LEN);
  51. tor_assert(offset == mac_msg_len);
  52. crypto_mac_sha3_256(mac_out, mac_out_len,
  53. mac_key, mac_key_len,
  54. mac_msg, mac_msg_len);
  55. memwipe(mac_msg, 0, sizeof(mac_msg));
  56. }
  57. /* From a set of keys, subcredential and the ENCRYPTED section of an
  58. * INTRODUCE2 cell, return a newly allocated intro cell keys structure.
  59. * Finally, the client public key is copied in client_pk. On error, return
  60. * NULL. */
  61. static hs_ntor_intro_cell_keys_t *
  62. get_introduce2_key_material(const ed25519_public_key_t *auth_key,
  63. const curve25519_keypair_t *enc_key,
  64. const uint8_t *subcredential,
  65. const uint8_t *encrypted_section,
  66. curve25519_public_key_t *client_pk)
  67. {
  68. hs_ntor_intro_cell_keys_t *keys;
  69. tor_assert(auth_key);
  70. tor_assert(enc_key);
  71. tor_assert(subcredential);
  72. tor_assert(encrypted_section);
  73. tor_assert(client_pk);
  74. keys = tor_malloc_zero(sizeof(*keys));
  75. /* First bytes of the ENCRYPTED section are the client public key. */
  76. memcpy(client_pk->public_key, encrypted_section, CURVE25519_PUBKEY_LEN);
  77. if (hs_ntor_service_get_introduce1_keys(auth_key, enc_key, client_pk,
  78. subcredential, keys) < 0) {
  79. /* Don't rely on the caller to wipe this on error. */
  80. memwipe(client_pk, 0, sizeof(curve25519_public_key_t));
  81. tor_free(keys);
  82. keys = NULL;
  83. }
  84. return keys;
  85. }
  86. /* Using the given encryption key, decrypt the encrypted_section of length
  87. * encrypted_section_len of an INTRODUCE2 cell and return a newly allocated
  88. * buffer containing the decrypted data. On decryption failure, NULL is
  89. * returned. */
  90. static uint8_t *
  91. decrypt_introduce2(const uint8_t *enc_key, const uint8_t *encrypted_section,
  92. size_t encrypted_section_len)
  93. {
  94. uint8_t *decrypted = NULL;
  95. crypto_cipher_t *cipher = NULL;
  96. tor_assert(enc_key);
  97. tor_assert(encrypted_section);
  98. /* Decrypt ENCRYPTED section. */
  99. cipher = crypto_cipher_new_with_bits((char *) enc_key,
  100. CURVE25519_PUBKEY_LEN * 8);
  101. tor_assert(cipher);
  102. /* This is symmetric encryption so can't be bigger than the encrypted
  103. * section length. */
  104. decrypted = tor_malloc_zero(encrypted_section_len);
  105. if (crypto_cipher_decrypt(cipher, (char *) decrypted,
  106. (const char *) encrypted_section,
  107. encrypted_section_len) < 0) {
  108. tor_free(decrypted);
  109. decrypted = NULL;
  110. goto done;
  111. }
  112. done:
  113. crypto_cipher_free(cipher);
  114. return decrypted;
  115. }
  116. /* Given a pointer to the decrypted data of the ENCRYPTED section of an
  117. * INTRODUCE2 cell of length decrypted_len, parse and validate the cell
  118. * content. Return a newly allocated cell structure or NULL on error. The
  119. * circuit and service object are only used for logging purposes. */
  120. static trn_cell_introduce_encrypted_t *
  121. parse_introduce2_encrypted(const uint8_t *decrypted_data,
  122. size_t decrypted_len, const origin_circuit_t *circ,
  123. const hs_service_t *service)
  124. {
  125. trn_cell_introduce_encrypted_t *enc_cell = NULL;
  126. tor_assert(decrypted_data);
  127. tor_assert(circ);
  128. tor_assert(service);
  129. if (trn_cell_introduce_encrypted_parse(&enc_cell, decrypted_data,
  130. decrypted_len) < 0) {
  131. log_info(LD_REND, "Unable to parse the decrypted ENCRYPTED section of "
  132. "the INTRODUCE2 cell on circuit %u for service %s",
  133. TO_CIRCUIT(circ)->n_circ_id,
  134. safe_str_client(service->onion_address));
  135. goto err;
  136. }
  137. if (trn_cell_introduce_encrypted_get_onion_key_type(enc_cell) !=
  138. HS_CELL_ONION_KEY_TYPE_NTOR) {
  139. log_info(LD_REND, "INTRODUCE2 onion key type is invalid. Got %u but "
  140. "expected %u on circuit %u for service %s",
  141. trn_cell_introduce_encrypted_get_onion_key_type(enc_cell),
  142. HS_CELL_ONION_KEY_TYPE_NTOR, TO_CIRCUIT(circ)->n_circ_id,
  143. safe_str_client(service->onion_address));
  144. goto err;
  145. }
  146. if (trn_cell_introduce_encrypted_getlen_onion_key(enc_cell) !=
  147. CURVE25519_PUBKEY_LEN) {
  148. log_info(LD_REND, "INTRODUCE2 onion key length is invalid. Got %u but "
  149. "expected %d on circuit %u for service %s",
  150. (unsigned)trn_cell_introduce_encrypted_getlen_onion_key(enc_cell),
  151. CURVE25519_PUBKEY_LEN, TO_CIRCUIT(circ)->n_circ_id,
  152. safe_str_client(service->onion_address));
  153. goto err;
  154. }
  155. /* XXX: Validate NSPEC field as well. */
  156. return enc_cell;
  157. err:
  158. trn_cell_introduce_encrypted_free(enc_cell);
  159. return NULL;
  160. }
  161. /* Build a legacy ESTABLISH_INTRO cell with the given circuit nonce and RSA
  162. * encryption key. The encoded cell is put in cell_out that MUST at least be
  163. * of the size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on
  164. * success else a negative value and cell_out is untouched. */
  165. static ssize_t
  166. build_legacy_establish_intro(const char *circ_nonce, crypto_pk_t *enc_key,
  167. uint8_t *cell_out)
  168. {
  169. ssize_t cell_len;
  170. tor_assert(circ_nonce);
  171. tor_assert(enc_key);
  172. tor_assert(cell_out);
  173. memwipe(cell_out, 0, RELAY_PAYLOAD_SIZE);
  174. cell_len = rend_service_encode_establish_intro_cell((char*)cell_out,
  175. RELAY_PAYLOAD_SIZE,
  176. enc_key, circ_nonce);
  177. return cell_len;
  178. }
  179. /* Parse an INTRODUCE2 cell from payload of size payload_len for the given
  180. * service and circuit which are used only for logging purposes. The resulting
  181. * parsed cell is put in cell_ptr_out.
  182. *
  183. * This function only parses prop224 INTRODUCE2 cells even when the intro point
  184. * is a legacy intro point. That's because intro points don't actually care
  185. * about the contents of the introduce cell. Legacy INTRODUCE cells are only
  186. * used by the legacy system now.
  187. *
  188. * Return 0 on success else a negative value and cell_ptr_out is untouched. */
  189. static int
  190. parse_introduce2_cell(const hs_service_t *service,
  191. const origin_circuit_t *circ, const uint8_t *payload,
  192. size_t payload_len,
  193. trn_cell_introduce1_t **cell_ptr_out)
  194. {
  195. trn_cell_introduce1_t *cell = NULL;
  196. tor_assert(service);
  197. tor_assert(circ);
  198. tor_assert(payload);
  199. tor_assert(cell_ptr_out);
  200. /* Parse the cell so we can start cell validation. */
  201. if (trn_cell_introduce1_parse(&cell, payload, payload_len) < 0) {
  202. log_info(LD_PROTOCOL, "Unable to parse INTRODUCE2 cell on circuit %u "
  203. "for service %s",
  204. TO_CIRCUIT(circ)->n_circ_id,
  205. safe_str_client(service->onion_address));
  206. goto err;
  207. }
  208. /* Success. */
  209. *cell_ptr_out = cell;
  210. return 0;
  211. err:
  212. return -1;
  213. }
  214. /* ========== */
  215. /* Public API */
  216. /* ========== */
  217. /* Build an ESTABLISH_INTRO cell with the given circuit nonce and intro point
  218. * object. The encoded cell is put in cell_out that MUST at least be of the
  219. * size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on success else
  220. * a negative value and cell_out is untouched. This function also supports
  221. * legacy cell creation. */
  222. ssize_t
  223. hs_cell_build_establish_intro(const char *circ_nonce,
  224. const hs_service_intro_point_t *ip,
  225. uint8_t *cell_out)
  226. {
  227. ssize_t cell_len = -1;
  228. uint16_t sig_len = ED25519_SIG_LEN;
  229. trn_cell_extension_t *ext;
  230. trn_cell_establish_intro_t *cell = NULL;
  231. tor_assert(circ_nonce);
  232. tor_assert(ip);
  233. /* Quickly handle the legacy IP. */
  234. if (ip->base.is_only_legacy) {
  235. tor_assert(ip->legacy_key);
  236. cell_len = build_legacy_establish_intro(circ_nonce, ip->legacy_key,
  237. cell_out);
  238. tor_assert(cell_len <= RELAY_PAYLOAD_SIZE);
  239. /* Success or not we are done here. */
  240. goto done;
  241. }
  242. /* Set extension data. None used here. */
  243. ext = trn_cell_extension_new();
  244. trn_cell_extension_set_num(ext, 0);
  245. cell = trn_cell_establish_intro_new();
  246. trn_cell_establish_intro_set_extensions(cell, ext);
  247. /* Set signature size. Array is then allocated in the cell. We need to do
  248. * this early so we can use trunnel API to get the signature length. */
  249. trn_cell_establish_intro_set_sig_len(cell, sig_len);
  250. trn_cell_establish_intro_setlen_sig(cell, sig_len);
  251. /* Set AUTH_KEY_TYPE: 2 means ed25519 */
  252. trn_cell_establish_intro_set_auth_key_type(cell,
  253. HS_INTRO_AUTH_KEY_TYPE_ED25519);
  254. /* Set AUTH_KEY and AUTH_KEY_LEN field. Must also set byte-length of
  255. * AUTH_KEY to match */
  256. {
  257. uint16_t auth_key_len = ED25519_PUBKEY_LEN;
  258. trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
  259. trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
  260. /* We do this call _after_ setting the length because it's reallocated at
  261. * that point only. */
  262. uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
  263. memcpy(auth_key_ptr, ip->auth_key_kp.pubkey.pubkey, auth_key_len);
  264. }
  265. /* Calculate HANDSHAKE_AUTH field (MAC). */
  266. {
  267. ssize_t tmp_cell_enc_len = 0;
  268. ssize_t tmp_cell_mac_offset =
  269. sig_len + sizeof(cell->sig_len) +
  270. trn_cell_establish_intro_getlen_handshake_mac(cell);
  271. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0};
  272. uint8_t mac[TRUNNEL_SHA3_256_LEN], *handshake_ptr;
  273. /* We first encode the current fields we have in the cell so we can
  274. * compute the MAC using the raw bytes. */
  275. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  276. sizeof(tmp_cell_enc),
  277. cell);
  278. if (BUG(tmp_cell_enc_len < 0)) {
  279. goto done;
  280. }
  281. /* Sanity check. */
  282. tor_assert(tmp_cell_enc_len > tmp_cell_mac_offset);
  283. /* Circuit nonce is always DIGEST_LEN according to tor-spec.txt. */
  284. crypto_mac_sha3_256(mac, sizeof(mac),
  285. (uint8_t *) circ_nonce, DIGEST_LEN,
  286. tmp_cell_enc, tmp_cell_enc_len - tmp_cell_mac_offset);
  287. handshake_ptr = trn_cell_establish_intro_getarray_handshake_mac(cell);
  288. memcpy(handshake_ptr, mac, sizeof(mac));
  289. memwipe(mac, 0, sizeof(mac));
  290. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  291. }
  292. /* Calculate the cell signature SIG. */
  293. {
  294. ssize_t tmp_cell_enc_len = 0;
  295. ssize_t tmp_cell_sig_offset = (sig_len + sizeof(cell->sig_len));
  296. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0}, *sig_ptr;
  297. ed25519_signature_t sig;
  298. /* We first encode the current fields we have in the cell so we can
  299. * compute the signature from the raw bytes of the cell. */
  300. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  301. sizeof(tmp_cell_enc),
  302. cell);
  303. if (BUG(tmp_cell_enc_len < 0)) {
  304. goto done;
  305. }
  306. if (ed25519_sign_prefixed(&sig, tmp_cell_enc,
  307. tmp_cell_enc_len - tmp_cell_sig_offset,
  308. ESTABLISH_INTRO_SIG_PREFIX, &ip->auth_key_kp)) {
  309. log_warn(LD_BUG, "Unable to make signature for ESTABLISH_INTRO cell.");
  310. goto done;
  311. }
  312. /* Copy the signature into the cell. */
  313. sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
  314. memcpy(sig_ptr, sig.sig, sig_len);
  315. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  316. }
  317. /* Encode the cell. Can't be bigger than a standard cell. */
  318. cell_len = trn_cell_establish_intro_encode(cell_out, RELAY_PAYLOAD_SIZE,
  319. cell);
  320. done:
  321. trn_cell_establish_intro_free(cell);
  322. return cell_len;
  323. }
  324. /* Parse the INTRO_ESTABLISHED cell in the payload of size payload_len. If we
  325. * are successful at parsing it, return the length of the parsed cell else a
  326. * negative value on error. */
  327. ssize_t
  328. hs_cell_parse_intro_established(const uint8_t *payload, size_t payload_len)
  329. {
  330. ssize_t ret;
  331. trn_cell_intro_established_t *cell = NULL;
  332. tor_assert(payload);
  333. /* Try to parse the payload into a cell making sure we do actually have a
  334. * valid cell. */
  335. ret = trn_cell_intro_established_parse(&cell, payload, payload_len);
  336. if (ret >= 0) {
  337. /* On success, we do not keep the cell, we just notify the caller that it
  338. * was successfully parsed. */
  339. trn_cell_intro_established_free(cell);
  340. }
  341. return ret;
  342. }
  343. /* Parsse the INTRODUCE2 cell using data which contains everything we need to
  344. * do so and contains the destination buffers of information we extract and
  345. * compute from the cell. Return 0 on success else a negative value. The
  346. * service and circ are only used for logging purposes. */
  347. ssize_t
  348. hs_cell_parse_introduce2(hs_cell_introduce2_data_t *data,
  349. const origin_circuit_t *circ,
  350. const hs_service_t *service)
  351. {
  352. int ret = -1;
  353. time_t elapsed;
  354. uint8_t *decrypted = NULL;
  355. size_t encrypted_section_len;
  356. const uint8_t *encrypted_section;
  357. trn_cell_introduce1_t *cell = NULL;
  358. trn_cell_introduce_encrypted_t *enc_cell = NULL;
  359. hs_ntor_intro_cell_keys_t *intro_keys = NULL;
  360. tor_assert(data);
  361. tor_assert(circ);
  362. tor_assert(service);
  363. /* Parse the cell into a decoded data structure pointed by cell_ptr. */
  364. if (parse_introduce2_cell(service, circ, data->payload, data->payload_len,
  365. &cell) < 0) {
  366. goto done;
  367. }
  368. log_info(LD_REND, "Received a decodable INTRODUCE2 cell on circuit %u "
  369. "for service %s. Decoding encrypted section...",
  370. TO_CIRCUIT(circ)->n_circ_id,
  371. safe_str_client(service->onion_address));
  372. encrypted_section = trn_cell_introduce1_getconstarray_encrypted(cell);
  373. encrypted_section_len = trn_cell_introduce1_getlen_encrypted(cell);
  374. /* Encrypted section must at least contain the CLIENT_PK and MAC which is
  375. * defined in section 3.3.2 of the specification. */
  376. if (encrypted_section_len < (CURVE25519_PUBKEY_LEN + DIGEST256_LEN)) {
  377. log_info(LD_REND, "Invalid INTRODUCE2 encrypted section length "
  378. "for service %s. Dropping cell.",
  379. safe_str_client(service->onion_address));
  380. goto done;
  381. }
  382. /* Check our replay cache for this introduction point. */
  383. if (replaycache_add_test_and_elapsed(data->replay_cache, encrypted_section,
  384. encrypted_section_len, &elapsed)) {
  385. log_warn(LD_REND, "Possible replay detected! An INTRODUCE2 cell with the"
  386. "same ENCRYPTED section was seen %ld seconds ago. "
  387. "Dropping cell.", elapsed);
  388. goto done;
  389. }
  390. /* Build the key material out of the key material found in the cell. */
  391. intro_keys = get_introduce2_key_material(data->auth_pk, data->enc_kp,
  392. data->subcredential,
  393. encrypted_section,
  394. &data->client_pk);
  395. if (intro_keys == NULL) {
  396. log_info(LD_REND, "Invalid INTRODUCE2 encrypted data. Unable to "
  397. "compute key material on circuit %u for service %s",
  398. TO_CIRCUIT(circ)->n_circ_id,
  399. safe_str_client(service->onion_address));
  400. goto done;
  401. }
  402. /* Validate MAC from the cell and our computed key material. The MAC field
  403. * in the cell is at the end of the encrypted section. */
  404. {
  405. uint8_t mac[DIGEST256_LEN];
  406. /* The MAC field is at the very end of the ENCRYPTED section. */
  407. size_t mac_offset = encrypted_section_len - sizeof(mac);
  408. /* Compute the MAC. Use the entire encoded payload with a length up to the
  409. * ENCRYPTED section. */
  410. compute_introduce_mac(data->payload,
  411. data->payload_len - encrypted_section_len,
  412. encrypted_section, encrypted_section_len,
  413. intro_keys->mac_key, sizeof(intro_keys->mac_key),
  414. mac, sizeof(mac));
  415. if (tor_memcmp(mac, encrypted_section + mac_offset, sizeof(mac))) {
  416. log_info(LD_REND, "Invalid MAC validation for INTRODUCE2 cell on "
  417. "circuit %u for service %s",
  418. TO_CIRCUIT(circ)->n_circ_id,
  419. safe_str_client(service->onion_address));
  420. goto done;
  421. }
  422. }
  423. {
  424. /* The ENCRYPTED_DATA section starts just after the CLIENT_PK. */
  425. const uint8_t *encrypted_data =
  426. encrypted_section + sizeof(data->client_pk);
  427. /* It's symmetric encryption so it's correct to use the ENCRYPTED length
  428. * for decryption. Computes the length of ENCRYPTED_DATA meaning removing
  429. * the CLIENT_PK and MAC length. */
  430. size_t encrypted_data_len =
  431. encrypted_section_len - (sizeof(data->client_pk) + DIGEST256_LEN);
  432. /* This decrypts the ENCRYPTED_DATA section of the cell. */
  433. decrypted = decrypt_introduce2(intro_keys->enc_key,
  434. encrypted_data, encrypted_data_len);
  435. if (decrypted == NULL) {
  436. log_info(LD_REND, "Unable to decrypt the ENCRYPTED section of an "
  437. "INTRODUCE2 cell on circuit %u for service %s",
  438. TO_CIRCUIT(circ)->n_circ_id,
  439. safe_str_client(service->onion_address));
  440. goto done;
  441. }
  442. /* Parse this blob into an encrypted cell structure so we can then extract
  443. * the data we need out of it. */
  444. enc_cell = parse_introduce2_encrypted(decrypted, encrypted_data_len,
  445. circ, service);
  446. memwipe(decrypted, 0, encrypted_data_len);
  447. if (enc_cell == NULL) {
  448. goto done;
  449. }
  450. }
  451. /* XXX: Implement client authorization checks. */
  452. /* Extract onion key and rendezvous cookie from the cell used for the
  453. * rendezvous point circuit e2e encryption. */
  454. memcpy(data->onion_pk.public_key,
  455. trn_cell_introduce_encrypted_getconstarray_onion_key(enc_cell),
  456. CURVE25519_PUBKEY_LEN);
  457. memcpy(data->rendezvous_cookie,
  458. trn_cell_introduce_encrypted_getconstarray_rend_cookie(enc_cell),
  459. sizeof(data->rendezvous_cookie));
  460. /* Extract rendezvous link specifiers. */
  461. for (size_t idx = 0;
  462. idx < trn_cell_introduce_encrypted_get_nspec(enc_cell); idx++) {
  463. link_specifier_t *lspec =
  464. trn_cell_introduce_encrypted_get_nspecs(enc_cell, idx);
  465. smartlist_add(data->link_specifiers, hs_link_specifier_dup(lspec));
  466. }
  467. /* Success. */
  468. ret = 0;
  469. log_info(LD_REND, "Valid INTRODUCE2 cell. Launching rendezvous circuit.");
  470. done:
  471. if (intro_keys) {
  472. memwipe(intro_keys, 0, sizeof(hs_ntor_intro_cell_keys_t));
  473. tor_free(intro_keys);
  474. }
  475. tor_free(decrypted);
  476. trn_cell_introduce_encrypted_free(enc_cell);
  477. trn_cell_introduce1_free(cell);
  478. return ret;
  479. }
  480. /* Build a RENDEZVOUS1 cell with the given rendezvous cookie and handshake
  481. * info. The encoded cell is put in cell_out and the length of the data is
  482. * returned. This can't fail. */
  483. ssize_t
  484. hs_cell_build_rendezvous1(const uint8_t *rendezvous_cookie,
  485. size_t rendezvous_cookie_len,
  486. const uint8_t *rendezvous_handshake_info,
  487. size_t rendezvous_handshake_info_len,
  488. uint8_t *cell_out)
  489. {
  490. ssize_t cell_len;
  491. trn_cell_rendezvous1_t *cell;
  492. tor_assert(rendezvous_cookie);
  493. tor_assert(rendezvous_handshake_info);
  494. tor_assert(cell_out);
  495. cell = trn_cell_rendezvous1_new();
  496. /* Set the RENDEZVOUS_COOKIE. */
  497. memcpy(trn_cell_rendezvous1_getarray_rendezvous_cookie(cell),
  498. rendezvous_cookie, rendezvous_cookie_len);
  499. /* Set the HANDSHAKE_INFO. */
  500. trn_cell_rendezvous1_setlen_handshake_info(cell,
  501. rendezvous_handshake_info_len);
  502. memcpy(trn_cell_rendezvous1_getarray_handshake_info(cell),
  503. rendezvous_handshake_info, rendezvous_handshake_info_len);
  504. /* Encoding. */
  505. cell_len = trn_cell_rendezvous1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
  506. tor_assert(cell_len > 0);
  507. trn_cell_rendezvous1_free(cell);
  508. return cell_len;
  509. }