test_relaycrypt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define CRYPT_PATH_PRIVATE
  6. #include "core/or/or.h"
  7. #include "core/or/circuitbuild.h"
  8. #define CIRCUITLIST_PRIVATE
  9. #include "core/or/circuitlist.h"
  10. #include "lib/crypt_ops/crypto_rand.h"
  11. #include "core/or/relay.h"
  12. #include "core/crypto/relay_crypto.h"
  13. #include "core/or/crypt_path.h"
  14. #include "core/or/cell_st.h"
  15. #include "core/or/or_circuit_st.h"
  16. #include "core/or/origin_circuit_st.h"
  17. #include "test/test.h"
  18. static const char KEY_MATERIAL[3][CPATH_KEY_MATERIAL_LEN] = {
  19. " 'My public key is in this signed x509 object', said Tom assertively.",
  20. "'Let's chart the pedal phlanges in the tomb', said Tom cryptographically",
  21. " 'Segmentation fault bugs don't _just happen_', said Tom seethingly.",
  22. };
  23. typedef struct testing_circuitset_t {
  24. or_circuit_t *or_circ[3];
  25. origin_circuit_t *origin_circ;
  26. } testing_circuitset_t;
  27. static int testing_circuitset_teardown(const struct testcase_t *testcase,
  28. void *ptr);
  29. static void *
  30. testing_circuitset_setup(const struct testcase_t *testcase)
  31. {
  32. testing_circuitset_t *cs = tor_malloc_zero(sizeof(testing_circuitset_t));
  33. int i;
  34. for (i=0; i<3; ++i) {
  35. cs->or_circ[i] = or_circuit_new(0, NULL);
  36. tt_int_op(0, OP_EQ,
  37. relay_crypto_init(&cs->or_circ[i]->crypto,
  38. KEY_MATERIAL[i], sizeof(KEY_MATERIAL[i]),
  39. 0, 0));
  40. }
  41. cs->origin_circ = origin_circuit_new();
  42. cs->origin_circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  43. for (i=0; i<3; ++i) {
  44. crypt_path_t *hop = tor_malloc_zero(sizeof(*hop));
  45. relay_crypto_init(&hop->pvt_crypto, KEY_MATERIAL[i],
  46. sizeof(KEY_MATERIAL[i]), 0, 0);
  47. hop->state = CPATH_STATE_OPEN;
  48. cpath_extend_linked_list(&cs->origin_circ->cpath, hop);
  49. tt_ptr_op(hop, OP_EQ, cs->origin_circ->cpath->prev);
  50. }
  51. return cs;
  52. done:
  53. testing_circuitset_teardown(testcase, cs);
  54. return NULL;
  55. }
  56. static int
  57. testing_circuitset_teardown(const struct testcase_t *testcase, void *ptr)
  58. {
  59. (void)testcase;
  60. testing_circuitset_t *cs = ptr;
  61. int i;
  62. for (i=0; i<3; ++i) {
  63. circuit_free_(TO_CIRCUIT(cs->or_circ[i]));
  64. }
  65. circuit_free_(TO_CIRCUIT(cs->origin_circ));
  66. tor_free(cs);
  67. return 1;
  68. }
  69. static const struct testcase_setup_t relaycrypt_setup = {
  70. testing_circuitset_setup, testing_circuitset_teardown
  71. };
  72. /* Test encrypting a cell to the final hop on a circuit, decrypting it
  73. * at each hop, and recognizing it at the other end. Then do it again
  74. * and again as the state evolves. */
  75. static void
  76. test_relaycrypt_outbound(void *arg)
  77. {
  78. testing_circuitset_t *cs = arg;
  79. tt_assert(cs);
  80. relay_header_t rh;
  81. cell_t orig;
  82. cell_t encrypted;
  83. int i, j;
  84. for (i = 0; i < 50; ++i) {
  85. crypto_rand((char *)&orig, sizeof(orig));
  86. relay_header_unpack(&rh, orig.payload);
  87. rh.recognized = 0;
  88. memset(rh.integrity, 0, sizeof(rh.integrity));
  89. relay_header_pack(orig.payload, &rh);
  90. memcpy(&encrypted, &orig, sizeof(orig));
  91. /* Encrypt the cell to the last hop */
  92. relay_encrypt_cell_outbound(&encrypted, cs->origin_circ,
  93. cs->origin_circ->cpath->prev);
  94. for (j = 0; j < 3; ++j) {
  95. crypt_path_t *layer_hint = NULL;
  96. char recognized = 0;
  97. int r = relay_decrypt_cell(TO_CIRCUIT(cs->or_circ[j]),
  98. &encrypted,
  99. CELL_DIRECTION_OUT,
  100. &layer_hint, &recognized);
  101. tt_int_op(r, OP_EQ, 0);
  102. tt_ptr_op(layer_hint, OP_EQ, NULL);
  103. tt_int_op(recognized != 0, OP_EQ, j == 2);
  104. }
  105. tt_mem_op(orig.payload, OP_EQ, encrypted.payload, CELL_PAYLOAD_SIZE);
  106. }
  107. done:
  108. ;
  109. }
  110. /* As above, but simulate inbound cells from the last hop. */
  111. static void
  112. test_relaycrypt_inbound(void *arg)
  113. {
  114. testing_circuitset_t *cs = arg;
  115. tt_assert(cs);
  116. relay_header_t rh;
  117. cell_t orig;
  118. cell_t encrypted;
  119. int i, j;
  120. for (i = 0; i < 50; ++i) {
  121. crypto_rand((char *)&orig, sizeof(orig));
  122. relay_header_unpack(&rh, orig.payload);
  123. rh.recognized = 0;
  124. memset(rh.integrity, 0, sizeof(rh.integrity));
  125. relay_header_pack(orig.payload, &rh);
  126. memcpy(&encrypted, &orig, sizeof(orig));
  127. /* Encrypt the cell to the last hop */
  128. relay_encrypt_cell_inbound(&encrypted, cs->or_circ[2]);
  129. crypt_path_t *layer_hint = NULL;
  130. char recognized = 0;
  131. int r;
  132. for (j = 1; j >= 0; --j) {
  133. r = relay_decrypt_cell(TO_CIRCUIT(cs->or_circ[j]),
  134. &encrypted,
  135. CELL_DIRECTION_IN,
  136. &layer_hint, &recognized);
  137. tt_int_op(r, OP_EQ, 0);
  138. tt_ptr_op(layer_hint, OP_EQ, NULL);
  139. tt_int_op(recognized, OP_EQ, 0);
  140. }
  141. relay_decrypt_cell(TO_CIRCUIT(cs->origin_circ),
  142. &encrypted,
  143. CELL_DIRECTION_IN,
  144. &layer_hint, &recognized);
  145. tt_int_op(r, OP_EQ, 0);
  146. tt_int_op(recognized, OP_EQ, 1);
  147. tt_ptr_op(layer_hint, OP_EQ, cs->origin_circ->cpath->prev);
  148. tt_mem_op(orig.payload, OP_EQ, encrypted.payload, CELL_PAYLOAD_SIZE);
  149. }
  150. done:
  151. ;
  152. }
  153. #define TEST(name) \
  154. { # name, test_relaycrypt_ ## name, 0, &relaycrypt_setup, NULL }
  155. struct testcase_t relaycrypt_tests[] = {
  156. TEST(outbound),
  157. TEST(inbound),
  158. END_OF_TESTCASES
  159. };