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