relay_crypto.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "or.h"
  7. #include "config.h"
  8. #include "relay_crypto.h"
  9. #include "relay.h"
  10. /** Update digest from the payload of cell. Assign integrity part to
  11. * cell.
  12. */
  13. static void
  14. relay_set_digest(crypto_digest_t *digest, cell_t *cell)
  15. {
  16. char integrity[4];
  17. relay_header_t rh;
  18. crypto_digest_add_bytes(digest, (char*)cell->payload, CELL_PAYLOAD_SIZE);
  19. crypto_digest_get_digest(digest, integrity, 4);
  20. // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
  21. // integrity[0], integrity[1], integrity[2], integrity[3]);
  22. relay_header_unpack(&rh, cell->payload);
  23. memcpy(rh.integrity, integrity, 4);
  24. relay_header_pack(cell->payload, &rh);
  25. }
  26. /** Does the digest for this circuit indicate that this cell is for us?
  27. *
  28. * Update digest from the payload of cell (with the integrity part set
  29. * to 0). If the integrity part is valid, return 1, else restore digest
  30. * and cell to their original state and return 0.
  31. */
  32. static int
  33. relay_digest_matches(crypto_digest_t *digest, cell_t *cell)
  34. {
  35. uint32_t received_integrity, calculated_integrity;
  36. relay_header_t rh;
  37. crypto_digest_checkpoint_t backup_digest;
  38. crypto_digest_checkpoint(&backup_digest, digest);
  39. relay_header_unpack(&rh, cell->payload);
  40. memcpy(&received_integrity, rh.integrity, 4);
  41. memset(rh.integrity, 0, 4);
  42. relay_header_pack(cell->payload, &rh);
  43. // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
  44. // received_integrity[0], received_integrity[1],
  45. // received_integrity[2], received_integrity[3]);
  46. crypto_digest_add_bytes(digest, (char*) cell->payload, CELL_PAYLOAD_SIZE);
  47. crypto_digest_get_digest(digest, (char*) &calculated_integrity, 4);
  48. int rv = 1;
  49. if (calculated_integrity != received_integrity) {
  50. // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
  51. // (%d vs %d).", received_integrity, calculated_integrity);
  52. /* restore digest to its old form */
  53. crypto_digest_restore(digest, &backup_digest);
  54. /* restore the relay header */
  55. memcpy(rh.integrity, &received_integrity, 4);
  56. relay_header_pack(cell->payload, &rh);
  57. rv = 0;
  58. }
  59. memwipe(&backup_digest, 0, sizeof(backup_digest));
  60. return rv;
  61. }
  62. /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
  63. * (in place).
  64. *
  65. * Note that we use the same operation for encrypting and for decrypting.
  66. */
  67. static void
  68. relay_crypt_one_payload(crypto_cipher_t *cipher, uint8_t *in)
  69. {
  70. crypto_cipher_crypt_inplace(cipher, (char*) in, CELL_PAYLOAD_SIZE);
  71. }
  72. /** Do the appropriate en/decryptions for <b>cell</b> arriving on
  73. * <b>circ</b> in direction <b>cell_direction</b>.
  74. *
  75. * If cell_direction == CELL_DIRECTION_IN:
  76. * - If we're at the origin (we're the OP), for hops 1..N,
  77. * decrypt cell. If recognized, stop.
  78. * - Else (we're not the OP), encrypt one hop. Cell is not recognized.
  79. *
  80. * If cell_direction == CELL_DIRECTION_OUT:
  81. * - decrypt one hop. Check if recognized.
  82. *
  83. * If cell is recognized, set *recognized to 1, and set
  84. * *layer_hint to the hop that recognized it.
  85. *
  86. * Return -1 to indicate that we should mark the circuit for close,
  87. * else return 0.
  88. */
  89. int
  90. relay_decrypt_cell(circuit_t *circ, cell_t *cell,
  91. cell_direction_t cell_direction,
  92. crypt_path_t **layer_hint, char *recognized)
  93. {
  94. relay_header_t rh;
  95. tor_assert(circ);
  96. tor_assert(cell);
  97. tor_assert(recognized);
  98. tor_assert(cell_direction == CELL_DIRECTION_IN ||
  99. cell_direction == CELL_DIRECTION_OUT);
  100. if (cell_direction == CELL_DIRECTION_IN) {
  101. if (CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
  102. * We'll want to do layered decrypts. */
  103. crypt_path_t *thishop, *cpath = TO_ORIGIN_CIRCUIT(circ)->cpath;
  104. thishop = cpath;
  105. if (thishop->state != CPATH_STATE_OPEN) {
  106. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  107. "Relay cell before first created cell? Closing.");
  108. return -1;
  109. }
  110. do { /* Remember: cpath is in forward order, that is, first hop first. */
  111. tor_assert(thishop);
  112. /* decrypt one layer */
  113. relay_crypt_one_payload(thishop->b_crypto, cell->payload);
  114. relay_header_unpack(&rh, cell->payload);
  115. if (rh.recognized == 0) {
  116. /* it's possibly recognized. have to check digest to be sure. */
  117. if (relay_digest_matches(thishop->b_digest, cell)) {
  118. *recognized = 1;
  119. *layer_hint = thishop;
  120. return 0;
  121. }
  122. }
  123. thishop = thishop->next;
  124. } while (thishop != cpath && thishop->state == CPATH_STATE_OPEN);
  125. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  126. "Incoming cell at client not recognized. Closing.");
  127. return -1;
  128. } else {
  129. /* We're in the middle. Encrypt one layer. */
  130. relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->p_crypto, cell->payload);
  131. }
  132. } else /* cell_direction == CELL_DIRECTION_OUT */ {
  133. /* We're in the middle. Decrypt one layer. */
  134. relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->n_crypto, cell->payload);
  135. relay_header_unpack(&rh, cell->payload);
  136. if (rh.recognized == 0) {
  137. /* it's possibly recognized. have to check digest to be sure. */
  138. if (relay_digest_matches(TO_OR_CIRCUIT(circ)->n_digest, cell)) {
  139. *recognized = 1;
  140. return 0;
  141. }
  142. }
  143. }
  144. return 0;
  145. }
  146. /**
  147. * Encrypt a cell <b>cell</b> that we are creating, and sending outbound on
  148. * <b>circ</b> until the hop corresponding to <b>layer_hint</b>.
  149. */
  150. void
  151. relay_encrypt_cell_outbound(cell_t *cell,
  152. origin_circuit_t *circ,
  153. crypt_path_t *layer_hint)
  154. {
  155. crypt_path_t *thishop; /* counter for repeated crypts */
  156. relay_set_digest(layer_hint->f_digest, cell);
  157. thishop = layer_hint;
  158. /* moving from farthest to nearest hop */
  159. do {
  160. tor_assert(thishop);
  161. log_debug(LD_OR,"encrypting a layer of the relay cell.");
  162. relay_crypt_one_payload(thishop->f_crypto, cell->payload);
  163. thishop = thishop->prev;
  164. } while (thishop != circ->cpath->prev);
  165. }
  166. /**
  167. * Encrypt a cell <b>cell</b> that we are creating, and sending on
  168. * <b>circuit</b> to the origin.
  169. */
  170. void
  171. relay_encrypt_cell_inbound(cell_t *cell,
  172. or_circuit_t *or_circ)
  173. {
  174. relay_set_digest(or_circ->p_digest, cell);
  175. /* encrypt one layer */
  176. relay_crypt_one_payload(or_circ->p_crypto, cell->payload);
  177. }