blob: 2c980d25e923fe51fcfb62ddbed247f9d91deed9 [file] [log] [blame]
Damien Millere7a1e5c2006-08-05 11:34:19 +10001/* $OpenBSD: packet.c,v 1.140 2006/07/26 13:57:17 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 Millere7a1e5c2006-08-05 11:34:19 +100056#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100057#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100058#include <unistd.h>
Darren Tucker5d196262006-07-12 22:15:16 +100059
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060#include "xmalloc.h"
61#include "buffer.h"
62#include "packet.h"
63#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
66#include "compress.h"
67#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000068#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Miller33b13562000-04-04 14:38:59 +100070#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100072#include "ssh2.h"
73
Damien Miller874d77b2000-10-14 16:23:11 +110074#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100075#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000076#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110079#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000080#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100081
82#ifdef PACKET_DEBUG
83#define DBG(x) x
84#else
85#define DBG(x)
86#endif
87
Damien Miller5428f641999-11-25 11:54:57 +110088/*
89 * This variable contains the file descriptors used for communicating with
90 * the other side. connection_in is used for reading; connection_out for
91 * writing. These can be the same descriptor, in which case it is assumed to
92 * be a socket.
93 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094static int connection_in = -1;
95static int connection_out = -1;
96
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000098static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
100/* Encryption context for receiving data. This is only used for decryption. */
101static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +1100102
103/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104static CipherContext send_context;
105
106/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000107Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
109/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000110Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
112/* Buffer for the partial outgoing packet being constructed. */
113static Buffer outgoing_packet;
114
115/* Buffer for the incoming packet currently being processed. */
116static Buffer incoming_packet;
117
118/* Scratch buffer for packet compression/decompression. */
119static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000120static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
122/* Flag indicating whether packet compression/decompression is enabled. */
123static int packet_compression = 0;
124
Damien Miller6162d121999-11-21 13:23:52 +1100125/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000126u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100127
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128/* Flag indicating whether this module has been initialized. */
129static int initialized = 0;
130
131/* Set to true if the connection is interactive. */
132static int interactive_mode = 0;
133
Damien Miller9786e6e2005-07-26 21:54:56 +1000134/* Set to true if we are the server side. */
135static int server_side = 0;
136
137/* Set to true if we are authenticated. */
138static int after_authentication = 0;
139
Damien Miller33b13562000-04-04 14:38:59 +1000140/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000141Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000142static struct packet_state {
143 u_int32_t seqnr;
144 u_int32_t packets;
145 u_int64_t blocks;
146} p_read, p_send;
147
148static u_int64_t max_blocks_in, max_blocks_out;
149static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000150
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000151/* Session key for protocol v1 */
152static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
153static u_int ssh1_keylen;
154
Damien Miller9f643902001-11-12 11:02:52 +1100155/* roundup current message to extra_pad bytes */
156static u_char extra_pad = 0;
157
Damien Millera5539d22003-04-09 20:50:06 +1000158struct packet {
159 TAILQ_ENTRY(packet) next;
160 u_char type;
161 Buffer payload;
162};
163TAILQ_HEAD(, packet) outgoing;
164
Damien Miller5428f641999-11-25 11:54:57 +1100165/*
166 * Sets the descriptors used for communication. Disables encryption until
167 * packet_set_encryption_key is called.
168 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169void
170packet_set_connection(int fd_in, int fd_out)
171{
Damien Miller874d77b2000-10-14 16:23:11 +1100172 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000173
Damien Miller874d77b2000-10-14 16:23:11 +1100174 if (none == NULL)
175 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100176 connection_in = fd_in;
177 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000178 cipher_init(&send_context, none, (const u_char *)"",
179 0, NULL, 0, CIPHER_ENCRYPT);
180 cipher_init(&receive_context, none, (const u_char *)"",
181 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000182 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100183 if (!initialized) {
184 initialized = 1;
185 buffer_init(&input);
186 buffer_init(&output);
187 buffer_init(&outgoing_packet);
188 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000189 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100190 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191}
192
Damien Miller34132e52000-01-14 15:45:46 +1100193/* Returns 1 if remote host is connected via socket, 0 if not. */
194
195int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000196packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100197{
198 struct sockaddr_storage from, to;
199 socklen_t fromlen, tolen;
200
201 /* filedescriptors in and out are the same, so it's a socket */
202 if (connection_in == connection_out)
203 return 1;
204 fromlen = sizeof(from);
205 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100206 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100207 return 0;
208 tolen = sizeof(to);
209 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100210 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100211 return 0;
212 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
213 return 0;
214 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
215 return 0;
216 return 1;
217}
218
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000219/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000220 * Exports an IV from the CipherContext required to export the key
221 * state back from the unprivileged child to the privileged parent
222 * process.
223 */
224
225void
226packet_get_keyiv(int mode, u_char *iv, u_int len)
227{
228 CipherContext *cc;
229
230 if (mode == MODE_OUT)
231 cc = &send_context;
232 else
233 cc = &receive_context;
234
235 cipher_get_keyiv(cc, iv, len);
236}
237
238int
239packet_get_keycontext(int mode, u_char *dat)
240{
241 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000242
Ben Lindstromf6027d32002-03-22 01:42:04 +0000243 if (mode == MODE_OUT)
244 cc = &send_context;
245 else
246 cc = &receive_context;
247
248 return (cipher_get_keycontext(cc, dat));
249}
250
251void
252packet_set_keycontext(int mode, u_char *dat)
253{
254 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000255
Ben Lindstromf6027d32002-03-22 01:42:04 +0000256 if (mode == MODE_OUT)
257 cc = &send_context;
258 else
259 cc = &receive_context;
260
261 cipher_set_keycontext(cc, dat);
262}
263
264int
265packet_get_keyiv_len(int mode)
266{
267 CipherContext *cc;
268
269 if (mode == MODE_OUT)
270 cc = &send_context;
271 else
272 cc = &receive_context;
273
274 return (cipher_get_keyiv_len(cc));
275}
Damien Miller4f7becb2006-03-26 14:10:14 +1100276
Ben Lindstromf6027d32002-03-22 01:42:04 +0000277void
278packet_set_iv(int mode, u_char *dat)
279{
280 CipherContext *cc;
281
282 if (mode == MODE_OUT)
283 cc = &send_context;
284 else
285 cc = &receive_context;
286
287 cipher_set_keyiv(cc, dat);
288}
Damien Miller4f7becb2006-03-26 14:10:14 +1100289
Ben Lindstromf6027d32002-03-22 01:42:04 +0000290int
Damien Miller2b92d322003-06-11 22:05:06 +1000291packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000292{
293 return (cipher_get_number(receive_context.cipher));
294}
295
Damien Millera5539d22003-04-09 20:50:06 +1000296void
297packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000298{
Damien Millera5539d22003-04-09 20:50:06 +1000299 struct packet_state *state;
300
301 state = (mode == MODE_IN) ? &p_read : &p_send;
302 *seqnr = state->seqnr;
303 *blocks = state->blocks;
304 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000305}
306
307void
Damien Millera5539d22003-04-09 20:50:06 +1000308packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000309{
Damien Millera5539d22003-04-09 20:50:06 +1000310 struct packet_state *state;
311
312 state = (mode == MODE_IN) ? &p_read : &p_send;
313 state->seqnr = seqnr;
314 state->blocks = blocks;
315 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000316}
317
Damien Miller34132e52000-01-14 15:45:46 +1100318/* returns 1 if connection is via ipv4 */
319
320int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000321packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100322{
323 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100324 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100325
326 memset(&to, 0, sizeof(to));
327 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
328 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000329 if (to.ss_family == AF_INET)
330 return 1;
331#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100332 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000333 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
334 return 1;
335#endif
336 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100337}
338
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339/* Sets the connection into non-blocking mode. */
340
341void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000342packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343{
Damien Miller95def091999-11-25 00:26:21 +1100344 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000345 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
Damien Miller232711f2004-06-15 10:35:30 +1000347 if (connection_out != connection_in)
348 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349}
350
351/* Returns the socket used for reading. */
352
353int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000354packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355{
Damien Miller95def091999-11-25 00:26:21 +1100356 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357}
358
359/* Returns the descriptor used for writing. */
360
361int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000362packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363{
Damien Miller95def091999-11-25 00:26:21 +1100364 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365}
366
367/* Closes the connection and clears and frees internal data structures. */
368
369void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000370packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371{
Damien Miller95def091999-11-25 00:26:21 +1100372 if (!initialized)
373 return;
374 initialized = 0;
375 if (connection_in == connection_out) {
376 shutdown(connection_out, SHUT_RDWR);
377 close(connection_out);
378 } else {
379 close(connection_in);
380 close(connection_out);
381 }
382 buffer_free(&input);
383 buffer_free(&output);
384 buffer_free(&outgoing_packet);
385 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000386 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100387 buffer_free(&compression_buffer);
388 buffer_compress_uninit();
389 }
Damien Miller963f6b22002-02-19 15:21:23 +1100390 cipher_cleanup(&send_context);
391 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392}
393
394/* Sets remote side protocol flags. */
395
396void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000397packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398{
Damien Miller95def091999-11-25 00:26:21 +1100399 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400}
401
402/* Returns the remote protocol flags set earlier by the above function. */
403
Ben Lindstrom46c16222000-12-22 01:43:59 +0000404u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000405packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406{
Damien Miller95def091999-11-25 00:26:21 +1100407 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408}
409
Damien Miller5428f641999-11-25 11:54:57 +1100410/*
411 * Starts packet compression from the next packet on in both directions.
412 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
413 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Ben Lindstrombba81212001-06-25 05:01:22 +0000415static void
416packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000417{
418 if (compression_buffer_ready == 1)
419 return;
420 compression_buffer_ready = 1;
421 buffer_init(&compression_buffer);
422}
423
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424void
425packet_start_compression(int level)
426{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000427 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100428 fatal("Compression already enabled.");
429 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000430 packet_init_compression();
431 buffer_compress_init_send(level);
432 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433}
434
Damien Miller5428f641999-11-25 11:54:57 +1100435/*
Damien Miller5428f641999-11-25 11:54:57 +1100436 * Causes any further packets to be encrypted using the given key. The same
437 * key is used for both sending and reception. However, both directions are
438 * encrypted independently of each other.
439 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000440
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000442packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100443 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444{
Damien Miller874d77b2000-10-14 16:23:11 +1100445 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000446
Damien Miller874d77b2000-10-14 16:23:11 +1100447 if (cipher == NULL)
448 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000449 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100450 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000451 if (keylen > SSH_SESSION_KEY_LENGTH)
452 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
453 memcpy(ssh1_key, key, keylen);
454 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100455 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
456 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457}
458
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000459u_int
460packet_get_encryption_key(u_char *key)
461{
462 if (key == NULL)
463 return (ssh1_keylen);
464 memcpy(key, ssh1_key, ssh1_keylen);
465 return (ssh1_keylen);
466}
467
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000468/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000470packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000472 u_char buf[9];
473 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Ben Lindstrom204e4882001-03-05 06:47:00 +0000475 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000476 len = compat20 ? 6 : 9;
477 memset(buf, 0, len - 1);
478 buf[len - 1] = type;
479 buffer_clear(&outgoing_packet);
480 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000481}
482
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000483/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484void
485packet_put_char(int value)
486{
Damien Miller95def091999-11-25 00:26:21 +1100487 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000488
Damien Miller95def091999-11-25 00:26:21 +1100489 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490}
Damien Miller4f7becb2006-03-26 14:10:14 +1100491
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000493packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494{
Damien Miller95def091999-11-25 00:26:21 +1100495 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496}
Damien Miller4f7becb2006-03-26 14:10:14 +1100497
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100499packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500{
Damien Miller95def091999-11-25 00:26:21 +1100501 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502}
Damien Miller4f7becb2006-03-26 14:10:14 +1100503
Damien Miller33b13562000-04-04 14:38:59 +1000504void
505packet_put_cstring(const char *str)
506{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000507 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000508}
Damien Miller4f7becb2006-03-26 14:10:14 +1100509
Damien Miller33b13562000-04-04 14:38:59 +1000510void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100511packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000512{
513 buffer_append(&outgoing_packet, buf, len);
514}
Damien Miller4f7becb2006-03-26 14:10:14 +1100515
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516void
Damien Miller95def091999-11-25 00:26:21 +1100517packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518{
Damien Miller95def091999-11-25 00:26:21 +1100519 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520}
Damien Miller4f7becb2006-03-26 14:10:14 +1100521
Damien Miller33b13562000-04-04 14:38:59 +1000522void
523packet_put_bignum2(BIGNUM * value)
524{
525 buffer_put_bignum2(&outgoing_packet, value);
526}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527
Damien Miller5428f641999-11-25 11:54:57 +1100528/*
529 * Finalizes and sends the packet. If the encryption key has been set,
530 * encrypts the packet before sending.
531 */
Damien Miller95def091999-11-25 00:26:21 +1100532
Ben Lindstrombba81212001-06-25 05:01:22 +0000533static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000534packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000536 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100537 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000538 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000539 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540
Damien Miller5428f641999-11-25 11:54:57 +1100541 /*
542 * If using packet compression, compress the payload of the outgoing
543 * packet.
544 */
Damien Miller95def091999-11-25 00:26:21 +1100545 if (packet_compression) {
546 buffer_clear(&compression_buffer);
547 /* Skip padding. */
548 buffer_consume(&outgoing_packet, 8);
549 /* padding */
550 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
551 buffer_compress(&outgoing_packet, &compression_buffer);
552 buffer_clear(&outgoing_packet);
553 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100554 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100555 }
556 /* Compute packet length without padding (add checksum, remove padding). */
557 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558
Damien Millere247cc42000-05-07 12:03:14 +1000559 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100560 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100561 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100562 cp = buffer_ptr(&outgoing_packet);
563 for (i = 0; i < padding; i++) {
564 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000565 rnd = arc4random();
566 cp[7 - i] = rnd & 0xff;
567 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100568 }
569 }
570 buffer_consume(&outgoing_packet, 8 - padding);
571
572 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100573 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000574 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100575 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100576 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577
578#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100579 fprintf(stderr, "packet_send plain: ");
580 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581#endif
582
Damien Miller95def091999-11-25 00:26:21 +1100583 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100584 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100585 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100586 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100587 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100588 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100589
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100591 fprintf(stderr, "encrypted: ");
592 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593#endif
594
Damien Miller95def091999-11-25 00:26:21 +1100595 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596
Damien Miller5428f641999-11-25 11:54:57 +1100597 /*
Damien Miller788f2122005-11-05 15:14:59 +1100598 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100599 * actually sent until packet_write_wait or packet_write_poll is
600 * called.
601 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602}
603
Ben Lindstromf6027d32002-03-22 01:42:04 +0000604void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000605set_newkeys(int mode)
606{
607 Enc *enc;
608 Mac *mac;
609 Comp *comp;
610 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000611 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000612 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000613
Ben Lindstrom064496f2002-12-23 02:04:22 +0000614 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000615
Damien Miller963f6b22002-02-19 15:21:23 +1100616 if (mode == MODE_OUT) {
617 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000618 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000619 p_send.packets = p_send.blocks = 0;
620 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100621 } else {
622 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000623 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000624 p_read.packets = p_read.blocks = 0;
625 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100626 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000627 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000628 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100629 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000630 enc = &newkeys[mode]->enc;
631 mac = &newkeys[mode]->mac;
632 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000633 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000634 xfree(enc->name);
635 xfree(enc->iv);
636 xfree(enc->key);
637 xfree(mac->name);
638 xfree(mac->key);
639 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000640 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000641 }
642 newkeys[mode] = kex_get_newkeys(mode);
643 if (newkeys[mode] == NULL)
644 fatal("newkeys: no keys for mode %d", mode);
645 enc = &newkeys[mode]->enc;
646 mac = &newkeys[mode]->mac;
647 comp = &newkeys[mode]->comp;
648 if (mac->md != NULL)
649 mac->enabled = 1;
650 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100651 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000652 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000653 /* Deleting the keys does not gain extra security */
654 /* memset(enc->iv, 0, enc->block_size);
655 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000656 if ((comp->type == COMP_ZLIB ||
657 (comp->type == COMP_DELAYED && after_authentication)) &&
658 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000659 packet_init_compression();
660 if (mode == MODE_OUT)
661 buffer_compress_init_send(6);
662 else
663 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000664 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000665 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000666 /*
667 * The 2^(blocksize*2) limit is too expensive for 3DES,
668 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
669 */
670 if (enc->block_size >= 16)
671 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
672 else
673 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000674 if (rekey_limit)
675 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000676}
677
Damien Miller5428f641999-11-25 11:54:57 +1100678/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000679 * Delayed compression for SSH2 is enabled after authentication:
680 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
681 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
682 */
683static void
684packet_enable_delayed_compress(void)
685{
686 Comp *comp = NULL;
687 int mode;
688
689 /*
690 * Remember that we are past the authentication step, so rekeying
691 * with COMP_DELAYED will turn on compression immediately.
692 */
693 after_authentication = 1;
694 for (mode = 0; mode < MODE_MAX; mode++) {
695 comp = &newkeys[mode]->comp;
696 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000697 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000698 if (mode == MODE_OUT)
699 buffer_compress_init_send(6);
700 else
701 buffer_compress_init_recv();
702 comp->enabled = 1;
703 }
704 }
705}
706
707/*
Damien Miller33b13562000-04-04 14:38:59 +1000708 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
709 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000710static void
Damien Millera5539d22003-04-09 20:50:06 +1000711packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000712{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000713 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100714 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000715 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100716 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000717 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000718 Enc *enc = NULL;
719 Mac *mac = NULL;
720 Comp *comp = NULL;
721 int block_size;
722
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000723 if (newkeys[MODE_OUT] != NULL) {
724 enc = &newkeys[MODE_OUT]->enc;
725 mac = &newkeys[MODE_OUT]->mac;
726 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000727 }
Damien Miller963f6b22002-02-19 15:21:23 +1100728 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000729
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000730 cp = buffer_ptr(&outgoing_packet);
731 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000732
733#ifdef PACKET_DEBUG
734 fprintf(stderr, "plain: ");
735 buffer_dump(&outgoing_packet);
736#endif
737
738 if (comp && comp->enabled) {
739 len = buffer_len(&outgoing_packet);
740 /* skip header, compress only payload */
741 buffer_consume(&outgoing_packet, 5);
742 buffer_clear(&compression_buffer);
743 buffer_compress(&outgoing_packet, &compression_buffer);
744 buffer_clear(&outgoing_packet);
745 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
746 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
747 buffer_len(&compression_buffer));
748 DBG(debug("compression: raw %d compressed %d", len,
749 buffer_len(&outgoing_packet)));
750 }
751
752 /* sizeof (packet_len + pad_len + payload) */
753 len = buffer_len(&outgoing_packet);
754
755 /*
756 * calc size of padding, alloc space, get random data,
757 * minimum padding is 4 bytes
758 */
759 padlen = block_size - (len % block_size);
760 if (padlen < 4)
761 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100762 if (extra_pad) {
763 /* will wrap if extra_pad+padlen > 255 */
764 extra_pad = roundup(extra_pad, block_size);
765 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000766 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100767 pad, len, padlen, extra_pad);
768 padlen += pad;
769 extra_pad = 0;
770 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100771 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100772 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000773 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000774 for (i = 0; i < padlen; i++) {
775 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000776 rnd = arc4random();
777 cp[i] = rnd & 0xff;
778 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000779 }
Damien Millere247cc42000-05-07 12:03:14 +1000780 } else {
781 /* clear padding */
782 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000783 }
784 /* packet_length includes payload, padding and padding length field */
785 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000786 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100787 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000788 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000789 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
790
791 /* compute MAC over seqnr and packet(length fields, payload, padding) */
792 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000793 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100794 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000795 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000796 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000797 }
798 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100799 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100800 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000801 buffer_len(&outgoing_packet));
802 /* append unencrypted MAC */
803 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100804 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000805#ifdef PACKET_DEBUG
806 fprintf(stderr, "encrypted: ");
807 buffer_dump(&output);
808#endif
Damien Miller4af51302000-04-16 11:18:38 +1000809 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000810 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000811 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000812 if (++p_send.packets == 0)
813 if (!(datafellows & SSH_BUG_NOREKEY))
814 fatal("XXX too many packets with same key");
815 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000816 buffer_clear(&outgoing_packet);
817
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000818 if (type == SSH2_MSG_NEWKEYS)
819 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000820 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
821 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000822}
823
Damien Millera5539d22003-04-09 20:50:06 +1000824static void
825packet_send2(void)
826{
827 static int rekeying = 0;
828 struct packet *p;
829 u_char type, *cp;
830
831 cp = buffer_ptr(&outgoing_packet);
832 type = cp[5];
833
834 /* during rekeying we can only send key exchange messages */
835 if (rekeying) {
836 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
837 (type <= SSH2_MSG_TRANSPORT_MAX))) {
838 debug("enqueue packet: %u", type);
839 p = xmalloc(sizeof(*p));
840 p->type = type;
841 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
842 buffer_init(&outgoing_packet);
843 TAILQ_INSERT_TAIL(&outgoing, p, next);
844 return;
845 }
846 }
847
848 /* rekeying starts with sending KEXINIT */
849 if (type == SSH2_MSG_KEXINIT)
850 rekeying = 1;
851
852 packet_send2_wrapped();
853
854 /* after a NEWKEYS message we can send the complete queue */
855 if (type == SSH2_MSG_NEWKEYS) {
856 rekeying = 0;
857 while ((p = TAILQ_FIRST(&outgoing))) {
858 type = p->type;
859 debug("dequeue packet: %u", type);
860 buffer_free(&outgoing_packet);
861 memcpy(&outgoing_packet, &p->payload,
862 sizeof(Buffer));
863 TAILQ_REMOVE(&outgoing, p, next);
864 xfree(p);
865 packet_send2_wrapped();
866 }
867 }
868}
869
Damien Miller33b13562000-04-04 14:38:59 +1000870void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000871packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000872{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000873 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000874 packet_send2();
875 else
876 packet_send1();
877 DBG(debug("packet_send done"));
878}
879
Damien Miller33b13562000-04-04 14:38:59 +1000880/*
Damien Miller5428f641999-11-25 11:54:57 +1100881 * Waits until a packet has been received, and returns its type. Note that
882 * no other data is processed until this returns, so this function should not
883 * be used during the interactive session.
884 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885
886int
Damien Millerdff50992002-01-22 23:16:32 +1100887packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888{
Damien Miller95def091999-11-25 00:26:21 +1100889 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000890 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100891 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000892 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller07d86be2006-03-26 14:19:21 +1100894 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000895 sizeof(fd_mask));
896
Damien Miller95def091999-11-25 00:26:21 +1100897 /* Since we are blocking, ensure that all written packets have been sent. */
898 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899
Damien Miller95def091999-11-25 00:26:21 +1100900 /* Stay in the loop until we have received a complete packet. */
901 for (;;) {
902 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100903 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000904 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000905 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100906 || type == SSH_SMSG_FAILURE
907 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000908 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100909 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100910 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000911 if (type != SSH_MSG_NONE) {
912 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100913 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000914 }
Damien Miller5428f641999-11-25 11:54:57 +1100915 /*
916 * Otherwise, wait for some data to arrive, add it to the
917 * buffer, and try again.
918 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000919 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
920 sizeof(fd_mask));
921 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100922
Damien Miller95def091999-11-25 00:26:21 +1100923 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000924 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000925 (errno == EAGAIN || errno == EINTR))
926 ;
Damien Miller5428f641999-11-25 11:54:57 +1100927
Damien Miller95def091999-11-25 00:26:21 +1100928 /* Read data from the socket. */
929 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100930 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000931 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000932 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100933 }
Damien Miller95def091999-11-25 00:26:21 +1100934 if (len < 0)
935 fatal("Read from socket failed: %.100s", strerror(errno));
936 /* Append it to the buffer. */
937 packet_process_incoming(buf, len);
938 }
939 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940}
941
Damien Miller278f9072001-12-21 15:00:19 +1100942int
Damien Millerdff50992002-01-22 23:16:32 +1100943packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100944{
Damien Millerdff50992002-01-22 23:16:32 +1100945 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100946}
947
Damien Miller5428f641999-11-25 11:54:57 +1100948/*
949 * Waits until a packet has been received, verifies that its type matches
950 * that given, and gives a fatal error and exits if there is a mismatch.
951 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
953void
Damien Millerdff50992002-01-22 23:16:32 +1100954packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955{
Damien Miller95def091999-11-25 00:26:21 +1100956 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
Damien Millerdff50992002-01-22 23:16:32 +1100958 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100959 if (type != expected_type)
960 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000961 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962}
963
964/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100965 * packet_process_incoming. If so, reads the packet; otherwise returns
966 * SSH_MSG_NONE. This does not wait for data from the connection.
967 *
968 * SSH_MSG_DISCONNECT is handled specially here. Also,
969 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
970 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100971 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Ben Lindstrombba81212001-06-25 05:01:22 +0000973static int
Damien Millerdff50992002-01-22 23:16:32 +1100974packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000976 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000977 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000978 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller95def091999-11-25 00:26:21 +1100980 /* Check if input size is less than minimum packet size. */
981 if (buffer_len(&input) < 4 + 8)
982 return SSH_MSG_NONE;
983 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000984 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100985 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100986 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000987 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100988 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989
Damien Miller95def091999-11-25 00:26:21 +1100990 /* Check if the packet has been entirely received. */
991 if (buffer_len(&input) < 4 + padded_len)
992 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993
Damien Miller95def091999-11-25 00:26:21 +1100994 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995
Damien Miller95def091999-11-25 00:26:21 +1100996 /* Consume packet length. */
997 buffer_consume(&input, 4);
998
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000999 /*
1000 * Cryptographic attack detector for ssh
1001 * (C)1998 CORE-SDI, Buenos Aires Argentina
1002 * Ariel Futoransky(futo@core-sdi.com)
1003 */
Damien Miller963f6b22002-02-19 15:21:23 +11001004 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +11001005 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001006 packet_disconnect("crc32 compensation attack: network attack detected");
1007
1008 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001009 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001010 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001011 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001012
Damien Miller95def091999-11-25 00:26:21 +11001013 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014
1015#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001016 fprintf(stderr, "read_poll plain: ");
1017 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019
Damien Miller95def091999-11-25 00:26:21 +11001020 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001021 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001022 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
Damien Miller95def091999-11-25 00:26:21 +11001024 /* Skip padding. */
1025 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001026
Damien Miller95def091999-11-25 00:26:21 +11001027 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001028 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001029 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001030 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001032 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001033 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001034 if (checksum != stored_checksum)
1035 packet_disconnect("Corrupted check bytes on input.");
1036 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037
Damien Miller95def091999-11-25 00:26:21 +11001038 if (packet_compression) {
1039 buffer_clear(&compression_buffer);
1040 buffer_uncompress(&incoming_packet, &compression_buffer);
1041 buffer_clear(&incoming_packet);
1042 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001043 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001044 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001045 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001046 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1047 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001048 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049}
Damien Miller95def091999-11-25 00:26:21 +11001050
Ben Lindstrombba81212001-06-25 05:01:22 +00001051static int
Damien Millerdff50992002-01-22 23:16:32 +11001052packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001053{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001054 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001055 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001056 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001057 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001058 Enc *enc = NULL;
1059 Mac *mac = NULL;
1060 Comp *comp = NULL;
1061
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001062 if (newkeys[MODE_IN] != NULL) {
1063 enc = &newkeys[MODE_IN]->enc;
1064 mac = &newkeys[MODE_IN]->mac;
1065 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001066 }
1067 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001068 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001069
1070 if (packet_length == 0) {
1071 /*
1072 * check if input size is less than the cipher block size,
1073 * decrypt first block and extract length of incoming packet
1074 */
1075 if (buffer_len(&input) < block_size)
1076 return SSH_MSG_NONE;
1077 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001078 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001079 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001080 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001081 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001082 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001083 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001084#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001085 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001086#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001087 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001088 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001089 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001090 buffer_consume(&input, block_size);
1091 }
1092 /* we have a partial packet of block_size bytes */
1093 need = 4 + packet_length - block_size;
1094 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1095 need, maclen));
1096 if (need % block_size != 0)
1097 fatal("padding error: need %d block %d mod %d",
1098 need, block_size, need % block_size);
1099 /*
1100 * check if the entire packet has been received and
1101 * decrypt into incoming_packet
1102 */
1103 if (buffer_len(&input) < need + maclen)
1104 return SSH_MSG_NONE;
1105#ifdef PACKET_DEBUG
1106 fprintf(stderr, "read_poll enc/full: ");
1107 buffer_dump(&input);
1108#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001109 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001110 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001111 buffer_consume(&input, need);
1112 /*
1113 * compute MAC over seqnr and packet,
1114 * increment sequence number for incoming packet
1115 */
Damien Miller4af51302000-04-16 11:18:38 +10001116 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001117 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001118 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001119 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001120 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001121 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001122 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001123 buffer_consume(&input, mac->mac_len);
1124 }
Damien Miller278f9072001-12-21 15:00:19 +11001125 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001126 *seqnr_p = p_read.seqnr;
1127 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001128 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001129 if (++p_read.packets == 0)
1130 if (!(datafellows & SSH_BUG_NOREKEY))
1131 fatal("XXX too many packets with same key");
1132 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001133
1134 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001135 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001136 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001137 DBG(debug("input: padlen %d", padlen));
1138 if (padlen < 4)
1139 packet_disconnect("Corrupted padlen %d on input.", padlen);
1140
1141 /* skip packet size + padlen, discard padding */
1142 buffer_consume(&incoming_packet, 4 + 1);
1143 buffer_consume_end(&incoming_packet, padlen);
1144
1145 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1146 if (comp && comp->enabled) {
1147 buffer_clear(&compression_buffer);
1148 buffer_uncompress(&incoming_packet, &compression_buffer);
1149 buffer_clear(&incoming_packet);
1150 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1151 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001152 DBG(debug("input: len after de-compress %d",
1153 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001154 }
1155 /*
1156 * get packet type, implies consume.
1157 * return length of payload (without type field)
1158 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001159 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001160 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1161 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001162 if (type == SSH2_MSG_NEWKEYS)
1163 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001164 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1165 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001166#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001167 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001168 buffer_dump(&incoming_packet);
1169#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001170 /* reset for next packet */
1171 packet_length = 0;
1172 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001173}
1174
1175int
Damien Millerdff50992002-01-22 23:16:32 +11001176packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001177{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001178 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001179 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001180 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001181
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001182 for (;;) {
1183 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001184 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001185 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001186 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001187 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001188 case SSH2_MSG_IGNORE:
1189 break;
1190 case SSH2_MSG_DEBUG:
1191 packet_get_char();
1192 msg = packet_get_string(NULL);
1193 debug("Remote: %.900s", msg);
1194 xfree(msg);
1195 msg = packet_get_string(NULL);
1196 xfree(msg);
1197 break;
1198 case SSH2_MSG_DISCONNECT:
1199 reason = packet_get_int();
1200 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001201 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001202 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001203 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001204 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001205 break;
Damien Miller659811f2002-01-22 23:23:11 +11001206 case SSH2_MSG_UNIMPLEMENTED:
1207 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001208 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1209 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001210 break;
Damien Miller33b13562000-04-04 14:38:59 +10001211 default:
1212 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001213 }
Damien Miller33b13562000-04-04 14:38:59 +10001214 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001215 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001216 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001217 case SSH_MSG_IGNORE:
1218 break;
1219 case SSH_MSG_DEBUG:
1220 msg = packet_get_string(NULL);
1221 debug("Remote: %.900s", msg);
1222 xfree(msg);
1223 break;
1224 case SSH_MSG_DISCONNECT:
1225 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001226 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001227 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001228 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001229 xfree(msg);
1230 break;
1231 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001232 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001233 DBG(debug("received packet type %d", type));
1234 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001235 }
Damien Miller33b13562000-04-04 14:38:59 +10001236 }
1237 }
1238}
1239
Damien Miller278f9072001-12-21 15:00:19 +11001240int
Damien Millerdff50992002-01-22 23:16:32 +11001241packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001242{
Damien Millerdff50992002-01-22 23:16:32 +11001243 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001244}
1245
Damien Miller5428f641999-11-25 11:54:57 +11001246/*
1247 * Buffers the given amount of input characters. This is intended to be used
1248 * together with packet_read_poll.
1249 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250
1251void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001252packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253{
Damien Miller95def091999-11-25 00:26:21 +11001254 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255}
1256
1257/* Returns a character from the packet. */
1258
Ben Lindstrom46c16222000-12-22 01:43:59 +00001259u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001260packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261{
Damien Miller95def091999-11-25 00:26:21 +11001262 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001263
Damien Miller95def091999-11-25 00:26:21 +11001264 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001265 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266}
1267
1268/* Returns an integer from the packet data. */
1269
Ben Lindstrom46c16222000-12-22 01:43:59 +00001270u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001271packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272{
Damien Miller95def091999-11-25 00:26:21 +11001273 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001274}
1275
Damien Miller5428f641999-11-25 11:54:57 +11001276/*
1277 * Returns an arbitrary precision integer from the packet data. The integer
1278 * must have been initialized before this call.
1279 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001280
1281void
Damien Millerd432ccf2002-01-22 23:14:44 +11001282packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001283{
Damien Miller76e1e362002-01-22 23:15:57 +11001284 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285}
1286
Damien Miller33b13562000-04-04 14:38:59 +10001287void
Damien Millerd432ccf2002-01-22 23:14:44 +11001288packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001289{
Damien Miller76e1e362002-01-22 23:15:57 +11001290 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001291}
1292
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001293void *
Damien Millereccb9de2005-06-17 12:59:34 +10001294packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001295{
Damien Millereccb9de2005-06-17 12:59:34 +10001296 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001297
Damien Miller33b13562000-04-04 14:38:59 +10001298 if (length_ptr != NULL)
1299 *length_ptr = bytes;
1300 return buffer_ptr(&incoming_packet);
1301}
1302
Damien Miller4af51302000-04-16 11:18:38 +10001303int
1304packet_remaining(void)
1305{
1306 return buffer_len(&incoming_packet);
1307}
1308
Damien Miller5428f641999-11-25 11:54:57 +11001309/*
1310 * Returns a string from the packet data. The string is allocated using
1311 * xmalloc; it is the responsibility of the calling program to free it when
1312 * no longer needed. The length_ptr argument may be NULL, or point to an
1313 * integer into which the length of the string is stored.
1314 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001316void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001317packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318{
Damien Miller95def091999-11-25 00:26:21 +11001319 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320}
1321
Damien Miller5428f641999-11-25 11:54:57 +11001322/*
1323 * Sends a diagnostic message from the server to the client. This message
1324 * can be sent at any time (but not while constructing another message). The
1325 * message is printed immediately, but only if the client is being executed
1326 * in verbose mode. These messages are primarily intended to ease debugging
1327 * authentication problems. The length of the formatted message must not
1328 * exceed 1024 bytes. This will automatically call packet_write_wait.
1329 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001330
1331void
Damien Miller95def091999-11-25 00:26:21 +11001332packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333{
Damien Miller95def091999-11-25 00:26:21 +11001334 char buf[1024];
1335 va_list args;
1336
Ben Lindstroma14ee472000-12-07 01:24:58 +00001337 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1338 return;
1339
Damien Miller95def091999-11-25 00:26:21 +11001340 va_start(args, fmt);
1341 vsnprintf(buf, sizeof(buf), fmt, args);
1342 va_end(args);
1343
Damien Miller7c8af4f2000-05-01 08:24:07 +10001344 if (compat20) {
1345 packet_start(SSH2_MSG_DEBUG);
1346 packet_put_char(0); /* bool: always display */
1347 packet_put_cstring(buf);
1348 packet_put_cstring("");
1349 } else {
1350 packet_start(SSH_MSG_DEBUG);
1351 packet_put_cstring(buf);
1352 }
Damien Miller95def091999-11-25 00:26:21 +11001353 packet_send();
1354 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355}
1356
Damien Miller5428f641999-11-25 11:54:57 +11001357/*
1358 * Logs the error plus constructs and sends a disconnect packet, closes the
1359 * connection, and exits. This function never returns. The error message
1360 * should not contain a newline. The length of the formatted message must
1361 * not exceed 1024 bytes.
1362 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363
1364void
Damien Miller95def091999-11-25 00:26:21 +11001365packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366{
Damien Miller95def091999-11-25 00:26:21 +11001367 char buf[1024];
1368 va_list args;
1369 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001370
Damien Miller95def091999-11-25 00:26:21 +11001371 if (disconnecting) /* Guard against recursive invocations. */
1372 fatal("packet_disconnect called recursively.");
1373 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001374
Damien Miller5428f641999-11-25 11:54:57 +11001375 /*
1376 * Format the message. Note that the caller must make sure the
1377 * message is of limited size.
1378 */
Damien Miller95def091999-11-25 00:26:21 +11001379 va_start(args, fmt);
1380 vsnprintf(buf, sizeof(buf), fmt, args);
1381 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001382
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001383 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001384 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001385
Damien Miller95def091999-11-25 00:26:21 +11001386 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001387 if (compat20) {
1388 packet_start(SSH2_MSG_DISCONNECT);
1389 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1390 packet_put_cstring(buf);
1391 packet_put_cstring("");
1392 } else {
1393 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001394 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001395 }
Damien Miller95def091999-11-25 00:26:21 +11001396 packet_send();
1397 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001398
Damien Miller95def091999-11-25 00:26:21 +11001399 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001400 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401
Damien Miller95def091999-11-25 00:26:21 +11001402 /* Close the connection. */
1403 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001404 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405}
1406
Damien Miller5428f641999-11-25 11:54:57 +11001407/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001408
1409void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001410packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411{
Damien Miller95def091999-11-25 00:26:21 +11001412 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001413
Damien Miller95def091999-11-25 00:26:21 +11001414 if (len > 0) {
1415 len = write(connection_out, buffer_ptr(&output), len);
1416 if (len <= 0) {
1417 if (errno == EAGAIN)
1418 return;
1419 else
1420 fatal("Write failed: %.100s", strerror(errno));
1421 }
1422 buffer_consume(&output, len);
1423 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424}
1425
Damien Miller5428f641999-11-25 11:54:57 +11001426/*
1427 * Calls packet_write_poll repeatedly until all pending output data has been
1428 * written.
1429 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430
1431void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001432packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001434 fd_set *setp;
1435
Damien Miller07d86be2006-03-26 14:19:21 +11001436 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001437 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001438 packet_write_poll();
1439 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001440 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1441 sizeof(fd_mask));
1442 FD_SET(connection_out, setp);
1443 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001444 (errno == EAGAIN || errno == EINTR))
1445 ;
Damien Miller95def091999-11-25 00:26:21 +11001446 packet_write_poll();
1447 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001448 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449}
1450
1451/* Returns true if there is buffered data to write to the connection. */
1452
1453int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001454packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455{
Damien Miller95def091999-11-25 00:26:21 +11001456 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001457}
1458
1459/* Returns true if there is not too much data to write to the connection. */
1460
1461int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001462packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001463{
Damien Miller95def091999-11-25 00:26:21 +11001464 if (interactive_mode)
1465 return buffer_len(&output) < 16384;
1466 else
1467 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468}
1469
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001470
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001471static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001472packet_set_tos(int interactive)
1473{
Damien Miller5924ceb2003-11-22 15:02:42 +11001474#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001475 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1476
1477 if (!packet_connection_is_on_socket() ||
1478 !packet_connection_is_ipv4())
1479 return;
1480 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1481 sizeof(tos)) < 0)
1482 error("setsockopt IP_TOS %d: %.100s:",
1483 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001484#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001485}
Ben Lindstroma7433982002-12-23 02:41:41 +00001486
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487/* Informs that the current session is interactive. Sets IP flags for that. */
1488
1489void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001490packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001492 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001494 if (called)
1495 return;
1496 called = 1;
1497
Damien Miller95def091999-11-25 00:26:21 +11001498 /* Record that we are in interactive mode. */
1499 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500
Damien Miller34132e52000-01-14 15:45:46 +11001501 /* Only set socket options if using a socket. */
1502 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001503 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001504 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001505 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506}
1507
1508/* Returns true if the current connection is interactive. */
1509
1510int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001511packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001512{
Damien Miller95def091999-11-25 00:26:21 +11001513 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001514}
Damien Miller6162d121999-11-21 13:23:52 +11001515
Darren Tucker1f8311c2004-05-13 16:39:33 +10001516int
Darren Tucker502d3842003-06-28 12:38:01 +10001517packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001518{
Damien Miller95def091999-11-25 00:26:21 +11001519 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001520
Damien Miller95def091999-11-25 00:26:21 +11001521 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001522 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001523 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001524 return -1;
1525 }
1526 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001527 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001528 return -1;
1529 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001530 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001531 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001532 max_packet_size = s;
1533 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001534}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001535
Damien Miller9f643902001-11-12 11:02:52 +11001536/* roundup current message to pad bytes */
1537void
1538packet_add_padding(u_char pad)
1539{
1540 extra_pad = pad;
1541}
1542
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001543/*
1544 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001545 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001546 * byte SSH_MSG_IGNORE
1547 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001548 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001549 * All implementations MUST understand (and ignore) this message at any
1550 * time (after receiving the protocol version). No implementation is
1551 * required to send them. This message can be used as an additional
1552 * protection measure against advanced traffic analysis techniques.
1553 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001554void
1555packet_send_ignore(int nbytes)
1556{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001557 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001558 int i;
1559
1560 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001561 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001562 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001563 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001564 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001565 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001566 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001567 }
1568}
Damien Millera5539d22003-04-09 20:50:06 +10001569
Darren Tucker1f8311c2004-05-13 16:39:33 +10001570#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001571int
1572packet_need_rekeying(void)
1573{
1574 if (datafellows & SSH_BUG_NOREKEY)
1575 return 0;
1576 return
1577 (p_send.packets > MAX_PACKETS) ||
1578 (p_read.packets > MAX_PACKETS) ||
1579 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1580 (max_blocks_in && (p_read.blocks > max_blocks_in));
1581}
1582
1583void
1584packet_set_rekey_limit(u_int32_t bytes)
1585{
1586 rekey_limit = bytes;
1587}
Damien Miller9786e6e2005-07-26 21:54:56 +10001588
1589void
1590packet_set_server(void)
1591{
1592 server_side = 1;
1593}
1594
1595void
1596packet_set_authenticated(void)
1597{
1598 after_authentication = 1;
1599}