blob: 52b4f664c8274edb344f0f3fa46a69ff70ceb434 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains code implementing the packet protocol and communication
6 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100013 *
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000016 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Darren Tucker3e33cec2003-10-02 16:12:36 +100040RCSID("$OpenBSD: packet.c,v 1.112 2003/09/23 20:17:11 markus Exp $");
Damien Millera5539d22003-04-09 20:50:06 +100041
Damien Millera0898b82003-04-09 21:05:52 +100042#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "xmalloc.h"
45#include "buffer.h"
46#include "packet.h"
47#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "getput.h"
50
51#include "compress.h"
52#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000053#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller33b13562000-04-04 14:38:59 +100055#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100057#include "ssh2.h"
58
Damien Miller874d77b2000-10-14 16:23:11 +110059#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100060#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000061#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "log.h"
63#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110064#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000065#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100066
67#ifdef PACKET_DEBUG
68#define DBG(x) x
69#else
70#define DBG(x)
71#endif
72
Damien Miller5428f641999-11-25 11:54:57 +110073/*
74 * This variable contains the file descriptors used for communicating with
75 * the other side. connection_in is used for reading; connection_out for
76 * writing. These can be the same descriptor, in which case it is assumed to
77 * be a socket.
78 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079static int connection_in = -1;
80static int connection_out = -1;
81
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000083static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
85/* Encryption context for receiving data. This is only used for decryption. */
86static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110087
88/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089static CipherContext send_context;
90
91/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000092Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000095Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
97/* Buffer for the partial outgoing packet being constructed. */
98static Buffer outgoing_packet;
99
100/* Buffer for the incoming packet currently being processed. */
101static Buffer incoming_packet;
102
103/* Scratch buffer for packet compression/decompression. */
104static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000105static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
107/* Flag indicating whether packet compression/decompression is enabled. */
108static int packet_compression = 0;
109
Damien Miller6162d121999-11-21 13:23:52 +1100110/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000111u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113/* Flag indicating whether this module has been initialized. */
114static int initialized = 0;
115
116/* Set to true if the connection is interactive. */
117static int interactive_mode = 0;
118
Damien Miller33b13562000-04-04 14:38:59 +1000119/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000120Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000121static struct packet_state {
122 u_int32_t seqnr;
123 u_int32_t packets;
124 u_int64_t blocks;
125} p_read, p_send;
126
127static u_int64_t max_blocks_in, max_blocks_out;
128static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000129
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000130/* Session key for protocol v1 */
131static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
132static u_int ssh1_keylen;
133
Damien Miller9f643902001-11-12 11:02:52 +1100134/* roundup current message to extra_pad bytes */
135static u_char extra_pad = 0;
136
Damien Millera5539d22003-04-09 20:50:06 +1000137struct packet {
138 TAILQ_ENTRY(packet) next;
139 u_char type;
140 Buffer payload;
141};
142TAILQ_HEAD(, packet) outgoing;
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
145 * Sets the descriptors used for communication. Disables encryption until
146 * packet_set_encryption_key is called.
147 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148void
149packet_set_connection(int fd_in, int fd_out)
150{
Damien Miller874d77b2000-10-14 16:23:11 +1100151 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000152
Damien Miller874d77b2000-10-14 16:23:11 +1100153 if (none == NULL)
154 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100155 connection_in = fd_in;
156 connection_out = fd_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100157 cipher_init(&send_context, none, "", 0, NULL, 0, CIPHER_ENCRYPT);
158 cipher_init(&receive_context, none, "", 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000159 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100160 if (!initialized) {
161 initialized = 1;
162 buffer_init(&input);
163 buffer_init(&output);
164 buffer_init(&outgoing_packet);
165 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000166 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100167 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168}
169
Damien Miller34132e52000-01-14 15:45:46 +1100170/* Returns 1 if remote host is connected via socket, 0 if not. */
171
172int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000173packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100174{
175 struct sockaddr_storage from, to;
176 socklen_t fromlen, tolen;
177
178 /* filedescriptors in and out are the same, so it's a socket */
179 if (connection_in == connection_out)
180 return 1;
181 fromlen = sizeof(from);
182 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100183 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100184 return 0;
185 tolen = sizeof(to);
186 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100187 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100188 return 0;
189 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
190 return 0;
191 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
192 return 0;
193 return 1;
194}
195
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000196/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000197 * Exports an IV from the CipherContext required to export the key
198 * state back from the unprivileged child to the privileged parent
199 * process.
200 */
201
202void
203packet_get_keyiv(int mode, u_char *iv, u_int len)
204{
205 CipherContext *cc;
206
207 if (mode == MODE_OUT)
208 cc = &send_context;
209 else
210 cc = &receive_context;
211
212 cipher_get_keyiv(cc, iv, len);
213}
214
215int
216packet_get_keycontext(int mode, u_char *dat)
217{
218 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000219
Ben Lindstromf6027d32002-03-22 01:42:04 +0000220 if (mode == MODE_OUT)
221 cc = &send_context;
222 else
223 cc = &receive_context;
224
225 return (cipher_get_keycontext(cc, dat));
226}
227
228void
229packet_set_keycontext(int mode, u_char *dat)
230{
231 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000232
Ben Lindstromf6027d32002-03-22 01:42:04 +0000233 if (mode == MODE_OUT)
234 cc = &send_context;
235 else
236 cc = &receive_context;
237
238 cipher_set_keycontext(cc, dat);
239}
240
241int
242packet_get_keyiv_len(int mode)
243{
244 CipherContext *cc;
245
246 if (mode == MODE_OUT)
247 cc = &send_context;
248 else
249 cc = &receive_context;
250
251 return (cipher_get_keyiv_len(cc));
252}
253void
254packet_set_iv(int mode, u_char *dat)
255{
256 CipherContext *cc;
257
258 if (mode == MODE_OUT)
259 cc = &send_context;
260 else
261 cc = &receive_context;
262
263 cipher_set_keyiv(cc, dat);
264}
265int
Damien Miller2b92d322003-06-11 22:05:06 +1000266packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000267{
268 return (cipher_get_number(receive_context.cipher));
269}
270
Damien Millera5539d22003-04-09 20:50:06 +1000271void
272packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000273{
Damien Millera5539d22003-04-09 20:50:06 +1000274 struct packet_state *state;
275
276 state = (mode == MODE_IN) ? &p_read : &p_send;
277 *seqnr = state->seqnr;
278 *blocks = state->blocks;
279 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000280}
281
282void
Damien Millera5539d22003-04-09 20:50:06 +1000283packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000284{
Damien Millera5539d22003-04-09 20:50:06 +1000285 struct packet_state *state;
286
287 state = (mode == MODE_IN) ? &p_read : &p_send;
288 state->seqnr = seqnr;
289 state->blocks = blocks;
290 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000291}
292
Damien Miller34132e52000-01-14 15:45:46 +1100293/* returns 1 if connection is via ipv4 */
294
295int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000296packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100297{
298 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100299 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100300
301 memset(&to, 0, sizeof(to));
302 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
303 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000304 if (to.ss_family == AF_INET)
305 return 1;
306#ifdef IPV4_IN_IPV6
307 if (to.ss_family == AF_INET6 &&
308 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
309 return 1;
310#endif
311 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100312}
313
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314/* Sets the connection into non-blocking mode. */
315
316void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000317packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318{
Damien Miller95def091999-11-25 00:26:21 +1100319 /* Set the socket into non-blocking mode. */
320 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
321 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller95def091999-11-25 00:26:21 +1100323 if (connection_out != connection_in) {
324 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
325 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
326 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327}
328
329/* Returns the socket used for reading. */
330
331int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000332packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333{
Damien Miller95def091999-11-25 00:26:21 +1100334 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335}
336
337/* Returns the descriptor used for writing. */
338
339int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000340packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341{
Damien Miller95def091999-11-25 00:26:21 +1100342 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343}
344
345/* Closes the connection and clears and frees internal data structures. */
346
347void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000348packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349{
Damien Miller95def091999-11-25 00:26:21 +1100350 if (!initialized)
351 return;
352 initialized = 0;
353 if (connection_in == connection_out) {
354 shutdown(connection_out, SHUT_RDWR);
355 close(connection_out);
356 } else {
357 close(connection_in);
358 close(connection_out);
359 }
360 buffer_free(&input);
361 buffer_free(&output);
362 buffer_free(&outgoing_packet);
363 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000364 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100365 buffer_free(&compression_buffer);
366 buffer_compress_uninit();
367 }
Damien Miller963f6b22002-02-19 15:21:23 +1100368 cipher_cleanup(&send_context);
369 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370}
371
372/* Sets remote side protocol flags. */
373
374void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000375packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376{
Damien Miller95def091999-11-25 00:26:21 +1100377 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378}
379
380/* Returns the remote protocol flags set earlier by the above function. */
381
Ben Lindstrom46c16222000-12-22 01:43:59 +0000382u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000383packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384{
Damien Miller95def091999-11-25 00:26:21 +1100385 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386}
387
Damien Miller5428f641999-11-25 11:54:57 +1100388/*
389 * Starts packet compression from the next packet on in both directions.
390 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
391 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392
Ben Lindstrombba81212001-06-25 05:01:22 +0000393static void
394packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000395{
396 if (compression_buffer_ready == 1)
397 return;
398 compression_buffer_ready = 1;
399 buffer_init(&compression_buffer);
400}
401
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402void
403packet_start_compression(int level)
404{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000405 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100406 fatal("Compression already enabled.");
407 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000408 packet_init_compression();
409 buffer_compress_init_send(level);
410 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411}
412
Damien Miller5428f641999-11-25 11:54:57 +1100413/*
Damien Miller5428f641999-11-25 11:54:57 +1100414 * Causes any further packets to be encrypted using the given key. The same
415 * key is used for both sending and reception. However, both directions are
416 * encrypted independently of each other.
417 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000418
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000420packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100421 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422{
Damien Miller874d77b2000-10-14 16:23:11 +1100423 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000424
Damien Miller874d77b2000-10-14 16:23:11 +1100425 if (cipher == NULL)
426 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000427 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100428 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000429 if (keylen > SSH_SESSION_KEY_LENGTH)
430 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
431 memcpy(ssh1_key, key, keylen);
432 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100433 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
434 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435}
436
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000437u_int
438packet_get_encryption_key(u_char *key)
439{
440 if (key == NULL)
441 return (ssh1_keylen);
442 memcpy(key, ssh1_key, ssh1_keylen);
443 return (ssh1_keylen);
444}
445
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000446/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000448packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000450 u_char buf[9];
451 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452
Ben Lindstrom204e4882001-03-05 06:47:00 +0000453 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000454 len = compat20 ? 6 : 9;
455 memset(buf, 0, len - 1);
456 buf[len - 1] = type;
457 buffer_clear(&outgoing_packet);
458 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000459}
460
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000461/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462void
463packet_put_char(int value)
464{
Damien Miller95def091999-11-25 00:26:21 +1100465 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000466
Damien Miller95def091999-11-25 00:26:21 +1100467 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000470packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471{
Damien Miller95def091999-11-25 00:26:21 +1100472 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100475packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476{
Damien Miller95def091999-11-25 00:26:21 +1100477 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478}
Damien Miller33b13562000-04-04 14:38:59 +1000479void
480packet_put_cstring(const char *str)
481{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000482 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000483}
Damien Miller33b13562000-04-04 14:38:59 +1000484void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100485packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000486{
487 buffer_append(&outgoing_packet, buf, len);
488}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489void
Damien Miller95def091999-11-25 00:26:21 +1100490packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491{
Damien Miller95def091999-11-25 00:26:21 +1100492 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493}
Damien Miller33b13562000-04-04 14:38:59 +1000494void
495packet_put_bignum2(BIGNUM * value)
496{
497 buffer_put_bignum2(&outgoing_packet, value);
498}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499
Damien Miller5428f641999-11-25 11:54:57 +1100500/*
501 * Finalizes and sends the packet. If the encryption key has been set,
502 * encrypts the packet before sending.
503 */
Damien Miller95def091999-11-25 00:26:21 +1100504
Ben Lindstrombba81212001-06-25 05:01:22 +0000505static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000506packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000508 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100509 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000510 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100511 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Miller5428f641999-11-25 11:54:57 +1100513 /*
514 * If using packet compression, compress the payload of the outgoing
515 * packet.
516 */
Damien Miller95def091999-11-25 00:26:21 +1100517 if (packet_compression) {
518 buffer_clear(&compression_buffer);
519 /* Skip padding. */
520 buffer_consume(&outgoing_packet, 8);
521 /* padding */
522 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
523 buffer_compress(&outgoing_packet, &compression_buffer);
524 buffer_clear(&outgoing_packet);
525 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100526 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100527 }
528 /* Compute packet length without padding (add checksum, remove padding). */
529 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530
Damien Millere247cc42000-05-07 12:03:14 +1000531 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100532 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100533 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100534 cp = buffer_ptr(&outgoing_packet);
535 for (i = 0; i < padding; i++) {
536 if (i % 4 == 0)
537 rand = arc4random();
538 cp[7 - i] = rand & 0xff;
539 rand >>= 8;
540 }
541 }
542 buffer_consume(&outgoing_packet, 8 - padding);
543
544 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100545 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000546 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100547 PUT_32BIT(buf, checksum);
548 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549
550#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100551 fprintf(stderr, "packet_send plain: ");
552 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553#endif
554
Damien Miller95def091999-11-25 00:26:21 +1100555 /* Append to output. */
556 PUT_32BIT(buf, len);
557 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100558 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100559 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100560 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100561
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100563 fprintf(stderr, "encrypted: ");
564 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565#endif
566
Damien Miller95def091999-11-25 00:26:21 +1100567 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568
Damien Miller5428f641999-11-25 11:54:57 +1100569 /*
570 * Note that the packet is now only buffered in output. It won\'t be
571 * actually sent until packet_write_wait or packet_write_poll is
572 * called.
573 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574}
575
Ben Lindstromf6027d32002-03-22 01:42:04 +0000576void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000577set_newkeys(int mode)
578{
579 Enc *enc;
580 Mac *mac;
581 Comp *comp;
582 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000583 u_int64_t *max_blocks;
Damien Miller963f6b22002-02-19 15:21:23 +1100584 int encrypt;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000585
Ben Lindstrom064496f2002-12-23 02:04:22 +0000586 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000587
Damien Miller963f6b22002-02-19 15:21:23 +1100588 if (mode == MODE_OUT) {
589 cc = &send_context;
590 encrypt = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000591 p_send.packets = p_send.blocks = 0;
592 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100593 } else {
594 cc = &receive_context;
595 encrypt = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000596 p_read.packets = p_read.blocks = 0;
597 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100598 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000599 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000600 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100601 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000602 enc = &newkeys[mode]->enc;
603 mac = &newkeys[mode]->mac;
604 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000605 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000606 xfree(enc->name);
607 xfree(enc->iv);
608 xfree(enc->key);
609 xfree(mac->name);
610 xfree(mac->key);
611 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000612 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000613 }
614 newkeys[mode] = kex_get_newkeys(mode);
615 if (newkeys[mode] == NULL)
616 fatal("newkeys: no keys for mode %d", mode);
617 enc = &newkeys[mode]->enc;
618 mac = &newkeys[mode]->mac;
619 comp = &newkeys[mode]->comp;
620 if (mac->md != NULL)
621 mac->enabled = 1;
622 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100623 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
624 enc->iv, enc->block_size, encrypt);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000625 /* Deleting the keys does not gain extra security */
626 /* memset(enc->iv, 0, enc->block_size);
627 memset(enc->key, 0, enc->key_len); */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000628 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000629 packet_init_compression();
630 if (mode == MODE_OUT)
631 buffer_compress_init_send(6);
632 else
633 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000634 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000635 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000636 /*
637 * The 2^(blocksize*2) limit is too expensive for 3DES,
638 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
639 */
640 if (enc->block_size >= 16)
641 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
642 else
643 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000644 if (rekey_limit)
645 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000646}
647
Damien Miller5428f641999-11-25 11:54:57 +1100648/*
Damien Miller33b13562000-04-04 14:38:59 +1000649 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
650 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000651static void
Damien Millera5539d22003-04-09 20:50:06 +1000652packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000653{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000654 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100655 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000656 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100657 u_int i, len;
Damien Miller33b13562000-04-04 14:38:59 +1000658 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000659 Enc *enc = NULL;
660 Mac *mac = NULL;
661 Comp *comp = NULL;
662 int block_size;
663
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000664 if (newkeys[MODE_OUT] != NULL) {
665 enc = &newkeys[MODE_OUT]->enc;
666 mac = &newkeys[MODE_OUT]->mac;
667 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000668 }
Damien Miller963f6b22002-02-19 15:21:23 +1100669 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000670
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000671 cp = buffer_ptr(&outgoing_packet);
672 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000673
674#ifdef PACKET_DEBUG
675 fprintf(stderr, "plain: ");
676 buffer_dump(&outgoing_packet);
677#endif
678
679 if (comp && comp->enabled) {
680 len = buffer_len(&outgoing_packet);
681 /* skip header, compress only payload */
682 buffer_consume(&outgoing_packet, 5);
683 buffer_clear(&compression_buffer);
684 buffer_compress(&outgoing_packet, &compression_buffer);
685 buffer_clear(&outgoing_packet);
686 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
687 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
688 buffer_len(&compression_buffer));
689 DBG(debug("compression: raw %d compressed %d", len,
690 buffer_len(&outgoing_packet)));
691 }
692
693 /* sizeof (packet_len + pad_len + payload) */
694 len = buffer_len(&outgoing_packet);
695
696 /*
697 * calc size of padding, alloc space, get random data,
698 * minimum padding is 4 bytes
699 */
700 padlen = block_size - (len % block_size);
701 if (padlen < 4)
702 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100703 if (extra_pad) {
704 /* will wrap if extra_pad+padlen > 255 */
705 extra_pad = roundup(extra_pad, block_size);
706 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000707 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100708 pad, len, padlen, extra_pad);
709 padlen += pad;
710 extra_pad = 0;
711 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100712 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100713 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000714 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000715 for (i = 0; i < padlen; i++) {
716 if (i % 4 == 0)
717 rand = arc4random();
718 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000719 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000720 }
Damien Millere247cc42000-05-07 12:03:14 +1000721 } else {
722 /* clear padding */
723 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000724 }
725 /* packet_length includes payload, padding and padding length field */
726 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000727 cp = buffer_ptr(&outgoing_packet);
728 PUT_32BIT(cp, packet_length);
729 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000730 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
731
732 /* compute MAC over seqnr and packet(length fields, payload, padding) */
733 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000734 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100735 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000736 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000737 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000738 }
739 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100740 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100741 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000742 buffer_len(&outgoing_packet));
743 /* append unencrypted MAC */
744 if (mac && mac->enabled)
745 buffer_append(&output, (char *)macbuf, mac->mac_len);
746#ifdef PACKET_DEBUG
747 fprintf(stderr, "encrypted: ");
748 buffer_dump(&output);
749#endif
Damien Miller4af51302000-04-16 11:18:38 +1000750 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000751 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000752 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000753 if (++p_send.packets == 0)
754 if (!(datafellows & SSH_BUG_NOREKEY))
755 fatal("XXX too many packets with same key");
756 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000757 buffer_clear(&outgoing_packet);
758
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000759 if (type == SSH2_MSG_NEWKEYS)
760 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000761}
762
Damien Millera5539d22003-04-09 20:50:06 +1000763static void
764packet_send2(void)
765{
766 static int rekeying = 0;
767 struct packet *p;
768 u_char type, *cp;
769
770 cp = buffer_ptr(&outgoing_packet);
771 type = cp[5];
772
773 /* during rekeying we can only send key exchange messages */
774 if (rekeying) {
775 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
776 (type <= SSH2_MSG_TRANSPORT_MAX))) {
777 debug("enqueue packet: %u", type);
778 p = xmalloc(sizeof(*p));
779 p->type = type;
780 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
781 buffer_init(&outgoing_packet);
782 TAILQ_INSERT_TAIL(&outgoing, p, next);
783 return;
784 }
785 }
786
787 /* rekeying starts with sending KEXINIT */
788 if (type == SSH2_MSG_KEXINIT)
789 rekeying = 1;
790
791 packet_send2_wrapped();
792
793 /* after a NEWKEYS message we can send the complete queue */
794 if (type == SSH2_MSG_NEWKEYS) {
795 rekeying = 0;
796 while ((p = TAILQ_FIRST(&outgoing))) {
797 type = p->type;
798 debug("dequeue packet: %u", type);
799 buffer_free(&outgoing_packet);
800 memcpy(&outgoing_packet, &p->payload,
801 sizeof(Buffer));
802 TAILQ_REMOVE(&outgoing, p, next);
803 xfree(p);
804 packet_send2_wrapped();
805 }
806 }
807}
808
Damien Miller33b13562000-04-04 14:38:59 +1000809void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000810packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000811{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000812 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000813 packet_send2();
814 else
815 packet_send1();
816 DBG(debug("packet_send done"));
817}
818
Damien Miller33b13562000-04-04 14:38:59 +1000819/*
Damien Miller5428f641999-11-25 11:54:57 +1100820 * Waits until a packet has been received, and returns its type. Note that
821 * no other data is processed until this returns, so this function should not
822 * be used during the interactive session.
823 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824
825int
Damien Millerdff50992002-01-22 23:16:32 +1100826packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000827{
Damien Miller95def091999-11-25 00:26:21 +1100828 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000829 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100830 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000831 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000833 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
834 sizeof(fd_mask));
835
Damien Miller95def091999-11-25 00:26:21 +1100836 /* Since we are blocking, ensure that all written packets have been sent. */
837 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838
Damien Miller95def091999-11-25 00:26:21 +1100839 /* Stay in the loop until we have received a complete packet. */
840 for (;;) {
841 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100842 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000843 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000844 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100845 || type == SSH_SMSG_FAILURE
846 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000847 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100848 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100849 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000850 if (type != SSH_MSG_NONE) {
851 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100852 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000853 }
Damien Miller5428f641999-11-25 11:54:57 +1100854 /*
855 * Otherwise, wait for some data to arrive, add it to the
856 * buffer, and try again.
857 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000858 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
859 sizeof(fd_mask));
860 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100861
Damien Miller95def091999-11-25 00:26:21 +1100862 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000863 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000864 (errno == EAGAIN || errno == EINTR))
865 ;
Damien Miller5428f641999-11-25 11:54:57 +1100866
Damien Miller95def091999-11-25 00:26:21 +1100867 /* Read data from the socket. */
868 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100869 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000870 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000871 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100872 }
Damien Miller95def091999-11-25 00:26:21 +1100873 if (len < 0)
874 fatal("Read from socket failed: %.100s", strerror(errno));
875 /* Append it to the buffer. */
876 packet_process_incoming(buf, len);
877 }
878 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879}
880
Damien Miller278f9072001-12-21 15:00:19 +1100881int
Damien Millerdff50992002-01-22 23:16:32 +1100882packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100883{
Damien Millerdff50992002-01-22 23:16:32 +1100884 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100885}
886
Damien Miller5428f641999-11-25 11:54:57 +1100887/*
888 * Waits until a packet has been received, verifies that its type matches
889 * that given, and gives a fatal error and exits if there is a mismatch.
890 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891
892void
Damien Millerdff50992002-01-22 23:16:32 +1100893packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894{
Damien Miller95def091999-11-25 00:26:21 +1100895 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896
Damien Millerdff50992002-01-22 23:16:32 +1100897 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100898 if (type != expected_type)
899 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000900 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901}
902
903/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100904 * packet_process_incoming. If so, reads the packet; otherwise returns
905 * SSH_MSG_NONE. This does not wait for data from the connection.
906 *
907 * SSH_MSG_DISCONNECT is handled specially here. Also,
908 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
909 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100910 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911
Ben Lindstrombba81212001-06-25 05:01:22 +0000912static int
Damien Millerdff50992002-01-22 23:16:32 +1100913packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000915 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000916 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000917 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918
Damien Miller95def091999-11-25 00:26:21 +1100919 /* Check if input size is less than minimum packet size. */
920 if (buffer_len(&input) < 4 + 8)
921 return SSH_MSG_NONE;
922 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000923 cp = buffer_ptr(&input);
924 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100925 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000926 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100927 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928
Damien Miller95def091999-11-25 00:26:21 +1100929 /* Check if the packet has been entirely received. */
930 if (buffer_len(&input) < 4 + padded_len)
931 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000932
Damien Miller95def091999-11-25 00:26:21 +1100933 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934
Damien Miller95def091999-11-25 00:26:21 +1100935 /* Consume packet length. */
936 buffer_consume(&input, 4);
937
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000938 /*
939 * Cryptographic attack detector for ssh
940 * (C)1998 CORE-SDI, Buenos Aires Argentina
941 * Ariel Futoransky(futo@core-sdi.com)
942 */
Damien Miller963f6b22002-02-19 15:21:23 +1100943 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000944 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
945 packet_disconnect("crc32 compensation attack: network attack detected");
946
947 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100948 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100949 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100950 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000951
Damien Miller95def091999-11-25 00:26:21 +1100952 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953
954#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100955 fprintf(stderr, "read_poll plain: ");
956 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
Damien Miller95def091999-11-25 00:26:21 +1100959 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100960 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000961 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962
Damien Miller95def091999-11-25 00:26:21 +1100963 /* Skip padding. */
964 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100965
Damien Miller95def091999-11-25 00:26:21 +1100966 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100967 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100968 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000969 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000971 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
972 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100973 if (checksum != stored_checksum)
974 packet_disconnect("Corrupted check bytes on input.");
975 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Damien Miller95def091999-11-25 00:26:21 +1100977 if (packet_compression) {
978 buffer_clear(&compression_buffer);
979 buffer_uncompress(&incoming_packet, &compression_buffer);
980 buffer_clear(&incoming_packet);
981 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000982 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100983 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000984 type = buffer_get_char(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000985 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986}
Damien Miller95def091999-11-25 00:26:21 +1100987
Ben Lindstrombba81212001-06-25 05:01:22 +0000988static int
Damien Millerdff50992002-01-22 23:16:32 +1100989packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000990{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000991 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000992 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000993 u_char *macbuf, *cp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000994 int maclen, block_size;
995 Enc *enc = NULL;
996 Mac *mac = NULL;
997 Comp *comp = NULL;
998
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000999 if (newkeys[MODE_IN] != NULL) {
1000 enc = &newkeys[MODE_IN]->enc;
1001 mac = &newkeys[MODE_IN]->mac;
1002 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001003 }
1004 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001005 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001006
1007 if (packet_length == 0) {
1008 /*
1009 * check if input size is less than the cipher block size,
1010 * decrypt first block and extract length of incoming packet
1011 */
1012 if (buffer_len(&input) < block_size)
1013 return SSH_MSG_NONE;
1014 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001015 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001016 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001017 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001018 cp = buffer_ptr(&incoming_packet);
1019 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001020 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001021#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001022 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001023#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001024 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001025 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001026 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001027 buffer_consume(&input, block_size);
1028 }
1029 /* we have a partial packet of block_size bytes */
1030 need = 4 + packet_length - block_size;
1031 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1032 need, maclen));
1033 if (need % block_size != 0)
1034 fatal("padding error: need %d block %d mod %d",
1035 need, block_size, need % block_size);
1036 /*
1037 * check if the entire packet has been received and
1038 * decrypt into incoming_packet
1039 */
1040 if (buffer_len(&input) < need + maclen)
1041 return SSH_MSG_NONE;
1042#ifdef PACKET_DEBUG
1043 fprintf(stderr, "read_poll enc/full: ");
1044 buffer_dump(&input);
1045#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001046 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001047 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001048 buffer_consume(&input, need);
1049 /*
1050 * compute MAC over seqnr and packet,
1051 * increment sequence number for incoming packet
1052 */
Damien Miller4af51302000-04-16 11:18:38 +10001053 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001054 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001055 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001056 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001057 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001058 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001059 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001060 buffer_consume(&input, mac->mac_len);
1061 }
Damien Miller278f9072001-12-21 15:00:19 +11001062 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001063 *seqnr_p = p_read.seqnr;
1064 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001065 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001066 if (++p_read.packets == 0)
1067 if (!(datafellows & SSH_BUG_NOREKEY))
1068 fatal("XXX too many packets with same key");
1069 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001070
1071 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001072 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001073 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001074 DBG(debug("input: padlen %d", padlen));
1075 if (padlen < 4)
1076 packet_disconnect("Corrupted padlen %d on input.", padlen);
1077
1078 /* skip packet size + padlen, discard padding */
1079 buffer_consume(&incoming_packet, 4 + 1);
1080 buffer_consume_end(&incoming_packet, padlen);
1081
1082 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1083 if (comp && comp->enabled) {
1084 buffer_clear(&compression_buffer);
1085 buffer_uncompress(&incoming_packet, &compression_buffer);
1086 buffer_clear(&incoming_packet);
1087 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1088 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001089 DBG(debug("input: len after de-compress %d",
1090 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001091 }
1092 /*
1093 * get packet type, implies consume.
1094 * return length of payload (without type field)
1095 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001096 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001097 if (type == SSH2_MSG_NEWKEYS)
1098 set_newkeys(MODE_IN);
Damien Miller33b13562000-04-04 14:38:59 +10001099#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001100 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001101 buffer_dump(&incoming_packet);
1102#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001103 /* reset for next packet */
1104 packet_length = 0;
1105 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001106}
1107
1108int
Damien Millerdff50992002-01-22 23:16:32 +11001109packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001110{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001111 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001112 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001113 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001114
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001115 for (;;) {
1116 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001117 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001118 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001119 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001120 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001121 case SSH2_MSG_IGNORE:
1122 break;
1123 case SSH2_MSG_DEBUG:
1124 packet_get_char();
1125 msg = packet_get_string(NULL);
1126 debug("Remote: %.900s", msg);
1127 xfree(msg);
1128 msg = packet_get_string(NULL);
1129 xfree(msg);
1130 break;
1131 case SSH2_MSG_DISCONNECT:
1132 reason = packet_get_int();
1133 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001134 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001135 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001136 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001137 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001138 break;
Damien Miller659811f2002-01-22 23:23:11 +11001139 case SSH2_MSG_UNIMPLEMENTED:
1140 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001141 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1142 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001143 break;
Damien Miller33b13562000-04-04 14:38:59 +10001144 default:
1145 return type;
1146 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001147 }
Damien Miller33b13562000-04-04 14:38:59 +10001148 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001149 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001150 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001151 case SSH_MSG_IGNORE:
1152 break;
1153 case SSH_MSG_DEBUG:
1154 msg = packet_get_string(NULL);
1155 debug("Remote: %.900s", msg);
1156 xfree(msg);
1157 break;
1158 case SSH_MSG_DISCONNECT:
1159 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001160 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001161 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001162 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001163 xfree(msg);
1164 break;
1165 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001166 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001167 DBG(debug("received packet type %d", type));
1168 return type;
1169 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001170 }
Damien Miller33b13562000-04-04 14:38:59 +10001171 }
1172 }
1173}
1174
Damien Miller278f9072001-12-21 15:00:19 +11001175int
Damien Millerdff50992002-01-22 23:16:32 +11001176packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001177{
Damien Millerdff50992002-01-22 23:16:32 +11001178 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001179}
1180
Damien Miller5428f641999-11-25 11:54:57 +11001181/*
1182 * Buffers the given amount of input characters. This is intended to be used
1183 * together with packet_read_poll.
1184 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185
1186void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001187packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188{
Damien Miller95def091999-11-25 00:26:21 +11001189 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190}
1191
1192/* Returns a character from the packet. */
1193
Ben Lindstrom46c16222000-12-22 01:43:59 +00001194u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001195packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001196{
Damien Miller95def091999-11-25 00:26:21 +11001197 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001198
Damien Miller95def091999-11-25 00:26:21 +11001199 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001200 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201}
1202
1203/* Returns an integer from the packet data. */
1204
Ben Lindstrom46c16222000-12-22 01:43:59 +00001205u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001206packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207{
Damien Miller95def091999-11-25 00:26:21 +11001208 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209}
1210
Damien Miller5428f641999-11-25 11:54:57 +11001211/*
1212 * Returns an arbitrary precision integer from the packet data. The integer
1213 * must have been initialized before this call.
1214 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215
1216void
Damien Millerd432ccf2002-01-22 23:14:44 +11001217packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218{
Damien Miller76e1e362002-01-22 23:15:57 +11001219 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220}
1221
Damien Miller33b13562000-04-04 14:38:59 +10001222void
Damien Millerd432ccf2002-01-22 23:14:44 +11001223packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001224{
Damien Miller76e1e362002-01-22 23:15:57 +11001225 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001226}
1227
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001228void *
Damien Miller33b13562000-04-04 14:38:59 +10001229packet_get_raw(int *length_ptr)
1230{
1231 int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001232
Damien Miller33b13562000-04-04 14:38:59 +10001233 if (length_ptr != NULL)
1234 *length_ptr = bytes;
1235 return buffer_ptr(&incoming_packet);
1236}
1237
Damien Miller4af51302000-04-16 11:18:38 +10001238int
1239packet_remaining(void)
1240{
1241 return buffer_len(&incoming_packet);
1242}
1243
Damien Miller5428f641999-11-25 11:54:57 +11001244/*
1245 * Returns a string from the packet data. The string is allocated using
1246 * xmalloc; it is the responsibility of the calling program to free it when
1247 * no longer needed. The length_ptr argument may be NULL, or point to an
1248 * integer into which the length of the string is stored.
1249 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001251void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001252packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253{
Damien Miller95def091999-11-25 00:26:21 +11001254 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255}
1256
Damien Miller5428f641999-11-25 11:54:57 +11001257/*
1258 * Sends a diagnostic message from the server to the client. This message
1259 * can be sent at any time (but not while constructing another message). The
1260 * message is printed immediately, but only if the client is being executed
1261 * in verbose mode. These messages are primarily intended to ease debugging
1262 * authentication problems. The length of the formatted message must not
1263 * exceed 1024 bytes. This will automatically call packet_write_wait.
1264 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265
1266void
Damien Miller95def091999-11-25 00:26:21 +11001267packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268{
Damien Miller95def091999-11-25 00:26:21 +11001269 char buf[1024];
1270 va_list args;
1271
Ben Lindstroma14ee472000-12-07 01:24:58 +00001272 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1273 return;
1274
Damien Miller95def091999-11-25 00:26:21 +11001275 va_start(args, fmt);
1276 vsnprintf(buf, sizeof(buf), fmt, args);
1277 va_end(args);
1278
Damien Miller7c8af4f2000-05-01 08:24:07 +10001279 if (compat20) {
1280 packet_start(SSH2_MSG_DEBUG);
1281 packet_put_char(0); /* bool: always display */
1282 packet_put_cstring(buf);
1283 packet_put_cstring("");
1284 } else {
1285 packet_start(SSH_MSG_DEBUG);
1286 packet_put_cstring(buf);
1287 }
Damien Miller95def091999-11-25 00:26:21 +11001288 packet_send();
1289 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001290}
1291
Damien Miller5428f641999-11-25 11:54:57 +11001292/*
1293 * Logs the error plus constructs and sends a disconnect packet, closes the
1294 * connection, and exits. This function never returns. The error message
1295 * should not contain a newline. The length of the formatted message must
1296 * not exceed 1024 bytes.
1297 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001298
1299void
Damien Miller95def091999-11-25 00:26:21 +11001300packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301{
Damien Miller95def091999-11-25 00:26:21 +11001302 char buf[1024];
1303 va_list args;
1304 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001305
Damien Miller95def091999-11-25 00:26:21 +11001306 if (disconnecting) /* Guard against recursive invocations. */
1307 fatal("packet_disconnect called recursively.");
1308 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309
Damien Miller5428f641999-11-25 11:54:57 +11001310 /*
1311 * Format the message. Note that the caller must make sure the
1312 * message is of limited size.
1313 */
Damien Miller95def091999-11-25 00:26:21 +11001314 va_start(args, fmt);
1315 vsnprintf(buf, sizeof(buf), fmt, args);
1316 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001317
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001318 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001319 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001320
Damien Miller95def091999-11-25 00:26:21 +11001321 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001322 if (compat20) {
1323 packet_start(SSH2_MSG_DISCONNECT);
1324 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1325 packet_put_cstring(buf);
1326 packet_put_cstring("");
1327 } else {
1328 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001329 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001330 }
Damien Miller95def091999-11-25 00:26:21 +11001331 packet_send();
1332 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333
Damien Miller95def091999-11-25 00:26:21 +11001334 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001335 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001336
Damien Miller95def091999-11-25 00:26:21 +11001337 /* Close the connection. */
1338 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001339 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340}
1341
Damien Miller5428f641999-11-25 11:54:57 +11001342/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343
1344void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001345packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346{
Damien Miller95def091999-11-25 00:26:21 +11001347 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001348
Damien Miller95def091999-11-25 00:26:21 +11001349 if (len > 0) {
1350 len = write(connection_out, buffer_ptr(&output), len);
1351 if (len <= 0) {
1352 if (errno == EAGAIN)
1353 return;
1354 else
1355 fatal("Write failed: %.100s", strerror(errno));
1356 }
1357 buffer_consume(&output, len);
1358 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359}
1360
Damien Miller5428f641999-11-25 11:54:57 +11001361/*
1362 * Calls packet_write_poll repeatedly until all pending output data has been
1363 * written.
1364 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365
1366void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001367packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001369 fd_set *setp;
1370
1371 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1372 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001373 packet_write_poll();
1374 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001375 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1376 sizeof(fd_mask));
1377 FD_SET(connection_out, setp);
1378 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001379 (errno == EAGAIN || errno == EINTR))
1380 ;
Damien Miller95def091999-11-25 00:26:21 +11001381 packet_write_poll();
1382 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001383 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384}
1385
1386/* Returns true if there is buffered data to write to the connection. */
1387
1388int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001389packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390{
Damien Miller95def091999-11-25 00:26:21 +11001391 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392}
1393
1394/* Returns true if there is not too much data to write to the connection. */
1395
1396int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001397packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001398{
Damien Miller95def091999-11-25 00:26:21 +11001399 if (interactive_mode)
1400 return buffer_len(&output) < 16384;
1401 else
1402 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403}
1404
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001405
1406#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001407static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001408packet_set_tos(int interactive)
1409{
1410 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1411
1412 if (!packet_connection_is_on_socket() ||
1413 !packet_connection_is_ipv4())
1414 return;
1415 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1416 sizeof(tos)) < 0)
1417 error("setsockopt IP_TOS %d: %.100s:",
1418 tos, strerror(errno));
1419}
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001420#endif
Ben Lindstroma7433982002-12-23 02:41:41 +00001421
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001422/* Informs that the current session is interactive. Sets IP flags for that. */
1423
1424void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001425packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001426{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001427 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001429 if (called)
1430 return;
1431 called = 1;
1432
Damien Miller95def091999-11-25 00:26:21 +11001433 /* Record that we are in interactive mode. */
1434 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435
Damien Miller34132e52000-01-14 15:45:46 +11001436 /* Only set socket options if using a socket. */
1437 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001438 return;
Ben Lindstroma7433982002-12-23 02:41:41 +00001439 if (interactive)
Damien Miller398e1cf2002-02-05 11:52:13 +11001440 set_nodelay(connection_in);
Damien Miller2ae714f2000-07-11 09:29:50 +10001441#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001442 packet_set_tos(interactive);
Damien Miller615f9392000-05-17 22:53:33 +10001443#endif
Ben Lindstroma7433982002-12-23 02:41:41 +00001444
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445}
1446
1447/* Returns true if the current connection is interactive. */
1448
1449int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001450packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451{
Damien Miller95def091999-11-25 00:26:21 +11001452 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453}
Damien Miller6162d121999-11-21 13:23:52 +11001454
Darren Tucker502d3842003-06-28 12:38:01 +10001455u_int
1456packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001457{
Damien Miller95def091999-11-25 00:26:21 +11001458 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001459
Damien Miller95def091999-11-25 00:26:21 +11001460 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001461 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001462 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001463 return -1;
1464 }
1465 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001466 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001467 return -1;
1468 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001469 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001470 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001471 max_packet_size = s;
1472 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001473}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001474
Damien Miller9f643902001-11-12 11:02:52 +11001475/* roundup current message to pad bytes */
1476void
1477packet_add_padding(u_char pad)
1478{
1479 extra_pad = pad;
1480}
1481
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001482/*
1483 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001484 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001485 * byte SSH_MSG_IGNORE
1486 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001487 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001488 * All implementations MUST understand (and ignore) this message at any
1489 * time (after receiving the protocol version). No implementation is
1490 * required to send them. This message can be used as an additional
1491 * protection measure against advanced traffic analysis techniques.
1492 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001493void
1494packet_send_ignore(int nbytes)
1495{
1496 u_int32_t rand = 0;
1497 int i;
1498
1499 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001500 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001501 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001502 if (i % 4 == 0)
1503 rand = arc4random();
1504 packet_put_char(rand & 0xff);
1505 rand >>= 8;
1506 }
1507}
Damien Millera5539d22003-04-09 20:50:06 +10001508
1509#define MAX_PACKETS (1<<31)
1510int
1511packet_need_rekeying(void)
1512{
1513 if (datafellows & SSH_BUG_NOREKEY)
1514 return 0;
1515 return
1516 (p_send.packets > MAX_PACKETS) ||
1517 (p_read.packets > MAX_PACKETS) ||
1518 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1519 (max_blocks_in && (p_read.blocks > max_blocks_in));
1520}
1521
1522void
1523packet_set_rekey_limit(u_int32_t bytes)
1524{
1525 rekey_limit = bytes;
1526}