hs_cell.c 23 KB

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