test_hs_intropoint.c 36 KB

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