test_hs_intropoint.c 28 KB

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