sshbpp.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #define ssh_bpp_handle_input(bpp) ((bpp)->vt->handle_input(bpp))
  38. #define ssh_bpp_handle_output(bpp) ((bpp)->vt->handle_output(bpp))
  39. #define ssh_bpp_new_pktout(bpp, type) ((bpp)->vt->new_pktout(type))
  40. #define ssh_bpp_queue_disconnect(bpp, msg, cat) \
  41. ((bpp)->vt->queue_disconnect(bpp, msg, cat))
  42. /* ssh_bpp_free is more than just a macro wrapper on the vtable; it
  43. * does centralised parts of the freeing too. */
  44. void ssh_bpp_free(BinaryPacketProtocol *bpp);
  45. BinaryPacketProtocol *ssh1_bpp_new(LogContext *logctx);
  46. void ssh1_bpp_new_cipher(BinaryPacketProtocol *bpp,
  47. const struct ssh1_cipheralg *cipher,
  48. const void *session_key);
  49. /* This is only called from outside the BPP in server mode; in client
  50. * mode the BPP detects compression start time automatically by
  51. * snooping message types */
  52. void ssh1_bpp_start_compression(BinaryPacketProtocol *bpp);
  53. /* Helper routine which does common BPP initialisation, e.g. setting
  54. * up in_pq and out_pq, and initialising input_consumer. */
  55. void ssh_bpp_common_setup(BinaryPacketProtocol *);
  56. /* Common helper functions between the SSH-2 full and bare BPPs */
  57. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  58. const char *msg, int category);
  59. bool ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin);
  60. /* Convenience macro for BPPs to send formatted strings to the Event
  61. * Log. Assumes a function parameter called 'bpp' is in scope. */
  62. #define bpp_logevent(...) ( \
  63. logevent_and_free((bpp)->logctx, dupprintf(__VA_ARGS__)))
  64. /*
  65. * Structure that tracks how much data is sent and received, for
  66. * purposes of triggering an SSH-2 rekey when either one gets over a
  67. * configured limit. In each direction, the flag 'running' indicates
  68. * that we haven't hit the limit yet, and 'remaining' tracks how much
  69. * longer until we do. The macro DTS_CONSUME subtracts a given amount
  70. * from the counter in a particular direction, and evaluates to a
  71. * boolean indicating whether the limit has been hit.
  72. *
  73. * The limit is sticky: once 'running' has flipped to false,
  74. * 'remaining' is no longer decremented, so it shouldn't dangerously
  75. * wrap round.
  76. */
  77. struct DataTransferStats {
  78. struct {
  79. bool running;
  80. unsigned long remaining;
  81. } in, out;
  82. };
  83. #define DTS_CONSUME(stats, direction, size) \
  84. ((stats)->direction.running && \
  85. (stats)->direction.remaining <= (size) ? \
  86. ((stats)->direction.running = false, true) : \
  87. ((stats)->direction.remaining -= (size), false))
  88. BinaryPacketProtocol *ssh2_bpp_new(
  89. LogContext *logctx, struct DataTransferStats *stats, bool is_server);
  90. void ssh2_bpp_new_outgoing_crypto(
  91. BinaryPacketProtocol *bpp,
  92. const struct ssh2_cipheralg *cipher, const void *ckey, const void *iv,
  93. const struct ssh2_macalg *mac, bool etm_mode, const void *mac_key,
  94. const struct ssh_compression_alg *compression, bool delayed_compression);
  95. void ssh2_bpp_new_incoming_crypto(
  96. BinaryPacketProtocol *bpp,
  97. const struct ssh2_cipheralg *cipher, const void *ckey, const void *iv,
  98. const struct ssh2_macalg *mac, bool etm_mode, const void *mac_key,
  99. const struct ssh_compression_alg *compression, bool delayed_compression);
  100. /*
  101. * A query method specific to the interface between ssh2transport and
  102. * ssh2bpp. If true, it indicates that we're potentially in the
  103. * race-condition-prone part of delayed compression setup and so
  104. * asynchronous outgoing transport-layer packets are currently not
  105. * being sent, which means in particular that it would be a bad idea
  106. * to start a rekey because then we'd stop responding to anything
  107. * _other_ than transport-layer packets and deadlock the protocol.
  108. */
  109. bool ssh2_bpp_rekey_inadvisable(BinaryPacketProtocol *bpp);
  110. BinaryPacketProtocol *ssh2_bare_bpp_new(LogContext *logctx);
  111. /*
  112. * The initial code to handle the SSH version exchange is also
  113. * structured as an implementation of BinaryPacketProtocol, because
  114. * that makes it easy to switch from that to the next BPP once it
  115. * tells us which one we're using.
  116. */
  117. struct ssh_version_receiver {
  118. void (*got_ssh_version)(struct ssh_version_receiver *rcv,
  119. int major_version);
  120. };
  121. BinaryPacketProtocol *ssh_verstring_new(
  122. Conf *conf, LogContext *logctx, bool bare_connection_mode,
  123. const char *protoversion, struct ssh_version_receiver *rcv,
  124. bool server_mode, const char *impl_name);
  125. const char *ssh_verstring_get_remote(BinaryPacketProtocol *);
  126. const char *ssh_verstring_get_local(BinaryPacketProtocol *);
  127. int ssh_verstring_get_bugs(BinaryPacketProtocol *);
  128. #endif /* PUTTY_SSHBPP_H */