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