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