blob: acf72f77e7ece1d37979043a1a411e9688b26377 [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: packet.c,v 1.141 2006/08/01 23:22:47 stevesk 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>
Darren Tucker5d196262006-07-12 22:15:16 +100060
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061#include "xmalloc.h"
62#include "buffer.h"
63#include "packet.h"
64#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
67#include "compress.h"
68#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000069#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Miller33b13562000-04-04 14:38:59 +100071#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100073#include "ssh2.h"
74
Damien Miller874d77b2000-10-14 16:23:11 +110075#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100076#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000077#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000078#include "log.h"
79#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110080#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000081#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100082
83#ifdef PACKET_DEBUG
84#define DBG(x) x
85#else
86#define DBG(x)
87#endif
88
Damien Miller5428f641999-11-25 11:54:57 +110089/*
90 * This variable contains the file descriptors used for communicating with
91 * the other side. connection_in is used for reading; connection_out for
92 * writing. These can be the same descriptor, in which case it is assumed to
93 * be a socket.
94 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095static int connection_in = -1;
96static int connection_out = -1;
97
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000099static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
101/* Encryption context for receiving data. This is only used for decryption. */
102static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +1100103
104/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105static CipherContext send_context;
106
107/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000108Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
110/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000111Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
113/* Buffer for the partial outgoing packet being constructed. */
114static Buffer outgoing_packet;
115
116/* Buffer for the incoming packet currently being processed. */
117static Buffer incoming_packet;
118
119/* Scratch buffer for packet compression/decompression. */
120static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000121static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
123/* Flag indicating whether packet compression/decompression is enabled. */
124static int packet_compression = 0;
125
Damien Miller6162d121999-11-21 13:23:52 +1100126/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000127u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100128
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129/* Flag indicating whether this module has been initialized. */
130static int initialized = 0;
131
132/* Set to true if the connection is interactive. */
133static int interactive_mode = 0;
134
Damien Miller9786e6e2005-07-26 21:54:56 +1000135/* Set to true if we are the server side. */
136static int server_side = 0;
137
138/* Set to true if we are authenticated. */
139static int after_authentication = 0;
140
Damien Miller33b13562000-04-04 14:38:59 +1000141/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000142Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000143static struct packet_state {
144 u_int32_t seqnr;
145 u_int32_t packets;
146 u_int64_t blocks;
147} p_read, p_send;
148
149static u_int64_t max_blocks_in, max_blocks_out;
150static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000151
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000152/* Session key for protocol v1 */
153static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
154static u_int ssh1_keylen;
155
Damien Miller9f643902001-11-12 11:02:52 +1100156/* roundup current message to extra_pad bytes */
157static u_char extra_pad = 0;
158
Damien Millera5539d22003-04-09 20:50:06 +1000159struct packet {
160 TAILQ_ENTRY(packet) next;
161 u_char type;
162 Buffer payload;
163};
164TAILQ_HEAD(, packet) outgoing;
165
Damien Miller5428f641999-11-25 11:54:57 +1100166/*
167 * Sets the descriptors used for communication. Disables encryption until
168 * packet_set_encryption_key is called.
169 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170void
171packet_set_connection(int fd_in, int fd_out)
172{
Damien Miller874d77b2000-10-14 16:23:11 +1100173 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000174
Damien Miller874d77b2000-10-14 16:23:11 +1100175 if (none == NULL)
176 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100177 connection_in = fd_in;
178 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000179 cipher_init(&send_context, none, (const u_char *)"",
180 0, NULL, 0, CIPHER_ENCRYPT);
181 cipher_init(&receive_context, none, (const u_char *)"",
182 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000183 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100184 if (!initialized) {
185 initialized = 1;
186 buffer_init(&input);
187 buffer_init(&output);
188 buffer_init(&outgoing_packet);
189 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000190 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100191 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192}
193
Damien Miller34132e52000-01-14 15:45:46 +1100194/* Returns 1 if remote host is connected via socket, 0 if not. */
195
196int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000197packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100198{
199 struct sockaddr_storage from, to;
200 socklen_t fromlen, tolen;
201
202 /* filedescriptors in and out are the same, so it's a socket */
203 if (connection_in == connection_out)
204 return 1;
205 fromlen = sizeof(from);
206 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100207 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100208 return 0;
209 tolen = sizeof(to);
210 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100211 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100212 return 0;
213 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
214 return 0;
215 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
216 return 0;
217 return 1;
218}
219
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000220/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000221 * Exports an IV from the CipherContext required to export the key
222 * state back from the unprivileged child to the privileged parent
223 * process.
224 */
225
226void
227packet_get_keyiv(int mode, u_char *iv, u_int len)
228{
229 CipherContext *cc;
230
231 if (mode == MODE_OUT)
232 cc = &send_context;
233 else
234 cc = &receive_context;
235
236 cipher_get_keyiv(cc, iv, len);
237}
238
239int
240packet_get_keycontext(int mode, u_char *dat)
241{
242 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000243
Ben Lindstromf6027d32002-03-22 01:42:04 +0000244 if (mode == MODE_OUT)
245 cc = &send_context;
246 else
247 cc = &receive_context;
248
249 return (cipher_get_keycontext(cc, dat));
250}
251
252void
253packet_set_keycontext(int mode, u_char *dat)
254{
255 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000256
Ben Lindstromf6027d32002-03-22 01:42:04 +0000257 if (mode == MODE_OUT)
258 cc = &send_context;
259 else
260 cc = &receive_context;
261
262 cipher_set_keycontext(cc, dat);
263}
264
265int
266packet_get_keyiv_len(int mode)
267{
268 CipherContext *cc;
269
270 if (mode == MODE_OUT)
271 cc = &send_context;
272 else
273 cc = &receive_context;
274
275 return (cipher_get_keyiv_len(cc));
276}
Damien Miller4f7becb2006-03-26 14:10:14 +1100277
Ben Lindstromf6027d32002-03-22 01:42:04 +0000278void
279packet_set_iv(int mode, u_char *dat)
280{
281 CipherContext *cc;
282
283 if (mode == MODE_OUT)
284 cc = &send_context;
285 else
286 cc = &receive_context;
287
288 cipher_set_keyiv(cc, dat);
289}
Damien Miller4f7becb2006-03-26 14:10:14 +1100290
Ben Lindstromf6027d32002-03-22 01:42:04 +0000291int
Damien Miller2b92d322003-06-11 22:05:06 +1000292packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000293{
294 return (cipher_get_number(receive_context.cipher));
295}
296
Damien Millera5539d22003-04-09 20:50:06 +1000297void
298packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000299{
Damien Millera5539d22003-04-09 20:50:06 +1000300 struct packet_state *state;
301
302 state = (mode == MODE_IN) ? &p_read : &p_send;
303 *seqnr = state->seqnr;
304 *blocks = state->blocks;
305 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000306}
307
308void
Damien Millera5539d22003-04-09 20:50:06 +1000309packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000310{
Damien Millera5539d22003-04-09 20:50:06 +1000311 struct packet_state *state;
312
313 state = (mode == MODE_IN) ? &p_read : &p_send;
314 state->seqnr = seqnr;
315 state->blocks = blocks;
316 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000317}
318
Damien Miller34132e52000-01-14 15:45:46 +1100319/* returns 1 if connection is via ipv4 */
320
321int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000322packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100323{
324 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100325 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100326
327 memset(&to, 0, sizeof(to));
328 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
329 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000330 if (to.ss_family == AF_INET)
331 return 1;
332#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100333 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000334 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
335 return 1;
336#endif
337 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100338}
339
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340/* Sets the connection into non-blocking mode. */
341
342void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000343packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344{
Damien Miller95def091999-11-25 00:26:21 +1100345 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000346 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347
Damien Miller232711f2004-06-15 10:35:30 +1000348 if (connection_out != connection_in)
349 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350}
351
352/* Returns the socket used for reading. */
353
354int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000355packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356{
Damien Miller95def091999-11-25 00:26:21 +1100357 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358}
359
360/* Returns the descriptor used for writing. */
361
362int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000363packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364{
Damien Miller95def091999-11-25 00:26:21 +1100365 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366}
367
368/* Closes the connection and clears and frees internal data structures. */
369
370void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000371packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372{
Damien Miller95def091999-11-25 00:26:21 +1100373 if (!initialized)
374 return;
375 initialized = 0;
376 if (connection_in == connection_out) {
377 shutdown(connection_out, SHUT_RDWR);
378 close(connection_out);
379 } else {
380 close(connection_in);
381 close(connection_out);
382 }
383 buffer_free(&input);
384 buffer_free(&output);
385 buffer_free(&outgoing_packet);
386 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000387 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100388 buffer_free(&compression_buffer);
389 buffer_compress_uninit();
390 }
Damien Miller963f6b22002-02-19 15:21:23 +1100391 cipher_cleanup(&send_context);
392 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393}
394
395/* Sets remote side protocol flags. */
396
397void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000398packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399{
Damien Miller95def091999-11-25 00:26:21 +1100400 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401}
402
403/* Returns the remote protocol flags set earlier by the above function. */
404
Ben Lindstrom46c16222000-12-22 01:43:59 +0000405u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000406packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407{
Damien Miller95def091999-11-25 00:26:21 +1100408 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409}
410
Damien Miller5428f641999-11-25 11:54:57 +1100411/*
412 * Starts packet compression from the next packet on in both directions.
413 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
414 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Ben Lindstrombba81212001-06-25 05:01:22 +0000416static void
417packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000418{
419 if (compression_buffer_ready == 1)
420 return;
421 compression_buffer_ready = 1;
422 buffer_init(&compression_buffer);
423}
424
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425void
426packet_start_compression(int level)
427{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000428 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100429 fatal("Compression already enabled.");
430 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000431 packet_init_compression();
432 buffer_compress_init_send(level);
433 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434}
435
Damien Miller5428f641999-11-25 11:54:57 +1100436/*
Damien Miller5428f641999-11-25 11:54:57 +1100437 * Causes any further packets to be encrypted using the given key. The same
438 * key is used for both sending and reception. However, both directions are
439 * encrypted independently of each other.
440 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000441
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000443packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100444 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445{
Damien Miller874d77b2000-10-14 16:23:11 +1100446 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000447
Damien Miller874d77b2000-10-14 16:23:11 +1100448 if (cipher == NULL)
449 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000450 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100451 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000452 if (keylen > SSH_SESSION_KEY_LENGTH)
453 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
454 memcpy(ssh1_key, key, keylen);
455 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100456 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
457 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458}
459
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000460u_int
461packet_get_encryption_key(u_char *key)
462{
463 if (key == NULL)
464 return (ssh1_keylen);
465 memcpy(key, ssh1_key, ssh1_keylen);
466 return (ssh1_keylen);
467}
468
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000469/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000471packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000473 u_char buf[9];
474 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475
Ben Lindstrom204e4882001-03-05 06:47:00 +0000476 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000477 len = compat20 ? 6 : 9;
478 memset(buf, 0, len - 1);
479 buf[len - 1] = type;
480 buffer_clear(&outgoing_packet);
481 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000482}
483
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000484/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485void
486packet_put_char(int value)
487{
Damien Miller95def091999-11-25 00:26:21 +1100488 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000489
Damien Miller95def091999-11-25 00:26:21 +1100490 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491}
Damien Miller4f7becb2006-03-26 14:10:14 +1100492
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000494packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495{
Damien Miller95def091999-11-25 00:26:21 +1100496 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497}
Damien Miller4f7becb2006-03-26 14:10:14 +1100498
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100500packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501{
Damien Miller95def091999-11-25 00:26:21 +1100502 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503}
Damien Miller4f7becb2006-03-26 14:10:14 +1100504
Damien Miller33b13562000-04-04 14:38:59 +1000505void
506packet_put_cstring(const char *str)
507{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000508 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000509}
Damien Miller4f7becb2006-03-26 14:10:14 +1100510
Damien Miller33b13562000-04-04 14:38:59 +1000511void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100512packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000513{
514 buffer_append(&outgoing_packet, buf, len);
515}
Damien Miller4f7becb2006-03-26 14:10:14 +1100516
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517void
Damien Miller95def091999-11-25 00:26:21 +1100518packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519{
Damien Miller95def091999-11-25 00:26:21 +1100520 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521}
Damien Miller4f7becb2006-03-26 14:10:14 +1100522
Damien Miller33b13562000-04-04 14:38:59 +1000523void
524packet_put_bignum2(BIGNUM * value)
525{
526 buffer_put_bignum2(&outgoing_packet, value);
527}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Damien Miller5428f641999-11-25 11:54:57 +1100529/*
530 * Finalizes and sends the packet. If the encryption key has been set,
531 * encrypts the packet before sending.
532 */
Damien Miller95def091999-11-25 00:26:21 +1100533
Ben Lindstrombba81212001-06-25 05:01:22 +0000534static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000535packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000537 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100538 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000539 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000540 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541
Damien Miller5428f641999-11-25 11:54:57 +1100542 /*
543 * If using packet compression, compress the payload of the outgoing
544 * packet.
545 */
Damien Miller95def091999-11-25 00:26:21 +1100546 if (packet_compression) {
547 buffer_clear(&compression_buffer);
548 /* Skip padding. */
549 buffer_consume(&outgoing_packet, 8);
550 /* padding */
551 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
552 buffer_compress(&outgoing_packet, &compression_buffer);
553 buffer_clear(&outgoing_packet);
554 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100555 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100556 }
557 /* Compute packet length without padding (add checksum, remove padding). */
558 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559
Damien Millere247cc42000-05-07 12:03:14 +1000560 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100561 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100562 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100563 cp = buffer_ptr(&outgoing_packet);
564 for (i = 0; i < padding; i++) {
565 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000566 rnd = arc4random();
567 cp[7 - i] = rnd & 0xff;
568 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100569 }
570 }
571 buffer_consume(&outgoing_packet, 8 - padding);
572
573 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100574 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000575 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100576 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100577 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
579#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100580 fprintf(stderr, "packet_send plain: ");
581 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582#endif
583
Damien Miller95def091999-11-25 00:26:21 +1100584 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100585 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100586 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100587 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100588 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100589 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100590
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100592 fprintf(stderr, "encrypted: ");
593 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594#endif
595
Damien Miller95def091999-11-25 00:26:21 +1100596 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597
Damien Miller5428f641999-11-25 11:54:57 +1100598 /*
Damien Miller788f2122005-11-05 15:14:59 +1100599 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100600 * actually sent until packet_write_wait or packet_write_poll is
601 * called.
602 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603}
604
Ben Lindstromf6027d32002-03-22 01:42:04 +0000605void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000606set_newkeys(int mode)
607{
608 Enc *enc;
609 Mac *mac;
610 Comp *comp;
611 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000612 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000613 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000614
Ben Lindstrom064496f2002-12-23 02:04:22 +0000615 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000616
Damien Miller963f6b22002-02-19 15:21:23 +1100617 if (mode == MODE_OUT) {
618 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000619 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000620 p_send.packets = p_send.blocks = 0;
621 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100622 } else {
623 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000624 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000625 p_read.packets = p_read.blocks = 0;
626 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100627 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000628 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000629 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100630 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000631 enc = &newkeys[mode]->enc;
632 mac = &newkeys[mode]->mac;
633 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000634 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000635 xfree(enc->name);
636 xfree(enc->iv);
637 xfree(enc->key);
638 xfree(mac->name);
639 xfree(mac->key);
640 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000641 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000642 }
643 newkeys[mode] = kex_get_newkeys(mode);
644 if (newkeys[mode] == NULL)
645 fatal("newkeys: no keys for mode %d", mode);
646 enc = &newkeys[mode]->enc;
647 mac = &newkeys[mode]->mac;
648 comp = &newkeys[mode]->comp;
649 if (mac->md != NULL)
650 mac->enabled = 1;
651 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100652 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000653 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000654 /* Deleting the keys does not gain extra security */
655 /* memset(enc->iv, 0, enc->block_size);
656 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000657 if ((comp->type == COMP_ZLIB ||
658 (comp->type == COMP_DELAYED && after_authentication)) &&
659 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000660 packet_init_compression();
661 if (mode == MODE_OUT)
662 buffer_compress_init_send(6);
663 else
664 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000665 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000666 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000667 /*
668 * The 2^(blocksize*2) limit is too expensive for 3DES,
669 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
670 */
671 if (enc->block_size >= 16)
672 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
673 else
674 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000675 if (rekey_limit)
676 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000677}
678
Damien Miller5428f641999-11-25 11:54:57 +1100679/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000680 * Delayed compression for SSH2 is enabled after authentication:
681 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
682 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
683 */
684static void
685packet_enable_delayed_compress(void)
686{
687 Comp *comp = NULL;
688 int mode;
689
690 /*
691 * Remember that we are past the authentication step, so rekeying
692 * with COMP_DELAYED will turn on compression immediately.
693 */
694 after_authentication = 1;
695 for (mode = 0; mode < MODE_MAX; mode++) {
696 comp = &newkeys[mode]->comp;
697 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000698 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000699 if (mode == MODE_OUT)
700 buffer_compress_init_send(6);
701 else
702 buffer_compress_init_recv();
703 comp->enabled = 1;
704 }
705 }
706}
707
708/*
Damien Miller33b13562000-04-04 14:38:59 +1000709 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
710 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000711static void
Damien Millera5539d22003-04-09 20:50:06 +1000712packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000713{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000714 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100715 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000716 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100717 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000718 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000719 Enc *enc = NULL;
720 Mac *mac = NULL;
721 Comp *comp = NULL;
722 int block_size;
723
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000724 if (newkeys[MODE_OUT] != NULL) {
725 enc = &newkeys[MODE_OUT]->enc;
726 mac = &newkeys[MODE_OUT]->mac;
727 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000728 }
Damien Miller963f6b22002-02-19 15:21:23 +1100729 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000730
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000731 cp = buffer_ptr(&outgoing_packet);
732 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000733
734#ifdef PACKET_DEBUG
735 fprintf(stderr, "plain: ");
736 buffer_dump(&outgoing_packet);
737#endif
738
739 if (comp && comp->enabled) {
740 len = buffer_len(&outgoing_packet);
741 /* skip header, compress only payload */
742 buffer_consume(&outgoing_packet, 5);
743 buffer_clear(&compression_buffer);
744 buffer_compress(&outgoing_packet, &compression_buffer);
745 buffer_clear(&outgoing_packet);
746 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
747 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
748 buffer_len(&compression_buffer));
749 DBG(debug("compression: raw %d compressed %d", len,
750 buffer_len(&outgoing_packet)));
751 }
752
753 /* sizeof (packet_len + pad_len + payload) */
754 len = buffer_len(&outgoing_packet);
755
756 /*
757 * calc size of padding, alloc space, get random data,
758 * minimum padding is 4 bytes
759 */
760 padlen = block_size - (len % block_size);
761 if (padlen < 4)
762 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100763 if (extra_pad) {
764 /* will wrap if extra_pad+padlen > 255 */
765 extra_pad = roundup(extra_pad, block_size);
766 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000767 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100768 pad, len, padlen, extra_pad);
769 padlen += pad;
770 extra_pad = 0;
771 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100772 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100773 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000774 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000775 for (i = 0; i < padlen; i++) {
776 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000777 rnd = arc4random();
778 cp[i] = rnd & 0xff;
779 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000780 }
Damien Millere247cc42000-05-07 12:03:14 +1000781 } else {
782 /* clear padding */
783 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000784 }
785 /* packet_length includes payload, padding and padding length field */
786 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000787 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100788 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000789 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000790 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
791
792 /* compute MAC over seqnr and packet(length fields, payload, padding) */
793 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000794 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100795 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000796 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000797 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000798 }
799 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100800 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100801 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000802 buffer_len(&outgoing_packet));
803 /* append unencrypted MAC */
804 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100805 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000806#ifdef PACKET_DEBUG
807 fprintf(stderr, "encrypted: ");
808 buffer_dump(&output);
809#endif
Damien Miller4af51302000-04-16 11:18:38 +1000810 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000811 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000812 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000813 if (++p_send.packets == 0)
814 if (!(datafellows & SSH_BUG_NOREKEY))
815 fatal("XXX too many packets with same key");
816 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000817 buffer_clear(&outgoing_packet);
818
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000819 if (type == SSH2_MSG_NEWKEYS)
820 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000821 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
822 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000823}
824
Damien Millera5539d22003-04-09 20:50:06 +1000825static void
826packet_send2(void)
827{
828 static int rekeying = 0;
829 struct packet *p;
830 u_char type, *cp;
831
832 cp = buffer_ptr(&outgoing_packet);
833 type = cp[5];
834
835 /* during rekeying we can only send key exchange messages */
836 if (rekeying) {
837 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
838 (type <= SSH2_MSG_TRANSPORT_MAX))) {
839 debug("enqueue packet: %u", type);
840 p = xmalloc(sizeof(*p));
841 p->type = type;
842 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
843 buffer_init(&outgoing_packet);
844 TAILQ_INSERT_TAIL(&outgoing, p, next);
845 return;
846 }
847 }
848
849 /* rekeying starts with sending KEXINIT */
850 if (type == SSH2_MSG_KEXINIT)
851 rekeying = 1;
852
853 packet_send2_wrapped();
854
855 /* after a NEWKEYS message we can send the complete queue */
856 if (type == SSH2_MSG_NEWKEYS) {
857 rekeying = 0;
858 while ((p = TAILQ_FIRST(&outgoing))) {
859 type = p->type;
860 debug("dequeue packet: %u", type);
861 buffer_free(&outgoing_packet);
862 memcpy(&outgoing_packet, &p->payload,
863 sizeof(Buffer));
864 TAILQ_REMOVE(&outgoing, p, next);
865 xfree(p);
866 packet_send2_wrapped();
867 }
868 }
869}
870
Damien Miller33b13562000-04-04 14:38:59 +1000871void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000872packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000873{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000874 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000875 packet_send2();
876 else
877 packet_send1();
878 DBG(debug("packet_send done"));
879}
880
Damien Miller33b13562000-04-04 14:38:59 +1000881/*
Damien Miller5428f641999-11-25 11:54:57 +1100882 * Waits until a packet has been received, and returns its type. Note that
883 * no other data is processed until this returns, so this function should not
884 * be used during the interactive session.
885 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886
887int
Damien Millerdff50992002-01-22 23:16:32 +1100888packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889{
Damien Miller95def091999-11-25 00:26:21 +1100890 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000891 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100892 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000893 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894
Damien Miller07d86be2006-03-26 14:19:21 +1100895 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000896 sizeof(fd_mask));
897
Damien Miller95def091999-11-25 00:26:21 +1100898 /* Since we are blocking, ensure that all written packets have been sent. */
899 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900
Damien Miller95def091999-11-25 00:26:21 +1100901 /* Stay in the loop until we have received a complete packet. */
902 for (;;) {
903 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100904 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000905 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000906 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100907 || type == SSH_SMSG_FAILURE
908 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000909 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100910 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100911 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000912 if (type != SSH_MSG_NONE) {
913 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100914 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000915 }
Damien Miller5428f641999-11-25 11:54:57 +1100916 /*
917 * Otherwise, wait for some data to arrive, add it to the
918 * buffer, and try again.
919 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000920 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
921 sizeof(fd_mask));
922 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100923
Damien Miller95def091999-11-25 00:26:21 +1100924 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000925 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000926 (errno == EAGAIN || errno == EINTR))
927 ;
Damien Miller5428f641999-11-25 11:54:57 +1100928
Damien Miller95def091999-11-25 00:26:21 +1100929 /* Read data from the socket. */
930 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100931 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000932 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000933 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100934 }
Damien Miller95def091999-11-25 00:26:21 +1100935 if (len < 0)
936 fatal("Read from socket failed: %.100s", strerror(errno));
937 /* Append it to the buffer. */
938 packet_process_incoming(buf, len);
939 }
940 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941}
942
Damien Miller278f9072001-12-21 15:00:19 +1100943int
Damien Millerdff50992002-01-22 23:16:32 +1100944packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100945{
Damien Millerdff50992002-01-22 23:16:32 +1100946 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100947}
948
Damien Miller5428f641999-11-25 11:54:57 +1100949/*
950 * Waits until a packet has been received, verifies that its type matches
951 * that given, and gives a fatal error and exits if there is a mismatch.
952 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953
954void
Damien Millerdff50992002-01-22 23:16:32 +1100955packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956{
Damien Miller95def091999-11-25 00:26:21 +1100957 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
Damien Millerdff50992002-01-22 23:16:32 +1100959 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100960 if (type != expected_type)
961 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000962 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963}
964
965/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100966 * packet_process_incoming. If so, reads the packet; otherwise returns
967 * SSH_MSG_NONE. This does not wait for data from the connection.
968 *
969 * SSH_MSG_DISCONNECT is handled specially here. Also,
970 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
971 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100972 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973
Ben Lindstrombba81212001-06-25 05:01:22 +0000974static int
Damien Millerdff50992002-01-22 23:16:32 +1100975packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000977 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000978 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000979 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Damien Miller95def091999-11-25 00:26:21 +1100981 /* Check if input size is less than minimum packet size. */
982 if (buffer_len(&input) < 4 + 8)
983 return SSH_MSG_NONE;
984 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000985 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100986 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100987 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000988 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100989 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990
Damien Miller95def091999-11-25 00:26:21 +1100991 /* Check if the packet has been entirely received. */
992 if (buffer_len(&input) < 4 + padded_len)
993 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller95def091999-11-25 00:26:21 +1100995 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996
Damien Miller95def091999-11-25 00:26:21 +1100997 /* Consume packet length. */
998 buffer_consume(&input, 4);
999
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001000 /*
1001 * Cryptographic attack detector for ssh
1002 * (C)1998 CORE-SDI, Buenos Aires Argentina
1003 * Ariel Futoransky(futo@core-sdi.com)
1004 */
Damien Miller963f6b22002-02-19 15:21:23 +11001005 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +11001006 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001007 packet_disconnect("crc32 compensation attack: network attack detected");
1008
1009 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001010 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001011 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001012 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001013
Damien Miller95def091999-11-25 00:26:21 +11001014 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015
1016#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001017 fprintf(stderr, "read_poll plain: ");
1018 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020
Damien Miller95def091999-11-25 00:26:21 +11001021 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001022 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001023 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024
Damien Miller95def091999-11-25 00:26:21 +11001025 /* Skip padding. */
1026 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001027
Damien Miller95def091999-11-25 00:26:21 +11001028 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001029 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001030 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001031 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001032
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001033 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001034 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001035 if (checksum != stored_checksum)
1036 packet_disconnect("Corrupted check bytes on input.");
1037 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038
Damien Miller95def091999-11-25 00:26:21 +11001039 if (packet_compression) {
1040 buffer_clear(&compression_buffer);
1041 buffer_uncompress(&incoming_packet, &compression_buffer);
1042 buffer_clear(&incoming_packet);
1043 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001044 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001045 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001046 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001047 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1048 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001049 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050}
Damien Miller95def091999-11-25 00:26:21 +11001051
Ben Lindstrombba81212001-06-25 05:01:22 +00001052static int
Damien Millerdff50992002-01-22 23:16:32 +11001053packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001054{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001055 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001056 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001057 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001058 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001059 Enc *enc = NULL;
1060 Mac *mac = NULL;
1061 Comp *comp = NULL;
1062
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001063 if (newkeys[MODE_IN] != NULL) {
1064 enc = &newkeys[MODE_IN]->enc;
1065 mac = &newkeys[MODE_IN]->mac;
1066 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001067 }
1068 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001069 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001070
1071 if (packet_length == 0) {
1072 /*
1073 * check if input size is less than the cipher block size,
1074 * decrypt first block and extract length of incoming packet
1075 */
1076 if (buffer_len(&input) < block_size)
1077 return SSH_MSG_NONE;
1078 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001079 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001080 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001081 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001082 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001083 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001084 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001085#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001086 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001087#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001088 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001089 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001090 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001091 buffer_consume(&input, block_size);
1092 }
1093 /* we have a partial packet of block_size bytes */
1094 need = 4 + packet_length - block_size;
1095 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1096 need, maclen));
1097 if (need % block_size != 0)
1098 fatal("padding error: need %d block %d mod %d",
1099 need, block_size, need % block_size);
1100 /*
1101 * check if the entire packet has been received and
1102 * decrypt into incoming_packet
1103 */
1104 if (buffer_len(&input) < need + maclen)
1105 return SSH_MSG_NONE;
1106#ifdef PACKET_DEBUG
1107 fprintf(stderr, "read_poll enc/full: ");
1108 buffer_dump(&input);
1109#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001110 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001111 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001112 buffer_consume(&input, need);
1113 /*
1114 * compute MAC over seqnr and packet,
1115 * increment sequence number for incoming packet
1116 */
Damien Miller4af51302000-04-16 11:18:38 +10001117 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001118 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001119 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001120 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001121 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001122 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001123 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001124 buffer_consume(&input, mac->mac_len);
1125 }
Damien Miller278f9072001-12-21 15:00:19 +11001126 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001127 *seqnr_p = p_read.seqnr;
1128 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001129 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001130 if (++p_read.packets == 0)
1131 if (!(datafellows & SSH_BUG_NOREKEY))
1132 fatal("XXX too many packets with same key");
1133 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001134
1135 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001136 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001137 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001138 DBG(debug("input: padlen %d", padlen));
1139 if (padlen < 4)
1140 packet_disconnect("Corrupted padlen %d on input.", padlen);
1141
1142 /* skip packet size + padlen, discard padding */
1143 buffer_consume(&incoming_packet, 4 + 1);
1144 buffer_consume_end(&incoming_packet, padlen);
1145
1146 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1147 if (comp && comp->enabled) {
1148 buffer_clear(&compression_buffer);
1149 buffer_uncompress(&incoming_packet, &compression_buffer);
1150 buffer_clear(&incoming_packet);
1151 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1152 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001153 DBG(debug("input: len after de-compress %d",
1154 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001155 }
1156 /*
1157 * get packet type, implies consume.
1158 * return length of payload (without type field)
1159 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001160 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001161 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1162 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001163 if (type == SSH2_MSG_NEWKEYS)
1164 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001165 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1166 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001167#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001168 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001169 buffer_dump(&incoming_packet);
1170#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001171 /* reset for next packet */
1172 packet_length = 0;
1173 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001174}
1175
1176int
Damien Millerdff50992002-01-22 23:16:32 +11001177packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001178{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001179 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001180 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001181 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001182
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001183 for (;;) {
1184 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001185 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001186 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001187 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001188 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001189 case SSH2_MSG_IGNORE:
1190 break;
1191 case SSH2_MSG_DEBUG:
1192 packet_get_char();
1193 msg = packet_get_string(NULL);
1194 debug("Remote: %.900s", msg);
1195 xfree(msg);
1196 msg = packet_get_string(NULL);
1197 xfree(msg);
1198 break;
1199 case SSH2_MSG_DISCONNECT:
1200 reason = packet_get_int();
1201 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001202 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001203 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001204 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001205 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001206 break;
Damien Miller659811f2002-01-22 23:23:11 +11001207 case SSH2_MSG_UNIMPLEMENTED:
1208 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001209 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1210 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001211 break;
Damien Miller33b13562000-04-04 14:38:59 +10001212 default:
1213 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001214 }
Damien Miller33b13562000-04-04 14:38:59 +10001215 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001216 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001217 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001218 case SSH_MSG_IGNORE:
1219 break;
1220 case SSH_MSG_DEBUG:
1221 msg = packet_get_string(NULL);
1222 debug("Remote: %.900s", msg);
1223 xfree(msg);
1224 break;
1225 case SSH_MSG_DISCONNECT:
1226 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001227 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001228 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001229 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001230 xfree(msg);
1231 break;
1232 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001233 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001234 DBG(debug("received packet type %d", type));
1235 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001236 }
Damien Miller33b13562000-04-04 14:38:59 +10001237 }
1238 }
1239}
1240
Damien Miller278f9072001-12-21 15:00:19 +11001241int
Damien Millerdff50992002-01-22 23:16:32 +11001242packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001243{
Damien Millerdff50992002-01-22 23:16:32 +11001244 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001245}
1246
Damien Miller5428f641999-11-25 11:54:57 +11001247/*
1248 * Buffers the given amount of input characters. This is intended to be used
1249 * together with packet_read_poll.
1250 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251
1252void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001253packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254{
Damien Miller95def091999-11-25 00:26:21 +11001255 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256}
1257
1258/* Returns a character from the packet. */
1259
Ben Lindstrom46c16222000-12-22 01:43:59 +00001260u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001261packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262{
Damien Miller95def091999-11-25 00:26:21 +11001263 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001264
Damien Miller95def091999-11-25 00:26:21 +11001265 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001266 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267}
1268
1269/* Returns an integer from the packet data. */
1270
Ben Lindstrom46c16222000-12-22 01:43:59 +00001271u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001272packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001273{
Damien Miller95def091999-11-25 00:26:21 +11001274 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275}
1276
Damien Miller5428f641999-11-25 11:54:57 +11001277/*
1278 * Returns an arbitrary precision integer from the packet data. The integer
1279 * must have been initialized before this call.
1280 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281
1282void
Damien Millerd432ccf2002-01-22 23:14:44 +11001283packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001284{
Damien Miller76e1e362002-01-22 23:15:57 +11001285 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001286}
1287
Damien Miller33b13562000-04-04 14:38:59 +10001288void
Damien Millerd432ccf2002-01-22 23:14:44 +11001289packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001290{
Damien Miller76e1e362002-01-22 23:15:57 +11001291 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001292}
1293
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001294void *
Damien Millereccb9de2005-06-17 12:59:34 +10001295packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001296{
Damien Millereccb9de2005-06-17 12:59:34 +10001297 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001298
Damien Miller33b13562000-04-04 14:38:59 +10001299 if (length_ptr != NULL)
1300 *length_ptr = bytes;
1301 return buffer_ptr(&incoming_packet);
1302}
1303
Damien Miller4af51302000-04-16 11:18:38 +10001304int
1305packet_remaining(void)
1306{
1307 return buffer_len(&incoming_packet);
1308}
1309
Damien Miller5428f641999-11-25 11:54:57 +11001310/*
1311 * Returns a string from the packet data. The string is allocated using
1312 * xmalloc; it is the responsibility of the calling program to free it when
1313 * no longer needed. The length_ptr argument may be NULL, or point to an
1314 * integer into which the length of the string is stored.
1315 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001317void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001318packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319{
Damien Miller95def091999-11-25 00:26:21 +11001320 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001321}
1322
Damien Miller5428f641999-11-25 11:54:57 +11001323/*
1324 * Sends a diagnostic message from the server to the client. This message
1325 * can be sent at any time (but not while constructing another message). The
1326 * message is printed immediately, but only if the client is being executed
1327 * in verbose mode. These messages are primarily intended to ease debugging
1328 * authentication problems. The length of the formatted message must not
1329 * exceed 1024 bytes. This will automatically call packet_write_wait.
1330 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331
1332void
Damien Miller95def091999-11-25 00:26:21 +11001333packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001334{
Damien Miller95def091999-11-25 00:26:21 +11001335 char buf[1024];
1336 va_list args;
1337
Ben Lindstroma14ee472000-12-07 01:24:58 +00001338 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1339 return;
1340
Damien Miller95def091999-11-25 00:26:21 +11001341 va_start(args, fmt);
1342 vsnprintf(buf, sizeof(buf), fmt, args);
1343 va_end(args);
1344
Damien Miller7c8af4f2000-05-01 08:24:07 +10001345 if (compat20) {
1346 packet_start(SSH2_MSG_DEBUG);
1347 packet_put_char(0); /* bool: always display */
1348 packet_put_cstring(buf);
1349 packet_put_cstring("");
1350 } else {
1351 packet_start(SSH_MSG_DEBUG);
1352 packet_put_cstring(buf);
1353 }
Damien Miller95def091999-11-25 00:26:21 +11001354 packet_send();
1355 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001356}
1357
Damien Miller5428f641999-11-25 11:54:57 +11001358/*
1359 * Logs the error plus constructs and sends a disconnect packet, closes the
1360 * connection, and exits. This function never returns. The error message
1361 * should not contain a newline. The length of the formatted message must
1362 * not exceed 1024 bytes.
1363 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364
1365void
Damien Miller95def091999-11-25 00:26:21 +11001366packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001367{
Damien Miller95def091999-11-25 00:26:21 +11001368 char buf[1024];
1369 va_list args;
1370 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001371
Damien Miller95def091999-11-25 00:26:21 +11001372 if (disconnecting) /* Guard against recursive invocations. */
1373 fatal("packet_disconnect called recursively.");
1374 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375
Damien Miller5428f641999-11-25 11:54:57 +11001376 /*
1377 * Format the message. Note that the caller must make sure the
1378 * message is of limited size.
1379 */
Damien Miller95def091999-11-25 00:26:21 +11001380 va_start(args, fmt);
1381 vsnprintf(buf, sizeof(buf), fmt, args);
1382 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001383
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001384 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001385 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001386
Damien Miller95def091999-11-25 00:26:21 +11001387 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001388 if (compat20) {
1389 packet_start(SSH2_MSG_DISCONNECT);
1390 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1391 packet_put_cstring(buf);
1392 packet_put_cstring("");
1393 } else {
1394 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001395 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001396 }
Damien Miller95def091999-11-25 00:26:21 +11001397 packet_send();
1398 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001399
Damien Miller95def091999-11-25 00:26:21 +11001400 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001401 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402
Damien Miller95def091999-11-25 00:26:21 +11001403 /* Close the connection. */
1404 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001405 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406}
1407
Damien Miller5428f641999-11-25 11:54:57 +11001408/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001409
1410void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001411packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001412{
Damien Miller95def091999-11-25 00:26:21 +11001413 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001414
Damien Miller95def091999-11-25 00:26:21 +11001415 if (len > 0) {
1416 len = write(connection_out, buffer_ptr(&output), len);
1417 if (len <= 0) {
1418 if (errno == EAGAIN)
1419 return;
1420 else
1421 fatal("Write failed: %.100s", strerror(errno));
1422 }
1423 buffer_consume(&output, len);
1424 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425}
1426
Damien Miller5428f641999-11-25 11:54:57 +11001427/*
1428 * Calls packet_write_poll repeatedly until all pending output data has been
1429 * written.
1430 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431
1432void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001433packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001435 fd_set *setp;
1436
Damien Miller07d86be2006-03-26 14:19:21 +11001437 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001438 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001439 packet_write_poll();
1440 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001441 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1442 sizeof(fd_mask));
1443 FD_SET(connection_out, setp);
1444 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001445 (errno == EAGAIN || errno == EINTR))
1446 ;
Damien Miller95def091999-11-25 00:26:21 +11001447 packet_write_poll();
1448 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001449 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450}
1451
1452/* Returns true if there is buffered data to write to the connection. */
1453
1454int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001455packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456{
Damien Miller95def091999-11-25 00:26:21 +11001457 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001458}
1459
1460/* Returns true if there is not too much data to write to the connection. */
1461
1462int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001463packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001464{
Damien Miller95def091999-11-25 00:26:21 +11001465 if (interactive_mode)
1466 return buffer_len(&output) < 16384;
1467 else
1468 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469}
1470
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001471
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001472static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001473packet_set_tos(int interactive)
1474{
Damien Miller5924ceb2003-11-22 15:02:42 +11001475#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001476 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1477
1478 if (!packet_connection_is_on_socket() ||
1479 !packet_connection_is_ipv4())
1480 return;
1481 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1482 sizeof(tos)) < 0)
1483 error("setsockopt IP_TOS %d: %.100s:",
1484 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001485#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001486}
Ben Lindstroma7433982002-12-23 02:41:41 +00001487
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488/* Informs that the current session is interactive. Sets IP flags for that. */
1489
1490void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001491packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001493 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001495 if (called)
1496 return;
1497 called = 1;
1498
Damien Miller95def091999-11-25 00:26:21 +11001499 /* Record that we are in interactive mode. */
1500 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001501
Damien Miller34132e52000-01-14 15:45:46 +11001502 /* Only set socket options if using a socket. */
1503 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001504 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001505 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001506 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001507}
1508
1509/* Returns true if the current connection is interactive. */
1510
1511int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001512packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001513{
Damien Miller95def091999-11-25 00:26:21 +11001514 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515}
Damien Miller6162d121999-11-21 13:23:52 +11001516
Darren Tucker1f8311c2004-05-13 16:39:33 +10001517int
Darren Tucker502d3842003-06-28 12:38:01 +10001518packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001519{
Damien Miller95def091999-11-25 00:26:21 +11001520 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001521
Damien Miller95def091999-11-25 00:26:21 +11001522 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001523 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001524 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001525 return -1;
1526 }
1527 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001528 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001529 return -1;
1530 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001531 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001532 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001533 max_packet_size = s;
1534 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001535}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001536
Damien Miller9f643902001-11-12 11:02:52 +11001537/* roundup current message to pad bytes */
1538void
1539packet_add_padding(u_char pad)
1540{
1541 extra_pad = pad;
1542}
1543
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001544/*
1545 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001546 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001547 * byte SSH_MSG_IGNORE
1548 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001549 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001550 * All implementations MUST understand (and ignore) this message at any
1551 * time (after receiving the protocol version). No implementation is
1552 * required to send them. This message can be used as an additional
1553 * protection measure against advanced traffic analysis techniques.
1554 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001555void
1556packet_send_ignore(int nbytes)
1557{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001558 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001559 int i;
1560
1561 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001562 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001563 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001564 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001565 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001566 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001567 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001568 }
1569}
Damien Millera5539d22003-04-09 20:50:06 +10001570
Darren Tucker1f8311c2004-05-13 16:39:33 +10001571#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001572int
1573packet_need_rekeying(void)
1574{
1575 if (datafellows & SSH_BUG_NOREKEY)
1576 return 0;
1577 return
1578 (p_send.packets > MAX_PACKETS) ||
1579 (p_read.packets > MAX_PACKETS) ||
1580 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1581 (max_blocks_in && (p_read.blocks > max_blocks_in));
1582}
1583
1584void
1585packet_set_rekey_limit(u_int32_t bytes)
1586{
1587 rekey_limit = bytes;
1588}
Damien Miller9786e6e2005-07-26 21:54:56 +10001589
1590void
1591packet_set_server(void)
1592{
1593 server_side = 1;
1594}
1595
1596void
1597packet_set_authenticated(void)
1598{
1599 after_authentication = 1;
1600}