hs_cell.c 24 KB

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