test_hs_intropoint.c 30 KB

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