markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1 | /* $OpenBSD: packet.c,v 1.200 2015/01/13 19:31:40 markus Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * This file contains code implementing the packet protocol and communication |
| 7 | * with the other side. This same code is used both on client and server side. |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * As far as I am concerned, the code I have written for this software |
| 10 | * can be used freely for any purpose. Any derived versions of this |
| 11 | * software must be clearly marked as such, and if the derived work is |
| 12 | * incompatible with the protocol description in the RFC file, it must be |
| 13 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 14 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | * |
| 16 | * SSH2 packet format added by Markus Friedl. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 17 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 18 | * |
| 19 | * Redistribution and use in source and binary forms, with or without |
| 20 | * modification, are permitted provided that the following conditions |
| 21 | * are met: |
| 22 | * 1. Redistributions of source code must retain the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer. |
| 24 | * 2. Redistributions in binary form must reproduce the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer in the |
| 26 | * documentation and/or other materials provided with the distribution. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 30 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 33 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 37 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 38 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
| 40 | #include "includes.h" |
Damien Miller | 68f8e99 | 2006-03-15 11:24:12 +1100 | [diff] [blame] | 41 | |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 42 | #include <sys/types.h> |
Damien Miller | a0898b8 | 2003-04-09 21:05:52 +1000 | [diff] [blame] | 43 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 44 | #include <sys/param.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 45 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 46 | #ifdef HAVE_SYS_TIME_H |
| 47 | # include <sys/time.h> |
| 48 | #endif |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 49 | |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 50 | #include <netinet/in.h> |
Damien Miller | 68f8e99 | 2006-03-15 11:24:12 +1100 | [diff] [blame] | 51 | #include <netinet/ip.h> |
Darren Tucker | dace233 | 2006-09-22 19:22:17 +1000 | [diff] [blame] | 52 | #include <arpa/inet.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 54 | #include <errno.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 55 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 56 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 57 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 58 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 59 | #include <unistd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 60 | #include <signal.h> |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 61 | #include <time.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 62 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 63 | #include "xmalloc.h" |
| 64 | #include "buffer.h" |
| 65 | #include "packet.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | #include "crc32.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 67 | #include "compress.h" |
| 68 | #include "deattack.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 69 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 70 | #include "ssh1.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 71 | #include "ssh2.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 72 | #include "cipher.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 73 | #include "key.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 74 | #include "kex.h" |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 75 | #include "digest.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 76 | #include "mac.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 77 | #include "log.h" |
| 78 | #include "canohost.h" |
Damien Miller | 4d00776 | 2002-02-05 11:52:54 +1100 | [diff] [blame] | 79 | #include "misc.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 80 | #include "channels.h" |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 81 | #include "ssh.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 82 | #include "ssherr.h" |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 83 | #include "roaming.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 84 | |
| 85 | #ifdef PACKET_DEBUG |
| 86 | #define DBG(x) x |
| 87 | #else |
| 88 | #define DBG(x) |
| 89 | #endif |
| 90 | |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 91 | #define PACKET_MAX_SIZE (256 * 1024) |
| 92 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 93 | struct packet_state { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 94 | u_int32_t seqnr; |
| 95 | u_int32_t packets; |
| 96 | u_int64_t blocks; |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 97 | u_int64_t bytes; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 98 | }; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 99 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 100 | struct packet { |
| 101 | TAILQ_ENTRY(packet) next; |
| 102 | u_char type; |
| 103 | Buffer payload; |
| 104 | }; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 105 | |
| 106 | struct session_state { |
| 107 | /* |
| 108 | * This variable contains the file descriptors used for |
| 109 | * communicating with the other side. connection_in is used for |
| 110 | * reading; connection_out for writing. These can be the same |
| 111 | * descriptor, in which case it is assumed to be a socket. |
| 112 | */ |
| 113 | int connection_in; |
| 114 | int connection_out; |
| 115 | |
| 116 | /* Protocol flags for the remote side. */ |
| 117 | u_int remote_protocol_flags; |
| 118 | |
| 119 | /* Encryption context for receiving data. Only used for decryption. */ |
| 120 | CipherContext receive_context; |
| 121 | |
| 122 | /* Encryption context for sending data. Only used for encryption. */ |
| 123 | CipherContext send_context; |
| 124 | |
| 125 | /* Buffer for raw input data from the socket. */ |
| 126 | Buffer input; |
| 127 | |
| 128 | /* Buffer for raw output data going to the socket. */ |
| 129 | Buffer output; |
| 130 | |
| 131 | /* Buffer for the partial outgoing packet being constructed. */ |
| 132 | Buffer outgoing_packet; |
| 133 | |
| 134 | /* Buffer for the incoming packet currently being processed. */ |
| 135 | Buffer incoming_packet; |
| 136 | |
| 137 | /* Scratch buffer for packet compression/decompression. */ |
| 138 | Buffer compression_buffer; |
| 139 | int compression_buffer_ready; |
| 140 | |
| 141 | /* |
| 142 | * Flag indicating whether packet compression/decompression is |
| 143 | * enabled. |
| 144 | */ |
| 145 | int packet_compression; |
| 146 | |
| 147 | /* default maximum packet size */ |
| 148 | u_int max_packet_size; |
| 149 | |
| 150 | /* Flag indicating whether this module has been initialized. */ |
| 151 | int initialized; |
| 152 | |
| 153 | /* Set to true if the connection is interactive. */ |
| 154 | int interactive_mode; |
| 155 | |
| 156 | /* Set to true if we are the server side. */ |
| 157 | int server_side; |
| 158 | |
| 159 | /* Set to true if we are authenticated. */ |
| 160 | int after_authentication; |
| 161 | |
| 162 | int keep_alive_timeouts; |
| 163 | |
| 164 | /* The maximum time that we will wait to send or receive a packet */ |
| 165 | int packet_timeout_ms; |
| 166 | |
| 167 | /* Session key information for Encryption and MAC */ |
| 168 | Newkeys *newkeys[MODE_MAX]; |
| 169 | struct packet_state p_read, p_send; |
| 170 | |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 171 | /* Volume-based rekeying */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 172 | u_int64_t max_blocks_in, max_blocks_out; |
| 173 | u_int32_t rekey_limit; |
| 174 | |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 175 | /* Time-based rekeying */ |
| 176 | time_t rekey_interval; /* how often in seconds */ |
| 177 | time_t rekey_time; /* time of last rekeying */ |
| 178 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 179 | /* Session key for protocol v1 */ |
| 180 | u_char ssh1_key[SSH_SESSION_KEY_LENGTH]; |
| 181 | u_int ssh1_keylen; |
| 182 | |
| 183 | /* roundup current message to extra_pad bytes */ |
| 184 | u_char extra_pad; |
| 185 | |
| 186 | /* XXX discard incoming data after MAC error */ |
| 187 | u_int packet_discard; |
| 188 | Mac *packet_discard_mac; |
| 189 | |
| 190 | /* Used in packet_read_poll2() */ |
| 191 | u_int packlen; |
| 192 | |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 193 | /* Used in packet_send2 */ |
| 194 | int rekeying; |
| 195 | |
| 196 | /* Used in packet_set_interactive */ |
| 197 | int set_interactive_called; |
| 198 | |
| 199 | /* Used in packet_set_maxsize */ |
| 200 | int set_maxsize_called; |
| 201 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 202 | TAILQ_HEAD(, packet) outgoing; |
| 203 | }; |
| 204 | |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 205 | static struct session_state *active_state, *backup_state; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 206 | |
| 207 | static struct session_state * |
Darren Tucker | b422afa | 2009-06-21 18:58:46 +1000 | [diff] [blame] | 208 | alloc_session_state(void) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 209 | { |
Damien Miller | 7a221a1 | 2010-11-20 15:14:29 +1100 | [diff] [blame] | 210 | struct session_state *s = xcalloc(1, sizeof(*s)); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 211 | |
Damien Miller | 7a221a1 | 2010-11-20 15:14:29 +1100 | [diff] [blame] | 212 | s->connection_in = -1; |
| 213 | s->connection_out = -1; |
| 214 | s->max_packet_size = 32768; |
| 215 | s->packet_timeout_ms = -1; |
| 216 | return s; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 217 | } |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 218 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 219 | /* |
| 220 | * Sets the descriptors used for communication. Disables encryption until |
| 221 | * packet_set_encryption_key is called. |
| 222 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 223 | void |
| 224 | packet_set_connection(int fd_in, int fd_out) |
| 225 | { |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 226 | const Cipher *none = cipher_by_name("none"); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 227 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 228 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 229 | if (none == NULL) |
| 230 | fatal("packet_set_connection: cannot load cipher 'none'"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 231 | if (active_state == NULL) |
| 232 | active_state = alloc_session_state(); |
| 233 | active_state->connection_in = fd_in; |
| 234 | active_state->connection_out = fd_out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 235 | if ((r = cipher_init(&active_state->send_context, none, |
| 236 | (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 || |
| 237 | (r = cipher_init(&active_state->receive_context, none, |
| 238 | (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) |
| 239 | fatal("%s: cipher_init: %s", __func__, ssh_err(r)); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 240 | active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL; |
| 241 | if (!active_state->initialized) { |
| 242 | active_state->initialized = 1; |
| 243 | buffer_init(&active_state->input); |
| 244 | buffer_init(&active_state->output); |
| 245 | buffer_init(&active_state->outgoing_packet); |
| 246 | buffer_init(&active_state->incoming_packet); |
| 247 | TAILQ_INIT(&active_state->outgoing); |
| 248 | active_state->p_send.packets = active_state->p_read.packets = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 249 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 250 | } |
| 251 | |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 252 | void |
| 253 | packet_set_timeout(int timeout, int count) |
| 254 | { |
Damien Miller | 8ed4de8 | 2011-12-19 10:52:50 +1100 | [diff] [blame] | 255 | if (timeout <= 0 || count <= 0) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 256 | active_state->packet_timeout_ms = -1; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | if ((INT_MAX / 1000) / count < timeout) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 260 | active_state->packet_timeout_ms = INT_MAX; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 261 | else |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 262 | active_state->packet_timeout_ms = timeout * count * 1000; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 263 | } |
| 264 | |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 265 | static void |
| 266 | packet_stop_discard(void) |
| 267 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 268 | if (active_state->packet_discard_mac) { |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 269 | char buf[1024]; |
| 270 | |
| 271 | memset(buf, 'a', sizeof(buf)); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 272 | while (buffer_len(&active_state->incoming_packet) < |
| 273 | PACKET_MAX_SIZE) |
| 274 | buffer_append(&active_state->incoming_packet, buf, |
| 275 | sizeof(buf)); |
| 276 | (void) mac_compute(active_state->packet_discard_mac, |
| 277 | active_state->p_read.seqnr, |
| 278 | buffer_ptr(&active_state->incoming_packet), |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 279 | PACKET_MAX_SIZE, NULL, 0); |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 280 | } |
| 281 | logit("Finished discarding for %.200s", get_remote_ipaddr()); |
| 282 | cleanup_exit(255); |
| 283 | } |
| 284 | |
| 285 | static void |
| 286 | packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard) |
| 287 | { |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 288 | if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 289 | packet_disconnect("Packet corrupt"); |
| 290 | if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 291 | active_state->packet_discard_mac = mac; |
| 292 | if (buffer_len(&active_state->input) >= discard) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 293 | packet_stop_discard(); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 294 | active_state->packet_discard = discard - |
| 295 | buffer_len(&active_state->input); |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 296 | } |
| 297 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 298 | /* Returns 1 if remote host is connected via socket, 0 if not. */ |
| 299 | |
| 300 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 301 | packet_connection_is_on_socket(void) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 302 | { |
| 303 | struct sockaddr_storage from, to; |
| 304 | socklen_t fromlen, tolen; |
| 305 | |
| 306 | /* filedescriptors in and out are the same, so it's a socket */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 307 | if (active_state->connection_in == active_state->connection_out) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 308 | return 1; |
| 309 | fromlen = sizeof(from); |
| 310 | memset(&from, 0, sizeof(from)); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 311 | if (getpeername(active_state->connection_in, (struct sockaddr *)&from, |
| 312 | &fromlen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 313 | return 0; |
| 314 | tolen = sizeof(to); |
| 315 | memset(&to, 0, sizeof(to)); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 316 | if (getpeername(active_state->connection_out, (struct sockaddr *)&to, |
| 317 | &tolen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 318 | return 0; |
| 319 | if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0) |
| 320 | return 0; |
| 321 | if (from.ss_family != AF_INET && from.ss_family != AF_INET6) |
| 322 | return 0; |
| 323 | return 1; |
| 324 | } |
| 325 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 326 | /* |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 327 | * Exports an IV from the CipherContext required to export the key |
| 328 | * state back from the unprivileged child to the privileged parent |
| 329 | * process. |
| 330 | */ |
| 331 | |
| 332 | void |
| 333 | packet_get_keyiv(int mode, u_char *iv, u_int len) |
| 334 | { |
| 335 | CipherContext *cc; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 336 | int r; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 337 | |
| 338 | if (mode == MODE_OUT) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 339 | cc = &active_state->send_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 340 | else |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 341 | cc = &active_state->receive_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 342 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 343 | if ((r = cipher_get_keyiv(cc, iv, len)) != 0) |
| 344 | fatal("%s: cipher_get_keyiv: %s", __func__, ssh_err(r)); |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | int |
| 348 | packet_get_keycontext(int mode, u_char *dat) |
| 349 | { |
| 350 | CipherContext *cc; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 351 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 352 | if (mode == MODE_OUT) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 353 | cc = &active_state->send_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 354 | else |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 355 | cc = &active_state->receive_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 356 | |
| 357 | return (cipher_get_keycontext(cc, dat)); |
| 358 | } |
| 359 | |
| 360 | void |
| 361 | packet_set_keycontext(int mode, u_char *dat) |
| 362 | { |
| 363 | CipherContext *cc; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 364 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 365 | if (mode == MODE_OUT) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 366 | cc = &active_state->send_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 367 | else |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 368 | cc = &active_state->receive_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 369 | |
| 370 | cipher_set_keycontext(cc, dat); |
| 371 | } |
| 372 | |
| 373 | int |
| 374 | packet_get_keyiv_len(int mode) |
| 375 | { |
| 376 | CipherContext *cc; |
| 377 | |
| 378 | if (mode == MODE_OUT) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 379 | cc = &active_state->send_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 380 | else |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 381 | cc = &active_state->receive_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 382 | |
| 383 | return (cipher_get_keyiv_len(cc)); |
| 384 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 385 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 386 | void |
| 387 | packet_set_iv(int mode, u_char *dat) |
| 388 | { |
| 389 | CipherContext *cc; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 390 | int r; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 391 | |
| 392 | if (mode == MODE_OUT) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 393 | cc = &active_state->send_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 394 | else |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 395 | cc = &active_state->receive_context; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 396 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 397 | if ((r = cipher_set_keyiv(cc, dat)) != 0) |
| 398 | fatal("%s: cipher_set_keyiv: %s", __func__, ssh_err(r)); |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 399 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 400 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 401 | int |
Damien Miller | 2b92d32 | 2003-06-11 22:05:06 +1000 | [diff] [blame] | 402 | packet_get_ssh1_cipher(void) |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 403 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 404 | return (cipher_get_number(active_state->receive_context.cipher)); |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 407 | void |
Damien Miller | 7a221a1 | 2010-11-20 15:14:29 +1100 | [diff] [blame] | 408 | packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, |
| 409 | u_int32_t *packets, u_int64_t *bytes) |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 410 | { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 411 | struct packet_state *state; |
| 412 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 413 | state = (mode == MODE_IN) ? |
| 414 | &active_state->p_read : &active_state->p_send; |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 415 | if (seqnr) |
| 416 | *seqnr = state->seqnr; |
| 417 | if (blocks) |
| 418 | *blocks = state->blocks; |
| 419 | if (packets) |
| 420 | *packets = state->packets; |
| 421 | if (bytes) |
| 422 | *bytes = state->bytes; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | void |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 426 | packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets, |
| 427 | u_int64_t bytes) |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 428 | { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 429 | struct packet_state *state; |
| 430 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 431 | state = (mode == MODE_IN) ? |
| 432 | &active_state->p_read : &active_state->p_send; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 433 | state->seqnr = seqnr; |
| 434 | state->blocks = blocks; |
| 435 | state->packets = packets; |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 436 | state->bytes = bytes; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 439 | static int |
| 440 | packet_connection_af(void) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 441 | { |
| 442 | struct sockaddr_storage to; |
Damien Miller | 6fe375d | 2000-01-23 09:38:00 +1100 | [diff] [blame] | 443 | socklen_t tolen = sizeof(to); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 444 | |
| 445 | memset(&to, 0, sizeof(to)); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 446 | if (getsockname(active_state->connection_out, (struct sockaddr *)&to, |
| 447 | &tolen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 448 | return 0; |
Damien Miller | aa100c5 | 2002-04-26 16:54:34 +1000 | [diff] [blame] | 449 | #ifdef IPV4_IN_IPV6 |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 450 | if (to.ss_family == AF_INET6 && |
Damien Miller | aa100c5 | 2002-04-26 16:54:34 +1000 | [diff] [blame] | 451 | IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr)) |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 452 | return AF_INET; |
Damien Miller | aa100c5 | 2002-04-26 16:54:34 +1000 | [diff] [blame] | 453 | #endif |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 454 | return to.ss_family; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 455 | } |
| 456 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 457 | /* Sets the connection into non-blocking mode. */ |
| 458 | |
| 459 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 460 | packet_set_nonblocking(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 461 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 462 | /* Set the socket into non-blocking mode. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 463 | set_nonblock(active_state->connection_in); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 464 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 465 | if (active_state->connection_out != active_state->connection_in) |
| 466 | set_nonblock(active_state->connection_out); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Returns the socket used for reading. */ |
| 470 | |
| 471 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 472 | packet_get_connection_in(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 473 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 474 | return active_state->connection_in; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* Returns the descriptor used for writing. */ |
| 478 | |
| 479 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 480 | packet_get_connection_out(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 481 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 482 | return active_state->connection_out; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* Closes the connection and clears and frees internal data structures. */ |
| 486 | |
| 487 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 488 | packet_close(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 489 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 490 | if (!active_state->initialized) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 491 | return; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 492 | active_state->initialized = 0; |
| 493 | if (active_state->connection_in == active_state->connection_out) { |
| 494 | shutdown(active_state->connection_out, SHUT_RDWR); |
| 495 | close(active_state->connection_out); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 496 | } else { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 497 | close(active_state->connection_in); |
| 498 | close(active_state->connection_out); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 499 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 500 | buffer_free(&active_state->input); |
| 501 | buffer_free(&active_state->output); |
| 502 | buffer_free(&active_state->outgoing_packet); |
| 503 | buffer_free(&active_state->incoming_packet); |
| 504 | if (active_state->compression_buffer_ready) { |
| 505 | buffer_free(&active_state->compression_buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 506 | buffer_compress_uninit(); |
| 507 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 508 | cipher_cleanup(&active_state->send_context); |
| 509 | cipher_cleanup(&active_state->receive_context); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* Sets remote side protocol flags. */ |
| 513 | |
| 514 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 515 | packet_set_protocol_flags(u_int protocol_flags) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 516 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 517 | active_state->remote_protocol_flags = protocol_flags; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* Returns the remote protocol flags set earlier by the above function. */ |
| 521 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 522 | u_int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 523 | packet_get_protocol_flags(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 524 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 525 | return active_state->remote_protocol_flags; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 526 | } |
| 527 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 528 | /* |
| 529 | * Starts packet compression from the next packet on in both directions. |
| 530 | * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip. |
| 531 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 532 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 533 | static void |
| 534 | packet_init_compression(void) |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 535 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 536 | if (active_state->compression_buffer_ready == 1) |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 537 | return; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 538 | active_state->compression_buffer_ready = 1; |
| 539 | buffer_init(&active_state->compression_buffer); |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 542 | void |
| 543 | packet_start_compression(int level) |
| 544 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 545 | if (active_state->packet_compression && !compat20) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 546 | fatal("Compression already enabled."); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 547 | active_state->packet_compression = 1; |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 548 | packet_init_compression(); |
| 549 | buffer_compress_init_send(level); |
| 550 | buffer_compress_init_recv(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 551 | } |
| 552 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 553 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 554 | * Causes any further packets to be encrypted using the given key. The same |
| 555 | * key is used for both sending and reception. However, both directions are |
| 556 | * encrypted independently of each other. |
| 557 | */ |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 558 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 559 | void |
Damien Miller | 7a221a1 | 2010-11-20 15:14:29 +1100 | [diff] [blame] | 560 | packet_set_encryption_key(const u_char *key, u_int keylen, int number) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 561 | { |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 562 | const Cipher *cipher = cipher_by_number(number); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 563 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 564 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 565 | if (cipher == NULL) |
| 566 | fatal("packet_set_encryption_key: unknown cipher number %d", number); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 567 | if (keylen < 20) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 568 | fatal("packet_set_encryption_key: keylen too small: %d", keylen); |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 569 | if (keylen > SSH_SESSION_KEY_LENGTH) |
| 570 | fatal("packet_set_encryption_key: keylen too big: %d", keylen); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 571 | memcpy(active_state->ssh1_key, key, keylen); |
| 572 | active_state->ssh1_keylen = keylen; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 573 | if ((r = cipher_init(&active_state->send_context, cipher, |
| 574 | key, keylen, NULL, 0, CIPHER_ENCRYPT)) != 0 || |
| 575 | (r = cipher_init(&active_state->receive_context, cipher, |
| 576 | key, keylen, NULL, 0, CIPHER_DECRYPT)) != 0) |
| 577 | fatal("%s: cipher_init: %s", __func__, ssh_err(r)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 578 | } |
| 579 | |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 580 | u_int |
| 581 | packet_get_encryption_key(u_char *key) |
| 582 | { |
| 583 | if (key == NULL) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 584 | return (active_state->ssh1_keylen); |
| 585 | memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen); |
| 586 | return (active_state->ssh1_keylen); |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 589 | /* Start constructing a packet to send. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 590 | void |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 591 | packet_start(u_char type) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 592 | { |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 593 | u_char buf[9]; |
| 594 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 595 | |
Ben Lindstrom | 204e488 | 2001-03-05 06:47:00 +0000 | [diff] [blame] | 596 | DBG(debug("packet_start[%d]", type)); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 597 | len = compat20 ? 6 : 9; |
| 598 | memset(buf, 0, len - 1); |
| 599 | buf[len - 1] = type; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 600 | buffer_clear(&active_state->outgoing_packet); |
| 601 | buffer_append(&active_state->outgoing_packet, buf, len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 602 | } |
| 603 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 604 | /* Append payload. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 605 | void |
| 606 | packet_put_char(int value) |
| 607 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 608 | char ch = value; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 609 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 610 | buffer_append(&active_state->outgoing_packet, &ch, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 611 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 612 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 613 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 614 | packet_put_int(u_int value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 615 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 616 | buffer_put_int(&active_state->outgoing_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 617 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 618 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 619 | void |
Darren Tucker | 761c389 | 2009-06-21 18:16:26 +1000 | [diff] [blame] | 620 | packet_put_int64(u_int64_t value) |
| 621 | { |
| 622 | buffer_put_int64(&active_state->outgoing_packet, value); |
| 623 | } |
| 624 | |
| 625 | void |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 626 | packet_put_string(const void *buf, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 627 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 628 | buffer_put_string(&active_state->outgoing_packet, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 629 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 630 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 631 | void |
| 632 | packet_put_cstring(const char *str) |
| 633 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 634 | buffer_put_cstring(&active_state->outgoing_packet, str); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 635 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 636 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 637 | void |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 638 | packet_put_raw(const void *buf, u_int len) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 639 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 640 | buffer_append(&active_state->outgoing_packet, buf, len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 641 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 642 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 643 | #ifdef WITH_OPENSSL |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 644 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 645 | packet_put_bignum(BIGNUM * value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 646 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 647 | buffer_put_bignum(&active_state->outgoing_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 648 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 649 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 650 | void |
| 651 | packet_put_bignum2(BIGNUM * value) |
| 652 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 653 | buffer_put_bignum2(&active_state->outgoing_packet, value); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 654 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 655 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 656 | |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 657 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 658 | void |
| 659 | packet_put_ecpoint(const EC_GROUP *curve, const EC_POINT *point) |
| 660 | { |
| 661 | buffer_put_ecpoint(&active_state->outgoing_packet, curve, point); |
| 662 | } |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 663 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 664 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 665 | /* |
| 666 | * Finalizes and sends the packet. If the encryption key has been set, |
| 667 | * encrypts the packet before sending. |
| 668 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 669 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 670 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 671 | packet_send1(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 672 | { |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 673 | u_char buf[8], *cp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 674 | int i, padding, len; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 675 | u_int checksum; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 676 | u_int32_t rnd = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 677 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 678 | /* |
| 679 | * If using packet compression, compress the payload of the outgoing |
| 680 | * packet. |
| 681 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 682 | if (active_state->packet_compression) { |
| 683 | buffer_clear(&active_state->compression_buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 684 | /* Skip padding. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 685 | buffer_consume(&active_state->outgoing_packet, 8); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 686 | /* padding */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 687 | buffer_append(&active_state->compression_buffer, |
| 688 | "\0\0\0\0\0\0\0\0", 8); |
| 689 | buffer_compress(&active_state->outgoing_packet, |
| 690 | &active_state->compression_buffer); |
| 691 | buffer_clear(&active_state->outgoing_packet); |
| 692 | buffer_append(&active_state->outgoing_packet, |
| 693 | buffer_ptr(&active_state->compression_buffer), |
| 694 | buffer_len(&active_state->compression_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 695 | } |
| 696 | /* Compute packet length without padding (add checksum, remove padding). */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 697 | len = buffer_len(&active_state->outgoing_packet) + 4 - 8; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 698 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 699 | /* Insert padding. Initialized to zero in packet_start1() */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 700 | padding = 8 - len % 8; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 701 | if (!active_state->send_context.plaintext) { |
| 702 | cp = buffer_ptr(&active_state->outgoing_packet); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 703 | for (i = 0; i < padding; i++) { |
| 704 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 705 | rnd = arc4random(); |
| 706 | cp[7 - i] = rnd & 0xff; |
| 707 | rnd >>= 8; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 708 | } |
| 709 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 710 | buffer_consume(&active_state->outgoing_packet, 8 - padding); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 711 | |
| 712 | /* Add check bytes. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 713 | checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet), |
| 714 | buffer_len(&active_state->outgoing_packet)); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 715 | put_u32(buf, checksum); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 716 | buffer_append(&active_state->outgoing_packet, buf, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 717 | |
| 718 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 719 | fprintf(stderr, "packet_send plain: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 720 | buffer_dump(&active_state->outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 721 | #endif |
| 722 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 723 | /* Append to output. */ |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 724 | put_u32(buf, len); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 725 | buffer_append(&active_state->output, buf, 4); |
| 726 | cp = buffer_append_space(&active_state->output, |
| 727 | buffer_len(&active_state->outgoing_packet)); |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 728 | if (cipher_crypt(&active_state->send_context, 0, cp, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 729 | buffer_ptr(&active_state->outgoing_packet), |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 730 | buffer_len(&active_state->outgoing_packet), 0, 0) != 0) |
| 731 | fatal("%s: cipher_crypt failed", __func__); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 732 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 733 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 734 | fprintf(stderr, "encrypted: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 735 | buffer_dump(&active_state->output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 736 | #endif |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 737 | active_state->p_send.packets++; |
| 738 | active_state->p_send.bytes += len + |
| 739 | buffer_len(&active_state->outgoing_packet); |
| 740 | buffer_clear(&active_state->outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 741 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 742 | /* |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 743 | * Note that the packet is now only buffered in output. It won't be |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 744 | * actually sent until packet_write_wait or packet_write_poll is |
| 745 | * called. |
| 746 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 747 | } |
| 748 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 749 | void |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 750 | set_newkeys(int mode) |
| 751 | { |
| 752 | Enc *enc; |
| 753 | Mac *mac; |
| 754 | Comp *comp; |
| 755 | CipherContext *cc; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 756 | u_int64_t *max_blocks; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 757 | int r, crypt_type; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 758 | |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 759 | debug2("set_newkeys: mode %d", mode); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 760 | |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 761 | if (mode == MODE_OUT) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 762 | cc = &active_state->send_context; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 763 | crypt_type = CIPHER_ENCRYPT; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 764 | active_state->p_send.packets = active_state->p_send.blocks = 0; |
| 765 | max_blocks = &active_state->max_blocks_out; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 766 | } else { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 767 | cc = &active_state->receive_context; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 768 | crypt_type = CIPHER_DECRYPT; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 769 | active_state->p_read.packets = active_state->p_read.blocks = 0; |
| 770 | max_blocks = &active_state->max_blocks_in; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 771 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 772 | if (active_state->newkeys[mode] != NULL) { |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 773 | debug("set_newkeys: rekeying"); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 774 | cipher_cleanup(cc); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 775 | enc = &active_state->newkeys[mode]->enc; |
| 776 | mac = &active_state->newkeys[mode]->mac; |
| 777 | comp = &active_state->newkeys[mode]->comp; |
Damien Miller | e45796f | 2007-06-11 14:01:42 +1000 | [diff] [blame] | 778 | mac_clear(mac); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 779 | explicit_bzero(enc->iv, enc->iv_len); |
| 780 | explicit_bzero(enc->key, enc->key_len); |
| 781 | explicit_bzero(mac->key, mac->key_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 782 | free(enc->name); |
| 783 | free(enc->iv); |
| 784 | free(enc->key); |
| 785 | free(mac->name); |
| 786 | free(mac->key); |
| 787 | free(comp->name); |
| 788 | free(active_state->newkeys[mode]); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 789 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 790 | active_state->newkeys[mode] = kex_get_newkeys(mode); |
| 791 | if (active_state->newkeys[mode] == NULL) |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 792 | fatal("newkeys: no keys for mode %d", mode); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 793 | enc = &active_state->newkeys[mode]->enc; |
| 794 | mac = &active_state->newkeys[mode]->mac; |
| 795 | comp = &active_state->newkeys[mode]->comp; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 796 | if (cipher_authlen(enc->cipher) == 0 && mac_init(mac) == 0) |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 797 | mac->enabled = 1; |
| 798 | DBG(debug("cipher_init_context: %d", mode)); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 799 | if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len, |
| 800 | enc->iv, enc->iv_len, crypt_type)) != 0) |
| 801 | fatal("%s: cipher_init: %s", __func__, ssh_err(r)); |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 802 | /* Deleting the keys does not gain extra security */ |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 803 | /* explicit_bzero(enc->iv, enc->block_size); |
| 804 | explicit_bzero(enc->key, enc->key_len); |
| 805 | explicit_bzero(mac->key, mac->key_len); */ |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 806 | if ((comp->type == COMP_ZLIB || |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 807 | (comp->type == COMP_DELAYED && |
| 808 | active_state->after_authentication)) && comp->enabled == 0) { |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 809 | packet_init_compression(); |
| 810 | if (mode == MODE_OUT) |
| 811 | buffer_compress_init_send(6); |
| 812 | else |
| 813 | buffer_compress_init_recv(); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 814 | comp->enabled = 1; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 815 | } |
Darren Tucker | 81a0b37 | 2003-07-14 17:31:06 +1000 | [diff] [blame] | 816 | /* |
| 817 | * The 2^(blocksize*2) limit is too expensive for 3DES, |
| 818 | * blowfish, etc, so enforce a 1GB limit for small blocksizes. |
| 819 | */ |
| 820 | if (enc->block_size >= 16) |
| 821 | *max_blocks = (u_int64_t)1 << (enc->block_size*2); |
| 822 | else |
| 823 | *max_blocks = ((u_int64_t)1 << 30) / enc->block_size; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 824 | if (active_state->rekey_limit) |
| 825 | *max_blocks = MIN(*max_blocks, |
| 826 | active_state->rekey_limit / enc->block_size); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 829 | /* |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 830 | * Delayed compression for SSH2 is enabled after authentication: |
Darren Tucker | f676c57 | 2006-08-05 18:51:08 +1000 | [diff] [blame] | 831 | * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent, |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 832 | * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received. |
| 833 | */ |
| 834 | static void |
| 835 | packet_enable_delayed_compress(void) |
| 836 | { |
| 837 | Comp *comp = NULL; |
| 838 | int mode; |
| 839 | |
| 840 | /* |
| 841 | * Remember that we are past the authentication step, so rekeying |
| 842 | * with COMP_DELAYED will turn on compression immediately. |
| 843 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 844 | active_state->after_authentication = 1; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 845 | for (mode = 0; mode < MODE_MAX; mode++) { |
Darren Tucker | 4aa665b | 2006-09-21 13:00:25 +1000 | [diff] [blame] | 846 | /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 847 | if (active_state->newkeys[mode] == NULL) |
Darren Tucker | 4aa665b | 2006-09-21 13:00:25 +1000 | [diff] [blame] | 848 | continue; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 849 | comp = &active_state->newkeys[mode]->comp; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 850 | if (comp && !comp->enabled && comp->type == COMP_DELAYED) { |
Damien Miller | b5c0125 | 2005-08-12 22:10:28 +1000 | [diff] [blame] | 851 | packet_init_compression(); |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 852 | if (mode == MODE_OUT) |
| 853 | buffer_compress_init_send(6); |
| 854 | else |
| 855 | buffer_compress_init_recv(); |
| 856 | comp->enabled = 1; |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | /* |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 862 | * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue) |
| 863 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 864 | static void |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 865 | packet_send2_wrapped(void) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 866 | { |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 867 | u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH]; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 868 | u_char padlen, pad = 0; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 869 | u_int i, len, authlen = 0, aadlen = 0; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 870 | u_int32_t rnd = 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 871 | Enc *enc = NULL; |
| 872 | Mac *mac = NULL; |
| 873 | Comp *comp = NULL; |
| 874 | int block_size; |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 875 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 876 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 877 | if (active_state->newkeys[MODE_OUT] != NULL) { |
| 878 | enc = &active_state->newkeys[MODE_OUT]->enc; |
| 879 | mac = &active_state->newkeys[MODE_OUT]->mac; |
| 880 | comp = &active_state->newkeys[MODE_OUT]->comp; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 881 | /* disable mac for authenticated encryption */ |
| 882 | if ((authlen = cipher_authlen(enc->cipher)) != 0) |
| 883 | mac = NULL; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 884 | } |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 885 | block_size = enc ? enc->block_size : 8; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 886 | aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 887 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 888 | cp = buffer_ptr(&active_state->outgoing_packet); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 889 | type = cp[5]; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 890 | |
| 891 | #ifdef PACKET_DEBUG |
| 892 | fprintf(stderr, "plain: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 893 | buffer_dump(&active_state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 894 | #endif |
| 895 | |
| 896 | if (comp && comp->enabled) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 897 | len = buffer_len(&active_state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 898 | /* skip header, compress only payload */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 899 | buffer_consume(&active_state->outgoing_packet, 5); |
| 900 | buffer_clear(&active_state->compression_buffer); |
| 901 | buffer_compress(&active_state->outgoing_packet, |
| 902 | &active_state->compression_buffer); |
| 903 | buffer_clear(&active_state->outgoing_packet); |
| 904 | buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5); |
| 905 | buffer_append(&active_state->outgoing_packet, |
| 906 | buffer_ptr(&active_state->compression_buffer), |
| 907 | buffer_len(&active_state->compression_buffer)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 908 | DBG(debug("compression: raw %d compressed %d", len, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 909 | buffer_len(&active_state->outgoing_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /* sizeof (packet_len + pad_len + payload) */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 913 | len = buffer_len(&active_state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 914 | |
| 915 | /* |
| 916 | * calc size of padding, alloc space, get random data, |
| 917 | * minimum padding is 4 bytes |
| 918 | */ |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 919 | len -= aadlen; /* packet length is not encrypted for EtM modes */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 920 | padlen = block_size - (len % block_size); |
| 921 | if (padlen < 4) |
| 922 | padlen += block_size; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 923 | if (active_state->extra_pad) { |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 924 | /* will wrap if extra_pad+padlen > 255 */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 925 | active_state->extra_pad = |
| 926 | roundup(active_state->extra_pad, block_size); |
| 927 | pad = active_state->extra_pad - |
| 928 | ((len + padlen) % active_state->extra_pad); |
Damien Miller | 2a32843 | 2014-04-20 13:24:01 +1000 | [diff] [blame] | 929 | DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)", |
| 930 | __func__, pad, len, padlen, active_state->extra_pad)); |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 931 | padlen += pad; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 932 | active_state->extra_pad = 0; |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 933 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 934 | cp = buffer_append_space(&active_state->outgoing_packet, padlen); |
| 935 | if (enc && !active_state->send_context.plaintext) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 936 | /* random padding */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 937 | for (i = 0; i < padlen; i++) { |
| 938 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 939 | rnd = arc4random(); |
| 940 | cp[i] = rnd & 0xff; |
| 941 | rnd >>= 8; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 942 | } |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 943 | } else { |
| 944 | /* clear padding */ |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 945 | explicit_bzero(cp, padlen); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 946 | } |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 947 | /* sizeof (packet_len + pad_len + payload + padding) */ |
| 948 | len = buffer_len(&active_state->outgoing_packet); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 949 | cp = buffer_ptr(&active_state->outgoing_packet); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 950 | /* packet_length includes payload, padding and padding length field */ |
| 951 | put_u32(cp, len - 4); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 952 | cp[4] = padlen; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 953 | DBG(debug("send: len %d (includes padlen %d, aadlen %d)", |
| 954 | len, padlen, aadlen)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 955 | |
| 956 | /* compute MAC over seqnr and packet(length fields, payload, padding) */ |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 957 | if (mac && mac->enabled && !mac->etm) { |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 958 | if ((r = mac_compute(mac, active_state->p_send.seqnr, |
| 959 | buffer_ptr(&active_state->outgoing_packet), len, |
| 960 | macbuf, sizeof(macbuf))) != 0) |
| 961 | fatal("%s: mac_compute: %s", __func__, ssh_err(r)); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 962 | DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 963 | } |
| 964 | /* encrypt packet and append to output buffer. */ |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 965 | cp = buffer_append_space(&active_state->output, len + authlen); |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 966 | if (cipher_crypt(&active_state->send_context, active_state->p_send.seqnr, |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 967 | cp, buffer_ptr(&active_state->outgoing_packet), |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 968 | len - aadlen, aadlen, authlen) != 0) |
| 969 | fatal("%s: cipher_crypt failed", __func__); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 970 | /* append unencrypted MAC */ |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 971 | if (mac && mac->enabled) { |
| 972 | if (mac->etm) { |
| 973 | /* EtM: compute mac over aadlen + cipher text */ |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 974 | if ((r = mac_compute(mac, |
| 975 | active_state->p_send.seqnr, cp, len, |
| 976 | macbuf, sizeof(macbuf))) != 0) |
| 977 | fatal("%s: mac_compute: %s", __func__, ssh_err(r)); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 978 | DBG(debug("done calc MAC(EtM) out #%d", |
| 979 | active_state->p_send.seqnr)); |
| 980 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 981 | buffer_append(&active_state->output, macbuf, mac->mac_len); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 982 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 983 | #ifdef PACKET_DEBUG |
| 984 | fprintf(stderr, "encrypted: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 985 | buffer_dump(&active_state->output); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 986 | #endif |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 987 | /* increment sequence number for outgoing packets */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 988 | if (++active_state->p_send.seqnr == 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 989 | logit("outgoing seqnr wraps around"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 990 | if (++active_state->p_send.packets == 0) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 991 | if (!(datafellows & SSH_BUG_NOREKEY)) |
| 992 | fatal("XXX too many packets with same key"); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 993 | active_state->p_send.blocks += len / block_size; |
| 994 | active_state->p_send.bytes += len; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 995 | buffer_clear(&active_state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 996 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 997 | if (type == SSH2_MSG_NEWKEYS) |
| 998 | set_newkeys(MODE_OUT); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 999 | else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side) |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1000 | packet_enable_delayed_compress(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1001 | } |
| 1002 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1003 | static void |
| 1004 | packet_send2(void) |
| 1005 | { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1006 | struct packet *p; |
| 1007 | u_char type, *cp; |
| 1008 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1009 | cp = buffer_ptr(&active_state->outgoing_packet); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1010 | type = cp[5]; |
| 1011 | |
| 1012 | /* during rekeying we can only send key exchange messages */ |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1013 | if (active_state->rekeying) { |
Damien Miller | 1de2cfe | 2012-02-11 08:18:43 +1100 | [diff] [blame] | 1014 | if ((type < SSH2_MSG_TRANSPORT_MIN) || |
| 1015 | (type > SSH2_MSG_TRANSPORT_MAX) || |
| 1016 | (type == SSH2_MSG_SERVICE_REQUEST) || |
| 1017 | (type == SSH2_MSG_SERVICE_ACCEPT)) { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1018 | debug("enqueue packet: %u", type); |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 1019 | p = xcalloc(1, sizeof(*p)); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1020 | p->type = type; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1021 | memcpy(&p->payload, &active_state->outgoing_packet, |
| 1022 | sizeof(Buffer)); |
| 1023 | buffer_init(&active_state->outgoing_packet); |
| 1024 | TAILQ_INSERT_TAIL(&active_state->outgoing, p, next); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1025 | return; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /* rekeying starts with sending KEXINIT */ |
| 1030 | if (type == SSH2_MSG_KEXINIT) |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1031 | active_state->rekeying = 1; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1032 | |
| 1033 | packet_send2_wrapped(); |
| 1034 | |
| 1035 | /* after a NEWKEYS message we can send the complete queue */ |
| 1036 | if (type == SSH2_MSG_NEWKEYS) { |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1037 | active_state->rekeying = 0; |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1038 | active_state->rekey_time = monotime(); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1039 | while ((p = TAILQ_FIRST(&active_state->outgoing))) { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1040 | type = p->type; |
| 1041 | debug("dequeue packet: %u", type); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1042 | buffer_free(&active_state->outgoing_packet); |
| 1043 | memcpy(&active_state->outgoing_packet, &p->payload, |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1044 | sizeof(Buffer)); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1045 | TAILQ_REMOVE(&active_state->outgoing, p, next); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1046 | free(p); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1047 | packet_send2_wrapped(); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1052 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1053 | packet_send(void) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1054 | { |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1055 | if (compat20) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1056 | packet_send2(); |
| 1057 | else |
| 1058 | packet_send1(); |
| 1059 | DBG(debug("packet_send done")); |
| 1060 | } |
| 1061 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1062 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1063 | * Waits until a packet has been received, and returns its type. Note that |
| 1064 | * no other data is processed until this returns, so this function should not |
| 1065 | * be used during the interactive session. |
| 1066 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1067 | |
| 1068 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1069 | packet_read_seqnr(u_int32_t *seqnr_p) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1070 | { |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1071 | int type, len, ret, cont, ms_remain = 0; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1072 | fd_set *setp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1073 | char buf[8192]; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1074 | struct timeval timeout, start, *timeoutp = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1075 | |
Darren Tucker | 99bb761 | 2008-06-13 22:02:50 +1000 | [diff] [blame] | 1076 | DBG(debug("packet_read()")); |
| 1077 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1078 | setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1, |
| 1079 | NFDBITS), sizeof(fd_mask)); |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1080 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1081 | /* Since we are blocking, ensure that all written packets have been sent. */ |
| 1082 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1083 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1084 | /* Stay in the loop until we have received a complete packet. */ |
| 1085 | for (;;) { |
| 1086 | /* Try to read a packet from the buffer. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1087 | type = packet_read_poll_seqnr(seqnr_p); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1088 | if (!compat20 && ( |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1089 | type == SSH_SMSG_SUCCESS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1090 | || type == SSH_SMSG_FAILURE |
| 1091 | || type == SSH_CMSG_EOF |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1092 | || type == SSH_CMSG_EXIT_CONFIRMATION)) |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1093 | packet_check_eom(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1094 | /* If we got a packet, return it. */ |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1095 | if (type != SSH_MSG_NONE) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1096 | free(setp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1097 | return type; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1098 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1099 | /* |
| 1100 | * Otherwise, wait for some data to arrive, add it to the |
| 1101 | * buffer, and try again. |
| 1102 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1103 | memset(setp, 0, howmany(active_state->connection_in + 1, |
| 1104 | NFDBITS) * sizeof(fd_mask)); |
| 1105 | FD_SET(active_state->connection_in, setp); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1106 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1107 | if (active_state->packet_timeout_ms > 0) { |
| 1108 | ms_remain = active_state->packet_timeout_ms; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1109 | timeoutp = &timeout; |
| 1110 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1111 | /* Wait for some data to arrive. */ |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1112 | for (;;) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1113 | if (active_state->packet_timeout_ms != -1) { |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1114 | ms_to_timeval(&timeout, ms_remain); |
| 1115 | gettimeofday(&start, NULL); |
| 1116 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1117 | if ((ret = select(active_state->connection_in + 1, setp, |
| 1118 | NULL, NULL, timeoutp)) >= 0) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1119 | break; |
Damien Miller | ea43742 | 2009-10-02 11:49:03 +1000 | [diff] [blame] | 1120 | if (errno != EAGAIN && errno != EINTR && |
| 1121 | errno != EWOULDBLOCK) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1122 | break; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1123 | if (active_state->packet_timeout_ms == -1) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1124 | continue; |
| 1125 | ms_subtract_diff(&start, &ms_remain); |
| 1126 | if (ms_remain <= 0) { |
| 1127 | ret = 0; |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | if (ret == 0) { |
| 1132 | logit("Connection to %.200s timed out while " |
| 1133 | "waiting to read", get_remote_ipaddr()); |
| 1134 | cleanup_exit(255); |
| 1135 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1136 | /* Read data from the socket. */ |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1137 | do { |
| 1138 | cont = 0; |
| 1139 | len = roaming_read(active_state->connection_in, buf, |
| 1140 | sizeof(buf), &cont); |
| 1141 | } while (len == 0 && cont); |
Damien Miller | 5e7c10e | 1999-12-16 13:18:04 +1100 | [diff] [blame] | 1142 | if (len == 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1143 | logit("Connection closed by %.200s", get_remote_ipaddr()); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1144 | cleanup_exit(255); |
Damien Miller | 5e7c10e | 1999-12-16 13:18:04 +1100 | [diff] [blame] | 1145 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1146 | if (len < 0) |
| 1147 | fatal("Read from socket failed: %.100s", strerror(errno)); |
| 1148 | /* Append it to the buffer. */ |
| 1149 | packet_process_incoming(buf, len); |
| 1150 | } |
| 1151 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1152 | } |
| 1153 | |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1154 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1155 | packet_read(void) |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1156 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1157 | return packet_read_seqnr(NULL); |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1158 | } |
| 1159 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1160 | /* |
| 1161 | * Waits until a packet has been received, verifies that its type matches |
| 1162 | * that given, and gives a fatal error and exits if there is a mismatch. |
| 1163 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1164 | |
| 1165 | void |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1166 | packet_read_expect(int expected_type) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1167 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1168 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1169 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1170 | type = packet_read(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1171 | if (type != expected_type) |
| 1172 | packet_disconnect("Protocol error: expected packet type %d, got %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1173 | expected_type, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /* Checks if a full packet is available in the data received so far via |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1177 | * packet_process_incoming. If so, reads the packet; otherwise returns |
| 1178 | * SSH_MSG_NONE. This does not wait for data from the connection. |
| 1179 | * |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1180 | * SSH_MSG_DISCONNECT is handled specially here. Also, |
| 1181 | * SSH_MSG_IGNORE messages are skipped by this function and are never returned |
| 1182 | * to higher levels. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1183 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1184 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1185 | static int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1186 | packet_read_poll1(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1187 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1188 | u_int len, padded_len; |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1189 | u_char *cp, type; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1190 | u_int checksum, stored_checksum; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1191 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1192 | /* Check if input size is less than minimum packet size. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1193 | if (buffer_len(&active_state->input) < 4 + 8) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1194 | return SSH_MSG_NONE; |
| 1195 | /* Get length of incoming packet. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1196 | cp = buffer_ptr(&active_state->input); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1197 | len = get_u32(cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1198 | if (len < 1 + 2 + 2 || len > 256 * 1024) |
Ben Lindstrom | 0cc2a47 | 2002-11-09 15:41:39 +0000 | [diff] [blame] | 1199 | packet_disconnect("Bad packet length %u.", len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1200 | padded_len = (len + 8) & ~7; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1201 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1202 | /* Check if the packet has been entirely received. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1203 | if (buffer_len(&active_state->input) < 4 + padded_len) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1204 | return SSH_MSG_NONE; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1205 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1206 | /* The entire packet is in buffer. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1207 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1208 | /* Consume packet length. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1209 | buffer_consume(&active_state->input, 4); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1210 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1211 | /* |
| 1212 | * Cryptographic attack detector for ssh |
| 1213 | * (C)1998 CORE-SDI, Buenos Aires Argentina |
| 1214 | * Ariel Futoransky(futo@core-sdi.com) |
| 1215 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1216 | if (!active_state->receive_context.plaintext) { |
| 1217 | switch (detect_attack(buffer_ptr(&active_state->input), |
| 1218 | padded_len)) { |
Damien Miller | 3c9c1fb | 2006-09-17 06:08:53 +1000 | [diff] [blame] | 1219 | case DEATTACK_DETECTED: |
| 1220 | packet_disconnect("crc32 compensation attack: " |
| 1221 | "network attack detected"); |
| 1222 | case DEATTACK_DOS_DETECTED: |
| 1223 | packet_disconnect("deattack denial of " |
| 1224 | "service detected"); |
| 1225 | } |
| 1226 | } |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1227 | |
| 1228 | /* Decrypt data to incoming_packet. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1229 | buffer_clear(&active_state->incoming_packet); |
| 1230 | cp = buffer_append_space(&active_state->incoming_packet, padded_len); |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 1231 | if (cipher_crypt(&active_state->receive_context, 0, cp, |
| 1232 | buffer_ptr(&active_state->input), padded_len, 0, 0) != 0) |
| 1233 | fatal("%s: cipher_crypt failed", __func__); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1234 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1235 | buffer_consume(&active_state->input, padded_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1236 | |
| 1237 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1238 | fprintf(stderr, "read_poll plain: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1239 | buffer_dump(&active_state->incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1240 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1241 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1242 | /* Compute packet checksum. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1243 | checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet), |
| 1244 | buffer_len(&active_state->incoming_packet) - 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1245 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1246 | /* Skip padding. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1247 | buffer_consume(&active_state->incoming_packet, 8 - len % 8); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 1248 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1249 | /* Test check bytes. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1250 | if (len != buffer_len(&active_state->incoming_packet)) |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1251 | packet_disconnect("packet_read_poll1: len %d != buffer_len %d.", |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1252 | len, buffer_len(&active_state->incoming_packet)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1253 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1254 | cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1255 | stored_checksum = get_u32(cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1256 | if (checksum != stored_checksum) |
| 1257 | packet_disconnect("Corrupted check bytes on input."); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1258 | buffer_consume_end(&active_state->incoming_packet, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1259 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1260 | if (active_state->packet_compression) { |
| 1261 | buffer_clear(&active_state->compression_buffer); |
| 1262 | buffer_uncompress(&active_state->incoming_packet, |
| 1263 | &active_state->compression_buffer); |
| 1264 | buffer_clear(&active_state->incoming_packet); |
| 1265 | buffer_append(&active_state->incoming_packet, |
| 1266 | buffer_ptr(&active_state->compression_buffer), |
| 1267 | buffer_len(&active_state->compression_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1268 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1269 | active_state->p_read.packets++; |
| 1270 | active_state->p_read.bytes += padded_len + 4; |
| 1271 | type = buffer_get_char(&active_state->incoming_packet); |
Darren Tucker | b2694f0 | 2004-11-05 20:27:54 +1100 | [diff] [blame] | 1272 | if (type < SSH_MSG_MIN || type > SSH_MSG_MAX) |
| 1273 | packet_disconnect("Invalid ssh1 packet type: %d", type); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1274 | return type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1275 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1276 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1277 | static int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1278 | packet_read_poll2(u_int32_t *seqnr_p) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1279 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1280 | u_int padlen, need; |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1281 | u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH]; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1282 | u_int maclen, authlen = 0, aadlen = 0, block_size; |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1283 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1284 | Enc *enc = NULL; |
| 1285 | Mac *mac = NULL; |
| 1286 | Comp *comp = NULL; |
| 1287 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1288 | if (active_state->packet_discard) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1289 | return SSH_MSG_NONE; |
| 1290 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1291 | if (active_state->newkeys[MODE_IN] != NULL) { |
| 1292 | enc = &active_state->newkeys[MODE_IN]->enc; |
| 1293 | mac = &active_state->newkeys[MODE_IN]->mac; |
| 1294 | comp = &active_state->newkeys[MODE_IN]->comp; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1295 | /* disable mac for authenticated encryption */ |
| 1296 | if ((authlen = cipher_authlen(enc->cipher)) != 0) |
| 1297 | mac = NULL; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1298 | } |
| 1299 | maclen = mac && mac->enabled ? mac->mac_len : 0; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 1300 | block_size = enc ? enc->block_size : 8; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1301 | aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1302 | |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1303 | if (aadlen && active_state->packlen == 0) { |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 1304 | if (cipher_get_length(&active_state->receive_context, |
| 1305 | &active_state->packlen, |
| 1306 | active_state->p_read.seqnr, |
| 1307 | buffer_ptr(&active_state->input), |
| 1308 | buffer_len(&active_state->input)) != 0) |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1309 | return SSH_MSG_NONE; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1310 | if (active_state->packlen < 1 + 4 || |
| 1311 | active_state->packlen > PACKET_MAX_SIZE) { |
| 1312 | #ifdef PACKET_DEBUG |
| 1313 | buffer_dump(&active_state->input); |
| 1314 | #endif |
| 1315 | logit("Bad packet length %u.", active_state->packlen); |
| 1316 | packet_disconnect("Packet corrupt"); |
| 1317 | } |
Damien Miller | 8c05da3 | 2012-12-13 07:18:59 +1100 | [diff] [blame] | 1318 | buffer_clear(&active_state->incoming_packet); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1319 | } else if (active_state->packlen == 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1320 | /* |
| 1321 | * check if input size is less than the cipher block size, |
| 1322 | * decrypt first block and extract length of incoming packet |
| 1323 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1324 | if (buffer_len(&active_state->input) < block_size) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1325 | return SSH_MSG_NONE; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1326 | buffer_clear(&active_state->incoming_packet); |
| 1327 | cp = buffer_append_space(&active_state->incoming_packet, |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1328 | block_size); |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 1329 | if (cipher_crypt(&active_state->receive_context, |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 1330 | active_state->p_read.seqnr, cp, |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 1331 | buffer_ptr(&active_state->input), block_size, 0, 0) != 0) |
| 1332 | fatal("Decryption integrity check failed"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1333 | cp = buffer_ptr(&active_state->incoming_packet); |
| 1334 | active_state->packlen = get_u32(cp); |
| 1335 | if (active_state->packlen < 1 + 4 || |
| 1336 | active_state->packlen > PACKET_MAX_SIZE) { |
Darren Tucker | a8151da | 2003-09-22 21:06:46 +1000 | [diff] [blame] | 1337 | #ifdef PACKET_DEBUG |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1338 | buffer_dump(&active_state->incoming_packet); |
Darren Tucker | a8151da | 2003-09-22 21:06:46 +1000 | [diff] [blame] | 1339 | #endif |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1340 | logit("Bad packet length %u.", active_state->packlen); |
| 1341 | packet_start_discard(enc, mac, active_state->packlen, |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1342 | PACKET_MAX_SIZE); |
| 1343 | return SSH_MSG_NONE; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1344 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1345 | buffer_consume(&active_state->input, block_size); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1346 | } |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1347 | DBG(debug("input: packet len %u", active_state->packlen+4)); |
| 1348 | if (aadlen) { |
| 1349 | /* only the payload is encrypted */ |
| 1350 | need = active_state->packlen; |
| 1351 | } else { |
| 1352 | /* |
| 1353 | * the payload size and the payload are encrypted, but we |
| 1354 | * have a partial packet of block_size bytes |
| 1355 | */ |
| 1356 | need = 4 + active_state->packlen - block_size; |
| 1357 | } |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1358 | DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d," |
| 1359 | " aadlen %d", block_size, need, maclen, authlen, aadlen)); |
Darren Tucker | 99d11a3 | 2008-12-01 21:40:48 +1100 | [diff] [blame] | 1360 | if (need % block_size != 0) { |
| 1361 | logit("padding error: need %d block %d mod %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1362 | need, block_size, need % block_size); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1363 | packet_start_discard(enc, mac, active_state->packlen, |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1364 | PACKET_MAX_SIZE - block_size); |
| 1365 | return SSH_MSG_NONE; |
Darren Tucker | 99d11a3 | 2008-12-01 21:40:48 +1100 | [diff] [blame] | 1366 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1367 | /* |
| 1368 | * check if the entire packet has been received and |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1369 | * decrypt into incoming_packet: |
| 1370 | * 'aadlen' bytes are unencrypted, but authenticated. |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1371 | * 'need' bytes are encrypted, followed by either |
| 1372 | * 'authlen' bytes of authentication tag or |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1373 | * 'maclen' bytes of message authentication code. |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1374 | */ |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1375 | if (buffer_len(&active_state->input) < aadlen + need + authlen + maclen) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1376 | return SSH_MSG_NONE; |
| 1377 | #ifdef PACKET_DEBUG |
| 1378 | fprintf(stderr, "read_poll enc/full: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1379 | buffer_dump(&active_state->input); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1380 | #endif |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1381 | /* EtM: compute mac over encrypted input */ |
| 1382 | if (mac && mac->enabled && mac->etm) |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1383 | if ((r = mac_compute(mac, active_state->p_read.seqnr, |
| 1384 | buffer_ptr(&active_state->input), aadlen + need, |
| 1385 | macbuf, sizeof(macbuf))) != 0) |
| 1386 | fatal("%s: mac_compute: %s", __func__, ssh_err(r)); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1387 | cp = buffer_append_space(&active_state->incoming_packet, aadlen + need); |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 1388 | if (cipher_crypt(&active_state->receive_context, |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 1389 | active_state->p_read.seqnr, cp, |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 1390 | buffer_ptr(&active_state->input), need, aadlen, authlen) != 0) |
| 1391 | fatal("Decryption integrity check failed"); |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1392 | buffer_consume(&active_state->input, aadlen + need + authlen); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1393 | /* |
| 1394 | * compute MAC over seqnr and packet, |
| 1395 | * increment sequence number for incoming packet |
| 1396 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1397 | if (mac && mac->enabled) { |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1398 | if (!mac->etm) |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1399 | if ((r = mac_compute(mac, active_state->p_read.seqnr, |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1400 | buffer_ptr(&active_state->incoming_packet), |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1401 | buffer_len(&active_state->incoming_packet), |
| 1402 | macbuf, sizeof(macbuf))) != 0) |
| 1403 | fatal("%s: mac_compute: %s", __func__, ssh_err(r)); |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1404 | if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input), |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1405 | mac->mac_len) != 0) { |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1406 | logit("Corrupted MAC on input."); |
| 1407 | if (need > PACKET_MAX_SIZE) |
| 1408 | fatal("internal error need %d", need); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1409 | packet_start_discard(enc, mac, active_state->packlen, |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1410 | PACKET_MAX_SIZE - need); |
| 1411 | return SSH_MSG_NONE; |
| 1412 | } |
| 1413 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1414 | DBG(debug("MAC #%d ok", active_state->p_read.seqnr)); |
| 1415 | buffer_consume(&active_state->input, mac->mac_len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1416 | } |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1417 | /* XXX now it's safe to use fatal/packet_disconnect */ |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1418 | if (seqnr_p != NULL) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1419 | *seqnr_p = active_state->p_read.seqnr; |
| 1420 | if (++active_state->p_read.seqnr == 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1421 | logit("incoming seqnr wraps around"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1422 | if (++active_state->p_read.packets == 0) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1423 | if (!(datafellows & SSH_BUG_NOREKEY)) |
| 1424 | fatal("XXX too many packets with same key"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1425 | active_state->p_read.blocks += (active_state->packlen + 4) / block_size; |
| 1426 | active_state->p_read.bytes += active_state->packlen + 4; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1427 | |
| 1428 | /* get padlen */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1429 | cp = buffer_ptr(&active_state->incoming_packet); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1430 | padlen = cp[4]; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1431 | DBG(debug("input: padlen %d", padlen)); |
| 1432 | if (padlen < 4) |
| 1433 | packet_disconnect("Corrupted padlen %d on input.", padlen); |
| 1434 | |
| 1435 | /* skip packet size + padlen, discard padding */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1436 | buffer_consume(&active_state->incoming_packet, 4 + 1); |
| 1437 | buffer_consume_end(&active_state->incoming_packet, padlen); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1438 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1439 | DBG(debug("input: len before de-compress %d", |
| 1440 | buffer_len(&active_state->incoming_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1441 | if (comp && comp->enabled) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1442 | buffer_clear(&active_state->compression_buffer); |
| 1443 | buffer_uncompress(&active_state->incoming_packet, |
| 1444 | &active_state->compression_buffer); |
| 1445 | buffer_clear(&active_state->incoming_packet); |
| 1446 | buffer_append(&active_state->incoming_packet, |
| 1447 | buffer_ptr(&active_state->compression_buffer), |
| 1448 | buffer_len(&active_state->compression_buffer)); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1449 | DBG(debug("input: len after de-compress %d", |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1450 | buffer_len(&active_state->incoming_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1451 | } |
| 1452 | /* |
| 1453 | * get packet type, implies consume. |
| 1454 | * return length of payload (without type field) |
| 1455 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1456 | type = buffer_get_char(&active_state->incoming_packet); |
Darren Tucker | b2694f0 | 2004-11-05 20:27:54 +1100 | [diff] [blame] | 1457 | if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN) |
| 1458 | packet_disconnect("Invalid ssh2 packet type: %d", type); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 1459 | if (type == SSH2_MSG_NEWKEYS) |
| 1460 | set_newkeys(MODE_IN); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1461 | else if (type == SSH2_MSG_USERAUTH_SUCCESS && |
| 1462 | !active_state->server_side) |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1463 | packet_enable_delayed_compress(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1464 | #ifdef PACKET_DEBUG |
Ben Lindstrom | 204e488 | 2001-03-05 06:47:00 +0000 | [diff] [blame] | 1465 | fprintf(stderr, "read/plain[%d]:\r\n", type); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1466 | buffer_dump(&active_state->incoming_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1467 | #endif |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1468 | /* reset for next packet */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1469 | active_state->packlen = 0; |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1470 | return type; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1474 | packet_read_poll_seqnr(u_int32_t *seqnr_p) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1475 | { |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1476 | u_int reason, seqnr; |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1477 | u_char type; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1478 | char *msg; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1479 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1480 | for (;;) { |
| 1481 | if (compat20) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1482 | type = packet_read_poll2(seqnr_p); |
Darren Tucker | 136e56f | 2008-06-08 12:49:30 +1000 | [diff] [blame] | 1483 | if (type) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1484 | active_state->keep_alive_timeouts = 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1485 | DBG(debug("received packet type %d", type)); |
Darren Tucker | 136e56f | 2008-06-08 12:49:30 +1000 | [diff] [blame] | 1486 | } |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1487 | switch (type) { |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1488 | case SSH2_MSG_IGNORE: |
Damien Miller | 58226f6 | 2008-03-07 18:33:30 +1100 | [diff] [blame] | 1489 | debug3("Received SSH2_MSG_IGNORE"); |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1490 | break; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1491 | case SSH2_MSG_DEBUG: |
| 1492 | packet_get_char(); |
| 1493 | msg = packet_get_string(NULL); |
| 1494 | debug("Remote: %.900s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1495 | free(msg); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1496 | msg = packet_get_string(NULL); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1497 | free(msg); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1498 | break; |
| 1499 | case SSH2_MSG_DISCONNECT: |
| 1500 | reason = packet_get_int(); |
| 1501 | msg = packet_get_string(NULL); |
Damien Miller | d5edefd | 2013-04-23 15:21:39 +1000 | [diff] [blame] | 1502 | /* Ignore normal client exit notifications */ |
| 1503 | do_log2(active_state->server_side && |
| 1504 | reason == SSH2_DISCONNECT_BY_APPLICATION ? |
| 1505 | SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR, |
| 1506 | "Received disconnect from %s: %u: %.400s", |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1507 | get_remote_ipaddr(), reason, msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1508 | free(msg); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1509 | cleanup_exit(255); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1510 | break; |
Damien Miller | 659811f | 2002-01-22 23:23:11 +1100 | [diff] [blame] | 1511 | case SSH2_MSG_UNIMPLEMENTED: |
| 1512 | seqnr = packet_get_int(); |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1513 | debug("Received SSH2_MSG_UNIMPLEMENTED for %u", |
| 1514 | seqnr); |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1515 | break; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1516 | default: |
| 1517 | return type; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1518 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1519 | } else { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1520 | type = packet_read_poll1(); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1521 | switch (type) { |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1522 | case SSH_MSG_NONE: |
| 1523 | return SSH_MSG_NONE; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1524 | case SSH_MSG_IGNORE: |
| 1525 | break; |
| 1526 | case SSH_MSG_DEBUG: |
| 1527 | msg = packet_get_string(NULL); |
| 1528 | debug("Remote: %.900s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1529 | free(msg); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1530 | break; |
| 1531 | case SSH_MSG_DISCONNECT: |
| 1532 | msg = packet_get_string(NULL); |
Damien Miller | 894926e | 2013-02-12 11:03:58 +1100 | [diff] [blame] | 1533 | error("Received disconnect from %s: %.400s", |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1534 | get_remote_ipaddr(), msg); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1535 | cleanup_exit(255); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1536 | break; |
| 1537 | default: |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1538 | DBG(debug("received packet type %d", type)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1539 | return type; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1540 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1545 | /* |
| 1546 | * Buffers the given amount of input characters. This is intended to be used |
| 1547 | * together with packet_read_poll. |
| 1548 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1549 | |
| 1550 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1551 | packet_process_incoming(const char *buf, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1552 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1553 | if (active_state->packet_discard) { |
| 1554 | active_state->keep_alive_timeouts = 0; /* ?? */ |
| 1555 | if (len >= active_state->packet_discard) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1556 | packet_stop_discard(); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1557 | active_state->packet_discard -= len; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1558 | return; |
| 1559 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1560 | buffer_append(&active_state->input, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | /* Returns a character from the packet. */ |
| 1564 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1565 | u_int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1566 | packet_get_char(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1567 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1568 | char ch; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1569 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1570 | buffer_get(&active_state->incoming_packet, &ch, 1); |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1571 | return (u_char) ch; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /* Returns an integer from the packet data. */ |
| 1575 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1576 | u_int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1577 | packet_get_int(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1578 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1579 | return buffer_get_int(&active_state->incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1580 | } |
| 1581 | |
Darren Tucker | 761c389 | 2009-06-21 18:16:26 +1000 | [diff] [blame] | 1582 | /* Returns an 64 bit integer from the packet data. */ |
| 1583 | |
| 1584 | u_int64_t |
| 1585 | packet_get_int64(void) |
| 1586 | { |
| 1587 | return buffer_get_int64(&active_state->incoming_packet); |
| 1588 | } |
| 1589 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1590 | /* |
| 1591 | * Returns an arbitrary precision integer from the packet data. The integer |
| 1592 | * must have been initialized before this call. |
| 1593 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1594 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1595 | #ifdef WITH_OPENSSL |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1596 | void |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 1597 | packet_get_bignum(BIGNUM * value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1598 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1599 | buffer_get_bignum(&active_state->incoming_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1600 | } |
| 1601 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1602 | void |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 1603 | packet_get_bignum2(BIGNUM * value) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1604 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1605 | buffer_get_bignum2(&active_state->incoming_packet, value); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1606 | } |
| 1607 | |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1608 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1609 | void |
| 1610 | packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point) |
| 1611 | { |
| 1612 | buffer_get_ecpoint(&active_state->incoming_packet, curve, point); |
| 1613 | } |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1614 | #endif |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 1615 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1616 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1617 | void * |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1618 | packet_get_raw(u_int *length_ptr) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1619 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1620 | u_int bytes = buffer_len(&active_state->incoming_packet); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1621 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1622 | if (length_ptr != NULL) |
| 1623 | *length_ptr = bytes; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1624 | return buffer_ptr(&active_state->incoming_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1625 | } |
| 1626 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1627 | int |
| 1628 | packet_remaining(void) |
| 1629 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1630 | return buffer_len(&active_state->incoming_packet); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1631 | } |
| 1632 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1633 | /* |
| 1634 | * Returns a string from the packet data. The string is allocated using |
| 1635 | * xmalloc; it is the responsibility of the calling program to free it when |
| 1636 | * no longer needed. The length_ptr argument may be NULL, or point to an |
| 1637 | * integer into which the length of the string is stored. |
| 1638 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1639 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1640 | void * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1641 | packet_get_string(u_int *length_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1642 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1643 | return buffer_get_string(&active_state->incoming_packet, length_ptr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1644 | } |
| 1645 | |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 1646 | const void * |
Damien Miller | db255ca | 2008-05-19 14:59:37 +1000 | [diff] [blame] | 1647 | packet_get_string_ptr(u_int *length_ptr) |
| 1648 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1649 | return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr); |
Damien Miller | db255ca | 2008-05-19 14:59:37 +1000 | [diff] [blame] | 1650 | } |
| 1651 | |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 1652 | /* Ensures the returned string has no embedded \0 characters in it. */ |
| 1653 | char * |
| 1654 | packet_get_cstring(u_int *length_ptr) |
| 1655 | { |
| 1656 | return buffer_get_cstring(&active_state->incoming_packet, length_ptr); |
| 1657 | } |
| 1658 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1659 | /* |
| 1660 | * Sends a diagnostic message from the server to the client. This message |
| 1661 | * can be sent at any time (but not while constructing another message). The |
| 1662 | * message is printed immediately, but only if the client is being executed |
| 1663 | * in verbose mode. These messages are primarily intended to ease debugging |
| 1664 | * authentication problems. The length of the formatted message must not |
| 1665 | * exceed 1024 bytes. This will automatically call packet_write_wait. |
| 1666 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1667 | |
| 1668 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1669 | packet_send_debug(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1670 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1671 | char buf[1024]; |
| 1672 | va_list args; |
| 1673 | |
Ben Lindstrom | a14ee47 | 2000-12-07 01:24:58 +0000 | [diff] [blame] | 1674 | if (compat20 && (datafellows & SSH_BUG_DEBUG)) |
| 1675 | return; |
| 1676 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1677 | va_start(args, fmt); |
| 1678 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 1679 | va_end(args); |
| 1680 | |
Damien Miller | 7c8af4f | 2000-05-01 08:24:07 +1000 | [diff] [blame] | 1681 | if (compat20) { |
| 1682 | packet_start(SSH2_MSG_DEBUG); |
| 1683 | packet_put_char(0); /* bool: always display */ |
| 1684 | packet_put_cstring(buf); |
| 1685 | packet_put_cstring(""); |
| 1686 | } else { |
| 1687 | packet_start(SSH_MSG_DEBUG); |
| 1688 | packet_put_cstring(buf); |
| 1689 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1690 | packet_send(); |
| 1691 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1692 | } |
| 1693 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1694 | /* |
| 1695 | * Logs the error plus constructs and sends a disconnect packet, closes the |
| 1696 | * connection, and exits. This function never returns. The error message |
| 1697 | * should not contain a newline. The length of the formatted message must |
| 1698 | * not exceed 1024 bytes. |
| 1699 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1700 | |
| 1701 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1702 | packet_disconnect(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1703 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1704 | char buf[1024]; |
| 1705 | va_list args; |
| 1706 | static int disconnecting = 0; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1707 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1708 | if (disconnecting) /* Guard against recursive invocations. */ |
| 1709 | fatal("packet_disconnect called recursively."); |
| 1710 | disconnecting = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1711 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1712 | /* |
| 1713 | * Format the message. Note that the caller must make sure the |
| 1714 | * message is of limited size. |
| 1715 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1716 | va_start(args, fmt); |
| 1717 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 1718 | va_end(args); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1719 | |
Ben Lindstrom | 9bda7ae | 2002-11-09 15:46:24 +0000 | [diff] [blame] | 1720 | /* Display the error locally */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1721 | logit("Disconnecting: %.100s", buf); |
Ben Lindstrom | 9bda7ae | 2002-11-09 15:46:24 +0000 | [diff] [blame] | 1722 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1723 | /* Send the disconnect message to the other side, and wait for it to get sent. */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1724 | if (compat20) { |
| 1725 | packet_start(SSH2_MSG_DISCONNECT); |
| 1726 | packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR); |
| 1727 | packet_put_cstring(buf); |
| 1728 | packet_put_cstring(""); |
| 1729 | } else { |
| 1730 | packet_start(SSH_MSG_DISCONNECT); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 1731 | packet_put_cstring(buf); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1732 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1733 | packet_send(); |
| 1734 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1735 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1736 | /* Stop listening for connections. */ |
Ben Lindstrom | 601e436 | 2001-06-21 03:19:23 +0000 | [diff] [blame] | 1737 | channel_close_all(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1738 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1739 | /* Close the connection. */ |
| 1740 | packet_close(); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1741 | cleanup_exit(255); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1742 | } |
| 1743 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1744 | /* Checks if there is any buffered output, and tries to write some of the output. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1745 | |
| 1746 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1747 | packet_write_poll(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1748 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1749 | int len = buffer_len(&active_state->output); |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1750 | int cont; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1751 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1752 | if (len > 0) { |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1753 | cont = 0; |
| 1754 | len = roaming_write(active_state->connection_out, |
| 1755 | buffer_ptr(&active_state->output), len, &cont); |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1756 | if (len == -1) { |
Damien Miller | ea43742 | 2009-10-02 11:49:03 +1000 | [diff] [blame] | 1757 | if (errno == EINTR || errno == EAGAIN || |
| 1758 | errno == EWOULDBLOCK) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1759 | return; |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1760 | fatal("Write failed: %.100s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1761 | } |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1762 | if (len == 0 && !cont) |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1763 | fatal("Write connection closed"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1764 | buffer_consume(&active_state->output, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1765 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1766 | } |
| 1767 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1768 | /* |
| 1769 | * Calls packet_write_poll repeatedly until all pending output data has been |
| 1770 | * written. |
| 1771 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1772 | |
| 1773 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1774 | packet_write_wait(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1775 | { |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1776 | fd_set *setp; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1777 | int ret, ms_remain = 0; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1778 | struct timeval start, timeout, *timeoutp = NULL; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1779 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1780 | setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1, |
| 1781 | NFDBITS), sizeof(fd_mask)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1782 | packet_write_poll(); |
| 1783 | while (packet_have_data_to_write()) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1784 | memset(setp, 0, howmany(active_state->connection_out + 1, |
| 1785 | NFDBITS) * sizeof(fd_mask)); |
| 1786 | FD_SET(active_state->connection_out, setp); |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1787 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1788 | if (active_state->packet_timeout_ms > 0) { |
| 1789 | ms_remain = active_state->packet_timeout_ms; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1790 | timeoutp = &timeout; |
| 1791 | } |
| 1792 | for (;;) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1793 | if (active_state->packet_timeout_ms != -1) { |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1794 | ms_to_timeval(&timeout, ms_remain); |
| 1795 | gettimeofday(&start, NULL); |
| 1796 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1797 | if ((ret = select(active_state->connection_out + 1, |
| 1798 | NULL, setp, NULL, timeoutp)) >= 0) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1799 | break; |
Damien Miller | ea43742 | 2009-10-02 11:49:03 +1000 | [diff] [blame] | 1800 | if (errno != EAGAIN && errno != EINTR && |
| 1801 | errno != EWOULDBLOCK) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1802 | break; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1803 | if (active_state->packet_timeout_ms == -1) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1804 | continue; |
| 1805 | ms_subtract_diff(&start, &ms_remain); |
| 1806 | if (ms_remain <= 0) { |
| 1807 | ret = 0; |
| 1808 | break; |
| 1809 | } |
| 1810 | } |
| 1811 | if (ret == 0) { |
| 1812 | logit("Connection to %.200s timed out while " |
| 1813 | "waiting to write", get_remote_ipaddr()); |
| 1814 | cleanup_exit(255); |
| 1815 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1816 | packet_write_poll(); |
| 1817 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1818 | free(setp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | /* Returns true if there is buffered data to write to the connection. */ |
| 1822 | |
| 1823 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1824 | packet_have_data_to_write(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1825 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1826 | return buffer_len(&active_state->output) != 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | /* Returns true if there is not too much data to write to the connection. */ |
| 1830 | |
| 1831 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1832 | packet_not_very_much_data_to_write(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1833 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1834 | if (active_state->interactive_mode) |
| 1835 | return buffer_len(&active_state->output) < 16384; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1836 | else |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1837 | return buffer_len(&active_state->output) < 128 * 1024; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1838 | } |
| 1839 | |
Ben Lindstrom | faa1ea8 | 2002-12-23 02:42:52 +0000 | [diff] [blame] | 1840 | static void |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1841 | packet_set_tos(int tos) |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1842 | { |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 1843 | #ifndef IP_TOS_IS_BROKEN |
| 1844 | if (!packet_connection_is_on_socket()) |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1845 | return; |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 1846 | switch (packet_connection_af()) { |
| 1847 | # ifdef IP_TOS |
| 1848 | case AF_INET: |
| 1849 | debug3("%s: set IP_TOS 0x%02x", __func__, tos); |
| 1850 | if (setsockopt(active_state->connection_in, |
| 1851 | IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) |
| 1852 | error("setsockopt IP_TOS %d: %.100s:", |
| 1853 | tos, strerror(errno)); |
| 1854 | break; |
| 1855 | # endif /* IP_TOS */ |
| 1856 | # ifdef IPV6_TCLASS |
| 1857 | case AF_INET6: |
| 1858 | debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos); |
| 1859 | if (setsockopt(active_state->connection_in, |
| 1860 | IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0) |
| 1861 | error("setsockopt IPV6_TCLASS %d: %.100s:", |
| 1862 | tos, strerror(errno)); |
| 1863 | break; |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 1864 | # endif /* IPV6_TCLASS */ |
Damien Miller | 23f425b | 2011-05-15 08:58:15 +1000 | [diff] [blame] | 1865 | } |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 1866 | #endif /* IP_TOS_IS_BROKEN */ |
Damien Miller | 5924ceb | 2003-11-22 15:02:42 +1100 | [diff] [blame] | 1867 | } |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1868 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1869 | /* Informs that the current session is interactive. Sets IP flags for that. */ |
| 1870 | |
| 1871 | void |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1872 | packet_set_interactive(int interactive, int qos_interactive, int qos_bulk) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1873 | { |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1874 | if (active_state->set_interactive_called) |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1875 | return; |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1876 | active_state->set_interactive_called = 1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1877 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1878 | /* Record that we are in interactive mode. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1879 | active_state->interactive_mode = interactive; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1880 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1881 | /* Only set socket options if using a socket. */ |
| 1882 | if (!packet_connection_is_on_socket()) |
Ben Lindstrom | 93b6b77 | 2003-04-27 17:55:33 +0000 | [diff] [blame] | 1883 | return; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1884 | set_nodelay(active_state->connection_in); |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1885 | packet_set_tos(interactive ? qos_interactive : qos_bulk); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | /* Returns true if the current connection is interactive. */ |
| 1889 | |
| 1890 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1891 | packet_is_interactive(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1892 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1893 | return active_state->interactive_mode; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1894 | } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1895 | |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 1896 | int |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 1897 | packet_set_maxsize(u_int s) |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1898 | { |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1899 | if (active_state->set_maxsize_called) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1900 | logit("packet_set_maxsize: called twice: old %d new %d", |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1901 | active_state->max_packet_size, s); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1902 | return -1; |
| 1903 | } |
| 1904 | if (s < 4 * 1024 || s > 1024 * 1024) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1905 | logit("packet_set_maxsize: bad size %d", s); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1906 | return -1; |
| 1907 | } |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1908 | active_state->set_maxsize_called = 1; |
Ben Lindstrom | 16d45b3 | 2001-06-13 04:39:18 +0000 | [diff] [blame] | 1909 | debug("packet_set_maxsize: setting to %d", s); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1910 | active_state->max_packet_size = s; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1911 | return s; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1912 | } |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1913 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1914 | int |
| 1915 | packet_inc_alive_timeouts(void) |
| 1916 | { |
| 1917 | return ++active_state->keep_alive_timeouts; |
| 1918 | } |
| 1919 | |
| 1920 | void |
| 1921 | packet_set_alive_timeouts(int ka) |
| 1922 | { |
| 1923 | active_state->keep_alive_timeouts = ka; |
| 1924 | } |
| 1925 | |
| 1926 | u_int |
| 1927 | packet_get_maxsize(void) |
| 1928 | { |
| 1929 | return active_state->max_packet_size; |
| 1930 | } |
| 1931 | |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1932 | /* roundup current message to pad bytes */ |
| 1933 | void |
| 1934 | packet_add_padding(u_char pad) |
| 1935 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1936 | active_state->extra_pad = pad; |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1937 | } |
| 1938 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1939 | /* |
| 1940 | * 9.2. Ignored Data Message |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1941 | * |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1942 | * byte SSH_MSG_IGNORE |
| 1943 | * string data |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1944 | * |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1945 | * All implementations MUST understand (and ignore) this message at any |
| 1946 | * time (after receiving the protocol version). No implementation is |
| 1947 | * required to send them. This message can be used as an additional |
| 1948 | * protection measure against advanced traffic analysis techniques. |
| 1949 | */ |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1950 | void |
| 1951 | packet_send_ignore(int nbytes) |
| 1952 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1953 | u_int32_t rnd = 0; |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1954 | int i; |
| 1955 | |
| 1956 | packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE); |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1957 | packet_put_int(nbytes); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1958 | for (i = 0; i < nbytes; i++) { |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1959 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1960 | rnd = arc4random(); |
Damien Miller | 8ba29fe | 2006-03-26 14:25:19 +1100 | [diff] [blame] | 1961 | packet_put_char((u_char)rnd & 0xff); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1962 | rnd >>= 8; |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1965 | |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 1966 | #define MAX_PACKETS (1U<<31) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1967 | int |
| 1968 | packet_need_rekeying(void) |
| 1969 | { |
| 1970 | if (datafellows & SSH_BUG_NOREKEY) |
| 1971 | return 0; |
| 1972 | return |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1973 | (active_state->p_send.packets > MAX_PACKETS) || |
| 1974 | (active_state->p_read.packets > MAX_PACKETS) || |
| 1975 | (active_state->max_blocks_out && |
| 1976 | (active_state->p_send.blocks > active_state->max_blocks_out)) || |
| 1977 | (active_state->max_blocks_in && |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1978 | (active_state->p_read.blocks > active_state->max_blocks_in)) || |
| 1979 | (active_state->rekey_interval != 0 && active_state->rekey_time + |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1980 | active_state->rekey_interval <= monotime()); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | void |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1984 | packet_set_rekey_limits(u_int32_t bytes, time_t seconds) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1985 | { |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1986 | debug3("rekey after %lld bytes, %d seconds", (long long)bytes, |
| 1987 | (int)seconds); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1988 | active_state->rekey_limit = bytes; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1989 | active_state->rekey_interval = seconds; |
| 1990 | /* |
| 1991 | * We set the time here so that in post-auth privsep slave we count |
| 1992 | * from the completion of the authentication. |
| 1993 | */ |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1994 | active_state->rekey_time = monotime(); |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | time_t |
| 1998 | packet_get_rekey_timeout(void) |
| 1999 | { |
| 2000 | time_t seconds; |
| 2001 | |
| 2002 | seconds = active_state->rekey_time + active_state->rekey_interval - |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2003 | monotime(); |
Darren Tucker | 5f96f3b | 2013-05-16 20:29:28 +1000 | [diff] [blame] | 2004 | return (seconds <= 0 ? 1 : seconds); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 2005 | } |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 2006 | |
| 2007 | void |
| 2008 | packet_set_server(void) |
| 2009 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2010 | active_state->server_side = 1; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | void |
| 2014 | packet_set_authenticated(void) |
| 2015 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2016 | active_state->after_authentication = 1; |
| 2017 | } |
| 2018 | |
| 2019 | void * |
| 2020 | packet_get_input(void) |
| 2021 | { |
| 2022 | return (void *)&active_state->input; |
| 2023 | } |
| 2024 | |
| 2025 | void * |
| 2026 | packet_get_output(void) |
| 2027 | { |
| 2028 | return (void *)&active_state->output; |
| 2029 | } |
| 2030 | |
| 2031 | void * |
| 2032 | packet_get_newkeys(int mode) |
| 2033 | { |
| 2034 | return (void *)active_state->newkeys[mode]; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 2035 | } |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2036 | |
| 2037 | /* |
| 2038 | * Save the state for the real connection, and use a separate state when |
| 2039 | * resuming a suspended connection. |
| 2040 | */ |
| 2041 | void |
| 2042 | packet_backup_state(void) |
| 2043 | { |
| 2044 | struct session_state *tmp; |
| 2045 | |
| 2046 | close(active_state->connection_in); |
| 2047 | active_state->connection_in = -1; |
| 2048 | close(active_state->connection_out); |
| 2049 | active_state->connection_out = -1; |
| 2050 | if (backup_state) |
| 2051 | tmp = backup_state; |
| 2052 | else |
| 2053 | tmp = alloc_session_state(); |
| 2054 | backup_state = active_state; |
| 2055 | active_state = tmp; |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * Swap in the old state when resuming a connecion. |
| 2060 | */ |
| 2061 | void |
| 2062 | packet_restore_state(void) |
| 2063 | { |
| 2064 | struct session_state *tmp; |
| 2065 | void *buf; |
| 2066 | u_int len; |
| 2067 | |
| 2068 | tmp = backup_state; |
| 2069 | backup_state = active_state; |
| 2070 | active_state = tmp; |
| 2071 | active_state->connection_in = backup_state->connection_in; |
| 2072 | backup_state->connection_in = -1; |
| 2073 | active_state->connection_out = backup_state->connection_out; |
| 2074 | backup_state->connection_out = -1; |
| 2075 | len = buffer_len(&backup_state->input); |
| 2076 | if (len > 0) { |
| 2077 | buf = buffer_ptr(&backup_state->input); |
| 2078 | buffer_append(&active_state->input, buf, len); |
| 2079 | buffer_clear(&backup_state->input); |
| 2080 | add_recv_bytes(len); |
| 2081 | } |
| 2082 | } |
Damien Miller | c31a0cd | 2014-05-15 14:37:39 +1000 | [diff] [blame] | 2083 | |
| 2084 | /* Reset after_authentication and reset compression in post-auth privsep */ |
| 2085 | void |
| 2086 | packet_set_postauth(void) |
| 2087 | { |
| 2088 | Comp *comp; |
| 2089 | int mode; |
| 2090 | |
| 2091 | debug("%s: called", __func__); |
| 2092 | /* This was set in net child, but is not visible in user child */ |
| 2093 | active_state->after_authentication = 1; |
| 2094 | active_state->rekeying = 0; |
| 2095 | for (mode = 0; mode < MODE_MAX; mode++) { |
| 2096 | if (active_state->newkeys[mode] == NULL) |
| 2097 | continue; |
| 2098 | comp = &active_state->newkeys[mode]->comp; |
| 2099 | if (comp && comp->enabled) |
| 2100 | packet_init_compression(); |
| 2101 | } |
| 2102 | } |