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