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