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