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