test_hs_cell.c 3.8 KB

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