Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1 | /* $OpenBSD: packet.c,v 1.188 2013/07/12 00:19:58 djm 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)); |
| 716 | cipher_crypt(&active_state->send_context, cp, |
| 717 | buffer_ptr(&active_state->outgoing_packet), |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 718 | buffer_len(&active_state->outgoing_packet), 0, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 719 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 720 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 721 | fprintf(stderr, "encrypted: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 722 | buffer_dump(&active_state->output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 723 | #endif |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 724 | active_state->p_send.packets++; |
| 725 | active_state->p_send.bytes += len + |
| 726 | buffer_len(&active_state->outgoing_packet); |
| 727 | buffer_clear(&active_state->outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 728 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 729 | /* |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 730 | * 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] | 731 | * actually sent until packet_write_wait or packet_write_poll is |
| 732 | * called. |
| 733 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 734 | } |
| 735 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 736 | void |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 737 | set_newkeys(int mode) |
| 738 | { |
| 739 | Enc *enc; |
| 740 | Mac *mac; |
| 741 | Comp *comp; |
| 742 | CipherContext *cc; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 743 | u_int64_t *max_blocks; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 744 | int crypt_type; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 745 | |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 746 | debug2("set_newkeys: mode %d", mode); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 747 | |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 748 | if (mode == MODE_OUT) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 749 | cc = &active_state->send_context; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 750 | crypt_type = CIPHER_ENCRYPT; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 751 | active_state->p_send.packets = active_state->p_send.blocks = 0; |
| 752 | max_blocks = &active_state->max_blocks_out; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 753 | } else { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 754 | cc = &active_state->receive_context; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 755 | crypt_type = CIPHER_DECRYPT; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 756 | active_state->p_read.packets = active_state->p_read.blocks = 0; |
| 757 | max_blocks = &active_state->max_blocks_in; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 758 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 759 | if (active_state->newkeys[mode] != NULL) { |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 760 | debug("set_newkeys: rekeying"); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 761 | cipher_cleanup(cc); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 762 | enc = &active_state->newkeys[mode]->enc; |
| 763 | mac = &active_state->newkeys[mode]->mac; |
| 764 | comp = &active_state->newkeys[mode]->comp; |
Damien Miller | e45796f | 2007-06-11 14:01:42 +1000 | [diff] [blame] | 765 | mac_clear(mac); |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 766 | memset(enc->iv, 0, enc->iv_len); |
Darren Tucker | 302889a | 2012-10-05 10:42:53 +1000 | [diff] [blame] | 767 | memset(enc->key, 0, enc->key_len); |
| 768 | memset(mac->key, 0, mac->key_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 769 | free(enc->name); |
| 770 | free(enc->iv); |
| 771 | free(enc->key); |
| 772 | free(mac->name); |
| 773 | free(mac->key); |
| 774 | free(comp->name); |
| 775 | free(active_state->newkeys[mode]); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 776 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 777 | active_state->newkeys[mode] = kex_get_newkeys(mode); |
| 778 | if (active_state->newkeys[mode] == NULL) |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 779 | fatal("newkeys: no keys for mode %d", mode); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 780 | enc = &active_state->newkeys[mode]->enc; |
| 781 | mac = &active_state->newkeys[mode]->mac; |
| 782 | comp = &active_state->newkeys[mode]->comp; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 783 | if (cipher_authlen(enc->cipher) == 0 && mac_init(mac) == 0) |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 784 | mac->enabled = 1; |
| 785 | DBG(debug("cipher_init_context: %d", mode)); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 786 | cipher_init(cc, enc->cipher, enc->key, enc->key_len, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 787 | enc->iv, enc->iv_len, crypt_type); |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 788 | /* Deleting the keys does not gain extra security */ |
| 789 | /* memset(enc->iv, 0, enc->block_size); |
Darren Tucker | 5f3d5be | 2007-06-05 18:30:18 +1000 | [diff] [blame] | 790 | memset(enc->key, 0, enc->key_len); |
| 791 | memset(mac->key, 0, mac->key_len); */ |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 792 | if ((comp->type == COMP_ZLIB || |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 793 | (comp->type == COMP_DELAYED && |
| 794 | active_state->after_authentication)) && comp->enabled == 0) { |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 795 | packet_init_compression(); |
| 796 | if (mode == MODE_OUT) |
| 797 | buffer_compress_init_send(6); |
| 798 | else |
| 799 | buffer_compress_init_recv(); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 800 | comp->enabled = 1; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 801 | } |
Darren Tucker | 81a0b37 | 2003-07-14 17:31:06 +1000 | [diff] [blame] | 802 | /* |
| 803 | * The 2^(blocksize*2) limit is too expensive for 3DES, |
| 804 | * blowfish, etc, so enforce a 1GB limit for small blocksizes. |
| 805 | */ |
| 806 | if (enc->block_size >= 16) |
| 807 | *max_blocks = (u_int64_t)1 << (enc->block_size*2); |
| 808 | else |
| 809 | *max_blocks = ((u_int64_t)1 << 30) / enc->block_size; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 810 | if (active_state->rekey_limit) |
| 811 | *max_blocks = MIN(*max_blocks, |
| 812 | active_state->rekey_limit / enc->block_size); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 815 | /* |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 816 | * Delayed compression for SSH2 is enabled after authentication: |
Darren Tucker | f676c57 | 2006-08-05 18:51:08 +1000 | [diff] [blame] | 817 | * 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] | 818 | * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received. |
| 819 | */ |
| 820 | static void |
| 821 | packet_enable_delayed_compress(void) |
| 822 | { |
| 823 | Comp *comp = NULL; |
| 824 | int mode; |
| 825 | |
| 826 | /* |
| 827 | * Remember that we are past the authentication step, so rekeying |
| 828 | * with COMP_DELAYED will turn on compression immediately. |
| 829 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 830 | active_state->after_authentication = 1; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 831 | for (mode = 0; mode < MODE_MAX; mode++) { |
Darren Tucker | 4aa665b | 2006-09-21 13:00:25 +1000 | [diff] [blame] | 832 | /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 833 | if (active_state->newkeys[mode] == NULL) |
Darren Tucker | 4aa665b | 2006-09-21 13:00:25 +1000 | [diff] [blame] | 834 | continue; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 835 | comp = &active_state->newkeys[mode]->comp; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 836 | if (comp && !comp->enabled && comp->type == COMP_DELAYED) { |
Damien Miller | b5c0125 | 2005-08-12 22:10:28 +1000 | [diff] [blame] | 837 | packet_init_compression(); |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 838 | if (mode == MODE_OUT) |
| 839 | buffer_compress_init_send(6); |
| 840 | else |
| 841 | buffer_compress_init_recv(); |
| 842 | comp->enabled = 1; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /* |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 848 | * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue) |
| 849 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 850 | static void |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 851 | packet_send2_wrapped(void) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 852 | { |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 853 | u_char type, *cp, *macbuf = NULL; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 854 | u_char padlen, pad = 0; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 855 | u_int i, len, authlen = 0, aadlen = 0; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 856 | u_int32_t rnd = 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 857 | Enc *enc = NULL; |
| 858 | Mac *mac = NULL; |
| 859 | Comp *comp = NULL; |
| 860 | int block_size; |
| 861 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 862 | if (active_state->newkeys[MODE_OUT] != NULL) { |
| 863 | enc = &active_state->newkeys[MODE_OUT]->enc; |
| 864 | mac = &active_state->newkeys[MODE_OUT]->mac; |
| 865 | comp = &active_state->newkeys[MODE_OUT]->comp; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 866 | /* disable mac for authenticated encryption */ |
| 867 | if ((authlen = cipher_authlen(enc->cipher)) != 0) |
| 868 | mac = NULL; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 869 | } |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 870 | block_size = enc ? enc->block_size : 8; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 871 | aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 872 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 873 | cp = buffer_ptr(&active_state->outgoing_packet); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 874 | type = cp[5]; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 875 | |
| 876 | #ifdef PACKET_DEBUG |
| 877 | fprintf(stderr, "plain: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 878 | buffer_dump(&active_state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 879 | #endif |
| 880 | |
| 881 | if (comp && comp->enabled) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 882 | len = buffer_len(&active_state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 883 | /* skip header, compress only payload */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 884 | buffer_consume(&active_state->outgoing_packet, 5); |
| 885 | buffer_clear(&active_state->compression_buffer); |
| 886 | buffer_compress(&active_state->outgoing_packet, |
| 887 | &active_state->compression_buffer); |
| 888 | buffer_clear(&active_state->outgoing_packet); |
| 889 | buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5); |
| 890 | buffer_append(&active_state->outgoing_packet, |
| 891 | buffer_ptr(&active_state->compression_buffer), |
| 892 | buffer_len(&active_state->compression_buffer)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 893 | DBG(debug("compression: raw %d compressed %d", len, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 894 | buffer_len(&active_state->outgoing_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /* sizeof (packet_len + pad_len + payload) */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 898 | len = buffer_len(&active_state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 899 | |
| 900 | /* |
| 901 | * calc size of padding, alloc space, get random data, |
| 902 | * minimum padding is 4 bytes |
| 903 | */ |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 904 | len -= aadlen; /* packet length is not encrypted for EtM modes */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 905 | padlen = block_size - (len % block_size); |
| 906 | if (padlen < 4) |
| 907 | padlen += block_size; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 908 | if (active_state->extra_pad) { |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 909 | /* will wrap if extra_pad+padlen > 255 */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 910 | active_state->extra_pad = |
| 911 | roundup(active_state->extra_pad, block_size); |
| 912 | pad = active_state->extra_pad - |
| 913 | ((len + padlen) % active_state->extra_pad); |
Ben Lindstrom | 8b08d81 | 2002-03-26 02:09:41 +0000 | [diff] [blame] | 914 | debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)", |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 915 | pad, len, padlen, active_state->extra_pad); |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 916 | padlen += pad; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 917 | active_state->extra_pad = 0; |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 918 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 919 | cp = buffer_append_space(&active_state->outgoing_packet, padlen); |
| 920 | if (enc && !active_state->send_context.plaintext) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 921 | /* random padding */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 922 | for (i = 0; i < padlen; i++) { |
| 923 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 924 | rnd = arc4random(); |
| 925 | cp[i] = rnd & 0xff; |
| 926 | rnd >>= 8; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 927 | } |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 928 | } else { |
| 929 | /* clear padding */ |
| 930 | memset(cp, 0, padlen); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 931 | } |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 932 | /* sizeof (packet_len + pad_len + payload + padding) */ |
| 933 | len = buffer_len(&active_state->outgoing_packet); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 934 | cp = buffer_ptr(&active_state->outgoing_packet); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 935 | /* packet_length includes payload, padding and padding length field */ |
| 936 | put_u32(cp, len - 4); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 937 | cp[4] = padlen; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 938 | DBG(debug("send: len %d (includes padlen %d, aadlen %d)", |
| 939 | len, padlen, aadlen)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 940 | |
| 941 | /* compute MAC over seqnr and packet(length fields, payload, padding) */ |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 942 | if (mac && mac->enabled && !mac->etm) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 943 | macbuf = mac_compute(mac, active_state->p_send.seqnr, |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 944 | buffer_ptr(&active_state->outgoing_packet), len); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 945 | DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 946 | } |
| 947 | /* encrypt packet and append to output buffer. */ |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 948 | cp = buffer_append_space(&active_state->output, len + authlen); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 949 | cipher_crypt(&active_state->send_context, cp, |
| 950 | buffer_ptr(&active_state->outgoing_packet), |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 951 | len - aadlen, aadlen, authlen); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 952 | /* append unencrypted MAC */ |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 953 | if (mac && mac->enabled) { |
| 954 | if (mac->etm) { |
| 955 | /* EtM: compute mac over aadlen + cipher text */ |
| 956 | macbuf = mac_compute(mac, |
| 957 | active_state->p_send.seqnr, cp, len); |
| 958 | DBG(debug("done calc MAC(EtM) out #%d", |
| 959 | active_state->p_send.seqnr)); |
| 960 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 961 | buffer_append(&active_state->output, macbuf, mac->mac_len); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 962 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 963 | #ifdef PACKET_DEBUG |
| 964 | fprintf(stderr, "encrypted: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 965 | buffer_dump(&active_state->output); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 966 | #endif |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 967 | /* increment sequence number for outgoing packets */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 968 | if (++active_state->p_send.seqnr == 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 969 | logit("outgoing seqnr wraps around"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 970 | if (++active_state->p_send.packets == 0) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 971 | if (!(datafellows & SSH_BUG_NOREKEY)) |
| 972 | fatal("XXX too many packets with same key"); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 973 | active_state->p_send.blocks += len / block_size; |
| 974 | active_state->p_send.bytes += len; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 975 | buffer_clear(&active_state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 976 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 977 | if (type == SSH2_MSG_NEWKEYS) |
| 978 | set_newkeys(MODE_OUT); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 979 | else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side) |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 980 | packet_enable_delayed_compress(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 981 | } |
| 982 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 983 | static void |
| 984 | packet_send2(void) |
| 985 | { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 986 | struct packet *p; |
| 987 | u_char type, *cp; |
| 988 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 989 | cp = buffer_ptr(&active_state->outgoing_packet); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 990 | type = cp[5]; |
| 991 | |
| 992 | /* during rekeying we can only send key exchange messages */ |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 993 | if (active_state->rekeying) { |
Damien Miller | 1de2cfe | 2012-02-11 08:18:43 +1100 | [diff] [blame] | 994 | if ((type < SSH2_MSG_TRANSPORT_MIN) || |
| 995 | (type > SSH2_MSG_TRANSPORT_MAX) || |
| 996 | (type == SSH2_MSG_SERVICE_REQUEST) || |
| 997 | (type == SSH2_MSG_SERVICE_ACCEPT)) { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 998 | debug("enqueue packet: %u", type); |
| 999 | p = xmalloc(sizeof(*p)); |
| 1000 | p->type = type; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1001 | memcpy(&p->payload, &active_state->outgoing_packet, |
| 1002 | sizeof(Buffer)); |
| 1003 | buffer_init(&active_state->outgoing_packet); |
| 1004 | TAILQ_INSERT_TAIL(&active_state->outgoing, p, next); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1005 | return; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | /* rekeying starts with sending KEXINIT */ |
| 1010 | if (type == SSH2_MSG_KEXINIT) |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1011 | active_state->rekeying = 1; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1012 | |
| 1013 | packet_send2_wrapped(); |
| 1014 | |
| 1015 | /* after a NEWKEYS message we can send the complete queue */ |
| 1016 | if (type == SSH2_MSG_NEWKEYS) { |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1017 | active_state->rekeying = 0; |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1018 | active_state->rekey_time = monotime(); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1019 | while ((p = TAILQ_FIRST(&active_state->outgoing))) { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1020 | type = p->type; |
| 1021 | debug("dequeue packet: %u", type); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1022 | buffer_free(&active_state->outgoing_packet); |
| 1023 | memcpy(&active_state->outgoing_packet, &p->payload, |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1024 | sizeof(Buffer)); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1025 | TAILQ_REMOVE(&active_state->outgoing, p, next); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1026 | free(p); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1027 | packet_send2_wrapped(); |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1032 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1033 | packet_send(void) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1034 | { |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1035 | if (compat20) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1036 | packet_send2(); |
| 1037 | else |
| 1038 | packet_send1(); |
| 1039 | DBG(debug("packet_send done")); |
| 1040 | } |
| 1041 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1042 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1043 | * Waits until a packet has been received, and returns its type. Note that |
| 1044 | * no other data is processed until this returns, so this function should not |
| 1045 | * be used during the interactive session. |
| 1046 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1047 | |
| 1048 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1049 | packet_read_seqnr(u_int32_t *seqnr_p) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1050 | { |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1051 | int type, len, ret, cont, ms_remain = 0; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1052 | fd_set *setp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1053 | char buf[8192]; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1054 | struct timeval timeout, start, *timeoutp = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1055 | |
Darren Tucker | 99bb761 | 2008-06-13 22:02:50 +1000 | [diff] [blame] | 1056 | DBG(debug("packet_read()")); |
| 1057 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1058 | setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1, |
| 1059 | NFDBITS), sizeof(fd_mask)); |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1060 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1061 | /* Since we are blocking, ensure that all written packets have been sent. */ |
| 1062 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1063 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1064 | /* Stay in the loop until we have received a complete packet. */ |
| 1065 | for (;;) { |
| 1066 | /* Try to read a packet from the buffer. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1067 | type = packet_read_poll_seqnr(seqnr_p); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1068 | if (!compat20 && ( |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1069 | type == SSH_SMSG_SUCCESS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1070 | || type == SSH_SMSG_FAILURE |
| 1071 | || type == SSH_CMSG_EOF |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1072 | || type == SSH_CMSG_EXIT_CONFIRMATION)) |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1073 | packet_check_eom(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1074 | /* If we got a packet, return it. */ |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1075 | if (type != SSH_MSG_NONE) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1076 | free(setp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1077 | return type; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1078 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1079 | /* |
| 1080 | * Otherwise, wait for some data to arrive, add it to the |
| 1081 | * buffer, and try again. |
| 1082 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1083 | memset(setp, 0, howmany(active_state->connection_in + 1, |
| 1084 | NFDBITS) * sizeof(fd_mask)); |
| 1085 | FD_SET(active_state->connection_in, setp); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1086 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1087 | if (active_state->packet_timeout_ms > 0) { |
| 1088 | ms_remain = active_state->packet_timeout_ms; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1089 | timeoutp = &timeout; |
| 1090 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1091 | /* Wait for some data to arrive. */ |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1092 | for (;;) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1093 | if (active_state->packet_timeout_ms != -1) { |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1094 | ms_to_timeval(&timeout, ms_remain); |
| 1095 | gettimeofday(&start, NULL); |
| 1096 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1097 | if ((ret = select(active_state->connection_in + 1, setp, |
| 1098 | NULL, NULL, timeoutp)) >= 0) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1099 | break; |
Damien Miller | ea43742 | 2009-10-02 11:49:03 +1000 | [diff] [blame] | 1100 | if (errno != EAGAIN && errno != EINTR && |
| 1101 | errno != EWOULDBLOCK) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1102 | break; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1103 | if (active_state->packet_timeout_ms == -1) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1104 | continue; |
| 1105 | ms_subtract_diff(&start, &ms_remain); |
| 1106 | if (ms_remain <= 0) { |
| 1107 | ret = 0; |
| 1108 | break; |
| 1109 | } |
| 1110 | } |
| 1111 | if (ret == 0) { |
| 1112 | logit("Connection to %.200s timed out while " |
| 1113 | "waiting to read", get_remote_ipaddr()); |
| 1114 | cleanup_exit(255); |
| 1115 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1116 | /* Read data from the socket. */ |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1117 | do { |
| 1118 | cont = 0; |
| 1119 | len = roaming_read(active_state->connection_in, buf, |
| 1120 | sizeof(buf), &cont); |
| 1121 | } while (len == 0 && cont); |
Damien Miller | 5e7c10e | 1999-12-16 13:18:04 +1100 | [diff] [blame] | 1122 | if (len == 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1123 | logit("Connection closed by %.200s", get_remote_ipaddr()); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1124 | cleanup_exit(255); |
Damien Miller | 5e7c10e | 1999-12-16 13:18:04 +1100 | [diff] [blame] | 1125 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1126 | if (len < 0) |
| 1127 | fatal("Read from socket failed: %.100s", strerror(errno)); |
| 1128 | /* Append it to the buffer. */ |
| 1129 | packet_process_incoming(buf, len); |
| 1130 | } |
| 1131 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1132 | } |
| 1133 | |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1134 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1135 | packet_read(void) |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1136 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1137 | return packet_read_seqnr(NULL); |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1138 | } |
| 1139 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1140 | /* |
| 1141 | * Waits until a packet has been received, verifies that its type matches |
| 1142 | * that given, and gives a fatal error and exits if there is a mismatch. |
| 1143 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1144 | |
| 1145 | void |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1146 | packet_read_expect(int expected_type) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1147 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1148 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1149 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1150 | type = packet_read(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1151 | if (type != expected_type) |
| 1152 | packet_disconnect("Protocol error: expected packet type %d, got %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1153 | expected_type, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /* 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] | 1157 | * packet_process_incoming. If so, reads the packet; otherwise returns |
| 1158 | * SSH_MSG_NONE. This does not wait for data from the connection. |
| 1159 | * |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1160 | * SSH_MSG_DISCONNECT is handled specially here. Also, |
| 1161 | * SSH_MSG_IGNORE messages are skipped by this function and are never returned |
| 1162 | * to higher levels. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1163 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1164 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1165 | static int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1166 | packet_read_poll1(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1167 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1168 | u_int len, padded_len; |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1169 | u_char *cp, type; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1170 | u_int checksum, stored_checksum; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1171 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1172 | /* Check if input size is less than minimum packet size. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1173 | if (buffer_len(&active_state->input) < 4 + 8) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1174 | return SSH_MSG_NONE; |
| 1175 | /* Get length of incoming packet. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1176 | cp = buffer_ptr(&active_state->input); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1177 | len = get_u32(cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1178 | if (len < 1 + 2 + 2 || len > 256 * 1024) |
Ben Lindstrom | 0cc2a47 | 2002-11-09 15:41:39 +0000 | [diff] [blame] | 1179 | packet_disconnect("Bad packet length %u.", len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1180 | padded_len = (len + 8) & ~7; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1181 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1182 | /* Check if the packet has been entirely received. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1183 | if (buffer_len(&active_state->input) < 4 + padded_len) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1184 | return SSH_MSG_NONE; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1185 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1186 | /* The entire packet is in buffer. */ |
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 | /* Consume packet length. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1189 | buffer_consume(&active_state->input, 4); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1190 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1191 | /* |
| 1192 | * Cryptographic attack detector for ssh |
| 1193 | * (C)1998 CORE-SDI, Buenos Aires Argentina |
| 1194 | * Ariel Futoransky(futo@core-sdi.com) |
| 1195 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1196 | if (!active_state->receive_context.plaintext) { |
| 1197 | switch (detect_attack(buffer_ptr(&active_state->input), |
| 1198 | padded_len)) { |
Damien Miller | 3c9c1fb | 2006-09-17 06:08:53 +1000 | [diff] [blame] | 1199 | case DEATTACK_DETECTED: |
| 1200 | packet_disconnect("crc32 compensation attack: " |
| 1201 | "network attack detected"); |
| 1202 | case DEATTACK_DOS_DETECTED: |
| 1203 | packet_disconnect("deattack denial of " |
| 1204 | "service detected"); |
| 1205 | } |
| 1206 | } |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1207 | |
| 1208 | /* Decrypt data to incoming_packet. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1209 | buffer_clear(&active_state->incoming_packet); |
| 1210 | cp = buffer_append_space(&active_state->incoming_packet, padded_len); |
| 1211 | cipher_crypt(&active_state->receive_context, cp, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1212 | buffer_ptr(&active_state->input), padded_len, 0, 0); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1213 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1214 | buffer_consume(&active_state->input, padded_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1215 | |
| 1216 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1217 | fprintf(stderr, "read_poll plain: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1218 | buffer_dump(&active_state->incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1219 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1220 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1221 | /* Compute packet checksum. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1222 | checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet), |
| 1223 | buffer_len(&active_state->incoming_packet) - 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1224 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1225 | /* Skip padding. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1226 | buffer_consume(&active_state->incoming_packet, 8 - len % 8); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 1227 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1228 | /* Test check bytes. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1229 | if (len != buffer_len(&active_state->incoming_packet)) |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1230 | packet_disconnect("packet_read_poll1: len %d != buffer_len %d.", |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1231 | len, buffer_len(&active_state->incoming_packet)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1232 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1233 | cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1234 | stored_checksum = get_u32(cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1235 | if (checksum != stored_checksum) |
| 1236 | packet_disconnect("Corrupted check bytes on input."); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1237 | buffer_consume_end(&active_state->incoming_packet, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1238 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1239 | if (active_state->packet_compression) { |
| 1240 | buffer_clear(&active_state->compression_buffer); |
| 1241 | buffer_uncompress(&active_state->incoming_packet, |
| 1242 | &active_state->compression_buffer); |
| 1243 | buffer_clear(&active_state->incoming_packet); |
| 1244 | buffer_append(&active_state->incoming_packet, |
| 1245 | buffer_ptr(&active_state->compression_buffer), |
| 1246 | buffer_len(&active_state->compression_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1247 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1248 | active_state->p_read.packets++; |
| 1249 | active_state->p_read.bytes += padded_len + 4; |
| 1250 | type = buffer_get_char(&active_state->incoming_packet); |
Darren Tucker | b2694f0 | 2004-11-05 20:27:54 +1100 | [diff] [blame] | 1251 | if (type < SSH_MSG_MIN || type > SSH_MSG_MAX) |
| 1252 | packet_disconnect("Invalid ssh1 packet type: %d", type); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1253 | return type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1254 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1255 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1256 | static int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1257 | packet_read_poll2(u_int32_t *seqnr_p) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1258 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1259 | u_int padlen, need; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1260 | u_char *macbuf = NULL, *cp, type; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1261 | u_int maclen, authlen = 0, aadlen = 0, block_size; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1262 | Enc *enc = NULL; |
| 1263 | Mac *mac = NULL; |
| 1264 | Comp *comp = NULL; |
| 1265 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1266 | if (active_state->packet_discard) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1267 | return SSH_MSG_NONE; |
| 1268 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1269 | if (active_state->newkeys[MODE_IN] != NULL) { |
| 1270 | enc = &active_state->newkeys[MODE_IN]->enc; |
| 1271 | mac = &active_state->newkeys[MODE_IN]->mac; |
| 1272 | comp = &active_state->newkeys[MODE_IN]->comp; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1273 | /* disable mac for authenticated encryption */ |
| 1274 | if ((authlen = cipher_authlen(enc->cipher)) != 0) |
| 1275 | mac = NULL; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1276 | } |
| 1277 | maclen = mac && mac->enabled ? mac->mac_len : 0; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 1278 | block_size = enc ? enc->block_size : 8; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1279 | aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1280 | |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1281 | if (aadlen && active_state->packlen == 0) { |
| 1282 | if (buffer_len(&active_state->input) < 4) |
| 1283 | return SSH_MSG_NONE; |
| 1284 | cp = buffer_ptr(&active_state->input); |
| 1285 | active_state->packlen = get_u32(cp); |
| 1286 | if (active_state->packlen < 1 + 4 || |
| 1287 | active_state->packlen > PACKET_MAX_SIZE) { |
| 1288 | #ifdef PACKET_DEBUG |
| 1289 | buffer_dump(&active_state->input); |
| 1290 | #endif |
| 1291 | logit("Bad packet length %u.", active_state->packlen); |
| 1292 | packet_disconnect("Packet corrupt"); |
| 1293 | } |
Damien Miller | 8c05da3 | 2012-12-13 07:18:59 +1100 | [diff] [blame] | 1294 | buffer_clear(&active_state->incoming_packet); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1295 | } else if (active_state->packlen == 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1296 | /* |
| 1297 | * check if input size is less than the cipher block size, |
| 1298 | * decrypt first block and extract length of incoming packet |
| 1299 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1300 | if (buffer_len(&active_state->input) < block_size) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1301 | return SSH_MSG_NONE; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1302 | buffer_clear(&active_state->incoming_packet); |
| 1303 | cp = buffer_append_space(&active_state->incoming_packet, |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1304 | block_size); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1305 | cipher_crypt(&active_state->receive_context, cp, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1306 | buffer_ptr(&active_state->input), block_size, 0, 0); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1307 | cp = buffer_ptr(&active_state->incoming_packet); |
| 1308 | active_state->packlen = get_u32(cp); |
| 1309 | if (active_state->packlen < 1 + 4 || |
| 1310 | active_state->packlen > PACKET_MAX_SIZE) { |
Darren Tucker | a8151da | 2003-09-22 21:06:46 +1000 | [diff] [blame] | 1311 | #ifdef PACKET_DEBUG |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1312 | buffer_dump(&active_state->incoming_packet); |
Darren Tucker | a8151da | 2003-09-22 21:06:46 +1000 | [diff] [blame] | 1313 | #endif |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1314 | logit("Bad packet length %u.", active_state->packlen); |
| 1315 | packet_start_discard(enc, mac, active_state->packlen, |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1316 | PACKET_MAX_SIZE); |
| 1317 | return SSH_MSG_NONE; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1318 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1319 | buffer_consume(&active_state->input, block_size); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1320 | } |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1321 | DBG(debug("input: packet len %u", active_state->packlen+4)); |
| 1322 | if (aadlen) { |
| 1323 | /* only the payload is encrypted */ |
| 1324 | need = active_state->packlen; |
| 1325 | } else { |
| 1326 | /* |
| 1327 | * the payload size and the payload are encrypted, but we |
| 1328 | * have a partial packet of block_size bytes |
| 1329 | */ |
| 1330 | need = 4 + active_state->packlen - block_size; |
| 1331 | } |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1332 | DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d," |
| 1333 | " aadlen %d", block_size, need, maclen, authlen, aadlen)); |
Darren Tucker | 99d11a3 | 2008-12-01 21:40:48 +1100 | [diff] [blame] | 1334 | if (need % block_size != 0) { |
| 1335 | logit("padding error: need %d block %d mod %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1336 | need, block_size, need % block_size); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1337 | packet_start_discard(enc, mac, active_state->packlen, |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1338 | PACKET_MAX_SIZE - block_size); |
| 1339 | return SSH_MSG_NONE; |
Darren Tucker | 99d11a3 | 2008-12-01 21:40:48 +1100 | [diff] [blame] | 1340 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1341 | /* |
| 1342 | * check if the entire packet has been received and |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1343 | * decrypt into incoming_packet: |
| 1344 | * 'aadlen' bytes are unencrypted, but authenticated. |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1345 | * 'need' bytes are encrypted, followed by either |
| 1346 | * 'authlen' bytes of authentication tag or |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1347 | * 'maclen' bytes of message authentication code. |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1348 | */ |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1349 | if (buffer_len(&active_state->input) < aadlen + need + authlen + maclen) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1350 | return SSH_MSG_NONE; |
| 1351 | #ifdef PACKET_DEBUG |
| 1352 | fprintf(stderr, "read_poll enc/full: "); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1353 | buffer_dump(&active_state->input); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1354 | #endif |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1355 | /* EtM: compute mac over encrypted input */ |
| 1356 | if (mac && mac->enabled && mac->etm) |
| 1357 | macbuf = mac_compute(mac, active_state->p_read.seqnr, |
| 1358 | buffer_ptr(&active_state->input), aadlen + need); |
| 1359 | cp = buffer_append_space(&active_state->incoming_packet, aadlen + need); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1360 | cipher_crypt(&active_state->receive_context, cp, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1361 | buffer_ptr(&active_state->input), need, aadlen, authlen); |
| 1362 | buffer_consume(&active_state->input, aadlen + need + authlen); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1363 | /* |
| 1364 | * compute MAC over seqnr and packet, |
| 1365 | * increment sequence number for incoming packet |
| 1366 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1367 | if (mac && mac->enabled) { |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1368 | if (!mac->etm) |
| 1369 | macbuf = mac_compute(mac, active_state->p_read.seqnr, |
| 1370 | buffer_ptr(&active_state->incoming_packet), |
| 1371 | buffer_len(&active_state->incoming_packet)); |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1372 | if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input), |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1373 | mac->mac_len) != 0) { |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1374 | logit("Corrupted MAC on input."); |
| 1375 | if (need > PACKET_MAX_SIZE) |
| 1376 | fatal("internal error need %d", need); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1377 | packet_start_discard(enc, mac, active_state->packlen, |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1378 | PACKET_MAX_SIZE - need); |
| 1379 | return SSH_MSG_NONE; |
| 1380 | } |
| 1381 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1382 | DBG(debug("MAC #%d ok", active_state->p_read.seqnr)); |
| 1383 | buffer_consume(&active_state->input, mac->mac_len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1384 | } |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1385 | /* XXX now it's safe to use fatal/packet_disconnect */ |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1386 | if (seqnr_p != NULL) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1387 | *seqnr_p = active_state->p_read.seqnr; |
| 1388 | if (++active_state->p_read.seqnr == 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1389 | logit("incoming seqnr wraps around"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1390 | if (++active_state->p_read.packets == 0) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1391 | if (!(datafellows & SSH_BUG_NOREKEY)) |
| 1392 | fatal("XXX too many packets with same key"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1393 | active_state->p_read.blocks += (active_state->packlen + 4) / block_size; |
| 1394 | active_state->p_read.bytes += active_state->packlen + 4; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1395 | |
| 1396 | /* get padlen */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1397 | cp = buffer_ptr(&active_state->incoming_packet); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1398 | padlen = cp[4]; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1399 | DBG(debug("input: padlen %d", padlen)); |
| 1400 | if (padlen < 4) |
| 1401 | packet_disconnect("Corrupted padlen %d on input.", padlen); |
| 1402 | |
| 1403 | /* skip packet size + padlen, discard padding */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1404 | buffer_consume(&active_state->incoming_packet, 4 + 1); |
| 1405 | buffer_consume_end(&active_state->incoming_packet, padlen); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1406 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1407 | DBG(debug("input: len before de-compress %d", |
| 1408 | buffer_len(&active_state->incoming_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1409 | if (comp && comp->enabled) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1410 | buffer_clear(&active_state->compression_buffer); |
| 1411 | buffer_uncompress(&active_state->incoming_packet, |
| 1412 | &active_state->compression_buffer); |
| 1413 | buffer_clear(&active_state->incoming_packet); |
| 1414 | buffer_append(&active_state->incoming_packet, |
| 1415 | buffer_ptr(&active_state->compression_buffer), |
| 1416 | buffer_len(&active_state->compression_buffer)); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1417 | DBG(debug("input: len after de-compress %d", |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1418 | buffer_len(&active_state->incoming_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1419 | } |
| 1420 | /* |
| 1421 | * get packet type, implies consume. |
| 1422 | * return length of payload (without type field) |
| 1423 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1424 | type = buffer_get_char(&active_state->incoming_packet); |
Darren Tucker | b2694f0 | 2004-11-05 20:27:54 +1100 | [diff] [blame] | 1425 | if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN) |
| 1426 | packet_disconnect("Invalid ssh2 packet type: %d", type); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 1427 | if (type == SSH2_MSG_NEWKEYS) |
| 1428 | set_newkeys(MODE_IN); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1429 | else if (type == SSH2_MSG_USERAUTH_SUCCESS && |
| 1430 | !active_state->server_side) |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1431 | packet_enable_delayed_compress(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1432 | #ifdef PACKET_DEBUG |
Ben Lindstrom | 204e488 | 2001-03-05 06:47:00 +0000 | [diff] [blame] | 1433 | fprintf(stderr, "read/plain[%d]:\r\n", type); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1434 | buffer_dump(&active_state->incoming_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1435 | #endif |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1436 | /* reset for next packet */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1437 | active_state->packlen = 0; |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1438 | return type; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1442 | packet_read_poll_seqnr(u_int32_t *seqnr_p) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1443 | { |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1444 | u_int reason, seqnr; |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1445 | u_char type; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1446 | char *msg; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1447 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1448 | for (;;) { |
| 1449 | if (compat20) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1450 | type = packet_read_poll2(seqnr_p); |
Darren Tucker | 136e56f | 2008-06-08 12:49:30 +1000 | [diff] [blame] | 1451 | if (type) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1452 | active_state->keep_alive_timeouts = 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1453 | DBG(debug("received packet type %d", type)); |
Darren Tucker | 136e56f | 2008-06-08 12:49:30 +1000 | [diff] [blame] | 1454 | } |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1455 | switch (type) { |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1456 | case SSH2_MSG_IGNORE: |
Damien Miller | 58226f6 | 2008-03-07 18:33:30 +1100 | [diff] [blame] | 1457 | debug3("Received SSH2_MSG_IGNORE"); |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1458 | break; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1459 | case SSH2_MSG_DEBUG: |
| 1460 | packet_get_char(); |
| 1461 | msg = packet_get_string(NULL); |
| 1462 | debug("Remote: %.900s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1463 | free(msg); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1464 | msg = packet_get_string(NULL); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1465 | free(msg); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1466 | break; |
| 1467 | case SSH2_MSG_DISCONNECT: |
| 1468 | reason = packet_get_int(); |
| 1469 | msg = packet_get_string(NULL); |
Damien Miller | d5edefd | 2013-04-23 15:21:39 +1000 | [diff] [blame] | 1470 | /* Ignore normal client exit notifications */ |
| 1471 | do_log2(active_state->server_side && |
| 1472 | reason == SSH2_DISCONNECT_BY_APPLICATION ? |
| 1473 | SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR, |
| 1474 | "Received disconnect from %s: %u: %.400s", |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1475 | get_remote_ipaddr(), reason, msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1476 | free(msg); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1477 | cleanup_exit(255); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1478 | break; |
Damien Miller | 659811f | 2002-01-22 23:23:11 +1100 | [diff] [blame] | 1479 | case SSH2_MSG_UNIMPLEMENTED: |
| 1480 | seqnr = packet_get_int(); |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1481 | debug("Received SSH2_MSG_UNIMPLEMENTED for %u", |
| 1482 | seqnr); |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1483 | break; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1484 | default: |
| 1485 | return type; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1486 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1487 | } else { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1488 | type = packet_read_poll1(); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1489 | switch (type) { |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1490 | case SSH_MSG_NONE: |
| 1491 | return SSH_MSG_NONE; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1492 | case SSH_MSG_IGNORE: |
| 1493 | break; |
| 1494 | case SSH_MSG_DEBUG: |
| 1495 | msg = packet_get_string(NULL); |
| 1496 | debug("Remote: %.900s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1497 | free(msg); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1498 | break; |
| 1499 | case SSH_MSG_DISCONNECT: |
| 1500 | msg = packet_get_string(NULL); |
Damien Miller | 894926e | 2013-02-12 11:03:58 +1100 | [diff] [blame] | 1501 | error("Received disconnect from %s: %.400s", |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1502 | get_remote_ipaddr(), msg); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1503 | cleanup_exit(255); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1504 | break; |
| 1505 | default: |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1506 | DBG(debug("received packet type %d", type)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1507 | return type; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1508 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1513 | /* |
| 1514 | * Buffers the given amount of input characters. This is intended to be used |
| 1515 | * together with packet_read_poll. |
| 1516 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1517 | |
| 1518 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1519 | packet_process_incoming(const char *buf, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1520 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1521 | if (active_state->packet_discard) { |
| 1522 | active_state->keep_alive_timeouts = 0; /* ?? */ |
| 1523 | if (len >= active_state->packet_discard) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1524 | packet_stop_discard(); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1525 | active_state->packet_discard -= len; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1526 | return; |
| 1527 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1528 | buffer_append(&active_state->input, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | /* Returns a character from the packet. */ |
| 1532 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1533 | u_int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1534 | packet_get_char(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1535 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1536 | char ch; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1537 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1538 | buffer_get(&active_state->incoming_packet, &ch, 1); |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1539 | return (u_char) ch; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | /* Returns an integer from the packet data. */ |
| 1543 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1544 | u_int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1545 | packet_get_int(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1546 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1547 | return buffer_get_int(&active_state->incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1548 | } |
| 1549 | |
Darren Tucker | 761c389 | 2009-06-21 18:16:26 +1000 | [diff] [blame] | 1550 | /* Returns an 64 bit integer from the packet data. */ |
| 1551 | |
| 1552 | u_int64_t |
| 1553 | packet_get_int64(void) |
| 1554 | { |
| 1555 | return buffer_get_int64(&active_state->incoming_packet); |
| 1556 | } |
| 1557 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1558 | /* |
| 1559 | * Returns an arbitrary precision integer from the packet data. The integer |
| 1560 | * must have been initialized before this call. |
| 1561 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1562 | |
| 1563 | void |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 1564 | packet_get_bignum(BIGNUM * value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1565 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1566 | buffer_get_bignum(&active_state->incoming_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1567 | } |
| 1568 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1569 | void |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 1570 | packet_get_bignum2(BIGNUM * value) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1571 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1572 | buffer_get_bignum2(&active_state->incoming_packet, value); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1573 | } |
| 1574 | |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1575 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1576 | void |
| 1577 | packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point) |
| 1578 | { |
| 1579 | buffer_get_ecpoint(&active_state->incoming_packet, curve, point); |
| 1580 | } |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1581 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1582 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1583 | void * |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1584 | packet_get_raw(u_int *length_ptr) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1585 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1586 | u_int bytes = buffer_len(&active_state->incoming_packet); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1587 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1588 | if (length_ptr != NULL) |
| 1589 | *length_ptr = bytes; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1590 | return buffer_ptr(&active_state->incoming_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1591 | } |
| 1592 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1593 | int |
| 1594 | packet_remaining(void) |
| 1595 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1596 | return buffer_len(&active_state->incoming_packet); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1597 | } |
| 1598 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1599 | /* |
| 1600 | * Returns a string from the packet data. The string is allocated using |
| 1601 | * xmalloc; it is the responsibility of the calling program to free it when |
| 1602 | * no longer needed. The length_ptr argument may be NULL, or point to an |
| 1603 | * integer into which the length of the string is stored. |
| 1604 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1605 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1606 | void * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1607 | packet_get_string(u_int *length_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1608 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1609 | return buffer_get_string(&active_state->incoming_packet, length_ptr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1610 | } |
| 1611 | |
Damien Miller | db255ca | 2008-05-19 14:59:37 +1000 | [diff] [blame] | 1612 | void * |
| 1613 | packet_get_string_ptr(u_int *length_ptr) |
| 1614 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1615 | return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr); |
Damien Miller | db255ca | 2008-05-19 14:59:37 +1000 | [diff] [blame] | 1616 | } |
| 1617 | |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 1618 | /* Ensures the returned string has no embedded \0 characters in it. */ |
| 1619 | char * |
| 1620 | packet_get_cstring(u_int *length_ptr) |
| 1621 | { |
| 1622 | return buffer_get_cstring(&active_state->incoming_packet, length_ptr); |
| 1623 | } |
| 1624 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1625 | /* |
| 1626 | * Sends a diagnostic message from the server to the client. This message |
| 1627 | * can be sent at any time (but not while constructing another message). The |
| 1628 | * message is printed immediately, but only if the client is being executed |
| 1629 | * in verbose mode. These messages are primarily intended to ease debugging |
| 1630 | * authentication problems. The length of the formatted message must not |
| 1631 | * exceed 1024 bytes. This will automatically call packet_write_wait. |
| 1632 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1633 | |
| 1634 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1635 | packet_send_debug(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1636 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1637 | char buf[1024]; |
| 1638 | va_list args; |
| 1639 | |
Ben Lindstrom | a14ee47 | 2000-12-07 01:24:58 +0000 | [diff] [blame] | 1640 | if (compat20 && (datafellows & SSH_BUG_DEBUG)) |
| 1641 | return; |
| 1642 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1643 | va_start(args, fmt); |
| 1644 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 1645 | va_end(args); |
| 1646 | |
Damien Miller | 7c8af4f | 2000-05-01 08:24:07 +1000 | [diff] [blame] | 1647 | if (compat20) { |
| 1648 | packet_start(SSH2_MSG_DEBUG); |
| 1649 | packet_put_char(0); /* bool: always display */ |
| 1650 | packet_put_cstring(buf); |
| 1651 | packet_put_cstring(""); |
| 1652 | } else { |
| 1653 | packet_start(SSH_MSG_DEBUG); |
| 1654 | packet_put_cstring(buf); |
| 1655 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1656 | packet_send(); |
| 1657 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1658 | } |
| 1659 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1660 | /* |
| 1661 | * Logs the error plus constructs and sends a disconnect packet, closes the |
| 1662 | * connection, and exits. This function never returns. The error message |
| 1663 | * should not contain a newline. The length of the formatted message must |
| 1664 | * not exceed 1024 bytes. |
| 1665 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1666 | |
| 1667 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1668 | packet_disconnect(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1669 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1670 | char buf[1024]; |
| 1671 | va_list args; |
| 1672 | static int disconnecting = 0; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1673 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1674 | if (disconnecting) /* Guard against recursive invocations. */ |
| 1675 | fatal("packet_disconnect called recursively."); |
| 1676 | disconnecting = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1677 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1678 | /* |
| 1679 | * Format the message. Note that the caller must make sure the |
| 1680 | * message is of limited size. |
| 1681 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1682 | va_start(args, fmt); |
| 1683 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 1684 | va_end(args); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1685 | |
Ben Lindstrom | 9bda7ae | 2002-11-09 15:46:24 +0000 | [diff] [blame] | 1686 | /* Display the error locally */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1687 | logit("Disconnecting: %.100s", buf); |
Ben Lindstrom | 9bda7ae | 2002-11-09 15:46:24 +0000 | [diff] [blame] | 1688 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1689 | /* 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] | 1690 | if (compat20) { |
| 1691 | packet_start(SSH2_MSG_DISCONNECT); |
| 1692 | packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR); |
| 1693 | packet_put_cstring(buf); |
| 1694 | packet_put_cstring(""); |
| 1695 | } else { |
| 1696 | packet_start(SSH_MSG_DISCONNECT); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 1697 | packet_put_cstring(buf); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1698 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1699 | packet_send(); |
| 1700 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1701 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1702 | /* Stop listening for connections. */ |
Ben Lindstrom | 601e436 | 2001-06-21 03:19:23 +0000 | [diff] [blame] | 1703 | channel_close_all(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1704 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1705 | /* Close the connection. */ |
| 1706 | packet_close(); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1707 | cleanup_exit(255); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1708 | } |
| 1709 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1710 | /* 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] | 1711 | |
| 1712 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1713 | packet_write_poll(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1714 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1715 | int len = buffer_len(&active_state->output); |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1716 | int cont; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1717 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1718 | if (len > 0) { |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1719 | cont = 0; |
| 1720 | len = roaming_write(active_state->connection_out, |
| 1721 | buffer_ptr(&active_state->output), len, &cont); |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1722 | if (len == -1) { |
Damien Miller | ea43742 | 2009-10-02 11:49:03 +1000 | [diff] [blame] | 1723 | if (errno == EINTR || errno == EAGAIN || |
| 1724 | errno == EWOULDBLOCK) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1725 | return; |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1726 | fatal("Write failed: %.100s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1727 | } |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1728 | if (len == 0 && !cont) |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1729 | fatal("Write connection closed"); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1730 | buffer_consume(&active_state->output, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1731 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1732 | } |
| 1733 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1734 | /* |
| 1735 | * Calls packet_write_poll repeatedly until all pending output data has been |
| 1736 | * written. |
| 1737 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1738 | |
| 1739 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1740 | packet_write_wait(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1741 | { |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1742 | fd_set *setp; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1743 | int ret, ms_remain = 0; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1744 | struct timeval start, timeout, *timeoutp = NULL; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1745 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1746 | setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1, |
| 1747 | NFDBITS), sizeof(fd_mask)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1748 | packet_write_poll(); |
| 1749 | while (packet_have_data_to_write()) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1750 | memset(setp, 0, howmany(active_state->connection_out + 1, |
| 1751 | NFDBITS) * sizeof(fd_mask)); |
| 1752 | FD_SET(active_state->connection_out, setp); |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1753 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1754 | if (active_state->packet_timeout_ms > 0) { |
| 1755 | ms_remain = active_state->packet_timeout_ms; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1756 | timeoutp = &timeout; |
| 1757 | } |
| 1758 | for (;;) { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1759 | if (active_state->packet_timeout_ms != -1) { |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1760 | ms_to_timeval(&timeout, ms_remain); |
| 1761 | gettimeofday(&start, NULL); |
| 1762 | } |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1763 | if ((ret = select(active_state->connection_out + 1, |
| 1764 | NULL, setp, NULL, timeoutp)) >= 0) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1765 | break; |
Damien Miller | ea43742 | 2009-10-02 11:49:03 +1000 | [diff] [blame] | 1766 | if (errno != EAGAIN && errno != EINTR && |
| 1767 | errno != EWOULDBLOCK) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1768 | break; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1769 | if (active_state->packet_timeout_ms == -1) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1770 | continue; |
| 1771 | ms_subtract_diff(&start, &ms_remain); |
| 1772 | if (ms_remain <= 0) { |
| 1773 | ret = 0; |
| 1774 | break; |
| 1775 | } |
| 1776 | } |
| 1777 | if (ret == 0) { |
| 1778 | logit("Connection to %.200s timed out while " |
| 1779 | "waiting to write", get_remote_ipaddr()); |
| 1780 | cleanup_exit(255); |
| 1781 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1782 | packet_write_poll(); |
| 1783 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1784 | free(setp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /* Returns true if there is buffered data to write to the connection. */ |
| 1788 | |
| 1789 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1790 | packet_have_data_to_write(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1791 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1792 | return buffer_len(&active_state->output) != 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | /* Returns true if there is not too much data to write to the connection. */ |
| 1796 | |
| 1797 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1798 | packet_not_very_much_data_to_write(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1799 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1800 | if (active_state->interactive_mode) |
| 1801 | return buffer_len(&active_state->output) < 16384; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1802 | else |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1803 | return buffer_len(&active_state->output) < 128 * 1024; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1804 | } |
| 1805 | |
Ben Lindstrom | faa1ea8 | 2002-12-23 02:42:52 +0000 | [diff] [blame] | 1806 | static void |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1807 | packet_set_tos(int tos) |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1808 | { |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 1809 | #ifndef IP_TOS_IS_BROKEN |
| 1810 | if (!packet_connection_is_on_socket()) |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1811 | return; |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 1812 | switch (packet_connection_af()) { |
| 1813 | # ifdef IP_TOS |
| 1814 | case AF_INET: |
| 1815 | debug3("%s: set IP_TOS 0x%02x", __func__, tos); |
| 1816 | if (setsockopt(active_state->connection_in, |
| 1817 | IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) |
| 1818 | error("setsockopt IP_TOS %d: %.100s:", |
| 1819 | tos, strerror(errno)); |
| 1820 | break; |
| 1821 | # endif /* IP_TOS */ |
| 1822 | # ifdef IPV6_TCLASS |
| 1823 | case AF_INET6: |
| 1824 | debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos); |
| 1825 | if (setsockopt(active_state->connection_in, |
| 1826 | IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0) |
| 1827 | error("setsockopt IPV6_TCLASS %d: %.100s:", |
| 1828 | tos, strerror(errno)); |
| 1829 | break; |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 1830 | # endif /* IPV6_TCLASS */ |
Damien Miller | 23f425b | 2011-05-15 08:58:15 +1000 | [diff] [blame] | 1831 | } |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 1832 | #endif /* IP_TOS_IS_BROKEN */ |
Damien Miller | 5924ceb | 2003-11-22 15:02:42 +1100 | [diff] [blame] | 1833 | } |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1834 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1835 | /* Informs that the current session is interactive. Sets IP flags for that. */ |
| 1836 | |
| 1837 | void |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1838 | packet_set_interactive(int interactive, int qos_interactive, int qos_bulk) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1839 | { |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1840 | if (active_state->set_interactive_called) |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1841 | return; |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1842 | active_state->set_interactive_called = 1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1843 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1844 | /* Record that we are in interactive mode. */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1845 | active_state->interactive_mode = interactive; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1846 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1847 | /* Only set socket options if using a socket. */ |
| 1848 | if (!packet_connection_is_on_socket()) |
Ben Lindstrom | 93b6b77 | 2003-04-27 17:55:33 +0000 | [diff] [blame] | 1849 | return; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1850 | set_nodelay(active_state->connection_in); |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1851 | packet_set_tos(interactive ? qos_interactive : qos_bulk); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | /* Returns true if the current connection is interactive. */ |
| 1855 | |
| 1856 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1857 | packet_is_interactive(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1858 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1859 | return active_state->interactive_mode; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1860 | } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1861 | |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 1862 | int |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 1863 | packet_set_maxsize(u_int s) |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1864 | { |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1865 | if (active_state->set_maxsize_called) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1866 | logit("packet_set_maxsize: called twice: old %d new %d", |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1867 | active_state->max_packet_size, s); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1868 | return -1; |
| 1869 | } |
| 1870 | if (s < 4 * 1024 || s > 1024 * 1024) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1871 | logit("packet_set_maxsize: bad size %d", s); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1872 | return -1; |
| 1873 | } |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 1874 | active_state->set_maxsize_called = 1; |
Ben Lindstrom | 16d45b3 | 2001-06-13 04:39:18 +0000 | [diff] [blame] | 1875 | debug("packet_set_maxsize: setting to %d", s); |
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 s; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1878 | } |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1879 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1880 | int |
| 1881 | packet_inc_alive_timeouts(void) |
| 1882 | { |
| 1883 | return ++active_state->keep_alive_timeouts; |
| 1884 | } |
| 1885 | |
| 1886 | void |
| 1887 | packet_set_alive_timeouts(int ka) |
| 1888 | { |
| 1889 | active_state->keep_alive_timeouts = ka; |
| 1890 | } |
| 1891 | |
| 1892 | u_int |
| 1893 | packet_get_maxsize(void) |
| 1894 | { |
| 1895 | return active_state->max_packet_size; |
| 1896 | } |
| 1897 | |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1898 | /* roundup current message to pad bytes */ |
| 1899 | void |
| 1900 | packet_add_padding(u_char pad) |
| 1901 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1902 | active_state->extra_pad = pad; |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1903 | } |
| 1904 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1905 | /* |
| 1906 | * 9.2. Ignored Data Message |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1907 | * |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1908 | * byte SSH_MSG_IGNORE |
| 1909 | * string data |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1910 | * |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1911 | * All implementations MUST understand (and ignore) this message at any |
| 1912 | * time (after receiving the protocol version). No implementation is |
| 1913 | * required to send them. This message can be used as an additional |
| 1914 | * protection measure against advanced traffic analysis techniques. |
| 1915 | */ |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1916 | void |
| 1917 | packet_send_ignore(int nbytes) |
| 1918 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1919 | u_int32_t rnd = 0; |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1920 | int i; |
| 1921 | |
| 1922 | packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE); |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1923 | packet_put_int(nbytes); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1924 | for (i = 0; i < nbytes; i++) { |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1925 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1926 | rnd = arc4random(); |
Damien Miller | 8ba29fe | 2006-03-26 14:25:19 +1100 | [diff] [blame] | 1927 | packet_put_char((u_char)rnd & 0xff); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1928 | rnd >>= 8; |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1929 | } |
| 1930 | } |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1931 | |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 1932 | #define MAX_PACKETS (1U<<31) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1933 | int |
| 1934 | packet_need_rekeying(void) |
| 1935 | { |
| 1936 | if (datafellows & SSH_BUG_NOREKEY) |
| 1937 | return 0; |
| 1938 | return |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1939 | (active_state->p_send.packets > MAX_PACKETS) || |
| 1940 | (active_state->p_read.packets > MAX_PACKETS) || |
| 1941 | (active_state->max_blocks_out && |
| 1942 | (active_state->p_send.blocks > active_state->max_blocks_out)) || |
| 1943 | (active_state->max_blocks_in && |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1944 | (active_state->p_read.blocks > active_state->max_blocks_in)) || |
| 1945 | (active_state->rekey_interval != 0 && active_state->rekey_time + |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1946 | active_state->rekey_interval <= monotime()); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | void |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1950 | packet_set_rekey_limits(u_int32_t bytes, time_t seconds) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1951 | { |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1952 | debug3("rekey after %lld bytes, %d seconds", (long long)bytes, |
| 1953 | (int)seconds); |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1954 | active_state->rekey_limit = bytes; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1955 | active_state->rekey_interval = seconds; |
| 1956 | /* |
| 1957 | * We set the time here so that in post-auth privsep slave we count |
| 1958 | * from the completion of the authentication. |
| 1959 | */ |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1960 | active_state->rekey_time = monotime(); |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | time_t |
| 1964 | packet_get_rekey_timeout(void) |
| 1965 | { |
| 1966 | time_t seconds; |
| 1967 | |
| 1968 | seconds = active_state->rekey_time + active_state->rekey_interval - |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1969 | monotime(); |
Darren Tucker | 5f96f3b | 2013-05-16 20:29:28 +1000 | [diff] [blame] | 1970 | return (seconds <= 0 ? 1 : seconds); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1971 | } |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1972 | |
| 1973 | void |
| 1974 | packet_set_server(void) |
| 1975 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1976 | active_state->server_side = 1; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | void |
| 1980 | packet_set_authenticated(void) |
| 1981 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1982 | active_state->after_authentication = 1; |
| 1983 | } |
| 1984 | |
| 1985 | void * |
| 1986 | packet_get_input(void) |
| 1987 | { |
| 1988 | return (void *)&active_state->input; |
| 1989 | } |
| 1990 | |
| 1991 | void * |
| 1992 | packet_get_output(void) |
| 1993 | { |
| 1994 | return (void *)&active_state->output; |
| 1995 | } |
| 1996 | |
| 1997 | void * |
| 1998 | packet_get_newkeys(int mode) |
| 1999 | { |
| 2000 | return (void *)active_state->newkeys[mode]; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 2001 | } |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2002 | |
| 2003 | /* |
| 2004 | * Save the state for the real connection, and use a separate state when |
| 2005 | * resuming a suspended connection. |
| 2006 | */ |
| 2007 | void |
| 2008 | packet_backup_state(void) |
| 2009 | { |
| 2010 | struct session_state *tmp; |
| 2011 | |
| 2012 | close(active_state->connection_in); |
| 2013 | active_state->connection_in = -1; |
| 2014 | close(active_state->connection_out); |
| 2015 | active_state->connection_out = -1; |
| 2016 | if (backup_state) |
| 2017 | tmp = backup_state; |
| 2018 | else |
| 2019 | tmp = alloc_session_state(); |
| 2020 | backup_state = active_state; |
| 2021 | active_state = tmp; |
| 2022 | } |
| 2023 | |
| 2024 | /* |
| 2025 | * Swap in the old state when resuming a connecion. |
| 2026 | */ |
| 2027 | void |
| 2028 | packet_restore_state(void) |
| 2029 | { |
| 2030 | struct session_state *tmp; |
| 2031 | void *buf; |
| 2032 | u_int len; |
| 2033 | |
| 2034 | tmp = backup_state; |
| 2035 | backup_state = active_state; |
| 2036 | active_state = tmp; |
| 2037 | active_state->connection_in = backup_state->connection_in; |
| 2038 | backup_state->connection_in = -1; |
| 2039 | active_state->connection_out = backup_state->connection_out; |
| 2040 | backup_state->connection_out = -1; |
| 2041 | len = buffer_len(&backup_state->input); |
| 2042 | if (len > 0) { |
| 2043 | buf = buffer_ptr(&backup_state->input); |
| 2044 | buffer_append(&active_state->input, buf, len); |
| 2045 | buffer_clear(&backup_state->input); |
| 2046 | add_recv_bytes(len); |
| 2047 | } |
| 2048 | } |