xref: /openssh-portable/packet.h (revision 714e1cbc)
1*714e1cbcSmarkus@openbsd.org /* $OpenBSD: packet.h,v 1.92 2020/03/06 18:11:10 markus Exp $ */
205764b92SBen Lindstrom 
3d4a8b7e3SDamien Miller /*
495def098SDamien Miller  * Author: Tatu Ylonen <ylo@cs.hut.fi>
595def098SDamien Miller  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
695def098SDamien Miller  *                    All rights reserved
795def098SDamien Miller  * Interface for the packet protocol functions.
895def098SDamien Miller  *
9e4340be5SDamien Miller  * As far as I am concerned, the code I have written for this software
10e4340be5SDamien Miller  * can be used freely for any purpose.  Any derived versions of this
11e4340be5SDamien Miller  * software must be clearly marked as such, and if the derived work is
12e4340be5SDamien Miller  * incompatible with the protocol description in the RFC file, it must be
13e4340be5SDamien Miller  * called by a name other than "ssh" or "Secure Shell".
14d4a8b7e3SDamien Miller  */
15d4a8b7e3SDamien Miller 
16d4a8b7e3SDamien Miller #ifndef PACKET_H
17d4a8b7e3SDamien Miller #define PACKET_H
18d4a8b7e3SDamien Miller 
1999bd21e3SDamien Miller #include <termios.h>
2099bd21e3SDamien Miller 
21091c3028Smarkus@openbsd.org #ifdef WITH_OPENSSL
22d4a8b7e3SDamien Miller # include <openssl/bn.h>
238ccb7392SDarren Tucker # ifdef OPENSSL_HAS_ECC
24eb8b60e3SDamien Miller #  include <openssl/ec.h>
25773dda25SDamien Miller # else /* OPENSSL_HAS_ECC */
26773dda25SDamien Miller #  define EC_KEY	void
27773dda25SDamien Miller #  define EC_GROUP	void
28773dda25SDamien Miller #  define EC_POINT	void
29773dda25SDamien Miller # endif /* OPENSSL_HAS_ECC */
30773dda25SDamien Miller #else /* WITH_OPENSSL */
31773dda25SDamien Miller # define BIGNUM		void
32773dda25SDamien Miller # define EC_KEY		void
33773dda25SDamien Miller # define EC_GROUP	void
34773dda25SDamien Miller # define EC_POINT	void
35773dda25SDamien Miller #endif /* WITH_OPENSSL */
36773dda25SDamien Miller 
3791020920STim Rice #include <signal.h>
3862f678ddSDamien Miller #include "openbsd-compat/sys-queue.h"
39d4a8b7e3SDamien Miller 
40091c3028Smarkus@openbsd.org struct kex;
41091c3028Smarkus@openbsd.org struct sshkey;
42091c3028Smarkus@openbsd.org struct sshbuf;
43091c3028Smarkus@openbsd.org struct session_state;	/* private session data */
44d4a8b7e3SDamien Miller 
453fdc88a0Smarkus@openbsd.org #include "dispatch.h"	/* typedef, DISPATCH_MAX */
463fdc88a0Smarkus@openbsd.org 
47f582f0e9Smarkus@openbsd.org struct key_entry {
48f582f0e9Smarkus@openbsd.org 	TAILQ_ENTRY(key_entry) next;
49f582f0e9Smarkus@openbsd.org 	struct sshkey *key;
50f582f0e9Smarkus@openbsd.org };
51f582f0e9Smarkus@openbsd.org 
52091c3028Smarkus@openbsd.org struct ssh {
53091c3028Smarkus@openbsd.org 	/* Session state */
54091c3028Smarkus@openbsd.org 	struct session_state *state;
55d4a8b7e3SDamien Miller 
56091c3028Smarkus@openbsd.org 	/* Key exchange */
57091c3028Smarkus@openbsd.org 	struct kex *kex;
58f6027d34SBen Lindstrom 
5995767262Sdjm@openbsd.org 	/* cached local and remote ip addresses and ports */
60091c3028Smarkus@openbsd.org 	char *remote_ipaddr;
61091c3028Smarkus@openbsd.org 	int remote_port;
6295767262Sdjm@openbsd.org 	char *local_ipaddr;
6395767262Sdjm@openbsd.org 	int local_port;
6435eb33fbSdjm@openbsd.org 	char *rdomain_in;
65d4a8b7e3SDamien Miller 
6607edd7e9Sdjm@openbsd.org 	/* Optional preamble for log messages (e.g. username) */
6707edd7e9Sdjm@openbsd.org 	char *log_preamble;
6807edd7e9Sdjm@openbsd.org 
693fdc88a0Smarkus@openbsd.org 	/* Dispatcher table */
703fdc88a0Smarkus@openbsd.org 	dispatch_fn *dispatch[DISPATCH_MAX];
713fdc88a0Smarkus@openbsd.org 	/* number of packets to ignore in the dispatcher */
723fdc88a0Smarkus@openbsd.org 	int dispatch_skip_packets;
733fdc88a0Smarkus@openbsd.org 
74091c3028Smarkus@openbsd.org 	/* datafellows */
75091c3028Smarkus@openbsd.org 	int compat;
76f582f0e9Smarkus@openbsd.org 
77f582f0e9Smarkus@openbsd.org 	/* Lists for private and public keys */
78f582f0e9Smarkus@openbsd.org 	TAILQ_HEAD(, key_entry) private_keys;
79f582f0e9Smarkus@openbsd.org 	TAILQ_HEAD(, key_entry) public_keys;
80f582f0e9Smarkus@openbsd.org 
815f4082d8Smarkus@openbsd.org 	/* Client/Server authentication context */
825f4082d8Smarkus@openbsd.org 	void *authctxt;
835f4082d8Smarkus@openbsd.org 
84dbee4119Sdjm@openbsd.org 	/* Channels context */
85dbee4119Sdjm@openbsd.org 	struct ssh_channels *chanctxt;
86dbee4119Sdjm@openbsd.org 
87f582f0e9Smarkus@openbsd.org 	/* APP data */
88f582f0e9Smarkus@openbsd.org 	void *app_data;
89091c3028Smarkus@openbsd.org };
90091c3028Smarkus@openbsd.org 
9139af7b44Sdjm@openbsd.org typedef int (ssh_packet_hook_fn)(struct ssh *, struct sshbuf *,
9239af7b44Sdjm@openbsd.org     u_char *, void *);
9339af7b44Sdjm@openbsd.org 
94091c3028Smarkus@openbsd.org struct ssh *ssh_alloc_session_state(void);
95091c3028Smarkus@openbsd.org struct ssh *ssh_packet_set_connection(struct ssh *, int, int);
96091c3028Smarkus@openbsd.org void     ssh_packet_set_timeout(struct ssh *, int, int);
97091c3028Smarkus@openbsd.org int	 ssh_packet_stop_discard(struct ssh *);
98091c3028Smarkus@openbsd.org int	 ssh_packet_connection_af(struct ssh *);
99091c3028Smarkus@openbsd.org void     ssh_packet_set_nonblocking(struct ssh *);
100091c3028Smarkus@openbsd.org int      ssh_packet_get_connection_in(struct ssh *);
101091c3028Smarkus@openbsd.org int      ssh_packet_get_connection_out(struct ssh *);
102091c3028Smarkus@openbsd.org void     ssh_packet_close(struct ssh *);
10339af7b44Sdjm@openbsd.org void	 ssh_packet_set_input_hook(struct ssh *, ssh_packet_hook_fn *, void *);
1041e0cdf8eSmarkus@openbsd.org void	 ssh_packet_clear_keys(struct ssh *);
1051e0cdf8eSmarkus@openbsd.org void	 ssh_clear_newkeys(struct ssh *, int);
10639af7b44Sdjm@openbsd.org 
10719bcf2eaSdjm@openbsd.org int	 ssh_packet_is_rekeying(struct ssh *);
108091c3028Smarkus@openbsd.org void     ssh_packet_set_protocol_flags(struct ssh *, u_int);
109091c3028Smarkus@openbsd.org u_int	 ssh_packet_get_protocol_flags(struct ssh *);
110091c3028Smarkus@openbsd.org void	 ssh_packet_set_tos(struct ssh *, int);
111091c3028Smarkus@openbsd.org void     ssh_packet_set_interactive(struct ssh *, int, int, int);
112091c3028Smarkus@openbsd.org int      ssh_packet_is_interactive(struct ssh *);
113091c3028Smarkus@openbsd.org void     ssh_packet_set_server(struct ssh *);
114091c3028Smarkus@openbsd.org void     ssh_packet_set_authenticated(struct ssh *);
1158d057847Smarkus@openbsd.org void     ssh_packet_set_mux(struct ssh *);
1168d057847Smarkus@openbsd.org int	 ssh_packet_get_mux(struct ssh *);
11707edd7e9Sdjm@openbsd.org int	 ssh_packet_set_log_preamble(struct ssh *, const char *, ...)
11807edd7e9Sdjm@openbsd.org     __attribute__((format(printf, 2, 3)));
1198d057847Smarkus@openbsd.org 
1208d057847Smarkus@openbsd.org int	 ssh_packet_log_type(u_char);
121091c3028Smarkus@openbsd.org 
122091c3028Smarkus@openbsd.org int	 ssh_packet_send2_wrapped(struct ssh *);
123091c3028Smarkus@openbsd.org int	 ssh_packet_send2(struct ssh *);
124091c3028Smarkus@openbsd.org 
125091c3028Smarkus@openbsd.org int      ssh_packet_read(struct ssh *);
1264509b5d4Sdjm@openbsd.org int	 ssh_packet_read_expect(struct ssh *, u_int type);
127091c3028Smarkus@openbsd.org int      ssh_packet_read_poll(struct ssh *);
128091c3028Smarkus@openbsd.org int ssh_packet_read_poll2(struct ssh *, u_char *, u_int32_t *seqnr_p);
129fae7bbe5Sdjm@openbsd.org int	 ssh_packet_process_incoming(struct ssh *, const char *buf, u_int len);
130091c3028Smarkus@openbsd.org int      ssh_packet_read_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
131091c3028Smarkus@openbsd.org int      ssh_packet_read_poll_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
132091c3028Smarkus@openbsd.org 
133091c3028Smarkus@openbsd.org const void *ssh_packet_get_string_ptr(struct ssh *, u_int *length_ptr);
134091c3028Smarkus@openbsd.org void     ssh_packet_disconnect(struct ssh *, const char *fmt, ...)
135091c3028Smarkus@openbsd.org 	__attribute__((format(printf, 2, 3)))
136091c3028Smarkus@openbsd.org 	__attribute__((noreturn));
137091c3028Smarkus@openbsd.org void     ssh_packet_send_debug(struct ssh *, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
138091c3028Smarkus@openbsd.org 
139091c3028Smarkus@openbsd.org int	 ssh_set_newkeys(struct ssh *, int mode);
140091c3028Smarkus@openbsd.org void	 ssh_packet_get_bytes(struct ssh *, u_int64_t *, u_int64_t *);
141091c3028Smarkus@openbsd.org 
1424509b5d4Sdjm@openbsd.org int	 ssh_packet_write_poll(struct ssh *);
1434509b5d4Sdjm@openbsd.org int	 ssh_packet_write_wait(struct ssh *);
144091c3028Smarkus@openbsd.org int      ssh_packet_have_data_to_write(struct ssh *);
145091c3028Smarkus@openbsd.org int      ssh_packet_not_very_much_data_to_write(struct ssh *);
146091c3028Smarkus@openbsd.org 
147091c3028Smarkus@openbsd.org int	 ssh_packet_connection_is_on_socket(struct ssh *);
148091c3028Smarkus@openbsd.org int	 ssh_packet_remaining(struct ssh *);
1494cc240daSBen Lindstrom 
15089dd615bSmarkus@openbsd.org void	 ssh_tty_make_modes(struct ssh *, int, struct termios *);
15189dd615bSmarkus@openbsd.org void	 ssh_tty_parse_modes(struct ssh *, int);
1524cc240daSBen Lindstrom 
153091c3028Smarkus@openbsd.org void	 ssh_packet_set_alive_timeouts(struct ssh *, int);
154091c3028Smarkus@openbsd.org int	 ssh_packet_inc_alive_timeouts(struct ssh *);
155091c3028Smarkus@openbsd.org int	 ssh_packet_set_maxsize(struct ssh *, u_int);
156091c3028Smarkus@openbsd.org u_int	 ssh_packet_get_maxsize(struct ssh *);
1576162d121SDamien Miller 
158091c3028Smarkus@openbsd.org int	 ssh_packet_get_state(struct ssh *, struct sshbuf *);
159091c3028Smarkus@openbsd.org int	 ssh_packet_set_state(struct ssh *, struct sshbuf *);
1604af51306SDamien Miller 
161091c3028Smarkus@openbsd.org const char *ssh_remote_ipaddr(struct ssh *);
162a4b9e0f4Sdjm@openbsd.org int	 ssh_remote_port(struct ssh *);
16395767262Sdjm@openbsd.org const char *ssh_local_ipaddr(struct ssh *);
16495767262Sdjm@openbsd.org int	 ssh_local_port(struct ssh *);
16535eb33fbSdjm@openbsd.org const char *ssh_packet_rdomain_in(struct ssh *);
166a5539d26SDamien Miller 
167c998bf0aSdtucker@openbsd.org void	 ssh_packet_set_rekey_limits(struct ssh *, u_int64_t, u_int32_t);
168091c3028Smarkus@openbsd.org time_t	 ssh_packet_get_rekey_timeout(struct ssh *);
169e841eb06SDarren Tucker 
170091c3028Smarkus@openbsd.org void	*ssh_packet_get_input(struct ssh *);
171091c3028Smarkus@openbsd.org void	*ssh_packet_get_output(struct ssh *);
172091c3028Smarkus@openbsd.org 
173091c3028Smarkus@openbsd.org /* new API */
174091c3028Smarkus@openbsd.org int	sshpkt_start(struct ssh *ssh, u_char type);
175091c3028Smarkus@openbsd.org int	sshpkt_send(struct ssh *ssh);
1764509b5d4Sdjm@openbsd.org int     sshpkt_disconnect(struct ssh *, const char *fmt, ...)
1774509b5d4Sdjm@openbsd.org 	    __attribute__((format(printf, 2, 3)));
178091c3028Smarkus@openbsd.org int	sshpkt_add_padding(struct ssh *, u_char);
179ad60b117Sdjm@openbsd.org void	sshpkt_fatal(struct ssh *ssh, int r, const char *fmt, ...)
180*714e1cbcSmarkus@openbsd.org 	    __attribute__((format(printf, 3, 4)))
181*714e1cbcSmarkus@openbsd.org 	    __attribute__((noreturn));
182dbee4119Sdjm@openbsd.org int	sshpkt_msg_ignore(struct ssh *, u_int);
183091c3028Smarkus@openbsd.org 
184091c3028Smarkus@openbsd.org int	sshpkt_put(struct ssh *ssh, const void *v, size_t len);
185091c3028Smarkus@openbsd.org int	sshpkt_putb(struct ssh *ssh, const struct sshbuf *b);
186091c3028Smarkus@openbsd.org int	sshpkt_put_u8(struct ssh *ssh, u_char val);
187091c3028Smarkus@openbsd.org int	sshpkt_put_u32(struct ssh *ssh, u_int32_t val);
188091c3028Smarkus@openbsd.org int	sshpkt_put_u64(struct ssh *ssh, u_int64_t val);
189091c3028Smarkus@openbsd.org int	sshpkt_put_string(struct ssh *ssh, const void *v, size_t len);
190091c3028Smarkus@openbsd.org int	sshpkt_put_cstring(struct ssh *ssh, const void *v);
191091c3028Smarkus@openbsd.org int	sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v);
192091c3028Smarkus@openbsd.org int	sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g);
193091c3028Smarkus@openbsd.org int	sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v);
194091c3028Smarkus@openbsd.org 
195091c3028Smarkus@openbsd.org int	sshpkt_get(struct ssh *ssh, void *valp, size_t len);
196091c3028Smarkus@openbsd.org int	sshpkt_get_u8(struct ssh *ssh, u_char *valp);
197091c3028Smarkus@openbsd.org int	sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp);
198091c3028Smarkus@openbsd.org int	sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp);
199091c3028Smarkus@openbsd.org int	sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp);
200091c3028Smarkus@openbsd.org int	sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
201dbee4119Sdjm@openbsd.org int	sshpkt_peek_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
202091c3028Smarkus@openbsd.org int	sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp);
20371e67fffSdjm@openbsd.org int	sshpkt_getb_froms(struct ssh *ssh, struct sshbuf **valp);
204091c3028Smarkus@openbsd.org int	sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g);
2057be8572bSdjm@openbsd.org int	sshpkt_get_bignum2(struct ssh *ssh, BIGNUM **valp);
206091c3028Smarkus@openbsd.org int	sshpkt_get_end(struct ssh *ssh);
20748c23a39Sdtucker@openbsd.org void	sshpkt_fmt_connection_id(struct ssh *ssh, char *s, size_t l);
208091c3028Smarkus@openbsd.org const u_char	*sshpkt_ptr(struct ssh *, size_t *lenp);
209091c3028Smarkus@openbsd.org 
210773dda25SDamien Miller #if !defined(WITH_OPENSSL)
211773dda25SDamien Miller # undef BIGNUM
212773dda25SDamien Miller # undef EC_KEY
213773dda25SDamien Miller # undef EC_GROUP
214773dda25SDamien Miller # undef EC_POINT
215773dda25SDamien Miller #elif !defined(OPENSSL_HAS_ECC)
216773dda25SDamien Miller # undef EC_KEY
217773dda25SDamien Miller # undef EC_GROUP
218773dda25SDamien Miller # undef EC_POINT
219773dda25SDamien Miller #endif
220773dda25SDamien Miller 
221d4a8b7e3SDamien Miller #endif				/* PACKET_H */
222