test_hs_service.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_COMMON_PRIVATE
  8. #define HS_SERVICE_PRIVATE
  9. #define HS_INTROPOINT_PRIVATE
  10. #include "test.h"
  11. #include "log_test_helpers.h"
  12. #include "crypto.h"
  13. #include "hs/cell_establish_intro.h"
  14. #include "hs_common.h"
  15. #include "hs_service.h"
  16. #include "hs_intropoint.h"
  17. #include "hs_ntor.h"
  18. /** We simulate the creation of an outgoing ESTABLISH_INTRO cell, and then we
  19. * parse it from the receiver side. */
  20. static void
  21. test_gen_establish_intro_cell(void *arg)
  22. {
  23. (void) arg;
  24. ssize_t retval;
  25. uint8_t circuit_key_material[DIGEST_LEN] = {0};
  26. uint8_t buf[RELAY_PAYLOAD_SIZE];
  27. trn_cell_establish_intro_t *cell_out = NULL;
  28. trn_cell_establish_intro_t *cell_in = NULL;
  29. crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  30. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  31. attempt to parse it. */
  32. {
  33. cell_out = generate_establish_intro_cell(circuit_key_material,
  34. sizeof(circuit_key_material));
  35. tt_assert(cell_out);
  36. retval = get_establish_intro_payload(buf, sizeof(buf), cell_out);
  37. tt_int_op(retval, >=, 0);
  38. }
  39. /* Parse it as the receiver */
  40. {
  41. ssize_t parse_result = trn_cell_establish_intro_parse(&cell_in,
  42. buf, sizeof(buf));
  43. tt_int_op(parse_result, >=, 0);
  44. retval = verify_establish_intro_cell(cell_in,
  45. circuit_key_material,
  46. sizeof(circuit_key_material));
  47. tt_int_op(retval, >=, 0);
  48. }
  49. done:
  50. trn_cell_establish_intro_free(cell_out);
  51. trn_cell_establish_intro_free(cell_in);
  52. }
  53. /* Mocked ed25519_sign_prefixed() function that always fails :) */
  54. static int
  55. mock_ed25519_sign_prefixed(ed25519_signature_t *signature_out,
  56. const uint8_t *msg, size_t msg_len,
  57. const char *prefix_str,
  58. const ed25519_keypair_t *keypair) {
  59. (void) signature_out;
  60. (void) msg;
  61. (void) msg_len;
  62. (void) prefix_str;
  63. (void) keypair;
  64. return -1;
  65. }
  66. /** We simulate a failure to create an ESTABLISH_INTRO cell */
  67. static void
  68. test_gen_establish_intro_cell_bad(void *arg)
  69. {
  70. (void) arg;
  71. trn_cell_establish_intro_t *cell = NULL;
  72. uint8_t circuit_key_material[DIGEST_LEN] = {0};
  73. MOCK(ed25519_sign_prefixed, mock_ed25519_sign_prefixed);
  74. crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  75. setup_full_capture_of_logs(LOG_WARN);
  76. /* Easiest way to make that function fail is to mock the
  77. ed25519_sign_prefixed() function and make it fail. */
  78. cell = generate_establish_intro_cell(circuit_key_material,
  79. sizeof(circuit_key_material));
  80. expect_log_msg_containing("Unable to gen signature for "
  81. "ESTABLISH_INTRO cell.");
  82. teardown_capture_of_logs();
  83. tt_assert(!cell);
  84. done:
  85. trn_cell_establish_intro_free(cell);
  86. UNMOCK(ed25519_sign_prefixed);
  87. }
  88. /** Test the HS ntor handshake. Simulate the sending of an encrypted INTRODUCE1
  89. * cell, and verify the proper derivation of decryption keys on the other end.
  90. * Then simulate the sending of an authenticated RENDEZVOUS1 cell and verify
  91. * the proper verification on the other end. */
  92. static void
  93. test_hs_ntor(void *arg)
  94. {
  95. int retval;
  96. uint8_t subcredential[DIGEST256_LEN];
  97. ed25519_keypair_t service_intro_auth_keypair;
  98. curve25519_keypair_t service_intro_enc_keypair;
  99. curve25519_keypair_t service_ephemeral_rend_keypair;
  100. curve25519_keypair_t client_ephemeral_enc_keypair;
  101. hs_ntor_intro_cell_keys_t client_hs_ntor_intro_cell_keys;
  102. hs_ntor_intro_cell_keys_t service_hs_ntor_intro_cell_keys;
  103. hs_ntor_rend_cell_keys_t service_hs_ntor_rend_cell_keys;
  104. hs_ntor_rend_cell_keys_t client_hs_ntor_rend_cell_keys;
  105. (void) arg;
  106. /* Generate fake data for this unittest */
  107. {
  108. /* Generate fake subcredential */
  109. memset(subcredential, 'Z', DIGEST256_LEN);
  110. /* service */
  111. curve25519_keypair_generate(&service_intro_enc_keypair, 0);
  112. ed25519_keypair_generate(&service_intro_auth_keypair, 0);
  113. curve25519_keypair_generate(&service_ephemeral_rend_keypair, 0);
  114. /* client */
  115. curve25519_keypair_generate(&client_ephemeral_enc_keypair, 0);
  116. }
  117. /* Client: Simulate the sending of an encrypted INTRODUCE1 cell */
  118. retval =
  119. hs_ntor_client_get_introduce1_keys(&service_intro_auth_keypair.pubkey,
  120. &service_intro_enc_keypair.pubkey,
  121. &client_ephemeral_enc_keypair,
  122. subcredential,
  123. &client_hs_ntor_intro_cell_keys);
  124. tt_int_op(retval, ==, 0);
  125. /* Service: Simulate the decryption of the received INTRODUCE1 */
  126. retval =
  127. hs_ntor_service_get_introduce1_keys(&service_intro_auth_keypair.pubkey,
  128. &service_intro_enc_keypair,
  129. &client_ephemeral_enc_keypair.pubkey,
  130. subcredential,
  131. &service_hs_ntor_intro_cell_keys);
  132. tt_int_op(retval, ==, 0);
  133. /* Test that the INTRODUCE1 encryption/mac keys match! */
  134. tt_mem_op(client_hs_ntor_intro_cell_keys.enc_key, OP_EQ,
  135. service_hs_ntor_intro_cell_keys.enc_key,
  136. CIPHER256_KEY_LEN);
  137. tt_mem_op(client_hs_ntor_intro_cell_keys.mac_key, OP_EQ,
  138. service_hs_ntor_intro_cell_keys.mac_key,
  139. DIGEST256_LEN);
  140. /* Service: Simulate creation of RENDEZVOUS1 key material. */
  141. retval =
  142. hs_ntor_service_get_rendezvous1_keys(&service_intro_auth_keypair.pubkey,
  143. &service_intro_enc_keypair,
  144. &service_ephemeral_rend_keypair,
  145. &client_ephemeral_enc_keypair.pubkey,
  146. &service_hs_ntor_rend_cell_keys);
  147. tt_int_op(retval, ==, 0);
  148. /* Client: Simulate the verification of a received RENDEZVOUS1 cell */
  149. retval =
  150. hs_ntor_client_get_rendezvous1_keys(&service_intro_auth_keypair.pubkey,
  151. &client_ephemeral_enc_keypair,
  152. &service_intro_enc_keypair.pubkey,
  153. &service_ephemeral_rend_keypair.pubkey,
  154. &client_hs_ntor_rend_cell_keys);
  155. tt_int_op(retval, ==, 0);
  156. /* Test that the RENDEZVOUS1 key material match! */
  157. tt_mem_op(client_hs_ntor_rend_cell_keys.rend_cell_auth_mac, OP_EQ,
  158. service_hs_ntor_rend_cell_keys.rend_cell_auth_mac,
  159. DIGEST256_LEN);
  160. tt_mem_op(client_hs_ntor_rend_cell_keys.ntor_key_seed, OP_EQ,
  161. service_hs_ntor_rend_cell_keys.ntor_key_seed,
  162. DIGEST256_LEN);
  163. done:
  164. ;
  165. }
  166. /** Test that our HS time period calculation functions work properly */
  167. static void
  168. test_time_period(void *arg)
  169. {
  170. (void) arg;
  171. uint64_t tn;
  172. int retval;
  173. time_t fake_time;
  174. /* Let's do the example in prop224 section [TIME-PERIODS] */
  175. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  176. &fake_time);
  177. tt_int_op(retval, ==, 0);
  178. /* Check that the time period number is right */
  179. tn = get_time_period_num(fake_time);
  180. tt_u64_op(tn, ==, 16903);
  181. /* Increase current time to 11:59:59 UTC and check that the time period
  182. number is still the same */
  183. fake_time += 3599;
  184. tn = get_time_period_num(fake_time);
  185. tt_u64_op(tn, ==, 16903);
  186. /* Now take time to 12:00:00 UTC and check that the time period rotated */
  187. fake_time += 1;
  188. tn = get_time_period_num(fake_time);
  189. tt_u64_op(tn, ==, 16904);
  190. /* Now also check our hs_get_next_time_period_num() function */
  191. tn = hs_get_next_time_period_num(fake_time);
  192. tt_u64_op(tn, ==, 16905);
  193. done:
  194. ;
  195. }
  196. struct testcase_t hs_service_tests[] = {
  197. { "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
  198. NULL, NULL },
  199. { "gen_establish_intro_cell_bad", test_gen_establish_intro_cell_bad, TT_FORK,
  200. NULL, NULL },
  201. { "hs_ntor", test_hs_ntor, TT_FORK,
  202. NULL, NULL },
  203. { "time_period", test_time_period, TT_FORK,
  204. NULL, NULL },
  205. END_OF_TESTCASES
  206. };