xref: /netbsd-src/crypto/external/bsd/openssh/dist/packet.h (revision 9469f4f13c84743995b7d51c506f9c9849ba30de)
1*9469f4f1Schristos /*	$NetBSD: packet.h,v 1.28 2024/09/24 21:32:18 christos Exp $	*/
2*9469f4f1Schristos /* $OpenBSD: packet.h,v 1.99 2024/08/15 00:51:51 djm Exp $ */
3ca32bd8dSchristos 
4ca32bd8dSchristos /*
5ca32bd8dSchristos  * Author: Tatu Ylonen <ylo@cs.hut.fi>
6ca32bd8dSchristos  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7ca32bd8dSchristos  *                    All rights reserved
8ca32bd8dSchristos  * Interface for the packet protocol functions.
9ca32bd8dSchristos  *
10ca32bd8dSchristos  * As far as I am concerned, the code I have written for this software
11ca32bd8dSchristos  * can be used freely for any purpose.  Any derived versions of this
12ca32bd8dSchristos  * software must be clearly marked as such, and if the derived work is
13ca32bd8dSchristos  * incompatible with the protocol description in the RFC file, it must be
14ca32bd8dSchristos  * called by a name other than "ssh" or "Secure Shell".
15ca32bd8dSchristos  */
16ca32bd8dSchristos 
17ca32bd8dSchristos #ifndef PACKET_H
18ca32bd8dSchristos #define PACKET_H
19ca32bd8dSchristos 
20ca32bd8dSchristos #include <termios.h>
21ca32bd8dSchristos 
22e4d43b82Schristos #include <sys/signal.h>
23e4d43b82Schristos #include <sys/queue.h>
24ca32bd8dSchristos 
25cd4ada6aSchristos #ifdef WITH_OPENSSL
26cd4ada6aSchristos #include <openssl/bn.h>
27cd4ada6aSchristos #include <openssl/ec.h>
28cd4ada6aSchristos #include <openssl/ecdsa.h>
29*9469f4f1Schristos #include <openssl/evp.h>
30cd4ada6aSchristos #else /* OPENSSL */
31cd4ada6aSchristos #define BIGNUM		void
32cd4ada6aSchristos #define EC_GROUP	void
33cd4ada6aSchristos #define EC_POINT	void
34*9469f4f1Schristos #define EVP_PKEY	void
35cd4ada6aSchristos #endif /* WITH_OPENSSL */
36cd4ada6aSchristos 
37e4d43b82Schristos struct kex;
38e4d43b82Schristos struct sshkey;
39e4d43b82Schristos struct sshbuf;
40e4d43b82Schristos struct session_state;	/* private session data */
41ca32bd8dSchristos 
42e4d43b82Schristos #include "dispatch.h"	/* typedef, DISPATCH_MAX */
43ca32bd8dSchristos 
44e4d43b82Schristos struct key_entry {
45e4d43b82Schristos 	TAILQ_ENTRY(key_entry) next;
46e4d43b82Schristos 	struct sshkey *key;
47e4d43b82Schristos };
48ca32bd8dSchristos 
49e4d43b82Schristos struct ssh {
50e4d43b82Schristos 	/* Session state */
51e4d43b82Schristos 	struct session_state *state;
52ca32bd8dSchristos 
53e4d43b82Schristos 	/* Key exchange */
54e4d43b82Schristos 	struct kex *kex;
55ca32bd8dSchristos 
565101d403Schristos 	/* cached local and remote ip addresses and ports */
57e4d43b82Schristos 	char *remote_ipaddr;
58e4d43b82Schristos 	int remote_port;
595101d403Schristos 	char *local_ipaddr;
605101d403Schristos 	int local_port;
61ffae97bbSchristos 	char *rdomain_in;
62ca32bd8dSchristos 
6341768fc1Schristos 	/* Optional preamble for log messages (e.g. username) */
6441768fc1Schristos 	char *log_preamble;
6541768fc1Schristos 
66e4d43b82Schristos 	/* Dispatcher table */
67e4d43b82Schristos 	dispatch_fn *dispatch[DISPATCH_MAX];
68e4d43b82Schristos 	/* number of packets to ignore in the dispatcher */
69e4d43b82Schristos 	int dispatch_skip_packets;
70e4d43b82Schristos 
71e4d43b82Schristos 	/* datafellows */
72e4d43b82Schristos 	int compat;
73e4d43b82Schristos 
74e4d43b82Schristos 	/* Lists for private and public keys */
75e4d43b82Schristos 	TAILQ_HEAD(, key_entry) private_keys;
76e4d43b82Schristos 	TAILQ_HEAD(, key_entry) public_keys;
77e4d43b82Schristos 
787a183406Schristos 	/* Client/Server authentication context */
797a183406Schristos 	void *authctxt;
807a183406Schristos 
817a183406Schristos 	/* Channels context */
827a183406Schristos 	struct ssh_channels *chanctxt;
837a183406Schristos 
84e4d43b82Schristos 	/* APP data */
85e4d43b82Schristos 	void *app_data;
86e4d43b82Schristos };
87e4d43b82Schristos 
88ee85abc4Schristos typedef int (ssh_packet_hook_fn)(struct ssh *, struct sshbuf *,
89ee85abc4Schristos     u_char *, void *);
90ee85abc4Schristos 
91e4d43b82Schristos struct ssh *ssh_alloc_session_state(void);
92e4d43b82Schristos struct ssh *ssh_packet_set_connection(struct ssh *, int, int);
93e4d43b82Schristos void     ssh_packet_set_timeout(struct ssh *, int, int);
94e4d43b82Schristos int	 ssh_packet_stop_discard(struct ssh *);
95e4d43b82Schristos int	 ssh_packet_connection_af(struct ssh *);
96e4d43b82Schristos void     ssh_packet_set_nonblocking(struct ssh *);
97e4d43b82Schristos int      ssh_packet_get_connection_in(struct ssh *);
98e4d43b82Schristos int      ssh_packet_get_connection_out(struct ssh *);
99e4d43b82Schristos void     ssh_packet_close(struct ssh *);
100ee85abc4Schristos void	 ssh_packet_set_input_hook(struct ssh *, ssh_packet_hook_fn *, void *);
1017a183406Schristos void	 ssh_packet_clear_keys(struct ssh *);
1027a183406Schristos void	 ssh_clear_newkeys(struct ssh *, int);
103ee85abc4Schristos 
10479976551Schristos int	 ssh_packet_is_rekeying(struct ssh *);
105b592f463Schristos int	 ssh_packet_check_rekey(struct ssh *);
106e4d43b82Schristos void     ssh_packet_set_protocol_flags(struct ssh *, u_int);
107e4d43b82Schristos u_int	 ssh_packet_get_protocol_flags(struct ssh *);
108e4d43b82Schristos void	 ssh_packet_set_tos(struct ssh *, int);
109e4d43b82Schristos void     ssh_packet_set_interactive(struct ssh *, int, int, int);
110e4d43b82Schristos int      ssh_packet_is_interactive(struct ssh *);
111e4d43b82Schristos void     ssh_packet_set_server(struct ssh *);
112e4d43b82Schristos void     ssh_packet_set_authenticated(struct ssh *);
113ee85abc4Schristos void     ssh_packet_set_mux(struct ssh *);
114ee85abc4Schristos int	 ssh_packet_get_mux(struct ssh *);
11541768fc1Schristos int	 ssh_packet_set_log_preamble(struct ssh *, const char *, ...)
11641768fc1Schristos     __attribute__((format(printf, 2, 3)));
117ee85abc4Schristos 
118ee85abc4Schristos int	 ssh_packet_log_type(u_char);
119e4d43b82Schristos 
120e4d43b82Schristos int	 ssh_packet_send2_wrapped(struct ssh *);
121e4d43b82Schristos int	 ssh_packet_send2(struct ssh *);
122aa36fcacSchristos int	 ssh_packet_authentication_state(struct ssh *);
123e4d43b82Schristos void	 ssh_packet_request_rekeying(void);
124e4d43b82Schristos 
125e4d43b82Schristos int      ssh_packet_read(struct ssh *);
126e4d43b82Schristos int ssh_packet_read_poll2(struct ssh *, u_char *, u_int32_t *seqnr_p);
127e4d43b82Schristos int	 ssh_packet_process_incoming(struct ssh *, const char *buf, u_int len);
128a03ec00cSchristos int	 ssh_packet_process_read(struct ssh *, int);
129e4d43b82Schristos int      ssh_packet_read_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
130e4d43b82Schristos int      ssh_packet_read_poll_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
131e4d43b82Schristos 
132e4d43b82Schristos void     ssh_packet_disconnect(struct ssh *, const char *fmt, ...)
133e4d43b82Schristos 	__attribute__((format(printf, 2, 3)))
134e4d43b82Schristos 	__attribute__((noreturn));
135e4d43b82Schristos void     ssh_packet_send_debug(struct ssh *, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
136e4d43b82Schristos 
137e4d43b82Schristos int	 ssh_set_newkeys(struct ssh *, int mode);
138e4d43b82Schristos void	 ssh_packet_get_bytes(struct ssh *, u_int64_t *, u_int64_t *);
139e4d43b82Schristos 
140e4d43b82Schristos int	 ssh_packet_write_poll(struct ssh *);
141e4d43b82Schristos int	 ssh_packet_write_wait(struct ssh *);
142e4d43b82Schristos int      ssh_packet_have_data_to_write(struct ssh *);
143e4d43b82Schristos int      ssh_packet_not_very_much_data_to_write(struct ssh *);
144a629fefcSchristos int	 ssh_packet_interactive_data_to_write(struct ssh *);
145e4d43b82Schristos 
146e4d43b82Schristos int	 ssh_packet_connection_is_on_socket(struct ssh *);
147e4d43b82Schristos int	 ssh_packet_remaining(struct ssh *);
148ca32bd8dSchristos 
14955a4608bSchristos void	 ssh_tty_make_modes(struct ssh *, int, struct termios *);
15055a4608bSchristos void	 ssh_tty_parse_modes(struct ssh *, int);
151ca32bd8dSchristos 
152e4d43b82Schristos void	 ssh_packet_set_alive_timeouts(struct ssh *, int);
153e4d43b82Schristos int	 ssh_packet_inc_alive_timeouts(struct ssh *);
154e4d43b82Schristos int	 ssh_packet_set_maxsize(struct ssh *, u_int);
155e4d43b82Schristos u_int	 ssh_packet_get_maxsize(struct ssh *);
156ca32bd8dSchristos 
157e4d43b82Schristos int	 ssh_packet_get_state(struct ssh *, struct sshbuf *);
158e4d43b82Schristos int	 ssh_packet_set_state(struct ssh *, struct sshbuf *);
159ca32bd8dSchristos 
160e4d43b82Schristos const char *ssh_remote_ipaddr(struct ssh *);
16179976551Schristos int	 ssh_remote_port(struct ssh *);
1625101d403Schristos const char *ssh_local_ipaddr(struct ssh *);
1635101d403Schristos int	 ssh_local_port(struct ssh *);
164ffae97bbSchristos const char *ssh_packet_rdomain_in(struct ssh *);
1651c7715ddSchristos char	*ssh_remote_hostname(struct ssh *);
166ca32bd8dSchristos 
16741768fc1Schristos void	 ssh_packet_set_rekey_limits(struct ssh *, u_int64_t, u_int32_t);
168e4d43b82Schristos time_t	 ssh_packet_get_rekey_timeout(struct ssh *);
16947dc7704Schristos 
170e4d43b82Schristos void	*ssh_packet_get_input(struct ssh *);
171e4d43b82Schristos void	*ssh_packet_get_output(struct ssh *);
172e4d43b82Schristos 
173e4d43b82Schristos /* new API */
174e4d43b82Schristos int	sshpkt_start(struct ssh *ssh, u_char type);
175e4d43b82Schristos int	sshpkt_send(struct ssh *ssh);
176e4d43b82Schristos int	sshpkt_sendx(struct ssh *ssh);
177e4d43b82Schristos int     sshpkt_disconnect(struct ssh *, const char *fmt, ...)
178e4d43b82Schristos 	    __attribute__((format(printf, 2, 3)));
179e4d43b82Schristos int	sshpkt_add_padding(struct ssh *, u_char);
180aa36fcacSchristos void	sshpkt_fatal(struct ssh *ssh, int r, const char *fmt, ...)
1818db691beSchristos 	    __attribute__((format(printf, 3, 4)))
1828db691beSchristos 	    __attribute__((noreturn));
1837a183406Schristos int	sshpkt_msg_ignore(struct ssh *, u_int);
184e4d43b82Schristos 
185e4d43b82Schristos int	sshpkt_put(struct ssh *ssh, const void *v, size_t len);
186e4d43b82Schristos int	sshpkt_putb(struct ssh *ssh, const struct sshbuf *b);
187e4d43b82Schristos int	sshpkt_put_u8(struct ssh *ssh, u_char val);
188e4d43b82Schristos int	sshpkt_put_u32(struct ssh *ssh, u_int32_t val);
189e4d43b82Schristos int	sshpkt_put_u64(struct ssh *ssh, u_int64_t val);
190e4d43b82Schristos int	sshpkt_put_string(struct ssh *ssh, const void *v, size_t len);
191e4d43b82Schristos int	sshpkt_put_cstring(struct ssh *ssh, const void *v);
192e4d43b82Schristos int	sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v);
193e4d43b82Schristos int	sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g);
194*9469f4f1Schristos int	sshpkt_put_ec_pkey(struct ssh *ssh, EVP_PKEY *pkey);
195e4d43b82Schristos int	sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v);
196e4d43b82Schristos 
197e4d43b82Schristos int	sshpkt_get(struct ssh *ssh, void *valp, size_t len);
198e4d43b82Schristos int	sshpkt_get_u8(struct ssh *ssh, u_char *valp);
199e4d43b82Schristos int	sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp);
200e4d43b82Schristos int	sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp);
201e4d43b82Schristos int	sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp);
202e4d43b82Schristos int	sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
2037a183406Schristos int	sshpkt_peek_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
204e4d43b82Schristos int	sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp);
205aa36fcacSchristos int	sshpkt_getb_froms(struct ssh *ssh, struct sshbuf **valp);
206e4d43b82Schristos int	sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g);
207aa36fcacSchristos int	sshpkt_get_bignum2(struct ssh *ssh, BIGNUM **valp);
208e4d43b82Schristos int	sshpkt_get_end(struct ssh *ssh);
209ffae97bbSchristos void	sshpkt_fmt_connection_id(struct ssh *ssh, char *s, size_t l);
210e4d43b82Schristos const u_char	*sshpkt_ptr(struct ssh *, size_t *lenp);
211e4d43b82Schristos 
212ca32bd8dSchristos #endif				/* PACKET_H */
213