test_hs_intropoint.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /* Copyright (c) 2016-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_service.c
  5. * \brief Test hidden service functionality.
  6. */
  7. #define HS_SERVICE_PRIVATE
  8. #define HS_INTROPOINT_PRIVATE
  9. #define RENDSERVICE_PRIVATE
  10. #define CIRCUITLIST_PRIVATE
  11. #include "test/test.h"
  12. #include "test/log_test_helpers.h"
  13. #include "lib/crypt_ops/crypto_rand.h"
  14. #include "core/or/or.h"
  15. #include "core/or/channel.h"
  16. #include "core/or/circuitlist.h"
  17. #include "core/or/circuituse.h"
  18. #include "ht.h"
  19. #include "core/or/relay.h"
  20. #include "feature/rend/rendservice.h"
  21. #include "feature/hs/hs_cell.h"
  22. #include "feature/hs/hs_circuitmap.h"
  23. #include "feature/hs/hs_common.h"
  24. #include "feature/hs/hs_dos.h"
  25. #include "feature/hs/hs_intropoint.h"
  26. #include "feature/hs/hs_service.h"
  27. #include "core/or/or_circuit_st.h"
  28. /* Trunnel. */
  29. #include "trunnel/hs/cell_establish_intro.h"
  30. #include "trunnel/hs/cell_introduce1.h"
  31. #include "trunnel/hs/cell_common.h"
  32. static size_t
  33. new_establish_intro_cell(const char *circ_nonce,
  34. trn_cell_establish_intro_t **cell_out)
  35. {
  36. ssize_t cell_len = 0;
  37. uint8_t buf[RELAY_PAYLOAD_SIZE] = {0};
  38. trn_cell_establish_intro_t *cell = NULL;
  39. hs_service_intro_point_t *ip = NULL;
  40. hs_service_config_t config;
  41. memset(&config, 0, sizeof(config));
  42. /* Ensure that *cell_out is NULL such that we can use to check if we need to
  43. * free `cell` in case of an error. */
  44. *cell_out = NULL;
  45. /* Auth key pair is generated in the constructor so we are all set for
  46. * using this IP object. */
  47. ip = service_intro_point_new(NULL);
  48. tt_assert(ip);
  49. cell_len = hs_cell_build_establish_intro(circ_nonce, &config, ip, buf);
  50. tt_i64_op(cell_len, OP_GT, 0);
  51. cell_len = trn_cell_establish_intro_parse(&cell, buf, sizeof(buf));
  52. tt_i64_op(cell_len, OP_GT, 0);
  53. tt_assert(cell);
  54. *cell_out = cell;
  55. done:
  56. if (*cell_out == NULL)
  57. trn_cell_establish_intro_free(cell);
  58. service_intro_point_free(ip);
  59. return cell_len;
  60. }
  61. static ssize_t
  62. new_establish_intro_encoded_cell(const char *circ_nonce, uint8_t *cell_out)
  63. {
  64. ssize_t cell_len = 0;
  65. hs_service_intro_point_t *ip = NULL;
  66. hs_service_config_t config;
  67. memset(&config, 0, sizeof(config));
  68. /* Auth key pair is generated in the constructor so we are all set for
  69. * using this IP object. */
  70. ip = service_intro_point_new(NULL);
  71. tt_assert(ip);
  72. cell_len = hs_cell_build_establish_intro(circ_nonce, &config, ip, cell_out);
  73. tt_i64_op(cell_len, OP_GT, 0);
  74. done:
  75. service_intro_point_free(ip);
  76. return cell_len;
  77. }
  78. /* Mock function to avoid networking in unittests */
  79. static int
  80. mock_send_intro_established_cell(or_circuit_t *circ)
  81. {
  82. (void) circ;
  83. return 0;
  84. }
  85. static int
  86. mock_relay_send_command_from_edge(streamid_t stream_id, circuit_t *circ,
  87. uint8_t relay_command, const char *payload,
  88. size_t payload_len,
  89. crypt_path_t *cpath_layer,
  90. const char *filename, int lineno)
  91. {
  92. (void) stream_id;
  93. (void) circ;
  94. (void) relay_command;
  95. (void) payload;
  96. (void) payload_len;
  97. (void) cpath_layer;
  98. (void) filename;
  99. (void) lineno;
  100. return 0;
  101. }
  102. static or_circuit_t *
  103. helper_create_intro_circuit(void)
  104. {
  105. or_circuit_t *circ = or_circuit_new(0, NULL);
  106. tt_assert(circ);
  107. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
  108. token_bucket_ctr_init(&circ->introduce2_bucket, 100, 100,
  109. (uint32_t) approx_time());
  110. done:
  111. return circ;
  112. }
  113. static trn_cell_introduce1_t *
  114. helper_create_introduce1_cell(void)
  115. {
  116. trn_cell_introduce1_t *cell = NULL;
  117. ed25519_keypair_t auth_key_kp;
  118. /* Generate the auth_key of the cell. */
  119. if (ed25519_keypair_generate(&auth_key_kp, 0) < 0) {
  120. goto err;
  121. }
  122. cell = trn_cell_introduce1_new();
  123. tt_assert(cell);
  124. /* Set the auth key. */
  125. {
  126. size_t auth_key_len = sizeof(auth_key_kp.pubkey);
  127. trn_cell_introduce1_set_auth_key_type(cell,
  128. TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519);
  129. trn_cell_introduce1_set_auth_key_len(cell, auth_key_len);
  130. trn_cell_introduce1_setlen_auth_key(cell, auth_key_len);
  131. uint8_t *auth_key_ptr = trn_cell_introduce1_getarray_auth_key(cell);
  132. memcpy(auth_key_ptr, auth_key_kp.pubkey.pubkey, auth_key_len);
  133. }
  134. /* Set the cell extensions to none. */
  135. {
  136. trn_cell_extension_t *ext = trn_cell_extension_new();
  137. trn_cell_extension_set_num(ext, 0);
  138. trn_cell_introduce1_set_extensions(cell, ext);
  139. }
  140. /* Set the encrypted section to some data. */
  141. {
  142. size_t enc_len = 128;
  143. trn_cell_introduce1_setlen_encrypted(cell, enc_len);
  144. uint8_t *enc_ptr = trn_cell_introduce1_getarray_encrypted(cell);
  145. memset(enc_ptr, 'a', enc_len);
  146. }
  147. return cell;
  148. err:
  149. done:
  150. trn_cell_introduce1_free(cell);
  151. return NULL;
  152. }
  153. /* Try sending an ESTABLISH_INTRO cell on a circuit that is already an intro
  154. * point. Should fail. */
  155. static void
  156. test_establish_intro_wrong_purpose(void *arg)
  157. {
  158. int retval;
  159. ssize_t cell_len = 0;
  160. char circ_nonce[DIGEST_LEN] = {0};
  161. uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  162. or_circuit_t *intro_circ = or_circuit_new(0,NULL);
  163. (void)arg;
  164. /* Get the auth key of the intro point */
  165. crypto_rand(circ_nonce, sizeof(circ_nonce));
  166. memcpy(intro_circ->rend_circ_nonce, circ_nonce, DIGEST_LEN);
  167. /* Set a bad circuit purpose!! :) */
  168. circuit_change_purpose(TO_CIRCUIT(intro_circ), CIRCUIT_PURPOSE_INTRO_POINT);
  169. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  170. attempt to parse it. */
  171. cell_len = new_establish_intro_encoded_cell(circ_nonce, cell_body);
  172. tt_i64_op(cell_len, OP_GT, 0);
  173. /* Receive the cell. Should fail. */
  174. setup_full_capture_of_logs(LOG_INFO);
  175. retval = hs_intro_received_establish_intro(intro_circ, cell_body, cell_len);
  176. expect_log_msg_containing("Rejecting ESTABLISH_INTRO on non-OR circuit.");
  177. teardown_capture_of_logs();
  178. tt_int_op(retval, OP_EQ, -1);
  179. done:
  180. circuit_free_(TO_CIRCUIT(intro_circ));
  181. }
  182. /* Prepare a circuit for accepting an ESTABLISH_INTRO cell */
  183. static void
  184. helper_prepare_circ_for_intro(or_circuit_t *circ, const char *circ_nonce)
  185. {
  186. /* Prepare the circuit for the incoming ESTABLISH_INTRO */
  187. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
  188. memcpy(circ->rend_circ_nonce, circ_nonce, DIGEST_LEN);
  189. }
  190. /* Send an empty ESTABLISH_INTRO cell. Should fail. */
  191. static void
  192. test_establish_intro_wrong_keytype(void *arg)
  193. {
  194. int retval;
  195. or_circuit_t *intro_circ = or_circuit_new(0,NULL);
  196. char circ_nonce[DIGEST_LEN] = {0};
  197. (void) arg;
  198. /* Get the auth key of the intro point */
  199. crypto_rand(circ_nonce, sizeof(circ_nonce));
  200. helper_prepare_circ_for_intro(intro_circ, circ_nonce);
  201. /* Receive the cell. Should fail. */
  202. setup_full_capture_of_logs(LOG_INFO);
  203. retval = hs_intro_received_establish_intro(intro_circ, (uint8_t *) "", 0);
  204. expect_log_msg_containing("Empty ESTABLISH_INTRO cell.");
  205. teardown_capture_of_logs();
  206. tt_int_op(retval, OP_EQ, -1);
  207. done:
  208. circuit_free_(TO_CIRCUIT(intro_circ));
  209. }
  210. /* Send an ESTABLISH_INTRO cell with an unknown auth key type. Should fail. */
  211. static void
  212. test_establish_intro_wrong_keytype2(void *arg)
  213. {
  214. int retval;
  215. char circ_nonce[DIGEST_LEN] = {0};
  216. uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  217. ssize_t cell_len = 0;
  218. or_circuit_t *intro_circ = or_circuit_new(0,NULL);
  219. (void) arg;
  220. /* Get the auth key of the intro point */
  221. crypto_rand(circ_nonce, sizeof(circ_nonce));
  222. helper_prepare_circ_for_intro(intro_circ, circ_nonce);
  223. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  224. * attempt to parse it. */
  225. cell_len = new_establish_intro_encoded_cell(circ_nonce, cell_body);
  226. tt_i64_op(cell_len, OP_GT, 0);
  227. /* Mutate the auth key type! :) */
  228. cell_body[0] = 42;
  229. /* Receive the cell. Should fail. */
  230. setup_full_capture_of_logs(LOG_INFO);
  231. retval = hs_intro_received_establish_intro(intro_circ, cell_body, cell_len);
  232. expect_log_msg_containing("Unrecognized AUTH_KEY_TYPE 42.");
  233. teardown_capture_of_logs();
  234. tt_int_op(retval, OP_EQ, -1);
  235. done:
  236. circuit_free_(TO_CIRCUIT(intro_circ));
  237. }
  238. /* Send a legit ESTABLISH_INTRO cell but with a wrong MAC. Should fail. */
  239. static void
  240. test_establish_intro_wrong_mac(void *arg)
  241. {
  242. int retval;
  243. char circ_nonce[DIGEST_LEN] = {0};
  244. ssize_t cell_len = 0;
  245. uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  246. trn_cell_establish_intro_t *cell = NULL;
  247. or_circuit_t *intro_circ = or_circuit_new(0,NULL);
  248. (void) arg;
  249. /* Get the auth key of the intro point */
  250. crypto_rand(circ_nonce, sizeof(circ_nonce));
  251. helper_prepare_circ_for_intro(intro_circ, circ_nonce);
  252. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  253. * attempt to parse it. */
  254. cell_len = new_establish_intro_cell(circ_nonce, &cell);
  255. tt_i64_op(cell_len, OP_GT, 0);
  256. tt_assert(cell);
  257. /* Mangle one byte of the MAC. */
  258. uint8_t *handshake_ptr =
  259. trn_cell_establish_intro_getarray_handshake_mac(cell);
  260. handshake_ptr[TRUNNEL_SHA3_256_LEN - 1]++;
  261. /* We need to resign the payload with that change. */
  262. {
  263. ed25519_signature_t sig;
  264. ed25519_keypair_t key_struct;
  265. /* New keypair for the signature since we don't have access to the private
  266. * key material generated earlier when creating the cell. */
  267. retval = ed25519_keypair_generate(&key_struct, 0);
  268. tt_int_op(retval, OP_EQ, 0);
  269. uint8_t *auth_key_ptr =
  270. trn_cell_establish_intro_getarray_auth_key(cell);
  271. memcpy(auth_key_ptr, key_struct.pubkey.pubkey, ED25519_PUBKEY_LEN);
  272. /* Encode payload so we can sign it. */
  273. cell_len = trn_cell_establish_intro_encode(cell_body, sizeof(cell_body),
  274. cell);
  275. tt_i64_op(cell_len, OP_GT, 0);
  276. retval = ed25519_sign_prefixed(&sig, cell_body,
  277. cell_len -
  278. (ED25519_SIG_LEN + sizeof(cell->sig_len)),
  279. ESTABLISH_INTRO_SIG_PREFIX, &key_struct);
  280. tt_int_op(retval, OP_EQ, 0);
  281. /* And write the signature to the cell */
  282. uint8_t *sig_ptr =
  283. trn_cell_establish_intro_getarray_sig(cell);
  284. memcpy(sig_ptr, sig.sig, cell->sig_len);
  285. /* Re-encode with the new signature. */
  286. cell_len = trn_cell_establish_intro_encode(cell_body, sizeof(cell_body),
  287. cell);
  288. tt_i64_op(cell_len, OP_GT, 0);
  289. }
  290. /* Receive the cell. Should fail because our MAC is wrong. */
  291. setup_full_capture_of_logs(LOG_INFO);
  292. retval = hs_intro_received_establish_intro(intro_circ, cell_body, cell_len);
  293. expect_log_msg_containing("ESTABLISH_INTRO handshake_auth not as expected");
  294. teardown_capture_of_logs();
  295. tt_int_op(retval, OP_EQ, -1);
  296. done:
  297. trn_cell_establish_intro_free(cell);
  298. circuit_free_(TO_CIRCUIT(intro_circ));
  299. }
  300. /* Send a legit ESTABLISH_INTRO cell but with a wrong auth key length. Should
  301. * fail. */
  302. static void
  303. test_establish_intro_wrong_auth_key_len(void *arg)
  304. {
  305. int retval;
  306. char circ_nonce[DIGEST_LEN] = {0};
  307. uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  308. ssize_t cell_len = 0;
  309. size_t bad_auth_key_len = ED25519_PUBKEY_LEN - 1;
  310. trn_cell_establish_intro_t *cell = NULL;
  311. or_circuit_t *intro_circ = or_circuit_new(0,NULL);
  312. (void) arg;
  313. /* Get the auth key of the intro point */
  314. crypto_rand(circ_nonce, sizeof(circ_nonce));
  315. helper_prepare_circ_for_intro(intro_circ, circ_nonce);
  316. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  317. * attempt to parse it. */
  318. cell_len = new_establish_intro_cell(circ_nonce, &cell);
  319. tt_i64_op(cell_len, OP_GT, 0);
  320. tt_assert(cell);
  321. /* Mangle the auth key length. */
  322. trn_cell_establish_intro_set_auth_key_len(cell, bad_auth_key_len);
  323. trn_cell_establish_intro_setlen_auth_key(cell, bad_auth_key_len);
  324. /* Encode cell. */
  325. cell_len = trn_cell_establish_intro_encode(cell_body, sizeof(cell_body),
  326. cell);
  327. tt_int_op(cell_len, OP_GT, 0);
  328. /* Receive the cell. Should fail. */
  329. setup_full_capture_of_logs(LOG_INFO);
  330. retval = hs_intro_received_establish_intro(intro_circ, cell_body, cell_len);
  331. expect_log_msg_containing("ESTABLISH_INTRO auth key length is invalid");
  332. teardown_capture_of_logs();
  333. tt_int_op(retval, OP_EQ, -1);
  334. done:
  335. trn_cell_establish_intro_free(cell);
  336. circuit_free_(TO_CIRCUIT(intro_circ));
  337. }
  338. /* Send a legit ESTABLISH_INTRO cell but with a wrong sig length. Should
  339. * fail. */
  340. static void
  341. test_establish_intro_wrong_sig_len(void *arg)
  342. {
  343. int retval;
  344. char circ_nonce[DIGEST_LEN] = {0};
  345. uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  346. ssize_t cell_len = 0;
  347. size_t bad_sig_len = ED25519_SIG_LEN - 1;
  348. trn_cell_establish_intro_t *cell = NULL;
  349. or_circuit_t *intro_circ = or_circuit_new(0,NULL);
  350. (void) arg;
  351. /* Get the auth key of the intro point */
  352. crypto_rand(circ_nonce, sizeof(circ_nonce));
  353. helper_prepare_circ_for_intro(intro_circ, circ_nonce);
  354. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  355. * attempt to parse it. */
  356. cell_len = new_establish_intro_cell(circ_nonce, &cell);
  357. tt_i64_op(cell_len, OP_GT, 0);
  358. tt_assert(cell);
  359. /* Mangle the signature length. */
  360. trn_cell_establish_intro_set_sig_len(cell, bad_sig_len);
  361. trn_cell_establish_intro_setlen_sig(cell, bad_sig_len);
  362. /* Encode cell. */
  363. cell_len = trn_cell_establish_intro_encode(cell_body, sizeof(cell_body),
  364. cell);
  365. tt_int_op(cell_len, OP_GT, 0);
  366. /* Receive the cell. Should fail. */
  367. setup_full_capture_of_logs(LOG_INFO);
  368. retval = hs_intro_received_establish_intro(intro_circ, cell_body, cell_len);
  369. expect_log_msg_containing("ESTABLISH_INTRO sig len is invalid");
  370. teardown_capture_of_logs();
  371. tt_int_op(retval, OP_EQ, -1);
  372. done:
  373. trn_cell_establish_intro_free(cell);
  374. circuit_free_(TO_CIRCUIT(intro_circ));
  375. }
  376. /* Send a legit ESTABLISH_INTRO cell but slightly change the signature. Should
  377. * fail. */
  378. static void
  379. test_establish_intro_wrong_sig(void *arg)
  380. {
  381. int retval;
  382. char circ_nonce[DIGEST_LEN] = {0};
  383. uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  384. ssize_t cell_len = 0;
  385. or_circuit_t *intro_circ = or_circuit_new(0,NULL);
  386. (void) arg;
  387. /* Get the auth key of the intro point */
  388. crypto_rand(circ_nonce, sizeof(circ_nonce));
  389. helper_prepare_circ_for_intro(intro_circ, circ_nonce);
  390. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  391. attempt to parse it. */
  392. cell_len = new_establish_intro_encoded_cell(circ_nonce, cell_body);
  393. tt_i64_op(cell_len, OP_GT, 0);
  394. /* Mutate the last byte (signature)! :) */
  395. cell_body[cell_len - 1]++;
  396. /* Receive the cell. Should fail. */
  397. setup_full_capture_of_logs(LOG_INFO);
  398. retval = hs_intro_received_establish_intro(intro_circ, cell_body,
  399. (size_t)cell_len);
  400. expect_log_msg_containing("Failed to verify ESTABLISH_INTRO cell.");
  401. teardown_capture_of_logs();
  402. tt_int_op(retval, OP_EQ, -1);
  403. done:
  404. circuit_free_(TO_CIRCUIT(intro_circ));
  405. }
  406. /* Helper function: Send a well-formed v3 ESTABLISH_INTRO cell to
  407. * <b>intro_circ</b>. Return the cell. */
  408. static trn_cell_establish_intro_t *
  409. helper_establish_intro_v3(or_circuit_t *intro_circ)
  410. {
  411. int retval;
  412. char circ_nonce[DIGEST_LEN] = {0};
  413. uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  414. ssize_t cell_len = 0;
  415. trn_cell_establish_intro_t *cell = NULL;
  416. tt_assert(intro_circ);
  417. /* Prepare the circuit for the incoming ESTABLISH_INTRO */
  418. crypto_rand(circ_nonce, sizeof(circ_nonce));
  419. helper_prepare_circ_for_intro(intro_circ, circ_nonce);
  420. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  421. * attempt to parse it. */
  422. cell_len = new_establish_intro_cell(circ_nonce, &cell);
  423. tt_i64_op(cell_len, OP_GT, 0);
  424. tt_assert(cell);
  425. cell_len = trn_cell_establish_intro_encode(cell_body, sizeof(cell_body),
  426. cell);
  427. tt_int_op(cell_len, OP_GT, 0);
  428. /* Receive the cell */
  429. retval = hs_intro_received_establish_intro(intro_circ, cell_body,
  430. (size_t) cell_len);
  431. tt_int_op(retval, OP_EQ, 0);
  432. done:
  433. return cell;
  434. }
  435. /* Helper function: Send a well-formed v2 ESTABLISH_INTRO cell to
  436. * <b>intro_circ</b>. Return the public key advertised in the cell. */
  437. static crypto_pk_t *
  438. helper_establish_intro_v2(or_circuit_t *intro_circ)
  439. {
  440. crypto_pk_t *key1 = NULL;
  441. int retval;
  442. uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  443. ssize_t cell_len = 0;
  444. char circ_nonce[DIGEST_LEN] = {0};
  445. tt_assert(intro_circ);
  446. /* Prepare the circuit for the incoming ESTABLISH_INTRO */
  447. crypto_rand(circ_nonce, sizeof(circ_nonce));
  448. helper_prepare_circ_for_intro(intro_circ, circ_nonce);
  449. /* Send legacy establish_intro */
  450. key1 = pk_generate(0);
  451. /* Use old circ_nonce why not */
  452. cell_len = rend_service_encode_establish_intro_cell(
  453. (char*)cell_body,
  454. sizeof(cell_body), key1,
  455. circ_nonce);
  456. tt_int_op(cell_len, OP_GT, 0);
  457. /* Receive legacy establish_intro */
  458. retval = hs_intro_received_establish_intro(intro_circ,
  459. cell_body, (size_t) cell_len);
  460. tt_int_op(retval, OP_EQ, 0);
  461. done:
  462. return key1;
  463. }
  464. /* Helper function: test circuitmap free_all function outside of
  465. * test_intro_point_registration to prevent Coverity from seeing a
  466. * double free if the assertion hypothetically fails.
  467. */
  468. static void
  469. test_circuitmap_free_all(void)
  470. {
  471. hs_circuitmap_ht *the_hs_circuitmap = NULL;
  472. the_hs_circuitmap = get_hs_circuitmap();
  473. tt_assert(the_hs_circuitmap);
  474. hs_circuitmap_free_all();
  475. the_hs_circuitmap = get_hs_circuitmap();
  476. tt_ptr_op(the_hs_circuitmap, OP_EQ, NULL);
  477. done:
  478. ;
  479. }
  480. /** Successfully register a v2 intro point and a v3 intro point. Ensure that HS
  481. * circuitmap is maintained properly. */
  482. static void
  483. test_intro_point_registration(void *arg)
  484. {
  485. int retval;
  486. hs_circuitmap_ht *the_hs_circuitmap = NULL;
  487. or_circuit_t *intro_circ = NULL;
  488. trn_cell_establish_intro_t *establish_intro_cell = NULL;
  489. ed25519_public_key_t auth_key;
  490. crypto_pk_t *legacy_auth_key = NULL;
  491. or_circuit_t *legacy_intro_circ = NULL;
  492. or_circuit_t *returned_intro_circ = NULL;
  493. (void) arg;
  494. MOCK(hs_intro_send_intro_established_cell, mock_send_intro_established_cell);
  495. hs_circuitmap_init();
  496. /* Check that the circuitmap is currently empty */
  497. {
  498. the_hs_circuitmap = get_hs_circuitmap();
  499. tt_assert(the_hs_circuitmap);
  500. tt_int_op(0, OP_EQ, HT_SIZE(the_hs_circuitmap));
  501. /* Do a circuitmap query in any case */
  502. returned_intro_circ =hs_circuitmap_get_intro_circ_v3_relay_side(&auth_key);
  503. tt_ptr_op(returned_intro_circ, OP_EQ, NULL);
  504. }
  505. /* Create a v3 intro point */
  506. {
  507. intro_circ = or_circuit_new(0, NULL);
  508. tt_assert(intro_circ);
  509. establish_intro_cell = helper_establish_intro_v3(intro_circ);
  510. /* Check that the intro point was registered on the HS circuitmap */
  511. the_hs_circuitmap = get_hs_circuitmap();
  512. tt_assert(the_hs_circuitmap);
  513. tt_int_op(1, OP_EQ, HT_SIZE(the_hs_circuitmap));
  514. get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO,
  515. establish_intro_cell);
  516. returned_intro_circ =
  517. hs_circuitmap_get_intro_circ_v3_relay_side(&auth_key);
  518. tt_ptr_op(intro_circ, OP_EQ, returned_intro_circ);
  519. }
  520. /* Create a v2 intro point */
  521. {
  522. char key_digest[DIGEST_LEN];
  523. legacy_intro_circ = or_circuit_new(1, NULL);
  524. tt_assert(legacy_intro_circ);
  525. legacy_auth_key = helper_establish_intro_v2(legacy_intro_circ);
  526. tt_assert(legacy_auth_key);
  527. /* Check that the circuitmap now has two elements */
  528. the_hs_circuitmap = get_hs_circuitmap();
  529. tt_assert(the_hs_circuitmap);
  530. tt_int_op(2, OP_EQ, HT_SIZE(the_hs_circuitmap));
  531. /* Check that the new element is our legacy intro circuit. */
  532. retval = crypto_pk_get_digest(legacy_auth_key, key_digest);
  533. tt_int_op(retval, OP_EQ, 0);
  534. returned_intro_circ =
  535. hs_circuitmap_get_intro_circ_v2_relay_side((uint8_t*)key_digest);
  536. tt_ptr_op(legacy_intro_circ, OP_EQ, returned_intro_circ);
  537. }
  538. /* XXX Continue test and try to register a second v3 intro point with the
  539. * same auth key. Make sure that old intro circuit gets closed. */
  540. done:
  541. crypto_pk_free(legacy_auth_key);
  542. circuit_free_(TO_CIRCUIT(intro_circ));
  543. circuit_free_(TO_CIRCUIT(legacy_intro_circ));
  544. trn_cell_establish_intro_free(establish_intro_cell);
  545. test_circuitmap_free_all();
  546. UNMOCK(hs_intro_send_intro_established_cell);
  547. }
  548. static void
  549. test_introduce1_suitable_circuit(void *arg)
  550. {
  551. int ret;
  552. or_circuit_t *circ = NULL;
  553. (void) arg;
  554. /* Valid suitable circuit. */
  555. {
  556. circ = or_circuit_new(0, NULL);
  557. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
  558. ret = circuit_is_suitable_for_introduce1(circ);
  559. circuit_free_(TO_CIRCUIT(circ));
  560. tt_int_op(ret, OP_EQ, 1);
  561. }
  562. /* Test if the circuit purpose safeguard works correctly. */
  563. {
  564. circ = or_circuit_new(0, NULL);
  565. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
  566. ret = circuit_is_suitable_for_introduce1(circ);
  567. circuit_free_(TO_CIRCUIT(circ));
  568. tt_int_op(ret, OP_EQ, 0);
  569. }
  570. /* Test the non-edge circuit safeguard works correctly. */
  571. {
  572. circ = or_circuit_new(0, NULL);
  573. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
  574. /* Bogus pointer, the check is against NULL on n_chan. */
  575. circ->base_.n_chan = (channel_t *) circ;
  576. ret = circuit_is_suitable_for_introduce1(circ);
  577. circuit_free_(TO_CIRCUIT(circ));
  578. tt_int_op(ret, OP_EQ, 0);
  579. }
  580. /* Mangle the circuit a bit more so see if our only one INTRODUCE1 cell
  581. * limit works correctly. */
  582. {
  583. circ = or_circuit_new(0, NULL);
  584. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
  585. circ->already_received_introduce1 = 1;
  586. ret = circuit_is_suitable_for_introduce1(circ);
  587. circuit_free_(TO_CIRCUIT(circ));
  588. tt_int_op(ret, OP_EQ, 0);
  589. }
  590. /* Single hop circuit should not be allowed. */
  591. {
  592. circ = or_circuit_new(0, NULL);
  593. circ->p_chan = tor_malloc_zero(sizeof(channel_t));
  594. circ->p_chan->is_client = 1;
  595. ret = circuit_is_suitable_for_introduce1(circ);
  596. tor_free(circ->p_chan);
  597. circuit_free_(TO_CIRCUIT(circ));
  598. tt_int_op(ret, OP_EQ, 0);
  599. }
  600. done:
  601. ;
  602. }
  603. static void
  604. test_introduce1_is_legacy(void *arg)
  605. {
  606. int ret;
  607. uint8_t request[256];
  608. (void) arg;
  609. /* For a cell to be considered legacy, according to the specification, the
  610. * first 20 bytes MUST BE non-zero else it's a v3 cell. */
  611. memset(request, 'a', DIGEST_LEN);
  612. memset(request + DIGEST_LEN, 0, sizeof(request) - DIGEST_LEN);
  613. ret = introduce1_cell_is_legacy(request);
  614. tt_int_op(ret, OP_EQ, 1);
  615. /* This is a NON legacy cell. */
  616. memset(request, 0, DIGEST_LEN);
  617. memset(request + DIGEST_LEN, 'a', sizeof(request) - DIGEST_LEN);
  618. ret = introduce1_cell_is_legacy(request);
  619. tt_int_op(ret, OP_EQ, 0);
  620. done:
  621. ;
  622. }
  623. static void
  624. test_introduce1_validation(void *arg)
  625. {
  626. int ret;
  627. trn_cell_introduce1_t *cell = NULL;
  628. (void) arg;
  629. /* Create our decoy cell that we'll modify as we go to test the validation
  630. * function of that parsed cell. */
  631. cell = helper_create_introduce1_cell();
  632. tt_assert(cell);
  633. /* It should NOT be a legacy cell which will trigger a BUG(). */
  634. memset(cell->legacy_key_id, 'a', sizeof(cell->legacy_key_id));
  635. tor_capture_bugs_(1);
  636. ret = validate_introduce1_parsed_cell(cell);
  637. tor_end_capture_bugs_();
  638. tt_int_op(ret, OP_EQ, -1);
  639. /* Reset legacy ID and make sure it's correct. */
  640. memset(cell->legacy_key_id, 0, sizeof(cell->legacy_key_id));
  641. ret = validate_introduce1_parsed_cell(cell);
  642. tt_int_op(ret, OP_EQ, 0);
  643. /* Non existing auth key type. */
  644. cell->auth_key_type = 42;
  645. ret = validate_introduce1_parsed_cell(cell);
  646. tt_int_op(ret, OP_EQ, -1);
  647. /* Reset is to correct value and make sure it's correct. */
  648. cell->auth_key_type = TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519;
  649. ret = validate_introduce1_parsed_cell(cell);
  650. tt_int_op(ret, OP_EQ, 0);
  651. /* Really bad key length. */
  652. cell->auth_key_len = 0;
  653. ret = validate_introduce1_parsed_cell(cell);
  654. tt_int_op(ret, OP_EQ, -1);
  655. cell->auth_key_len = UINT16_MAX;
  656. ret = validate_introduce1_parsed_cell(cell);
  657. tt_int_op(ret, OP_EQ, -1);
  658. /* Correct size, let's try that. */
  659. cell->auth_key_len = sizeof(ed25519_public_key_t);
  660. ret = validate_introduce1_parsed_cell(cell);
  661. tt_int_op(ret, OP_EQ, 0);
  662. /* Set an invalid size of the auth key buffer. */
  663. trn_cell_introduce1_setlen_auth_key(cell, 3);
  664. ret = validate_introduce1_parsed_cell(cell);
  665. tt_int_op(ret, OP_EQ, -1);
  666. /* Reset auth key buffer and make sure it works. */
  667. trn_cell_introduce1_setlen_auth_key(cell, sizeof(ed25519_public_key_t));
  668. ret = validate_introduce1_parsed_cell(cell);
  669. tt_int_op(ret, OP_EQ, 0);
  670. /* Empty encrypted section. */
  671. trn_cell_introduce1_setlen_encrypted(cell, 0);
  672. ret = validate_introduce1_parsed_cell(cell);
  673. tt_int_op(ret, OP_EQ, -1);
  674. /* Reset it to some non zero bytes and validate. */
  675. trn_cell_introduce1_setlen_encrypted(cell, 1);
  676. ret = validate_introduce1_parsed_cell(cell);
  677. tt_int_op(ret, OP_EQ, 0);
  678. done:
  679. trn_cell_introduce1_free(cell);
  680. }
  681. static void
  682. test_received_introduce1_handling(void *arg)
  683. {
  684. int ret;
  685. uint8_t *request = NULL, buf[128];
  686. trn_cell_introduce1_t *cell = NULL;
  687. or_circuit_t *circ = NULL;
  688. (void) arg;
  689. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  690. hs_circuitmap_init();
  691. /* Too small request length. An INTRODUCE1 expect at the very least a
  692. * DIGEST_LEN size. */
  693. {
  694. memset(buf, 0, sizeof(buf));
  695. circ = helper_create_intro_circuit();
  696. ret = hs_intro_received_introduce1(circ, buf, DIGEST_LEN - 1);
  697. tt_int_op(ret, OP_EQ, -1);
  698. circuit_free_(TO_CIRCUIT(circ));
  699. }
  700. /* We have a unit test only for the suitability of a circuit to receive an
  701. * INTRODUCE1 cell so from now on we'll only test the handling of a cell. */
  702. /* Bad request. */
  703. {
  704. circ = helper_create_intro_circuit();
  705. uint8_t test[2]; /* Too small request. */
  706. memset(test, 0, sizeof(test));
  707. ret = handle_introduce1(circ, test, sizeof(test));
  708. tor_free(circ->p_chan);
  709. circuit_free_(TO_CIRCUIT(circ));
  710. tt_int_op(ret, OP_EQ, -1);
  711. }
  712. /* Valid case. */
  713. {
  714. cell = helper_create_introduce1_cell();
  715. ssize_t request_len = trn_cell_introduce1_encoded_len(cell);
  716. tt_int_op((int)request_len, OP_GT, 0);
  717. request = tor_malloc_zero(request_len);
  718. ssize_t encoded_len =
  719. trn_cell_introduce1_encode(request, request_len, cell);
  720. tt_int_op((int)encoded_len, OP_GT, 0);
  721. circ = helper_create_intro_circuit();
  722. or_circuit_t *service_circ = helper_create_intro_circuit();
  723. circuit_change_purpose(TO_CIRCUIT(service_circ),
  724. CIRCUIT_PURPOSE_INTRO_POINT);
  725. /* Register the circuit in the map for the auth key of the cell. */
  726. ed25519_public_key_t auth_key;
  727. const uint8_t *cell_auth_key =
  728. trn_cell_introduce1_getconstarray_auth_key(cell);
  729. memcpy(auth_key.pubkey, cell_auth_key, ED25519_PUBKEY_LEN);
  730. hs_circuitmap_register_intro_circ_v3_relay_side(service_circ, &auth_key);
  731. ret = hs_intro_received_introduce1(circ, request, request_len);
  732. circuit_free_(TO_CIRCUIT(circ));
  733. circuit_free_(TO_CIRCUIT(service_circ));
  734. tt_int_op(ret, OP_EQ, 0);
  735. }
  736. /* Valid legacy cell. */
  737. {
  738. tor_free(request);
  739. trn_cell_introduce1_free(cell);
  740. cell = helper_create_introduce1_cell();
  741. uint8_t *legacy_key_id = trn_cell_introduce1_getarray_legacy_key_id(cell);
  742. memset(legacy_key_id, 'a', DIGEST_LEN);
  743. /* Add an arbitrary amount of data for the payload of a v2 cell. */
  744. size_t request_len = trn_cell_introduce1_encoded_len(cell) + 256;
  745. tt_size_op(request_len, OP_GT, 0);
  746. request = tor_malloc_zero(request_len + 256);
  747. ssize_t encoded_len =
  748. trn_cell_introduce1_encode(request, request_len, cell);
  749. tt_int_op((int)encoded_len, OP_GT, 0);
  750. circ = helper_create_intro_circuit();
  751. or_circuit_t *service_circ = helper_create_intro_circuit();
  752. circuit_change_purpose(TO_CIRCUIT(service_circ),
  753. CIRCUIT_PURPOSE_INTRO_POINT);
  754. /* Register the circuit in the map for the auth key of the cell. */
  755. uint8_t token[REND_TOKEN_LEN];
  756. memcpy(token, legacy_key_id, sizeof(token));
  757. hs_circuitmap_register_intro_circ_v2_relay_side(service_circ, token);
  758. ret = hs_intro_received_introduce1(circ, request, request_len);
  759. circuit_free_(TO_CIRCUIT(circ));
  760. circuit_free_(TO_CIRCUIT(service_circ));
  761. tt_int_op(ret, OP_EQ, 0);
  762. }
  763. done:
  764. trn_cell_introduce1_free(cell);
  765. tor_free(request);
  766. hs_circuitmap_free_all();
  767. UNMOCK(relay_send_command_from_edge_);
  768. }
  769. static void *
  770. hs_subsystem_setup_fn(const struct testcase_t *tc)
  771. {
  772. (void) tc;
  773. return NULL;
  774. }
  775. static int
  776. hs_subsystem_cleanup_fn(const struct testcase_t *tc, void *arg)
  777. {
  778. (void) tc;
  779. (void) arg;
  780. return 1;
  781. }
  782. static struct testcase_setup_t test_setup = {
  783. hs_subsystem_setup_fn, hs_subsystem_cleanup_fn
  784. };
  785. struct testcase_t hs_intropoint_tests[] = {
  786. { "intro_point_registration",
  787. test_intro_point_registration, TT_FORK, NULL, &test_setup},
  788. { "receive_establish_intro_wrong_keytype",
  789. test_establish_intro_wrong_keytype, TT_FORK, NULL, &test_setup},
  790. { "receive_establish_intro_wrong_keytype2",
  791. test_establish_intro_wrong_keytype2, TT_FORK, NULL, &test_setup},
  792. { "receive_establish_intro_wrong_purpose",
  793. test_establish_intro_wrong_purpose, TT_FORK, NULL, &test_setup},
  794. { "receive_establish_intro_wrong_sig",
  795. test_establish_intro_wrong_sig, TT_FORK, NULL, &test_setup},
  796. { "receive_establish_intro_wrong_sig_len",
  797. test_establish_intro_wrong_sig_len, TT_FORK, NULL, &test_setup},
  798. { "receive_establish_intro_wrong_auth_key_len",
  799. test_establish_intro_wrong_auth_key_len, TT_FORK, NULL, &test_setup},
  800. { "receive_establish_intro_wrong_mac",
  801. test_establish_intro_wrong_mac, TT_FORK, NULL, &test_setup},
  802. { "introduce1_suitable_circuit",
  803. test_introduce1_suitable_circuit, TT_FORK, NULL, &test_setup},
  804. { "introduce1_is_legacy",
  805. test_introduce1_is_legacy, TT_FORK, NULL, &test_setup},
  806. { "introduce1_validation",
  807. test_introduce1_validation, TT_FORK, NULL, &test_setup},
  808. { "received_introduce1_handling",
  809. test_received_introduce1_handling, TT_FORK, NULL, &test_setup},
  810. END_OF_TESTCASES
  811. };