test_hs_intropoint.c 29 KB

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