test_hs_intropoint.c 29 KB

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