test_hs_intropoint.c 29 KB

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