sshbpp.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. int 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. int 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. int 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, and
  62. * takes a double pair of parens because it passes a whole argument
  63. * list to dupprintf. */
  64. #define bpp_logevent(params) ( \
  65. logevent_and_free((bpp)->logctx, dupprintf params))
  66. /*
  67. * Structure that tracks how much data is sent and received, for
  68. * purposes of triggering an SSH-2 rekey when either one gets over a
  69. * configured limit. In each direction, the flag 'running' indicates
  70. * that we haven't hit the limit yet, and 'remaining' tracks how much
  71. * longer until we do. The macro DTS_CONSUME subtracts a given amount
  72. * from the counter in a particular direction, and evaluates to a
  73. * boolean indicating whether the limit has been hit.
  74. *
  75. * The limit is sticky: once 'running' has flipped to false,
  76. * 'remaining' is no longer decremented, so it shouldn't dangerously
  77. * wrap round.
  78. */
  79. struct DataTransferStats {
  80. struct {
  81. int running;
  82. unsigned long remaining;
  83. } in, out;
  84. };
  85. #define DTS_CONSUME(stats, direction, size) \
  86. ((stats)->direction.running && \
  87. (stats)->direction.remaining <= (size) ? \
  88. ((stats)->direction.running = FALSE, TRUE) : \
  89. ((stats)->direction.remaining -= (size), FALSE))
  90. BinaryPacketProtocol *ssh2_bpp_new(
  91. LogContext *logctx, struct DataTransferStats *stats, int is_server);
  92. void ssh2_bpp_new_outgoing_crypto(
  93. BinaryPacketProtocol *bpp,
  94. const struct ssh2_cipheralg *cipher, const void *ckey, const void *iv,
  95. const struct ssh2_macalg *mac, int etm_mode, const void *mac_key,
  96. const struct ssh_compression_alg *compression, int delayed_compression);
  97. void ssh2_bpp_new_incoming_crypto(
  98. BinaryPacketProtocol *bpp,
  99. const struct ssh2_cipheralg *cipher, const void *ckey, const void *iv,
  100. const struct ssh2_macalg *mac, int etm_mode, const void *mac_key,
  101. const struct ssh_compression_alg *compression, int delayed_compression);
  102. /*
  103. * A query method specific to the interface between ssh2transport and
  104. * ssh2bpp. If true, it indicates that we're potentially in the
  105. * race-condition-prone part of delayed compression setup and so
  106. * asynchronous outgoing transport-layer packets are currently not
  107. * being sent, which means in particular that it would be a bad idea
  108. * to start a rekey because then we'd stop responding to anything
  109. * _other_ than transport-layer packets and deadlock the protocol.
  110. */
  111. int ssh2_bpp_rekey_inadvisable(BinaryPacketProtocol *bpp);
  112. BinaryPacketProtocol *ssh2_bare_bpp_new(LogContext *logctx);
  113. /*
  114. * The initial code to handle the SSH version exchange is also
  115. * structured as an implementation of BinaryPacketProtocol, because
  116. * that makes it easy to switch from that to the next BPP once it
  117. * tells us which one we're using.
  118. */
  119. struct ssh_version_receiver {
  120. void (*got_ssh_version)(struct ssh_version_receiver *rcv,
  121. int major_version);
  122. };
  123. BinaryPacketProtocol *ssh_verstring_new(
  124. Conf *conf, LogContext *logctx, int bare_connection_mode,
  125. const char *protoversion, struct ssh_version_receiver *rcv,
  126. int server_mode, const char *impl_name);
  127. const char *ssh_verstring_get_remote(BinaryPacketProtocol *);
  128. const char *ssh_verstring_get_local(BinaryPacketProtocol *);
  129. int ssh_verstring_get_bugs(BinaryPacketProtocol *);
  130. #ifdef MPEXT
  131. const ssh1_cipher * ssh1_bpp_get_cipher(BinaryPacketProtocol *bpp);
  132. const ssh2_cipher * ssh2_bpp_get_cscipher(BinaryPacketProtocol *bpp);
  133. const ssh2_cipher * ssh2_bpp_get_sccipher(BinaryPacketProtocol *bpp);
  134. const struct ssh_compressor * ssh2_bpp_get_cscomp(BinaryPacketProtocol *bpp);
  135. const struct ssh_decompressor * ssh2_bpp_get_sccomp(BinaryPacketProtocol *bpp);
  136. #endif
  137. #endif /* PUTTY_SSHBPP_H */