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