test_hs_service.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "test.h"
  10. #include "log_test_helpers.h"
  11. #include "crypto.h"
  12. #include "hs/cell_establish_intro.h"
  13. #include "hs_service.h"
  14. #include "hs_intropoint.h"
  15. #include "hs_ntor.h"
  16. /** We simulate the creation of an outgoing ESTABLISH_INTRO cell, and then we
  17. * parse it from the receiver side. */
  18. static void
  19. test_gen_establish_intro_cell(void *arg)
  20. {
  21. (void) arg;
  22. ssize_t retval;
  23. uint8_t circuit_key_material[DIGEST_LEN] = {0};
  24. uint8_t buf[RELAY_PAYLOAD_SIZE];
  25. hs_cell_establish_intro_t *cell_out = NULL;
  26. hs_cell_establish_intro_t *cell_in = NULL;
  27. crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  28. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  29. attempt to parse it. */
  30. {
  31. cell_out = generate_establish_intro_cell(circuit_key_material,
  32. sizeof(circuit_key_material));
  33. tt_assert(cell_out);
  34. retval = get_establish_intro_payload(buf, sizeof(buf), cell_out);
  35. tt_int_op(retval, >=, 0);
  36. }
  37. /* Parse it as the receiver */
  38. {
  39. ssize_t parse_result = hs_cell_establish_intro_parse(&cell_in,
  40. buf, sizeof(buf));
  41. tt_int_op(parse_result, >=, 0);
  42. retval = verify_establish_intro_cell(cell_in,
  43. circuit_key_material,
  44. sizeof(circuit_key_material));
  45. tt_int_op(retval, >=, 0);
  46. }
  47. done:
  48. hs_cell_establish_intro_free(cell_out);
  49. hs_cell_establish_intro_free(cell_in);
  50. }
  51. /* Mocked ed25519_sign_prefixed() function that always fails :) */
  52. static int
  53. mock_ed25519_sign_prefixed(ed25519_signature_t *signature_out,
  54. const uint8_t *msg, size_t msg_len,
  55. const char *prefix_str,
  56. const ed25519_keypair_t *keypair) {
  57. (void) signature_out;
  58. (void) msg;
  59. (void) msg_len;
  60. (void) prefix_str;
  61. (void) keypair;
  62. return -1;
  63. }
  64. /** We simulate a failure to create an ESTABLISH_INTRO cell */
  65. static void
  66. test_gen_establish_intro_cell_bad(void *arg)
  67. {
  68. (void) arg;
  69. hs_cell_establish_intro_t *cell = NULL;
  70. uint8_t circuit_key_material[DIGEST_LEN] = {0};
  71. MOCK(ed25519_sign_prefixed, mock_ed25519_sign_prefixed);
  72. crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  73. setup_full_capture_of_logs(LOG_WARN);
  74. /* Easiest way to make that function fail is to mock the
  75. ed25519_sign_prefixed() function and make it fail. */
  76. cell = generate_establish_intro_cell(circuit_key_material,
  77. sizeof(circuit_key_material));
  78. expect_log_msg_containing("Unable to gen signature for "
  79. "ESTABLISH_INTRO cell.");
  80. teardown_capture_of_logs();
  81. tt_assert(!cell);
  82. done:
  83. hs_cell_establish_intro_free(cell);
  84. UNMOCK(ed25519_sign_prefixed);
  85. }
  86. /** Test the HS ntor handshake. Simulate the sending of an encrypted INTRODUCE1
  87. * cell, and verify the proper derivation of decryption keys on the other end.
  88. * Then simulate the sending of an authenticated RENDEZVOUS1 cell and verify
  89. * the proper verification on the other end. */
  90. static void
  91. test_hs_ntor(void *arg)
  92. {
  93. int retval;
  94. uint8_t subcredential[DIGEST256_LEN];
  95. ed25519_keypair_t service_intro_auth_keypair;
  96. curve25519_keypair_t service_intro_enc_keypair;
  97. curve25519_keypair_t service_ephemeral_rend_keypair;
  98. curve25519_keypair_t client_ephemeral_enc_keypair;
  99. hs_ntor_intro_cell_keys_t client_hs_ntor_intro_cell_keys;
  100. hs_ntor_intro_cell_keys_t service_hs_ntor_intro_cell_keys;
  101. hs_ntor_rend_cell_keys_t service_hs_ntor_rend_cell_keys;
  102. hs_ntor_rend_cell_keys_t client_hs_ntor_rend_cell_keys;
  103. (void) arg;
  104. /* Generate fake data for this unittest */
  105. {
  106. /* Generate fake subcredential */
  107. memset(subcredential, 'Z', DIGEST256_LEN);
  108. /* service */
  109. curve25519_keypair_generate(&service_intro_enc_keypair, 0);
  110. ed25519_keypair_generate(&service_intro_auth_keypair, 0);
  111. curve25519_keypair_generate(&service_ephemeral_rend_keypair, 0);
  112. /* client */
  113. curve25519_keypair_generate(&client_ephemeral_enc_keypair, 0);
  114. }
  115. /* Client: Simulate the sending of an encrypted INTRODUCE1 cell */
  116. retval =
  117. hs_ntor_client_get_introduce1_keys(&service_intro_auth_keypair.pubkey,
  118. &service_intro_enc_keypair.pubkey,
  119. &client_ephemeral_enc_keypair,
  120. subcredential,
  121. &client_hs_ntor_intro_cell_keys);
  122. tt_int_op(retval, ==, 0);
  123. /* Service: Simulate the decryption of the received INTRODUCE1 */
  124. retval =
  125. hs_ntor_service_get_introduce1_keys(&service_intro_auth_keypair.pubkey,
  126. &service_intro_enc_keypair,
  127. &client_ephemeral_enc_keypair.pubkey,
  128. subcredential,
  129. &service_hs_ntor_intro_cell_keys);
  130. tt_int_op(retval, ==, 0);
  131. /* Test that the INTRODUCE1 encryption/mac keys match! */
  132. tt_mem_op(client_hs_ntor_intro_cell_keys.enc_key, OP_EQ,
  133. service_hs_ntor_intro_cell_keys.enc_key,
  134. CIPHER256_KEY_LEN);
  135. tt_mem_op(client_hs_ntor_intro_cell_keys.mac_key, OP_EQ,
  136. service_hs_ntor_intro_cell_keys.mac_key,
  137. DIGEST256_LEN);
  138. /* Service: Simulate creation of RENDEZVOUS1 key material. */
  139. retval =
  140. hs_ntor_service_get_rendezvous1_keys(&service_intro_auth_keypair.pubkey,
  141. &service_intro_enc_keypair,
  142. &service_ephemeral_rend_keypair,
  143. &client_ephemeral_enc_keypair.pubkey,
  144. &service_hs_ntor_rend_cell_keys);
  145. tt_int_op(retval, ==, 0);
  146. /* Client: Simulate the verification of a received RENDEZVOUS1 cell */
  147. retval =
  148. hs_ntor_client_get_rendezvous1_keys(&service_intro_auth_keypair.pubkey,
  149. &client_ephemeral_enc_keypair,
  150. &service_intro_enc_keypair.pubkey,
  151. &service_ephemeral_rend_keypair.pubkey,
  152. &client_hs_ntor_rend_cell_keys);
  153. tt_int_op(retval, ==, 0);
  154. /* Test that the RENDEZVOUS1 key material match! */
  155. tt_mem_op(client_hs_ntor_rend_cell_keys.rend_cell_auth_mac, OP_EQ,
  156. service_hs_ntor_rend_cell_keys.rend_cell_auth_mac,
  157. DIGEST256_LEN);
  158. tt_mem_op(client_hs_ntor_rend_cell_keys.ntor_key_seed, OP_EQ,
  159. service_hs_ntor_rend_cell_keys.ntor_key_seed,
  160. DIGEST256_LEN);
  161. done:
  162. ;
  163. }
  164. struct testcase_t hs_service_tests[] = {
  165. { "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
  166. NULL, NULL },
  167. { "gen_establish_intro_cell_bad", test_gen_establish_intro_cell_bad, TT_FORK,
  168. NULL, NULL },
  169. { "hs_ntor", test_hs_ntor, TT_FORK, NULL, NULL },
  170. END_OF_TESTCASES
  171. };