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