test_hs_intropoint.c 29 KB

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