xref: /dflybsd-src/crypto/openssh/packet.h (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
1*ba1276acSMatthew Dillon /* $OpenBSD: packet.h,v 1.98 2024/05/17 06:42:04 jsg Exp $ */
218de8d7fSPeter Avalos 
318de8d7fSPeter Avalos /*
418de8d7fSPeter Avalos  * Author: Tatu Ylonen <ylo@cs.hut.fi>
518de8d7fSPeter Avalos  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
618de8d7fSPeter Avalos  *                    All rights reserved
718de8d7fSPeter Avalos  * Interface for the packet protocol functions.
818de8d7fSPeter Avalos  *
918de8d7fSPeter Avalos  * As far as I am concerned, the code I have written for this software
1018de8d7fSPeter Avalos  * can be used freely for any purpose.  Any derived versions of this
1118de8d7fSPeter Avalos  * software must be clearly marked as such, and if the derived work is
1218de8d7fSPeter Avalos  * incompatible with the protocol description in the RFC file, it must be
1318de8d7fSPeter Avalos  * called by a name other than "ssh" or "Secure Shell".
1418de8d7fSPeter Avalos  */
1518de8d7fSPeter Avalos 
1618de8d7fSPeter Avalos #ifndef PACKET_H
1718de8d7fSPeter Avalos #define PACKET_H
1818de8d7fSPeter Avalos 
1918de8d7fSPeter Avalos #include <termios.h>
2018de8d7fSPeter Avalos 
21e9778795SPeter Avalos #ifdef WITH_OPENSSL
2218de8d7fSPeter Avalos # include <openssl/bn.h>
239f304aafSPeter Avalos # ifdef OPENSSL_HAS_ECC
249f304aafSPeter Avalos #  include <openssl/ec.h>
25e9778795SPeter Avalos # else /* OPENSSL_HAS_ECC */
26e9778795SPeter Avalos #  define EC_KEY	void
27e9778795SPeter Avalos #  define EC_GROUP	void
28e9778795SPeter Avalos #  define EC_POINT	void
29e9778795SPeter Avalos # endif /* OPENSSL_HAS_ECC */
30e9778795SPeter Avalos #else /* WITH_OPENSSL */
31e9778795SPeter Avalos # define BIGNUM		void
32e9778795SPeter Avalos # define EC_KEY		void
33e9778795SPeter Avalos # define EC_GROUP	void
34e9778795SPeter Avalos # define EC_POINT	void
35e9778795SPeter Avalos #endif /* WITH_OPENSSL */
3618de8d7fSPeter Avalos 
37e9778795SPeter Avalos #include <signal.h>
38e9778795SPeter Avalos #include "openbsd-compat/sys-queue.h"
3918de8d7fSPeter Avalos 
40e9778795SPeter Avalos struct kex;
41e9778795SPeter Avalos struct sshkey;
42e9778795SPeter Avalos struct sshbuf;
43e9778795SPeter Avalos struct session_state;	/* private session data */
4418de8d7fSPeter Avalos 
45e9778795SPeter Avalos #include "dispatch.h"	/* typedef, DISPATCH_MAX */
4618de8d7fSPeter Avalos 
47e9778795SPeter Avalos struct key_entry {
48e9778795SPeter Avalos 	TAILQ_ENTRY(key_entry) next;
49e9778795SPeter Avalos 	struct sshkey *key;
50e9778795SPeter Avalos };
5118de8d7fSPeter Avalos 
52e9778795SPeter Avalos struct ssh {
53e9778795SPeter Avalos 	/* Session state */
54e9778795SPeter Avalos 	struct session_state *state;
5518de8d7fSPeter Avalos 
56e9778795SPeter Avalos 	/* Key exchange */
57e9778795SPeter Avalos 	struct kex *kex;
5818de8d7fSPeter Avalos 
59e9778795SPeter Avalos 	/* cached local and remote ip addresses and ports */
60e9778795SPeter Avalos 	char *remote_ipaddr;
61e9778795SPeter Avalos 	int remote_port;
62e9778795SPeter Avalos 	char *local_ipaddr;
63e9778795SPeter Avalos 	int local_port;
64664f4763Szrj 	char *rdomain_in;
65e9778795SPeter Avalos 
66ce74bacaSMatthew Dillon 	/* Optional preamble for log messages (e.g. username) */
67ce74bacaSMatthew Dillon 	char *log_preamble;
68ce74bacaSMatthew Dillon 
69e9778795SPeter Avalos 	/* Dispatcher table */
70e9778795SPeter Avalos 	dispatch_fn *dispatch[DISPATCH_MAX];
71e9778795SPeter Avalos 	/* number of packets to ignore in the dispatcher */
72e9778795SPeter Avalos 	int dispatch_skip_packets;
73e9778795SPeter Avalos 
74e9778795SPeter Avalos 	/* datafellows */
75e9778795SPeter Avalos 	int compat;
76e9778795SPeter Avalos 
77e9778795SPeter Avalos 	/* Lists for private and public keys */
78e9778795SPeter Avalos 	TAILQ_HEAD(, key_entry) private_keys;
79e9778795SPeter Avalos 	TAILQ_HEAD(, key_entry) public_keys;
80e9778795SPeter Avalos 
81ce74bacaSMatthew Dillon 	/* Client/Server authentication context */
82ce74bacaSMatthew Dillon 	void *authctxt;
83ce74bacaSMatthew Dillon 
84ce74bacaSMatthew Dillon 	/* Channels context */
85ce74bacaSMatthew Dillon 	struct ssh_channels *chanctxt;
86ce74bacaSMatthew Dillon 
87e9778795SPeter Avalos 	/* APP data */
88e9778795SPeter Avalos 	void *app_data;
89e9778795SPeter Avalos };
90e9778795SPeter Avalos 
91ce74bacaSMatthew Dillon typedef int (ssh_packet_hook_fn)(struct ssh *, struct sshbuf *,
92ce74bacaSMatthew Dillon     u_char *, void *);
93ce74bacaSMatthew Dillon 
94e9778795SPeter Avalos struct ssh *ssh_alloc_session_state(void);
95e9778795SPeter Avalos struct ssh *ssh_packet_set_connection(struct ssh *, int, int);
96e9778795SPeter Avalos void     ssh_packet_set_timeout(struct ssh *, int, int);
97e9778795SPeter Avalos int	 ssh_packet_stop_discard(struct ssh *);
98e9778795SPeter Avalos int	 ssh_packet_connection_af(struct ssh *);
99e9778795SPeter Avalos void     ssh_packet_set_nonblocking(struct ssh *);
100e9778795SPeter Avalos int      ssh_packet_get_connection_in(struct ssh *);
101e9778795SPeter Avalos int      ssh_packet_get_connection_out(struct ssh *);
102e9778795SPeter Avalos void     ssh_packet_close(struct ssh *);
103ce74bacaSMatthew Dillon void	 ssh_packet_set_input_hook(struct ssh *, ssh_packet_hook_fn *, void *);
104ce74bacaSMatthew Dillon void	 ssh_packet_clear_keys(struct ssh *);
105ce74bacaSMatthew Dillon void	 ssh_clear_newkeys(struct ssh *, int);
106ce74bacaSMatthew Dillon 
107e9778795SPeter Avalos int	 ssh_packet_is_rekeying(struct ssh *);
10850a69bb5SSascha Wildner int	 ssh_packet_check_rekey(struct ssh *);
109e9778795SPeter Avalos void     ssh_packet_set_protocol_flags(struct ssh *, u_int);
110e9778795SPeter Avalos u_int	 ssh_packet_get_protocol_flags(struct ssh *);
111e9778795SPeter Avalos void	 ssh_packet_set_tos(struct ssh *, int);
112e9778795SPeter Avalos void     ssh_packet_set_interactive(struct ssh *, int, int, int);
113e9778795SPeter Avalos int      ssh_packet_is_interactive(struct ssh *);
114e9778795SPeter Avalos void     ssh_packet_set_server(struct ssh *);
115e9778795SPeter Avalos void     ssh_packet_set_authenticated(struct ssh *);
116ce74bacaSMatthew Dillon void     ssh_packet_set_mux(struct ssh *);
117ce74bacaSMatthew Dillon int	 ssh_packet_get_mux(struct ssh *);
118ce74bacaSMatthew Dillon int	 ssh_packet_set_log_preamble(struct ssh *, const char *, ...)
119ce74bacaSMatthew Dillon     __attribute__((format(printf, 2, 3)));
120e9778795SPeter Avalos 
121ce74bacaSMatthew Dillon int	 ssh_packet_log_type(u_char);
122ce74bacaSMatthew Dillon 
123e9778795SPeter Avalos int	 ssh_packet_send2_wrapped(struct ssh *);
124e9778795SPeter Avalos int	 ssh_packet_send2(struct ssh *);
125e9778795SPeter Avalos 
126e9778795SPeter Avalos int      ssh_packet_read(struct ssh *);
127e9778795SPeter Avalos int ssh_packet_read_poll2(struct ssh *, u_char *, u_int32_t *seqnr_p);
128e9778795SPeter Avalos int	 ssh_packet_process_incoming(struct ssh *, const char *buf, u_int len);
129ee116499SAntonio Huete Jimenez int	 ssh_packet_process_read(struct ssh *, int);
130e9778795SPeter Avalos int      ssh_packet_read_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
131e9778795SPeter Avalos int      ssh_packet_read_poll_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
132e9778795SPeter Avalos 
133e9778795SPeter Avalos void     ssh_packet_disconnect(struct ssh *, const char *fmt, ...)
134e9778795SPeter Avalos 	__attribute__((format(printf, 2, 3)))
135e9778795SPeter Avalos 	__attribute__((noreturn));
136e9778795SPeter Avalos void     ssh_packet_send_debug(struct ssh *, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
137e9778795SPeter Avalos 
138e9778795SPeter Avalos int	 ssh_set_newkeys(struct ssh *, int mode);
139e9778795SPeter Avalos void	 ssh_packet_get_bytes(struct ssh *, u_int64_t *, u_int64_t *);
140e9778795SPeter Avalos 
141e9778795SPeter Avalos int	 ssh_packet_write_poll(struct ssh *);
142e9778795SPeter Avalos int	 ssh_packet_write_wait(struct ssh *);
143e9778795SPeter Avalos int      ssh_packet_have_data_to_write(struct ssh *);
144e9778795SPeter Avalos int      ssh_packet_not_very_much_data_to_write(struct ssh *);
145*ba1276acSMatthew Dillon int	 ssh_packet_interactive_data_to_write(struct ssh *);
146e9778795SPeter Avalos 
147e9778795SPeter Avalos int	 ssh_packet_connection_is_on_socket(struct ssh *);
148e9778795SPeter Avalos int	 ssh_packet_remaining(struct ssh *);
14918de8d7fSPeter Avalos 
150664f4763Szrj void	 ssh_tty_make_modes(struct ssh *, int, struct termios *);
151664f4763Szrj void	 ssh_tty_parse_modes(struct ssh *, int);
15218de8d7fSPeter Avalos 
153e9778795SPeter Avalos void	 ssh_packet_set_alive_timeouts(struct ssh *, int);
154e9778795SPeter Avalos int	 ssh_packet_inc_alive_timeouts(struct ssh *);
155e9778795SPeter Avalos int	 ssh_packet_set_maxsize(struct ssh *, u_int);
156e9778795SPeter Avalos u_int	 ssh_packet_get_maxsize(struct ssh *);
15718de8d7fSPeter Avalos 
158e9778795SPeter Avalos int	 ssh_packet_get_state(struct ssh *, struct sshbuf *);
159e9778795SPeter Avalos int	 ssh_packet_set_state(struct ssh *, struct sshbuf *);
16018de8d7fSPeter Avalos 
161e9778795SPeter Avalos const char *ssh_remote_ipaddr(struct ssh *);
162e9778795SPeter Avalos int	 ssh_remote_port(struct ssh *);
163e9778795SPeter Avalos const char *ssh_local_ipaddr(struct ssh *);
164e9778795SPeter Avalos int	 ssh_local_port(struct ssh *);
165664f4763Szrj const char *ssh_packet_rdomain_in(struct ssh *);
166*ba1276acSMatthew Dillon char	*ssh_remote_hostname(struct ssh *);
16718de8d7fSPeter Avalos 
168ce74bacaSMatthew Dillon void	 ssh_packet_set_rekey_limits(struct ssh *, u_int64_t, u_int32_t);
169e9778795SPeter Avalos time_t	 ssh_packet_get_rekey_timeout(struct ssh *);
17040c002afSPeter Avalos 
171e9778795SPeter Avalos void	*ssh_packet_get_input(struct ssh *);
172e9778795SPeter Avalos void	*ssh_packet_get_output(struct ssh *);
173e9778795SPeter Avalos 
174e9778795SPeter Avalos /* new API */
175e9778795SPeter Avalos int	sshpkt_start(struct ssh *ssh, u_char type);
176e9778795SPeter Avalos int	sshpkt_send(struct ssh *ssh);
177e9778795SPeter Avalos int     sshpkt_disconnect(struct ssh *, const char *fmt, ...)
178e9778795SPeter Avalos 	    __attribute__((format(printf, 2, 3)));
179e9778795SPeter Avalos int	sshpkt_add_padding(struct ssh *, u_char);
180664f4763Szrj void	sshpkt_fatal(struct ssh *ssh, int r, const char *fmt, ...)
1810cbfa66cSDaniel Fojt 	    __attribute__((format(printf, 3, 4)))
1820cbfa66cSDaniel Fojt 	    __attribute__((noreturn));
183ce74bacaSMatthew Dillon int	sshpkt_msg_ignore(struct ssh *, u_int);
184e9778795SPeter Avalos 
185e9778795SPeter Avalos int	sshpkt_put(struct ssh *ssh, const void *v, size_t len);
186e9778795SPeter Avalos int	sshpkt_putb(struct ssh *ssh, const struct sshbuf *b);
187e9778795SPeter Avalos int	sshpkt_put_u8(struct ssh *ssh, u_char val);
188e9778795SPeter Avalos int	sshpkt_put_u32(struct ssh *ssh, u_int32_t val);
189e9778795SPeter Avalos int	sshpkt_put_u64(struct ssh *ssh, u_int64_t val);
190e9778795SPeter Avalos int	sshpkt_put_string(struct ssh *ssh, const void *v, size_t len);
191e9778795SPeter Avalos int	sshpkt_put_cstring(struct ssh *ssh, const void *v);
192e9778795SPeter Avalos int	sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v);
193e9778795SPeter Avalos int	sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g);
194e9778795SPeter Avalos int	sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v);
195e9778795SPeter Avalos 
196e9778795SPeter Avalos int	sshpkt_get(struct ssh *ssh, void *valp, size_t len);
197e9778795SPeter Avalos int	sshpkt_get_u8(struct ssh *ssh, u_char *valp);
198e9778795SPeter Avalos int	sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp);
199e9778795SPeter Avalos int	sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp);
200e9778795SPeter Avalos int	sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp);
201e9778795SPeter Avalos int	sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
202ce74bacaSMatthew Dillon int	sshpkt_peek_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
203e9778795SPeter Avalos int	sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp);
204664f4763Szrj int	sshpkt_getb_froms(struct ssh *ssh, struct sshbuf **valp);
205e9778795SPeter Avalos int	sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g);
206664f4763Szrj int	sshpkt_get_bignum2(struct ssh *ssh, BIGNUM **valp);
207e9778795SPeter Avalos int	sshpkt_get_end(struct ssh *ssh);
208664f4763Szrj void	sshpkt_fmt_connection_id(struct ssh *ssh, char *s, size_t l);
209e9778795SPeter Avalos const u_char	*sshpkt_ptr(struct ssh *, size_t *lenp);
210e9778795SPeter Avalos 
211e9778795SPeter Avalos #if !defined(WITH_OPENSSL)
212e9778795SPeter Avalos # undef BIGNUM
213e9778795SPeter Avalos # undef EC_KEY
214e9778795SPeter Avalos # undef EC_GROUP
215e9778795SPeter Avalos # undef EC_POINT
216e9778795SPeter Avalos #elif !defined(OPENSSL_HAS_ECC)
217e9778795SPeter Avalos # undef EC_KEY
218e9778795SPeter Avalos # undef EC_GROUP
219e9778795SPeter Avalos # undef EC_POINT
220e9778795SPeter Avalos #endif
22140c002afSPeter Avalos 
22218de8d7fSPeter Avalos #endif				/* PACKET_H */
223