test_hs_cell.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (c) 2017-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_cell.c
  5. * \brief Test hidden service cell functionality.
  6. */
  7. #define HS_INTROPOINT_PRIVATE
  8. #define HS_SERVICE_PRIVATE
  9. #include "test/test.h"
  10. #include "test/test_helpers.h"
  11. #include "test/log_test_helpers.h"
  12. #include "lib/crypt_ops/crypto_ed25519.h"
  13. #include "lib/crypt_ops/crypto_rand.h"
  14. #include "feature/hs/hs_cell.h"
  15. #include "feature/hs/hs_intropoint.h"
  16. #include "feature/hs/hs_service.h"
  17. /* Trunnel. */
  18. #include "trunnel/hs/cell_common.h"
  19. #include "trunnel/hs/cell_establish_intro.h"
  20. /** We simulate the creation of an outgoing ESTABLISH_INTRO cell, and then we
  21. * parse it from the receiver side. */
  22. static void
  23. test_gen_establish_intro_cell(void *arg)
  24. {
  25. (void) arg;
  26. ssize_t ret;
  27. char circ_nonce[DIGEST_LEN] = {0};
  28. uint8_t buf[RELAY_PAYLOAD_SIZE];
  29. trn_cell_establish_intro_t *cell_in = NULL;
  30. crypto_rand(circ_nonce, sizeof(circ_nonce));
  31. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  32. attempt to parse it. */
  33. {
  34. hs_service_config_t config;
  35. memset(&config, 0, sizeof(config));
  36. /* We only need the auth key pair here. */
  37. hs_service_intro_point_t *ip = service_intro_point_new(NULL);
  38. /* Auth key pair is generated in the constructor so we are all set for
  39. * using this IP object. */
  40. ret = hs_cell_build_establish_intro(circ_nonce, &config, ip, buf);
  41. service_intro_point_free(ip);
  42. tt_u64_op(ret, OP_GT, 0);
  43. }
  44. /* Check the contents of the cell */
  45. {
  46. /* First byte is the auth key type: make sure its correct */
  47. tt_int_op(buf[0], OP_EQ, TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519);
  48. /* Next two bytes is auth key len */
  49. tt_int_op(ntohs(get_uint16(buf+1)), OP_EQ, ED25519_PUBKEY_LEN);
  50. /* Skip to the number of extensions: no extensions */
  51. tt_int_op(buf[35], OP_EQ, 0);
  52. /* Skip to the sig len. Make sure it's the size of an ed25519 sig */
  53. tt_int_op(ntohs(get_uint16(buf+35+1+32)), OP_EQ, ED25519_SIG_LEN);
  54. }
  55. /* Parse it as the receiver */
  56. {
  57. ret = trn_cell_establish_intro_parse(&cell_in, buf, sizeof(buf));
  58. tt_u64_op(ret, OP_GT, 0);
  59. ret = verify_establish_intro_cell(cell_in,
  60. (const uint8_t *) circ_nonce,
  61. sizeof(circ_nonce));
  62. tt_u64_op(ret, OP_EQ, 0);
  63. }
  64. done:
  65. trn_cell_establish_intro_free(cell_in);
  66. }
  67. /* Mocked ed25519_sign_prefixed() function that always fails :) */
  68. static int
  69. mock_ed25519_sign_prefixed(ed25519_signature_t *signature_out,
  70. const uint8_t *msg, size_t msg_len,
  71. const char *prefix_str,
  72. const ed25519_keypair_t *keypair) {
  73. (void) signature_out;
  74. (void) msg;
  75. (void) msg_len;
  76. (void) prefix_str;
  77. (void) keypair;
  78. return -1;
  79. }
  80. /** We simulate a failure to create an ESTABLISH_INTRO cell */
  81. static void
  82. test_gen_establish_intro_cell_bad(void *arg)
  83. {
  84. (void) arg;
  85. ssize_t cell_len = 0;
  86. trn_cell_establish_intro_t *cell = NULL;
  87. char circ_nonce[DIGEST_LEN] = {0};
  88. hs_service_intro_point_t *ip = NULL;
  89. hs_service_config_t config;
  90. memset(&config, 0, sizeof(config));
  91. MOCK(ed25519_sign_prefixed, mock_ed25519_sign_prefixed);
  92. crypto_rand(circ_nonce, sizeof(circ_nonce));
  93. setup_full_capture_of_logs(LOG_WARN);
  94. /* Easiest way to make that function fail is to mock the
  95. ed25519_sign_prefixed() function and make it fail. */
  96. cell = trn_cell_establish_intro_new();
  97. tt_assert(cell);
  98. ip = service_intro_point_new(NULL);
  99. cell_len = hs_cell_build_establish_intro(circ_nonce, &config, ip, NULL);
  100. service_intro_point_free(ip);
  101. expect_log_msg_containing("Unable to make signature for "
  102. "ESTABLISH_INTRO cell.");
  103. teardown_capture_of_logs();
  104. tt_i64_op(cell_len, OP_EQ, -1);
  105. done:
  106. trn_cell_establish_intro_free(cell);
  107. UNMOCK(ed25519_sign_prefixed);
  108. }
  109. static void
  110. test_gen_establish_intro_dos_ext(void *arg)
  111. {
  112. ssize_t ret;
  113. hs_service_config_t config;
  114. hs_service_intro_point_t *ip = NULL;
  115. trn_cell_extension_t *extensions = NULL;
  116. trn_cell_extension_dos_t *dos = NULL;
  117. (void) arg;
  118. memset(&config, 0, sizeof(config));
  119. ip = service_intro_point_new(NULL);
  120. tt_assert(ip);
  121. ip->support_intro2_dos_defense = 1;
  122. /* Case 1: No DoS parameters so no extension to be built. */
  123. extensions = build_establish_intro_extensions(&config, ip);
  124. tt_int_op(trn_cell_extension_get_num(extensions), OP_EQ, 0);
  125. trn_cell_extension_free(extensions);
  126. extensions = NULL;
  127. /* Case 2: Enable the DoS extension. Parameter set to 0 should indicate to
  128. * disable the defense on the intro point but there should be an extension
  129. * nonetheless in the cell. */
  130. config.has_dos_defense_enabled = 1;
  131. extensions = build_establish_intro_extensions(&config, ip);
  132. tt_int_op(trn_cell_extension_get_num(extensions), OP_EQ, 1);
  133. /* Validate the extension. */
  134. const trn_cell_extension_field_t *field =
  135. trn_cell_extension_getconst_fields(extensions, 0);
  136. tt_int_op(trn_cell_extension_field_get_field_type(field), OP_EQ,
  137. TRUNNEL_CELL_EXTENSION_TYPE_DOS);
  138. ret = trn_cell_extension_dos_parse(&dos,
  139. trn_cell_extension_field_getconstarray_field(field),
  140. trn_cell_extension_field_getlen_field(field));
  141. tt_int_op(ret, OP_EQ, 19);
  142. /* Rate per sec param. */
  143. const trn_cell_extension_dos_param_t *param =
  144. trn_cell_extension_dos_getconst_params(dos, 0);
  145. tt_int_op(trn_cell_extension_dos_param_get_type(param), OP_EQ,
  146. TRUNNEL_DOS_PARAM_TYPE_INTRO2_RATE_PER_SEC);
  147. tt_u64_op(trn_cell_extension_dos_param_get_value(param), OP_EQ, 0);
  148. /* Burst per sec param. */
  149. param = trn_cell_extension_dos_getconst_params(dos, 1);
  150. tt_int_op(trn_cell_extension_dos_param_get_type(param), OP_EQ,
  151. TRUNNEL_DOS_PARAM_TYPE_INTRO2_BURST_PER_SEC);
  152. tt_u64_op(trn_cell_extension_dos_param_get_value(param), OP_EQ, 0);
  153. trn_cell_extension_dos_free(dos); dos = NULL;
  154. trn_cell_extension_free(extensions); extensions = NULL;
  155. /* Case 3: Enable the DoS extension. Parameter set to some normal values. */
  156. config.has_dos_defense_enabled = 1;
  157. config.intro_dos_rate_per_sec = 42;
  158. config.intro_dos_burst_per_sec = 250;
  159. extensions = build_establish_intro_extensions(&config, ip);
  160. tt_int_op(trn_cell_extension_get_num(extensions), OP_EQ, 1);
  161. /* Validate the extension. */
  162. field = trn_cell_extension_getconst_fields(extensions, 0);
  163. tt_int_op(trn_cell_extension_field_get_field_type(field), OP_EQ,
  164. TRUNNEL_CELL_EXTENSION_TYPE_DOS);
  165. ret = trn_cell_extension_dos_parse(&dos,
  166. trn_cell_extension_field_getconstarray_field(field),
  167. trn_cell_extension_field_getlen_field(field));
  168. tt_int_op(ret, OP_EQ, 19);
  169. /* Rate per sec param. */
  170. param = trn_cell_extension_dos_getconst_params(dos, 0);
  171. tt_int_op(trn_cell_extension_dos_param_get_type(param), OP_EQ,
  172. TRUNNEL_DOS_PARAM_TYPE_INTRO2_RATE_PER_SEC);
  173. tt_u64_op(trn_cell_extension_dos_param_get_value(param), OP_EQ, 42);
  174. /* Burst per sec param. */
  175. param = trn_cell_extension_dos_getconst_params(dos, 1);
  176. tt_int_op(trn_cell_extension_dos_param_get_type(param), OP_EQ,
  177. TRUNNEL_DOS_PARAM_TYPE_INTRO2_BURST_PER_SEC);
  178. tt_u64_op(trn_cell_extension_dos_param_get_value(param), OP_EQ, 250);
  179. trn_cell_extension_dos_free(dos); dos = NULL;
  180. trn_cell_extension_free(extensions); extensions = NULL;
  181. done:
  182. service_intro_point_free(ip);
  183. trn_cell_extension_dos_free(dos);
  184. trn_cell_extension_free(extensions);
  185. }
  186. struct testcase_t hs_cell_tests[] = {
  187. { "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
  188. NULL, NULL },
  189. { "gen_establish_intro_cell_bad", test_gen_establish_intro_cell_bad, TT_FORK,
  190. NULL, NULL },
  191. { "gen_establish_intro_dos_ext", test_gen_establish_intro_dos_ext, TT_FORK,
  192. NULL, NULL },
  193. END_OF_TESTCASES
  194. };