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