test_hs_intropoint.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_service.c
  5. * \brief Test hidden service functionality.
  6. */
  7. #define HS_SERVICE_PRIVATE
  8. #define HS_INTROPOINT_PRIVATE
  9. #define RENDSERVICE_PRIVATE
  10. #define CIRCUITLIST_PRIVATE
  11. #include "test.h"
  12. #include "log_test_helpers.h"
  13. #include "crypto.h"
  14. #include "log_test_helpers.h"
  15. #include "or.h"
  16. #include "circuitlist.h"
  17. #include "circuituse.h"
  18. #include "ht.h"
  19. #include "relay.h"
  20. #include "rendservice.h"
  21. #include "hs_cell.h"
  22. #include "hs_circuitmap.h"
  23. #include "hs_common.h"
  24. #include "hs_intropoint.h"
  25. #include "hs_service.h"
  26. /* Trunnel. */
  27. #include "hs/cell_establish_intro.h"
  28. #include "hs/cell_introduce1.h"
  29. #include "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_u64_op(cell_len, OP_GT, 0);
  44. cell_len = trn_cell_establish_intro_parse(&cell, buf, sizeof(buf));
  45. tt_int_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_u64_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 extentions 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_u64_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_u64_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_u64_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_int_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_int_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_u64_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_u64_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_assert(!the_hs_circuitmap);
  464. done:
  465. ;
  466. }
  467. /** Successfuly 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. /* 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. circ = helper_create_intro_circuit();
  671. ret = hs_intro_received_introduce1(circ, buf, DIGEST_LEN - 1);
  672. tt_int_op(ret, OP_EQ, -1);
  673. circuit_free(TO_CIRCUIT(circ));
  674. }
  675. /* We have a unit test only for the suitability of a circuit to receive an
  676. * INTRODUCE1 cell so from now on we'll only test the handling of a cell. */
  677. /* Bad request. */
  678. {
  679. circ = helper_create_intro_circuit();
  680. uint8_t test[2]; /* Too small request. */
  681. ret = handle_introduce1(circ, test, sizeof(test));
  682. tor_free(circ->p_chan);
  683. circuit_free(TO_CIRCUIT(circ));
  684. tt_int_op(ret, OP_EQ, -1);
  685. }
  686. /* Valid case. */
  687. {
  688. cell = helper_create_introduce1_cell();
  689. ssize_t request_len = trn_cell_introduce1_encoded_len(cell);
  690. tt_int_op((int)request_len, OP_GT, 0);
  691. request = tor_malloc_zero(request_len);
  692. ssize_t encoded_len =
  693. trn_cell_introduce1_encode(request, request_len, cell);
  694. tt_int_op((int)encoded_len, OP_GT, 0);
  695. circ = helper_create_intro_circuit();
  696. or_circuit_t *service_circ = helper_create_intro_circuit();
  697. circuit_change_purpose(TO_CIRCUIT(service_circ),
  698. CIRCUIT_PURPOSE_INTRO_POINT);
  699. /* Register the circuit in the map for the auth key of the cell. */
  700. ed25519_public_key_t auth_key;
  701. const uint8_t *cell_auth_key =
  702. trn_cell_introduce1_getconstarray_auth_key(cell);
  703. memcpy(auth_key.pubkey, cell_auth_key, ED25519_PUBKEY_LEN);
  704. hs_circuitmap_register_intro_circ_v3_relay_side(service_circ, &auth_key);
  705. ret = hs_intro_received_introduce1(circ, request, request_len);
  706. circuit_free(TO_CIRCUIT(circ));
  707. circuit_free(TO_CIRCUIT(service_circ));
  708. tt_int_op(ret, OP_EQ, 0);
  709. }
  710. /* Valid legacy cell. */
  711. {
  712. tor_free(request);
  713. trn_cell_introduce1_free(cell);
  714. cell = helper_create_introduce1_cell();
  715. uint8_t *legacy_key_id = trn_cell_introduce1_getarray_legacy_key_id(cell);
  716. memset(legacy_key_id, 'a', DIGEST_LEN);
  717. /* Add an arbitrary amount of data for the payload of a v2 cell. */
  718. size_t request_len = trn_cell_introduce1_encoded_len(cell) + 256;
  719. tt_size_op(request_len, OP_GT, 0);
  720. request = tor_malloc_zero(request_len + 256);
  721. ssize_t encoded_len =
  722. trn_cell_introduce1_encode(request, request_len, cell);
  723. tt_int_op((int)encoded_len, OP_GT, 0);
  724. circ = helper_create_intro_circuit();
  725. or_circuit_t *service_circ = helper_create_intro_circuit();
  726. circuit_change_purpose(TO_CIRCUIT(service_circ),
  727. CIRCUIT_PURPOSE_INTRO_POINT);
  728. /* Register the circuit in the map for the auth key of the cell. */
  729. uint8_t token[REND_TOKEN_LEN];
  730. memcpy(token, legacy_key_id, sizeof(token));
  731. hs_circuitmap_register_intro_circ_v2_relay_side(service_circ, token);
  732. ret = hs_intro_received_introduce1(circ, request, request_len);
  733. circuit_free(TO_CIRCUIT(circ));
  734. circuit_free(TO_CIRCUIT(service_circ));
  735. tt_int_op(ret, OP_EQ, 0);
  736. }
  737. done:
  738. trn_cell_introduce1_free(cell);
  739. tor_free(request);
  740. hs_circuitmap_free_all();
  741. UNMOCK(relay_send_command_from_edge_);
  742. }
  743. struct testcase_t hs_intropoint_tests[] = {
  744. { "intro_point_registration",
  745. test_intro_point_registration, TT_FORK, NULL, NULL },
  746. { "receive_establish_intro_wrong_keytype",
  747. test_establish_intro_wrong_keytype, TT_FORK, NULL, NULL },
  748. { "receive_establish_intro_wrong_keytype2",
  749. test_establish_intro_wrong_keytype2, TT_FORK, NULL, NULL },
  750. { "receive_establish_intro_wrong_purpose",
  751. test_establish_intro_wrong_purpose, TT_FORK, NULL, NULL },
  752. { "receive_establish_intro_wrong_sig",
  753. test_establish_intro_wrong_sig, TT_FORK, NULL, NULL },
  754. { "receive_establish_intro_wrong_sig_len",
  755. test_establish_intro_wrong_sig_len, TT_FORK, NULL, NULL },
  756. { "receive_establish_intro_wrong_auth_key_len",
  757. test_establish_intro_wrong_auth_key_len, TT_FORK, NULL, NULL },
  758. { "receive_establish_intro_wrong_mac",
  759. test_establish_intro_wrong_mac, TT_FORK, NULL, NULL },
  760. { "introduce1_suitable_circuit",
  761. test_introduce1_suitable_circuit, TT_FORK, NULL, NULL },
  762. { "introduce1_is_legacy",
  763. test_introduce1_is_legacy, TT_FORK, NULL, NULL },
  764. { "introduce1_validation",
  765. test_introduce1_validation, TT_FORK, NULL, NULL },
  766. { "received_introduce1_handling",
  767. test_received_introduce1_handling, TT_FORK, NULL, NULL },
  768. END_OF_TESTCASES
  769. };