1296853Sdes/* $OpenBSD: packet.h,v 1.70 2016/02/08 10:57:07 djm Exp $ */
292559Sdes
357429Smarkm/*
457429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi>
557429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
657429Smarkm *                    All rights reserved
757429Smarkm * Interface for the packet protocol functions.
860576Skris *
965674Skris * As far as I am concerned, the code I have written for this software
1065674Skris * can be used freely for any purpose.  Any derived versions of this
1165674Skris * software must be clearly marked as such, and if the derived work is
1265674Skris * incompatible with the protocol description in the RFC file, it must be
1365674Skris * called by a name other than "ssh" or "Secure Shell".
1457429Smarkm */
1557429Smarkm
1657429Smarkm#ifndef PACKET_H
1757429Smarkm#define PACKET_H
1857429Smarkm
19162856Sdes#include <termios.h>
20162856Sdes
21295367Sdes#ifdef WITH_OPENSSL
22295367Sdes# include <openssl/bn.h>
23295367Sdes# ifdef OPENSSL_HAS_ECC
24295367Sdes#  include <openssl/ec.h>
25295367Sdes# else /* OPENSSL_HAS_ECC */
26295367Sdes#  define EC_KEY	void
27295367Sdes#  define EC_GROUP	void
28295367Sdes#  define EC_POINT	void
29295367Sdes# endif /* OPENSSL_HAS_ECC */
30295367Sdes#else /* WITH_OPENSSL */
31295367Sdes# define BIGNUM		void
32295367Sdes# define EC_KEY		void
33295367Sdes# define EC_GROUP	void
34295367Sdes# define EC_POINT	void
35295367Sdes#endif /* WITH_OPENSSL */
3657429Smarkm
37295367Sdes#include <signal.h>
38295367Sdes#include "openbsd-compat/sys-queue.h"
3957429Smarkm
40295367Sdesstruct kex;
41295367Sdesstruct sshkey;
42295367Sdesstruct sshbuf;
43295367Sdesstruct session_state;	/* private session data */
4457429Smarkm
45295367Sdes#include "dispatch.h"	/* typedef, DISPATCH_MAX */
4657429Smarkm
47295367Sdesstruct key_entry {
48295367Sdes	TAILQ_ENTRY(key_entry) next;
49295367Sdes	struct sshkey *key;
50295367Sdes};
5157429Smarkm
52295367Sdesstruct ssh {
53295367Sdes	/* Session state */
54295367Sdes	struct session_state *state;
5598684Sdes
56295367Sdes	/* Key exchange */
57295367Sdes	struct kex *kex;
5857429Smarkm
59295367Sdes	/* cached remote ip address and port*/
60295367Sdes	char *remote_ipaddr;
61295367Sdes	int remote_port;
6257429Smarkm
63295367Sdes	/* Dispatcher table */
64295367Sdes	dispatch_fn *dispatch[DISPATCH_MAX];
65295367Sdes	/* number of packets to ignore in the dispatcher */
66295367Sdes	int dispatch_skip_packets;
67295367Sdes
68295367Sdes	/* datafellows */
69295367Sdes	int compat;
70295367Sdes
71295367Sdes	/* Lists for private and public keys */
72295367Sdes	TAILQ_HEAD(, key_entry) private_keys;
73295367Sdes	TAILQ_HEAD(, key_entry) public_keys;
74295367Sdes
75295367Sdes	/* APP data */
76295367Sdes	void *app_data;
77295367Sdes};
78295367Sdes
79295367Sdesstruct ssh *ssh_alloc_session_state(void);
80295367Sdesstruct ssh *ssh_packet_set_connection(struct ssh *, int, int);
81295367Sdesvoid     ssh_packet_set_timeout(struct ssh *, int, int);
82295367Sdesint	 ssh_packet_stop_discard(struct ssh *);
83295367Sdesint	 ssh_packet_connection_af(struct ssh *);
84295367Sdesvoid     ssh_packet_set_nonblocking(struct ssh *);
85295367Sdesint      ssh_packet_get_connection_in(struct ssh *);
86295367Sdesint      ssh_packet_get_connection_out(struct ssh *);
87295367Sdesvoid     ssh_packet_close(struct ssh *);
88295367Sdesvoid	 ssh_packet_set_encryption_key(struct ssh *, const u_char *, u_int, int);
89296853Sdesint	 ssh_packet_is_rekeying(struct ssh *);
90295367Sdesvoid     ssh_packet_set_protocol_flags(struct ssh *, u_int);
91295367Sdesu_int	 ssh_packet_get_protocol_flags(struct ssh *);
92295367Sdesint      ssh_packet_start_compression(struct ssh *, int);
93295367Sdesvoid	 ssh_packet_set_tos(struct ssh *, int);
94295367Sdesvoid     ssh_packet_set_interactive(struct ssh *, int, int, int);
95295367Sdesint      ssh_packet_is_interactive(struct ssh *);
96295367Sdesvoid     ssh_packet_set_server(struct ssh *);
97295367Sdesvoid     ssh_packet_set_authenticated(struct ssh *);
98295367Sdes
99295367Sdesint	 ssh_packet_send1(struct ssh *);
100295367Sdesint	 ssh_packet_send2_wrapped(struct ssh *);
101295367Sdesint	 ssh_packet_send2(struct ssh *);
102295367Sdes
103295367Sdesint      ssh_packet_read(struct ssh *);
104295367Sdesint	 ssh_packet_read_expect(struct ssh *, u_int type);
105295367Sdesint      ssh_packet_read_poll(struct ssh *);
106295367Sdesint ssh_packet_read_poll1(struct ssh *, u_char *);
107295367Sdesint ssh_packet_read_poll2(struct ssh *, u_char *, u_int32_t *seqnr_p);
108295367Sdesint	 ssh_packet_process_incoming(struct ssh *, const char *buf, u_int len);
109295367Sdesint      ssh_packet_read_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
110295367Sdesint      ssh_packet_read_poll_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
111295367Sdes
112295367Sdesconst void *ssh_packet_get_string_ptr(struct ssh *, u_int *length_ptr);
113295367Sdesvoid     ssh_packet_disconnect(struct ssh *, const char *fmt, ...)
114295367Sdes	__attribute__((format(printf, 2, 3)))
115295367Sdes	__attribute__((noreturn));
116295367Sdesvoid     ssh_packet_send_debug(struct ssh *, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
117295367Sdes
118295367Sdesint	 ssh_set_newkeys(struct ssh *, int mode);
119295367Sdesvoid	 ssh_packet_get_bytes(struct ssh *, u_int64_t *, u_int64_t *);
120295367Sdes
121295367Sdestypedef void *(ssh_packet_comp_alloc_func)(void *, u_int, u_int);
122295367Sdestypedef void (ssh_packet_comp_free_func)(void *, void *);
123295367Sdesvoid	 ssh_packet_set_compress_hooks(struct ssh *, void *,
124295367Sdes    ssh_packet_comp_alloc_func *, ssh_packet_comp_free_func *);
125295367Sdes
126295367Sdesint	 ssh_packet_write_poll(struct ssh *);
127295367Sdesint	 ssh_packet_write_wait(struct ssh *);
128295367Sdesint      ssh_packet_have_data_to_write(struct ssh *);
129295367Sdesint      ssh_packet_not_very_much_data_to_write(struct ssh *);
130295367Sdes
131295367Sdesint	 ssh_packet_connection_is_on_socket(struct ssh *);
132295367Sdesint	 ssh_packet_remaining(struct ssh *);
133295367Sdesvoid	 ssh_packet_send_ignore(struct ssh *, int);
134295367Sdes
13592559Sdesvoid	 tty_make_modes(int, struct termios *);
13692559Sdesvoid	 tty_parse_modes(int, int *);
13757429Smarkm
138295367Sdesvoid	 ssh_packet_set_alive_timeouts(struct ssh *, int);
139295367Sdesint	 ssh_packet_inc_alive_timeouts(struct ssh *);
140295367Sdesint	 ssh_packet_set_maxsize(struct ssh *, u_int);
141295367Sdesu_int	 ssh_packet_get_maxsize(struct ssh *);
14257429Smarkm
143295367Sdesint	 ssh_packet_get_state(struct ssh *, struct sshbuf *);
144295367Sdesint	 ssh_packet_set_state(struct ssh *, struct sshbuf *);
14560576Skris
146295367Sdesconst char *ssh_remote_ipaddr(struct ssh *);
147296853Sdesint	 ssh_remote_port(struct ssh *);
148124211Sdes
149296853Sdesvoid	 ssh_packet_set_rekey_limits(struct ssh *, u_int64_t, time_t);
150295367Sdestime_t	 ssh_packet_get_rekey_timeout(struct ssh *);
151197679Sdes
152295367Sdesvoid	*ssh_packet_get_input(struct ssh *);
153295367Sdesvoid	*ssh_packet_get_output(struct ssh *);
154295367Sdes
155295367Sdes/* new API */
156295367Sdesint	sshpkt_start(struct ssh *ssh, u_char type);
157295367Sdesint	sshpkt_send(struct ssh *ssh);
158295367Sdesint     sshpkt_disconnect(struct ssh *, const char *fmt, ...)
159295367Sdes	    __attribute__((format(printf, 2, 3)));
160295367Sdesint	sshpkt_add_padding(struct ssh *, u_char);
161295367Sdesvoid	sshpkt_fatal(struct ssh *ssh, const char *tag, int r);
162295367Sdes
163295367Sdesint	sshpkt_put(struct ssh *ssh, const void *v, size_t len);
164295367Sdesint	sshpkt_putb(struct ssh *ssh, const struct sshbuf *b);
165295367Sdesint	sshpkt_put_u8(struct ssh *ssh, u_char val);
166295367Sdesint	sshpkt_put_u32(struct ssh *ssh, u_int32_t val);
167295367Sdesint	sshpkt_put_u64(struct ssh *ssh, u_int64_t val);
168295367Sdesint	sshpkt_put_string(struct ssh *ssh, const void *v, size_t len);
169295367Sdesint	sshpkt_put_cstring(struct ssh *ssh, const void *v);
170295367Sdesint	sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v);
171295367Sdesint	sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g);
172295367Sdesint	sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v);
173295367Sdesint	sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v);
174295367Sdes
175295367Sdesint	sshpkt_get(struct ssh *ssh, void *valp, size_t len);
176295367Sdesint	sshpkt_get_u8(struct ssh *ssh, u_char *valp);
177295367Sdesint	sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp);
178295367Sdesint	sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp);
179295367Sdesint	sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp);
180295367Sdesint	sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
181295367Sdesint	sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp);
182295367Sdesint	sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g);
183295367Sdesint	sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v);
184295367Sdesint	sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v);
185295367Sdesint	sshpkt_get_end(struct ssh *ssh);
186295367Sdesconst u_char	*sshpkt_ptr(struct ssh *, size_t *lenp);
187295367Sdes
188295367Sdes/* OLD API */
189295367Sdesextern struct ssh *active_state;
190295367Sdes#include "opacket.h"
191295367Sdes
192295367Sdes#if !defined(WITH_OPENSSL)
193295367Sdes# undef BIGNUM
194295367Sdes# undef EC_KEY
195295367Sdes# undef EC_GROUP
196295367Sdes# undef EC_POINT
197295367Sdes#elif !defined(OPENSSL_HAS_ECC)
198295367Sdes# undef EC_KEY
199295367Sdes# undef EC_GROUP
200295367Sdes# undef EC_POINT
201295367Sdes#endif
202295367Sdes
20357429Smarkm#endif				/* PACKET_H */
204