test_hs_intropoint.c 28 KB

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