test_hs_intropoint.c 28 KB

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