test_hs_intropoint.c 29 KB

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