test_hs_service.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 CIRCUITBUILD_PRIVATE
  8. #define CIRCUITLIST_PRIVATE
  9. #define CONNECTION_PRIVATE
  10. #define CRYPTO_PRIVATE
  11. #define HS_COMMON_PRIVATE
  12. #define HS_INTROPOINT_PRIVATE
  13. #define MAIN_PRIVATE
  14. #define TOR_CHANNEL_INTERNAL_
  15. #include "test.h"
  16. #include "log_test_helpers.h"
  17. #include "rend_test_helpers.h"
  18. #include "or.h"
  19. #include "channeltls.h"
  20. #include "circuitbuild.h"
  21. #include "circuitlist.h"
  22. #include "circuituse.h"
  23. #include "config.h"
  24. #include "connection.h"
  25. #include "hs_circuit.h"
  26. #include "hs_common.h"
  27. #include "hs_ident.h"
  28. #include "hs_intropoint.h"
  29. #include "hs_ntor.h"
  30. #include "hs_service.h"
  31. #include "main.h"
  32. #include "rendservice.h"
  33. /** We simulate the creation of an outgoing ESTABLISH_INTRO cell, and then we
  34. * parse it from the receiver side. */
  35. static void
  36. test_gen_establish_intro_cell(void *arg)
  37. {
  38. (void) arg;
  39. ssize_t retval;
  40. uint8_t circuit_key_material[DIGEST_LEN] = {0};
  41. uint8_t buf[RELAY_PAYLOAD_SIZE];
  42. trn_cell_establish_intro_t *cell_out = NULL;
  43. trn_cell_establish_intro_t *cell_in = NULL;
  44. crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  45. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  46. attempt to parse it. */
  47. {
  48. cell_out = generate_establish_intro_cell(circuit_key_material,
  49. sizeof(circuit_key_material));
  50. tt_assert(cell_out);
  51. retval = get_establish_intro_payload(buf, sizeof(buf), cell_out);
  52. tt_int_op(retval, >=, 0);
  53. }
  54. /* Parse it as the receiver */
  55. {
  56. ssize_t parse_result = trn_cell_establish_intro_parse(&cell_in,
  57. buf, sizeof(buf));
  58. tt_int_op(parse_result, >=, 0);
  59. retval = verify_establish_intro_cell(cell_in,
  60. circuit_key_material,
  61. sizeof(circuit_key_material));
  62. tt_int_op(retval, >=, 0);
  63. }
  64. done:
  65. trn_cell_establish_intro_free(cell_out);
  66. trn_cell_establish_intro_free(cell_in);
  67. }
  68. /* Mocked ed25519_sign_prefixed() function that always fails :) */
  69. static int
  70. mock_ed25519_sign_prefixed(ed25519_signature_t *signature_out,
  71. const uint8_t *msg, size_t msg_len,
  72. const char *prefix_str,
  73. const ed25519_keypair_t *keypair) {
  74. (void) signature_out;
  75. (void) msg;
  76. (void) msg_len;
  77. (void) prefix_str;
  78. (void) keypair;
  79. return -1;
  80. }
  81. /** We simulate a failure to create an ESTABLISH_INTRO cell */
  82. static void
  83. test_gen_establish_intro_cell_bad(void *arg)
  84. {
  85. (void) arg;
  86. trn_cell_establish_intro_t *cell = NULL;
  87. uint8_t circuit_key_material[DIGEST_LEN] = {0};
  88. MOCK(ed25519_sign_prefixed, mock_ed25519_sign_prefixed);
  89. crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  90. setup_full_capture_of_logs(LOG_WARN);
  91. /* Easiest way to make that function fail is to mock the
  92. ed25519_sign_prefixed() function and make it fail. */
  93. cell = generate_establish_intro_cell(circuit_key_material,
  94. sizeof(circuit_key_material));
  95. expect_log_msg_containing("Unable to gen signature for "
  96. "ESTABLISH_INTRO cell.");
  97. teardown_capture_of_logs();
  98. tt_assert(!cell);
  99. done:
  100. trn_cell_establish_intro_free(cell);
  101. UNMOCK(ed25519_sign_prefixed);
  102. }
  103. /** Test the HS ntor handshake. Simulate the sending of an encrypted INTRODUCE1
  104. * cell, and verify the proper derivation of decryption keys on the other end.
  105. * Then simulate the sending of an authenticated RENDEZVOUS1 cell and verify
  106. * the proper verification on the other end. */
  107. static void
  108. test_hs_ntor(void *arg)
  109. {
  110. int retval;
  111. uint8_t subcredential[DIGEST256_LEN];
  112. ed25519_keypair_t service_intro_auth_keypair;
  113. curve25519_keypair_t service_intro_enc_keypair;
  114. curve25519_keypair_t service_ephemeral_rend_keypair;
  115. curve25519_keypair_t client_ephemeral_enc_keypair;
  116. hs_ntor_intro_cell_keys_t client_hs_ntor_intro_cell_keys;
  117. hs_ntor_intro_cell_keys_t service_hs_ntor_intro_cell_keys;
  118. hs_ntor_rend_cell_keys_t service_hs_ntor_rend_cell_keys;
  119. hs_ntor_rend_cell_keys_t client_hs_ntor_rend_cell_keys;
  120. (void) arg;
  121. /* Generate fake data for this unittest */
  122. {
  123. /* Generate fake subcredential */
  124. memset(subcredential, 'Z', DIGEST256_LEN);
  125. /* service */
  126. curve25519_keypair_generate(&service_intro_enc_keypair, 0);
  127. ed25519_keypair_generate(&service_intro_auth_keypair, 0);
  128. curve25519_keypair_generate(&service_ephemeral_rend_keypair, 0);
  129. /* client */
  130. curve25519_keypair_generate(&client_ephemeral_enc_keypair, 0);
  131. }
  132. /* Client: Simulate the sending of an encrypted INTRODUCE1 cell */
  133. retval =
  134. hs_ntor_client_get_introduce1_keys(&service_intro_auth_keypair.pubkey,
  135. &service_intro_enc_keypair.pubkey,
  136. &client_ephemeral_enc_keypair,
  137. subcredential,
  138. &client_hs_ntor_intro_cell_keys);
  139. tt_int_op(retval, ==, 0);
  140. /* Service: Simulate the decryption of the received INTRODUCE1 */
  141. retval =
  142. hs_ntor_service_get_introduce1_keys(&service_intro_auth_keypair.pubkey,
  143. &service_intro_enc_keypair,
  144. &client_ephemeral_enc_keypair.pubkey,
  145. subcredential,
  146. &service_hs_ntor_intro_cell_keys);
  147. tt_int_op(retval, ==, 0);
  148. /* Test that the INTRODUCE1 encryption/mac keys match! */
  149. tt_mem_op(client_hs_ntor_intro_cell_keys.enc_key, OP_EQ,
  150. service_hs_ntor_intro_cell_keys.enc_key,
  151. CIPHER256_KEY_LEN);
  152. tt_mem_op(client_hs_ntor_intro_cell_keys.mac_key, OP_EQ,
  153. service_hs_ntor_intro_cell_keys.mac_key,
  154. DIGEST256_LEN);
  155. /* Service: Simulate creation of RENDEZVOUS1 key material. */
  156. retval =
  157. hs_ntor_service_get_rendezvous1_keys(&service_intro_auth_keypair.pubkey,
  158. &service_intro_enc_keypair,
  159. &service_ephemeral_rend_keypair,
  160. &client_ephemeral_enc_keypair.pubkey,
  161. &service_hs_ntor_rend_cell_keys);
  162. tt_int_op(retval, ==, 0);
  163. /* Client: Simulate the verification of a received RENDEZVOUS1 cell */
  164. retval =
  165. hs_ntor_client_get_rendezvous1_keys(&service_intro_auth_keypair.pubkey,
  166. &client_ephemeral_enc_keypair,
  167. &service_intro_enc_keypair.pubkey,
  168. &service_ephemeral_rend_keypair.pubkey,
  169. &client_hs_ntor_rend_cell_keys);
  170. tt_int_op(retval, ==, 0);
  171. /* Test that the RENDEZVOUS1 key material match! */
  172. tt_mem_op(client_hs_ntor_rend_cell_keys.rend_cell_auth_mac, OP_EQ,
  173. service_hs_ntor_rend_cell_keys.rend_cell_auth_mac,
  174. DIGEST256_LEN);
  175. tt_mem_op(client_hs_ntor_rend_cell_keys.ntor_key_seed, OP_EQ,
  176. service_hs_ntor_rend_cell_keys.ntor_key_seed,
  177. DIGEST256_LEN);
  178. done:
  179. ;
  180. }
  181. /** Test that our HS time period calculation functions work properly */
  182. static void
  183. test_time_period(void *arg)
  184. {
  185. (void) arg;
  186. uint64_t tn;
  187. int retval;
  188. time_t fake_time;
  189. /* Let's do the example in prop224 section [TIME-PERIODS] */
  190. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  191. &fake_time);
  192. tt_int_op(retval, ==, 0);
  193. /* Check that the time period number is right */
  194. tn = get_time_period_num(fake_time);
  195. tt_u64_op(tn, ==, 16903);
  196. /* Increase current time to 11:59:59 UTC and check that the time period
  197. number is still the same */
  198. fake_time += 3599;
  199. tn = get_time_period_num(fake_time);
  200. tt_u64_op(tn, ==, 16903);
  201. /* Now take time to 12:00:00 UTC and check that the time period rotated */
  202. fake_time += 1;
  203. tn = get_time_period_num(fake_time);
  204. tt_u64_op(tn, ==, 16904);
  205. /* Now also check our hs_get_next_time_period_num() function */
  206. tn = hs_get_next_time_period_num(fake_time);
  207. tt_u64_op(tn, ==, 16905);
  208. done:
  209. ;
  210. }
  211. /* Test: Ensure that setting up rendezvous circuits works correctly. */
  212. static void
  213. test_e2e_rend_circuit_setup(void *arg)
  214. {
  215. ed25519_public_key_t service_pk;
  216. origin_circuit_t *or_circ;
  217. int retval;
  218. /** In this test we create a v3 prop224 service-side rendezvous circuit.
  219. * We simulate an HS ntor key exchange with a client, and check that
  220. * the circuit was setup correctly and is ready to accept rendezvous data */
  221. (void) arg;
  222. /* Now make dummy circuit */
  223. {
  224. or_circ = origin_circuit_new();
  225. or_circ->base_.purpose = CIRCUIT_PURPOSE_S_CONNECT_REND;
  226. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  227. or_circ->build_state->is_internal = 1;
  228. /* prop224: Setup hs conn identifier on the stream */
  229. ed25519_secret_key_t sk;
  230. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  231. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  232. or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
  233. HS_IDENT_CIRCUIT_RENDEZVOUS);
  234. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  235. }
  236. /* Check number of hops */
  237. retval = cpath_get_n_hops(&or_circ->cpath);
  238. tt_int_op(retval, OP_EQ, 0);
  239. /* Setup the circuit: do the ntor key exchange */
  240. {
  241. uint8_t ntor_key_seed[DIGEST256_LEN] = {2};
  242. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  243. ntor_key_seed, sizeof(ntor_key_seed),
  244. 1);
  245. tt_int_op(retval, OP_EQ, 0);
  246. }
  247. /* See that a hop was added to the circuit's cpath */
  248. retval = cpath_get_n_hops(&or_circ->cpath);
  249. tt_int_op(retval, OP_EQ, 1);
  250. /* Check the digest algo */
  251. tt_int_op(or_circ->cpath->f_digest->algorithm, OP_EQ, DIGEST_SHA3_256);
  252. tt_int_op(or_circ->cpath->b_digest->algorithm, OP_EQ, DIGEST_SHA3_256);
  253. tt_assert(or_circ->cpath->f_crypto);
  254. tt_assert(or_circ->cpath->b_crypto);
  255. /* Ensure that circ purpose was changed */
  256. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  257. done:
  258. circuit_free(TO_CIRCUIT(or_circ));
  259. }
  260. struct testcase_t hs_service_tests[] = {
  261. { "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
  262. NULL, NULL },
  263. { "gen_establish_intro_cell_bad", test_gen_establish_intro_cell_bad, TT_FORK,
  264. NULL, NULL },
  265. { "hs_ntor", test_hs_ntor, TT_FORK,
  266. NULL, NULL },
  267. { "time_period", test_time_period, TT_FORK,
  268. NULL, NULL },
  269. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  270. NULL, NULL },
  271. END_OF_TESTCASES
  272. };