blob: 62239eb7d6541c90e4eb4d4230a382e7252d5864 [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.
16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
17 *
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"
Damien Millere4340be2000-09-16 13:29:08 +110040RCSID("$OpenBSD: packet.c,v 1.35 2000/09/07 20:27:52 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "xmalloc.h"
43#include "buffer.h"
44#include "packet.h"
45#include "bufaux.h"
46#include "ssh.h"
47#include "crc32.h"
48#include "cipher.h"
49#include "getput.h"
50
51#include "compress.h"
52#include "deattack.h"
Damien Millerb38eff82000-04-01 11:09:21 +100053#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller33b13562000-04-04 14:38:59 +100055#include "compat.h"
56#include "ssh2.h"
57
Damien Miller5f056372000-04-16 12:31:48 +100058#include <openssl/bn.h>
59#include <openssl/dh.h>
60#include <openssl/hmac.h>
Damien Miller33b13562000-04-04 14:38:59 +100061#include "buffer.h"
62#include "kex.h"
63#include "hmac.h"
64
65#ifdef PACKET_DEBUG
66#define DBG(x) x
67#else
68#define DBG(x)
69#endif
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * This variable contains the file descriptors used for communicating with
73 * the other side. connection_in is used for reading; connection_out for
74 * writing. These can be the same descriptor, in which case it is assumed to
75 * be a socket.
76 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077static int connection_in = -1;
78static int connection_out = -1;
79
Damien Miller5428f641999-11-25 11:54:57 +110080/*
81 * Cipher type. This value is only used to determine whether to pad the
82 * packets with zeroes or random data.
83 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084static int cipher_type = SSH_CIPHER_NONE;
85
86/* Protocol flags for the remote side. */
87static unsigned int remote_protocol_flags = 0;
88
89/* Encryption context for receiving data. This is only used for decryption. */
90static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110091
92/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093static CipherContext send_context;
94
95/* Buffer for raw input data from the socket. */
96static Buffer input;
97
98/* Buffer for raw output data going to the socket. */
99static Buffer output;
100
101/* Buffer for the partial outgoing packet being constructed. */
102static Buffer outgoing_packet;
103
104/* Buffer for the incoming packet currently being processed. */
105static Buffer incoming_packet;
106
107/* Scratch buffer for packet compression/decompression. */
108static Buffer compression_buffer;
109
110/* Flag indicating whether packet compression/decompression is enabled. */
111static int packet_compression = 0;
112
Damien Miller6162d121999-11-21 13:23:52 +1100113/* default maximum packet size */
114int max_packet_size = 32768;
115
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116/* Flag indicating whether this module has been initialized. */
117static int initialized = 0;
118
119/* Set to true if the connection is interactive. */
120static int interactive_mode = 0;
121
Damien Miller33b13562000-04-04 14:38:59 +1000122/* True if SSH2 packet format is used */
123int use_ssh2_packet_format = 0;
124
125/* Session key information for Encryption and MAC */
126Kex *kex = NULL;
127
128void
129packet_set_kex(Kex *k)
130{
131 if( k->mac[MODE_IN ].key == NULL ||
132 k->enc[MODE_IN ].key == NULL ||
133 k->enc[MODE_IN ].iv == NULL ||
134 k->mac[MODE_OUT].key == NULL ||
135 k->enc[MODE_OUT].key == NULL ||
136 k->enc[MODE_OUT].iv == NULL)
137 fatal("bad KEX");
138 kex = k;
139}
140void
141clear_enc_keys(Enc *enc, int len)
142{
143 memset(enc->iv, 0, len);
144 memset(enc->key, 0, len);
145 xfree(enc->iv);
146 xfree(enc->key);
147 enc->iv = NULL;
148 enc->key = NULL;
149}
150void
151packet_set_ssh2_format(void)
152{
Damien Miller35dabd02000-05-01 21:10:33 +1000153 DBG(debug("use_ssh2_packet_format"));
Damien Miller33b13562000-04-04 14:38:59 +1000154 use_ssh2_packet_format = 1;
155}
156
Damien Miller5428f641999-11-25 11:54:57 +1100157/*
158 * Sets the descriptors used for communication. Disables encryption until
159 * packet_set_encryption_key is called.
160 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161void
162packet_set_connection(int fd_in, int fd_out)
163{
Damien Miller95def091999-11-25 00:26:21 +1100164 connection_in = fd_in;
165 connection_out = fd_out;
166 cipher_type = SSH_CIPHER_NONE;
Damien Miller1383bd82000-04-06 12:32:37 +1000167 cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *) "", 0);
168 cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *) "", 0);
Damien Miller95def091999-11-25 00:26:21 +1100169 if (!initialized) {
170 initialized = 1;
171 buffer_init(&input);
172 buffer_init(&output);
173 buffer_init(&outgoing_packet);
174 buffer_init(&incoming_packet);
175 }
176 /* Kludge: arrange the close function to be called from fatal(). */
177 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
Damien Miller34132e52000-01-14 15:45:46 +1100180/* Returns 1 if remote host is connected via socket, 0 if not. */
181
182int
183packet_connection_is_on_socket()
184{
185 struct sockaddr_storage from, to;
186 socklen_t fromlen, tolen;
187
188 /* filedescriptors in and out are the same, so it's a socket */
189 if (connection_in == connection_out)
190 return 1;
191 fromlen = sizeof(from);
192 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100193 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100194 return 0;
195 tolen = sizeof(to);
196 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100197 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100198 return 0;
199 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
200 return 0;
201 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
202 return 0;
203 return 1;
204}
205
206/* returns 1 if connection is via ipv4 */
207
208int
209packet_connection_is_ipv4()
210{
211 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100212 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100213
214 memset(&to, 0, sizeof(to));
215 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
216 return 0;
217 if (to.ss_family != AF_INET)
218 return 0;
219 return 1;
220}
221
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222/* Sets the connection into non-blocking mode. */
223
224void
225packet_set_nonblocking()
226{
Damien Miller95def091999-11-25 00:26:21 +1100227 /* Set the socket into non-blocking mode. */
228 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
229 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230
Damien Miller95def091999-11-25 00:26:21 +1100231 if (connection_out != connection_in) {
232 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
233 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
234 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235}
236
237/* Returns the socket used for reading. */
238
239int
240packet_get_connection_in()
241{
Damien Miller95def091999-11-25 00:26:21 +1100242 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243}
244
245/* Returns the descriptor used for writing. */
246
247int
248packet_get_connection_out()
249{
Damien Miller95def091999-11-25 00:26:21 +1100250 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251}
252
253/* Closes the connection and clears and frees internal data structures. */
254
255void
256packet_close()
257{
Damien Miller95def091999-11-25 00:26:21 +1100258 if (!initialized)
259 return;
260 initialized = 0;
261 if (connection_in == connection_out) {
262 shutdown(connection_out, SHUT_RDWR);
263 close(connection_out);
264 } else {
265 close(connection_in);
266 close(connection_out);
267 }
268 buffer_free(&input);
269 buffer_free(&output);
270 buffer_free(&outgoing_packet);
271 buffer_free(&incoming_packet);
272 if (packet_compression) {
273 buffer_free(&compression_buffer);
274 buffer_compress_uninit();
275 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
277
278/* Sets remote side protocol flags. */
279
280void
281packet_set_protocol_flags(unsigned int protocol_flags)
282{
Damien Miller95def091999-11-25 00:26:21 +1100283 remote_protocol_flags = protocol_flags;
284 channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285}
286
287/* Returns the remote protocol flags set earlier by the above function. */
288
289unsigned int
290packet_get_protocol_flags()
291{
Damien Miller95def091999-11-25 00:26:21 +1100292 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293}
294
Damien Miller5428f641999-11-25 11:54:57 +1100295/*
296 * Starts packet compression from the next packet on in both directions.
297 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
298 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299
Damien Miller33b13562000-04-04 14:38:59 +1000300/*** XXXXX todo: kex means re-init */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301void
302packet_start_compression(int level)
303{
Damien Miller95def091999-11-25 00:26:21 +1100304 if (packet_compression)
305 fatal("Compression already enabled.");
306 packet_compression = 1;
307 buffer_init(&compression_buffer);
308 buffer_compress_init(level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309}
310
Damien Miller5428f641999-11-25 11:54:57 +1100311/*
312 * Encrypts the given number of bytes, copying from src to dest. bytes is
313 * known to be a multiple of 8.
314 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315
316void
Damien Miller95def091999-11-25 00:26:21 +1100317packet_encrypt(CipherContext * cc, void *dest, void *src,
Damien Miller33b13562000-04-04 14:38:59 +1000318 unsigned int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319{
Damien Miller95def091999-11-25 00:26:21 +1100320 cipher_encrypt(cc, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321}
322
Damien Miller5428f641999-11-25 11:54:57 +1100323/*
324 * Decrypts the given number of bytes, copying from src to dest. bytes is
325 * known to be a multiple of 8.
326 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
328void
Damien Miller95def091999-11-25 00:26:21 +1100329packet_decrypt(CipherContext * cc, void *dest, void *src,
Damien Miller33b13562000-04-04 14:38:59 +1000330 unsigned int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331{
Damien Miller95def091999-11-25 00:26:21 +1100332 int i;
333
334 if ((bytes % 8) != 0)
335 fatal("packet_decrypt: bad ciphertext length %d", bytes);
336
Damien Miller5428f641999-11-25 11:54:57 +1100337 /*
338 * Cryptographic attack detector for ssh - Modifications for packet.c
339 * (C)1998 CORE-SDI, Buenos Aires Argentina Ariel Futoransky(futo@core-sdi.com)
340 */
Damien Miller95def091999-11-25 00:26:21 +1100341
Damien Miller33b13562000-04-04 14:38:59 +1000342 if (cc->type == SSH_CIPHER_NONE || compat20) {
Damien Miller95def091999-11-25 00:26:21 +1100343 i = DEATTACK_OK;
Damien Miller33b13562000-04-04 14:38:59 +1000344 } else {
Damien Miller95def091999-11-25 00:26:21 +1100345 i = detect_attack(src, bytes, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100346 }
Damien Miller95def091999-11-25 00:26:21 +1100347 if (i == DEATTACK_DETECTED)
348 packet_disconnect("crc32 compensation attack: network attack detected");
349
350 cipher_decrypt(cc, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351}
352
Damien Miller5428f641999-11-25 11:54:57 +1100353/*
354 * Causes any further packets to be encrypted using the given key. The same
355 * key is used for both sending and reception. However, both directions are
356 * encrypted independently of each other.
357 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358
359void
360packet_set_encryption_key(const unsigned char *key, unsigned int keylen,
Damien Miller33b13562000-04-04 14:38:59 +1000361 int cipher)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362{
Damien Miller33b13562000-04-04 14:38:59 +1000363 if (keylen < 20)
364 fatal("keylen too small: %d", keylen);
365
Damien Miller95def091999-11-25 00:26:21 +1100366 /* All other ciphers use the same key in both directions for now. */
Damien Miller1383bd82000-04-06 12:32:37 +1000367 cipher_set_key(&receive_context, cipher, key, keylen);
368 cipher_set_key(&send_context, cipher, key, keylen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369}
370
371/* Starts constructing a packet to send. */
372
373void
Damien Miller33b13562000-04-04 14:38:59 +1000374packet_start1(int type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375{
Damien Miller95def091999-11-25 00:26:21 +1100376 char buf[9];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377
Damien Miller95def091999-11-25 00:26:21 +1100378 buffer_clear(&outgoing_packet);
379 memset(buf, 0, 8);
380 buf[8] = type;
381 buffer_append(&outgoing_packet, buf, 9);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382}
383
Damien Miller33b13562000-04-04 14:38:59 +1000384void
385packet_start2(int type)
386{
387 char buf[4+1+1];
388
389 buffer_clear(&outgoing_packet);
390 memset(buf, 0, sizeof buf);
391 /* buf[0..3] = payload_len; */
392 /* buf[4] = pad_len; */
393 buf[5] = type & 0xff;
394 buffer_append(&outgoing_packet, buf, sizeof buf);
395}
396
397void
398packet_start(int type)
399{
400 DBG(debug("packet_start[%d]",type));
401 if (use_ssh2_packet_format)
402 packet_start2(type);
403 else
404 packet_start1(type);
405}
406
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407/* Appends a character to the packet data. */
408
409void
410packet_put_char(int value)
411{
Damien Miller95def091999-11-25 00:26:21 +1100412 char ch = value;
413 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414}
415
416/* Appends an integer to the packet data. */
417
418void
419packet_put_int(unsigned int value)
420{
Damien Miller95def091999-11-25 00:26:21 +1100421 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422}
423
424/* Appends a string to packet data. */
425
426void
427packet_put_string(const char *buf, unsigned int len)
428{
Damien Miller95def091999-11-25 00:26:21 +1100429 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430}
Damien Miller33b13562000-04-04 14:38:59 +1000431void
432packet_put_cstring(const char *str)
433{
434 buffer_put_string(&outgoing_packet, str, strlen(str));
435}
436
437void
438packet_put_raw(const char *buf, unsigned int len)
439{
440 buffer_append(&outgoing_packet, buf, len);
441}
442
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443
444/* Appends an arbitrary precision integer to packet data. */
445
446void
Damien Miller95def091999-11-25 00:26:21 +1100447packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448{
Damien Miller95def091999-11-25 00:26:21 +1100449 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450}
Damien Miller33b13562000-04-04 14:38:59 +1000451void
452packet_put_bignum2(BIGNUM * value)
453{
454 buffer_put_bignum2(&outgoing_packet, value);
455}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456
Damien Miller5428f641999-11-25 11:54:57 +1100457/*
458 * Finalizes and sends the packet. If the encryption key has been set,
459 * encrypts the packet before sending.
460 */
Damien Miller95def091999-11-25 00:26:21 +1100461
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462void
Damien Miller33b13562000-04-04 14:38:59 +1000463packet_send1()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464{
Damien Miller95def091999-11-25 00:26:21 +1100465 char buf[8], *cp;
466 int i, padding, len;
467 unsigned int checksum;
468 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469
Damien Miller5428f641999-11-25 11:54:57 +1100470 /*
471 * If using packet compression, compress the payload of the outgoing
472 * packet.
473 */
Damien Miller95def091999-11-25 00:26:21 +1100474 if (packet_compression) {
475 buffer_clear(&compression_buffer);
476 /* Skip padding. */
477 buffer_consume(&outgoing_packet, 8);
478 /* padding */
479 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
480 buffer_compress(&outgoing_packet, &compression_buffer);
481 buffer_clear(&outgoing_packet);
482 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
483 buffer_len(&compression_buffer));
484 }
485 /* Compute packet length without padding (add checksum, remove padding). */
486 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487
Damien Millere247cc42000-05-07 12:03:14 +1000488 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100489 padding = 8 - len % 8;
490 if (cipher_type != SSH_CIPHER_NONE) {
491 cp = buffer_ptr(&outgoing_packet);
492 for (i = 0; i < padding; i++) {
493 if (i % 4 == 0)
494 rand = arc4random();
495 cp[7 - i] = rand & 0xff;
496 rand >>= 8;
497 }
498 }
499 buffer_consume(&outgoing_packet, 8 - padding);
500
501 /* Add check bytes. */
Damien Millerad833b32000-08-23 10:46:23 +1000502 checksum = ssh_crc32((unsigned char *) buffer_ptr(&outgoing_packet),
503 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100504 PUT_32BIT(buf, checksum);
505 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506
507#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100508 fprintf(stderr, "packet_send plain: ");
509 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510#endif
511
Damien Miller95def091999-11-25 00:26:21 +1100512 /* Append to output. */
513 PUT_32BIT(buf, len);
514 buffer_append(&output, buf, 4);
515 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
516 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
517 buffer_len(&outgoing_packet));
518
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100520 fprintf(stderr, "encrypted: ");
521 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522#endif
523
Damien Miller95def091999-11-25 00:26:21 +1100524 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Damien Miller5428f641999-11-25 11:54:57 +1100526 /*
527 * Note that the packet is now only buffered in output. It won\'t be
528 * actually sent until packet_write_wait or packet_write_poll is
529 * called.
530 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531}
532
Damien Miller5428f641999-11-25 11:54:57 +1100533/*
Damien Miller33b13562000-04-04 14:38:59 +1000534 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
535 */
536void
537packet_send2()
538{
539 unsigned char *macbuf = NULL;
540 char *cp;
541 unsigned int packet_length = 0;
542 unsigned int i, padlen, len;
543 u_int32_t rand = 0;
Damien Miller4af51302000-04-16 11:18:38 +1000544 static unsigned int seqnr = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000545 int type;
546 Enc *enc = NULL;
547 Mac *mac = NULL;
548 Comp *comp = NULL;
549 int block_size;
550
551 if (kex != NULL) {
552 enc = &kex->enc[MODE_OUT];
553 mac = &kex->mac[MODE_OUT];
554 comp = &kex->comp[MODE_OUT];
555 }
556 block_size = enc ? enc->block_size : 8;
557
558 cp = buffer_ptr(&outgoing_packet);
559 type = cp[5] & 0xff;
560
561#ifdef PACKET_DEBUG
562 fprintf(stderr, "plain: ");
563 buffer_dump(&outgoing_packet);
564#endif
565
566 if (comp && comp->enabled) {
567 len = buffer_len(&outgoing_packet);
568 /* skip header, compress only payload */
569 buffer_consume(&outgoing_packet, 5);
570 buffer_clear(&compression_buffer);
571 buffer_compress(&outgoing_packet, &compression_buffer);
572 buffer_clear(&outgoing_packet);
573 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
574 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
575 buffer_len(&compression_buffer));
576 DBG(debug("compression: raw %d compressed %d", len,
577 buffer_len(&outgoing_packet)));
578 }
579
580 /* sizeof (packet_len + pad_len + payload) */
581 len = buffer_len(&outgoing_packet);
582
583 /*
584 * calc size of padding, alloc space, get random data,
585 * minimum padding is 4 bytes
586 */
587 padlen = block_size - (len % block_size);
588 if (padlen < 4)
589 padlen += block_size;
590 buffer_append_space(&outgoing_packet, &cp, padlen);
591 if (enc && enc->type != SSH_CIPHER_NONE) {
Damien Millere247cc42000-05-07 12:03:14 +1000592 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000593 for (i = 0; i < padlen; i++) {
594 if (i % 4 == 0)
595 rand = arc4random();
596 cp[i] = rand & 0xff;
597 rand <<= 8;
598 }
Damien Millere247cc42000-05-07 12:03:14 +1000599 } else {
600 /* clear padding */
601 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000602 }
603 /* packet_length includes payload, padding and padding length field */
604 packet_length = buffer_len(&outgoing_packet) - 4;
605 cp = buffer_ptr(&outgoing_packet);
606 PUT_32BIT(cp, packet_length);
607 cp[4] = padlen & 0xff;
608 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
609
610 /* compute MAC over seqnr and packet(length fields, payload, padding) */
611 if (mac && mac->enabled) {
612 macbuf = hmac( mac->md, seqnr,
613 (unsigned char *) buffer_ptr(&outgoing_packet),
614 buffer_len(&outgoing_packet),
615 mac->key, mac->key_len
616 );
617 DBG(debug("done calc HMAC out #%d", seqnr));
618 }
619 /* encrypt packet and append to output buffer. */
620 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
621 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
622 buffer_len(&outgoing_packet));
623 /* append unencrypted MAC */
624 if (mac && mac->enabled)
625 buffer_append(&output, (char *)macbuf, mac->mac_len);
626#ifdef PACKET_DEBUG
627 fprintf(stderr, "encrypted: ");
628 buffer_dump(&output);
629#endif
Damien Miller4af51302000-04-16 11:18:38 +1000630 /* increment sequence number for outgoing packets */
631 if (++seqnr == 0)
632 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000633 buffer_clear(&outgoing_packet);
634
635 if (type == SSH2_MSG_NEWKEYS) {
636 if (kex==NULL || mac==NULL || enc==NULL || comp==NULL)
637 fatal("packet_send2: no KEX");
638 if (mac->md != NULL)
639 mac->enabled = 1;
Damien Miller35dabd02000-05-01 21:10:33 +1000640 DBG(debug("cipher_set_key_iv send_context"));
Damien Miller33b13562000-04-04 14:38:59 +1000641 cipher_set_key_iv(&send_context, enc->type,
642 enc->key, enc->key_len,
643 enc->iv, enc->iv_len);
644 clear_enc_keys(enc, kex->we_need);
645 if (comp->type != 0 && comp->enabled == 0) {
646 comp->enabled = 1;
647 if (! packet_compression)
648 packet_start_compression(6);
649 }
650 }
651}
652
653void
654packet_send()
655{
656 if (use_ssh2_packet_format)
657 packet_send2();
658 else
659 packet_send1();
660 DBG(debug("packet_send done"));
661}
662
Damien Miller33b13562000-04-04 14:38:59 +1000663/*
Damien Miller5428f641999-11-25 11:54:57 +1100664 * Waits until a packet has been received, and returns its type. Note that
665 * no other data is processed until this returns, so this function should not
666 * be used during the interactive session.
667 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668
669int
670packet_read(int *payload_len_ptr)
671{
Damien Miller95def091999-11-25 00:26:21 +1100672 int type, len;
673 fd_set set;
674 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000675 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676
Damien Miller95def091999-11-25 00:26:21 +1100677 /* Since we are blocking, ensure that all written packets have been sent. */
678 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679
Damien Miller95def091999-11-25 00:26:21 +1100680 /* Stay in the loop until we have received a complete packet. */
681 for (;;) {
682 /* Try to read a packet from the buffer. */
683 type = packet_read_poll(payload_len_ptr);
Damien Millere247cc42000-05-07 12:03:14 +1000684 if (!use_ssh2_packet_format && (
685 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100686 || type == SSH_SMSG_FAILURE
687 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000688 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller95def091999-11-25 00:26:21 +1100689 packet_integrity_check(*payload_len_ptr, 0, type);
690 /* If we got a packet, return it. */
691 if (type != SSH_MSG_NONE)
692 return type;
Damien Miller5428f641999-11-25 11:54:57 +1100693 /*
694 * Otherwise, wait for some data to arrive, add it to the
695 * buffer, and try again.
696 */
Damien Miller95def091999-11-25 00:26:21 +1100697 FD_ZERO(&set);
698 FD_SET(connection_in, &set);
Damien Miller5428f641999-11-25 11:54:57 +1100699
Damien Miller95def091999-11-25 00:26:21 +1100700 /* Wait for some data to arrive. */
701 select(connection_in + 1, &set, NULL, NULL, NULL);
Damien Miller5428f641999-11-25 11:54:57 +1100702
Damien Miller95def091999-11-25 00:26:21 +1100703 /* Read data from the socket. */
704 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100705 if (len == 0) {
706 log("Connection closed by %.200s", get_remote_ipaddr());
707 fatal_cleanup();
708 }
Damien Miller95def091999-11-25 00:26:21 +1100709 if (len < 0)
710 fatal("Read from socket failed: %.100s", strerror(errno));
711 /* Append it to the buffer. */
712 packet_process_incoming(buf, len);
713 }
714 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715}
716
Damien Miller5428f641999-11-25 11:54:57 +1100717/*
718 * Waits until a packet has been received, verifies that its type matches
719 * that given, and gives a fatal error and exits if there is a mismatch.
720 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721
722void
723packet_read_expect(int *payload_len_ptr, int expected_type)
724{
Damien Miller95def091999-11-25 00:26:21 +1100725 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726
Damien Miller95def091999-11-25 00:26:21 +1100727 type = packet_read(payload_len_ptr);
728 if (type != expected_type)
729 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000730 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731}
732
733/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100734 * packet_process_incoming. If so, reads the packet; otherwise returns
735 * SSH_MSG_NONE. This does not wait for data from the connection.
736 *
737 * SSH_MSG_DISCONNECT is handled specially here. Also,
738 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
739 * to higher levels.
740 *
741 * The returned payload_len does include space consumed by:
742 * Packet length
743 * Padding
744 * Packet type
745 * Check bytes
746 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747
748int
Damien Miller33b13562000-04-04 14:38:59 +1000749packet_read_poll1(int *payload_len_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750{
Damien Miller95def091999-11-25 00:26:21 +1100751 unsigned int len, padded_len;
752 unsigned char *ucp;
Damien Miller33b13562000-04-04 14:38:59 +1000753 char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100754 unsigned int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755
Damien Miller95def091999-11-25 00:26:21 +1100756 /* Check if input size is less than minimum packet size. */
757 if (buffer_len(&input) < 4 + 8)
758 return SSH_MSG_NONE;
759 /* Get length of incoming packet. */
760 ucp = (unsigned char *) buffer_ptr(&input);
761 len = GET_32BIT(ucp);
762 if (len < 1 + 2 + 2 || len > 256 * 1024)
763 packet_disconnect("Bad packet length %d.", len);
764 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765
Damien Miller95def091999-11-25 00:26:21 +1100766 /* Check if the packet has been entirely received. */
767 if (buffer_len(&input) < 4 + padded_len)
768 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769
Damien Miller95def091999-11-25 00:26:21 +1100770 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771
Damien Miller95def091999-11-25 00:26:21 +1100772 /* Consume packet length. */
773 buffer_consume(&input, 4);
774
775 /* Copy data to incoming_packet. */
776 buffer_clear(&incoming_packet);
777 buffer_append_space(&incoming_packet, &cp, padded_len);
778 packet_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
779 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780
781#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100782 fprintf(stderr, "read_poll plain: ");
783 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Damien Miller95def091999-11-25 00:26:21 +1100786 /* Compute packet checksum. */
Damien Millerad833b32000-08-23 10:46:23 +1000787 checksum = ssh_crc32((unsigned char *) buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000788 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789
Damien Miller95def091999-11-25 00:26:21 +1100790 /* Skip padding. */
791 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100792
Damien Miller95def091999-11-25 00:26:21 +1100793 /* Test check bytes. */
Damien Millerfd7c9111999-11-08 16:15:55 +1100794
Damien Miller95def091999-11-25 00:26:21 +1100795 if (len != buffer_len(&incoming_packet))
796 packet_disconnect("packet_read_poll: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000797 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798
Damien Miller95def091999-11-25 00:26:21 +1100799 ucp = (unsigned char *) buffer_ptr(&incoming_packet) + len - 4;
800 stored_checksum = GET_32BIT(ucp);
801 if (checksum != stored_checksum)
802 packet_disconnect("Corrupted check bytes on input.");
803 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804
Damien Miller95def091999-11-25 00:26:21 +1100805 /* If using packet compression, decompress the packet. */
806 if (packet_compression) {
807 buffer_clear(&compression_buffer);
808 buffer_uncompress(&incoming_packet, &compression_buffer);
809 buffer_clear(&incoming_packet);
810 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000811 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100812 }
813 /* Get packet type. */
814 buffer_get(&incoming_packet, &buf[0], 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815
Damien Miller95def091999-11-25 00:26:21 +1100816 /* Return length of payload (without type field). */
817 *payload_len_ptr = buffer_len(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818
Damien Miller95def091999-11-25 00:26:21 +1100819 /* Return type. */
820 return (unsigned char) buf[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821}
Damien Miller95def091999-11-25 00:26:21 +1100822
Damien Miller33b13562000-04-04 14:38:59 +1000823int
824packet_read_poll2(int *payload_len_ptr)
825{
826 unsigned int padlen, need;
827 unsigned char buf[8], *macbuf;
828 unsigned char *ucp;
829 char *cp;
830 static unsigned int packet_length = 0;
831 static unsigned int seqnr = 0;
832 int type;
833 int maclen, block_size;
834 Enc *enc = NULL;
835 Mac *mac = NULL;
836 Comp *comp = NULL;
837
838 if (kex != NULL) {
839 enc = &kex->enc[MODE_IN];
840 mac = &kex->mac[MODE_IN];
841 comp = &kex->comp[MODE_IN];
842 }
843 maclen = mac && mac->enabled ? mac->mac_len : 0;
844 block_size = enc ? enc->block_size : 8;
845
846 if (packet_length == 0) {
847 /*
848 * check if input size is less than the cipher block size,
849 * decrypt first block and extract length of incoming packet
850 */
851 if (buffer_len(&input) < block_size)
852 return SSH_MSG_NONE;
853 buffer_clear(&incoming_packet);
854 buffer_append_space(&incoming_packet, &cp, block_size);
855 packet_decrypt(&receive_context, cp, buffer_ptr(&input),
856 block_size);
857 ucp = (unsigned char *) buffer_ptr(&incoming_packet);
858 packet_length = GET_32BIT(ucp);
859 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
860 buffer_dump(&incoming_packet);
861 packet_disconnect("Bad packet length %d.", packet_length);
862 }
863 DBG(debug("input: packet len %d", packet_length+4));
864 buffer_consume(&input, block_size);
865 }
866 /* we have a partial packet of block_size bytes */
867 need = 4 + packet_length - block_size;
868 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
869 need, maclen));
870 if (need % block_size != 0)
871 fatal("padding error: need %d block %d mod %d",
872 need, block_size, need % block_size);
873 /*
874 * check if the entire packet has been received and
875 * decrypt into incoming_packet
876 */
877 if (buffer_len(&input) < need + maclen)
878 return SSH_MSG_NONE;
879#ifdef PACKET_DEBUG
880 fprintf(stderr, "read_poll enc/full: ");
881 buffer_dump(&input);
882#endif
883 buffer_append_space(&incoming_packet, &cp, need);
884 packet_decrypt(&receive_context, cp, buffer_ptr(&input), need);
885 buffer_consume(&input, need);
886 /*
887 * compute MAC over seqnr and packet,
888 * increment sequence number for incoming packet
889 */
Damien Miller4af51302000-04-16 11:18:38 +1000890 if (mac && mac->enabled) {
Damien Miller33b13562000-04-04 14:38:59 +1000891 macbuf = hmac( mac->md, seqnr,
892 (unsigned char *) buffer_ptr(&incoming_packet),
893 buffer_len(&incoming_packet),
894 mac->key, mac->key_len
895 );
896 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
897 packet_disconnect("Corrupted HMAC on input.");
898 DBG(debug("HMAC #%d ok", seqnr));
899 buffer_consume(&input, mac->mac_len);
900 }
Damien Miller4af51302000-04-16 11:18:38 +1000901 if (++seqnr == 0)
902 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000903
904 /* get padlen */
905 cp = buffer_ptr(&incoming_packet) + 4;
906 padlen = *cp & 0xff;
907 DBG(debug("input: padlen %d", padlen));
908 if (padlen < 4)
909 packet_disconnect("Corrupted padlen %d on input.", padlen);
910
911 /* skip packet size + padlen, discard padding */
912 buffer_consume(&incoming_packet, 4 + 1);
913 buffer_consume_end(&incoming_packet, padlen);
914
915 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
916 if (comp && comp->enabled) {
917 buffer_clear(&compression_buffer);
918 buffer_uncompress(&incoming_packet, &compression_buffer);
919 buffer_clear(&incoming_packet);
920 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
921 buffer_len(&compression_buffer));
922 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
923 }
924 /*
925 * get packet type, implies consume.
926 * return length of payload (without type field)
927 */
928 buffer_get(&incoming_packet, (char *)&buf[0], 1);
929 *payload_len_ptr = buffer_len(&incoming_packet);
930
931 /* reset for next packet */
932 packet_length = 0;
933
934 /* extract packet type */
935 type = (unsigned char)buf[0];
936
937 if (type == SSH2_MSG_NEWKEYS) {
938 if (kex==NULL || mac==NULL || enc==NULL || comp==NULL)
939 fatal("packet_read_poll2: no KEX");
940 if (mac->md != NULL)
941 mac->enabled = 1;
Damien Miller35dabd02000-05-01 21:10:33 +1000942 DBG(debug("cipher_set_key_iv receive_context"));
Damien Miller33b13562000-04-04 14:38:59 +1000943 cipher_set_key_iv(&receive_context, enc->type,
944 enc->key, enc->key_len,
945 enc->iv, enc->iv_len);
946 clear_enc_keys(enc, kex->we_need);
947 if (comp->type != 0 && comp->enabled == 0) {
948 comp->enabled = 1;
949 if (! packet_compression)
950 packet_start_compression(6);
951 }
952 }
953
954#ifdef PACKET_DEBUG
955 fprintf(stderr, "read/plain[%d]:\r\n",type);
956 buffer_dump(&incoming_packet);
957#endif
958 return (unsigned char)type;
959}
960
961int
962packet_read_poll(int *payload_len_ptr)
963{
964 char *msg;
965 for (;;) {
966 int type = use_ssh2_packet_format ?
967 packet_read_poll2(payload_len_ptr):
968 packet_read_poll1(payload_len_ptr);
969
970 if(compat20) {
971 int reason;
972 if (type != 0)
973 DBG(debug("received packet type %d", type));
974 switch(type) {
975 case SSH2_MSG_IGNORE:
976 break;
977 case SSH2_MSG_DEBUG:
978 packet_get_char();
979 msg = packet_get_string(NULL);
980 debug("Remote: %.900s", msg);
981 xfree(msg);
982 msg = packet_get_string(NULL);
983 xfree(msg);
984 break;
985 case SSH2_MSG_DISCONNECT:
986 reason = packet_get_int();
987 msg = packet_get_string(NULL);
988 log("Received disconnect: %d: %.900s", reason, msg);
989 xfree(msg);
990 fatal_cleanup();
991 break;
992 default:
993 return type;
994 break;
995 }
996 } else {
997 switch(type) {
998 case SSH_MSG_IGNORE:
999 break;
1000 case SSH_MSG_DEBUG:
1001 msg = packet_get_string(NULL);
1002 debug("Remote: %.900s", msg);
1003 xfree(msg);
1004 break;
1005 case SSH_MSG_DISCONNECT:
1006 msg = packet_get_string(NULL);
1007 log("Received disconnect: %.900s", msg);
1008 fatal_cleanup();
1009 xfree(msg);
1010 break;
1011 default:
1012 if (type != 0)
1013 DBG(debug("received packet type %d", type));
1014 return type;
1015 break;
1016 }
1017 }
1018 }
1019}
1020
Damien Miller5428f641999-11-25 11:54:57 +11001021/*
1022 * Buffers the given amount of input characters. This is intended to be used
1023 * together with packet_read_poll.
1024 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001025
1026void
1027packet_process_incoming(const char *buf, unsigned int len)
1028{
Damien Miller95def091999-11-25 00:26:21 +11001029 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030}
1031
1032/* Returns a character from the packet. */
1033
1034unsigned int
1035packet_get_char()
1036{
Damien Miller95def091999-11-25 00:26:21 +11001037 char ch;
1038 buffer_get(&incoming_packet, &ch, 1);
1039 return (unsigned char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040}
1041
1042/* Returns an integer from the packet data. */
1043
1044unsigned int
1045packet_get_int()
1046{
Damien Miller95def091999-11-25 00:26:21 +11001047 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048}
1049
Damien Miller5428f641999-11-25 11:54:57 +11001050/*
1051 * Returns an arbitrary precision integer from the packet data. The integer
1052 * must have been initialized before this call.
1053 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001054
1055void
Damien Miller95def091999-11-25 00:26:21 +11001056packet_get_bignum(BIGNUM * value, int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057{
Damien Miller95def091999-11-25 00:26:21 +11001058 *length_ptr = buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059}
1060
Damien Miller33b13562000-04-04 14:38:59 +10001061void
1062packet_get_bignum2(BIGNUM * value, int *length_ptr)
1063{
1064 *length_ptr = buffer_get_bignum2(&incoming_packet, value);
1065}
1066
1067char *
1068packet_get_raw(int *length_ptr)
1069{
1070 int bytes = buffer_len(&incoming_packet);
1071 if (length_ptr != NULL)
1072 *length_ptr = bytes;
1073 return buffer_ptr(&incoming_packet);
1074}
1075
Damien Miller4af51302000-04-16 11:18:38 +10001076int
1077packet_remaining(void)
1078{
1079 return buffer_len(&incoming_packet);
1080}
1081
Damien Miller5428f641999-11-25 11:54:57 +11001082/*
1083 * Returns a string from the packet data. The string is allocated using
1084 * xmalloc; it is the responsibility of the calling program to free it when
1085 * no longer needed. The length_ptr argument may be NULL, or point to an
1086 * integer into which the length of the string is stored.
1087 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001088
Damien Miller5428f641999-11-25 11:54:57 +11001089char *
Damien Miller95def091999-11-25 00:26:21 +11001090packet_get_string(unsigned int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001091{
Damien Miller95def091999-11-25 00:26:21 +11001092 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001093}
1094
Damien Miller5428f641999-11-25 11:54:57 +11001095/*
1096 * Sends a diagnostic message from the server to the client. This message
1097 * can be sent at any time (but not while constructing another message). The
1098 * message is printed immediately, but only if the client is being executed
1099 * in verbose mode. These messages are primarily intended to ease debugging
1100 * authentication problems. The length of the formatted message must not
1101 * exceed 1024 bytes. This will automatically call packet_write_wait.
1102 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001103
1104void
Damien Miller95def091999-11-25 00:26:21 +11001105packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106{
Damien Miller95def091999-11-25 00:26:21 +11001107 char buf[1024];
1108 va_list args;
1109
1110 va_start(args, fmt);
1111 vsnprintf(buf, sizeof(buf), fmt, args);
1112 va_end(args);
1113
Damien Miller7c8af4f2000-05-01 08:24:07 +10001114 if (compat20) {
1115 packet_start(SSH2_MSG_DEBUG);
1116 packet_put_char(0); /* bool: always display */
1117 packet_put_cstring(buf);
1118 packet_put_cstring("");
1119 } else {
1120 packet_start(SSH_MSG_DEBUG);
1121 packet_put_cstring(buf);
1122 }
Damien Miller95def091999-11-25 00:26:21 +11001123 packet_send();
1124 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001125}
1126
Damien Miller5428f641999-11-25 11:54:57 +11001127/*
1128 * Logs the error plus constructs and sends a disconnect packet, closes the
1129 * connection, and exits. This function never returns. The error message
1130 * should not contain a newline. The length of the formatted message must
1131 * not exceed 1024 bytes.
1132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133
1134void
Damien Miller95def091999-11-25 00:26:21 +11001135packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136{
Damien Miller95def091999-11-25 00:26:21 +11001137 char buf[1024];
1138 va_list args;
1139 static int disconnecting = 0;
1140 if (disconnecting) /* Guard against recursive invocations. */
1141 fatal("packet_disconnect called recursively.");
1142 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143
Damien Miller5428f641999-11-25 11:54:57 +11001144 /*
1145 * Format the message. Note that the caller must make sure the
1146 * message is of limited size.
1147 */
Damien Miller95def091999-11-25 00:26:21 +11001148 va_start(args, fmt);
1149 vsnprintf(buf, sizeof(buf), fmt, args);
1150 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001151
Damien Miller95def091999-11-25 00:26:21 +11001152 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001153 if (compat20) {
1154 packet_start(SSH2_MSG_DISCONNECT);
1155 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1156 packet_put_cstring(buf);
1157 packet_put_cstring("");
1158 } else {
1159 packet_start(SSH_MSG_DISCONNECT);
1160 packet_put_string(buf, strlen(buf));
1161 }
Damien Miller95def091999-11-25 00:26:21 +11001162 packet_send();
1163 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164
Damien Miller95def091999-11-25 00:26:21 +11001165 /* Stop listening for connections. */
1166 channel_stop_listening();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167
Damien Miller95def091999-11-25 00:26:21 +11001168 /* Close the connection. */
1169 packet_close();
1170
1171 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001172 log("Disconnecting: %.100s", buf);
1173 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174}
1175
Damien Miller5428f641999-11-25 11:54:57 +11001176/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001177
1178void
1179packet_write_poll()
1180{
Damien Miller95def091999-11-25 00:26:21 +11001181 int len = buffer_len(&output);
1182 if (len > 0) {
1183 len = write(connection_out, buffer_ptr(&output), len);
1184 if (len <= 0) {
1185 if (errno == EAGAIN)
1186 return;
1187 else
1188 fatal("Write failed: %.100s", strerror(errno));
1189 }
1190 buffer_consume(&output, len);
1191 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192}
1193
Damien Miller5428f641999-11-25 11:54:57 +11001194/*
1195 * Calls packet_write_poll repeatedly until all pending output data has been
1196 * written.
1197 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198
1199void
1200packet_write_wait()
1201{
Damien Miller95def091999-11-25 00:26:21 +11001202 packet_write_poll();
1203 while (packet_have_data_to_write()) {
1204 fd_set set;
1205 FD_ZERO(&set);
1206 FD_SET(connection_out, &set);
1207 select(connection_out + 1, NULL, &set, NULL, NULL);
1208 packet_write_poll();
1209 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210}
1211
1212/* Returns true if there is buffered data to write to the connection. */
1213
1214int
1215packet_have_data_to_write()
1216{
Damien Miller95def091999-11-25 00:26:21 +11001217 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218}
1219
1220/* Returns true if there is not too much data to write to the connection. */
1221
1222int
1223packet_not_very_much_data_to_write()
1224{
Damien Miller95def091999-11-25 00:26:21 +11001225 if (interactive_mode)
1226 return buffer_len(&output) < 16384;
1227 else
1228 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001229}
1230
1231/* Informs that the current session is interactive. Sets IP flags for that. */
1232
1233void
1234packet_set_interactive(int interactive, int keepalives)
1235{
Damien Miller95def091999-11-25 00:26:21 +11001236 int on = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237
Damien Miller95def091999-11-25 00:26:21 +11001238 /* Record that we are in interactive mode. */
1239 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240
Damien Miller34132e52000-01-14 15:45:46 +11001241 /* Only set socket options if using a socket. */
1242 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001243 return;
Damien Miller95def091999-11-25 00:26:21 +11001244 if (keepalives) {
1245 /* Set keepalives if requested. */
1246 if (setsockopt(connection_in, SOL_SOCKET, SO_KEEPALIVE, (void *) &on,
Damien Miller34132e52000-01-14 15:45:46 +11001247 sizeof(on)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001248 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1249 }
Damien Miller34132e52000-01-14 15:45:46 +11001250 /*
1251 * IPTOS_LOWDELAY, TCP_NODELAY and IPTOS_THROUGHPUT are IPv4 only
1252 */
1253 if (!packet_connection_is_ipv4())
1254 return;
Damien Miller95def091999-11-25 00:26:21 +11001255 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001256 /*
1257 * Set IP options for an interactive connection. Use
1258 * IPTOS_LOWDELAY and TCP_NODELAY.
1259 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001260#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Damien Miller95def091999-11-25 00:26:21 +11001261 int lowdelay = IPTOS_LOWDELAY;
1262 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &lowdelay,
Damien Miller34132e52000-01-14 15:45:46 +11001263 sizeof(lowdelay)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001264 error("setsockopt IPTOS_LOWDELAY: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001265#endif
Damien Miller95def091999-11-25 00:26:21 +11001266 if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on,
Damien Miller34132e52000-01-14 15:45:46 +11001267 sizeof(on)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001268 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
1269 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001270 /*
1271 * Set IP options for a non-interactive connection. Use
1272 * IPTOS_THROUGHPUT.
1273 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001274#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Damien Miller95def091999-11-25 00:26:21 +11001275 int throughput = IPTOS_THROUGHPUT;
1276 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001277 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001278 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001279#endif
Damien Miller95def091999-11-25 00:26:21 +11001280 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281}
1282
1283/* Returns true if the current connection is interactive. */
1284
1285int
1286packet_is_interactive()
1287{
Damien Miller95def091999-11-25 00:26:21 +11001288 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001289}
Damien Miller6162d121999-11-21 13:23:52 +11001290
1291int
1292packet_set_maxsize(int s)
1293{
Damien Miller95def091999-11-25 00:26:21 +11001294 static int called = 0;
1295 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001296 log("packet_set_maxsize: called twice: old %d new %d",
1297 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001298 return -1;
1299 }
1300 if (s < 4 * 1024 || s > 1024 * 1024) {
1301 log("packet_set_maxsize: bad size %d", s);
1302 return -1;
1303 }
1304 log("packet_set_maxsize: setting to %d", s);
1305 max_packet_size = s;
1306 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001307}