test_relaycrypt.c 5.2 KB

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