hs_cell.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /* Copyright (c) 2017-2018, 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/or.h"
  8. #include "or/config.h"
  9. #include "lib/crypt_ops/crypto_util.h"
  10. #include "or/rendservice.h"
  11. #include "or/replaycache.h"
  12. #include "common/util.h"
  13. #include "or/hs_cell.h"
  14. #include "or/hs_ntor.h"
  15. #include "or/origin_circuit_st.h"
  16. /* Trunnel. */
  17. #include "trunnel/ed25519_cert.h"
  18. #include "trunnel/hs/cell_common.h"
  19. #include "trunnel/hs/cell_establish_intro.h"
  20. #include "trunnel/hs/cell_introduce1.h"
  21. #include "trunnel/hs/cell_rendezvous.h"
  22. /* Compute the MAC of an INTRODUCE cell in mac_out. The encoded_cell param is
  23. * the cell content up to the ENCRYPTED section of length encoded_cell_len.
  24. * The encrypted param is the start of the ENCRYPTED section of length
  25. * encrypted_len. The mac_key is the key needed for the computation of the MAC
  26. * derived from the ntor handshake of length mac_key_len.
  27. *
  28. * The length mac_out_len must be at least DIGEST256_LEN. */
  29. static void
  30. compute_introduce_mac(const uint8_t *encoded_cell, size_t encoded_cell_len,
  31. const uint8_t *encrypted, size_t encrypted_len,
  32. const uint8_t *mac_key, size_t mac_key_len,
  33. uint8_t *mac_out, size_t mac_out_len)
  34. {
  35. size_t offset = 0;
  36. size_t mac_msg_len;
  37. uint8_t mac_msg[RELAY_PAYLOAD_SIZE] = {0};
  38. tor_assert(encoded_cell);
  39. tor_assert(encrypted);
  40. tor_assert(mac_key);
  41. tor_assert(mac_out);
  42. tor_assert(mac_out_len >= DIGEST256_LEN);
  43. /* Compute the size of the message which is basically the entire cell until
  44. * the MAC field of course. */
  45. mac_msg_len = encoded_cell_len + (encrypted_len - DIGEST256_LEN);
  46. tor_assert(mac_msg_len <= sizeof(mac_msg));
  47. /* First, put the encoded cell in the msg. */
  48. memcpy(mac_msg, encoded_cell, encoded_cell_len);
  49. offset += encoded_cell_len;
  50. /* Second, put the CLIENT_PK + ENCRYPTED_DATA but ommit the MAC field (which
  51. * is junk at this point). */
  52. memcpy(mac_msg + offset, encrypted, (encrypted_len - DIGEST256_LEN));
  53. offset += (encrypted_len - DIGEST256_LEN);
  54. tor_assert(offset == mac_msg_len);
  55. crypto_mac_sha3_256(mac_out, mac_out_len,
  56. mac_key, mac_key_len,
  57. mac_msg, mac_msg_len);
  58. memwipe(mac_msg, 0, sizeof(mac_msg));
  59. }
  60. /* From a set of keys, subcredential and the ENCRYPTED section of an
  61. * INTRODUCE2 cell, return a newly allocated intro cell keys structure.
  62. * Finally, the client public key is copied in client_pk. On error, return
  63. * NULL. */
  64. static hs_ntor_intro_cell_keys_t *
  65. get_introduce2_key_material(const ed25519_public_key_t *auth_key,
  66. const curve25519_keypair_t *enc_key,
  67. const uint8_t *subcredential,
  68. const uint8_t *encrypted_section,
  69. curve25519_public_key_t *client_pk)
  70. {
  71. hs_ntor_intro_cell_keys_t *keys;
  72. tor_assert(auth_key);
  73. tor_assert(enc_key);
  74. tor_assert(subcredential);
  75. tor_assert(encrypted_section);
  76. tor_assert(client_pk);
  77. keys = tor_malloc_zero(sizeof(*keys));
  78. /* First bytes of the ENCRYPTED section are the client public key. */
  79. memcpy(client_pk->public_key, encrypted_section, CURVE25519_PUBKEY_LEN);
  80. if (hs_ntor_service_get_introduce1_keys(auth_key, enc_key, client_pk,
  81. subcredential, keys) < 0) {
  82. /* Don't rely on the caller to wipe this on error. */
  83. memwipe(client_pk, 0, sizeof(curve25519_public_key_t));
  84. tor_free(keys);
  85. keys = NULL;
  86. }
  87. return keys;
  88. }
  89. /* Using the given encryption key, decrypt the encrypted_section of length
  90. * encrypted_section_len of an INTRODUCE2 cell and return a newly allocated
  91. * buffer containing the decrypted data. On decryption failure, NULL is
  92. * returned. */
  93. static uint8_t *
  94. decrypt_introduce2(const uint8_t *enc_key, const uint8_t *encrypted_section,
  95. size_t encrypted_section_len)
  96. {
  97. uint8_t *decrypted = NULL;
  98. crypto_cipher_t *cipher = NULL;
  99. tor_assert(enc_key);
  100. tor_assert(encrypted_section);
  101. /* Decrypt ENCRYPTED section. */
  102. cipher = crypto_cipher_new_with_bits((char *) enc_key,
  103. CURVE25519_PUBKEY_LEN * 8);
  104. tor_assert(cipher);
  105. /* This is symmetric encryption so can't be bigger than the encrypted
  106. * section length. */
  107. decrypted = tor_malloc_zero(encrypted_section_len);
  108. if (crypto_cipher_decrypt(cipher, (char *) decrypted,
  109. (const char *) encrypted_section,
  110. encrypted_section_len) < 0) {
  111. tor_free(decrypted);
  112. decrypted = NULL;
  113. goto done;
  114. }
  115. done:
  116. crypto_cipher_free(cipher);
  117. return decrypted;
  118. }
  119. /* Given a pointer to the decrypted data of the ENCRYPTED section of an
  120. * INTRODUCE2 cell of length decrypted_len, parse and validate the cell
  121. * content. Return a newly allocated cell structure or NULL on error. The
  122. * circuit and service object are only used for logging purposes. */
  123. static trn_cell_introduce_encrypted_t *
  124. parse_introduce2_encrypted(const uint8_t *decrypted_data,
  125. size_t decrypted_len, const origin_circuit_t *circ,
  126. const hs_service_t *service)
  127. {
  128. trn_cell_introduce_encrypted_t *enc_cell = NULL;
  129. tor_assert(decrypted_data);
  130. tor_assert(circ);
  131. tor_assert(service);
  132. if (trn_cell_introduce_encrypted_parse(&enc_cell, decrypted_data,
  133. decrypted_len) < 0) {
  134. log_info(LD_REND, "Unable to parse the decrypted ENCRYPTED section of "
  135. "the INTRODUCE2 cell on circuit %u for service %s",
  136. TO_CIRCUIT(circ)->n_circ_id,
  137. safe_str_client(service->onion_address));
  138. goto err;
  139. }
  140. if (trn_cell_introduce_encrypted_get_onion_key_type(enc_cell) !=
  141. HS_CELL_ONION_KEY_TYPE_NTOR) {
  142. log_info(LD_REND, "INTRODUCE2 onion key type is invalid. Got %u but "
  143. "expected %u on circuit %u for service %s",
  144. trn_cell_introduce_encrypted_get_onion_key_type(enc_cell),
  145. HS_CELL_ONION_KEY_TYPE_NTOR, TO_CIRCUIT(circ)->n_circ_id,
  146. safe_str_client(service->onion_address));
  147. goto err;
  148. }
  149. if (trn_cell_introduce_encrypted_getlen_onion_key(enc_cell) !=
  150. CURVE25519_PUBKEY_LEN) {
  151. log_info(LD_REND, "INTRODUCE2 onion key length is invalid. Got %u but "
  152. "expected %d on circuit %u for service %s",
  153. (unsigned)trn_cell_introduce_encrypted_getlen_onion_key(enc_cell),
  154. CURVE25519_PUBKEY_LEN, TO_CIRCUIT(circ)->n_circ_id,
  155. safe_str_client(service->onion_address));
  156. goto err;
  157. }
  158. /* XXX: Validate NSPEC field as well. */
  159. return enc_cell;
  160. err:
  161. trn_cell_introduce_encrypted_free(enc_cell);
  162. return NULL;
  163. }
  164. /* Build a legacy ESTABLISH_INTRO cell with the given circuit nonce and RSA
  165. * encryption key. The encoded cell is put in cell_out that MUST at least be
  166. * of the size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on
  167. * success else a negative value and cell_out is untouched. */
  168. static ssize_t
  169. build_legacy_establish_intro(const char *circ_nonce, crypto_pk_t *enc_key,
  170. uint8_t *cell_out)
  171. {
  172. ssize_t cell_len;
  173. tor_assert(circ_nonce);
  174. tor_assert(enc_key);
  175. tor_assert(cell_out);
  176. memwipe(cell_out, 0, RELAY_PAYLOAD_SIZE);
  177. cell_len = rend_service_encode_establish_intro_cell((char*)cell_out,
  178. RELAY_PAYLOAD_SIZE,
  179. enc_key, circ_nonce);
  180. return cell_len;
  181. }
  182. /* Parse an INTRODUCE2 cell from payload of size payload_len for the given
  183. * service and circuit which are used only for logging purposes. The resulting
  184. * parsed cell is put in cell_ptr_out.
  185. *
  186. * This function only parses prop224 INTRODUCE2 cells even when the intro point
  187. * is a legacy intro point. That's because intro points don't actually care
  188. * about the contents of the introduce cell. Legacy INTRODUCE cells are only
  189. * used by the legacy system now.
  190. *
  191. * Return 0 on success else a negative value and cell_ptr_out is untouched. */
  192. static int
  193. parse_introduce2_cell(const hs_service_t *service,
  194. const origin_circuit_t *circ, const uint8_t *payload,
  195. size_t payload_len,
  196. trn_cell_introduce1_t **cell_ptr_out)
  197. {
  198. trn_cell_introduce1_t *cell = NULL;
  199. tor_assert(service);
  200. tor_assert(circ);
  201. tor_assert(payload);
  202. tor_assert(cell_ptr_out);
  203. /* Parse the cell so we can start cell validation. */
  204. if (trn_cell_introduce1_parse(&cell, payload, payload_len) < 0) {
  205. log_info(LD_PROTOCOL, "Unable to parse INTRODUCE2 cell on circuit %u "
  206. "for service %s",
  207. TO_CIRCUIT(circ)->n_circ_id,
  208. safe_str_client(service->onion_address));
  209. goto err;
  210. }
  211. /* Success. */
  212. *cell_ptr_out = cell;
  213. return 0;
  214. err:
  215. return -1;
  216. }
  217. /* Set the onion public key onion_pk in cell, the encrypted section of an
  218. * INTRODUCE1 cell. */
  219. static void
  220. introduce1_set_encrypted_onion_key(trn_cell_introduce_encrypted_t *cell,
  221. const uint8_t *onion_pk)
  222. {
  223. tor_assert(cell);
  224. tor_assert(onion_pk);
  225. /* There is only one possible key type for a non legacy cell. */
  226. trn_cell_introduce_encrypted_set_onion_key_type(cell,
  227. HS_CELL_ONION_KEY_TYPE_NTOR);
  228. trn_cell_introduce_encrypted_set_onion_key_len(cell, CURVE25519_PUBKEY_LEN);
  229. trn_cell_introduce_encrypted_setlen_onion_key(cell, CURVE25519_PUBKEY_LEN);
  230. memcpy(trn_cell_introduce_encrypted_getarray_onion_key(cell), onion_pk,
  231. trn_cell_introduce_encrypted_getlen_onion_key(cell));
  232. }
  233. /* Set the link specifiers in lspecs in cell, the encrypted section of an
  234. * INTRODUCE1 cell. */
  235. static void
  236. introduce1_set_encrypted_link_spec(trn_cell_introduce_encrypted_t *cell,
  237. const smartlist_t *lspecs)
  238. {
  239. tor_assert(cell);
  240. tor_assert(lspecs);
  241. tor_assert(smartlist_len(lspecs) > 0);
  242. tor_assert(smartlist_len(lspecs) <= UINT8_MAX);
  243. uint8_t lspecs_num = (uint8_t) smartlist_len(lspecs);
  244. trn_cell_introduce_encrypted_set_nspec(cell, lspecs_num);
  245. /* We aren't duplicating the link specifiers object here which means that
  246. * the ownership goes to the trn_cell_introduce_encrypted_t cell and those
  247. * object will be freed when the cell is. */
  248. SMARTLIST_FOREACH(lspecs, link_specifier_t *, ls,
  249. trn_cell_introduce_encrypted_add_nspecs(cell, ls));
  250. }
  251. /* Set padding in the enc_cell only if needed that is the total length of both
  252. * sections are below the mininum required for an INTRODUCE1 cell. */
  253. static void
  254. introduce1_set_encrypted_padding(const trn_cell_introduce1_t *cell,
  255. trn_cell_introduce_encrypted_t *enc_cell)
  256. {
  257. tor_assert(cell);
  258. tor_assert(enc_cell);
  259. /* This is the length we expect to have once encoded of the whole cell. */
  260. ssize_t full_len = trn_cell_introduce1_encoded_len(cell) +
  261. trn_cell_introduce_encrypted_encoded_len(enc_cell);
  262. tor_assert(full_len > 0);
  263. if (full_len < HS_CELL_INTRODUCE1_MIN_SIZE) {
  264. size_t padding = HS_CELL_INTRODUCE1_MIN_SIZE - full_len;
  265. trn_cell_introduce_encrypted_setlen_pad(enc_cell, padding);
  266. memset(trn_cell_introduce_encrypted_getarray_pad(enc_cell), 0,
  267. trn_cell_introduce_encrypted_getlen_pad(enc_cell));
  268. }
  269. }
  270. /* Encrypt the ENCRYPTED payload and encode it in the cell using the enc_cell
  271. * and the INTRODUCE1 data.
  272. *
  273. * This can't fail but it is very important that the caller sets every field
  274. * in data so the computation of the INTRODUCE1 keys doesn't fail. */
  275. static void
  276. introduce1_encrypt_and_encode(trn_cell_introduce1_t *cell,
  277. const trn_cell_introduce_encrypted_t *enc_cell,
  278. const hs_cell_introduce1_data_t *data)
  279. {
  280. size_t offset = 0;
  281. ssize_t encrypted_len;
  282. ssize_t encoded_cell_len, encoded_enc_cell_len;
  283. uint8_t encoded_cell[RELAY_PAYLOAD_SIZE] = {0};
  284. uint8_t encoded_enc_cell[RELAY_PAYLOAD_SIZE] = {0};
  285. uint8_t *encrypted = NULL;
  286. uint8_t mac[DIGEST256_LEN];
  287. crypto_cipher_t *cipher = NULL;
  288. hs_ntor_intro_cell_keys_t keys;
  289. tor_assert(cell);
  290. tor_assert(enc_cell);
  291. tor_assert(data);
  292. /* Encode the cells up to now of what we have to we can perform the MAC
  293. * computation on it. */
  294. encoded_cell_len = trn_cell_introduce1_encode(encoded_cell,
  295. sizeof(encoded_cell), cell);
  296. /* We have a much more serious issue if this isn't true. */
  297. tor_assert(encoded_cell_len > 0);
  298. encoded_enc_cell_len =
  299. trn_cell_introduce_encrypted_encode(encoded_enc_cell,
  300. sizeof(encoded_enc_cell), enc_cell);
  301. /* We have a much more serious issue if this isn't true. */
  302. tor_assert(encoded_enc_cell_len > 0);
  303. /* Get the key material for the encryption. */
  304. if (hs_ntor_client_get_introduce1_keys(data->auth_pk, data->enc_pk,
  305. data->client_kp,
  306. data->subcredential, &keys) < 0) {
  307. tor_assert_unreached();
  308. }
  309. /* Prepare cipher with the encryption key just computed. */
  310. cipher = crypto_cipher_new_with_bits((const char *) keys.enc_key,
  311. sizeof(keys.enc_key) * 8);
  312. tor_assert(cipher);
  313. /* Compute the length of the ENCRYPTED section which is the CLIENT_PK,
  314. * ENCRYPTED_DATA and MAC length. */
  315. encrypted_len = sizeof(data->client_kp->pubkey) + encoded_enc_cell_len +
  316. sizeof(mac);
  317. tor_assert(encrypted_len < RELAY_PAYLOAD_SIZE);
  318. encrypted = tor_malloc_zero(encrypted_len);
  319. /* Put the CLIENT_PK first. */
  320. memcpy(encrypted, data->client_kp->pubkey.public_key,
  321. sizeof(data->client_kp->pubkey.public_key));
  322. offset += sizeof(data->client_kp->pubkey.public_key);
  323. /* Then encrypt and set the ENCRYPTED_DATA. This can't fail. */
  324. crypto_cipher_encrypt(cipher, (char *) encrypted + offset,
  325. (const char *) encoded_enc_cell, encoded_enc_cell_len);
  326. crypto_cipher_free(cipher);
  327. offset += encoded_enc_cell_len;
  328. /* Compute MAC from the above and put it in the buffer. This function will
  329. * make the adjustment to the encrypted_len to omit the MAC length. */
  330. compute_introduce_mac(encoded_cell, encoded_cell_len,
  331. encrypted, encrypted_len,
  332. keys.mac_key, sizeof(keys.mac_key),
  333. mac, sizeof(mac));
  334. memcpy(encrypted + offset, mac, sizeof(mac));
  335. offset += sizeof(mac);
  336. tor_assert(offset == (size_t) encrypted_len);
  337. /* Set the ENCRYPTED section in the cell. */
  338. trn_cell_introduce1_setlen_encrypted(cell, encrypted_len);
  339. memcpy(trn_cell_introduce1_getarray_encrypted(cell),
  340. encrypted, encrypted_len);
  341. /* Cleanup. */
  342. memwipe(&keys, 0, sizeof(keys));
  343. memwipe(mac, 0, sizeof(mac));
  344. memwipe(encrypted, 0, sizeof(encrypted_len));
  345. memwipe(encoded_enc_cell, 0, sizeof(encoded_enc_cell));
  346. tor_free(encrypted);
  347. }
  348. /* Using the INTRODUCE1 data, setup the ENCRYPTED section in cell. This means
  349. * set it, encrypt it and encode it. */
  350. static void
  351. introduce1_set_encrypted(trn_cell_introduce1_t *cell,
  352. const hs_cell_introduce1_data_t *data)
  353. {
  354. trn_cell_introduce_encrypted_t *enc_cell;
  355. trn_cell_extension_t *ext;
  356. tor_assert(cell);
  357. tor_assert(data);
  358. enc_cell = trn_cell_introduce_encrypted_new();
  359. tor_assert(enc_cell);
  360. /* Set extension data. None are used. */
  361. ext = trn_cell_extension_new();
  362. tor_assert(ext);
  363. trn_cell_extension_set_num(ext, 0);
  364. trn_cell_introduce_encrypted_set_extensions(enc_cell, ext);
  365. /* Set the rendezvous cookie. */
  366. memcpy(trn_cell_introduce_encrypted_getarray_rend_cookie(enc_cell),
  367. data->rendezvous_cookie, REND_COOKIE_LEN);
  368. /* Set the onion public key. */
  369. introduce1_set_encrypted_onion_key(enc_cell, data->onion_pk->public_key);
  370. /* Set the link specifiers. */
  371. introduce1_set_encrypted_link_spec(enc_cell, data->link_specifiers);
  372. /* Set padding. */
  373. introduce1_set_encrypted_padding(cell, enc_cell);
  374. /* Encrypt and encode it in the cell. */
  375. introduce1_encrypt_and_encode(cell, enc_cell, data);
  376. /* Cleanup. */
  377. trn_cell_introduce_encrypted_free(enc_cell);
  378. }
  379. /* Set the authentication key in the INTRODUCE1 cell from the given data. */
  380. static void
  381. introduce1_set_auth_key(trn_cell_introduce1_t *cell,
  382. const hs_cell_introduce1_data_t *data)
  383. {
  384. tor_assert(cell);
  385. tor_assert(data);
  386. /* There is only one possible type for a non legacy cell. */
  387. trn_cell_introduce1_set_auth_key_type(cell, HS_INTRO_AUTH_KEY_TYPE_ED25519);
  388. trn_cell_introduce1_set_auth_key_len(cell, ED25519_PUBKEY_LEN);
  389. trn_cell_introduce1_setlen_auth_key(cell, ED25519_PUBKEY_LEN);
  390. memcpy(trn_cell_introduce1_getarray_auth_key(cell),
  391. data->auth_pk->pubkey, trn_cell_introduce1_getlen_auth_key(cell));
  392. }
  393. /* Set the legacy ID field in the INTRODUCE1 cell from the given data. */
  394. static void
  395. introduce1_set_legacy_id(trn_cell_introduce1_t *cell,
  396. const hs_cell_introduce1_data_t *data)
  397. {
  398. tor_assert(cell);
  399. tor_assert(data);
  400. if (data->is_legacy) {
  401. uint8_t digest[DIGEST_LEN];
  402. if (BUG(crypto_pk_get_digest(data->legacy_key, (char *) digest) < 0)) {
  403. return;
  404. }
  405. memcpy(trn_cell_introduce1_getarray_legacy_key_id(cell),
  406. digest, trn_cell_introduce1_getlen_legacy_key_id(cell));
  407. } else {
  408. /* We have to zeroed the LEGACY_KEY_ID field. */
  409. memset(trn_cell_introduce1_getarray_legacy_key_id(cell), 0,
  410. trn_cell_introduce1_getlen_legacy_key_id(cell));
  411. }
  412. }
  413. /* ========== */
  414. /* Public API */
  415. /* ========== */
  416. /* Build an ESTABLISH_INTRO cell with the given circuit nonce and intro point
  417. * object. The encoded cell is put in cell_out that MUST at least be of the
  418. * size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on success else
  419. * a negative value and cell_out is untouched. This function also supports
  420. * legacy cell creation. */
  421. ssize_t
  422. hs_cell_build_establish_intro(const char *circ_nonce,
  423. const hs_service_intro_point_t *ip,
  424. uint8_t *cell_out)
  425. {
  426. ssize_t cell_len = -1;
  427. uint16_t sig_len = ED25519_SIG_LEN;
  428. trn_cell_extension_t *ext;
  429. trn_cell_establish_intro_t *cell = NULL;
  430. tor_assert(circ_nonce);
  431. tor_assert(ip);
  432. /* Quickly handle the legacy IP. */
  433. if (ip->base.is_only_legacy) {
  434. tor_assert(ip->legacy_key);
  435. cell_len = build_legacy_establish_intro(circ_nonce, ip->legacy_key,
  436. cell_out);
  437. tor_assert(cell_len <= RELAY_PAYLOAD_SIZE);
  438. /* Success or not we are done here. */
  439. goto done;
  440. }
  441. /* Set extension data. None used here. */
  442. ext = trn_cell_extension_new();
  443. trn_cell_extension_set_num(ext, 0);
  444. cell = trn_cell_establish_intro_new();
  445. trn_cell_establish_intro_set_extensions(cell, ext);
  446. /* Set signature size. Array is then allocated in the cell. We need to do
  447. * this early so we can use trunnel API to get the signature length. */
  448. trn_cell_establish_intro_set_sig_len(cell, sig_len);
  449. trn_cell_establish_intro_setlen_sig(cell, sig_len);
  450. /* Set AUTH_KEY_TYPE: 2 means ed25519 */
  451. trn_cell_establish_intro_set_auth_key_type(cell,
  452. HS_INTRO_AUTH_KEY_TYPE_ED25519);
  453. /* Set AUTH_KEY and AUTH_KEY_LEN field. Must also set byte-length of
  454. * AUTH_KEY to match */
  455. {
  456. uint16_t auth_key_len = ED25519_PUBKEY_LEN;
  457. trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
  458. trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
  459. /* We do this call _after_ setting the length because it's reallocated at
  460. * that point only. */
  461. uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
  462. memcpy(auth_key_ptr, ip->auth_key_kp.pubkey.pubkey, auth_key_len);
  463. }
  464. /* Calculate HANDSHAKE_AUTH field (MAC). */
  465. {
  466. ssize_t tmp_cell_enc_len = 0;
  467. ssize_t tmp_cell_mac_offset =
  468. sig_len + sizeof(cell->sig_len) +
  469. trn_cell_establish_intro_getlen_handshake_mac(cell);
  470. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0};
  471. uint8_t mac[TRUNNEL_SHA3_256_LEN], *handshake_ptr;
  472. /* We first encode the current fields we have in the cell so we can
  473. * compute the MAC using the raw bytes. */
  474. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  475. sizeof(tmp_cell_enc),
  476. cell);
  477. if (BUG(tmp_cell_enc_len < 0)) {
  478. goto done;
  479. }
  480. /* Sanity check. */
  481. tor_assert(tmp_cell_enc_len > tmp_cell_mac_offset);
  482. /* Circuit nonce is always DIGEST_LEN according to tor-spec.txt. */
  483. crypto_mac_sha3_256(mac, sizeof(mac),
  484. (uint8_t *) circ_nonce, DIGEST_LEN,
  485. tmp_cell_enc, tmp_cell_enc_len - tmp_cell_mac_offset);
  486. handshake_ptr = trn_cell_establish_intro_getarray_handshake_mac(cell);
  487. memcpy(handshake_ptr, mac, sizeof(mac));
  488. memwipe(mac, 0, sizeof(mac));
  489. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  490. }
  491. /* Calculate the cell signature SIG. */
  492. {
  493. ssize_t tmp_cell_enc_len = 0;
  494. ssize_t tmp_cell_sig_offset = (sig_len + sizeof(cell->sig_len));
  495. uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0}, *sig_ptr;
  496. ed25519_signature_t sig;
  497. /* We first encode the current fields we have in the cell so we can
  498. * compute the signature from the raw bytes of the cell. */
  499. tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
  500. sizeof(tmp_cell_enc),
  501. cell);
  502. if (BUG(tmp_cell_enc_len < 0)) {
  503. goto done;
  504. }
  505. if (ed25519_sign_prefixed(&sig, tmp_cell_enc,
  506. tmp_cell_enc_len - tmp_cell_sig_offset,
  507. ESTABLISH_INTRO_SIG_PREFIX, &ip->auth_key_kp)) {
  508. log_warn(LD_BUG, "Unable to make signature for ESTABLISH_INTRO cell.");
  509. goto done;
  510. }
  511. /* Copy the signature into the cell. */
  512. sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
  513. memcpy(sig_ptr, sig.sig, sig_len);
  514. memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
  515. }
  516. /* Encode the cell. Can't be bigger than a standard cell. */
  517. cell_len = trn_cell_establish_intro_encode(cell_out, RELAY_PAYLOAD_SIZE,
  518. cell);
  519. done:
  520. trn_cell_establish_intro_free(cell);
  521. return cell_len;
  522. }
  523. /* Parse the INTRO_ESTABLISHED cell in the payload of size payload_len. If we
  524. * are successful at parsing it, return the length of the parsed cell else a
  525. * negative value on error. */
  526. ssize_t
  527. hs_cell_parse_intro_established(const uint8_t *payload, size_t payload_len)
  528. {
  529. ssize_t ret;
  530. trn_cell_intro_established_t *cell = NULL;
  531. tor_assert(payload);
  532. /* Try to parse the payload into a cell making sure we do actually have a
  533. * valid cell. */
  534. ret = trn_cell_intro_established_parse(&cell, payload, payload_len);
  535. if (ret >= 0) {
  536. /* On success, we do not keep the cell, we just notify the caller that it
  537. * was successfully parsed. */
  538. trn_cell_intro_established_free(cell);
  539. }
  540. return ret;
  541. }
  542. /* Parsse the INTRODUCE2 cell using data which contains everything we need to
  543. * do so and contains the destination buffers of information we extract and
  544. * compute from the cell. Return 0 on success else a negative value. The
  545. * service and circ are only used for logging purposes. */
  546. ssize_t
  547. hs_cell_parse_introduce2(hs_cell_introduce2_data_t *data,
  548. const origin_circuit_t *circ,
  549. const hs_service_t *service)
  550. {
  551. int ret = -1;
  552. time_t elapsed;
  553. uint8_t *decrypted = NULL;
  554. size_t encrypted_section_len;
  555. const uint8_t *encrypted_section;
  556. trn_cell_introduce1_t *cell = NULL;
  557. trn_cell_introduce_encrypted_t *enc_cell = NULL;
  558. hs_ntor_intro_cell_keys_t *intro_keys = NULL;
  559. tor_assert(data);
  560. tor_assert(circ);
  561. tor_assert(service);
  562. /* Parse the cell into a decoded data structure pointed by cell_ptr. */
  563. if (parse_introduce2_cell(service, circ, data->payload, data->payload_len,
  564. &cell) < 0) {
  565. goto done;
  566. }
  567. log_info(LD_REND, "Received a decodable INTRODUCE2 cell on circuit %u "
  568. "for service %s. Decoding encrypted section...",
  569. TO_CIRCUIT(circ)->n_circ_id,
  570. safe_str_client(service->onion_address));
  571. encrypted_section = trn_cell_introduce1_getconstarray_encrypted(cell);
  572. encrypted_section_len = trn_cell_introduce1_getlen_encrypted(cell);
  573. /* Encrypted section must at least contain the CLIENT_PK and MAC which is
  574. * defined in section 3.3.2 of the specification. */
  575. if (encrypted_section_len < (CURVE25519_PUBKEY_LEN + DIGEST256_LEN)) {
  576. log_info(LD_REND, "Invalid INTRODUCE2 encrypted section length "
  577. "for service %s. Dropping cell.",
  578. safe_str_client(service->onion_address));
  579. goto done;
  580. }
  581. /* Check our replay cache for this introduction point. */
  582. if (replaycache_add_test_and_elapsed(data->replay_cache, encrypted_section,
  583. encrypted_section_len, &elapsed)) {
  584. log_warn(LD_REND, "Possible replay detected! An INTRODUCE2 cell with the"
  585. "same ENCRYPTED section was seen %ld seconds ago. "
  586. "Dropping cell.", (long int) elapsed);
  587. goto done;
  588. }
  589. /* Build the key material out of the key material found in the cell. */
  590. intro_keys = get_introduce2_key_material(data->auth_pk, data->enc_kp,
  591. data->subcredential,
  592. encrypted_section,
  593. &data->client_pk);
  594. if (intro_keys == NULL) {
  595. log_info(LD_REND, "Invalid INTRODUCE2 encrypted data. Unable to "
  596. "compute key material on circuit %u for service %s",
  597. TO_CIRCUIT(circ)->n_circ_id,
  598. safe_str_client(service->onion_address));
  599. goto done;
  600. }
  601. /* Validate MAC from the cell and our computed key material. The MAC field
  602. * in the cell is at the end of the encrypted section. */
  603. {
  604. uint8_t mac[DIGEST256_LEN];
  605. /* The MAC field is at the very end of the ENCRYPTED section. */
  606. size_t mac_offset = encrypted_section_len - sizeof(mac);
  607. /* Compute the MAC. Use the entire encoded payload with a length up to the
  608. * ENCRYPTED section. */
  609. compute_introduce_mac(data->payload,
  610. data->payload_len - encrypted_section_len,
  611. encrypted_section, encrypted_section_len,
  612. intro_keys->mac_key, sizeof(intro_keys->mac_key),
  613. mac, sizeof(mac));
  614. if (tor_memcmp(mac, encrypted_section + mac_offset, sizeof(mac))) {
  615. log_info(LD_REND, "Invalid MAC validation for INTRODUCE2 cell on "
  616. "circuit %u for service %s",
  617. TO_CIRCUIT(circ)->n_circ_id,
  618. safe_str_client(service->onion_address));
  619. goto done;
  620. }
  621. }
  622. {
  623. /* The ENCRYPTED_DATA section starts just after the CLIENT_PK. */
  624. const uint8_t *encrypted_data =
  625. encrypted_section + sizeof(data->client_pk);
  626. /* It's symmetric encryption so it's correct to use the ENCRYPTED length
  627. * for decryption. Computes the length of ENCRYPTED_DATA meaning removing
  628. * the CLIENT_PK and MAC length. */
  629. size_t encrypted_data_len =
  630. encrypted_section_len - (sizeof(data->client_pk) + DIGEST256_LEN);
  631. /* This decrypts the ENCRYPTED_DATA section of the cell. */
  632. decrypted = decrypt_introduce2(intro_keys->enc_key,
  633. encrypted_data, encrypted_data_len);
  634. if (decrypted == NULL) {
  635. log_info(LD_REND, "Unable to decrypt the ENCRYPTED section of an "
  636. "INTRODUCE2 cell on circuit %u for service %s",
  637. TO_CIRCUIT(circ)->n_circ_id,
  638. safe_str_client(service->onion_address));
  639. goto done;
  640. }
  641. /* Parse this blob into an encrypted cell structure so we can then extract
  642. * the data we need out of it. */
  643. enc_cell = parse_introduce2_encrypted(decrypted, encrypted_data_len,
  644. circ, service);
  645. memwipe(decrypted, 0, encrypted_data_len);
  646. if (enc_cell == NULL) {
  647. goto done;
  648. }
  649. }
  650. /* XXX: Implement client authorization checks. */
  651. /* Extract onion key and rendezvous cookie from the cell used for the
  652. * rendezvous point circuit e2e encryption. */
  653. memcpy(data->onion_pk.public_key,
  654. trn_cell_introduce_encrypted_getconstarray_onion_key(enc_cell),
  655. CURVE25519_PUBKEY_LEN);
  656. memcpy(data->rendezvous_cookie,
  657. trn_cell_introduce_encrypted_getconstarray_rend_cookie(enc_cell),
  658. sizeof(data->rendezvous_cookie));
  659. /* Extract rendezvous link specifiers. */
  660. for (size_t idx = 0;
  661. idx < trn_cell_introduce_encrypted_get_nspec(enc_cell); idx++) {
  662. link_specifier_t *lspec =
  663. trn_cell_introduce_encrypted_get_nspecs(enc_cell, idx);
  664. smartlist_add(data->link_specifiers, hs_link_specifier_dup(lspec));
  665. }
  666. /* Success. */
  667. ret = 0;
  668. log_info(LD_REND, "Valid INTRODUCE2 cell. Launching rendezvous circuit.");
  669. done:
  670. if (intro_keys) {
  671. memwipe(intro_keys, 0, sizeof(hs_ntor_intro_cell_keys_t));
  672. tor_free(intro_keys);
  673. }
  674. tor_free(decrypted);
  675. trn_cell_introduce_encrypted_free(enc_cell);
  676. trn_cell_introduce1_free(cell);
  677. return ret;
  678. }
  679. /* Build a RENDEZVOUS1 cell with the given rendezvous cookie and handshake
  680. * info. The encoded cell is put in cell_out and the length of the data is
  681. * returned. This can't fail. */
  682. ssize_t
  683. hs_cell_build_rendezvous1(const uint8_t *rendezvous_cookie,
  684. size_t rendezvous_cookie_len,
  685. const uint8_t *rendezvous_handshake_info,
  686. size_t rendezvous_handshake_info_len,
  687. uint8_t *cell_out)
  688. {
  689. ssize_t cell_len;
  690. trn_cell_rendezvous1_t *cell;
  691. tor_assert(rendezvous_cookie);
  692. tor_assert(rendezvous_handshake_info);
  693. tor_assert(cell_out);
  694. cell = trn_cell_rendezvous1_new();
  695. /* Set the RENDEZVOUS_COOKIE. */
  696. memcpy(trn_cell_rendezvous1_getarray_rendezvous_cookie(cell),
  697. rendezvous_cookie, rendezvous_cookie_len);
  698. /* Set the HANDSHAKE_INFO. */
  699. trn_cell_rendezvous1_setlen_handshake_info(cell,
  700. rendezvous_handshake_info_len);
  701. memcpy(trn_cell_rendezvous1_getarray_handshake_info(cell),
  702. rendezvous_handshake_info, rendezvous_handshake_info_len);
  703. /* Encoding. */
  704. cell_len = trn_cell_rendezvous1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
  705. tor_assert(cell_len > 0);
  706. trn_cell_rendezvous1_free(cell);
  707. return cell_len;
  708. }
  709. /* Build an INTRODUCE1 cell from the given data. The encoded cell is put in
  710. * cell_out which must be of at least size RELAY_PAYLOAD_SIZE. On success, the
  711. * encoded length is returned else a negative value and the content of
  712. * cell_out should be ignored. */
  713. ssize_t
  714. hs_cell_build_introduce1(const hs_cell_introduce1_data_t *data,
  715. uint8_t *cell_out)
  716. {
  717. ssize_t cell_len;
  718. trn_cell_introduce1_t *cell;
  719. trn_cell_extension_t *ext;
  720. tor_assert(data);
  721. tor_assert(cell_out);
  722. cell = trn_cell_introduce1_new();
  723. tor_assert(cell);
  724. /* Set extension data. None are used. */
  725. ext = trn_cell_extension_new();
  726. tor_assert(ext);
  727. trn_cell_extension_set_num(ext, 0);
  728. trn_cell_introduce1_set_extensions(cell, ext);
  729. /* Set the legacy ID field. */
  730. introduce1_set_legacy_id(cell, data);
  731. /* Set the authentication key. */
  732. introduce1_set_auth_key(cell, data);
  733. /* Set the encrypted section. This will set, encrypt and encode the
  734. * ENCRYPTED section in the cell. After this, we'll be ready to encode. */
  735. introduce1_set_encrypted(cell, data);
  736. /* Final encoding. */
  737. cell_len = trn_cell_introduce1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
  738. trn_cell_introduce1_free(cell);
  739. return cell_len;
  740. }
  741. /* Build an ESTABLISH_RENDEZVOUS cell from the given rendezvous_cookie. The
  742. * encoded cell is put in cell_out which must be of at least
  743. * RELAY_PAYLOAD_SIZE. On success, the encoded length is returned and the
  744. * caller should clear up the content of the cell.
  745. *
  746. * This function can't fail. */
  747. ssize_t
  748. hs_cell_build_establish_rendezvous(const uint8_t *rendezvous_cookie,
  749. uint8_t *cell_out)
  750. {
  751. tor_assert(rendezvous_cookie);
  752. tor_assert(cell_out);
  753. memcpy(cell_out, rendezvous_cookie, HS_REND_COOKIE_LEN);
  754. return HS_REND_COOKIE_LEN;
  755. }
  756. /* Handle an INTRODUCE_ACK cell encoded in payload of length payload_len.
  757. * Return the status code on success else a negative value if the cell as not
  758. * decodable. */
  759. int
  760. hs_cell_parse_introduce_ack(const uint8_t *payload, size_t payload_len)
  761. {
  762. int ret = -1;
  763. trn_cell_introduce_ack_t *cell = NULL;
  764. tor_assert(payload);
  765. /* If it is a legacy IP, rend-spec.txt specifies that a ACK is 0 byte and a
  766. * NACK is 1 byte. We can't use the legacy function for this so we have to
  767. * do a special case. */
  768. if (payload_len <= 1) {
  769. if (payload_len == 0) {
  770. ret = HS_CELL_INTRO_ACK_SUCCESS;
  771. } else {
  772. ret = HS_CELL_INTRO_ACK_FAILURE;
  773. }
  774. goto end;
  775. }
  776. if (trn_cell_introduce_ack_parse(&cell, payload, payload_len) < 0) {
  777. log_info(LD_REND, "Invalid INTRODUCE_ACK cell. Unable to parse it.");
  778. goto end;
  779. }
  780. ret = trn_cell_introduce_ack_get_status(cell);
  781. end:
  782. trn_cell_introduce_ack_free(cell);
  783. return ret;
  784. }
  785. /* Handle a RENDEZVOUS2 cell encoded in payload of length payload_len. On
  786. * success, handshake_info contains the data in the HANDSHAKE_INFO field, and
  787. * 0 is returned. On error, a negative value is returned. */
  788. int
  789. hs_cell_parse_rendezvous2(const uint8_t *payload, size_t payload_len,
  790. uint8_t *handshake_info, size_t handshake_info_len)
  791. {
  792. int ret = -1;
  793. trn_cell_rendezvous2_t *cell = NULL;
  794. tor_assert(payload);
  795. tor_assert(handshake_info);
  796. if (trn_cell_rendezvous2_parse(&cell, payload, payload_len) < 0) {
  797. log_info(LD_REND, "Invalid RENDEZVOUS2 cell. Unable to parse it.");
  798. goto end;
  799. }
  800. /* Static size, we should never have an issue with this else we messed up
  801. * our code flow. */
  802. tor_assert(trn_cell_rendezvous2_getlen_handshake_info(cell) ==
  803. handshake_info_len);
  804. memcpy(handshake_info,
  805. trn_cell_rendezvous2_getconstarray_handshake_info(cell),
  806. handshake_info_len);
  807. ret = 0;
  808. end:
  809. trn_cell_rendezvous2_free(cell);
  810. return ret;
  811. }
  812. /* Clear the given INTRODUCE1 data structure data. */
  813. void
  814. hs_cell_introduce1_data_clear(hs_cell_introduce1_data_t *data)
  815. {
  816. if (data == NULL) {
  817. return;
  818. }
  819. /* Object in this list have been moved to the cell object when building it
  820. * so they've been freed earlier. We do that in order to avoid duplicating
  821. * them leading to more memory and CPU time being used for nothing. */
  822. smartlist_free(data->link_specifiers);
  823. /* The data object has no ownership of any members. */
  824. memwipe(data, 0, sizeof(hs_cell_introduce1_data_t));
  825. }