test_relaycrypt.c 5.1 KB

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