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