test_hs_intropoint.c 28 KB

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