hs_cell.c 19 KB

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