sshbpp.h 7.3 KB

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