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