test_hs_cell.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright (c) 2017, 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.h"
  10. #include "test_helpers.h"
  11. #include "log_test_helpers.h"
  12. #include "crypto_ed25519.h"
  13. #include "hs_cell.h"
  14. #include "hs_intropoint.h"
  15. #include "hs_service.h"
  16. /* Trunnel. */
  17. #include "hs/cell_establish_intro.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 ret;
  25. char circ_nonce[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(circ_nonce, sizeof(circ_nonce));
  30. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  31. attempt to parse it. */
  32. {
  33. cell_out = trn_cell_establish_intro_new();
  34. /* We only need the auth key pair here. */
  35. hs_service_intro_point_t *ip = service_intro_point_new(NULL, 0);
  36. /* Auth key pair is generated in the constructor so we are all set for
  37. * using this IP object. */
  38. ret = hs_cell_build_establish_intro(circ_nonce, ip, buf);
  39. service_intro_point_free(ip);
  40. tt_u64_op(ret, OP_GT, 0);
  41. ret = trn_cell_establish_intro_encode(buf, sizeof(buf), cell_out);
  42. tt_u64_op(ret, OP_GT, 0);
  43. }
  44. /* Parse it as the receiver */
  45. {
  46. ret = trn_cell_establish_intro_parse(&cell_in, buf, sizeof(buf));
  47. tt_u64_op(ret, OP_GT, 0);
  48. ret = verify_establish_intro_cell(cell_in,
  49. (const uint8_t *) circ_nonce,
  50. sizeof(circ_nonce));
  51. tt_u64_op(ret, OP_EQ, 0);
  52. }
  53. done:
  54. trn_cell_establish_intro_free(cell_in);
  55. trn_cell_establish_intro_free(cell_out);
  56. }
  57. /* Mocked ed25519_sign_prefixed() function that always fails :) */
  58. static int
  59. mock_ed25519_sign_prefixed(ed25519_signature_t *signature_out,
  60. const uint8_t *msg, size_t msg_len,
  61. const char *prefix_str,
  62. const ed25519_keypair_t *keypair) {
  63. (void) signature_out;
  64. (void) msg;
  65. (void) msg_len;
  66. (void) prefix_str;
  67. (void) keypair;
  68. return -1;
  69. }
  70. /** We simulate a failure to create an ESTABLISH_INTRO cell */
  71. static void
  72. test_gen_establish_intro_cell_bad(void *arg)
  73. {
  74. (void) arg;
  75. ssize_t cell_len = 0;
  76. trn_cell_establish_intro_t *cell = NULL;
  77. char circ_nonce[DIGEST_LEN] = {0};
  78. hs_service_intro_point_t *ip = NULL;
  79. MOCK(ed25519_sign_prefixed, mock_ed25519_sign_prefixed);
  80. crypto_rand(circ_nonce, sizeof(circ_nonce));
  81. setup_full_capture_of_logs(LOG_WARN);
  82. /* Easiest way to make that function fail is to mock the
  83. ed25519_sign_prefixed() function and make it fail. */
  84. cell = trn_cell_establish_intro_new();
  85. tt_assert(cell);
  86. ip = service_intro_point_new(NULL, 0);
  87. cell_len = hs_cell_build_establish_intro(circ_nonce, ip, NULL);
  88. service_intro_point_free(ip);
  89. expect_log_msg_containing("Unable to make signature for "
  90. "ESTABLISH_INTRO cell.");
  91. teardown_capture_of_logs();
  92. tt_u64_op(cell_len, OP_EQ, -1);
  93. done:
  94. trn_cell_establish_intro_free(cell);
  95. UNMOCK(ed25519_sign_prefixed);
  96. }
  97. struct testcase_t hs_cell_tests[] = {
  98. { "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
  99. NULL, NULL },
  100. { "gen_establish_intro_cell_bad", test_gen_establish_intro_cell_bad, TT_FORK,
  101. NULL, NULL },
  102. END_OF_TESTCASES
  103. };