Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1 | /* $OpenBSD: packet.c,v 1.156 2008/07/04 23:08:25 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * This file contains code implementing the packet protocol and communication |
| 7 | * 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] | 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * As far as I am concerned, the code I have written for this software |
| 10 | * can be used freely for any purpose. Any derived versions of this |
| 11 | * software must be clearly marked as such, and if the derived work is |
| 12 | * incompatible with the protocol description in the RFC file, it must be |
| 13 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 14 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | * |
| 16 | * SSH2 packet format added by Markus Friedl. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 17 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 18 | * |
| 19 | * Redistribution and use in source and binary forms, with or without |
| 20 | * modification, are permitted provided that the following conditions |
| 21 | * are met: |
| 22 | * 1. Redistributions of source code must retain the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer. |
| 24 | * 2. Redistributions in binary form must reproduce the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer in the |
| 26 | * documentation and/or other materials provided with the distribution. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 30 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 33 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 37 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 38 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
| 40 | #include "includes.h" |
Damien Miller | 68f8e99 | 2006-03-15 11:24:12 +1100 | [diff] [blame] | 41 | |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 42 | #include <sys/types.h> |
Damien Miller | a0898b8 | 2003-04-09 21:05:52 +1000 | [diff] [blame] | 43 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 44 | #include <sys/param.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 45 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 46 | #ifdef HAVE_SYS_TIME_H |
| 47 | # include <sys/time.h> |
| 48 | #endif |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 49 | |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 50 | #include <netinet/in.h> |
Damien Miller | 68f8e99 | 2006-03-15 11:24:12 +1100 | [diff] [blame] | 51 | #include <netinet/ip.h> |
Darren Tucker | dace233 | 2006-09-22 19:22:17 +1000 | [diff] [blame] | 52 | #include <arpa/inet.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 54 | #include <errno.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 55 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 56 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 57 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 58 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 59 | #include <unistd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 60 | #include <signal.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 61 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | #include "xmalloc.h" |
| 63 | #include "buffer.h" |
| 64 | #include "packet.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 65 | #include "crc32.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | #include "compress.h" |
| 67 | #include "deattack.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 68 | #include "channels.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 69 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 70 | #include "ssh1.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 71 | #include "ssh2.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 72 | #include "cipher.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 73 | #include "key.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 74 | #include "kex.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 75 | #include "mac.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 76 | #include "log.h" |
| 77 | #include "canohost.h" |
Damien Miller | 4d00776 | 2002-02-05 11:52:54 +1100 | [diff] [blame] | 78 | #include "misc.h" |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 79 | #include "ssh.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 80 | |
| 81 | #ifdef PACKET_DEBUG |
| 82 | #define DBG(x) x |
| 83 | #else |
| 84 | #define DBG(x) |
| 85 | #endif |
| 86 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 87 | /* |
| 88 | * This variable contains the file descriptors used for communicating with |
| 89 | * the other side. connection_in is used for reading; connection_out for |
| 90 | * writing. These can be the same descriptor, in which case it is assumed to |
| 91 | * be a socket. |
| 92 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 93 | static int connection_in = -1; |
| 94 | static int connection_out = -1; |
| 95 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 96 | /* Protocol flags for the remote side. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 97 | static u_int remote_protocol_flags = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 98 | |
| 99 | /* Encryption context for receiving data. This is only used for decryption. */ |
| 100 | static CipherContext receive_context; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 101 | |
| 102 | /* Encryption context for sending data. This is only used for encryption. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | static CipherContext send_context; |
| 104 | |
| 105 | /* Buffer for raw input data from the socket. */ |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 106 | Buffer input; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | |
| 108 | /* Buffer for raw output data going to the socket. */ |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 109 | Buffer output; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 110 | |
| 111 | /* Buffer for the partial outgoing packet being constructed. */ |
| 112 | static Buffer outgoing_packet; |
| 113 | |
| 114 | /* Buffer for the incoming packet currently being processed. */ |
| 115 | static Buffer incoming_packet; |
| 116 | |
| 117 | /* Scratch buffer for packet compression/decompression. */ |
| 118 | static Buffer compression_buffer; |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 119 | static int compression_buffer_ready = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 120 | |
| 121 | /* Flag indicating whether packet compression/decompression is enabled. */ |
| 122 | static int packet_compression = 0; |
| 123 | |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 124 | /* default maximum packet size */ |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 125 | u_int max_packet_size = 32768; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 126 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 127 | /* Flag indicating whether this module has been initialized. */ |
| 128 | static int initialized = 0; |
| 129 | |
| 130 | /* Set to true if the connection is interactive. */ |
| 131 | static int interactive_mode = 0; |
| 132 | |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 133 | /* Set to true if we are the server side. */ |
| 134 | static int server_side = 0; |
| 135 | |
| 136 | /* Set to true if we are authenticated. */ |
| 137 | static int after_authentication = 0; |
| 138 | |
Damien Miller | 58226f6 | 2008-03-07 18:33:30 +1100 | [diff] [blame] | 139 | int keep_alive_timeouts = 0; |
| 140 | |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 141 | /* Set to the maximum time that we will wait to send or receive a packet */ |
| 142 | static int packet_timeout_ms = -1; |
| 143 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 144 | /* Session key information for Encryption and MAC */ |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 145 | Newkeys *newkeys[MODE_MAX]; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 146 | static struct packet_state { |
| 147 | u_int32_t seqnr; |
| 148 | u_int32_t packets; |
| 149 | u_int64_t blocks; |
| 150 | } p_read, p_send; |
| 151 | |
| 152 | static u_int64_t max_blocks_in, max_blocks_out; |
| 153 | static u_int32_t rekey_limit; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 154 | |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 155 | /* Session key for protocol v1 */ |
| 156 | static u_char ssh1_key[SSH_SESSION_KEY_LENGTH]; |
| 157 | static u_int ssh1_keylen; |
| 158 | |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 159 | /* roundup current message to extra_pad bytes */ |
| 160 | static u_char extra_pad = 0; |
| 161 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 162 | struct packet { |
| 163 | TAILQ_ENTRY(packet) next; |
| 164 | u_char type; |
| 165 | Buffer payload; |
| 166 | }; |
| 167 | TAILQ_HEAD(, packet) outgoing; |
| 168 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 169 | /* |
| 170 | * Sets the descriptors used for communication. Disables encryption until |
| 171 | * packet_set_encryption_key is called. |
| 172 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 173 | void |
| 174 | packet_set_connection(int fd_in, int fd_out) |
| 175 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 176 | Cipher *none = cipher_by_name("none"); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 177 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 178 | if (none == NULL) |
| 179 | fatal("packet_set_connection: cannot load cipher 'none'"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 180 | connection_in = fd_in; |
| 181 | connection_out = fd_out; |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 182 | cipher_init(&send_context, none, (const u_char *)"", |
| 183 | 0, NULL, 0, CIPHER_ENCRYPT); |
| 184 | cipher_init(&receive_context, none, (const u_char *)"", |
| 185 | 0, NULL, 0, CIPHER_DECRYPT); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 186 | newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 187 | if (!initialized) { |
| 188 | initialized = 1; |
| 189 | buffer_init(&input); |
| 190 | buffer_init(&output); |
| 191 | buffer_init(&outgoing_packet); |
| 192 | buffer_init(&incoming_packet); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 193 | TAILQ_INIT(&outgoing); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 194 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 195 | } |
| 196 | |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 197 | void |
| 198 | packet_set_timeout(int timeout, int count) |
| 199 | { |
| 200 | if (timeout == 0 || count == 0) { |
| 201 | packet_timeout_ms = -1; |
| 202 | return; |
| 203 | } |
| 204 | if ((INT_MAX / 1000) / count < timeout) |
| 205 | packet_timeout_ms = INT_MAX; |
| 206 | else |
| 207 | packet_timeout_ms = timeout * count * 1000; |
| 208 | } |
| 209 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 210 | /* Returns 1 if remote host is connected via socket, 0 if not. */ |
| 211 | |
| 212 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 213 | packet_connection_is_on_socket(void) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 214 | { |
| 215 | struct sockaddr_storage from, to; |
| 216 | socklen_t fromlen, tolen; |
| 217 | |
| 218 | /* filedescriptors in and out are the same, so it's a socket */ |
| 219 | if (connection_in == connection_out) |
| 220 | return 1; |
| 221 | fromlen = sizeof(from); |
| 222 | memset(&from, 0, sizeof(from)); |
Damien Miller | f052aaf | 2000-01-22 19:47:21 +1100 | [diff] [blame] | 223 | if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 224 | return 0; |
| 225 | tolen = sizeof(to); |
| 226 | memset(&to, 0, sizeof(to)); |
Damien Miller | f052aaf | 2000-01-22 19:47:21 +1100 | [diff] [blame] | 227 | if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 228 | return 0; |
| 229 | if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0) |
| 230 | return 0; |
| 231 | if (from.ss_family != AF_INET && from.ss_family != AF_INET6) |
| 232 | return 0; |
| 233 | return 1; |
| 234 | } |
| 235 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 236 | /* |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 237 | * Exports an IV from the CipherContext required to export the key |
| 238 | * state back from the unprivileged child to the privileged parent |
| 239 | * process. |
| 240 | */ |
| 241 | |
| 242 | void |
| 243 | packet_get_keyiv(int mode, u_char *iv, u_int len) |
| 244 | { |
| 245 | CipherContext *cc; |
| 246 | |
| 247 | if (mode == MODE_OUT) |
| 248 | cc = &send_context; |
| 249 | else |
| 250 | cc = &receive_context; |
| 251 | |
| 252 | cipher_get_keyiv(cc, iv, len); |
| 253 | } |
| 254 | |
| 255 | int |
| 256 | packet_get_keycontext(int mode, u_char *dat) |
| 257 | { |
| 258 | CipherContext *cc; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 259 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 260 | if (mode == MODE_OUT) |
| 261 | cc = &send_context; |
| 262 | else |
| 263 | cc = &receive_context; |
| 264 | |
| 265 | return (cipher_get_keycontext(cc, dat)); |
| 266 | } |
| 267 | |
| 268 | void |
| 269 | packet_set_keycontext(int mode, u_char *dat) |
| 270 | { |
| 271 | CipherContext *cc; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 272 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 273 | if (mode == MODE_OUT) |
| 274 | cc = &send_context; |
| 275 | else |
| 276 | cc = &receive_context; |
| 277 | |
| 278 | cipher_set_keycontext(cc, dat); |
| 279 | } |
| 280 | |
| 281 | int |
| 282 | packet_get_keyiv_len(int mode) |
| 283 | { |
| 284 | CipherContext *cc; |
| 285 | |
| 286 | if (mode == MODE_OUT) |
| 287 | cc = &send_context; |
| 288 | else |
| 289 | cc = &receive_context; |
| 290 | |
| 291 | return (cipher_get_keyiv_len(cc)); |
| 292 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 293 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 294 | void |
| 295 | packet_set_iv(int mode, u_char *dat) |
| 296 | { |
| 297 | CipherContext *cc; |
| 298 | |
| 299 | if (mode == MODE_OUT) |
| 300 | cc = &send_context; |
| 301 | else |
| 302 | cc = &receive_context; |
| 303 | |
| 304 | cipher_set_keyiv(cc, dat); |
| 305 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 306 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 307 | int |
Damien Miller | 2b92d32 | 2003-06-11 22:05:06 +1000 | [diff] [blame] | 308 | packet_get_ssh1_cipher(void) |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 309 | { |
| 310 | return (cipher_get_number(receive_context.cipher)); |
| 311 | } |
| 312 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 313 | void |
| 314 | packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets) |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 315 | { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 316 | struct packet_state *state; |
| 317 | |
| 318 | state = (mode == MODE_IN) ? &p_read : &p_send; |
| 319 | *seqnr = state->seqnr; |
| 320 | *blocks = state->blocks; |
| 321 | *packets = state->packets; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 325 | packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets) |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 326 | { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 327 | struct packet_state *state; |
| 328 | |
| 329 | state = (mode == MODE_IN) ? &p_read : &p_send; |
| 330 | state->seqnr = seqnr; |
| 331 | state->blocks = blocks; |
| 332 | state->packets = packets; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 335 | /* returns 1 if connection is via ipv4 */ |
| 336 | |
| 337 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 338 | packet_connection_is_ipv4(void) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 339 | { |
| 340 | struct sockaddr_storage to; |
Damien Miller | 6fe375d | 2000-01-23 09:38:00 +1100 | [diff] [blame] | 341 | socklen_t tolen = sizeof(to); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 342 | |
| 343 | memset(&to, 0, sizeof(to)); |
| 344 | if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0) |
| 345 | return 0; |
Damien Miller | aa100c5 | 2002-04-26 16:54:34 +1000 | [diff] [blame] | 346 | if (to.ss_family == AF_INET) |
| 347 | return 1; |
| 348 | #ifdef IPV4_IN_IPV6 |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 349 | if (to.ss_family == AF_INET6 && |
Damien Miller | aa100c5 | 2002-04-26 16:54:34 +1000 | [diff] [blame] | 350 | IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr)) |
| 351 | return 1; |
| 352 | #endif |
| 353 | return 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 354 | } |
| 355 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 356 | /* Sets the connection into non-blocking mode. */ |
| 357 | |
| 358 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 359 | packet_set_nonblocking(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 360 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | /* Set the socket into non-blocking mode. */ |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 362 | set_nonblock(connection_in); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 363 | |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 364 | if (connection_out != connection_in) |
| 365 | set_nonblock(connection_out); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* Returns the socket used for reading. */ |
| 369 | |
| 370 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 371 | packet_get_connection_in(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 372 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 373 | return connection_in; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* Returns the descriptor used for writing. */ |
| 377 | |
| 378 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 379 | packet_get_connection_out(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 380 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 381 | return connection_out; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* Closes the connection and clears and frees internal data structures. */ |
| 385 | |
| 386 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 387 | packet_close(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 388 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 389 | if (!initialized) |
| 390 | return; |
| 391 | initialized = 0; |
| 392 | if (connection_in == connection_out) { |
| 393 | shutdown(connection_out, SHUT_RDWR); |
| 394 | close(connection_out); |
| 395 | } else { |
| 396 | close(connection_in); |
| 397 | close(connection_out); |
| 398 | } |
| 399 | buffer_free(&input); |
| 400 | buffer_free(&output); |
| 401 | buffer_free(&outgoing_packet); |
| 402 | buffer_free(&incoming_packet); |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 403 | if (compression_buffer_ready) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 404 | buffer_free(&compression_buffer); |
| 405 | buffer_compress_uninit(); |
| 406 | } |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 407 | cipher_cleanup(&send_context); |
| 408 | cipher_cleanup(&receive_context); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* Sets remote side protocol flags. */ |
| 412 | |
| 413 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 414 | packet_set_protocol_flags(u_int protocol_flags) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 415 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 416 | remote_protocol_flags = protocol_flags; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* Returns the remote protocol flags set earlier by the above function. */ |
| 420 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 421 | u_int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 422 | packet_get_protocol_flags(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 423 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 424 | return remote_protocol_flags; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 425 | } |
| 426 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 427 | /* |
| 428 | * Starts packet compression from the next packet on in both directions. |
| 429 | * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip. |
| 430 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 431 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 432 | static void |
| 433 | packet_init_compression(void) |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 434 | { |
| 435 | if (compression_buffer_ready == 1) |
| 436 | return; |
| 437 | compression_buffer_ready = 1; |
| 438 | buffer_init(&compression_buffer); |
| 439 | } |
| 440 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 441 | void |
| 442 | packet_start_compression(int level) |
| 443 | { |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 444 | if (packet_compression && !compat20) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 445 | fatal("Compression already enabled."); |
| 446 | packet_compression = 1; |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 447 | packet_init_compression(); |
| 448 | buffer_compress_init_send(level); |
| 449 | buffer_compress_init_recv(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 450 | } |
| 451 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 452 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 453 | * Causes any further packets to be encrypted using the given key. The same |
| 454 | * key is used for both sending and reception. However, both directions are |
| 455 | * encrypted independently of each other. |
| 456 | */ |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 457 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 458 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 459 | packet_set_encryption_key(const u_char *key, u_int keylen, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 460 | int number) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 461 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 462 | Cipher *cipher = cipher_by_number(number); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 463 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 464 | if (cipher == NULL) |
| 465 | fatal("packet_set_encryption_key: unknown cipher number %d", number); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 466 | if (keylen < 20) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 467 | fatal("packet_set_encryption_key: keylen too small: %d", keylen); |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 468 | if (keylen > SSH_SESSION_KEY_LENGTH) |
| 469 | fatal("packet_set_encryption_key: keylen too big: %d", keylen); |
| 470 | memcpy(ssh1_key, key, keylen); |
| 471 | ssh1_keylen = keylen; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 472 | cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT); |
| 473 | cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 474 | } |
| 475 | |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 476 | u_int |
| 477 | packet_get_encryption_key(u_char *key) |
| 478 | { |
| 479 | if (key == NULL) |
| 480 | return (ssh1_keylen); |
| 481 | memcpy(key, ssh1_key, ssh1_keylen); |
| 482 | return (ssh1_keylen); |
| 483 | } |
| 484 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 485 | /* Start constructing a packet to send. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 486 | void |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 487 | packet_start(u_char type) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 488 | { |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 489 | u_char buf[9]; |
| 490 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 491 | |
Ben Lindstrom | 204e488 | 2001-03-05 06:47:00 +0000 | [diff] [blame] | 492 | DBG(debug("packet_start[%d]", type)); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 493 | len = compat20 ? 6 : 9; |
| 494 | memset(buf, 0, len - 1); |
| 495 | buf[len - 1] = type; |
| 496 | buffer_clear(&outgoing_packet); |
| 497 | buffer_append(&outgoing_packet, buf, len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 498 | } |
| 499 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 500 | /* Append payload. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 501 | void |
| 502 | packet_put_char(int value) |
| 503 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 504 | char ch = value; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 505 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 506 | buffer_append(&outgoing_packet, &ch, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 507 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 508 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 509 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 510 | packet_put_int(u_int value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 511 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 512 | buffer_put_int(&outgoing_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 513 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 514 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 515 | void |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 516 | packet_put_string(const void *buf, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 517 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 518 | buffer_put_string(&outgoing_packet, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 519 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 520 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 521 | void |
| 522 | packet_put_cstring(const char *str) |
| 523 | { |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 524 | buffer_put_cstring(&outgoing_packet, str); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 525 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 526 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 527 | void |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 528 | packet_put_raw(const void *buf, u_int len) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 529 | { |
| 530 | buffer_append(&outgoing_packet, buf, len); |
| 531 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 532 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 533 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 534 | packet_put_bignum(BIGNUM * value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 535 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 536 | buffer_put_bignum(&outgoing_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 537 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 538 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 539 | void |
| 540 | packet_put_bignum2(BIGNUM * value) |
| 541 | { |
| 542 | buffer_put_bignum2(&outgoing_packet, value); |
| 543 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 544 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 545 | /* |
| 546 | * Finalizes and sends the packet. If the encryption key has been set, |
| 547 | * encrypts the packet before sending. |
| 548 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 549 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 550 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 551 | packet_send1(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 552 | { |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 553 | u_char buf[8], *cp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 554 | int i, padding, len; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 555 | u_int checksum; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 556 | u_int32_t rnd = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 557 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 558 | /* |
| 559 | * If using packet compression, compress the payload of the outgoing |
| 560 | * packet. |
| 561 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 562 | if (packet_compression) { |
| 563 | buffer_clear(&compression_buffer); |
| 564 | /* Skip padding. */ |
| 565 | buffer_consume(&outgoing_packet, 8); |
| 566 | /* padding */ |
| 567 | buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8); |
| 568 | buffer_compress(&outgoing_packet, &compression_buffer); |
| 569 | buffer_clear(&outgoing_packet); |
| 570 | buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 571 | buffer_len(&compression_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 572 | } |
| 573 | /* Compute packet length without padding (add checksum, remove padding). */ |
| 574 | len = buffer_len(&outgoing_packet) + 4 - 8; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 575 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 576 | /* Insert padding. Initialized to zero in packet_start1() */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 577 | padding = 8 - len % 8; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 578 | if (!send_context.plaintext) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 579 | cp = buffer_ptr(&outgoing_packet); |
| 580 | for (i = 0; i < padding; i++) { |
| 581 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 582 | rnd = arc4random(); |
| 583 | cp[7 - i] = rnd & 0xff; |
| 584 | rnd >>= 8; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | buffer_consume(&outgoing_packet, 8 - padding); |
| 588 | |
| 589 | /* Add check bytes. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 590 | checksum = ssh_crc32(buffer_ptr(&outgoing_packet), |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 591 | buffer_len(&outgoing_packet)); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 592 | put_u32(buf, checksum); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 593 | buffer_append(&outgoing_packet, buf, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 594 | |
| 595 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 596 | fprintf(stderr, "packet_send plain: "); |
| 597 | buffer_dump(&outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 598 | #endif |
| 599 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 600 | /* Append to output. */ |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 601 | put_u32(buf, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 602 | buffer_append(&output, buf, 4); |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 603 | cp = buffer_append_space(&output, buffer_len(&outgoing_packet)); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 604 | cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 605 | buffer_len(&outgoing_packet)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 606 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 607 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 608 | fprintf(stderr, "encrypted: "); |
| 609 | buffer_dump(&output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 610 | #endif |
| 611 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 612 | buffer_clear(&outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 613 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 614 | /* |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 615 | * Note that the packet is now only buffered in output. It won't be |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 616 | * actually sent until packet_write_wait or packet_write_poll is |
| 617 | * called. |
| 618 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 619 | } |
| 620 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 621 | void |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 622 | set_newkeys(int mode) |
| 623 | { |
| 624 | Enc *enc; |
| 625 | Mac *mac; |
| 626 | Comp *comp; |
| 627 | CipherContext *cc; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 628 | u_int64_t *max_blocks; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 629 | int crypt_type; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 630 | |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 631 | debug2("set_newkeys: mode %d", mode); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 632 | |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 633 | if (mode == MODE_OUT) { |
| 634 | cc = &send_context; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 635 | crypt_type = CIPHER_ENCRYPT; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 636 | p_send.packets = p_send.blocks = 0; |
| 637 | max_blocks = &max_blocks_out; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 638 | } else { |
| 639 | cc = &receive_context; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 640 | crypt_type = CIPHER_DECRYPT; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 641 | p_read.packets = p_read.blocks = 0; |
| 642 | max_blocks = &max_blocks_in; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 643 | } |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 644 | if (newkeys[mode] != NULL) { |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 645 | debug("set_newkeys: rekeying"); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 646 | cipher_cleanup(cc); |
Ben Lindstrom | 5ba23b3 | 2001-04-05 02:05:21 +0000 | [diff] [blame] | 647 | enc = &newkeys[mode]->enc; |
| 648 | mac = &newkeys[mode]->mac; |
| 649 | comp = &newkeys[mode]->comp; |
Damien Miller | e45796f | 2007-06-11 14:01:42 +1000 | [diff] [blame] | 650 | mac_clear(mac); |
Ben Lindstrom | 5ba23b3 | 2001-04-05 02:05:21 +0000 | [diff] [blame] | 651 | xfree(enc->name); |
| 652 | xfree(enc->iv); |
| 653 | xfree(enc->key); |
| 654 | xfree(mac->name); |
| 655 | xfree(mac->key); |
| 656 | xfree(comp->name); |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 657 | xfree(newkeys[mode]); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 658 | } |
| 659 | newkeys[mode] = kex_get_newkeys(mode); |
| 660 | if (newkeys[mode] == NULL) |
| 661 | fatal("newkeys: no keys for mode %d", mode); |
| 662 | enc = &newkeys[mode]->enc; |
| 663 | mac = &newkeys[mode]->mac; |
| 664 | comp = &newkeys[mode]->comp; |
Damien Miller | e45796f | 2007-06-11 14:01:42 +1000 | [diff] [blame] | 665 | if (mac_init(mac) == 0) |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 666 | mac->enabled = 1; |
| 667 | DBG(debug("cipher_init_context: %d", mode)); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 668 | cipher_init(cc, enc->cipher, enc->key, enc->key_len, |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 669 | enc->iv, enc->block_size, crypt_type); |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 670 | /* Deleting the keys does not gain extra security */ |
| 671 | /* memset(enc->iv, 0, enc->block_size); |
Darren Tucker | 5f3d5be | 2007-06-05 18:30:18 +1000 | [diff] [blame] | 672 | memset(enc->key, 0, enc->key_len); |
| 673 | memset(mac->key, 0, mac->key_len); */ |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 674 | if ((comp->type == COMP_ZLIB || |
| 675 | (comp->type == COMP_DELAYED && after_authentication)) && |
| 676 | comp->enabled == 0) { |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 677 | packet_init_compression(); |
| 678 | if (mode == MODE_OUT) |
| 679 | buffer_compress_init_send(6); |
| 680 | else |
| 681 | buffer_compress_init_recv(); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 682 | comp->enabled = 1; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 683 | } |
Darren Tucker | 81a0b37 | 2003-07-14 17:31:06 +1000 | [diff] [blame] | 684 | /* |
| 685 | * The 2^(blocksize*2) limit is too expensive for 3DES, |
| 686 | * blowfish, etc, so enforce a 1GB limit for small blocksizes. |
| 687 | */ |
| 688 | if (enc->block_size >= 16) |
| 689 | *max_blocks = (u_int64_t)1 << (enc->block_size*2); |
| 690 | else |
| 691 | *max_blocks = ((u_int64_t)1 << 30) / enc->block_size; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 692 | if (rekey_limit) |
| 693 | *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 696 | /* |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 697 | * Delayed compression for SSH2 is enabled after authentication: |
Darren Tucker | f676c57 | 2006-08-05 18:51:08 +1000 | [diff] [blame] | 698 | * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent, |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 699 | * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received. |
| 700 | */ |
| 701 | static void |
| 702 | packet_enable_delayed_compress(void) |
| 703 | { |
| 704 | Comp *comp = NULL; |
| 705 | int mode; |
| 706 | |
| 707 | /* |
| 708 | * Remember that we are past the authentication step, so rekeying |
| 709 | * with COMP_DELAYED will turn on compression immediately. |
| 710 | */ |
| 711 | after_authentication = 1; |
| 712 | for (mode = 0; mode < MODE_MAX; mode++) { |
Darren Tucker | 4aa665b | 2006-09-21 13:00:25 +1000 | [diff] [blame] | 713 | /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */ |
| 714 | if (newkeys[mode] == NULL) |
| 715 | continue; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 716 | comp = &newkeys[mode]->comp; |
| 717 | if (comp && !comp->enabled && comp->type == COMP_DELAYED) { |
Damien Miller | b5c0125 | 2005-08-12 22:10:28 +1000 | [diff] [blame] | 718 | packet_init_compression(); |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 719 | if (mode == MODE_OUT) |
| 720 | buffer_compress_init_send(6); |
| 721 | else |
| 722 | buffer_compress_init_recv(); |
| 723 | comp->enabled = 1; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /* |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 729 | * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue) |
| 730 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 731 | static void |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 732 | packet_send2_wrapped(void) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 733 | { |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 734 | u_char type, *cp, *macbuf = NULL; |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 735 | u_char padlen, pad; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 736 | u_int packet_length = 0; |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 737 | u_int i, len; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 738 | u_int32_t rnd = 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 739 | Enc *enc = NULL; |
| 740 | Mac *mac = NULL; |
| 741 | Comp *comp = NULL; |
| 742 | int block_size; |
| 743 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 744 | if (newkeys[MODE_OUT] != NULL) { |
| 745 | enc = &newkeys[MODE_OUT]->enc; |
| 746 | mac = &newkeys[MODE_OUT]->mac; |
| 747 | comp = &newkeys[MODE_OUT]->comp; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 748 | } |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 749 | block_size = enc ? enc->block_size : 8; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 750 | |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 751 | cp = buffer_ptr(&outgoing_packet); |
| 752 | type = cp[5]; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 753 | |
| 754 | #ifdef PACKET_DEBUG |
| 755 | fprintf(stderr, "plain: "); |
| 756 | buffer_dump(&outgoing_packet); |
| 757 | #endif |
| 758 | |
| 759 | if (comp && comp->enabled) { |
| 760 | len = buffer_len(&outgoing_packet); |
| 761 | /* skip header, compress only payload */ |
| 762 | buffer_consume(&outgoing_packet, 5); |
| 763 | buffer_clear(&compression_buffer); |
| 764 | buffer_compress(&outgoing_packet, &compression_buffer); |
| 765 | buffer_clear(&outgoing_packet); |
| 766 | buffer_append(&outgoing_packet, "\0\0\0\0\0", 5); |
| 767 | buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer), |
| 768 | buffer_len(&compression_buffer)); |
| 769 | DBG(debug("compression: raw %d compressed %d", len, |
| 770 | buffer_len(&outgoing_packet))); |
| 771 | } |
| 772 | |
| 773 | /* sizeof (packet_len + pad_len + payload) */ |
| 774 | len = buffer_len(&outgoing_packet); |
| 775 | |
| 776 | /* |
| 777 | * calc size of padding, alloc space, get random data, |
| 778 | * minimum padding is 4 bytes |
| 779 | */ |
| 780 | padlen = block_size - (len % block_size); |
| 781 | if (padlen < 4) |
| 782 | padlen += block_size; |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 783 | if (extra_pad) { |
| 784 | /* will wrap if extra_pad+padlen > 255 */ |
| 785 | extra_pad = roundup(extra_pad, block_size); |
| 786 | pad = extra_pad - ((len + padlen) % extra_pad); |
Ben Lindstrom | 8b08d81 | 2002-03-26 02:09:41 +0000 | [diff] [blame] | 787 | debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)", |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 788 | pad, len, padlen, extra_pad); |
| 789 | padlen += pad; |
| 790 | extra_pad = 0; |
| 791 | } |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 792 | cp = buffer_append_space(&outgoing_packet, padlen); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 793 | if (enc && !send_context.plaintext) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 794 | /* random padding */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 795 | for (i = 0; i < padlen; i++) { |
| 796 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 797 | rnd = arc4random(); |
| 798 | cp[i] = rnd & 0xff; |
| 799 | rnd >>= 8; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 800 | } |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 801 | } else { |
| 802 | /* clear padding */ |
| 803 | memset(cp, 0, padlen); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 804 | } |
| 805 | /* packet_length includes payload, padding and padding length field */ |
| 806 | packet_length = buffer_len(&outgoing_packet) - 4; |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 807 | cp = buffer_ptr(&outgoing_packet); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 808 | put_u32(cp, packet_length); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 809 | cp[4] = padlen; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 810 | DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen)); |
| 811 | |
| 812 | /* compute MAC over seqnr and packet(length fields, payload, padding) */ |
| 813 | if (mac && mac->enabled) { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 814 | macbuf = mac_compute(mac, p_send.seqnr, |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 815 | buffer_ptr(&outgoing_packet), |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 816 | buffer_len(&outgoing_packet)); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 817 | DBG(debug("done calc MAC out #%d", p_send.seqnr)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 818 | } |
| 819 | /* encrypt packet and append to output buffer. */ |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 820 | cp = buffer_append_space(&output, buffer_len(&outgoing_packet)); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 821 | cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet), |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 822 | buffer_len(&outgoing_packet)); |
| 823 | /* append unencrypted MAC */ |
| 824 | if (mac && mac->enabled) |
Damien Miller | a0fdce9 | 2006-03-26 14:28:50 +1100 | [diff] [blame] | 825 | buffer_append(&output, macbuf, mac->mac_len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 826 | #ifdef PACKET_DEBUG |
| 827 | fprintf(stderr, "encrypted: "); |
| 828 | buffer_dump(&output); |
| 829 | #endif |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 830 | /* increment sequence number for outgoing packets */ |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 831 | if (++p_send.seqnr == 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 832 | logit("outgoing seqnr wraps around"); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 833 | if (++p_send.packets == 0) |
| 834 | if (!(datafellows & SSH_BUG_NOREKEY)) |
| 835 | fatal("XXX too many packets with same key"); |
| 836 | p_send.blocks += (packet_length + 4) / block_size; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 837 | buffer_clear(&outgoing_packet); |
| 838 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 839 | if (type == SSH2_MSG_NEWKEYS) |
| 840 | set_newkeys(MODE_OUT); |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 841 | else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side) |
| 842 | packet_enable_delayed_compress(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 843 | } |
| 844 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 845 | static void |
| 846 | packet_send2(void) |
| 847 | { |
| 848 | static int rekeying = 0; |
| 849 | struct packet *p; |
| 850 | u_char type, *cp; |
| 851 | |
| 852 | cp = buffer_ptr(&outgoing_packet); |
| 853 | type = cp[5]; |
| 854 | |
| 855 | /* during rekeying we can only send key exchange messages */ |
| 856 | if (rekeying) { |
| 857 | if (!((type >= SSH2_MSG_TRANSPORT_MIN) && |
| 858 | (type <= SSH2_MSG_TRANSPORT_MAX))) { |
| 859 | debug("enqueue packet: %u", type); |
| 860 | p = xmalloc(sizeof(*p)); |
| 861 | p->type = type; |
| 862 | memcpy(&p->payload, &outgoing_packet, sizeof(Buffer)); |
| 863 | buffer_init(&outgoing_packet); |
| 864 | TAILQ_INSERT_TAIL(&outgoing, p, next); |
| 865 | return; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | /* rekeying starts with sending KEXINIT */ |
| 870 | if (type == SSH2_MSG_KEXINIT) |
| 871 | rekeying = 1; |
| 872 | |
| 873 | packet_send2_wrapped(); |
| 874 | |
| 875 | /* after a NEWKEYS message we can send the complete queue */ |
| 876 | if (type == SSH2_MSG_NEWKEYS) { |
| 877 | rekeying = 0; |
| 878 | while ((p = TAILQ_FIRST(&outgoing))) { |
| 879 | type = p->type; |
| 880 | debug("dequeue packet: %u", type); |
| 881 | buffer_free(&outgoing_packet); |
| 882 | memcpy(&outgoing_packet, &p->payload, |
| 883 | sizeof(Buffer)); |
| 884 | TAILQ_REMOVE(&outgoing, p, next); |
| 885 | xfree(p); |
| 886 | packet_send2_wrapped(); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 891 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 892 | packet_send(void) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 893 | { |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 894 | if (compat20) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 895 | packet_send2(); |
| 896 | else |
| 897 | packet_send1(); |
| 898 | DBG(debug("packet_send done")); |
| 899 | } |
| 900 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 901 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 902 | * Waits until a packet has been received, and returns its type. Note that |
| 903 | * no other data is processed until this returns, so this function should not |
| 904 | * be used during the interactive session. |
| 905 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 906 | |
| 907 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 908 | packet_read_seqnr(u_int32_t *seqnr_p) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 909 | { |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 910 | int type, len, ret, ms_remain; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 911 | fd_set *setp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 912 | char buf[8192]; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 913 | struct timeval timeout, start, *timeoutp = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 914 | |
Darren Tucker | 99bb761 | 2008-06-13 22:02:50 +1000 | [diff] [blame] | 915 | DBG(debug("packet_read()")); |
| 916 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 917 | setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS), |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 918 | sizeof(fd_mask)); |
| 919 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 920 | /* Since we are blocking, ensure that all written packets have been sent. */ |
| 921 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 922 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 923 | /* Stay in the loop until we have received a complete packet. */ |
| 924 | for (;;) { |
| 925 | /* Try to read a packet from the buffer. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 926 | type = packet_read_poll_seqnr(seqnr_p); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 927 | if (!compat20 && ( |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 928 | type == SSH_SMSG_SUCCESS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 929 | || type == SSH_SMSG_FAILURE |
| 930 | || type == SSH_CMSG_EOF |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 931 | || type == SSH_CMSG_EXIT_CONFIRMATION)) |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 932 | packet_check_eom(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 933 | /* If we got a packet, return it. */ |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 934 | if (type != SSH_MSG_NONE) { |
| 935 | xfree(setp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 936 | return type; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 937 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 938 | /* |
| 939 | * Otherwise, wait for some data to arrive, add it to the |
| 940 | * buffer, and try again. |
| 941 | */ |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 942 | memset(setp, 0, howmany(connection_in + 1, NFDBITS) * |
| 943 | sizeof(fd_mask)); |
| 944 | FD_SET(connection_in, setp); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 945 | |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 946 | if (packet_timeout_ms > 0) { |
| 947 | ms_remain = packet_timeout_ms; |
| 948 | timeoutp = &timeout; |
| 949 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 950 | /* Wait for some data to arrive. */ |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 951 | for (;;) { |
| 952 | if (packet_timeout_ms != -1) { |
| 953 | ms_to_timeval(&timeout, ms_remain); |
| 954 | gettimeofday(&start, NULL); |
| 955 | } |
| 956 | if ((ret = select(connection_in + 1, setp, NULL, |
| 957 | NULL, timeoutp)) >= 0) |
| 958 | break; |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 959 | if (errno != EAGAIN && errno != EINTR && |
| 960 | errno != EWOULDBLOCK) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 961 | break; |
| 962 | if (packet_timeout_ms == -1) |
| 963 | continue; |
| 964 | ms_subtract_diff(&start, &ms_remain); |
| 965 | if (ms_remain <= 0) { |
| 966 | ret = 0; |
| 967 | break; |
| 968 | } |
| 969 | } |
| 970 | if (ret == 0) { |
| 971 | logit("Connection to %.200s timed out while " |
| 972 | "waiting to read", get_remote_ipaddr()); |
| 973 | cleanup_exit(255); |
| 974 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 975 | /* Read data from the socket. */ |
| 976 | len = read(connection_in, buf, sizeof(buf)); |
Damien Miller | 5e7c10e | 1999-12-16 13:18:04 +1100 | [diff] [blame] | 977 | if (len == 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 978 | logit("Connection closed by %.200s", get_remote_ipaddr()); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 979 | cleanup_exit(255); |
Damien Miller | 5e7c10e | 1999-12-16 13:18:04 +1100 | [diff] [blame] | 980 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 981 | if (len < 0) |
| 982 | fatal("Read from socket failed: %.100s", strerror(errno)); |
| 983 | /* Append it to the buffer. */ |
| 984 | packet_process_incoming(buf, len); |
| 985 | } |
| 986 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 987 | } |
| 988 | |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 989 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 990 | packet_read(void) |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 991 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 992 | return packet_read_seqnr(NULL); |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 993 | } |
| 994 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 995 | /* |
| 996 | * Waits until a packet has been received, verifies that its type matches |
| 997 | * that given, and gives a fatal error and exits if there is a mismatch. |
| 998 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 999 | |
| 1000 | void |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1001 | packet_read_expect(int expected_type) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1002 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1003 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1004 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1005 | type = packet_read(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1006 | if (type != expected_type) |
| 1007 | packet_disconnect("Protocol error: expected packet type %d, got %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1008 | expected_type, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /* 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] | 1012 | * packet_process_incoming. If so, reads the packet; otherwise returns |
| 1013 | * SSH_MSG_NONE. This does not wait for data from the connection. |
| 1014 | * |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1015 | * SSH_MSG_DISCONNECT is handled specially here. Also, |
| 1016 | * SSH_MSG_IGNORE messages are skipped by this function and are never returned |
| 1017 | * to higher levels. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1018 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1019 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1020 | static int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1021 | packet_read_poll1(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1022 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1023 | u_int len, padded_len; |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1024 | u_char *cp, type; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1025 | u_int checksum, stored_checksum; |
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 | /* Check if input size is less than minimum packet size. */ |
| 1028 | if (buffer_len(&input) < 4 + 8) |
| 1029 | return SSH_MSG_NONE; |
| 1030 | /* Get length of incoming packet. */ |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1031 | cp = buffer_ptr(&input); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1032 | len = get_u32(cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1033 | if (len < 1 + 2 + 2 || len > 256 * 1024) |
Ben Lindstrom | 0cc2a47 | 2002-11-09 15:41:39 +0000 | [diff] [blame] | 1034 | packet_disconnect("Bad packet length %u.", len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1035 | padded_len = (len + 8) & ~7; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1036 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1037 | /* Check if the packet has been entirely received. */ |
| 1038 | if (buffer_len(&input) < 4 + padded_len) |
| 1039 | return SSH_MSG_NONE; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1040 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1041 | /* The entire packet is in buffer. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1042 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1043 | /* Consume packet length. */ |
| 1044 | buffer_consume(&input, 4); |
| 1045 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1046 | /* |
| 1047 | * Cryptographic attack detector for ssh |
| 1048 | * (C)1998 CORE-SDI, Buenos Aires Argentina |
| 1049 | * Ariel Futoransky(futo@core-sdi.com) |
| 1050 | */ |
Damien Miller | 3c9c1fb | 2006-09-17 06:08:53 +1000 | [diff] [blame] | 1051 | if (!receive_context.plaintext) { |
| 1052 | switch (detect_attack(buffer_ptr(&input), padded_len)) { |
| 1053 | case DEATTACK_DETECTED: |
| 1054 | packet_disconnect("crc32 compensation attack: " |
| 1055 | "network attack detected"); |
| 1056 | case DEATTACK_DOS_DETECTED: |
| 1057 | packet_disconnect("deattack denial of " |
| 1058 | "service detected"); |
| 1059 | } |
| 1060 | } |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1061 | |
| 1062 | /* Decrypt data to incoming_packet. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1063 | buffer_clear(&incoming_packet); |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1064 | cp = buffer_append_space(&incoming_packet, padded_len); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 1065 | cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1066 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1067 | buffer_consume(&input, padded_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1068 | |
| 1069 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1070 | fprintf(stderr, "read_poll plain: "); |
| 1071 | buffer_dump(&incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1072 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1073 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1074 | /* Compute packet checksum. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 1075 | checksum = ssh_crc32(buffer_ptr(&incoming_packet), |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1076 | buffer_len(&incoming_packet) - 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1077 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1078 | /* Skip padding. */ |
| 1079 | buffer_consume(&incoming_packet, 8 - len % 8); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 1080 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1081 | /* Test check bytes. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1082 | if (len != buffer_len(&incoming_packet)) |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1083 | packet_disconnect("packet_read_poll1: len %d != buffer_len %d.", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1084 | len, buffer_len(&incoming_packet)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1085 | |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1086 | cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1087 | stored_checksum = get_u32(cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1088 | if (checksum != stored_checksum) |
| 1089 | packet_disconnect("Corrupted check bytes on input."); |
| 1090 | buffer_consume_end(&incoming_packet, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1091 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1092 | if (packet_compression) { |
| 1093 | buffer_clear(&compression_buffer); |
| 1094 | buffer_uncompress(&incoming_packet, &compression_buffer); |
| 1095 | buffer_clear(&incoming_packet); |
| 1096 | buffer_append(&incoming_packet, buffer_ptr(&compression_buffer), |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1097 | buffer_len(&compression_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1098 | } |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1099 | type = buffer_get_char(&incoming_packet); |
Darren Tucker | b2694f0 | 2004-11-05 20:27:54 +1100 | [diff] [blame] | 1100 | if (type < SSH_MSG_MIN || type > SSH_MSG_MAX) |
| 1101 | packet_disconnect("Invalid ssh1 packet type: %d", type); |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1102 | return type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1103 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1104 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1105 | static int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1106 | packet_read_poll2(u_int32_t *seqnr_p) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1107 | { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1108 | static u_int packet_length = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1109 | u_int padlen, need; |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1110 | u_char *macbuf, *cp, type; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1111 | u_int maclen, block_size; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1112 | Enc *enc = NULL; |
| 1113 | Mac *mac = NULL; |
| 1114 | Comp *comp = NULL; |
| 1115 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 1116 | if (newkeys[MODE_IN] != NULL) { |
| 1117 | enc = &newkeys[MODE_IN]->enc; |
| 1118 | mac = &newkeys[MODE_IN]->mac; |
| 1119 | comp = &newkeys[MODE_IN]->comp; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1120 | } |
| 1121 | maclen = mac && mac->enabled ? mac->mac_len : 0; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 1122 | block_size = enc ? enc->block_size : 8; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1123 | |
| 1124 | if (packet_length == 0) { |
| 1125 | /* |
| 1126 | * check if input size is less than the cipher block size, |
| 1127 | * decrypt first block and extract length of incoming packet |
| 1128 | */ |
| 1129 | if (buffer_len(&input) < block_size) |
| 1130 | return SSH_MSG_NONE; |
| 1131 | buffer_clear(&incoming_packet); |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1132 | cp = buffer_append_space(&incoming_packet, block_size); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 1133 | cipher_crypt(&receive_context, cp, buffer_ptr(&input), |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1134 | block_size); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1135 | cp = buffer_ptr(&incoming_packet); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1136 | packet_length = get_u32(cp); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1137 | if (packet_length < 1 + 4 || packet_length > 256 * 1024) { |
Darren Tucker | a8151da | 2003-09-22 21:06:46 +1000 | [diff] [blame] | 1138 | #ifdef PACKET_DEBUG |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1139 | buffer_dump(&incoming_packet); |
Darren Tucker | a8151da | 2003-09-22 21:06:46 +1000 | [diff] [blame] | 1140 | #endif |
Ben Lindstrom | 0cc2a47 | 2002-11-09 15:41:39 +0000 | [diff] [blame] | 1141 | packet_disconnect("Bad packet length %u.", packet_length); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1142 | } |
Ben Lindstrom | 0cc2a47 | 2002-11-09 15:41:39 +0000 | [diff] [blame] | 1143 | DBG(debug("input: packet len %u", packet_length+4)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1144 | buffer_consume(&input, block_size); |
| 1145 | } |
| 1146 | /* we have a partial packet of block_size bytes */ |
| 1147 | need = 4 + packet_length - block_size; |
| 1148 | DBG(debug("partial packet %d, need %d, maclen %d", block_size, |
| 1149 | need, maclen)); |
| 1150 | if (need % block_size != 0) |
| 1151 | fatal("padding error: need %d block %d mod %d", |
| 1152 | need, block_size, need % block_size); |
| 1153 | /* |
| 1154 | * check if the entire packet has been received and |
| 1155 | * decrypt into incoming_packet |
| 1156 | */ |
| 1157 | if (buffer_len(&input) < need + maclen) |
| 1158 | return SSH_MSG_NONE; |
| 1159 | #ifdef PACKET_DEBUG |
| 1160 | fprintf(stderr, "read_poll enc/full: "); |
| 1161 | buffer_dump(&input); |
| 1162 | #endif |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1163 | cp = buffer_append_space(&incoming_packet, need); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 1164 | cipher_crypt(&receive_context, cp, buffer_ptr(&input), need); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1165 | buffer_consume(&input, need); |
| 1166 | /* |
| 1167 | * compute MAC over seqnr and packet, |
| 1168 | * increment sequence number for incoming packet |
| 1169 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1170 | if (mac && mac->enabled) { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1171 | macbuf = mac_compute(mac, p_read.seqnr, |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 1172 | buffer_ptr(&incoming_packet), |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1173 | buffer_len(&incoming_packet)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1174 | if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1175 | packet_disconnect("Corrupted MAC on input."); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1176 | DBG(debug("MAC #%d ok", p_read.seqnr)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1177 | buffer_consume(&input, mac->mac_len); |
| 1178 | } |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1179 | if (seqnr_p != NULL) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1180 | *seqnr_p = p_read.seqnr; |
| 1181 | if (++p_read.seqnr == 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1182 | logit("incoming seqnr wraps around"); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1183 | if (++p_read.packets == 0) |
| 1184 | if (!(datafellows & SSH_BUG_NOREKEY)) |
| 1185 | fatal("XXX too many packets with same key"); |
| 1186 | p_read.blocks += (packet_length + 4) / block_size; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1187 | |
| 1188 | /* get padlen */ |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1189 | cp = buffer_ptr(&incoming_packet); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1190 | padlen = cp[4]; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1191 | DBG(debug("input: padlen %d", padlen)); |
| 1192 | if (padlen < 4) |
| 1193 | packet_disconnect("Corrupted padlen %d on input.", padlen); |
| 1194 | |
| 1195 | /* skip packet size + padlen, discard padding */ |
| 1196 | buffer_consume(&incoming_packet, 4 + 1); |
| 1197 | buffer_consume_end(&incoming_packet, padlen); |
| 1198 | |
| 1199 | DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet))); |
| 1200 | if (comp && comp->enabled) { |
| 1201 | buffer_clear(&compression_buffer); |
| 1202 | buffer_uncompress(&incoming_packet, &compression_buffer); |
| 1203 | buffer_clear(&incoming_packet); |
| 1204 | buffer_append(&incoming_packet, buffer_ptr(&compression_buffer), |
| 1205 | buffer_len(&compression_buffer)); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1206 | DBG(debug("input: len after de-compress %d", |
| 1207 | buffer_len(&incoming_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1208 | } |
| 1209 | /* |
| 1210 | * get packet type, implies consume. |
| 1211 | * return length of payload (without type field) |
| 1212 | */ |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1213 | type = buffer_get_char(&incoming_packet); |
Darren Tucker | b2694f0 | 2004-11-05 20:27:54 +1100 | [diff] [blame] | 1214 | if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN) |
| 1215 | packet_disconnect("Invalid ssh2 packet type: %d", type); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 1216 | if (type == SSH2_MSG_NEWKEYS) |
| 1217 | set_newkeys(MODE_IN); |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1218 | else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side) |
| 1219 | packet_enable_delayed_compress(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1220 | #ifdef PACKET_DEBUG |
Ben Lindstrom | 204e488 | 2001-03-05 06:47:00 +0000 | [diff] [blame] | 1221 | fprintf(stderr, "read/plain[%d]:\r\n", type); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1222 | buffer_dump(&incoming_packet); |
| 1223 | #endif |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1224 | /* reset for next packet */ |
| 1225 | packet_length = 0; |
| 1226 | return type; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1230 | packet_read_poll_seqnr(u_int32_t *seqnr_p) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1231 | { |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1232 | u_int reason, seqnr; |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1233 | u_char type; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1234 | char *msg; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1235 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1236 | for (;;) { |
| 1237 | if (compat20) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1238 | type = packet_read_poll2(seqnr_p); |
Darren Tucker | 136e56f | 2008-06-08 12:49:30 +1000 | [diff] [blame] | 1239 | if (type) { |
| 1240 | keep_alive_timeouts = 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1241 | DBG(debug("received packet type %d", type)); |
Darren Tucker | 136e56f | 2008-06-08 12:49:30 +1000 | [diff] [blame] | 1242 | } |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1243 | switch (type) { |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1244 | case SSH2_MSG_IGNORE: |
Damien Miller | 58226f6 | 2008-03-07 18:33:30 +1100 | [diff] [blame] | 1245 | debug3("Received SSH2_MSG_IGNORE"); |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1246 | break; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1247 | case SSH2_MSG_DEBUG: |
| 1248 | packet_get_char(); |
| 1249 | msg = packet_get_string(NULL); |
| 1250 | debug("Remote: %.900s", msg); |
| 1251 | xfree(msg); |
| 1252 | msg = packet_get_string(NULL); |
| 1253 | xfree(msg); |
| 1254 | break; |
| 1255 | case SSH2_MSG_DISCONNECT: |
| 1256 | reason = packet_get_int(); |
| 1257 | msg = packet_get_string(NULL); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1258 | logit("Received disconnect from %s: %u: %.400s", |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1259 | get_remote_ipaddr(), reason, msg); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1260 | xfree(msg); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1261 | cleanup_exit(255); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1262 | break; |
Damien Miller | 659811f | 2002-01-22 23:23:11 +1100 | [diff] [blame] | 1263 | case SSH2_MSG_UNIMPLEMENTED: |
| 1264 | seqnr = packet_get_int(); |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1265 | debug("Received SSH2_MSG_UNIMPLEMENTED for %u", |
| 1266 | seqnr); |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1267 | break; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1268 | default: |
| 1269 | return type; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1270 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1271 | } else { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1272 | type = packet_read_poll1(); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1273 | switch (type) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1274 | case SSH_MSG_IGNORE: |
| 1275 | break; |
| 1276 | case SSH_MSG_DEBUG: |
| 1277 | msg = packet_get_string(NULL); |
| 1278 | debug("Remote: %.900s", msg); |
| 1279 | xfree(msg); |
| 1280 | break; |
| 1281 | case SSH_MSG_DISCONNECT: |
| 1282 | msg = packet_get_string(NULL); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1283 | logit("Received disconnect from %s: %.400s", |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1284 | get_remote_ipaddr(), msg); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1285 | cleanup_exit(255); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1286 | break; |
| 1287 | default: |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1288 | if (type) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1289 | DBG(debug("received packet type %d", type)); |
| 1290 | return type; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1291 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 | |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1296 | int |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1297 | packet_read_poll(void) |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1298 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1299 | return packet_read_poll_seqnr(NULL); |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1300 | } |
| 1301 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1302 | /* |
| 1303 | * Buffers the given amount of input characters. This is intended to be used |
| 1304 | * together with packet_read_poll. |
| 1305 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1306 | |
| 1307 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1308 | packet_process_incoming(const char *buf, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1309 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1310 | buffer_append(&input, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /* Returns a character from the packet. */ |
| 1314 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1315 | u_int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1316 | packet_get_char(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1317 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1318 | char ch; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1319 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1320 | buffer_get(&incoming_packet, &ch, 1); |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1321 | return (u_char) ch; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | /* Returns an integer from the packet data. */ |
| 1325 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1326 | u_int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1327 | packet_get_int(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1328 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1329 | return buffer_get_int(&incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1330 | } |
| 1331 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1332 | /* |
| 1333 | * Returns an arbitrary precision integer from the packet data. The integer |
| 1334 | * must have been initialized before this call. |
| 1335 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1336 | |
| 1337 | void |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 1338 | packet_get_bignum(BIGNUM * value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1339 | { |
Damien Miller | 76e1e36 | 2002-01-22 23:15:57 +1100 | [diff] [blame] | 1340 | buffer_get_bignum(&incoming_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1341 | } |
| 1342 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1343 | void |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 1344 | packet_get_bignum2(BIGNUM * value) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1345 | { |
Damien Miller | 76e1e36 | 2002-01-22 23:15:57 +1100 | [diff] [blame] | 1346 | buffer_get_bignum2(&incoming_packet, value); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1347 | } |
| 1348 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1349 | void * |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1350 | packet_get_raw(u_int *length_ptr) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1351 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1352 | u_int bytes = buffer_len(&incoming_packet); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1353 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1354 | if (length_ptr != NULL) |
| 1355 | *length_ptr = bytes; |
| 1356 | return buffer_ptr(&incoming_packet); |
| 1357 | } |
| 1358 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1359 | int |
| 1360 | packet_remaining(void) |
| 1361 | { |
| 1362 | return buffer_len(&incoming_packet); |
| 1363 | } |
| 1364 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1365 | /* |
| 1366 | * Returns a string from the packet data. The string is allocated using |
| 1367 | * xmalloc; it is the responsibility of the calling program to free it when |
| 1368 | * no longer needed. The length_ptr argument may be NULL, or point to an |
| 1369 | * integer into which the length of the string is stored. |
| 1370 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1371 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1372 | void * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1373 | packet_get_string(u_int *length_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1374 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1375 | return buffer_get_string(&incoming_packet, length_ptr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1376 | } |
| 1377 | |
Damien Miller | db255ca | 2008-05-19 14:59:37 +1000 | [diff] [blame] | 1378 | void * |
| 1379 | packet_get_string_ptr(u_int *length_ptr) |
| 1380 | { |
| 1381 | return buffer_get_string_ptr(&incoming_packet, length_ptr); |
| 1382 | } |
| 1383 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1384 | /* |
| 1385 | * Sends a diagnostic message from the server to the client. This message |
| 1386 | * can be sent at any time (but not while constructing another message). The |
| 1387 | * message is printed immediately, but only if the client is being executed |
| 1388 | * in verbose mode. These messages are primarily intended to ease debugging |
| 1389 | * authentication problems. The length of the formatted message must not |
| 1390 | * exceed 1024 bytes. This will automatically call packet_write_wait. |
| 1391 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1392 | |
| 1393 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1394 | packet_send_debug(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1395 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1396 | char buf[1024]; |
| 1397 | va_list args; |
| 1398 | |
Ben Lindstrom | a14ee47 | 2000-12-07 01:24:58 +0000 | [diff] [blame] | 1399 | if (compat20 && (datafellows & SSH_BUG_DEBUG)) |
| 1400 | return; |
| 1401 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1402 | va_start(args, fmt); |
| 1403 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 1404 | va_end(args); |
| 1405 | |
Damien Miller | 7c8af4f | 2000-05-01 08:24:07 +1000 | [diff] [blame] | 1406 | if (compat20) { |
| 1407 | packet_start(SSH2_MSG_DEBUG); |
| 1408 | packet_put_char(0); /* bool: always display */ |
| 1409 | packet_put_cstring(buf); |
| 1410 | packet_put_cstring(""); |
| 1411 | } else { |
| 1412 | packet_start(SSH_MSG_DEBUG); |
| 1413 | packet_put_cstring(buf); |
| 1414 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1415 | packet_send(); |
| 1416 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1417 | } |
| 1418 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1419 | /* |
| 1420 | * Logs the error plus constructs and sends a disconnect packet, closes the |
| 1421 | * connection, and exits. This function never returns. The error message |
| 1422 | * should not contain a newline. The length of the formatted message must |
| 1423 | * not exceed 1024 bytes. |
| 1424 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1425 | |
| 1426 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1427 | packet_disconnect(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1428 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1429 | char buf[1024]; |
| 1430 | va_list args; |
| 1431 | static int disconnecting = 0; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1432 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1433 | if (disconnecting) /* Guard against recursive invocations. */ |
| 1434 | fatal("packet_disconnect called recursively."); |
| 1435 | disconnecting = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1436 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1437 | /* |
| 1438 | * Format the message. Note that the caller must make sure the |
| 1439 | * message is of limited size. |
| 1440 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1441 | va_start(args, fmt); |
| 1442 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 1443 | va_end(args); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1444 | |
Ben Lindstrom | 9bda7ae | 2002-11-09 15:46:24 +0000 | [diff] [blame] | 1445 | /* Display the error locally */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1446 | logit("Disconnecting: %.100s", buf); |
Ben Lindstrom | 9bda7ae | 2002-11-09 15:46:24 +0000 | [diff] [blame] | 1447 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1448 | /* 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] | 1449 | if (compat20) { |
| 1450 | packet_start(SSH2_MSG_DISCONNECT); |
| 1451 | packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR); |
| 1452 | packet_put_cstring(buf); |
| 1453 | packet_put_cstring(""); |
| 1454 | } else { |
| 1455 | packet_start(SSH_MSG_DISCONNECT); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 1456 | packet_put_cstring(buf); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1457 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1458 | packet_send(); |
| 1459 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1460 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1461 | /* Stop listening for connections. */ |
Ben Lindstrom | 601e436 | 2001-06-21 03:19:23 +0000 | [diff] [blame] | 1462 | channel_close_all(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1463 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1464 | /* Close the connection. */ |
| 1465 | packet_close(); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1466 | cleanup_exit(255); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1467 | } |
| 1468 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1469 | /* 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] | 1470 | |
| 1471 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1472 | packet_write_poll(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1473 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1474 | int len = buffer_len(&output); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1475 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1476 | if (len > 0) { |
| 1477 | len = write(connection_out, buffer_ptr(&output), len); |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1478 | if (len == -1) { |
| 1479 | if (errno == EINTR || errno == EAGAIN || |
| 1480 | errno == EWOULDBLOCK) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1481 | return; |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1482 | fatal("Write failed: %.100s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1483 | } |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1484 | if (len == 0) |
| 1485 | fatal("Write connection closed"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1486 | buffer_consume(&output, len); |
| 1487 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1488 | } |
| 1489 | |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1490 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1491 | /* |
| 1492 | * Calls packet_write_poll repeatedly until all pending output data has been |
| 1493 | * written. |
| 1494 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1495 | |
| 1496 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1497 | packet_write_wait(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1498 | { |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1499 | fd_set *setp; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1500 | int ret, ms_remain; |
| 1501 | struct timeval start, timeout, *timeoutp = NULL; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1502 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1503 | setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS), |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1504 | sizeof(fd_mask)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1505 | packet_write_poll(); |
| 1506 | while (packet_have_data_to_write()) { |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1507 | memset(setp, 0, howmany(connection_out + 1, NFDBITS) * |
| 1508 | sizeof(fd_mask)); |
| 1509 | FD_SET(connection_out, setp); |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1510 | |
| 1511 | if (packet_timeout_ms > 0) { |
| 1512 | ms_remain = packet_timeout_ms; |
| 1513 | timeoutp = &timeout; |
| 1514 | } |
| 1515 | for (;;) { |
| 1516 | if (packet_timeout_ms != -1) { |
| 1517 | ms_to_timeval(&timeout, ms_remain); |
| 1518 | gettimeofday(&start, NULL); |
| 1519 | } |
| 1520 | if ((ret = select(connection_out + 1, NULL, setp, |
| 1521 | NULL, timeoutp)) >= 0) |
| 1522 | break; |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 1523 | if (errno != EAGAIN && errno != EINTR && |
| 1524 | errno != EWOULDBLOCK) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1525 | break; |
| 1526 | if (packet_timeout_ms == -1) |
| 1527 | continue; |
| 1528 | ms_subtract_diff(&start, &ms_remain); |
| 1529 | if (ms_remain <= 0) { |
| 1530 | ret = 0; |
| 1531 | break; |
| 1532 | } |
| 1533 | } |
| 1534 | if (ret == 0) { |
| 1535 | logit("Connection to %.200s timed out while " |
| 1536 | "waiting to write", get_remote_ipaddr()); |
| 1537 | cleanup_exit(255); |
| 1538 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1539 | packet_write_poll(); |
| 1540 | } |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1541 | xfree(setp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* Returns true if there is buffered data to write to the connection. */ |
| 1545 | |
| 1546 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1547 | packet_have_data_to_write(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1548 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1549 | return buffer_len(&output) != 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | /* Returns true if there is not too much data to write to the connection. */ |
| 1553 | |
| 1554 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1555 | packet_not_very_much_data_to_write(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1556 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1557 | if (interactive_mode) |
| 1558 | return buffer_len(&output) < 16384; |
| 1559 | else |
| 1560 | return buffer_len(&output) < 128 * 1024; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1561 | } |
| 1562 | |
Ben Lindstrom | c8a49d7 | 2003-04-02 15:18:22 +0000 | [diff] [blame] | 1563 | |
Ben Lindstrom | faa1ea8 | 2002-12-23 02:42:52 +0000 | [diff] [blame] | 1564 | static void |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1565 | packet_set_tos(int interactive) |
| 1566 | { |
Damien Miller | 5924ceb | 2003-11-22 15:02:42 +1100 | [diff] [blame] | 1567 | #if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN) |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1568 | int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT; |
| 1569 | |
| 1570 | if (!packet_connection_is_on_socket() || |
| 1571 | !packet_connection_is_ipv4()) |
| 1572 | return; |
| 1573 | if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos, |
| 1574 | sizeof(tos)) < 0) |
| 1575 | error("setsockopt IP_TOS %d: %.100s:", |
| 1576 | tos, strerror(errno)); |
Ben Lindstrom | c8a49d7 | 2003-04-02 15:18:22 +0000 | [diff] [blame] | 1577 | #endif |
Damien Miller | 5924ceb | 2003-11-22 15:02:42 +1100 | [diff] [blame] | 1578 | } |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1579 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1580 | /* Informs that the current session is interactive. Sets IP flags for that. */ |
| 1581 | |
| 1582 | void |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1583 | packet_set_interactive(int interactive) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1584 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1585 | static int called = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1586 | |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1587 | if (called) |
| 1588 | return; |
| 1589 | called = 1; |
| 1590 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1591 | /* Record that we are in interactive mode. */ |
| 1592 | interactive_mode = interactive; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1593 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1594 | /* Only set socket options if using a socket. */ |
| 1595 | if (!packet_connection_is_on_socket()) |
Ben Lindstrom | 93b6b77 | 2003-04-27 17:55:33 +0000 | [diff] [blame] | 1596 | return; |
Damien Miller | 314dd4b | 2006-03-15 12:05:22 +1100 | [diff] [blame] | 1597 | set_nodelay(connection_in); |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 1598 | packet_set_tos(interactive); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /* Returns true if the current connection is interactive. */ |
| 1602 | |
| 1603 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1604 | packet_is_interactive(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1605 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1606 | return interactive_mode; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1607 | } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1608 | |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 1609 | int |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 1610 | packet_set_maxsize(u_int s) |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1611 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1612 | static int called = 0; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1613 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1614 | if (called) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1615 | logit("packet_set_maxsize: called twice: old %d new %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1616 | max_packet_size, s); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1617 | return -1; |
| 1618 | } |
| 1619 | if (s < 4 * 1024 || s > 1024 * 1024) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1620 | logit("packet_set_maxsize: bad size %d", s); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1621 | return -1; |
| 1622 | } |
Ben Lindstrom | ae3de4b | 2001-10-03 17:10:17 +0000 | [diff] [blame] | 1623 | called = 1; |
Ben Lindstrom | 16d45b3 | 2001-06-13 04:39:18 +0000 | [diff] [blame] | 1624 | debug("packet_set_maxsize: setting to %d", s); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1625 | max_packet_size = s; |
| 1626 | return s; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1627 | } |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1628 | |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1629 | /* roundup current message to pad bytes */ |
| 1630 | void |
| 1631 | packet_add_padding(u_char pad) |
| 1632 | { |
| 1633 | extra_pad = pad; |
| 1634 | } |
| 1635 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1636 | /* |
| 1637 | * 9.2. Ignored Data Message |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1638 | * |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1639 | * byte SSH_MSG_IGNORE |
| 1640 | * string data |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1641 | * |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1642 | * All implementations MUST understand (and ignore) this message at any |
| 1643 | * time (after receiving the protocol version). No implementation is |
| 1644 | * required to send them. This message can be used as an additional |
| 1645 | * protection measure against advanced traffic analysis techniques. |
| 1646 | */ |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1647 | void |
| 1648 | packet_send_ignore(int nbytes) |
| 1649 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1650 | u_int32_t rnd = 0; |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1651 | int i; |
| 1652 | |
| 1653 | packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE); |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1654 | packet_put_int(nbytes); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1655 | for (i = 0; i < nbytes; i++) { |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1656 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1657 | rnd = arc4random(); |
Damien Miller | 8ba29fe | 2006-03-26 14:25:19 +1100 | [diff] [blame] | 1658 | packet_put_char((u_char)rnd & 0xff); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1659 | rnd >>= 8; |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1660 | } |
| 1661 | } |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1662 | |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 1663 | #define MAX_PACKETS (1U<<31) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1664 | int |
| 1665 | packet_need_rekeying(void) |
| 1666 | { |
| 1667 | if (datafellows & SSH_BUG_NOREKEY) |
| 1668 | return 0; |
| 1669 | return |
| 1670 | (p_send.packets > MAX_PACKETS) || |
| 1671 | (p_read.packets > MAX_PACKETS) || |
| 1672 | (max_blocks_out && (p_send.blocks > max_blocks_out)) || |
| 1673 | (max_blocks_in && (p_read.blocks > max_blocks_in)); |
| 1674 | } |
| 1675 | |
| 1676 | void |
| 1677 | packet_set_rekey_limit(u_int32_t bytes) |
| 1678 | { |
| 1679 | rekey_limit = bytes; |
| 1680 | } |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1681 | |
| 1682 | void |
| 1683 | packet_set_server(void) |
| 1684 | { |
| 1685 | server_side = 1; |
| 1686 | } |
| 1687 | |
| 1688 | void |
| 1689 | packet_set_authenticated(void) |
| 1690 | { |
| 1691 | after_authentication = 1; |
| 1692 | } |