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