1*ce74bacaSMatthew Dillon /* $OpenBSD: packet.h,v 1.82 2017/09/12 06:32:07 djm 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; 64e9778795SPeter Avalos 65*ce74bacaSMatthew Dillon /* Optional preamble for log messages (e.g. username) */ 66*ce74bacaSMatthew Dillon char *log_preamble; 67*ce74bacaSMatthew Dillon 68e9778795SPeter Avalos /* Dispatcher table */ 69e9778795SPeter Avalos dispatch_fn *dispatch[DISPATCH_MAX]; 70e9778795SPeter Avalos /* number of packets to ignore in the dispatcher */ 71e9778795SPeter Avalos int dispatch_skip_packets; 72e9778795SPeter Avalos 73e9778795SPeter Avalos /* datafellows */ 74e9778795SPeter Avalos int compat; 75e9778795SPeter Avalos 76e9778795SPeter Avalos /* Lists for private and public keys */ 77e9778795SPeter Avalos TAILQ_HEAD(, key_entry) private_keys; 78e9778795SPeter Avalos TAILQ_HEAD(, key_entry) public_keys; 79e9778795SPeter Avalos 80*ce74bacaSMatthew Dillon /* Client/Server authentication context */ 81*ce74bacaSMatthew Dillon void *authctxt; 82*ce74bacaSMatthew Dillon 83*ce74bacaSMatthew Dillon /* Channels context */ 84*ce74bacaSMatthew Dillon struct ssh_channels *chanctxt; 85*ce74bacaSMatthew Dillon 86e9778795SPeter Avalos /* APP data */ 87e9778795SPeter Avalos void *app_data; 88e9778795SPeter Avalos }; 89e9778795SPeter Avalos 90*ce74bacaSMatthew Dillon typedef int (ssh_packet_hook_fn)(struct ssh *, struct sshbuf *, 91*ce74bacaSMatthew Dillon u_char *, void *); 92*ce74bacaSMatthew Dillon 93e9778795SPeter Avalos struct ssh *ssh_alloc_session_state(void); 94e9778795SPeter Avalos struct ssh *ssh_packet_set_connection(struct ssh *, int, int); 95e9778795SPeter Avalos void ssh_packet_set_timeout(struct ssh *, int, int); 96e9778795SPeter Avalos int ssh_packet_stop_discard(struct ssh *); 97e9778795SPeter Avalos int ssh_packet_connection_af(struct ssh *); 98e9778795SPeter Avalos void ssh_packet_set_nonblocking(struct ssh *); 99e9778795SPeter Avalos int ssh_packet_get_connection_in(struct ssh *); 100e9778795SPeter Avalos int ssh_packet_get_connection_out(struct ssh *); 101e9778795SPeter Avalos void ssh_packet_close(struct ssh *); 102*ce74bacaSMatthew Dillon void ssh_packet_set_input_hook(struct ssh *, ssh_packet_hook_fn *, void *); 103*ce74bacaSMatthew Dillon void ssh_packet_clear_keys(struct ssh *); 104*ce74bacaSMatthew Dillon void ssh_clear_newkeys(struct ssh *, int); 105*ce74bacaSMatthew Dillon 106e9778795SPeter Avalos int ssh_packet_is_rekeying(struct ssh *); 107e9778795SPeter Avalos void ssh_packet_set_protocol_flags(struct ssh *, u_int); 108e9778795SPeter Avalos u_int ssh_packet_get_protocol_flags(struct ssh *); 109e9778795SPeter Avalos int ssh_packet_start_compression(struct ssh *, int); 110e9778795SPeter Avalos void ssh_packet_set_tos(struct ssh *, int); 111e9778795SPeter Avalos void ssh_packet_set_interactive(struct ssh *, int, int, int); 112e9778795SPeter Avalos int ssh_packet_is_interactive(struct ssh *); 113e9778795SPeter Avalos void ssh_packet_set_server(struct ssh *); 114e9778795SPeter Avalos void ssh_packet_set_authenticated(struct ssh *); 115*ce74bacaSMatthew Dillon void ssh_packet_set_mux(struct ssh *); 116*ce74bacaSMatthew Dillon int ssh_packet_get_mux(struct ssh *); 117*ce74bacaSMatthew Dillon int ssh_packet_set_log_preamble(struct ssh *, const char *, ...) 118*ce74bacaSMatthew Dillon __attribute__((format(printf, 2, 3))); 119e9778795SPeter Avalos 120*ce74bacaSMatthew Dillon int ssh_packet_log_type(u_char); 121*ce74bacaSMatthew Dillon 122e9778795SPeter Avalos int ssh_packet_send2_wrapped(struct ssh *); 123e9778795SPeter Avalos int ssh_packet_send2(struct ssh *); 124e9778795SPeter Avalos 125e9778795SPeter Avalos int ssh_packet_read(struct ssh *); 126e9778795SPeter Avalos int ssh_packet_read_expect(struct ssh *, u_int type); 127e9778795SPeter Avalos int ssh_packet_read_poll(struct ssh *); 128e9778795SPeter Avalos int ssh_packet_read_poll2(struct ssh *, u_char *, u_int32_t *seqnr_p); 129e9778795SPeter Avalos int ssh_packet_process_incoming(struct ssh *, const char *buf, u_int len); 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 const void *ssh_packet_get_string_ptr(struct ssh *, u_int *length_ptr); 134e9778795SPeter Avalos void ssh_packet_disconnect(struct ssh *, const char *fmt, ...) 135e9778795SPeter Avalos __attribute__((format(printf, 2, 3))) 136e9778795SPeter Avalos __attribute__((noreturn)); 137e9778795SPeter Avalos void ssh_packet_send_debug(struct ssh *, const char *fmt, ...) __attribute__((format(printf, 2, 3))); 138e9778795SPeter Avalos 139e9778795SPeter Avalos int ssh_set_newkeys(struct ssh *, int mode); 140e9778795SPeter Avalos void ssh_packet_get_bytes(struct ssh *, u_int64_t *, u_int64_t *); 141e9778795SPeter Avalos 142e9778795SPeter Avalos int ssh_packet_write_poll(struct ssh *); 143e9778795SPeter Avalos int ssh_packet_write_wait(struct ssh *); 144e9778795SPeter Avalos int ssh_packet_have_data_to_write(struct ssh *); 145e9778795SPeter Avalos int ssh_packet_not_very_much_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 15018de8d7fSPeter Avalos void tty_make_modes(int, struct termios *); 15118de8d7fSPeter Avalos void tty_parse_modes(int, 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 *); 16518de8d7fSPeter Avalos 166*ce74bacaSMatthew Dillon void ssh_packet_set_rekey_limits(struct ssh *, u_int64_t, u_int32_t); 167e9778795SPeter Avalos time_t ssh_packet_get_rekey_timeout(struct ssh *); 16840c002afSPeter Avalos 169e9778795SPeter Avalos void *ssh_packet_get_input(struct ssh *); 170e9778795SPeter Avalos void *ssh_packet_get_output(struct ssh *); 171e9778795SPeter Avalos 172e9778795SPeter Avalos /* new API */ 173e9778795SPeter Avalos int sshpkt_start(struct ssh *ssh, u_char type); 174e9778795SPeter Avalos int sshpkt_send(struct ssh *ssh); 175e9778795SPeter Avalos int sshpkt_disconnect(struct ssh *, const char *fmt, ...) 176e9778795SPeter Avalos __attribute__((format(printf, 2, 3))); 177e9778795SPeter Avalos int sshpkt_add_padding(struct ssh *, u_char); 178e9778795SPeter Avalos void sshpkt_fatal(struct ssh *ssh, const char *tag, int r); 179*ce74bacaSMatthew Dillon int sshpkt_msg_ignore(struct ssh *, u_int); 180e9778795SPeter Avalos 181e9778795SPeter Avalos int sshpkt_put(struct ssh *ssh, const void *v, size_t len); 182e9778795SPeter Avalos int sshpkt_putb(struct ssh *ssh, const struct sshbuf *b); 183e9778795SPeter Avalos int sshpkt_put_u8(struct ssh *ssh, u_char val); 184e9778795SPeter Avalos int sshpkt_put_u32(struct ssh *ssh, u_int32_t val); 185e9778795SPeter Avalos int sshpkt_put_u64(struct ssh *ssh, u_int64_t val); 186e9778795SPeter Avalos int sshpkt_put_string(struct ssh *ssh, const void *v, size_t len); 187e9778795SPeter Avalos int sshpkt_put_cstring(struct ssh *ssh, const void *v); 188e9778795SPeter Avalos int sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v); 189e9778795SPeter Avalos int sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g); 190e9778795SPeter Avalos int sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v); 191e9778795SPeter Avalos 192e9778795SPeter Avalos int sshpkt_get(struct ssh *ssh, void *valp, size_t len); 193e9778795SPeter Avalos int sshpkt_get_u8(struct ssh *ssh, u_char *valp); 194e9778795SPeter Avalos int sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp); 195e9778795SPeter Avalos int sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp); 196e9778795SPeter Avalos int sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp); 197e9778795SPeter Avalos int sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp); 198*ce74bacaSMatthew Dillon int sshpkt_peek_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp); 199e9778795SPeter Avalos int sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp); 200e9778795SPeter Avalos int sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g); 201e9778795SPeter Avalos int sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v); 202e9778795SPeter Avalos int sshpkt_get_end(struct ssh *ssh); 203e9778795SPeter Avalos const u_char *sshpkt_ptr(struct ssh *, size_t *lenp); 204e9778795SPeter Avalos 205e9778795SPeter Avalos /* OLD API */ 206e9778795SPeter Avalos extern struct ssh *active_state; 207e9778795SPeter Avalos #include "opacket.h" 208e9778795SPeter Avalos 209e9778795SPeter Avalos #if !defined(WITH_OPENSSL) 210e9778795SPeter Avalos # undef BIGNUM 211e9778795SPeter Avalos # undef EC_KEY 212e9778795SPeter Avalos # undef EC_GROUP 213e9778795SPeter Avalos # undef EC_POINT 214e9778795SPeter Avalos #elif !defined(OPENSSL_HAS_ECC) 215e9778795SPeter Avalos # undef EC_KEY 216e9778795SPeter Avalos # undef EC_GROUP 217e9778795SPeter Avalos # undef EC_POINT 218e9778795SPeter Avalos #endif 21940c002afSPeter Avalos 22018de8d7fSPeter Avalos #endif /* PACKET_H */ 221