bpp.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Abstraction of the binary packet protocols used in SSH.
  3. */
  4. #ifndef PUTTY_SSHBPP_H
  5. #define PUTTY_SSHBPP_H
  6. typedef struct BinaryPacketProtocolVtable BinaryPacketProtocolVtable;
  7. struct BinaryPacketProtocolVtable {
  8. void (*free)(BinaryPacketProtocol *);
  9. void (*handle_input)(BinaryPacketProtocol *);
  10. void (*handle_output)(BinaryPacketProtocol *);
  11. PktOut *(*new_pktout)(int type);
  12. void (*queue_disconnect)(BinaryPacketProtocol *,
  13. const char *msg, int category);
  14. uint32_t packet_size_limit;
  15. };
  16. struct BinaryPacketProtocol {
  17. const struct BinaryPacketProtocolVtable *vt;
  18. bufchain *in_raw, *out_raw;
  19. bool input_eof; /* set this if in_raw will never be added to again */
  20. PktInQueue in_pq;
  21. PktOutQueue out_pq;
  22. PacketLogSettings *pls;
  23. LogContext *logctx;
  24. Ssh *ssh;
  25. /* ic_in_raw is filled in by the BPP (probably by calling
  26. * ssh_bpp_common_setup). The BPP's owner triggers it when data is
  27. * added to in_raw, and also when the BPP is newly created. */
  28. IdempotentCallback ic_in_raw;
  29. /* ic_out_pq is entirely internal to the BPP itself; it's used as
  30. * the callback on out_pq. */
  31. IdempotentCallback ic_out_pq;
  32. /* Information that all packet layers sharing this BPP will
  33. * potentially be interested in. */
  34. int remote_bugs;
  35. bool ext_info_rsa_sha256_ok, ext_info_rsa_sha512_ok;
  36. /* Set this if remote connection closure should not generate an
  37. * error message (either because it's not to be treated as an
  38. * error at all, or because some other error message has already
  39. * been emitted). */
  40. bool expect_close;
  41. };
  42. static inline void ssh_bpp_handle_input(BinaryPacketProtocol *bpp)
  43. { bpp->vt->handle_input(bpp); }
  44. static inline void ssh_bpp_handle_output(BinaryPacketProtocol *bpp)
  45. { bpp->vt->handle_output(bpp); }
  46. static inline PktOut *ssh_bpp_new_pktout(BinaryPacketProtocol *bpp, int type)
  47. { return bpp->vt->new_pktout(type); }
  48. static inline void ssh_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  49. const char *msg, int category)
  50. { bpp->vt->queue_disconnect(bpp, msg, category); }
  51. /* ssh_bpp_free is more than just a macro wrapper on the vtable; it
  52. * does centralised parts of the freeing too. */
  53. void ssh_bpp_free(BinaryPacketProtocol *bpp);
  54. BinaryPacketProtocol *ssh1_bpp_new(LogContext *logctx);
  55. void ssh1_bpp_new_cipher(BinaryPacketProtocol *bpp,
  56. const ssh_cipheralg *cipher,
  57. const void *session_key);
  58. /* This is only called from outside the BPP in server mode; in client
  59. * mode the BPP detects compression start time automatically by
  60. * snooping message types */
  61. void ssh1_bpp_start_compression(BinaryPacketProtocol *bpp);
  62. /* Helper routine which does common BPP initialisation, e.g. setting
  63. * up in_pq and out_pq, and initialising input_consumer. */
  64. void ssh_bpp_common_setup(BinaryPacketProtocol *);
  65. /* Common helper functions between the SSH-2 full and bare BPPs */
  66. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  67. const char *msg, int category);
  68. bool ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin);
  69. /* Convenience macro for BPPs to send formatted strings to the Event
  70. * Log. Assumes a function parameter called 'bpp' is in scope. */
  71. #define bpp_logevent(...) ( \
  72. logevent_and_free((bpp)->logctx, dupprintf(__VA_ARGS__)))
  73. /*
  74. * Structure that tracks how much data is sent and received, for
  75. * purposes of triggering an SSH-2 rekey when either one gets over a
  76. * configured limit. In each direction, the flag 'running' indicates
  77. * that we haven't hit the limit yet, and 'remaining' tracks how much
  78. * longer until we do. The function dts_consume() subtracts a given
  79. * amount from the counter in a particular direction, and sets
  80. * 'expired' if the limit has been hit.
  81. *
  82. * The limit is sticky: once 'running' has flipped to false,
  83. * 'remaining' is no longer decremented, so it shouldn't dangerously
  84. * wrap round.
  85. */
  86. struct DataTransferStatsDirection {
  87. bool running, expired;
  88. unsigned long remaining;
  89. };
  90. struct DataTransferStats {
  91. struct DataTransferStatsDirection in, out;
  92. };
  93. static inline void dts_consume(struct DataTransferStatsDirection *s,
  94. unsigned long size_consumed)
  95. {
  96. if (s->running) {
  97. if (s->remaining <= size_consumed) {
  98. s->running = false;
  99. s->expired = true;
  100. } else {
  101. s->remaining -= size_consumed;
  102. }
  103. }
  104. }
  105. static inline void dts_reset(struct DataTransferStatsDirection *s,
  106. unsigned long starting_size)
  107. {
  108. s->expired = false;
  109. s->remaining = starting_size;
  110. /*
  111. * The semantics of setting CONF_ssh_rekey_data to zero are to
  112. * disable data-volume based rekeying completely. So if the
  113. * starting size is actually zero, we don't set 'running' to true
  114. * in the first place, which means we won't ever set the expired
  115. * flag.
  116. */
  117. s->running = (starting_size != 0);
  118. }
  119. BinaryPacketProtocol *ssh2_bpp_new(
  120. LogContext *logctx, struct DataTransferStats *stats, bool is_server);
  121. void ssh2_bpp_new_outgoing_crypto(
  122. BinaryPacketProtocol *bpp,
  123. const ssh_cipheralg *cipher, const void *ckey, const void *iv,
  124. const ssh2_macalg *mac, bool etm_mode, const void *mac_key,
  125. const ssh_compression_alg *compression, bool delayed_compression,
  126. bool reset_sequence_number);
  127. void ssh2_bpp_new_incoming_crypto(
  128. BinaryPacketProtocol *bpp,
  129. const ssh_cipheralg *cipher, const void *ckey, const void *iv,
  130. const ssh2_macalg *mac, bool etm_mode, const void *mac_key,
  131. const ssh_compression_alg *compression, bool delayed_compression,
  132. bool reset_sequence_number);
  133. /*
  134. * A query method specific to the interface between ssh2transport and
  135. * ssh2bpp. If true, it indicates that we're potentially in the
  136. * race-condition-prone part of delayed compression setup and so
  137. * asynchronous outgoing transport-layer packets are currently not
  138. * being sent, which means in particular that it would be a bad idea
  139. * to start a rekey because then we'd stop responding to anything
  140. * _other_ than transport-layer packets and deadlock the protocol.
  141. */
  142. bool ssh2_bpp_rekey_inadvisable(BinaryPacketProtocol *bpp);
  143. BinaryPacketProtocol *ssh2_bare_bpp_new(LogContext *logctx);
  144. /*
  145. * The initial code to handle the SSH version exchange is also
  146. * structured as an implementation of BinaryPacketProtocol, because
  147. * that makes it easy to switch from that to the next BPP once it
  148. * tells us which one we're using.
  149. */
  150. struct ssh_version_receiver {
  151. void (*got_ssh_version)(struct ssh_version_receiver *rcv,
  152. int major_version);
  153. };
  154. BinaryPacketProtocol *ssh_verstring_new(
  155. Conf *conf, LogContext *logctx, bool bare_connection_mode,
  156. const char *protoversion, struct ssh_version_receiver *rcv,
  157. bool server_mode, const char *impl_name);
  158. const char *ssh_verstring_get_remote(BinaryPacketProtocol *);
  159. const char *ssh_verstring_get_local(BinaryPacketProtocol *);
  160. int ssh_verstring_get_bugs(BinaryPacketProtocol *);
  161. #endif /* PUTTY_SSHBPP_H */