test_relaycrypt.c 5.2 KB

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