test_hs_cell.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "crypto_rand.h"
  14. #include "hs_cell.h"
  15. #include "hs_intropoint.h"
  16. #include "hs_service.h"
  17. /* Trunnel. */
  18. #include "hs/cell_establish_intro.h"
  19. /** We simulate the creation of an outgoing ESTABLISH_INTRO cell, and then we
  20. * parse it from the receiver side. */
  21. static void
  22. test_gen_establish_intro_cell(void *arg)
  23. {
  24. (void) arg;
  25. ssize_t ret;
  26. char circ_nonce[DIGEST_LEN] = {0};
  27. uint8_t buf[RELAY_PAYLOAD_SIZE];
  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. /* We only need the auth key pair here. */
  34. hs_service_intro_point_t *ip = service_intro_point_new(NULL, 0);
  35. /* Auth key pair is generated in the constructor so we are all set for
  36. * using this IP object. */
  37. ret = hs_cell_build_establish_intro(circ_nonce, ip, buf);
  38. service_intro_point_free(ip);
  39. tt_u64_op(ret, OP_GT, 0);
  40. }
  41. /* Check the contents of the cell */
  42. {
  43. /* First byte is the auth key type: make sure its correct */
  44. tt_int_op(buf[0], OP_EQ, HS_INTRO_AUTH_KEY_TYPE_ED25519);
  45. /* Next two bytes is auth key len */
  46. tt_int_op(ntohs(get_uint16(buf+1)), OP_EQ, ED25519_PUBKEY_LEN);
  47. /* Skip to the number of extensions: no extensions */
  48. tt_int_op(buf[35], OP_EQ, 0);
  49. /* Skip to the sig len. Make sure it's the size of an ed25519 sig */
  50. tt_int_op(ntohs(get_uint16(buf+35+1+32)), OP_EQ, ED25519_SIG_LEN);
  51. }
  52. /* Parse it as the receiver */
  53. {
  54. ret = trn_cell_establish_intro_parse(&cell_in, buf, sizeof(buf));
  55. tt_u64_op(ret, OP_GT, 0);
  56. ret = verify_establish_intro_cell(cell_in,
  57. (const uint8_t *) circ_nonce,
  58. sizeof(circ_nonce));
  59. tt_u64_op(ret, OP_EQ, 0);
  60. }
  61. done:
  62. trn_cell_establish_intro_free(cell_in);
  63. }
  64. /* Mocked ed25519_sign_prefixed() function that always fails :) */
  65. static int
  66. mock_ed25519_sign_prefixed(ed25519_signature_t *signature_out,
  67. const uint8_t *msg, size_t msg_len,
  68. const char *prefix_str,
  69. const ed25519_keypair_t *keypair) {
  70. (void) signature_out;
  71. (void) msg;
  72. (void) msg_len;
  73. (void) prefix_str;
  74. (void) keypair;
  75. return -1;
  76. }
  77. /** We simulate a failure to create an ESTABLISH_INTRO cell */
  78. static void
  79. test_gen_establish_intro_cell_bad(void *arg)
  80. {
  81. (void) arg;
  82. ssize_t cell_len = 0;
  83. trn_cell_establish_intro_t *cell = NULL;
  84. char circ_nonce[DIGEST_LEN] = {0};
  85. hs_service_intro_point_t *ip = NULL;
  86. MOCK(ed25519_sign_prefixed, mock_ed25519_sign_prefixed);
  87. crypto_rand(circ_nonce, sizeof(circ_nonce));
  88. setup_full_capture_of_logs(LOG_WARN);
  89. /* Easiest way to make that function fail is to mock the
  90. ed25519_sign_prefixed() function and make it fail. */
  91. cell = trn_cell_establish_intro_new();
  92. tt_assert(cell);
  93. ip = service_intro_point_new(NULL, 0);
  94. cell_len = hs_cell_build_establish_intro(circ_nonce, ip, NULL);
  95. service_intro_point_free(ip);
  96. expect_log_msg_containing("Unable to make signature for "
  97. "ESTABLISH_INTRO cell.");
  98. teardown_capture_of_logs();
  99. tt_i64_op(cell_len, OP_EQ, -1);
  100. done:
  101. trn_cell_establish_intro_free(cell);
  102. UNMOCK(ed25519_sign_prefixed);
  103. }
  104. struct testcase_t hs_cell_tests[] = {
  105. { "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
  106. NULL, NULL },
  107. { "gen_establish_intro_cell_bad", test_gen_establish_intro_cell_bad, TT_FORK,
  108. NULL, NULL },
  109. END_OF_TESTCASES
  110. };