blob: a1e7d32f677a54e77183568a9cb6b5a3596c99da [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains code implementing the packet protocol and communication
6 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100013 *
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000016 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110040
Damien Millera0898b82003-04-09 21:05:52 +100041#include "openbsd-compat/sys-queue.h"
Damien Miller68f8e992006-03-15 11:24:12 +110042#include <netinet/in_systm.h>
43#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "xmalloc.h"
46#include "buffer.h"
47#include "packet.h"
48#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "getput.h"
51
52#include "compress.h"
53#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000054#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
Damien Miller33b13562000-04-04 14:38:59 +100056#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100058#include "ssh2.h"
59
Damien Miller874d77b2000-10-14 16:23:11 +110060#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100061#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000062#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
64#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110065#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000066#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100067
68#ifdef PACKET_DEBUG
69#define DBG(x) x
70#else
71#define DBG(x)
72#endif
73
Damien Miller5428f641999-11-25 11:54:57 +110074/*
75 * This variable contains the file descriptors used for communicating with
76 * the other side. connection_in is used for reading; connection_out for
77 * writing. These can be the same descriptor, in which case it is assumed to
78 * be a socket.
79 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080static int connection_in = -1;
81static int connection_out = -1;
82
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000084static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86/* Encryption context for receiving data. This is only used for decryption. */
87static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110088
89/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090static CipherContext send_context;
91
92/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000093Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
95/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000096Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
98/* Buffer for the partial outgoing packet being constructed. */
99static Buffer outgoing_packet;
100
101/* Buffer for the incoming packet currently being processed. */
102static Buffer incoming_packet;
103
104/* Scratch buffer for packet compression/decompression. */
105static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000106static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
108/* Flag indicating whether packet compression/decompression is enabled. */
109static int packet_compression = 0;
110
Damien Miller6162d121999-11-21 13:23:52 +1100111/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000112u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100113
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114/* Flag indicating whether this module has been initialized. */
115static int initialized = 0;
116
117/* Set to true if the connection is interactive. */
118static int interactive_mode = 0;
119
Damien Miller9786e6e2005-07-26 21:54:56 +1000120/* Set to true if we are the server side. */
121static int server_side = 0;
122
123/* Set to true if we are authenticated. */
124static int after_authentication = 0;
125
Damien Miller33b13562000-04-04 14:38:59 +1000126/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000127Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000128static struct packet_state {
129 u_int32_t seqnr;
130 u_int32_t packets;
131 u_int64_t blocks;
132} p_read, p_send;
133
134static u_int64_t max_blocks_in, max_blocks_out;
135static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000136
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000137/* Session key for protocol v1 */
138static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
139static u_int ssh1_keylen;
140
Damien Miller9f643902001-11-12 11:02:52 +1100141/* roundup current message to extra_pad bytes */
142static u_char extra_pad = 0;
143
Damien Millera5539d22003-04-09 20:50:06 +1000144struct packet {
145 TAILQ_ENTRY(packet) next;
146 u_char type;
147 Buffer payload;
148};
149TAILQ_HEAD(, packet) outgoing;
150
Damien Miller5428f641999-11-25 11:54:57 +1100151/*
152 * Sets the descriptors used for communication. Disables encryption until
153 * packet_set_encryption_key is called.
154 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155void
156packet_set_connection(int fd_in, int fd_out)
157{
Damien Miller874d77b2000-10-14 16:23:11 +1100158 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000159
Damien Miller874d77b2000-10-14 16:23:11 +1100160 if (none == NULL)
161 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100162 connection_in = fd_in;
163 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000164 cipher_init(&send_context, none, (const u_char *)"",
165 0, NULL, 0, CIPHER_ENCRYPT);
166 cipher_init(&receive_context, none, (const u_char *)"",
167 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000168 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100169 if (!initialized) {
170 initialized = 1;
171 buffer_init(&input);
172 buffer_init(&output);
173 buffer_init(&outgoing_packet);
174 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000175 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100176 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177}
178
Damien Miller34132e52000-01-14 15:45:46 +1100179/* Returns 1 if remote host is connected via socket, 0 if not. */
180
181int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000182packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100183{
184 struct sockaddr_storage from, to;
185 socklen_t fromlen, tolen;
186
187 /* filedescriptors in and out are the same, so it's a socket */
188 if (connection_in == connection_out)
189 return 1;
190 fromlen = sizeof(from);
191 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100192 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100193 return 0;
194 tolen = sizeof(to);
195 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100196 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100197 return 0;
198 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
199 return 0;
200 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
201 return 0;
202 return 1;
203}
204
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000205/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000206 * Exports an IV from the CipherContext required to export the key
207 * state back from the unprivileged child to the privileged parent
208 * process.
209 */
210
211void
212packet_get_keyiv(int mode, u_char *iv, u_int len)
213{
214 CipherContext *cc;
215
216 if (mode == MODE_OUT)
217 cc = &send_context;
218 else
219 cc = &receive_context;
220
221 cipher_get_keyiv(cc, iv, len);
222}
223
224int
225packet_get_keycontext(int mode, u_char *dat)
226{
227 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000228
Ben Lindstromf6027d32002-03-22 01:42:04 +0000229 if (mode == MODE_OUT)
230 cc = &send_context;
231 else
232 cc = &receive_context;
233
234 return (cipher_get_keycontext(cc, dat));
235}
236
237void
238packet_set_keycontext(int mode, u_char *dat)
239{
240 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000241
Ben Lindstromf6027d32002-03-22 01:42:04 +0000242 if (mode == MODE_OUT)
243 cc = &send_context;
244 else
245 cc = &receive_context;
246
247 cipher_set_keycontext(cc, dat);
248}
249
250int
251packet_get_keyiv_len(int mode)
252{
253 CipherContext *cc;
254
255 if (mode == MODE_OUT)
256 cc = &send_context;
257 else
258 cc = &receive_context;
259
260 return (cipher_get_keyiv_len(cc));
261}
262void
263packet_set_iv(int mode, u_char *dat)
264{
265 CipherContext *cc;
266
267 if (mode == MODE_OUT)
268 cc = &send_context;
269 else
270 cc = &receive_context;
271
272 cipher_set_keyiv(cc, dat);
273}
274int
Damien Miller2b92d322003-06-11 22:05:06 +1000275packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000276{
277 return (cipher_get_number(receive_context.cipher));
278}
279
Damien Millera5539d22003-04-09 20:50:06 +1000280void
281packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000282{
Damien Millera5539d22003-04-09 20:50:06 +1000283 struct packet_state *state;
284
285 state = (mode == MODE_IN) ? &p_read : &p_send;
286 *seqnr = state->seqnr;
287 *blocks = state->blocks;
288 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000289}
290
291void
Damien Millera5539d22003-04-09 20:50:06 +1000292packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000293{
Damien Millera5539d22003-04-09 20:50:06 +1000294 struct packet_state *state;
295
296 state = (mode == MODE_IN) ? &p_read : &p_send;
297 state->seqnr = seqnr;
298 state->blocks = blocks;
299 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000300}
301
Damien Miller34132e52000-01-14 15:45:46 +1100302/* returns 1 if connection is via ipv4 */
303
304int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000305packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100306{
307 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100308 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100309
310 memset(&to, 0, sizeof(to));
311 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
312 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000313 if (to.ss_family == AF_INET)
314 return 1;
315#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100316 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000317 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
318 return 1;
319#endif
320 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100321}
322
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323/* Sets the connection into non-blocking mode. */
324
325void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000326packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327{
Damien Miller95def091999-11-25 00:26:21 +1100328 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000329 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
Damien Miller232711f2004-06-15 10:35:30 +1000331 if (connection_out != connection_in)
332 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333}
334
335/* Returns the socket used for reading. */
336
337int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000338packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339{
Damien Miller95def091999-11-25 00:26:21 +1100340 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341}
342
343/* Returns the descriptor used for writing. */
344
345int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000346packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347{
Damien Miller95def091999-11-25 00:26:21 +1100348 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349}
350
351/* Closes the connection and clears and frees internal data structures. */
352
353void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000354packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355{
Damien Miller95def091999-11-25 00:26:21 +1100356 if (!initialized)
357 return;
358 initialized = 0;
359 if (connection_in == connection_out) {
360 shutdown(connection_out, SHUT_RDWR);
361 close(connection_out);
362 } else {
363 close(connection_in);
364 close(connection_out);
365 }
366 buffer_free(&input);
367 buffer_free(&output);
368 buffer_free(&outgoing_packet);
369 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000370 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100371 buffer_free(&compression_buffer);
372 buffer_compress_uninit();
373 }
Damien Miller963f6b22002-02-19 15:21:23 +1100374 cipher_cleanup(&send_context);
375 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376}
377
378/* Sets remote side protocol flags. */
379
380void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000381packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382{
Damien Miller95def091999-11-25 00:26:21 +1100383 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384}
385
386/* Returns the remote protocol flags set earlier by the above function. */
387
Ben Lindstrom46c16222000-12-22 01:43:59 +0000388u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000389packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390{
Damien Miller95def091999-11-25 00:26:21 +1100391 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392}
393
Damien Miller5428f641999-11-25 11:54:57 +1100394/*
395 * Starts packet compression from the next packet on in both directions.
396 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
397 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398
Ben Lindstrombba81212001-06-25 05:01:22 +0000399static void
400packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000401{
402 if (compression_buffer_ready == 1)
403 return;
404 compression_buffer_ready = 1;
405 buffer_init(&compression_buffer);
406}
407
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408void
409packet_start_compression(int level)
410{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000411 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100412 fatal("Compression already enabled.");
413 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000414 packet_init_compression();
415 buffer_compress_init_send(level);
416 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417}
418
Damien Miller5428f641999-11-25 11:54:57 +1100419/*
Damien Miller5428f641999-11-25 11:54:57 +1100420 * Causes any further packets to be encrypted using the given key. The same
421 * key is used for both sending and reception. However, both directions are
422 * encrypted independently of each other.
423 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000424
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000426packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100427 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428{
Damien Miller874d77b2000-10-14 16:23:11 +1100429 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000430
Damien Miller874d77b2000-10-14 16:23:11 +1100431 if (cipher == NULL)
432 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000433 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100434 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000435 if (keylen > SSH_SESSION_KEY_LENGTH)
436 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
437 memcpy(ssh1_key, key, keylen);
438 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100439 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
440 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441}
442
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000443u_int
444packet_get_encryption_key(u_char *key)
445{
446 if (key == NULL)
447 return (ssh1_keylen);
448 memcpy(key, ssh1_key, ssh1_keylen);
449 return (ssh1_keylen);
450}
451
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000452/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000454packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000456 u_char buf[9];
457 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458
Ben Lindstrom204e4882001-03-05 06:47:00 +0000459 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000460 len = compat20 ? 6 : 9;
461 memset(buf, 0, len - 1);
462 buf[len - 1] = type;
463 buffer_clear(&outgoing_packet);
464 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000465}
466
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000467/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468void
469packet_put_char(int value)
470{
Damien Miller95def091999-11-25 00:26:21 +1100471 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000472
Damien Miller95def091999-11-25 00:26:21 +1100473 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000476packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477{
Damien Miller95def091999-11-25 00:26:21 +1100478 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100481packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482{
Damien Miller95def091999-11-25 00:26:21 +1100483 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484}
Damien Miller33b13562000-04-04 14:38:59 +1000485void
486packet_put_cstring(const char *str)
487{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000488 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000489}
Damien Miller33b13562000-04-04 14:38:59 +1000490void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100491packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000492{
493 buffer_append(&outgoing_packet, buf, len);
494}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495void
Damien Miller95def091999-11-25 00:26:21 +1100496packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497{
Damien Miller95def091999-11-25 00:26:21 +1100498 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499}
Damien Miller33b13562000-04-04 14:38:59 +1000500void
501packet_put_bignum2(BIGNUM * value)
502{
503 buffer_put_bignum2(&outgoing_packet, value);
504}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505
Damien Miller5428f641999-11-25 11:54:57 +1100506/*
507 * Finalizes and sends the packet. If the encryption key has been set,
508 * encrypts the packet before sending.
509 */
Damien Miller95def091999-11-25 00:26:21 +1100510
Ben Lindstrombba81212001-06-25 05:01:22 +0000511static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000512packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000514 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100515 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000516 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000517 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller5428f641999-11-25 11:54:57 +1100519 /*
520 * If using packet compression, compress the payload of the outgoing
521 * packet.
522 */
Damien Miller95def091999-11-25 00:26:21 +1100523 if (packet_compression) {
524 buffer_clear(&compression_buffer);
525 /* Skip padding. */
526 buffer_consume(&outgoing_packet, 8);
527 /* padding */
528 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
529 buffer_compress(&outgoing_packet, &compression_buffer);
530 buffer_clear(&outgoing_packet);
531 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100532 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100533 }
534 /* Compute packet length without padding (add checksum, remove padding). */
535 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536
Damien Millere247cc42000-05-07 12:03:14 +1000537 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100538 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100539 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100540 cp = buffer_ptr(&outgoing_packet);
541 for (i = 0; i < padding; i++) {
542 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000543 rnd = arc4random();
544 cp[7 - i] = rnd & 0xff;
545 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100546 }
547 }
548 buffer_consume(&outgoing_packet, 8 - padding);
549
550 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100551 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000552 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100553 PUT_32BIT(buf, checksum);
554 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555
556#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100557 fprintf(stderr, "packet_send plain: ");
558 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559#endif
560
Damien Miller95def091999-11-25 00:26:21 +1100561 /* Append to output. */
562 PUT_32BIT(buf, len);
563 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100564 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100565 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100566 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100567
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100569 fprintf(stderr, "encrypted: ");
570 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571#endif
572
Damien Miller95def091999-11-25 00:26:21 +1100573 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574
Damien Miller5428f641999-11-25 11:54:57 +1100575 /*
Damien Miller788f2122005-11-05 15:14:59 +1100576 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100577 * actually sent until packet_write_wait or packet_write_poll is
578 * called.
579 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580}
581
Ben Lindstromf6027d32002-03-22 01:42:04 +0000582void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000583set_newkeys(int mode)
584{
585 Enc *enc;
586 Mac *mac;
587 Comp *comp;
588 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000589 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000590 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000591
Ben Lindstrom064496f2002-12-23 02:04:22 +0000592 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000593
Damien Miller963f6b22002-02-19 15:21:23 +1100594 if (mode == MODE_OUT) {
595 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000596 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000597 p_send.packets = p_send.blocks = 0;
598 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100599 } else {
600 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000601 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000602 p_read.packets = p_read.blocks = 0;
603 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100604 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000605 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000606 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100607 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000608 enc = &newkeys[mode]->enc;
609 mac = &newkeys[mode]->mac;
610 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000611 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000612 xfree(enc->name);
613 xfree(enc->iv);
614 xfree(enc->key);
615 xfree(mac->name);
616 xfree(mac->key);
617 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000618 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000619 }
620 newkeys[mode] = kex_get_newkeys(mode);
621 if (newkeys[mode] == NULL)
622 fatal("newkeys: no keys for mode %d", mode);
623 enc = &newkeys[mode]->enc;
624 mac = &newkeys[mode]->mac;
625 comp = &newkeys[mode]->comp;
626 if (mac->md != NULL)
627 mac->enabled = 1;
628 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100629 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000630 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000631 /* Deleting the keys does not gain extra security */
632 /* memset(enc->iv, 0, enc->block_size);
633 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000634 if ((comp->type == COMP_ZLIB ||
635 (comp->type == COMP_DELAYED && after_authentication)) &&
636 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000637 packet_init_compression();
638 if (mode == MODE_OUT)
639 buffer_compress_init_send(6);
640 else
641 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000642 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000643 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000644 /*
645 * The 2^(blocksize*2) limit is too expensive for 3DES,
646 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
647 */
648 if (enc->block_size >= 16)
649 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
650 else
651 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000652 if (rekey_limit)
653 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000654}
655
Damien Miller5428f641999-11-25 11:54:57 +1100656/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000657 * Delayed compression for SSH2 is enabled after authentication:
658 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
659 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
660 */
661static void
662packet_enable_delayed_compress(void)
663{
664 Comp *comp = NULL;
665 int mode;
666
667 /*
668 * Remember that we are past the authentication step, so rekeying
669 * with COMP_DELAYED will turn on compression immediately.
670 */
671 after_authentication = 1;
672 for (mode = 0; mode < MODE_MAX; mode++) {
673 comp = &newkeys[mode]->comp;
674 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000675 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000676 if (mode == MODE_OUT)
677 buffer_compress_init_send(6);
678 else
679 buffer_compress_init_recv();
680 comp->enabled = 1;
681 }
682 }
683}
684
685/*
Damien Miller33b13562000-04-04 14:38:59 +1000686 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
687 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000688static void
Damien Millera5539d22003-04-09 20:50:06 +1000689packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000690{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000691 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100692 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000693 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100694 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000695 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000696 Enc *enc = NULL;
697 Mac *mac = NULL;
698 Comp *comp = NULL;
699 int block_size;
700
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000701 if (newkeys[MODE_OUT] != NULL) {
702 enc = &newkeys[MODE_OUT]->enc;
703 mac = &newkeys[MODE_OUT]->mac;
704 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000705 }
Damien Miller963f6b22002-02-19 15:21:23 +1100706 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000707
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000708 cp = buffer_ptr(&outgoing_packet);
709 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000710
711#ifdef PACKET_DEBUG
712 fprintf(stderr, "plain: ");
713 buffer_dump(&outgoing_packet);
714#endif
715
716 if (comp && comp->enabled) {
717 len = buffer_len(&outgoing_packet);
718 /* skip header, compress only payload */
719 buffer_consume(&outgoing_packet, 5);
720 buffer_clear(&compression_buffer);
721 buffer_compress(&outgoing_packet, &compression_buffer);
722 buffer_clear(&outgoing_packet);
723 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
724 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
725 buffer_len(&compression_buffer));
726 DBG(debug("compression: raw %d compressed %d", len,
727 buffer_len(&outgoing_packet)));
728 }
729
730 /* sizeof (packet_len + pad_len + payload) */
731 len = buffer_len(&outgoing_packet);
732
733 /*
734 * calc size of padding, alloc space, get random data,
735 * minimum padding is 4 bytes
736 */
737 padlen = block_size - (len % block_size);
738 if (padlen < 4)
739 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100740 if (extra_pad) {
741 /* will wrap if extra_pad+padlen > 255 */
742 extra_pad = roundup(extra_pad, block_size);
743 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000744 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100745 pad, len, padlen, extra_pad);
746 padlen += pad;
747 extra_pad = 0;
748 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100749 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100750 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000751 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000752 for (i = 0; i < padlen; i++) {
753 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000754 rnd = arc4random();
755 cp[i] = rnd & 0xff;
756 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000757 }
Damien Millere247cc42000-05-07 12:03:14 +1000758 } else {
759 /* clear padding */
760 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000761 }
762 /* packet_length includes payload, padding and padding length field */
763 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000764 cp = buffer_ptr(&outgoing_packet);
765 PUT_32BIT(cp, packet_length);
766 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000767 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
768
769 /* compute MAC over seqnr and packet(length fields, payload, padding) */
770 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000771 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100772 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000773 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000774 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000775 }
776 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100777 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100778 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000779 buffer_len(&outgoing_packet));
780 /* append unencrypted MAC */
781 if (mac && mac->enabled)
782 buffer_append(&output, (char *)macbuf, mac->mac_len);
783#ifdef PACKET_DEBUG
784 fprintf(stderr, "encrypted: ");
785 buffer_dump(&output);
786#endif
Damien Miller4af51302000-04-16 11:18:38 +1000787 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000788 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000789 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000790 if (++p_send.packets == 0)
791 if (!(datafellows & SSH_BUG_NOREKEY))
792 fatal("XXX too many packets with same key");
793 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000794 buffer_clear(&outgoing_packet);
795
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000796 if (type == SSH2_MSG_NEWKEYS)
797 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000798 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
799 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000800}
801
Damien Millera5539d22003-04-09 20:50:06 +1000802static void
803packet_send2(void)
804{
805 static int rekeying = 0;
806 struct packet *p;
807 u_char type, *cp;
808
809 cp = buffer_ptr(&outgoing_packet);
810 type = cp[5];
811
812 /* during rekeying we can only send key exchange messages */
813 if (rekeying) {
814 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
815 (type <= SSH2_MSG_TRANSPORT_MAX))) {
816 debug("enqueue packet: %u", type);
817 p = xmalloc(sizeof(*p));
818 p->type = type;
819 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
820 buffer_init(&outgoing_packet);
821 TAILQ_INSERT_TAIL(&outgoing, p, next);
822 return;
823 }
824 }
825
826 /* rekeying starts with sending KEXINIT */
827 if (type == SSH2_MSG_KEXINIT)
828 rekeying = 1;
829
830 packet_send2_wrapped();
831
832 /* after a NEWKEYS message we can send the complete queue */
833 if (type == SSH2_MSG_NEWKEYS) {
834 rekeying = 0;
835 while ((p = TAILQ_FIRST(&outgoing))) {
836 type = p->type;
837 debug("dequeue packet: %u", type);
838 buffer_free(&outgoing_packet);
839 memcpy(&outgoing_packet, &p->payload,
840 sizeof(Buffer));
841 TAILQ_REMOVE(&outgoing, p, next);
842 xfree(p);
843 packet_send2_wrapped();
844 }
845 }
846}
847
Damien Miller33b13562000-04-04 14:38:59 +1000848void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000849packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000850{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000851 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000852 packet_send2();
853 else
854 packet_send1();
855 DBG(debug("packet_send done"));
856}
857
Damien Miller33b13562000-04-04 14:38:59 +1000858/*
Damien Miller5428f641999-11-25 11:54:57 +1100859 * Waits until a packet has been received, and returns its type. Note that
860 * no other data is processed until this returns, so this function should not
861 * be used during the interactive session.
862 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863
864int
Damien Millerdff50992002-01-22 23:16:32 +1100865packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866{
Damien Miller95def091999-11-25 00:26:21 +1100867 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000868 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100869 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000870 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000872 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
873 sizeof(fd_mask));
874
Damien Miller95def091999-11-25 00:26:21 +1100875 /* Since we are blocking, ensure that all written packets have been sent. */
876 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877
Damien Miller95def091999-11-25 00:26:21 +1100878 /* Stay in the loop until we have received a complete packet. */
879 for (;;) {
880 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100881 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000882 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000883 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100884 || type == SSH_SMSG_FAILURE
885 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000886 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100887 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100888 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000889 if (type != SSH_MSG_NONE) {
890 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100891 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000892 }
Damien Miller5428f641999-11-25 11:54:57 +1100893 /*
894 * Otherwise, wait for some data to arrive, add it to the
895 * buffer, and try again.
896 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000897 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
898 sizeof(fd_mask));
899 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100900
Damien Miller95def091999-11-25 00:26:21 +1100901 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000902 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000903 (errno == EAGAIN || errno == EINTR))
904 ;
Damien Miller5428f641999-11-25 11:54:57 +1100905
Damien Miller95def091999-11-25 00:26:21 +1100906 /* Read data from the socket. */
907 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100908 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000909 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000910 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100911 }
Damien Miller95def091999-11-25 00:26:21 +1100912 if (len < 0)
913 fatal("Read from socket failed: %.100s", strerror(errno));
914 /* Append it to the buffer. */
915 packet_process_incoming(buf, len);
916 }
917 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918}
919
Damien Miller278f9072001-12-21 15:00:19 +1100920int
Damien Millerdff50992002-01-22 23:16:32 +1100921packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100922{
Damien Millerdff50992002-01-22 23:16:32 +1100923 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100924}
925
Damien Miller5428f641999-11-25 11:54:57 +1100926/*
927 * Waits until a packet has been received, verifies that its type matches
928 * that given, and gives a fatal error and exits if there is a mismatch.
929 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930
931void
Damien Millerdff50992002-01-22 23:16:32 +1100932packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933{
Damien Miller95def091999-11-25 00:26:21 +1100934 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935
Damien Millerdff50992002-01-22 23:16:32 +1100936 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100937 if (type != expected_type)
938 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000939 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940}
941
942/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100943 * packet_process_incoming. If so, reads the packet; otherwise returns
944 * SSH_MSG_NONE. This does not wait for data from the connection.
945 *
946 * SSH_MSG_DISCONNECT is handled specially here. Also,
947 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
948 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100949 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950
Ben Lindstrombba81212001-06-25 05:01:22 +0000951static int
Damien Millerdff50992002-01-22 23:16:32 +1100952packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000954 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000955 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000956 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
Damien Miller95def091999-11-25 00:26:21 +1100958 /* Check if input size is less than minimum packet size. */
959 if (buffer_len(&input) < 4 + 8)
960 return SSH_MSG_NONE;
961 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000962 cp = buffer_ptr(&input);
963 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100964 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000965 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100966 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967
Damien Miller95def091999-11-25 00:26:21 +1100968 /* Check if the packet has been entirely received. */
969 if (buffer_len(&input) < 4 + padded_len)
970 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Damien Miller95def091999-11-25 00:26:21 +1100972 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973
Damien Miller95def091999-11-25 00:26:21 +1100974 /* Consume packet length. */
975 buffer_consume(&input, 4);
976
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000977 /*
978 * Cryptographic attack detector for ssh
979 * (C)1998 CORE-SDI, Buenos Aires Argentina
980 * Ariel Futoransky(futo@core-sdi.com)
981 */
Damien Miller963f6b22002-02-19 15:21:23 +1100982 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000983 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
984 packet_disconnect("crc32 compensation attack: network attack detected");
985
986 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100987 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100988 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100989 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000990
Damien Miller95def091999-11-25 00:26:21 +1100991 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992
993#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100994 fprintf(stderr, "read_poll plain: ");
995 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997
Damien Miller95def091999-11-25 00:26:21 +1100998 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100999 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001000 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001
Damien Miller95def091999-11-25 00:26:21 +11001002 /* Skip padding. */
1003 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001004
Damien Miller95def091999-11-25 00:26:21 +11001005 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001006 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001007 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001008 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001010 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
1011 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +11001012 if (checksum != stored_checksum)
1013 packet_disconnect("Corrupted check bytes on input.");
1014 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015
Damien Miller95def091999-11-25 00:26:21 +11001016 if (packet_compression) {
1017 buffer_clear(&compression_buffer);
1018 buffer_uncompress(&incoming_packet, &compression_buffer);
1019 buffer_clear(&incoming_packet);
1020 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001021 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001022 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001023 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001024 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1025 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001026 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027}
Damien Miller95def091999-11-25 00:26:21 +11001028
Ben Lindstrombba81212001-06-25 05:01:22 +00001029static int
Damien Millerdff50992002-01-22 23:16:32 +11001030packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001031{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001032 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001033 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001034 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001035 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001036 Enc *enc = NULL;
1037 Mac *mac = NULL;
1038 Comp *comp = NULL;
1039
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001040 if (newkeys[MODE_IN] != NULL) {
1041 enc = &newkeys[MODE_IN]->enc;
1042 mac = &newkeys[MODE_IN]->mac;
1043 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001044 }
1045 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001046 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001047
1048 if (packet_length == 0) {
1049 /*
1050 * check if input size is less than the cipher block size,
1051 * decrypt first block and extract length of incoming packet
1052 */
1053 if (buffer_len(&input) < block_size)
1054 return SSH_MSG_NONE;
1055 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001056 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001057 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001058 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001059 cp = buffer_ptr(&incoming_packet);
1060 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001061 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001062#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001063 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001064#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001065 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001066 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001067 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001068 buffer_consume(&input, block_size);
1069 }
1070 /* we have a partial packet of block_size bytes */
1071 need = 4 + packet_length - block_size;
1072 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1073 need, maclen));
1074 if (need % block_size != 0)
1075 fatal("padding error: need %d block %d mod %d",
1076 need, block_size, need % block_size);
1077 /*
1078 * check if the entire packet has been received and
1079 * decrypt into incoming_packet
1080 */
1081 if (buffer_len(&input) < need + maclen)
1082 return SSH_MSG_NONE;
1083#ifdef PACKET_DEBUG
1084 fprintf(stderr, "read_poll enc/full: ");
1085 buffer_dump(&input);
1086#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001087 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001088 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001089 buffer_consume(&input, need);
1090 /*
1091 * compute MAC over seqnr and packet,
1092 * increment sequence number for incoming packet
1093 */
Damien Miller4af51302000-04-16 11:18:38 +10001094 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001095 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001096 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001097 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001098 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001099 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001100 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001101 buffer_consume(&input, mac->mac_len);
1102 }
Damien Miller278f9072001-12-21 15:00:19 +11001103 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001104 *seqnr_p = p_read.seqnr;
1105 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001106 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001107 if (++p_read.packets == 0)
1108 if (!(datafellows & SSH_BUG_NOREKEY))
1109 fatal("XXX too many packets with same key");
1110 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001111
1112 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001113 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001114 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001115 DBG(debug("input: padlen %d", padlen));
1116 if (padlen < 4)
1117 packet_disconnect("Corrupted padlen %d on input.", padlen);
1118
1119 /* skip packet size + padlen, discard padding */
1120 buffer_consume(&incoming_packet, 4 + 1);
1121 buffer_consume_end(&incoming_packet, padlen);
1122
1123 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1124 if (comp && comp->enabled) {
1125 buffer_clear(&compression_buffer);
1126 buffer_uncompress(&incoming_packet, &compression_buffer);
1127 buffer_clear(&incoming_packet);
1128 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1129 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001130 DBG(debug("input: len after de-compress %d",
1131 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001132 }
1133 /*
1134 * get packet type, implies consume.
1135 * return length of payload (without type field)
1136 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001137 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001138 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1139 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001140 if (type == SSH2_MSG_NEWKEYS)
1141 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001142 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1143 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001144#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001145 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001146 buffer_dump(&incoming_packet);
1147#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001148 /* reset for next packet */
1149 packet_length = 0;
1150 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001151}
1152
1153int
Damien Millerdff50992002-01-22 23:16:32 +11001154packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001155{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001156 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001157 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001158 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001159
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001160 for (;;) {
1161 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001162 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001163 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001164 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001165 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001166 case SSH2_MSG_IGNORE:
1167 break;
1168 case SSH2_MSG_DEBUG:
1169 packet_get_char();
1170 msg = packet_get_string(NULL);
1171 debug("Remote: %.900s", msg);
1172 xfree(msg);
1173 msg = packet_get_string(NULL);
1174 xfree(msg);
1175 break;
1176 case SSH2_MSG_DISCONNECT:
1177 reason = packet_get_int();
1178 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001179 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001180 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001181 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001182 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001183 break;
Damien Miller659811f2002-01-22 23:23:11 +11001184 case SSH2_MSG_UNIMPLEMENTED:
1185 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001186 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1187 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001188 break;
Damien Miller33b13562000-04-04 14:38:59 +10001189 default:
1190 return type;
1191 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001192 }
Damien Miller33b13562000-04-04 14:38:59 +10001193 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001194 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001195 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001196 case SSH_MSG_IGNORE:
1197 break;
1198 case SSH_MSG_DEBUG:
1199 msg = packet_get_string(NULL);
1200 debug("Remote: %.900s", msg);
1201 xfree(msg);
1202 break;
1203 case SSH_MSG_DISCONNECT:
1204 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001205 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001206 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001207 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001208 xfree(msg);
1209 break;
1210 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001211 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001212 DBG(debug("received packet type %d", type));
1213 return type;
1214 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001215 }
Damien Miller33b13562000-04-04 14:38:59 +10001216 }
1217 }
1218}
1219
Damien Miller278f9072001-12-21 15:00:19 +11001220int
Damien Millerdff50992002-01-22 23:16:32 +11001221packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001222{
Damien Millerdff50992002-01-22 23:16:32 +11001223 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001224}
1225
Damien Miller5428f641999-11-25 11:54:57 +11001226/*
1227 * Buffers the given amount of input characters. This is intended to be used
1228 * together with packet_read_poll.
1229 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230
1231void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001232packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001233{
Damien Miller95def091999-11-25 00:26:21 +11001234 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235}
1236
1237/* Returns a character from the packet. */
1238
Ben Lindstrom46c16222000-12-22 01:43:59 +00001239u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001240packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241{
Damien Miller95def091999-11-25 00:26:21 +11001242 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001243
Damien Miller95def091999-11-25 00:26:21 +11001244 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001245 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246}
1247
1248/* Returns an integer from the packet data. */
1249
Ben Lindstrom46c16222000-12-22 01:43:59 +00001250u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001251packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252{
Damien Miller95def091999-11-25 00:26:21 +11001253 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254}
1255
Damien Miller5428f641999-11-25 11:54:57 +11001256/*
1257 * Returns an arbitrary precision integer from the packet data. The integer
1258 * must have been initialized before this call.
1259 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260
1261void
Damien Millerd432ccf2002-01-22 23:14:44 +11001262packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001263{
Damien Miller76e1e362002-01-22 23:15:57 +11001264 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265}
1266
Damien Miller33b13562000-04-04 14:38:59 +10001267void
Damien Millerd432ccf2002-01-22 23:14:44 +11001268packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001269{
Damien Miller76e1e362002-01-22 23:15:57 +11001270 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001271}
1272
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001273void *
Damien Millereccb9de2005-06-17 12:59:34 +10001274packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001275{
Damien Millereccb9de2005-06-17 12:59:34 +10001276 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001277
Damien Miller33b13562000-04-04 14:38:59 +10001278 if (length_ptr != NULL)
1279 *length_ptr = bytes;
1280 return buffer_ptr(&incoming_packet);
1281}
1282
Damien Miller4af51302000-04-16 11:18:38 +10001283int
1284packet_remaining(void)
1285{
1286 return buffer_len(&incoming_packet);
1287}
1288
Damien Miller5428f641999-11-25 11:54:57 +11001289/*
1290 * Returns a string from the packet data. The string is allocated using
1291 * xmalloc; it is the responsibility of the calling program to free it when
1292 * no longer needed. The length_ptr argument may be NULL, or point to an
1293 * integer into which the length of the string is stored.
1294 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001295
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001296void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001297packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001298{
Damien Miller95def091999-11-25 00:26:21 +11001299 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001300}
1301
Damien Miller5428f641999-11-25 11:54:57 +11001302/*
1303 * Sends a diagnostic message from the server to the client. This message
1304 * can be sent at any time (but not while constructing another message). The
1305 * message is printed immediately, but only if the client is being executed
1306 * in verbose mode. These messages are primarily intended to ease debugging
1307 * authentication problems. The length of the formatted message must not
1308 * exceed 1024 bytes. This will automatically call packet_write_wait.
1309 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310
1311void
Damien Miller95def091999-11-25 00:26:21 +11001312packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001313{
Damien Miller95def091999-11-25 00:26:21 +11001314 char buf[1024];
1315 va_list args;
1316
Ben Lindstroma14ee472000-12-07 01:24:58 +00001317 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1318 return;
1319
Damien Miller95def091999-11-25 00:26:21 +11001320 va_start(args, fmt);
1321 vsnprintf(buf, sizeof(buf), fmt, args);
1322 va_end(args);
1323
Damien Miller7c8af4f2000-05-01 08:24:07 +10001324 if (compat20) {
1325 packet_start(SSH2_MSG_DEBUG);
1326 packet_put_char(0); /* bool: always display */
1327 packet_put_cstring(buf);
1328 packet_put_cstring("");
1329 } else {
1330 packet_start(SSH_MSG_DEBUG);
1331 packet_put_cstring(buf);
1332 }
Damien Miller95def091999-11-25 00:26:21 +11001333 packet_send();
1334 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335}
1336
Damien Miller5428f641999-11-25 11:54:57 +11001337/*
1338 * Logs the error plus constructs and sends a disconnect packet, closes the
1339 * connection, and exits. This function never returns. The error message
1340 * should not contain a newline. The length of the formatted message must
1341 * not exceed 1024 bytes.
1342 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343
1344void
Damien Miller95def091999-11-25 00:26:21 +11001345packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346{
Damien Miller95def091999-11-25 00:26:21 +11001347 char buf[1024];
1348 va_list args;
1349 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001350
Damien Miller95def091999-11-25 00:26:21 +11001351 if (disconnecting) /* Guard against recursive invocations. */
1352 fatal("packet_disconnect called recursively.");
1353 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001354
Damien Miller5428f641999-11-25 11:54:57 +11001355 /*
1356 * Format the message. Note that the caller must make sure the
1357 * message is of limited size.
1358 */
Damien Miller95def091999-11-25 00:26:21 +11001359 va_start(args, fmt);
1360 vsnprintf(buf, sizeof(buf), fmt, args);
1361 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001362
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001363 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001364 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001365
Damien Miller95def091999-11-25 00:26:21 +11001366 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001367 if (compat20) {
1368 packet_start(SSH2_MSG_DISCONNECT);
1369 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1370 packet_put_cstring(buf);
1371 packet_put_cstring("");
1372 } else {
1373 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001374 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001375 }
Damien Miller95def091999-11-25 00:26:21 +11001376 packet_send();
1377 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378
Damien Miller95def091999-11-25 00:26:21 +11001379 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001380 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381
Damien Miller95def091999-11-25 00:26:21 +11001382 /* Close the connection. */
1383 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001384 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001385}
1386
Damien Miller5428f641999-11-25 11:54:57 +11001387/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001388
1389void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001390packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391{
Damien Miller95def091999-11-25 00:26:21 +11001392 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001393
Damien Miller95def091999-11-25 00:26:21 +11001394 if (len > 0) {
1395 len = write(connection_out, buffer_ptr(&output), len);
1396 if (len <= 0) {
1397 if (errno == EAGAIN)
1398 return;
1399 else
1400 fatal("Write failed: %.100s", strerror(errno));
1401 }
1402 buffer_consume(&output, len);
1403 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404}
1405
Damien Miller5428f641999-11-25 11:54:57 +11001406/*
1407 * Calls packet_write_poll repeatedly until all pending output data has been
1408 * written.
1409 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410
1411void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001412packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001414 fd_set *setp;
1415
1416 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1417 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001418 packet_write_poll();
1419 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001420 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1421 sizeof(fd_mask));
1422 FD_SET(connection_out, setp);
1423 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001424 (errno == EAGAIN || errno == EINTR))
1425 ;
Damien Miller95def091999-11-25 00:26:21 +11001426 packet_write_poll();
1427 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001428 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429}
1430
1431/* Returns true if there is buffered data to write to the connection. */
1432
1433int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001434packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435{
Damien Miller95def091999-11-25 00:26:21 +11001436 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437}
1438
1439/* Returns true if there is not too much data to write to the connection. */
1440
1441int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001442packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443{
Damien Miller95def091999-11-25 00:26:21 +11001444 if (interactive_mode)
1445 return buffer_len(&output) < 16384;
1446 else
1447 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001448}
1449
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001450
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001451static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001452packet_set_tos(int interactive)
1453{
Damien Miller5924ceb2003-11-22 15:02:42 +11001454#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001455 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1456
1457 if (!packet_connection_is_on_socket() ||
1458 !packet_connection_is_ipv4())
1459 return;
1460 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1461 sizeof(tos)) < 0)
1462 error("setsockopt IP_TOS %d: %.100s:",
1463 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001464#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001465}
Ben Lindstroma7433982002-12-23 02:41:41 +00001466
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467/* Informs that the current session is interactive. Sets IP flags for that. */
1468
1469void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001470packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001471{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001472 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001474 if (called)
1475 return;
1476 called = 1;
1477
Damien Miller95def091999-11-25 00:26:21 +11001478 /* Record that we are in interactive mode. */
1479 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480
Damien Miller34132e52000-01-14 15:45:46 +11001481 /* Only set socket options if using a socket. */
1482 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001483 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001484 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001485 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486}
1487
1488/* Returns true if the current connection is interactive. */
1489
1490int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001491packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492{
Damien Miller95def091999-11-25 00:26:21 +11001493 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494}
Damien Miller6162d121999-11-21 13:23:52 +11001495
Darren Tucker1f8311c2004-05-13 16:39:33 +10001496int
Darren Tucker502d3842003-06-28 12:38:01 +10001497packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001498{
Damien Miller95def091999-11-25 00:26:21 +11001499 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001500
Damien Miller95def091999-11-25 00:26:21 +11001501 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001502 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001503 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001504 return -1;
1505 }
1506 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001507 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001508 return -1;
1509 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001510 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001511 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001512 max_packet_size = s;
1513 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001514}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001515
Damien Miller9f643902001-11-12 11:02:52 +11001516/* roundup current message to pad bytes */
1517void
1518packet_add_padding(u_char pad)
1519{
1520 extra_pad = pad;
1521}
1522
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001523/*
1524 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001525 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001526 * byte SSH_MSG_IGNORE
1527 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001528 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001529 * All implementations MUST understand (and ignore) this message at any
1530 * time (after receiving the protocol version). No implementation is
1531 * required to send them. This message can be used as an additional
1532 * protection measure against advanced traffic analysis techniques.
1533 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001534void
1535packet_send_ignore(int nbytes)
1536{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001537 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001538 int i;
1539
1540 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001541 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001542 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001543 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001544 rnd = arc4random();
1545 packet_put_char(rnd & 0xff);
1546 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001547 }
1548}
Damien Millera5539d22003-04-09 20:50:06 +10001549
Darren Tucker1f8311c2004-05-13 16:39:33 +10001550#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001551int
1552packet_need_rekeying(void)
1553{
1554 if (datafellows & SSH_BUG_NOREKEY)
1555 return 0;
1556 return
1557 (p_send.packets > MAX_PACKETS) ||
1558 (p_read.packets > MAX_PACKETS) ||
1559 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1560 (max_blocks_in && (p_read.blocks > max_blocks_in));
1561}
1562
1563void
1564packet_set_rekey_limit(u_int32_t bytes)
1565{
1566 rekey_limit = bytes;
1567}
Damien Miller9786e6e2005-07-26 21:54:56 +10001568
1569void
1570packet_set_server(void)
1571{
1572 server_side = 1;
1573}
1574
1575void
1576packet_set_authenticated(void)
1577{
1578 after_authentication = 1;
1579}