blob: e153a7c1b299fc5c52a953fbe25f530b9a0165b5 [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: packet.c,v 1.135 2006/07/11 20:07:25 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 Miller8ec8c3e2006-07-10 20:35:38 +100044#include <sys/socket.h>
45
46#include <netinet/in_systm.h>
47#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110048#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
Darren Tucker39972492006-07-12 22:22:46 +100050#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100051#include <stdarg.h>
52
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "xmalloc.h"
54#include "buffer.h"
55#include "packet.h"
56#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
59#include "compress.h"
60#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000061#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller33b13562000-04-04 14:38:59 +100063#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100065#include "ssh2.h"
66
Damien Miller874d77b2000-10-14 16:23:11 +110067#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100068#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000069#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "log.h"
71#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110072#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000073#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100074
75#ifdef PACKET_DEBUG
76#define DBG(x) x
77#else
78#define DBG(x)
79#endif
80
Damien Miller5428f641999-11-25 11:54:57 +110081/*
82 * This variable contains the file descriptors used for communicating with
83 * the other side. connection_in is used for reading; connection_out for
84 * writing. These can be the same descriptor, in which case it is assumed to
85 * be a socket.
86 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087static int connection_in = -1;
88static int connection_out = -1;
89
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000091static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* Encryption context for receiving data. This is only used for decryption. */
94static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110095
96/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097static CipherContext send_context;
98
99/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000100Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
102/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000103Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
105/* Buffer for the partial outgoing packet being constructed. */
106static Buffer outgoing_packet;
107
108/* Buffer for the incoming packet currently being processed. */
109static Buffer incoming_packet;
110
111/* Scratch buffer for packet compression/decompression. */
112static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000113static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
115/* Flag indicating whether packet compression/decompression is enabled. */
116static int packet_compression = 0;
117
Damien Miller6162d121999-11-21 13:23:52 +1100118/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000119u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100120
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121/* Flag indicating whether this module has been initialized. */
122static int initialized = 0;
123
124/* Set to true if the connection is interactive. */
125static int interactive_mode = 0;
126
Damien Miller9786e6e2005-07-26 21:54:56 +1000127/* Set to true if we are the server side. */
128static int server_side = 0;
129
130/* Set to true if we are authenticated. */
131static int after_authentication = 0;
132
Damien Miller33b13562000-04-04 14:38:59 +1000133/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000134Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000135static struct packet_state {
136 u_int32_t seqnr;
137 u_int32_t packets;
138 u_int64_t blocks;
139} p_read, p_send;
140
141static u_int64_t max_blocks_in, max_blocks_out;
142static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000143
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000144/* Session key for protocol v1 */
145static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
146static u_int ssh1_keylen;
147
Damien Miller9f643902001-11-12 11:02:52 +1100148/* roundup current message to extra_pad bytes */
149static u_char extra_pad = 0;
150
Damien Millera5539d22003-04-09 20:50:06 +1000151struct packet {
152 TAILQ_ENTRY(packet) next;
153 u_char type;
154 Buffer payload;
155};
156TAILQ_HEAD(, packet) outgoing;
157
Damien Miller5428f641999-11-25 11:54:57 +1100158/*
159 * Sets the descriptors used for communication. Disables encryption until
160 * packet_set_encryption_key is called.
161 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162void
163packet_set_connection(int fd_in, int fd_out)
164{
Damien Miller874d77b2000-10-14 16:23:11 +1100165 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000166
Damien Miller874d77b2000-10-14 16:23:11 +1100167 if (none == NULL)
168 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100169 connection_in = fd_in;
170 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000171 cipher_init(&send_context, none, (const u_char *)"",
172 0, NULL, 0, CIPHER_ENCRYPT);
173 cipher_init(&receive_context, none, (const u_char *)"",
174 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000175 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100176 if (!initialized) {
177 initialized = 1;
178 buffer_init(&input);
179 buffer_init(&output);
180 buffer_init(&outgoing_packet);
181 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000182 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100183 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184}
185
Damien Miller34132e52000-01-14 15:45:46 +1100186/* Returns 1 if remote host is connected via socket, 0 if not. */
187
188int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000189packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100190{
191 struct sockaddr_storage from, to;
192 socklen_t fromlen, tolen;
193
194 /* filedescriptors in and out are the same, so it's a socket */
195 if (connection_in == connection_out)
196 return 1;
197 fromlen = sizeof(from);
198 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100199 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100200 return 0;
201 tolen = sizeof(to);
202 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100203 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100204 return 0;
205 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
206 return 0;
207 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
208 return 0;
209 return 1;
210}
211
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000212/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000213 * Exports an IV from the CipherContext required to export the key
214 * state back from the unprivileged child to the privileged parent
215 * process.
216 */
217
218void
219packet_get_keyiv(int mode, u_char *iv, u_int len)
220{
221 CipherContext *cc;
222
223 if (mode == MODE_OUT)
224 cc = &send_context;
225 else
226 cc = &receive_context;
227
228 cipher_get_keyiv(cc, iv, len);
229}
230
231int
232packet_get_keycontext(int mode, u_char *dat)
233{
234 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000235
Ben Lindstromf6027d32002-03-22 01:42:04 +0000236 if (mode == MODE_OUT)
237 cc = &send_context;
238 else
239 cc = &receive_context;
240
241 return (cipher_get_keycontext(cc, dat));
242}
243
244void
245packet_set_keycontext(int mode, u_char *dat)
246{
247 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000248
Ben Lindstromf6027d32002-03-22 01:42:04 +0000249 if (mode == MODE_OUT)
250 cc = &send_context;
251 else
252 cc = &receive_context;
253
254 cipher_set_keycontext(cc, dat);
255}
256
257int
258packet_get_keyiv_len(int mode)
259{
260 CipherContext *cc;
261
262 if (mode == MODE_OUT)
263 cc = &send_context;
264 else
265 cc = &receive_context;
266
267 return (cipher_get_keyiv_len(cc));
268}
Damien Miller4f7becb2006-03-26 14:10:14 +1100269
Ben Lindstromf6027d32002-03-22 01:42:04 +0000270void
271packet_set_iv(int mode, u_char *dat)
272{
273 CipherContext *cc;
274
275 if (mode == MODE_OUT)
276 cc = &send_context;
277 else
278 cc = &receive_context;
279
280 cipher_set_keyiv(cc, dat);
281}
Damien Miller4f7becb2006-03-26 14:10:14 +1100282
Ben Lindstromf6027d32002-03-22 01:42:04 +0000283int
Damien Miller2b92d322003-06-11 22:05:06 +1000284packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000285{
286 return (cipher_get_number(receive_context.cipher));
287}
288
Damien Millera5539d22003-04-09 20:50:06 +1000289void
290packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000291{
Damien Millera5539d22003-04-09 20:50:06 +1000292 struct packet_state *state;
293
294 state = (mode == MODE_IN) ? &p_read : &p_send;
295 *seqnr = state->seqnr;
296 *blocks = state->blocks;
297 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000298}
299
300void
Damien Millera5539d22003-04-09 20:50:06 +1000301packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000302{
Damien Millera5539d22003-04-09 20:50:06 +1000303 struct packet_state *state;
304
305 state = (mode == MODE_IN) ? &p_read : &p_send;
306 state->seqnr = seqnr;
307 state->blocks = blocks;
308 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000309}
310
Damien Miller34132e52000-01-14 15:45:46 +1100311/* returns 1 if connection is via ipv4 */
312
313int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000314packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100315{
316 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100317 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100318
319 memset(&to, 0, sizeof(to));
320 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
321 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000322 if (to.ss_family == AF_INET)
323 return 1;
324#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100325 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000326 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
327 return 1;
328#endif
329 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100330}
331
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332/* Sets the connection into non-blocking mode. */
333
334void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000335packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336{
Damien Miller95def091999-11-25 00:26:21 +1100337 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000338 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
Damien Miller232711f2004-06-15 10:35:30 +1000340 if (connection_out != connection_in)
341 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342}
343
344/* Returns the socket used for reading. */
345
346int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000347packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348{
Damien Miller95def091999-11-25 00:26:21 +1100349 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350}
351
352/* Returns the descriptor used for writing. */
353
354int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000355packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356{
Damien Miller95def091999-11-25 00:26:21 +1100357 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358}
359
360/* Closes the connection and clears and frees internal data structures. */
361
362void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000363packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364{
Damien Miller95def091999-11-25 00:26:21 +1100365 if (!initialized)
366 return;
367 initialized = 0;
368 if (connection_in == connection_out) {
369 shutdown(connection_out, SHUT_RDWR);
370 close(connection_out);
371 } else {
372 close(connection_in);
373 close(connection_out);
374 }
375 buffer_free(&input);
376 buffer_free(&output);
377 buffer_free(&outgoing_packet);
378 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000379 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100380 buffer_free(&compression_buffer);
381 buffer_compress_uninit();
382 }
Damien Miller963f6b22002-02-19 15:21:23 +1100383 cipher_cleanup(&send_context);
384 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385}
386
387/* Sets remote side protocol flags. */
388
389void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000390packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391{
Damien Miller95def091999-11-25 00:26:21 +1100392 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393}
394
395/* Returns the remote protocol flags set earlier by the above function. */
396
Ben Lindstrom46c16222000-12-22 01:43:59 +0000397u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000398packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399{
Damien Miller95def091999-11-25 00:26:21 +1100400 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401}
402
Damien Miller5428f641999-11-25 11:54:57 +1100403/*
404 * Starts packet compression from the next packet on in both directions.
405 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
406 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407
Ben Lindstrombba81212001-06-25 05:01:22 +0000408static void
409packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000410{
411 if (compression_buffer_ready == 1)
412 return;
413 compression_buffer_ready = 1;
414 buffer_init(&compression_buffer);
415}
416
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417void
418packet_start_compression(int level)
419{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000420 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100421 fatal("Compression already enabled.");
422 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000423 packet_init_compression();
424 buffer_compress_init_send(level);
425 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426}
427
Damien Miller5428f641999-11-25 11:54:57 +1100428/*
Damien Miller5428f641999-11-25 11:54:57 +1100429 * Causes any further packets to be encrypted using the given key. The same
430 * key is used for both sending and reception. However, both directions are
431 * encrypted independently of each other.
432 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000433
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000435packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100436 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437{
Damien Miller874d77b2000-10-14 16:23:11 +1100438 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000439
Damien Miller874d77b2000-10-14 16:23:11 +1100440 if (cipher == NULL)
441 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000442 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100443 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000444 if (keylen > SSH_SESSION_KEY_LENGTH)
445 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
446 memcpy(ssh1_key, key, keylen);
447 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100448 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
449 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450}
451
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000452u_int
453packet_get_encryption_key(u_char *key)
454{
455 if (key == NULL)
456 return (ssh1_keylen);
457 memcpy(key, ssh1_key, ssh1_keylen);
458 return (ssh1_keylen);
459}
460
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000461/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000463packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000465 u_char buf[9];
466 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467
Ben Lindstrom204e4882001-03-05 06:47:00 +0000468 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000469 len = compat20 ? 6 : 9;
470 memset(buf, 0, len - 1);
471 buf[len - 1] = type;
472 buffer_clear(&outgoing_packet);
473 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000474}
475
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000476/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477void
478packet_put_char(int value)
479{
Damien Miller95def091999-11-25 00:26:21 +1100480 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000481
Damien Miller95def091999-11-25 00:26:21 +1100482 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483}
Damien Miller4f7becb2006-03-26 14:10:14 +1100484
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000486packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487{
Damien Miller95def091999-11-25 00:26:21 +1100488 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489}
Damien Miller4f7becb2006-03-26 14:10:14 +1100490
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100492packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493{
Damien Miller95def091999-11-25 00:26:21 +1100494 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495}
Damien Miller4f7becb2006-03-26 14:10:14 +1100496
Damien Miller33b13562000-04-04 14:38:59 +1000497void
498packet_put_cstring(const char *str)
499{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000500 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000501}
Damien Miller4f7becb2006-03-26 14:10:14 +1100502
Damien Miller33b13562000-04-04 14:38:59 +1000503void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100504packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000505{
506 buffer_append(&outgoing_packet, buf, len);
507}
Damien Miller4f7becb2006-03-26 14:10:14 +1100508
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509void
Damien Miller95def091999-11-25 00:26:21 +1100510packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511{
Damien Miller95def091999-11-25 00:26:21 +1100512 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513}
Damien Miller4f7becb2006-03-26 14:10:14 +1100514
Damien Miller33b13562000-04-04 14:38:59 +1000515void
516packet_put_bignum2(BIGNUM * value)
517{
518 buffer_put_bignum2(&outgoing_packet, value);
519}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520
Damien Miller5428f641999-11-25 11:54:57 +1100521/*
522 * Finalizes and sends the packet. If the encryption key has been set,
523 * encrypts the packet before sending.
524 */
Damien Miller95def091999-11-25 00:26:21 +1100525
Ben Lindstrombba81212001-06-25 05:01:22 +0000526static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000527packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000529 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100530 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000531 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000532 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533
Damien Miller5428f641999-11-25 11:54:57 +1100534 /*
535 * If using packet compression, compress the payload of the outgoing
536 * packet.
537 */
Damien Miller95def091999-11-25 00:26:21 +1100538 if (packet_compression) {
539 buffer_clear(&compression_buffer);
540 /* Skip padding. */
541 buffer_consume(&outgoing_packet, 8);
542 /* padding */
543 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
544 buffer_compress(&outgoing_packet, &compression_buffer);
545 buffer_clear(&outgoing_packet);
546 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100547 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100548 }
549 /* Compute packet length without padding (add checksum, remove padding). */
550 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551
Damien Millere247cc42000-05-07 12:03:14 +1000552 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100553 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100554 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100555 cp = buffer_ptr(&outgoing_packet);
556 for (i = 0; i < padding; i++) {
557 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000558 rnd = arc4random();
559 cp[7 - i] = rnd & 0xff;
560 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100561 }
562 }
563 buffer_consume(&outgoing_packet, 8 - padding);
564
565 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100566 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000567 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100568 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100569 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
571#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100572 fprintf(stderr, "packet_send plain: ");
573 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574#endif
575
Damien Miller95def091999-11-25 00:26:21 +1100576 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100577 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100578 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100579 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100580 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100581 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100582
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100584 fprintf(stderr, "encrypted: ");
585 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586#endif
587
Damien Miller95def091999-11-25 00:26:21 +1100588 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589
Damien Miller5428f641999-11-25 11:54:57 +1100590 /*
Damien Miller788f2122005-11-05 15:14:59 +1100591 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100592 * actually sent until packet_write_wait or packet_write_poll is
593 * called.
594 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595}
596
Ben Lindstromf6027d32002-03-22 01:42:04 +0000597void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000598set_newkeys(int mode)
599{
600 Enc *enc;
601 Mac *mac;
602 Comp *comp;
603 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000604 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000605 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000606
Ben Lindstrom064496f2002-12-23 02:04:22 +0000607 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000608
Damien Miller963f6b22002-02-19 15:21:23 +1100609 if (mode == MODE_OUT) {
610 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000611 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000612 p_send.packets = p_send.blocks = 0;
613 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100614 } else {
615 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000616 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000617 p_read.packets = p_read.blocks = 0;
618 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100619 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000620 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000621 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100622 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000623 enc = &newkeys[mode]->enc;
624 mac = &newkeys[mode]->mac;
625 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000626 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000627 xfree(enc->name);
628 xfree(enc->iv);
629 xfree(enc->key);
630 xfree(mac->name);
631 xfree(mac->key);
632 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000633 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000634 }
635 newkeys[mode] = kex_get_newkeys(mode);
636 if (newkeys[mode] == NULL)
637 fatal("newkeys: no keys for mode %d", mode);
638 enc = &newkeys[mode]->enc;
639 mac = &newkeys[mode]->mac;
640 comp = &newkeys[mode]->comp;
641 if (mac->md != NULL)
642 mac->enabled = 1;
643 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100644 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000645 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000646 /* Deleting the keys does not gain extra security */
647 /* memset(enc->iv, 0, enc->block_size);
648 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000649 if ((comp->type == COMP_ZLIB ||
650 (comp->type == COMP_DELAYED && after_authentication)) &&
651 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000652 packet_init_compression();
653 if (mode == MODE_OUT)
654 buffer_compress_init_send(6);
655 else
656 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000657 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000658 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000659 /*
660 * The 2^(blocksize*2) limit is too expensive for 3DES,
661 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
662 */
663 if (enc->block_size >= 16)
664 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
665 else
666 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000667 if (rekey_limit)
668 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000669}
670
Damien Miller5428f641999-11-25 11:54:57 +1100671/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000672 * Delayed compression for SSH2 is enabled after authentication:
673 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
674 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
675 */
676static void
677packet_enable_delayed_compress(void)
678{
679 Comp *comp = NULL;
680 int mode;
681
682 /*
683 * Remember that we are past the authentication step, so rekeying
684 * with COMP_DELAYED will turn on compression immediately.
685 */
686 after_authentication = 1;
687 for (mode = 0; mode < MODE_MAX; mode++) {
688 comp = &newkeys[mode]->comp;
689 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000690 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000691 if (mode == MODE_OUT)
692 buffer_compress_init_send(6);
693 else
694 buffer_compress_init_recv();
695 comp->enabled = 1;
696 }
697 }
698}
699
700/*
Damien Miller33b13562000-04-04 14:38:59 +1000701 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
702 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000703static void
Damien Millera5539d22003-04-09 20:50:06 +1000704packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000705{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000706 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100707 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000708 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100709 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000710 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000711 Enc *enc = NULL;
712 Mac *mac = NULL;
713 Comp *comp = NULL;
714 int block_size;
715
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000716 if (newkeys[MODE_OUT] != NULL) {
717 enc = &newkeys[MODE_OUT]->enc;
718 mac = &newkeys[MODE_OUT]->mac;
719 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000720 }
Damien Miller963f6b22002-02-19 15:21:23 +1100721 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000722
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000723 cp = buffer_ptr(&outgoing_packet);
724 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000725
726#ifdef PACKET_DEBUG
727 fprintf(stderr, "plain: ");
728 buffer_dump(&outgoing_packet);
729#endif
730
731 if (comp && comp->enabled) {
732 len = buffer_len(&outgoing_packet);
733 /* skip header, compress only payload */
734 buffer_consume(&outgoing_packet, 5);
735 buffer_clear(&compression_buffer);
736 buffer_compress(&outgoing_packet, &compression_buffer);
737 buffer_clear(&outgoing_packet);
738 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
739 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
740 buffer_len(&compression_buffer));
741 DBG(debug("compression: raw %d compressed %d", len,
742 buffer_len(&outgoing_packet)));
743 }
744
745 /* sizeof (packet_len + pad_len + payload) */
746 len = buffer_len(&outgoing_packet);
747
748 /*
749 * calc size of padding, alloc space, get random data,
750 * minimum padding is 4 bytes
751 */
752 padlen = block_size - (len % block_size);
753 if (padlen < 4)
754 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100755 if (extra_pad) {
756 /* will wrap if extra_pad+padlen > 255 */
757 extra_pad = roundup(extra_pad, block_size);
758 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000759 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100760 pad, len, padlen, extra_pad);
761 padlen += pad;
762 extra_pad = 0;
763 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100764 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100765 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000766 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000767 for (i = 0; i < padlen; i++) {
768 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000769 rnd = arc4random();
770 cp[i] = rnd & 0xff;
771 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000772 }
Damien Millere247cc42000-05-07 12:03:14 +1000773 } else {
774 /* clear padding */
775 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000776 }
777 /* packet_length includes payload, padding and padding length field */
778 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000779 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100780 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000781 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000782 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
783
784 /* compute MAC over seqnr and packet(length fields, payload, padding) */
785 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000786 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100787 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000788 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000789 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000790 }
791 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100792 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100793 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000794 buffer_len(&outgoing_packet));
795 /* append unencrypted MAC */
796 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100797 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000798#ifdef PACKET_DEBUG
799 fprintf(stderr, "encrypted: ");
800 buffer_dump(&output);
801#endif
Damien Miller4af51302000-04-16 11:18:38 +1000802 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000803 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000804 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000805 if (++p_send.packets == 0)
806 if (!(datafellows & SSH_BUG_NOREKEY))
807 fatal("XXX too many packets with same key");
808 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000809 buffer_clear(&outgoing_packet);
810
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000811 if (type == SSH2_MSG_NEWKEYS)
812 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000813 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
814 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000815}
816
Damien Millera5539d22003-04-09 20:50:06 +1000817static void
818packet_send2(void)
819{
820 static int rekeying = 0;
821 struct packet *p;
822 u_char type, *cp;
823
824 cp = buffer_ptr(&outgoing_packet);
825 type = cp[5];
826
827 /* during rekeying we can only send key exchange messages */
828 if (rekeying) {
829 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
830 (type <= SSH2_MSG_TRANSPORT_MAX))) {
831 debug("enqueue packet: %u", type);
832 p = xmalloc(sizeof(*p));
833 p->type = type;
834 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
835 buffer_init(&outgoing_packet);
836 TAILQ_INSERT_TAIL(&outgoing, p, next);
837 return;
838 }
839 }
840
841 /* rekeying starts with sending KEXINIT */
842 if (type == SSH2_MSG_KEXINIT)
843 rekeying = 1;
844
845 packet_send2_wrapped();
846
847 /* after a NEWKEYS message we can send the complete queue */
848 if (type == SSH2_MSG_NEWKEYS) {
849 rekeying = 0;
850 while ((p = TAILQ_FIRST(&outgoing))) {
851 type = p->type;
852 debug("dequeue packet: %u", type);
853 buffer_free(&outgoing_packet);
854 memcpy(&outgoing_packet, &p->payload,
855 sizeof(Buffer));
856 TAILQ_REMOVE(&outgoing, p, next);
857 xfree(p);
858 packet_send2_wrapped();
859 }
860 }
861}
862
Damien Miller33b13562000-04-04 14:38:59 +1000863void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000864packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000865{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000866 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000867 packet_send2();
868 else
869 packet_send1();
870 DBG(debug("packet_send done"));
871}
872
Damien Miller33b13562000-04-04 14:38:59 +1000873/*
Damien Miller5428f641999-11-25 11:54:57 +1100874 * Waits until a packet has been received, and returns its type. Note that
875 * no other data is processed until this returns, so this function should not
876 * be used during the interactive session.
877 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878
879int
Damien Millerdff50992002-01-22 23:16:32 +1100880packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881{
Damien Miller95def091999-11-25 00:26:21 +1100882 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000883 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100884 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000885 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886
Damien Miller07d86be2006-03-26 14:19:21 +1100887 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000888 sizeof(fd_mask));
889
Damien Miller95def091999-11-25 00:26:21 +1100890 /* Since we are blocking, ensure that all written packets have been sent. */
891 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller95def091999-11-25 00:26:21 +1100893 /* Stay in the loop until we have received a complete packet. */
894 for (;;) {
895 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100896 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000897 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000898 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100899 || type == SSH_SMSG_FAILURE
900 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000901 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100902 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100903 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000904 if (type != SSH_MSG_NONE) {
905 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100906 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000907 }
Damien Miller5428f641999-11-25 11:54:57 +1100908 /*
909 * Otherwise, wait for some data to arrive, add it to the
910 * buffer, and try again.
911 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000912 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
913 sizeof(fd_mask));
914 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100915
Damien Miller95def091999-11-25 00:26:21 +1100916 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000917 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000918 (errno == EAGAIN || errno == EINTR))
919 ;
Damien Miller5428f641999-11-25 11:54:57 +1100920
Damien Miller95def091999-11-25 00:26:21 +1100921 /* Read data from the socket. */
922 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100923 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000924 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000925 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100926 }
Damien Miller95def091999-11-25 00:26:21 +1100927 if (len < 0)
928 fatal("Read from socket failed: %.100s", strerror(errno));
929 /* Append it to the buffer. */
930 packet_process_incoming(buf, len);
931 }
932 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933}
934
Damien Miller278f9072001-12-21 15:00:19 +1100935int
Damien Millerdff50992002-01-22 23:16:32 +1100936packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100937{
Damien Millerdff50992002-01-22 23:16:32 +1100938 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100939}
940
Damien Miller5428f641999-11-25 11:54:57 +1100941/*
942 * Waits until a packet has been received, verifies that its type matches
943 * that given, and gives a fatal error and exits if there is a mismatch.
944 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945
946void
Damien Millerdff50992002-01-22 23:16:32 +1100947packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948{
Damien Miller95def091999-11-25 00:26:21 +1100949 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950
Damien Millerdff50992002-01-22 23:16:32 +1100951 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100952 if (type != expected_type)
953 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000954 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955}
956
957/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100958 * packet_process_incoming. If so, reads the packet; otherwise returns
959 * SSH_MSG_NONE. This does not wait for data from the connection.
960 *
961 * SSH_MSG_DISCONNECT is handled specially here. Also,
962 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
963 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100964 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965
Ben Lindstrombba81212001-06-25 05:01:22 +0000966static int
Damien Millerdff50992002-01-22 23:16:32 +1100967packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000969 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000970 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000971 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Damien Miller95def091999-11-25 00:26:21 +1100973 /* Check if input size is less than minimum packet size. */
974 if (buffer_len(&input) < 4 + 8)
975 return SSH_MSG_NONE;
976 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000977 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100978 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100979 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000980 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100981 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller95def091999-11-25 00:26:21 +1100983 /* Check if the packet has been entirely received. */
984 if (buffer_len(&input) < 4 + padded_len)
985 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Damien Miller95def091999-11-25 00:26:21 +1100987 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988
Damien Miller95def091999-11-25 00:26:21 +1100989 /* Consume packet length. */
990 buffer_consume(&input, 4);
991
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000992 /*
993 * Cryptographic attack detector for ssh
994 * (C)1998 CORE-SDI, Buenos Aires Argentina
995 * Ariel Futoransky(futo@core-sdi.com)
996 */
Damien Miller963f6b22002-02-19 15:21:23 +1100997 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +1100998 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000999 packet_disconnect("crc32 compensation attack: network attack detected");
1000
1001 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001002 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001003 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001004 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001005
Damien Miller95def091999-11-25 00:26:21 +11001006 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007
1008#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001009 fprintf(stderr, "read_poll plain: ");
1010 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
Damien Miller95def091999-11-25 00:26:21 +11001013 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001014 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001015 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016
Damien Miller95def091999-11-25 00:26:21 +11001017 /* Skip padding. */
1018 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001019
Damien Miller95def091999-11-25 00:26:21 +11001020 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001021 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001022 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001023 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001025 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001026 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001027 if (checksum != stored_checksum)
1028 packet_disconnect("Corrupted check bytes on input.");
1029 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030
Damien Miller95def091999-11-25 00:26:21 +11001031 if (packet_compression) {
1032 buffer_clear(&compression_buffer);
1033 buffer_uncompress(&incoming_packet, &compression_buffer);
1034 buffer_clear(&incoming_packet);
1035 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001036 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001037 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001038 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001039 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1040 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001041 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001042}
Damien Miller95def091999-11-25 00:26:21 +11001043
Ben Lindstrombba81212001-06-25 05:01:22 +00001044static int
Damien Millerdff50992002-01-22 23:16:32 +11001045packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001046{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001047 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001048 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001049 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001050 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001051 Enc *enc = NULL;
1052 Mac *mac = NULL;
1053 Comp *comp = NULL;
1054
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001055 if (newkeys[MODE_IN] != NULL) {
1056 enc = &newkeys[MODE_IN]->enc;
1057 mac = &newkeys[MODE_IN]->mac;
1058 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001059 }
1060 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001061 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001062
1063 if (packet_length == 0) {
1064 /*
1065 * check if input size is less than the cipher block size,
1066 * decrypt first block and extract length of incoming packet
1067 */
1068 if (buffer_len(&input) < block_size)
1069 return SSH_MSG_NONE;
1070 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001071 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001072 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001073 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001074 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001075 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001076 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001077#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001078 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001079#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001080 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001081 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001082 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001083 buffer_consume(&input, block_size);
1084 }
1085 /* we have a partial packet of block_size bytes */
1086 need = 4 + packet_length - block_size;
1087 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1088 need, maclen));
1089 if (need % block_size != 0)
1090 fatal("padding error: need %d block %d mod %d",
1091 need, block_size, need % block_size);
1092 /*
1093 * check if the entire packet has been received and
1094 * decrypt into incoming_packet
1095 */
1096 if (buffer_len(&input) < need + maclen)
1097 return SSH_MSG_NONE;
1098#ifdef PACKET_DEBUG
1099 fprintf(stderr, "read_poll enc/full: ");
1100 buffer_dump(&input);
1101#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001102 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001103 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001104 buffer_consume(&input, need);
1105 /*
1106 * compute MAC over seqnr and packet,
1107 * increment sequence number for incoming packet
1108 */
Damien Miller4af51302000-04-16 11:18:38 +10001109 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001110 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001111 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001112 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001113 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001114 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001115 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001116 buffer_consume(&input, mac->mac_len);
1117 }
Damien Miller278f9072001-12-21 15:00:19 +11001118 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001119 *seqnr_p = p_read.seqnr;
1120 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001121 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001122 if (++p_read.packets == 0)
1123 if (!(datafellows & SSH_BUG_NOREKEY))
1124 fatal("XXX too many packets with same key");
1125 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001126
1127 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001128 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001129 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001130 DBG(debug("input: padlen %d", padlen));
1131 if (padlen < 4)
1132 packet_disconnect("Corrupted padlen %d on input.", padlen);
1133
1134 /* skip packet size + padlen, discard padding */
1135 buffer_consume(&incoming_packet, 4 + 1);
1136 buffer_consume_end(&incoming_packet, padlen);
1137
1138 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1139 if (comp && comp->enabled) {
1140 buffer_clear(&compression_buffer);
1141 buffer_uncompress(&incoming_packet, &compression_buffer);
1142 buffer_clear(&incoming_packet);
1143 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1144 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001145 DBG(debug("input: len after de-compress %d",
1146 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001147 }
1148 /*
1149 * get packet type, implies consume.
1150 * return length of payload (without type field)
1151 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001152 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001153 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1154 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001155 if (type == SSH2_MSG_NEWKEYS)
1156 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001157 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1158 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001159#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001160 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001161 buffer_dump(&incoming_packet);
1162#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001163 /* reset for next packet */
1164 packet_length = 0;
1165 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001166}
1167
1168int
Damien Millerdff50992002-01-22 23:16:32 +11001169packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001170{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001171 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001172 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001173 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001174
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001175 for (;;) {
1176 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001177 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001178 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001179 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001180 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001181 case SSH2_MSG_IGNORE:
1182 break;
1183 case SSH2_MSG_DEBUG:
1184 packet_get_char();
1185 msg = packet_get_string(NULL);
1186 debug("Remote: %.900s", msg);
1187 xfree(msg);
1188 msg = packet_get_string(NULL);
1189 xfree(msg);
1190 break;
1191 case SSH2_MSG_DISCONNECT:
1192 reason = packet_get_int();
1193 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001194 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001195 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001196 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001197 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001198 break;
Damien Miller659811f2002-01-22 23:23:11 +11001199 case SSH2_MSG_UNIMPLEMENTED:
1200 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001201 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1202 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001203 break;
Damien Miller33b13562000-04-04 14:38:59 +10001204 default:
1205 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001206 }
Damien Miller33b13562000-04-04 14:38:59 +10001207 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001208 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001209 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001210 case SSH_MSG_IGNORE:
1211 break;
1212 case SSH_MSG_DEBUG:
1213 msg = packet_get_string(NULL);
1214 debug("Remote: %.900s", msg);
1215 xfree(msg);
1216 break;
1217 case SSH_MSG_DISCONNECT:
1218 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001219 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001220 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001221 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001222 xfree(msg);
1223 break;
1224 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001225 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001226 DBG(debug("received packet type %d", type));
1227 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001228 }
Damien Miller33b13562000-04-04 14:38:59 +10001229 }
1230 }
1231}
1232
Damien Miller278f9072001-12-21 15:00:19 +11001233int
Damien Millerdff50992002-01-22 23:16:32 +11001234packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001235{
Damien Millerdff50992002-01-22 23:16:32 +11001236 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001237}
1238
Damien Miller5428f641999-11-25 11:54:57 +11001239/*
1240 * Buffers the given amount of input characters. This is intended to be used
1241 * together with packet_read_poll.
1242 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243
1244void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001245packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246{
Damien Miller95def091999-11-25 00:26:21 +11001247 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248}
1249
1250/* Returns a character from the packet. */
1251
Ben Lindstrom46c16222000-12-22 01:43:59 +00001252u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001253packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254{
Damien Miller95def091999-11-25 00:26:21 +11001255 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001256
Damien Miller95def091999-11-25 00:26:21 +11001257 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001258 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259}
1260
1261/* Returns an integer from the packet data. */
1262
Ben Lindstrom46c16222000-12-22 01:43:59 +00001263u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001264packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265{
Damien Miller95def091999-11-25 00:26:21 +11001266 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267}
1268
Damien Miller5428f641999-11-25 11:54:57 +11001269/*
1270 * Returns an arbitrary precision integer from the packet data. The integer
1271 * must have been initialized before this call.
1272 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001273
1274void
Damien Millerd432ccf2002-01-22 23:14:44 +11001275packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001276{
Damien Miller76e1e362002-01-22 23:15:57 +11001277 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001278}
1279
Damien Miller33b13562000-04-04 14:38:59 +10001280void
Damien Millerd432ccf2002-01-22 23:14:44 +11001281packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001282{
Damien Miller76e1e362002-01-22 23:15:57 +11001283 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001284}
1285
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001286void *
Damien Millereccb9de2005-06-17 12:59:34 +10001287packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001288{
Damien Millereccb9de2005-06-17 12:59:34 +10001289 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001290
Damien Miller33b13562000-04-04 14:38:59 +10001291 if (length_ptr != NULL)
1292 *length_ptr = bytes;
1293 return buffer_ptr(&incoming_packet);
1294}
1295
Damien Miller4af51302000-04-16 11:18:38 +10001296int
1297packet_remaining(void)
1298{
1299 return buffer_len(&incoming_packet);
1300}
1301
Damien Miller5428f641999-11-25 11:54:57 +11001302/*
1303 * Returns a string from the packet data. The string is allocated using
1304 * xmalloc; it is the responsibility of the calling program to free it when
1305 * no longer needed. The length_ptr argument may be NULL, or point to an
1306 * integer into which the length of the string is stored.
1307 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001308
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001309void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001310packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311{
Damien Miller95def091999-11-25 00:26:21 +11001312 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001313}
1314
Damien Miller5428f641999-11-25 11:54:57 +11001315/*
1316 * Sends a diagnostic message from the server to the client. This message
1317 * can be sent at any time (but not while constructing another message). The
1318 * message is printed immediately, but only if the client is being executed
1319 * in verbose mode. These messages are primarily intended to ease debugging
1320 * authentication problems. The length of the formatted message must not
1321 * exceed 1024 bytes. This will automatically call packet_write_wait.
1322 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001323
1324void
Damien Miller95def091999-11-25 00:26:21 +11001325packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001326{
Damien Miller95def091999-11-25 00:26:21 +11001327 char buf[1024];
1328 va_list args;
1329
Ben Lindstroma14ee472000-12-07 01:24:58 +00001330 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1331 return;
1332
Damien Miller95def091999-11-25 00:26:21 +11001333 va_start(args, fmt);
1334 vsnprintf(buf, sizeof(buf), fmt, args);
1335 va_end(args);
1336
Damien Miller7c8af4f2000-05-01 08:24:07 +10001337 if (compat20) {
1338 packet_start(SSH2_MSG_DEBUG);
1339 packet_put_char(0); /* bool: always display */
1340 packet_put_cstring(buf);
1341 packet_put_cstring("");
1342 } else {
1343 packet_start(SSH_MSG_DEBUG);
1344 packet_put_cstring(buf);
1345 }
Damien Miller95def091999-11-25 00:26:21 +11001346 packet_send();
1347 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348}
1349
Damien Miller5428f641999-11-25 11:54:57 +11001350/*
1351 * Logs the error plus constructs and sends a disconnect packet, closes the
1352 * connection, and exits. This function never returns. The error message
1353 * should not contain a newline. The length of the formatted message must
1354 * not exceed 1024 bytes.
1355 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001356
1357void
Damien Miller95def091999-11-25 00:26:21 +11001358packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359{
Damien Miller95def091999-11-25 00:26:21 +11001360 char buf[1024];
1361 va_list args;
1362 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001363
Damien Miller95def091999-11-25 00:26:21 +11001364 if (disconnecting) /* Guard against recursive invocations. */
1365 fatal("packet_disconnect called recursively.");
1366 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001367
Damien Miller5428f641999-11-25 11:54:57 +11001368 /*
1369 * Format the message. Note that the caller must make sure the
1370 * message is of limited size.
1371 */
Damien Miller95def091999-11-25 00:26:21 +11001372 va_start(args, fmt);
1373 vsnprintf(buf, sizeof(buf), fmt, args);
1374 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001376 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001377 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001378
Damien Miller95def091999-11-25 00:26:21 +11001379 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001380 if (compat20) {
1381 packet_start(SSH2_MSG_DISCONNECT);
1382 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1383 packet_put_cstring(buf);
1384 packet_put_cstring("");
1385 } else {
1386 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001387 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001388 }
Damien Miller95def091999-11-25 00:26:21 +11001389 packet_send();
1390 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391
Damien Miller95def091999-11-25 00:26:21 +11001392 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001393 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394
Damien Miller95def091999-11-25 00:26:21 +11001395 /* Close the connection. */
1396 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001397 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001398}
1399
Damien Miller5428f641999-11-25 11:54:57 +11001400/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401
1402void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001403packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404{
Damien Miller95def091999-11-25 00:26:21 +11001405 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001406
Damien Miller95def091999-11-25 00:26:21 +11001407 if (len > 0) {
1408 len = write(connection_out, buffer_ptr(&output), len);
1409 if (len <= 0) {
1410 if (errno == EAGAIN)
1411 return;
1412 else
1413 fatal("Write failed: %.100s", strerror(errno));
1414 }
1415 buffer_consume(&output, len);
1416 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417}
1418
Damien Miller5428f641999-11-25 11:54:57 +11001419/*
1420 * Calls packet_write_poll repeatedly until all pending output data has been
1421 * written.
1422 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423
1424void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001425packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001426{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001427 fd_set *setp;
1428
Damien Miller07d86be2006-03-26 14:19:21 +11001429 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001430 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001431 packet_write_poll();
1432 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001433 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1434 sizeof(fd_mask));
1435 FD_SET(connection_out, setp);
1436 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001437 (errno == EAGAIN || errno == EINTR))
1438 ;
Damien Miller95def091999-11-25 00:26:21 +11001439 packet_write_poll();
1440 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001441 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001442}
1443
1444/* Returns true if there is buffered data to write to the connection. */
1445
1446int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001447packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001448{
Damien Miller95def091999-11-25 00:26:21 +11001449 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450}
1451
1452/* Returns true if there is not too much data to write to the connection. */
1453
1454int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001455packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456{
Damien Miller95def091999-11-25 00:26:21 +11001457 if (interactive_mode)
1458 return buffer_len(&output) < 16384;
1459 else
1460 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461}
1462
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001463
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001464static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001465packet_set_tos(int interactive)
1466{
Damien Miller5924ceb2003-11-22 15:02:42 +11001467#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001468 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1469
1470 if (!packet_connection_is_on_socket() ||
1471 !packet_connection_is_ipv4())
1472 return;
1473 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1474 sizeof(tos)) < 0)
1475 error("setsockopt IP_TOS %d: %.100s:",
1476 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001477#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001478}
Ben Lindstroma7433982002-12-23 02:41:41 +00001479
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480/* Informs that the current session is interactive. Sets IP flags for that. */
1481
1482void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001483packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001484{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001485 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001487 if (called)
1488 return;
1489 called = 1;
1490
Damien Miller95def091999-11-25 00:26:21 +11001491 /* Record that we are in interactive mode. */
1492 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493
Damien Miller34132e52000-01-14 15:45:46 +11001494 /* Only set socket options if using a socket. */
1495 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001496 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001497 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001498 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499}
1500
1501/* Returns true if the current connection is interactive. */
1502
1503int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001504packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001505{
Damien Miller95def091999-11-25 00:26:21 +11001506 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001507}
Damien Miller6162d121999-11-21 13:23:52 +11001508
Darren Tucker1f8311c2004-05-13 16:39:33 +10001509int
Darren Tucker502d3842003-06-28 12:38:01 +10001510packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001511{
Damien Miller95def091999-11-25 00:26:21 +11001512 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001513
Damien Miller95def091999-11-25 00:26:21 +11001514 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001515 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001516 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001517 return -1;
1518 }
1519 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001520 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001521 return -1;
1522 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001523 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001524 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001525 max_packet_size = s;
1526 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001527}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001528
Damien Miller9f643902001-11-12 11:02:52 +11001529/* roundup current message to pad bytes */
1530void
1531packet_add_padding(u_char pad)
1532{
1533 extra_pad = pad;
1534}
1535
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001536/*
1537 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001538 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001539 * byte SSH_MSG_IGNORE
1540 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001541 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001542 * All implementations MUST understand (and ignore) this message at any
1543 * time (after receiving the protocol version). No implementation is
1544 * required to send them. This message can be used as an additional
1545 * protection measure against advanced traffic analysis techniques.
1546 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001547void
1548packet_send_ignore(int nbytes)
1549{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001550 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001551 int i;
1552
1553 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001554 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001555 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001556 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001557 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001558 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001559 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001560 }
1561}
Damien Millera5539d22003-04-09 20:50:06 +10001562
Darren Tucker1f8311c2004-05-13 16:39:33 +10001563#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001564int
1565packet_need_rekeying(void)
1566{
1567 if (datafellows & SSH_BUG_NOREKEY)
1568 return 0;
1569 return
1570 (p_send.packets > MAX_PACKETS) ||
1571 (p_read.packets > MAX_PACKETS) ||
1572 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1573 (max_blocks_in && (p_read.blocks > max_blocks_in));
1574}
1575
1576void
1577packet_set_rekey_limit(u_int32_t bytes)
1578{
1579 rekey_limit = bytes;
1580}
Damien Miller9786e6e2005-07-26 21:54:56 +10001581
1582void
1583packet_set_server(void)
1584{
1585 server_side = 1;
1586}
1587
1588void
1589packet_set_authenticated(void)
1590{
1591 after_authentication = 1;
1592}