blob: da843b2c2100cf236b058f1a4e00f1e0a412a07c [file] [log] [blame]
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001/* $OpenBSD: packet.c,v 1.144 2006/09/16 19:53:37 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * 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 Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * 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 Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
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 Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
Damien Millere3b60b52006-07-10 21:08:03 +100042#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100043#include "openbsd-compat/sys-queue.h"
Damien Miller8dbffe72006-08-05 11:02:17 +100044#include <sys/param.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100046#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049
50#include <netinet/in_systm.h>
51#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110052#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100055#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100056#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100057#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100058#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100059#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100060#include <signal.h>
Darren Tucker5d196262006-07-12 22:15:16 +100061
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "xmalloc.h"
63#include "buffer.h"
64#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "compress.h"
67#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000068#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100069#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100071#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110072#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100073#include "key.h"
Damien Miller33b13562000-04-04 14:38:59 +100074#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000075#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
77#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110078#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000079#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100080
81#ifdef PACKET_DEBUG
82#define DBG(x) x
83#else
84#define DBG(x)
85#endif
86
Damien Miller5428f641999-11-25 11:54:57 +110087/*
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 Millerd4a8b7e1999-10-27 13:42:43 +100093static int connection_in = -1;
94static int connection_out = -1;
95
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000097static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99/* Encryption context for receiving data. This is only used for decryption. */
100static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +1100101
102/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103static CipherContext send_context;
104
105/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000106Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
108/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000109Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111/* Buffer for the partial outgoing packet being constructed. */
112static Buffer outgoing_packet;
113
114/* Buffer for the incoming packet currently being processed. */
115static Buffer incoming_packet;
116
117/* Scratch buffer for packet compression/decompression. */
118static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000119static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
121/* Flag indicating whether packet compression/decompression is enabled. */
122static int packet_compression = 0;
123
Damien Miller6162d121999-11-21 13:23:52 +1100124/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000125u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100126
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127/* Flag indicating whether this module has been initialized. */
128static int initialized = 0;
129
130/* Set to true if the connection is interactive. */
131static int interactive_mode = 0;
132
Damien Miller9786e6e2005-07-26 21:54:56 +1000133/* Set to true if we are the server side. */
134static int server_side = 0;
135
136/* Set to true if we are authenticated. */
137static int after_authentication = 0;
138
Damien Miller33b13562000-04-04 14:38:59 +1000139/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000140Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000141static struct packet_state {
142 u_int32_t seqnr;
143 u_int32_t packets;
144 u_int64_t blocks;
145} p_read, p_send;
146
147static u_int64_t max_blocks_in, max_blocks_out;
148static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000149
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000150/* Session key for protocol v1 */
151static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
152static u_int ssh1_keylen;
153
Damien Miller9f643902001-11-12 11:02:52 +1100154/* roundup current message to extra_pad bytes */
155static u_char extra_pad = 0;
156
Damien Millera5539d22003-04-09 20:50:06 +1000157struct packet {
158 TAILQ_ENTRY(packet) next;
159 u_char type;
160 Buffer payload;
161};
162TAILQ_HEAD(, packet) outgoing;
163
Damien Miller5428f641999-11-25 11:54:57 +1100164/*
165 * Sets the descriptors used for communication. Disables encryption until
166 * packet_set_encryption_key is called.
167 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168void
169packet_set_connection(int fd_in, int fd_out)
170{
Damien Miller874d77b2000-10-14 16:23:11 +1100171 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000172
Damien Miller874d77b2000-10-14 16:23:11 +1100173 if (none == NULL)
174 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100175 connection_in = fd_in;
176 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000177 cipher_init(&send_context, none, (const u_char *)"",
178 0, NULL, 0, CIPHER_ENCRYPT);
179 cipher_init(&receive_context, none, (const u_char *)"",
180 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000181 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100182 if (!initialized) {
183 initialized = 1;
184 buffer_init(&input);
185 buffer_init(&output);
186 buffer_init(&outgoing_packet);
187 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000188 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100189 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
Damien Miller34132e52000-01-14 15:45:46 +1100192/* Returns 1 if remote host is connected via socket, 0 if not. */
193
194int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000195packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100196{
197 struct sockaddr_storage from, to;
198 socklen_t fromlen, tolen;
199
200 /* filedescriptors in and out are the same, so it's a socket */
201 if (connection_in == connection_out)
202 return 1;
203 fromlen = sizeof(from);
204 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100205 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100206 return 0;
207 tolen = sizeof(to);
208 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100209 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100210 return 0;
211 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
212 return 0;
213 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
214 return 0;
215 return 1;
216}
217
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000218/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000219 * Exports an IV from the CipherContext required to export the key
220 * state back from the unprivileged child to the privileged parent
221 * process.
222 */
223
224void
225packet_get_keyiv(int mode, u_char *iv, u_int len)
226{
227 CipherContext *cc;
228
229 if (mode == MODE_OUT)
230 cc = &send_context;
231 else
232 cc = &receive_context;
233
234 cipher_get_keyiv(cc, iv, len);
235}
236
237int
238packet_get_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 return (cipher_get_keycontext(cc, dat));
248}
249
250void
251packet_set_keycontext(int mode, u_char *dat)
252{
253 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000254
Ben Lindstromf6027d32002-03-22 01:42:04 +0000255 if (mode == MODE_OUT)
256 cc = &send_context;
257 else
258 cc = &receive_context;
259
260 cipher_set_keycontext(cc, dat);
261}
262
263int
264packet_get_keyiv_len(int mode)
265{
266 CipherContext *cc;
267
268 if (mode == MODE_OUT)
269 cc = &send_context;
270 else
271 cc = &receive_context;
272
273 return (cipher_get_keyiv_len(cc));
274}
Damien Miller4f7becb2006-03-26 14:10:14 +1100275
Ben Lindstromf6027d32002-03-22 01:42:04 +0000276void
277packet_set_iv(int mode, u_char *dat)
278{
279 CipherContext *cc;
280
281 if (mode == MODE_OUT)
282 cc = &send_context;
283 else
284 cc = &receive_context;
285
286 cipher_set_keyiv(cc, dat);
287}
Damien Miller4f7becb2006-03-26 14:10:14 +1100288
Ben Lindstromf6027d32002-03-22 01:42:04 +0000289int
Damien Miller2b92d322003-06-11 22:05:06 +1000290packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000291{
292 return (cipher_get_number(receive_context.cipher));
293}
294
Damien Millera5539d22003-04-09 20:50:06 +1000295void
296packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000297{
Damien Millera5539d22003-04-09 20:50:06 +1000298 struct packet_state *state;
299
300 state = (mode == MODE_IN) ? &p_read : &p_send;
301 *seqnr = state->seqnr;
302 *blocks = state->blocks;
303 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000304}
305
306void
Damien Millera5539d22003-04-09 20:50:06 +1000307packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000308{
Damien Millera5539d22003-04-09 20:50:06 +1000309 struct packet_state *state;
310
311 state = (mode == MODE_IN) ? &p_read : &p_send;
312 state->seqnr = seqnr;
313 state->blocks = blocks;
314 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000315}
316
Damien Miller34132e52000-01-14 15:45:46 +1100317/* returns 1 if connection is via ipv4 */
318
319int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000320packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100321{
322 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100323 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100324
325 memset(&to, 0, sizeof(to));
326 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
327 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000328 if (to.ss_family == AF_INET)
329 return 1;
330#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100331 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000332 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
333 return 1;
334#endif
335 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100336}
337
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338/* Sets the connection into non-blocking mode. */
339
340void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000341packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342{
Damien Miller95def091999-11-25 00:26:21 +1100343 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000344 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Miller232711f2004-06-15 10:35:30 +1000346 if (connection_out != connection_in)
347 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348}
349
350/* Returns the socket used for reading. */
351
352int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000353packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354{
Damien Miller95def091999-11-25 00:26:21 +1100355 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356}
357
358/* Returns the descriptor used for writing. */
359
360int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000361packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362{
Damien Miller95def091999-11-25 00:26:21 +1100363 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364}
365
366/* Closes the connection and clears and frees internal data structures. */
367
368void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000369packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370{
Damien Miller95def091999-11-25 00:26:21 +1100371 if (!initialized)
372 return;
373 initialized = 0;
374 if (connection_in == connection_out) {
375 shutdown(connection_out, SHUT_RDWR);
376 close(connection_out);
377 } else {
378 close(connection_in);
379 close(connection_out);
380 }
381 buffer_free(&input);
382 buffer_free(&output);
383 buffer_free(&outgoing_packet);
384 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000385 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100386 buffer_free(&compression_buffer);
387 buffer_compress_uninit();
388 }
Damien Miller963f6b22002-02-19 15:21:23 +1100389 cipher_cleanup(&send_context);
390 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391}
392
393/* Sets remote side protocol flags. */
394
395void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000396packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397{
Damien Miller95def091999-11-25 00:26:21 +1100398 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399}
400
401/* Returns the remote protocol flags set earlier by the above function. */
402
Ben Lindstrom46c16222000-12-22 01:43:59 +0000403u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000404packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405{
Damien Miller95def091999-11-25 00:26:21 +1100406 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407}
408
Damien Miller5428f641999-11-25 11:54:57 +1100409/*
410 * Starts packet compression from the next packet on in both directions.
411 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
412 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Ben Lindstrombba81212001-06-25 05:01:22 +0000414static void
415packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000416{
417 if (compression_buffer_ready == 1)
418 return;
419 compression_buffer_ready = 1;
420 buffer_init(&compression_buffer);
421}
422
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423void
424packet_start_compression(int level)
425{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000426 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100427 fatal("Compression already enabled.");
428 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000429 packet_init_compression();
430 buffer_compress_init_send(level);
431 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
Damien Miller5428f641999-11-25 11:54:57 +1100434/*
Damien Miller5428f641999-11-25 11:54:57 +1100435 * Causes any further packets to be encrypted using the given key. The same
436 * key is used for both sending and reception. However, both directions are
437 * encrypted independently of each other.
438 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000439
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000441packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100442 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443{
Damien Miller874d77b2000-10-14 16:23:11 +1100444 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000445
Damien Miller874d77b2000-10-14 16:23:11 +1100446 if (cipher == NULL)
447 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000448 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100449 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000450 if (keylen > SSH_SESSION_KEY_LENGTH)
451 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
452 memcpy(ssh1_key, key, keylen);
453 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100454 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
455 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456}
457
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000458u_int
459packet_get_encryption_key(u_char *key)
460{
461 if (key == NULL)
462 return (ssh1_keylen);
463 memcpy(key, ssh1_key, ssh1_keylen);
464 return (ssh1_keylen);
465}
466
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000467/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000469packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000471 u_char buf[9];
472 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473
Ben Lindstrom204e4882001-03-05 06:47:00 +0000474 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000475 len = compat20 ? 6 : 9;
476 memset(buf, 0, len - 1);
477 buf[len - 1] = type;
478 buffer_clear(&outgoing_packet);
479 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000480}
481
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000482/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483void
484packet_put_char(int value)
485{
Damien Miller95def091999-11-25 00:26:21 +1100486 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000487
Damien Miller95def091999-11-25 00:26:21 +1100488 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489}
Damien Miller4f7becb2006-03-26 14:10:14 +1100490
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000492packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493{
Damien Miller95def091999-11-25 00:26:21 +1100494 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495}
Damien Miller4f7becb2006-03-26 14:10:14 +1100496
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100498packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499{
Damien Miller95def091999-11-25 00:26:21 +1100500 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501}
Damien Miller4f7becb2006-03-26 14:10:14 +1100502
Damien Miller33b13562000-04-04 14:38:59 +1000503void
504packet_put_cstring(const char *str)
505{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000506 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000507}
Damien Miller4f7becb2006-03-26 14:10:14 +1100508
Damien Miller33b13562000-04-04 14:38:59 +1000509void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100510packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000511{
512 buffer_append(&outgoing_packet, buf, len);
513}
Damien Miller4f7becb2006-03-26 14:10:14 +1100514
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515void
Damien Miller95def091999-11-25 00:26:21 +1100516packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517{
Damien Miller95def091999-11-25 00:26:21 +1100518 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519}
Damien Miller4f7becb2006-03-26 14:10:14 +1100520
Damien Miller33b13562000-04-04 14:38:59 +1000521void
522packet_put_bignum2(BIGNUM * value)
523{
524 buffer_put_bignum2(&outgoing_packet, value);
525}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Damien Miller5428f641999-11-25 11:54:57 +1100527/*
528 * Finalizes and sends the packet. If the encryption key has been set,
529 * encrypts the packet before sending.
530 */
Damien Miller95def091999-11-25 00:26:21 +1100531
Ben Lindstrombba81212001-06-25 05:01:22 +0000532static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000533packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000535 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100536 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000537 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000538 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539
Damien Miller5428f641999-11-25 11:54:57 +1100540 /*
541 * If using packet compression, compress the payload of the outgoing
542 * packet.
543 */
Damien Miller95def091999-11-25 00:26:21 +1100544 if (packet_compression) {
545 buffer_clear(&compression_buffer);
546 /* Skip padding. */
547 buffer_consume(&outgoing_packet, 8);
548 /* padding */
549 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
550 buffer_compress(&outgoing_packet, &compression_buffer);
551 buffer_clear(&outgoing_packet);
552 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100553 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100554 }
555 /* Compute packet length without padding (add checksum, remove padding). */
556 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
Damien Millere247cc42000-05-07 12:03:14 +1000558 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100559 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100560 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100561 cp = buffer_ptr(&outgoing_packet);
562 for (i = 0; i < padding; i++) {
563 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000564 rnd = arc4random();
565 cp[7 - i] = rnd & 0xff;
566 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100567 }
568 }
569 buffer_consume(&outgoing_packet, 8 - padding);
570
571 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100572 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000573 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100574 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100575 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576
577#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100578 fprintf(stderr, "packet_send plain: ");
579 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580#endif
581
Damien Miller95def091999-11-25 00:26:21 +1100582 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100583 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100584 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100585 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100586 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100587 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100588
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100590 fprintf(stderr, "encrypted: ");
591 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592#endif
593
Damien Miller95def091999-11-25 00:26:21 +1100594 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
Damien Miller5428f641999-11-25 11:54:57 +1100596 /*
Damien Miller788f2122005-11-05 15:14:59 +1100597 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100598 * actually sent until packet_write_wait or packet_write_poll is
599 * called.
600 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601}
602
Ben Lindstromf6027d32002-03-22 01:42:04 +0000603void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000604set_newkeys(int mode)
605{
606 Enc *enc;
607 Mac *mac;
608 Comp *comp;
609 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000610 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000611 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000612
Ben Lindstrom064496f2002-12-23 02:04:22 +0000613 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000614
Damien Miller963f6b22002-02-19 15:21:23 +1100615 if (mode == MODE_OUT) {
616 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000617 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000618 p_send.packets = p_send.blocks = 0;
619 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100620 } else {
621 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000622 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000623 p_read.packets = p_read.blocks = 0;
624 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100625 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000626 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000627 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100628 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000629 enc = &newkeys[mode]->enc;
630 mac = &newkeys[mode]->mac;
631 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000632 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000633 xfree(enc->name);
634 xfree(enc->iv);
635 xfree(enc->key);
636 xfree(mac->name);
637 xfree(mac->key);
638 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000639 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000640 }
641 newkeys[mode] = kex_get_newkeys(mode);
642 if (newkeys[mode] == NULL)
643 fatal("newkeys: no keys for mode %d", mode);
644 enc = &newkeys[mode]->enc;
645 mac = &newkeys[mode]->mac;
646 comp = &newkeys[mode]->comp;
647 if (mac->md != NULL)
648 mac->enabled = 1;
649 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100650 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000651 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000652 /* Deleting the keys does not gain extra security */
653 /* memset(enc->iv, 0, enc->block_size);
654 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000655 if ((comp->type == COMP_ZLIB ||
656 (comp->type == COMP_DELAYED && after_authentication)) &&
657 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000658 packet_init_compression();
659 if (mode == MODE_OUT)
660 buffer_compress_init_send(6);
661 else
662 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000663 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000664 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000665 /*
666 * The 2^(blocksize*2) limit is too expensive for 3DES,
667 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
668 */
669 if (enc->block_size >= 16)
670 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
671 else
672 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000673 if (rekey_limit)
674 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000675}
676
Damien Miller5428f641999-11-25 11:54:57 +1100677/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000678 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000679 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000680 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
681 */
682static void
683packet_enable_delayed_compress(void)
684{
685 Comp *comp = NULL;
686 int mode;
687
688 /*
689 * Remember that we are past the authentication step, so rekeying
690 * with COMP_DELAYED will turn on compression immediately.
691 */
692 after_authentication = 1;
693 for (mode = 0; mode < MODE_MAX; mode++) {
694 comp = &newkeys[mode]->comp;
695 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000696 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000697 if (mode == MODE_OUT)
698 buffer_compress_init_send(6);
699 else
700 buffer_compress_init_recv();
701 comp->enabled = 1;
702 }
703 }
704}
705
706/*
Damien Miller33b13562000-04-04 14:38:59 +1000707 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
708 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000709static void
Damien Millera5539d22003-04-09 20:50:06 +1000710packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000711{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000712 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100713 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000714 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100715 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000716 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000717 Enc *enc = NULL;
718 Mac *mac = NULL;
719 Comp *comp = NULL;
720 int block_size;
721
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000722 if (newkeys[MODE_OUT] != NULL) {
723 enc = &newkeys[MODE_OUT]->enc;
724 mac = &newkeys[MODE_OUT]->mac;
725 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000726 }
Damien Miller963f6b22002-02-19 15:21:23 +1100727 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000728
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000729 cp = buffer_ptr(&outgoing_packet);
730 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000731
732#ifdef PACKET_DEBUG
733 fprintf(stderr, "plain: ");
734 buffer_dump(&outgoing_packet);
735#endif
736
737 if (comp && comp->enabled) {
738 len = buffer_len(&outgoing_packet);
739 /* skip header, compress only payload */
740 buffer_consume(&outgoing_packet, 5);
741 buffer_clear(&compression_buffer);
742 buffer_compress(&outgoing_packet, &compression_buffer);
743 buffer_clear(&outgoing_packet);
744 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
745 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
746 buffer_len(&compression_buffer));
747 DBG(debug("compression: raw %d compressed %d", len,
748 buffer_len(&outgoing_packet)));
749 }
750
751 /* sizeof (packet_len + pad_len + payload) */
752 len = buffer_len(&outgoing_packet);
753
754 /*
755 * calc size of padding, alloc space, get random data,
756 * minimum padding is 4 bytes
757 */
758 padlen = block_size - (len % block_size);
759 if (padlen < 4)
760 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100761 if (extra_pad) {
762 /* will wrap if extra_pad+padlen > 255 */
763 extra_pad = roundup(extra_pad, block_size);
764 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000765 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100766 pad, len, padlen, extra_pad);
767 padlen += pad;
768 extra_pad = 0;
769 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100770 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100771 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000772 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000773 for (i = 0; i < padlen; i++) {
774 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000775 rnd = arc4random();
776 cp[i] = rnd & 0xff;
777 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000778 }
Damien Millere247cc42000-05-07 12:03:14 +1000779 } else {
780 /* clear padding */
781 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000782 }
783 /* packet_length includes payload, padding and padding length field */
784 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000785 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100786 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000787 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000788 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
789
790 /* compute MAC over seqnr and packet(length fields, payload, padding) */
791 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000792 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100793 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000794 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000795 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000796 }
797 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100798 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100799 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000800 buffer_len(&outgoing_packet));
801 /* append unencrypted MAC */
802 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100803 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000804#ifdef PACKET_DEBUG
805 fprintf(stderr, "encrypted: ");
806 buffer_dump(&output);
807#endif
Damien Miller4af51302000-04-16 11:18:38 +1000808 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000809 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000810 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000811 if (++p_send.packets == 0)
812 if (!(datafellows & SSH_BUG_NOREKEY))
813 fatal("XXX too many packets with same key");
814 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000815 buffer_clear(&outgoing_packet);
816
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000817 if (type == SSH2_MSG_NEWKEYS)
818 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000819 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
820 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000821}
822
Damien Millera5539d22003-04-09 20:50:06 +1000823static void
824packet_send2(void)
825{
826 static int rekeying = 0;
827 struct packet *p;
828 u_char type, *cp;
829
830 cp = buffer_ptr(&outgoing_packet);
831 type = cp[5];
832
833 /* during rekeying we can only send key exchange messages */
834 if (rekeying) {
835 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
836 (type <= SSH2_MSG_TRANSPORT_MAX))) {
837 debug("enqueue packet: %u", type);
838 p = xmalloc(sizeof(*p));
839 p->type = type;
840 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
841 buffer_init(&outgoing_packet);
842 TAILQ_INSERT_TAIL(&outgoing, p, next);
843 return;
844 }
845 }
846
847 /* rekeying starts with sending KEXINIT */
848 if (type == SSH2_MSG_KEXINIT)
849 rekeying = 1;
850
851 packet_send2_wrapped();
852
853 /* after a NEWKEYS message we can send the complete queue */
854 if (type == SSH2_MSG_NEWKEYS) {
855 rekeying = 0;
856 while ((p = TAILQ_FIRST(&outgoing))) {
857 type = p->type;
858 debug("dequeue packet: %u", type);
859 buffer_free(&outgoing_packet);
860 memcpy(&outgoing_packet, &p->payload,
861 sizeof(Buffer));
862 TAILQ_REMOVE(&outgoing, p, next);
863 xfree(p);
864 packet_send2_wrapped();
865 }
866 }
867}
868
Damien Miller33b13562000-04-04 14:38:59 +1000869void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000870packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000871{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000872 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000873 packet_send2();
874 else
875 packet_send1();
876 DBG(debug("packet_send done"));
877}
878
Damien Miller33b13562000-04-04 14:38:59 +1000879/*
Damien Miller5428f641999-11-25 11:54:57 +1100880 * Waits until a packet has been received, and returns its type. Note that
881 * no other data is processed until this returns, so this function should not
882 * be used during the interactive session.
883 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
885int
Damien Millerdff50992002-01-22 23:16:32 +1100886packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887{
Damien Miller95def091999-11-25 00:26:21 +1100888 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000889 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100890 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000891 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller07d86be2006-03-26 14:19:21 +1100893 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000894 sizeof(fd_mask));
895
Damien Miller95def091999-11-25 00:26:21 +1100896 /* Since we are blocking, ensure that all written packets have been sent. */
897 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898
Damien Miller95def091999-11-25 00:26:21 +1100899 /* Stay in the loop until we have received a complete packet. */
900 for (;;) {
901 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100902 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000903 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000904 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100905 || type == SSH_SMSG_FAILURE
906 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000907 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100908 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100909 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000910 if (type != SSH_MSG_NONE) {
911 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100912 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000913 }
Damien Miller5428f641999-11-25 11:54:57 +1100914 /*
915 * Otherwise, wait for some data to arrive, add it to the
916 * buffer, and try again.
917 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000918 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
919 sizeof(fd_mask));
920 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100921
Damien Miller95def091999-11-25 00:26:21 +1100922 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000923 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000924 (errno == EAGAIN || errno == EINTR))
925 ;
Damien Miller5428f641999-11-25 11:54:57 +1100926
Damien Miller95def091999-11-25 00:26:21 +1100927 /* Read data from the socket. */
928 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100929 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000930 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000931 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100932 }
Damien Miller95def091999-11-25 00:26:21 +1100933 if (len < 0)
934 fatal("Read from socket failed: %.100s", strerror(errno));
935 /* Append it to the buffer. */
936 packet_process_incoming(buf, len);
937 }
938 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939}
940
Damien Miller278f9072001-12-21 15:00:19 +1100941int
Damien Millerdff50992002-01-22 23:16:32 +1100942packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100943{
Damien Millerdff50992002-01-22 23:16:32 +1100944 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100945}
946
Damien Miller5428f641999-11-25 11:54:57 +1100947/*
948 * Waits until a packet has been received, verifies that its type matches
949 * that given, and gives a fatal error and exits if there is a mismatch.
950 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
952void
Damien Millerdff50992002-01-22 23:16:32 +1100953packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954{
Damien Miller95def091999-11-25 00:26:21 +1100955 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
Damien Millerdff50992002-01-22 23:16:32 +1100957 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100958 if (type != expected_type)
959 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000960 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961}
962
963/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100964 * packet_process_incoming. If so, reads the packet; otherwise returns
965 * SSH_MSG_NONE. This does not wait for data from the connection.
966 *
967 * SSH_MSG_DISCONNECT is handled specially here. Also,
968 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
969 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100970 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Ben Lindstrombba81212001-06-25 05:01:22 +0000972static int
Damien Millerdff50992002-01-22 23:16:32 +1100973packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000975 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000976 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000977 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 /* Check if input size is less than minimum packet size. */
980 if (buffer_len(&input) < 4 + 8)
981 return SSH_MSG_NONE;
982 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000983 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100984 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100985 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000986 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100987 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988
Damien Miller95def091999-11-25 00:26:21 +1100989 /* Check if the packet has been entirely received. */
990 if (buffer_len(&input) < 4 + padded_len)
991 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992
Damien Miller95def091999-11-25 00:26:21 +1100993 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller95def091999-11-25 00:26:21 +1100995 /* Consume packet length. */
996 buffer_consume(&input, 4);
997
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000998 /*
999 * Cryptographic attack detector for ssh
1000 * (C)1998 CORE-SDI, Buenos Aires Argentina
1001 * Ariel Futoransky(futo@core-sdi.com)
1002 */
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001003 if (!receive_context.plaintext) {
1004 switch (detect_attack(buffer_ptr(&input), padded_len)) {
1005 case DEATTACK_DETECTED:
1006 packet_disconnect("crc32 compensation attack: "
1007 "network attack detected");
1008 case DEATTACK_DOS_DETECTED:
1009 packet_disconnect("deattack denial of "
1010 "service detected");
1011 }
1012 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001013
1014 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001015 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001016 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001017 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001018
Damien Miller95def091999-11-25 00:26:21 +11001019 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020
1021#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001022 fprintf(stderr, "read_poll plain: ");
1023 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001025
Damien Miller95def091999-11-25 00:26:21 +11001026 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001027 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001028 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001029
Damien Miller95def091999-11-25 00:26:21 +11001030 /* Skip padding. */
1031 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001032
Damien Miller95def091999-11-25 00:26:21 +11001033 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001034 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001035 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001036 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001038 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001039 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001040 if (checksum != stored_checksum)
1041 packet_disconnect("Corrupted check bytes on input.");
1042 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043
Damien Miller95def091999-11-25 00:26:21 +11001044 if (packet_compression) {
1045 buffer_clear(&compression_buffer);
1046 buffer_uncompress(&incoming_packet, &compression_buffer);
1047 buffer_clear(&incoming_packet);
1048 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001049 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001050 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001051 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001052 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1053 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001054 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055}
Damien Miller95def091999-11-25 00:26:21 +11001056
Ben Lindstrombba81212001-06-25 05:01:22 +00001057static int
Damien Millerdff50992002-01-22 23:16:32 +11001058packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001059{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001060 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001061 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001062 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001063 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001064 Enc *enc = NULL;
1065 Mac *mac = NULL;
1066 Comp *comp = NULL;
1067
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001068 if (newkeys[MODE_IN] != NULL) {
1069 enc = &newkeys[MODE_IN]->enc;
1070 mac = &newkeys[MODE_IN]->mac;
1071 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001072 }
1073 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001074 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001075
1076 if (packet_length == 0) {
1077 /*
1078 * check if input size is less than the cipher block size,
1079 * decrypt first block and extract length of incoming packet
1080 */
1081 if (buffer_len(&input) < block_size)
1082 return SSH_MSG_NONE;
1083 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001084 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001085 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001086 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001087 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001088 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001089 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001090#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001091 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001092#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001093 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001094 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001095 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001096 buffer_consume(&input, block_size);
1097 }
1098 /* we have a partial packet of block_size bytes */
1099 need = 4 + packet_length - block_size;
1100 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1101 need, maclen));
1102 if (need % block_size != 0)
1103 fatal("padding error: need %d block %d mod %d",
1104 need, block_size, need % block_size);
1105 /*
1106 * check if the entire packet has been received and
1107 * decrypt into incoming_packet
1108 */
1109 if (buffer_len(&input) < need + maclen)
1110 return SSH_MSG_NONE;
1111#ifdef PACKET_DEBUG
1112 fprintf(stderr, "read_poll enc/full: ");
1113 buffer_dump(&input);
1114#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001115 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001116 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001117 buffer_consume(&input, need);
1118 /*
1119 * compute MAC over seqnr and packet,
1120 * increment sequence number for incoming packet
1121 */
Damien Miller4af51302000-04-16 11:18:38 +10001122 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001123 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001124 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001125 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001126 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001127 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001128 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001129 buffer_consume(&input, mac->mac_len);
1130 }
Damien Miller278f9072001-12-21 15:00:19 +11001131 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001132 *seqnr_p = p_read.seqnr;
1133 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001134 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001135 if (++p_read.packets == 0)
1136 if (!(datafellows & SSH_BUG_NOREKEY))
1137 fatal("XXX too many packets with same key");
1138 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001139
1140 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001141 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001142 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001143 DBG(debug("input: padlen %d", padlen));
1144 if (padlen < 4)
1145 packet_disconnect("Corrupted padlen %d on input.", padlen);
1146
1147 /* skip packet size + padlen, discard padding */
1148 buffer_consume(&incoming_packet, 4 + 1);
1149 buffer_consume_end(&incoming_packet, padlen);
1150
1151 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1152 if (comp && comp->enabled) {
1153 buffer_clear(&compression_buffer);
1154 buffer_uncompress(&incoming_packet, &compression_buffer);
1155 buffer_clear(&incoming_packet);
1156 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1157 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001158 DBG(debug("input: len after de-compress %d",
1159 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001160 }
1161 /*
1162 * get packet type, implies consume.
1163 * return length of payload (without type field)
1164 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001165 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001166 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1167 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001168 if (type == SSH2_MSG_NEWKEYS)
1169 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001170 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1171 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001172#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001173 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001174 buffer_dump(&incoming_packet);
1175#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001176 /* reset for next packet */
1177 packet_length = 0;
1178 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001179}
1180
1181int
Damien Millerdff50992002-01-22 23:16:32 +11001182packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001183{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001184 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001185 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001186 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001187
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001188 for (;;) {
1189 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001190 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001191 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001192 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001193 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001194 case SSH2_MSG_IGNORE:
1195 break;
1196 case SSH2_MSG_DEBUG:
1197 packet_get_char();
1198 msg = packet_get_string(NULL);
1199 debug("Remote: %.900s", msg);
1200 xfree(msg);
1201 msg = packet_get_string(NULL);
1202 xfree(msg);
1203 break;
1204 case SSH2_MSG_DISCONNECT:
1205 reason = packet_get_int();
1206 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001207 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001208 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001209 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001210 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001211 break;
Damien Miller659811f2002-01-22 23:23:11 +11001212 case SSH2_MSG_UNIMPLEMENTED:
1213 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001214 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1215 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001216 break;
Damien Miller33b13562000-04-04 14:38:59 +10001217 default:
1218 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001219 }
Damien Miller33b13562000-04-04 14:38:59 +10001220 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001221 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001222 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001223 case SSH_MSG_IGNORE:
1224 break;
1225 case SSH_MSG_DEBUG:
1226 msg = packet_get_string(NULL);
1227 debug("Remote: %.900s", msg);
1228 xfree(msg);
1229 break;
1230 case SSH_MSG_DISCONNECT:
1231 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001232 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001233 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001234 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001235 xfree(msg);
1236 break;
1237 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001238 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001239 DBG(debug("received packet type %d", type));
1240 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001241 }
Damien Miller33b13562000-04-04 14:38:59 +10001242 }
1243 }
1244}
1245
Damien Miller278f9072001-12-21 15:00:19 +11001246int
Damien Millerdff50992002-01-22 23:16:32 +11001247packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001248{
Damien Millerdff50992002-01-22 23:16:32 +11001249 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001250}
1251
Damien Miller5428f641999-11-25 11:54:57 +11001252/*
1253 * Buffers the given amount of input characters. This is intended to be used
1254 * together with packet_read_poll.
1255 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256
1257void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001258packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259{
Damien Miller95def091999-11-25 00:26:21 +11001260 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261}
1262
1263/* Returns a character from the packet. */
1264
Ben Lindstrom46c16222000-12-22 01:43:59 +00001265u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001266packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267{
Damien Miller95def091999-11-25 00:26:21 +11001268 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001269
Damien Miller95def091999-11-25 00:26:21 +11001270 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001271 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272}
1273
1274/* Returns an integer from the packet data. */
1275
Ben Lindstrom46c16222000-12-22 01:43:59 +00001276u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001277packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001278{
Damien Miller95def091999-11-25 00:26:21 +11001279 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001280}
1281
Damien Miller5428f641999-11-25 11:54:57 +11001282/*
1283 * Returns an arbitrary precision integer from the packet data. The integer
1284 * must have been initialized before this call.
1285 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001286
1287void
Damien Millerd432ccf2002-01-22 23:14:44 +11001288packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001289{
Damien Miller76e1e362002-01-22 23:15:57 +11001290 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001291}
1292
Damien Miller33b13562000-04-04 14:38:59 +10001293void
Damien Millerd432ccf2002-01-22 23:14:44 +11001294packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001295{
Damien Miller76e1e362002-01-22 23:15:57 +11001296 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001297}
1298
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001299void *
Damien Millereccb9de2005-06-17 12:59:34 +10001300packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001301{
Damien Millereccb9de2005-06-17 12:59:34 +10001302 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001303
Damien Miller33b13562000-04-04 14:38:59 +10001304 if (length_ptr != NULL)
1305 *length_ptr = bytes;
1306 return buffer_ptr(&incoming_packet);
1307}
1308
Damien Miller4af51302000-04-16 11:18:38 +10001309int
1310packet_remaining(void)
1311{
1312 return buffer_len(&incoming_packet);
1313}
1314
Damien Miller5428f641999-11-25 11:54:57 +11001315/*
1316 * Returns a string from the packet data. The string is allocated using
1317 * xmalloc; it is the responsibility of the calling program to free it when
1318 * no longer needed. The length_ptr argument may be NULL, or point to an
1319 * integer into which the length of the string is stored.
1320 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001321
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001322void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001323packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001324{
Damien Miller95def091999-11-25 00:26:21 +11001325 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001326}
1327
Damien Miller5428f641999-11-25 11:54:57 +11001328/*
1329 * Sends a diagnostic message from the server to the client. This message
1330 * can be sent at any time (but not while constructing another message). The
1331 * message is printed immediately, but only if the client is being executed
1332 * in verbose mode. These messages are primarily intended to ease debugging
1333 * authentication problems. The length of the formatted message must not
1334 * exceed 1024 bytes. This will automatically call packet_write_wait.
1335 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001336
1337void
Damien Miller95def091999-11-25 00:26:21 +11001338packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001339{
Damien Miller95def091999-11-25 00:26:21 +11001340 char buf[1024];
1341 va_list args;
1342
Ben Lindstroma14ee472000-12-07 01:24:58 +00001343 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1344 return;
1345
Damien Miller95def091999-11-25 00:26:21 +11001346 va_start(args, fmt);
1347 vsnprintf(buf, sizeof(buf), fmt, args);
1348 va_end(args);
1349
Damien Miller7c8af4f2000-05-01 08:24:07 +10001350 if (compat20) {
1351 packet_start(SSH2_MSG_DEBUG);
1352 packet_put_char(0); /* bool: always display */
1353 packet_put_cstring(buf);
1354 packet_put_cstring("");
1355 } else {
1356 packet_start(SSH_MSG_DEBUG);
1357 packet_put_cstring(buf);
1358 }
Damien Miller95def091999-11-25 00:26:21 +11001359 packet_send();
1360 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361}
1362
Damien Miller5428f641999-11-25 11:54:57 +11001363/*
1364 * Logs the error plus constructs and sends a disconnect packet, closes the
1365 * connection, and exits. This function never returns. The error message
1366 * should not contain a newline. The length of the formatted message must
1367 * not exceed 1024 bytes.
1368 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001369
1370void
Damien Miller95def091999-11-25 00:26:21 +11001371packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001372{
Damien Miller95def091999-11-25 00:26:21 +11001373 char buf[1024];
1374 va_list args;
1375 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001376
Damien Miller95def091999-11-25 00:26:21 +11001377 if (disconnecting) /* Guard against recursive invocations. */
1378 fatal("packet_disconnect called recursively.");
1379 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001380
Damien Miller5428f641999-11-25 11:54:57 +11001381 /*
1382 * Format the message. Note that the caller must make sure the
1383 * message is of limited size.
1384 */
Damien Miller95def091999-11-25 00:26:21 +11001385 va_start(args, fmt);
1386 vsnprintf(buf, sizeof(buf), fmt, args);
1387 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001388
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001389 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001390 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001391
Damien Miller95def091999-11-25 00:26:21 +11001392 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001393 if (compat20) {
1394 packet_start(SSH2_MSG_DISCONNECT);
1395 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1396 packet_put_cstring(buf);
1397 packet_put_cstring("");
1398 } else {
1399 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001400 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001401 }
Damien Miller95def091999-11-25 00:26:21 +11001402 packet_send();
1403 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404
Damien Miller95def091999-11-25 00:26:21 +11001405 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001406 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407
Damien Miller95def091999-11-25 00:26:21 +11001408 /* Close the connection. */
1409 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001410 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411}
1412
Damien Miller5428f641999-11-25 11:54:57 +11001413/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414
1415void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001416packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417{
Damien Miller95def091999-11-25 00:26:21 +11001418 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001419
Damien Miller95def091999-11-25 00:26:21 +11001420 if (len > 0) {
1421 len = write(connection_out, buffer_ptr(&output), len);
1422 if (len <= 0) {
1423 if (errno == EAGAIN)
1424 return;
1425 else
1426 fatal("Write failed: %.100s", strerror(errno));
1427 }
1428 buffer_consume(&output, len);
1429 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430}
1431
Damien Miller5428f641999-11-25 11:54:57 +11001432/*
1433 * Calls packet_write_poll repeatedly until all pending output data has been
1434 * written.
1435 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436
1437void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001438packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001439{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001440 fd_set *setp;
1441
Damien Miller07d86be2006-03-26 14:19:21 +11001442 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001443 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001444 packet_write_poll();
1445 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001446 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1447 sizeof(fd_mask));
1448 FD_SET(connection_out, setp);
1449 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001450 (errno == EAGAIN || errno == EINTR))
1451 ;
Damien Miller95def091999-11-25 00:26:21 +11001452 packet_write_poll();
1453 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001454 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455}
1456
1457/* Returns true if there is buffered data to write to the connection. */
1458
1459int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001460packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461{
Damien Miller95def091999-11-25 00:26:21 +11001462 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001463}
1464
1465/* Returns true if there is not too much data to write to the connection. */
1466
1467int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001468packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469{
Damien Miller95def091999-11-25 00:26:21 +11001470 if (interactive_mode)
1471 return buffer_len(&output) < 16384;
1472 else
1473 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474}
1475
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001476
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001477static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001478packet_set_tos(int interactive)
1479{
Damien Miller5924ceb2003-11-22 15:02:42 +11001480#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001481 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1482
1483 if (!packet_connection_is_on_socket() ||
1484 !packet_connection_is_ipv4())
1485 return;
1486 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1487 sizeof(tos)) < 0)
1488 error("setsockopt IP_TOS %d: %.100s:",
1489 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001490#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001491}
Ben Lindstroma7433982002-12-23 02:41:41 +00001492
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493/* Informs that the current session is interactive. Sets IP flags for that. */
1494
1495void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001496packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001498 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001500 if (called)
1501 return;
1502 called = 1;
1503
Damien Miller95def091999-11-25 00:26:21 +11001504 /* Record that we are in interactive mode. */
1505 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506
Damien Miller34132e52000-01-14 15:45:46 +11001507 /* Only set socket options if using a socket. */
1508 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001509 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001510 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001511 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001512}
1513
1514/* Returns true if the current connection is interactive. */
1515
1516int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001517packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001518{
Damien Miller95def091999-11-25 00:26:21 +11001519 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001520}
Damien Miller6162d121999-11-21 13:23:52 +11001521
Darren Tucker1f8311c2004-05-13 16:39:33 +10001522int
Darren Tucker502d3842003-06-28 12:38:01 +10001523packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001524{
Damien Miller95def091999-11-25 00:26:21 +11001525 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001526
Damien Miller95def091999-11-25 00:26:21 +11001527 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001528 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001529 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001530 return -1;
1531 }
1532 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001533 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001534 return -1;
1535 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001536 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001537 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001538 max_packet_size = s;
1539 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001540}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001541
Damien Miller9f643902001-11-12 11:02:52 +11001542/* roundup current message to pad bytes */
1543void
1544packet_add_padding(u_char pad)
1545{
1546 extra_pad = pad;
1547}
1548
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001549/*
1550 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001551 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001552 * byte SSH_MSG_IGNORE
1553 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001554 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001555 * All implementations MUST understand (and ignore) this message at any
1556 * time (after receiving the protocol version). No implementation is
1557 * required to send them. This message can be used as an additional
1558 * protection measure against advanced traffic analysis techniques.
1559 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001560void
1561packet_send_ignore(int nbytes)
1562{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001563 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001564 int i;
1565
1566 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001567 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001568 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001569 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001570 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001571 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001572 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001573 }
1574}
Damien Millera5539d22003-04-09 20:50:06 +10001575
Darren Tucker1f8311c2004-05-13 16:39:33 +10001576#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001577int
1578packet_need_rekeying(void)
1579{
1580 if (datafellows & SSH_BUG_NOREKEY)
1581 return 0;
1582 return
1583 (p_send.packets > MAX_PACKETS) ||
1584 (p_read.packets > MAX_PACKETS) ||
1585 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1586 (max_blocks_in && (p_read.blocks > max_blocks_in));
1587}
1588
1589void
1590packet_set_rekey_limit(u_int32_t bytes)
1591{
1592 rekey_limit = bytes;
1593}
Damien Miller9786e6e2005-07-26 21:54:56 +10001594
1595void
1596packet_set_server(void)
1597{
1598 server_side = 1;
1599}
1600
1601void
1602packet_set_authenticated(void)
1603{
1604 after_authentication = 1;
1605}