test_hs_intropoint.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /* Copyright (c) 2016, 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 hs_cell_introduce1_t *
  63. helper_create_introduce1_cell(void)
  64. {
  65. hs_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 = hs_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. hs_cell_introduce1_set_auth_key_type(cell,
  77. HS_INTRO_AUTH_KEY_TYPE_ED25519);
  78. hs_cell_introduce1_set_auth_key_len(cell, auth_key_len);
  79. hs_cell_introduce1_setlen_auth_key(cell, auth_key_len);
  80. uint8_t *auth_key_ptr = hs_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. cell_extension_t *ext = cell_extension_new();
  86. cell_extension_set_num(ext, 0);
  87. hs_cell_introduce1_set_extensions(cell, ext);
  88. }
  89. /* Set the encrypted section to some data. */
  90. {
  91. size_t enc_len = 128;
  92. hs_cell_introduce1_setlen_encrypted(cell, enc_len);
  93. uint8_t *enc_ptr = hs_cell_introduce1_getarray_encrypted(cell);
  94. memset(enc_ptr, 'a', enc_len);
  95. }
  96. return cell;
  97. err:
  98. done:
  99. hs_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. hs_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. hs_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. hs_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. hs_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. hs_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. hs_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. hs_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. hs_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. hs_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. hs_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. hs_cell_establish_intro_set_auth_key_len(establish_intro_cell,
  285. bad_auth_key_len);
  286. hs_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. hs_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. hs_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. hs_cell_establish_intro_set_sig_len(establish_intro_cell, bad_sig_len);
  324. hs_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. hs_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. hs_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. hs_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 hs_cell_establish_intro_t *
  376. helper_establish_intro_v3(or_circuit_t *intro_circ)
  377. {
  378. int retval;
  379. hs_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. key1,
  420. (char *) circuit_key_material);
  421. tt_int_op(cell_len, >, 0);
  422. /* Receive legacy establish_intro */
  423. retval = hs_intro_received_establish_intro(intro_circ,
  424. cell_body, cell_len);
  425. tt_int_op(retval, ==, 0);
  426. done:
  427. return key1;
  428. }
  429. /** Successfuly register a v2 intro point and a v3 intro point. Ensure that HS
  430. * circuitmap is maintained properly. */
  431. static void
  432. test_intro_point_registration(void *arg)
  433. {
  434. int retval;
  435. hs_circuitmap_ht *the_hs_circuitmap = NULL;
  436. or_circuit_t *intro_circ = NULL;
  437. hs_cell_establish_intro_t *establish_intro_cell = NULL;
  438. ed25519_public_key_t auth_key;
  439. crypto_pk_t *legacy_auth_key = NULL;
  440. or_circuit_t *legacy_intro_circ = NULL;
  441. or_circuit_t *returned_intro_circ = NULL;
  442. (void) arg;
  443. MOCK(hs_intro_send_intro_established_cell, mock_send_intro_established_cell);
  444. hs_circuitmap_init();
  445. /* Check that the circuitmap is currently empty */
  446. {
  447. the_hs_circuitmap = get_hs_circuitmap();
  448. tt_assert(the_hs_circuitmap);
  449. tt_int_op(0, ==, HT_SIZE(the_hs_circuitmap));
  450. /* Do a circuitmap query in any case */
  451. returned_intro_circ = hs_circuitmap_get_intro_circ_v3(&auth_key);
  452. tt_ptr_op(returned_intro_circ, ==, NULL);
  453. }
  454. /* Create a v3 intro point */
  455. {
  456. intro_circ = or_circuit_new(0, NULL);
  457. tt_assert(intro_circ);
  458. establish_intro_cell = helper_establish_intro_v3(intro_circ);
  459. /* Check that the intro point was registered on the HS circuitmap */
  460. the_hs_circuitmap = get_hs_circuitmap();
  461. tt_assert(the_hs_circuitmap);
  462. tt_int_op(1, ==, HT_SIZE(the_hs_circuitmap));
  463. get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO,
  464. establish_intro_cell);
  465. returned_intro_circ = hs_circuitmap_get_intro_circ_v3(&auth_key);
  466. tt_ptr_op(intro_circ, ==, returned_intro_circ);
  467. }
  468. /* Create a v2 intro point */
  469. {
  470. char key_digest[DIGEST_LEN];
  471. legacy_intro_circ = or_circuit_new(1, NULL);
  472. tt_assert(legacy_intro_circ);
  473. legacy_auth_key = helper_establish_intro_v2(legacy_intro_circ);
  474. tt_assert(legacy_auth_key);
  475. /* Check that the circuitmap now has two elements */
  476. the_hs_circuitmap = get_hs_circuitmap();
  477. tt_assert(the_hs_circuitmap);
  478. tt_int_op(2, ==, HT_SIZE(the_hs_circuitmap));
  479. /* Check that the new element is our legacy intro circuit. */
  480. retval = crypto_pk_get_digest(legacy_auth_key, key_digest);
  481. tt_int_op(retval, ==, 0);
  482. returned_intro_circ= hs_circuitmap_get_intro_circ_v2((uint8_t*)key_digest);
  483. tt_ptr_op(legacy_intro_circ, ==, returned_intro_circ);
  484. }
  485. /* XXX Continue test and try to register a second v3 intro point with the
  486. * same auth key. Make sure that old intro circuit gets closed. */
  487. done:
  488. crypto_pk_free(legacy_auth_key);
  489. circuit_free(TO_CIRCUIT(intro_circ));
  490. circuit_free(TO_CIRCUIT(legacy_intro_circ));
  491. hs_cell_establish_intro_free(establish_intro_cell);
  492. { /* Test circuitmap free_all function. */
  493. the_hs_circuitmap = get_hs_circuitmap();
  494. tt_assert(the_hs_circuitmap);
  495. hs_circuitmap_free_all();
  496. the_hs_circuitmap = get_hs_circuitmap();
  497. tt_assert(!the_hs_circuitmap);
  498. }
  499. UNMOCK(hs_intro_send_intro_established_cell);
  500. }
  501. static void
  502. test_introduce1_suitable_circuit(void *arg)
  503. {
  504. int ret;
  505. or_circuit_t *circ = NULL;
  506. (void) arg;
  507. /* Valid suitable circuit. */
  508. {
  509. circ = or_circuit_new(0, NULL);
  510. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
  511. ret = circuit_is_suitable_for_introduce1(circ);
  512. circuit_free(TO_CIRCUIT(circ));
  513. tt_int_op(ret, OP_EQ, 1);
  514. }
  515. /* Test if the circuit purpose safeguard works correctly. */
  516. {
  517. circ = or_circuit_new(0, NULL);
  518. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
  519. ret = circuit_is_suitable_for_introduce1(circ);
  520. circuit_free(TO_CIRCUIT(circ));
  521. tt_int_op(ret, OP_EQ, 0);
  522. }
  523. /* Test the non-edge circuit safeguard works correctly. */
  524. {
  525. circ = or_circuit_new(0, NULL);
  526. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
  527. /* Bogus pointer, the check is against NULL on n_chan. */
  528. circ->base_.n_chan = (channel_t *) circ;
  529. ret = circuit_is_suitable_for_introduce1(circ);
  530. circuit_free(TO_CIRCUIT(circ));
  531. tt_int_op(ret, OP_EQ, 0);
  532. }
  533. /* Mangle the circuit a bit more so see if our only one INTRODUCE1 cell
  534. * limit works correctly. */
  535. {
  536. circ = or_circuit_new(0, NULL);
  537. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
  538. circ->already_received_introduce1 = 1;
  539. ret = circuit_is_suitable_for_introduce1(circ);
  540. circuit_free(TO_CIRCUIT(circ));
  541. tt_int_op(ret, OP_EQ, 0);
  542. }
  543. done:
  544. ;
  545. }
  546. static void
  547. test_introduce1_is_legacy(void *arg)
  548. {
  549. int ret;
  550. uint8_t request[256];
  551. (void) arg;
  552. /* For a cell to be considered legacy, according to the specification, the
  553. * first 20 bytes MUST BE non-zero else it's a v3 cell. */
  554. memset(request, 'a', DIGEST_LEN);
  555. memset(request + DIGEST_LEN, 0, sizeof(request) - DIGEST_LEN);
  556. ret = introduce1_cell_is_legacy(request);
  557. tt_int_op(ret, OP_EQ, 1);
  558. /* This is a NON legacy cell. */
  559. memset(request, 0, DIGEST_LEN);
  560. memset(request + DIGEST_LEN, 'a', sizeof(request) - DIGEST_LEN);
  561. ret = introduce1_cell_is_legacy(request);
  562. tt_int_op(ret, OP_EQ, 0);
  563. done:
  564. ;
  565. }
  566. static void
  567. test_introduce1_validation(void *arg)
  568. {
  569. int ret;
  570. hs_cell_introduce1_t *cell = NULL;
  571. (void) arg;
  572. /* Create our decoy cell that we'll modify as we go to test the validation
  573. * function of that parsed cell. */
  574. cell = helper_create_introduce1_cell();
  575. /* It should NOT be a legacy cell which will trigger a BUG(). */
  576. memset(cell->legacy_key_id, 'a', sizeof(cell->legacy_key_id));
  577. tor_capture_bugs_(1);
  578. ret = validate_introduce1_parsed_cell(cell);
  579. tor_end_capture_bugs_();
  580. tt_int_op(ret, OP_EQ, -1);
  581. /* Reset legacy ID and make sure it's correct. */
  582. memset(cell->legacy_key_id, 0, sizeof(cell->legacy_key_id));
  583. ret = validate_introduce1_parsed_cell(cell);
  584. tt_int_op(ret, OP_EQ, 0);
  585. /* Non existing auth key type. */
  586. cell->auth_key_type = 42;
  587. ret = validate_introduce1_parsed_cell(cell);
  588. tt_int_op(ret, OP_EQ, -1);
  589. /* Reset is to correct value and make sure it's correct. */
  590. cell->auth_key_type = HS_INTRO_AUTH_KEY_TYPE_ED25519;
  591. ret = validate_introduce1_parsed_cell(cell);
  592. tt_int_op(ret, OP_EQ, 0);
  593. /* Really bad key length. */
  594. cell->auth_key_len = 0;
  595. ret = validate_introduce1_parsed_cell(cell);
  596. tt_int_op(ret, OP_EQ, -1);
  597. cell->auth_key_len = UINT16_MAX;
  598. ret = validate_introduce1_parsed_cell(cell);
  599. tt_int_op(ret, OP_EQ, -1);
  600. /* Correct size, let's try that. */
  601. cell->auth_key_len = sizeof(ed25519_public_key_t);
  602. ret = validate_introduce1_parsed_cell(cell);
  603. tt_int_op(ret, OP_EQ, 0);
  604. /* Set an invalid size of the auth key buffer. */
  605. hs_cell_introduce1_setlen_auth_key(cell, 3);
  606. ret = validate_introduce1_parsed_cell(cell);
  607. tt_int_op(ret, OP_EQ, -1);
  608. /* Reset auth key buffer and make sure it works. */
  609. hs_cell_introduce1_setlen_auth_key(cell, sizeof(ed25519_public_key_t));
  610. ret = validate_introduce1_parsed_cell(cell);
  611. tt_int_op(ret, OP_EQ, 0);
  612. /* Empty encrypted section. */
  613. hs_cell_introduce1_setlen_encrypted(cell, 0);
  614. ret = validate_introduce1_parsed_cell(cell);
  615. tt_int_op(ret, OP_EQ, -1);
  616. /* Reset it to some non zero bytes and validate. */
  617. hs_cell_introduce1_setlen_encrypted(cell, 1);
  618. ret = validate_introduce1_parsed_cell(cell);
  619. tt_int_op(ret, OP_EQ, 0);
  620. done:
  621. hs_cell_introduce1_free(cell);
  622. }
  623. static void
  624. test_received_introduce1_handling(void *arg)
  625. {
  626. int ret;
  627. uint8_t *request = NULL, buf[128];
  628. hs_cell_introduce1_t *cell = NULL;
  629. or_circuit_t *circ = NULL;
  630. (void) arg;
  631. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  632. hs_circuitmap_init();
  633. /* Too small request length. An INTRODUCE1 expect at the very least a
  634. * DIGEST_LEN size. */
  635. {
  636. circ = helper_create_intro_circuit();
  637. ret = hs_intro_received_introduce1(circ, buf, DIGEST_LEN - 1);
  638. tt_int_op(ret, OP_EQ, -1);
  639. circuit_free(TO_CIRCUIT(circ));
  640. }
  641. /* We have a unit test only for the suitability of a circuit to receive an
  642. * INTRODUCE1 cell so from now on we'll only test the handling of a cell. */
  643. /* Bad request. */
  644. {
  645. circ = helper_create_intro_circuit();
  646. uint8_t test[2]; /* Too small request. */
  647. ret = handle_introduce1(circ, test, sizeof(test));
  648. tor_free(circ->p_chan);
  649. circuit_free(TO_CIRCUIT(circ));
  650. tt_int_op(ret, OP_EQ, -1);
  651. }
  652. /* Valid case. */
  653. {
  654. cell = helper_create_introduce1_cell();
  655. ssize_t request_len = hs_cell_introduce1_encoded_len(cell);
  656. tt_size_op(request_len, OP_GT, 0);
  657. request = tor_malloc_zero(request_len);
  658. ssize_t encoded_len =
  659. hs_cell_introduce1_encode(request, request_len, cell);
  660. tt_size_op(encoded_len, OP_GT, 0);
  661. circ = helper_create_intro_circuit();
  662. or_circuit_t *service_circ = helper_create_intro_circuit();
  663. circuit_change_purpose(TO_CIRCUIT(service_circ),
  664. CIRCUIT_PURPOSE_INTRO_POINT);
  665. /* Register the circuit in the map for the auth key of the cell. */
  666. ed25519_public_key_t auth_key;
  667. const uint8_t *cell_auth_key =
  668. hs_cell_introduce1_getconstarray_auth_key(cell);
  669. memcpy(auth_key.pubkey, cell_auth_key, ED25519_PUBKEY_LEN);
  670. hs_circuitmap_register_intro_circ_v3(service_circ, &auth_key);
  671. ret = hs_intro_received_introduce1(circ, request, request_len);
  672. circuit_free(TO_CIRCUIT(circ));
  673. circuit_free(TO_CIRCUIT(service_circ));
  674. tt_int_op(ret, OP_EQ, 0);
  675. }
  676. /* Valid legacy cell. */
  677. {
  678. tor_free(request);
  679. hs_cell_introduce1_free(cell);
  680. cell = helper_create_introduce1_cell();
  681. uint8_t *legacy_key_id = hs_cell_introduce1_getarray_legacy_key_id(cell);
  682. memset(legacy_key_id, 'a', DIGEST_LEN);
  683. /* Add an arbitrary amount of data for the payload of a v2 cell. */
  684. size_t request_len = hs_cell_introduce1_encoded_len(cell) + 256;
  685. tt_size_op(request_len, OP_GT, 0);
  686. request = tor_malloc_zero(request_len + 256);
  687. ssize_t encoded_len =
  688. hs_cell_introduce1_encode(request, request_len, cell);
  689. tt_size_op(encoded_len, OP_GT, 0);
  690. circ = helper_create_intro_circuit();
  691. or_circuit_t *service_circ = helper_create_intro_circuit();
  692. circuit_change_purpose(TO_CIRCUIT(service_circ),
  693. CIRCUIT_PURPOSE_INTRO_POINT);
  694. /* Register the circuit in the map for the auth key of the cell. */
  695. uint8_t token[REND_TOKEN_LEN];
  696. memcpy(token, legacy_key_id, sizeof(token));
  697. hs_circuitmap_register_intro_circ_v2(service_circ, token);
  698. ret = hs_intro_received_introduce1(circ, request, request_len);
  699. circuit_free(TO_CIRCUIT(circ));
  700. circuit_free(TO_CIRCUIT(service_circ));
  701. tt_int_op(ret, OP_EQ, 0);
  702. }
  703. done:
  704. hs_cell_introduce1_free(cell);
  705. tor_free(request);
  706. hs_circuitmap_free_all();
  707. UNMOCK(relay_send_command_from_edge_);
  708. }
  709. struct testcase_t hs_intropoint_tests[] = {
  710. { "intro_point_registration",
  711. test_intro_point_registration, TT_FORK, NULL, NULL },
  712. { "receive_establish_intro_wrong_keytype",
  713. test_establish_intro_wrong_keytype, TT_FORK, NULL, NULL },
  714. { "receive_establish_intro_wrong_keytype2",
  715. test_establish_intro_wrong_keytype2, TT_FORK, NULL, NULL },
  716. { "receive_establish_intro_wrong_purpose",
  717. test_establish_intro_wrong_purpose, TT_FORK, NULL, NULL },
  718. { "receive_establish_intro_wrong_sig",
  719. test_establish_intro_wrong_sig, TT_FORK, NULL, NULL },
  720. { "receive_establish_intro_wrong_sig_len",
  721. test_establish_intro_wrong_sig_len, TT_FORK, NULL, NULL },
  722. { "receive_establish_intro_wrong_auth_key_len",
  723. test_establish_intro_wrong_auth_key_len, TT_FORK, NULL, NULL },
  724. { "receive_establish_intro_wrong_mac",
  725. test_establish_intro_wrong_mac, TT_FORK, NULL, NULL },
  726. { "introduce1_suitable_circuit",
  727. test_introduce1_suitable_circuit, TT_FORK, NULL, NULL },
  728. { "introduce1_is_legacy",
  729. test_introduce1_is_legacy, TT_FORK, NULL, NULL },
  730. { "introduce1_validation",
  731. test_introduce1_validation, TT_FORK, NULL, NULL },
  732. { "received_introduce1_handling",
  733. test_received_introduce1_handling, TT_FORK, NULL, NULL },
  734. END_OF_TESTCASES
  735. };