hs_cell.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. * Return 0 on success else a negative value and cell_ptr_out is untouched. */
  184. static int
  185. parse_introduce2_cell(const hs_service_t *service,
  186. const origin_circuit_t *circ, const uint8_t *payload,
  187. size_t payload_len,
  188. trn_cell_introduce1_t **cell_ptr_out)
  189. {
  190. trn_cell_introduce1_t *cell = NULL;
  191. tor_assert(service);
  192. tor_assert(circ);
  193. tor_assert(payload);
  194. tor_assert(cell_ptr_out);
  195. /* Parse the cell so we can start cell validation. */
  196. if (trn_cell_introduce1_parse(&cell, payload, payload_len) < 0) {
  197. log_info(LD_PROTOCOL, "Unable to parse INTRODUCE2 cell on circuit %u "
  198. "for service %s",
  199. TO_CIRCUIT(circ)->n_circ_id,
  200. safe_str_client(service->onion_address));
  201. goto err;
  202. }
  203. /* Success. */
  204. *cell_ptr_out = cell;
  205. return 0;
  206. err:
  207. return -1;
  208. }
  209. /* ========== */
  210. /* Public API */
  211. /* ========== */
  212. /* Build an ESTABLISH_INTRO cell with the given circuit nonce and intro point
  213. * object. The encoded cell is put in cell_out that MUST at least be of the
  214. * size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on success else
  215. * a negative value and cell_out is untouched. This function also supports
  216. * legacy cell creation. */
  217. ssize_t
  218. hs_cell_build_establish_intro(const char *circ_nonce,
  219. const hs_service_intro_point_t *ip,
  220. uint8_t *cell_out)
  221. {
  222. ssize_t cell_len = -1;
  223. uint16_t sig_len = ED25519_SIG_LEN;
  224. trn_cell_extension_t *ext;
  225. trn_cell_establish_intro_t *cell = NULL;
  226. tor_assert(circ_nonce);
  227. tor_assert(ip);
  228. /* Quickly handle the legacy IP. */
  229. if (ip->base.is_only_legacy) {
  230. tor_assert(ip->legacy_key);
  231. cell_len = build_legacy_establish_intro(circ_nonce, ip->legacy_key,
  232. cell_out);
  233. tor_assert(cell_len <= RELAY_PAYLOAD_SIZE);
  234. /* Success or not we are done here. */
  235. goto done;
  236. }
  237. /* Set extension data. None used here. */
  238. ext = trn_cell_extension_new();
  239. trn_cell_extension_set_num(ext, 0);
  240. cell = trn_cell_establish_intro_new();
  241. trn_cell_establish_intro_set_extensions(cell, ext);
  242. /* Set signature size. Array is then allocated in the cell. We need to do
  243. * this early so we can use trunnel API to get the signature length. */
  244. trn_cell_establish_intro_set_sig_len(cell, sig_len);
  245. trn_cell_establish_intro_setlen_sig(cell, sig_len);
  246. /* Set AUTH_KEY_TYPE: 2 means ed25519 */
  247. trn_cell_establish_intro_set_auth_key_type(cell,
  248. HS_INTRO_AUTH_KEY_TYPE_ED25519);
  249. /* Set AUTH_KEY and AUTH_KEY_LEN field. Must also set byte-length of
  250. * AUTH_KEY to match */
  251. {
  252. uint16_t auth_key_len = ED25519_PUBKEY_LEN;
  253. trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
  254. trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
  255. /* We do this call _after_ setting the length because it's reallocated at
  256. * that point only. */
  257. uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
  258. memcpy(auth_key_ptr, ip->auth_key_kp.pubkey.pubkey, auth_key_len);
  259. }
  260. /* Calculate HANDSHAKE_AUTH field (MAC). */
  261. {
  262. ssize_t tmp_cell_enc_len = 0;
  263. ssize_t tmp_cell_mac_offset =
  264. sig_len + sizeof(cell->sig_len) +
  265. trn_cell_establish_intro_getlen_handshake_mac(cell);
  266. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0};
  267. uint8_t mac[TRUNNEL_SHA3_256_LEN], *handshake_ptr;
  268. /* We first encode the current fields we have in the cell so we can
  269. * compute the MAC using the raw bytes. */
  270. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  271. sizeof(tmp_cell_enc),
  272. cell);
  273. if (BUG(tmp_cell_enc_len < 0)) {
  274. goto done;
  275. }
  276. /* Sanity check. */
  277. tor_assert(tmp_cell_enc_len > tmp_cell_mac_offset);
  278. /* Circuit nonce is always DIGEST_LEN according to tor-spec.txt. */
  279. crypto_mac_sha3_256(mac, sizeof(mac),
  280. (uint8_t *) circ_nonce, DIGEST_LEN,
  281. tmp_cell_enc, tmp_cell_enc_len - tmp_cell_mac_offset);
  282. handshake_ptr = trn_cell_establish_intro_getarray_handshake_mac(cell);
  283. memcpy(handshake_ptr, mac, sizeof(mac));
  284. memwipe(mac, 0, sizeof(mac));
  285. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  286. }
  287. /* Calculate the cell signature SIG. */
  288. {
  289. ssize_t tmp_cell_enc_len = 0;
  290. ssize_t tmp_cell_sig_offset = (sig_len + sizeof(cell->sig_len));
  291. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0}, *sig_ptr;
  292. ed25519_signature_t sig;
  293. /* We first encode the current fields we have in the cell so we can
  294. * compute the signature from the raw bytes of the cell. */
  295. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  296. sizeof(tmp_cell_enc),
  297. cell);
  298. if (BUG(tmp_cell_enc_len < 0)) {
  299. goto done;
  300. }
  301. if (ed25519_sign_prefixed(&sig, tmp_cell_enc,
  302. tmp_cell_enc_len - tmp_cell_sig_offset,
  303. ESTABLISH_INTRO_SIG_PREFIX, &ip->auth_key_kp)) {
  304. log_warn(LD_BUG, "Unable to make signature for ESTABLISH_INTRO cell.");
  305. goto done;
  306. }
  307. /* Copy the signature into the cell. */
  308. sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
  309. memcpy(sig_ptr, sig.sig, sig_len);
  310. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  311. }
  312. /* Encode the cell. Can't be bigger than a standard cell. */
  313. cell_len = trn_cell_establish_intro_encode(cell_out, RELAY_PAYLOAD_SIZE,
  314. cell);
  315. done:
  316. trn_cell_establish_intro_free(cell);
  317. return cell_len;
  318. }
  319. /* Parse the INTRO_ESTABLISHED cell in the payload of size payload_len. If we
  320. * are successful at parsing it, return the length of the parsed cell else a
  321. * negative value on error. */
  322. ssize_t
  323. hs_cell_parse_intro_established(const uint8_t *payload, size_t payload_len)
  324. {
  325. ssize_t ret;
  326. trn_cell_intro_established_t *cell = NULL;
  327. tor_assert(payload);
  328. /* Try to parse the payload into a cell making sure we do actually have a
  329. * valid cell. */
  330. ret = trn_cell_intro_established_parse(&cell, payload, payload_len);
  331. if (ret >= 0) {
  332. /* On success, we do not keep the cell, we just notify the caller that it
  333. * was successfully parsed. */
  334. trn_cell_intro_established_free(cell);
  335. }
  336. return ret;
  337. }
  338. /* Parsse the INTRODUCE2 cell using data which contains everything we need to
  339. * do so and contains the destination buffers of information we extract and
  340. * compute from the cell. Return 0 on success else a negative value. The
  341. * service and circ are only used for logging purposes. */
  342. ssize_t
  343. hs_cell_parse_introduce2(hs_cell_introduce2_data_t *data,
  344. const origin_circuit_t *circ,
  345. const hs_service_t *service)
  346. {
  347. int ret = -1;
  348. time_t elapsed;
  349. uint8_t *decrypted = NULL;
  350. size_t encrypted_section_len;
  351. const uint8_t *encrypted_section;
  352. trn_cell_introduce1_t *cell = NULL;
  353. trn_cell_introduce_encrypted_t *enc_cell = NULL;
  354. hs_ntor_intro_cell_keys_t *intro_keys = NULL;
  355. tor_assert(data);
  356. tor_assert(circ);
  357. tor_assert(service);
  358. /* Parse the cell into a decoded data structure pointed by cell_ptr. */
  359. if (parse_introduce2_cell(service, circ, data->payload, data->payload_len,
  360. &cell) < 0) {
  361. goto done;
  362. }
  363. log_info(LD_REND, "Received a decodable INTRODUCE2 cell on circuit %u "
  364. "for service %s. Decoding encrypted section...",
  365. TO_CIRCUIT(circ)->n_circ_id,
  366. safe_str_client(service->onion_address));
  367. encrypted_section = trn_cell_introduce1_getconstarray_encrypted(cell);
  368. encrypted_section_len = trn_cell_introduce1_getlen_encrypted(cell);
  369. /* Encrypted section must at least contain the CLIENT_PK and MAC which is
  370. * defined in section 3.3.2 of the specification. */
  371. if (encrypted_section_len < (CURVE25519_PUBKEY_LEN + DIGEST256_LEN)) {
  372. log_info(LD_REND, "Invalid INTRODUCE2 encrypted section length "
  373. "for service %s. Dropping cell.",
  374. safe_str_client(service->onion_address));
  375. goto done;
  376. }
  377. /* Check our replay cache for this introduction point. */
  378. if (replaycache_add_test_and_elapsed(data->replay_cache, encrypted_section,
  379. encrypted_section_len, &elapsed)) {
  380. log_warn(LD_REND, "Possible replay detected! An INTRODUCE2 cell with the"
  381. "same ENCRYPTED section was seen %ld seconds ago. "
  382. "Dropping cell.", elapsed);
  383. goto done;
  384. }
  385. /* Build the key material out of the key material found in the cell. */
  386. intro_keys = get_introduce2_key_material(data->auth_pk, data->enc_kp,
  387. data->subcredential,
  388. encrypted_section,
  389. &data->client_pk);
  390. if (intro_keys == NULL) {
  391. log_info(LD_REND, "Invalid INTRODUCE2 encrypted data. Unable to "
  392. "compute key material on circuit %u for service %s",
  393. TO_CIRCUIT(circ)->n_circ_id,
  394. safe_str_client(service->onion_address));
  395. goto done;
  396. }
  397. /* Validate MAC from the cell and our computed key material. The MAC field
  398. * in the cell is at the end of the encrypted section. */
  399. {
  400. uint8_t mac[DIGEST256_LEN];
  401. /* The MAC field is at the very end of the ENCRYPTED section. */
  402. size_t mac_offset = encrypted_section_len - sizeof(mac);
  403. /* Compute the MAC. Use the entire encoded payload with a length up to the
  404. * ENCRYPTED section. */
  405. compute_introduce_mac(data->payload,
  406. data->payload_len - encrypted_section_len,
  407. encrypted_section, encrypted_section_len,
  408. intro_keys->mac_key, sizeof(intro_keys->mac_key),
  409. mac, sizeof(mac));
  410. if (tor_memcmp(mac, encrypted_section + mac_offset, sizeof(mac))) {
  411. log_info(LD_REND, "Invalid MAC validation for INTRODUCE2 cell on "
  412. "circuit %u for service %s",
  413. TO_CIRCUIT(circ)->n_circ_id,
  414. safe_str_client(service->onion_address));
  415. goto done;
  416. }
  417. }
  418. {
  419. /* The ENCRYPTED_DATA section starts just after the CLIENT_PK. */
  420. const uint8_t *encrypted_data =
  421. encrypted_section + sizeof(data->client_pk);
  422. /* It's symmetric encryption so it's correct to use the ENCRYPTED length
  423. * for decryption. Computes the length of ENCRYPTED_DATA meaning removing
  424. * the CLIENT_PK and MAC length. */
  425. size_t encrypted_data_len =
  426. encrypted_section_len - (sizeof(data->client_pk) + DIGEST256_LEN);
  427. /* This decrypts the ENCRYPTED_DATA section of the cell. */
  428. decrypted = decrypt_introduce2(intro_keys->enc_key,
  429. encrypted_data, encrypted_data_len);
  430. if (decrypted == NULL) {
  431. log_info(LD_REND, "Unable to decrypt the ENCRYPTED section of an "
  432. "INTRODUCE2 cell on circuit %u for service %s",
  433. TO_CIRCUIT(circ)->n_circ_id,
  434. safe_str_client(service->onion_address));
  435. goto done;
  436. }
  437. /* Parse this blob into an encrypted cell structure so we can then extract
  438. * the data we need out of it. */
  439. enc_cell = parse_introduce2_encrypted(decrypted, encrypted_data_len,
  440. circ, service);
  441. memwipe(decrypted, 0, encrypted_data_len);
  442. if (enc_cell == NULL) {
  443. goto done;
  444. }
  445. }
  446. /* XXX: Implement client authorization checks. */
  447. /* Extract onion key and rendezvous cookie from the cell used for the
  448. * rendezvous point circuit e2e encryption. */
  449. memcpy(data->onion_pk.public_key,
  450. trn_cell_introduce_encrypted_getconstarray_onion_key(enc_cell),
  451. CURVE25519_PUBKEY_LEN);
  452. memcpy(data->rendezvous_cookie,
  453. trn_cell_introduce_encrypted_getconstarray_rend_cookie(enc_cell),
  454. sizeof(data->rendezvous_cookie));
  455. /* Extract rendezvous link specifiers. */
  456. for (size_t idx = 0;
  457. idx < trn_cell_introduce_encrypted_get_nspec(enc_cell); idx++) {
  458. link_specifier_t *lspec =
  459. trn_cell_introduce_encrypted_get_nspecs(enc_cell, idx);
  460. smartlist_add(data->link_specifiers, hs_link_specifier_dup(lspec));
  461. }
  462. /* Success. */
  463. ret = 0;
  464. log_info(LD_REND, "Valid INTRODUCE2 cell. Launching rendezvous circuit.");
  465. done:
  466. if (intro_keys) {
  467. memwipe(intro_keys, 0, sizeof(hs_ntor_intro_cell_keys_t));
  468. tor_free(intro_keys);
  469. }
  470. tor_free(decrypted);
  471. trn_cell_introduce_encrypted_free(enc_cell);
  472. trn_cell_introduce1_free(cell);
  473. return ret;
  474. }
  475. /* Build a RENDEZVOUS1 cell with the given rendezvous cookie and handshake
  476. * info. The encoded cell is put in cell_out and the length of the data is
  477. * returned. This can't fail. */
  478. ssize_t
  479. hs_cell_build_rendezvous1(const uint8_t *rendezvous_cookie,
  480. size_t rendezvous_cookie_len,
  481. const uint8_t *rendezvous_handshake_info,
  482. size_t rendezvous_handshake_info_len,
  483. uint8_t *cell_out)
  484. {
  485. ssize_t cell_len;
  486. trn_cell_rendezvous1_t *cell;
  487. tor_assert(rendezvous_cookie);
  488. tor_assert(rendezvous_handshake_info);
  489. tor_assert(cell_out);
  490. cell = trn_cell_rendezvous1_new();
  491. /* Set the RENDEZVOUS_COOKIE. */
  492. memcpy(trn_cell_rendezvous1_getarray_rendezvous_cookie(cell),
  493. rendezvous_cookie, rendezvous_cookie_len);
  494. /* Set the HANDSHAKE_INFO. */
  495. trn_cell_rendezvous1_setlen_handshake_info(cell,
  496. rendezvous_handshake_info_len);
  497. memcpy(trn_cell_rendezvous1_getarray_handshake_info(cell),
  498. rendezvous_handshake_info, rendezvous_handshake_info_len);
  499. /* Encoding. */
  500. cell_len = trn_cell_rendezvous1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
  501. tor_assert(cell_len > 0);
  502. trn_cell_rendezvous1_free(cell);
  503. return cell_len;
  504. }