test_relaycrypt.c 5.3 KB

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