packet.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* $OpenBSD: packet.h,v 1.92 2020/03/06 18:11:10 markus Exp $ */
  2. /*
  3. * Author: Tatu Ylonen <ylo@cs.hut.fi>
  4. * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  5. * All rights reserved
  6. * Interface for the packet protocol functions.
  7. *
  8. * As far as I am concerned, the code I have written for this software
  9. * can be used freely for any purpose. Any derived versions of this
  10. * software must be clearly marked as such, and if the derived work is
  11. * incompatible with the protocol description in the RFC file, it must be
  12. * called by a name other than "ssh" or "Secure Shell".
  13. */
  14. #ifndef PACKET_H
  15. #define PACKET_H
  16. #include <termios.h>
  17. #ifdef WITH_OPENSSL
  18. # include <openssl/bn.h>
  19. # ifdef OPENSSL_HAS_ECC
  20. # include <openssl/ec.h>
  21. # else /* OPENSSL_HAS_ECC */
  22. # define EC_KEY void
  23. # define EC_GROUP void
  24. # define EC_POINT void
  25. # endif /* OPENSSL_HAS_ECC */
  26. #else /* WITH_OPENSSL */
  27. # define BIGNUM void
  28. # define EC_KEY void
  29. # define EC_GROUP void
  30. # define EC_POINT void
  31. #endif /* WITH_OPENSSL */
  32. #include <signal.h>
  33. #include "openbsd-compat/sys-queue.h"
  34. struct kex;
  35. struct sshkey;
  36. struct sshbuf;
  37. struct session_state; /* private session data */
  38. #include "dispatch.h" /* typedef, DISPATCH_MAX */
  39. struct key_entry {
  40. TAILQ_ENTRY(key_entry) next;
  41. struct sshkey *key;
  42. };
  43. struct ssh {
  44. /* Session state */
  45. struct session_state *state;
  46. /* Key exchange */
  47. struct kex *kex;
  48. /* cached local and remote ip addresses and ports */
  49. char *remote_ipaddr;
  50. int remote_port;
  51. char *local_ipaddr;
  52. int local_port;
  53. char *rdomain_in;
  54. /* Optional preamble for log messages (e.g. username) */
  55. char *log_preamble;
  56. /* Dispatcher table */
  57. dispatch_fn *dispatch[DISPATCH_MAX];
  58. /* number of packets to ignore in the dispatcher */
  59. int dispatch_skip_packets;
  60. /* datafellows */
  61. int compat;
  62. /* Lists for private and public keys */
  63. TAILQ_HEAD(, key_entry) private_keys;
  64. TAILQ_HEAD(, key_entry) public_keys;
  65. /* Client/Server authentication context */
  66. void *authctxt;
  67. /* Channels context */
  68. struct ssh_channels *chanctxt;
  69. /* APP data */
  70. void *app_data;
  71. /* logging data for ServerLogging patch*/
  72. double start_time;
  73. u_long fdout_bytes;
  74. u_long stdin_bytes;
  75. /* track that we are in a none cipher/mac state */
  76. int none;
  77. };
  78. typedef int (ssh_packet_hook_fn)(struct ssh *, struct sshbuf *,
  79. u_char *, void *);
  80. struct ssh *ssh_alloc_session_state(void);
  81. struct ssh *ssh_packet_set_connection(struct ssh *, int, int);
  82. void ssh_packet_set_timeout(struct ssh *, int, int);
  83. int ssh_packet_stop_discard(struct ssh *);
  84. int ssh_packet_connection_af(struct ssh *);
  85. void ssh_packet_set_nonblocking(struct ssh *);
  86. int ssh_packet_get_connection_in(struct ssh *);
  87. int ssh_packet_get_connection_out(struct ssh *);
  88. void ssh_packet_close(struct ssh *);
  89. void ssh_packet_set_input_hook(struct ssh *, ssh_packet_hook_fn *, void *);
  90. void ssh_packet_clear_keys(struct ssh *);
  91. void ssh_clear_newkeys(struct ssh *, int);
  92. int ssh_packet_is_rekeying(struct ssh *);
  93. void ssh_packet_set_protocol_flags(struct ssh *, u_int);
  94. u_int ssh_packet_get_protocol_flags(struct ssh *);
  95. void ssh_packet_set_tos(struct ssh *, int);
  96. void ssh_packet_set_interactive(struct ssh *, int, int, int);
  97. int ssh_packet_is_interactive(struct ssh *);
  98. void ssh_packet_set_server(struct ssh *);
  99. void ssh_packet_set_authenticated(struct ssh *);
  100. void ssh_packet_set_mux(struct ssh *);
  101. int ssh_packet_get_mux(struct ssh *);
  102. int ssh_packet_set_log_preamble(struct ssh *, const char *, ...)
  103. __attribute__((format(printf, 2, 3)));
  104. int ssh_packet_log_type(u_char);
  105. int ssh_packet_send2_wrapped(struct ssh *);
  106. int ssh_packet_send2(struct ssh *);
  107. int ssh_packet_read(struct ssh *);
  108. int ssh_packet_read_expect(struct ssh *, u_int type);
  109. int ssh_packet_read_poll(struct ssh *);
  110. int ssh_packet_read_poll2(struct ssh *, u_char *, u_int32_t *seqnr_p);
  111. int ssh_packet_process_incoming(struct ssh *, const char *buf, u_int len);
  112. int ssh_packet_read_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
  113. int ssh_packet_read_poll_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
  114. const void *ssh_packet_get_string_ptr(struct ssh *, u_int *length_ptr);
  115. void ssh_packet_disconnect(struct ssh *, const char *fmt, ...)
  116. __attribute__((format(printf, 2, 3)))
  117. __attribute__((noreturn));
  118. void ssh_packet_send_debug(struct ssh *, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
  119. int ssh_set_newkeys(struct ssh *, int mode);
  120. void ssh_packet_get_bytes(struct ssh *, u_int64_t *, u_int64_t *);
  121. int ssh_packet_write_poll(struct ssh *);
  122. int ssh_packet_write_wait(struct ssh *);
  123. int ssh_packet_have_data_to_write(struct ssh *);
  124. int ssh_packet_not_very_much_data_to_write(struct ssh *);
  125. int ssh_packet_connection_is_on_socket(struct ssh *);
  126. int ssh_packet_remaining(struct ssh *);
  127. void ssh_tty_make_modes(struct ssh *, int, struct termios *);
  128. void ssh_tty_parse_modes(struct ssh *, int);
  129. void ssh_packet_set_alive_timeouts(struct ssh *, int);
  130. int ssh_packet_inc_alive_timeouts(struct ssh *);
  131. int ssh_packet_set_maxsize(struct ssh *, u_int);
  132. u_int ssh_packet_get_maxsize(struct ssh *);
  133. int packet_authentication_state(const struct ssh *);
  134. int ssh_packet_get_state(struct ssh *, struct sshbuf *);
  135. int ssh_packet_set_state(struct ssh *, struct sshbuf *);
  136. const char *ssh_remote_ipaddr(struct ssh *);
  137. int ssh_remote_port(struct ssh *);
  138. const char *ssh_local_ipaddr(struct ssh *);
  139. int ssh_local_port(struct ssh *);
  140. const char *ssh_packet_rdomain_in(struct ssh *);
  141. void ssh_packet_set_rekey_limits(struct ssh *, u_int64_t, u_int32_t);
  142. time_t ssh_packet_get_rekey_timeout(struct ssh *);
  143. void *ssh_packet_get_input(struct ssh *);
  144. void *ssh_packet_get_output(struct ssh *);
  145. void *ssh_packet_get_receive_context(struct ssh *);
  146. void *ssh_packet_get_send_context(struct ssh *);
  147. /* for forced packet rekeying post auth */
  148. void packet_request_rekeying(void);
  149. /* final log entry support */
  150. void sshpkt_final_log_entry (struct ssh *);
  151. /* new API */
  152. int sshpkt_start(struct ssh *ssh, u_char type);
  153. int sshpkt_send(struct ssh *ssh);
  154. int sshpkt_disconnect(struct ssh *, const char *fmt, ...)
  155. __attribute__((format(printf, 2, 3)));
  156. int sshpkt_add_padding(struct ssh *, u_char);
  157. void sshpkt_fatal(struct ssh *ssh, int r, const char *fmt, ...)
  158. __attribute__((format(printf, 3, 4)))
  159. __attribute__((noreturn));
  160. int sshpkt_msg_ignore(struct ssh *, u_int);
  161. int sshpkt_put(struct ssh *ssh, const void *v, size_t len);
  162. int sshpkt_putb(struct ssh *ssh, const struct sshbuf *b);
  163. int sshpkt_put_u8(struct ssh *ssh, u_char val);
  164. int sshpkt_put_u32(struct ssh *ssh, u_int32_t val);
  165. int sshpkt_put_u64(struct ssh *ssh, u_int64_t val);
  166. int sshpkt_put_string(struct ssh *ssh, const void *v, size_t len);
  167. int sshpkt_put_cstring(struct ssh *ssh, const void *v);
  168. int sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v);
  169. int sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g);
  170. int sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v);
  171. int sshpkt_get(struct ssh *ssh, void *valp, size_t len);
  172. int sshpkt_get_u8(struct ssh *ssh, u_char *valp);
  173. int sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp);
  174. int sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp);
  175. int sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp);
  176. int sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
  177. int sshpkt_peek_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
  178. int sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp);
  179. int sshpkt_getb_froms(struct ssh *ssh, struct sshbuf **valp);
  180. int sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g);
  181. int sshpkt_get_bignum2(struct ssh *ssh, BIGNUM **valp);
  182. int sshpkt_get_end(struct ssh *ssh);
  183. void sshpkt_fmt_connection_id(struct ssh *ssh, char *s, size_t l);
  184. const u_char *sshpkt_ptr(struct ssh *, size_t *lenp);
  185. #if !defined(WITH_OPENSSL)
  186. # undef BIGNUM
  187. # undef EC_KEY
  188. # undef EC_GROUP
  189. # undef EC_POINT
  190. #elif !defined(OPENSSL_HAS_ECC)
  191. # undef EC_KEY
  192. # undef EC_GROUP
  193. # undef EC_POINT
  194. #endif
  195. void packet_destroy_all(struct ssh *, int, int);
  196. #endif /* PACKET_H */