]> andersk Git - openssh.git/blame - packet.c
- (dtucker) [configure.ac defines.h sftp-client.c sftp-server.c sftp.c] Do not ...
[openssh.git] / packet.c
CommitLineData
17f02f0a 1/* $OpenBSD: packet.c,v 1.152 2008/05/08 06:59:01 markus Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5260325f 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
5260325f 6 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
7e7327a1 8 *
bcbf86ec 9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 *
7e7327a1 16 * SSH2 packet format added by Markus Friedl.
a96070d4 17 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
bcbf86ec 18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
7e7327a1 27 *
bcbf86ec 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5260325f 38 */
8efc0c15 39
40#include "includes.h"
a3dcf543 41
5b04a8bf 42#include <sys/types.h>
2f5b2528 43#include "openbsd-compat/sys-queue.h"
536c14e8 44#include <sys/param.h>
9794d008 45#include <sys/socket.h>
e264ac72 46#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
9794d008 49
9794d008 50#include <netinet/in.h>
a3dcf543 51#include <netinet/ip.h>
c5bca5d4 52#include <arpa/inet.h>
8efc0c15 53
028094f4 54#include <errno.h>
4c72fcfd 55#include <stdarg.h>
cf851879 56#include <stdio.h>
ffa517a8 57#include <stdlib.h>
00146caa 58#include <string.h>
5188ba17 59#include <unistd.h>
31652869 60#include <signal.h>
4c72fcfd 61
8efc0c15 62#include "xmalloc.h"
63#include "buffer.h"
64#include "packet.h"
8efc0c15 65#include "crc32.h"
8efc0c15 66#include "compress.h"
67#include "deattack.h"
a5efa1bb 68#include "channels.h"
7e7327a1 69#include "compat.h"
42f11eb2 70#include "ssh1.h"
7e7327a1 71#include "ssh2.h"
94ec8c6b 72#include "cipher.h"
31652869 73#include "key.h"
7e7327a1 74#include "kex.h"
b2552997 75#include "mac.h"
42f11eb2 76#include "log.h"
77#include "canohost.h"
2ac91be1 78#include "misc.h"
9459414c 79#include "ssh.h"
7e7327a1 80
81#ifdef PACKET_DEBUG
82#define DBG(x) x
83#else
84#define DBG(x)
85#endif
86
aa3378df 87/*
88 * This variable contains the file descriptors used for communicating with
89 * the other side. connection_in is used for reading; connection_out for
90 * writing. These can be the same descriptor, in which case it is assumed to
91 * be a socket.
92 */
8efc0c15 93static int connection_in = -1;
94static int connection_out = -1;
95
8efc0c15 96/* Protocol flags for the remote side. */
1e3b8b07 97static u_int remote_protocol_flags = 0;
8efc0c15 98
99/* Encryption context for receiving data. This is only used for decryption. */
100static CipherContext receive_context;
5260325f 101
102/* Encryption context for sending data. This is only used for encryption. */
8efc0c15 103static CipherContext send_context;
104
105/* Buffer for raw input data from the socket. */
e050d348 106Buffer input;
8efc0c15 107
108/* Buffer for raw output data going to the socket. */
e050d348 109Buffer output;
8efc0c15 110
111/* Buffer for the partial outgoing packet being constructed. */
112static Buffer outgoing_packet;
113
114/* Buffer for the incoming packet currently being processed. */
115static Buffer incoming_packet;
116
117/* Scratch buffer for packet compression/decompression. */
118static Buffer compression_buffer;
6ba22c93 119static int compression_buffer_ready = 0;
8efc0c15 120
121/* Flag indicating whether packet compression/decompression is enabled. */
122static int packet_compression = 0;
123
9d6b7add 124/* default maximum packet size */
a27002e5 125u_int max_packet_size = 32768;
9d6b7add 126
8efc0c15 127/* Flag indicating whether this module has been initialized. */
128static int initialized = 0;
129
130/* Set to true if the connection is interactive. */
131static int interactive_mode = 0;
132
07200973 133/* Set to true if we are the server side. */
134static int server_side = 0;
135
136/* Set to true if we are authenticated. */
137static int after_authentication = 0;
138
ac47b626 139int keep_alive_timeouts = 0;
140
7e7327a1 141/* Session key information for Encryption and MAC */
d1ac6175 142Newkeys *newkeys[MODE_MAX];
ffd7b36b 143static struct packet_state {
144 u_int32_t seqnr;
145 u_int32_t packets;
146 u_int64_t blocks;
147} p_read, p_send;
148
149static u_int64_t max_blocks_in, max_blocks_out;
150static u_int32_t rekey_limit;
7e7327a1 151
9459414c 152/* Session key for protocol v1 */
153static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
154static u_int ssh1_keylen;
155
b4b701be 156/* roundup current message to extra_pad bytes */
157static u_char extra_pad = 0;
158
ffd7b36b 159struct packet {
160 TAILQ_ENTRY(packet) next;
161 u_char type;
162 Buffer payload;
163};
164TAILQ_HEAD(, packet) outgoing;
165
aa3378df 166/*
167 * Sets the descriptors used for communication. Disables encryption until
168 * packet_set_encryption_key is called.
169 */
8efc0c15 170void
171packet_set_connection(int fd_in, int fd_out)
172{
94ec8c6b 173 Cipher *none = cipher_by_name("none");
e1feb9bf 174
94ec8c6b 175 if (none == NULL)
176 fatal("packet_set_connection: cannot load cipher 'none'");
5260325f 177 connection_in = fd_in;
178 connection_out = fd_out;
f6be21a0 179 cipher_init(&send_context, none, (const u_char *)"",
180 0, NULL, 0, CIPHER_ENCRYPT);
181 cipher_init(&receive_context, none, (const u_char *)"",
182 0, NULL, 0, CIPHER_DECRYPT);
08dcb5d7 183 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
5260325f 184 if (!initialized) {
185 initialized = 1;
186 buffer_init(&input);
187 buffer_init(&output);
188 buffer_init(&outgoing_packet);
189 buffer_init(&incoming_packet);
ffd7b36b 190 TAILQ_INIT(&outgoing);
5260325f 191 }
8efc0c15 192}
193
48e671d5 194/* Returns 1 if remote host is connected via socket, 0 if not. */
195
196int
d5bb9418 197packet_connection_is_on_socket(void)
48e671d5 198{
199 struct sockaddr_storage from, to;
200 socklen_t fromlen, tolen;
201
202 /* filedescriptors in and out are the same, so it's a socket */
203 if (connection_in == connection_out)
204 return 1;
205 fromlen = sizeof(from);
206 memset(&from, 0, sizeof(from));
9bc5ddfe 207 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
48e671d5 208 return 0;
209 tolen = sizeof(to);
210 memset(&to, 0, sizeof(to));
9bc5ddfe 211 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
48e671d5 212 return 0;
213 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
214 return 0;
215 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
216 return 0;
217 return 1;
218}
219
762715ce 220/*
e050d348 221 * Exports an IV from the CipherContext required to export the key
222 * state back from the unprivileged child to the privileged parent
223 * process.
224 */
225
226void
227packet_get_keyiv(int mode, u_char *iv, u_int len)
228{
229 CipherContext *cc;
230
231 if (mode == MODE_OUT)
232 cc = &send_context;
233 else
234 cc = &receive_context;
235
236 cipher_get_keyiv(cc, iv, len);
237}
238
239int
240packet_get_keycontext(int mode, u_char *dat)
241{
242 CipherContext *cc;
762715ce 243
e050d348 244 if (mode == MODE_OUT)
245 cc = &send_context;
246 else
247 cc = &receive_context;
248
249 return (cipher_get_keycontext(cc, dat));
250}
251
252void
253packet_set_keycontext(int mode, u_char *dat)
254{
255 CipherContext *cc;
762715ce 256
e050d348 257 if (mode == MODE_OUT)
258 cc = &send_context;
259 else
260 cc = &receive_context;
261
262 cipher_set_keycontext(cc, dat);
263}
264
265int
266packet_get_keyiv_len(int mode)
267{
268 CipherContext *cc;
269
270 if (mode == MODE_OUT)
271 cc = &send_context;
272 else
273 cc = &receive_context;
274
275 return (cipher_get_keyiv_len(cc));
276}
a7137f6b 277
e050d348 278void
279packet_set_iv(int mode, u_char *dat)
280{
281 CipherContext *cc;
282
283 if (mode == MODE_OUT)
284 cc = &send_context;
285 else
286 cc = &receive_context;
287
288 cipher_set_keyiv(cc, dat);
289}
a7137f6b 290
e050d348 291int
0daa6547 292packet_get_ssh1_cipher(void)
e050d348 293{
294 return (cipher_get_number(receive_context.cipher));
295}
296
ffd7b36b 297void
298packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
e050d348 299{
ffd7b36b 300 struct packet_state *state;
301
302 state = (mode == MODE_IN) ? &p_read : &p_send;
303 *seqnr = state->seqnr;
304 *blocks = state->blocks;
305 *packets = state->packets;
e050d348 306}
307
308void
ffd7b36b 309packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
e050d348 310{
ffd7b36b 311 struct packet_state *state;
312
313 state = (mode == MODE_IN) ? &p_read : &p_send;
314 state->seqnr = seqnr;
315 state->blocks = blocks;
316 state->packets = packets;
e050d348 317}
318
48e671d5 319/* returns 1 if connection is via ipv4 */
320
321int
d5bb9418 322packet_connection_is_ipv4(void)
48e671d5 323{
324 struct sockaddr_storage to;
d45317d8 325 socklen_t tolen = sizeof(to);
48e671d5 326
327 memset(&to, 0, sizeof(to));
328 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
329 return 0;
782e2103 330 if (to.ss_family == AF_INET)
331 return 1;
332#ifdef IPV4_IN_IPV6
aff51935 333 if (to.ss_family == AF_INET6 &&
782e2103 334 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
335 return 1;
336#endif
337 return 0;
48e671d5 338}
339
8efc0c15 340/* Sets the connection into non-blocking mode. */
341
342void
d5bb9418 343packet_set_nonblocking(void)
8efc0c15 344{
5260325f 345 /* Set the socket into non-blocking mode. */
170694d7 346 set_nonblock(connection_in);
8efc0c15 347
170694d7 348 if (connection_out != connection_in)
349 set_nonblock(connection_out);
8efc0c15 350}
351
352/* Returns the socket used for reading. */
353
354int
d5bb9418 355packet_get_connection_in(void)
8efc0c15 356{
5260325f 357 return connection_in;
8efc0c15 358}
359
360/* Returns the descriptor used for writing. */
361
362int
d5bb9418 363packet_get_connection_out(void)
8efc0c15 364{
5260325f 365 return connection_out;
8efc0c15 366}
367
368/* Closes the connection and clears and frees internal data structures. */
369
370void
d5bb9418 371packet_close(void)
8efc0c15 372{
5260325f 373 if (!initialized)
374 return;
375 initialized = 0;
376 if (connection_in == connection_out) {
377 shutdown(connection_out, SHUT_RDWR);
378 close(connection_out);
379 } else {
380 close(connection_in);
381 close(connection_out);
382 }
383 buffer_free(&input);
384 buffer_free(&output);
385 buffer_free(&outgoing_packet);
386 buffer_free(&incoming_packet);
6ba22c93 387 if (compression_buffer_ready) {
5260325f 388 buffer_free(&compression_buffer);
389 buffer_compress_uninit();
390 }
3ee832e5 391 cipher_cleanup(&send_context);
392 cipher_cleanup(&receive_context);
8efc0c15 393}
394
395/* Sets remote side protocol flags. */
396
397void
1e3b8b07 398packet_set_protocol_flags(u_int protocol_flags)
8efc0c15 399{
5260325f 400 remote_protocol_flags = protocol_flags;
8efc0c15 401}
402
403/* Returns the remote protocol flags set earlier by the above function. */
404
1e3b8b07 405u_int
d5bb9418 406packet_get_protocol_flags(void)
8efc0c15 407{
5260325f 408 return remote_protocol_flags;
8efc0c15 409}
410
aa3378df 411/*
412 * Starts packet compression from the next packet on in both directions.
413 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
414 */
8efc0c15 415
396c147e 416static void
417packet_init_compression(void)
6ba22c93 418{
419 if (compression_buffer_ready == 1)
420 return;
421 compression_buffer_ready = 1;
422 buffer_init(&compression_buffer);
423}
424
8efc0c15 425void
426packet_start_compression(int level)
427{
08dcb5d7 428 if (packet_compression && !compat20)
5260325f 429 fatal("Compression already enabled.");
430 packet_compression = 1;
6ba22c93 431 packet_init_compression();
432 buffer_compress_init_send(level);
433 buffer_compress_init_recv();
8efc0c15 434}
435
aa3378df 436/*
437 * Causes any further packets to be encrypted using the given key. The same
438 * key is used for both sending and reception. However, both directions are
439 * encrypted independently of each other.
440 */
9459414c 441
8efc0c15 442void
1e3b8b07 443packet_set_encryption_key(const u_char *key, u_int keylen,
94ec8c6b 444 int number)
8efc0c15 445{
94ec8c6b 446 Cipher *cipher = cipher_by_number(number);
e1feb9bf 447
94ec8c6b 448 if (cipher == NULL)
449 fatal("packet_set_encryption_key: unknown cipher number %d", number);
7e7327a1 450 if (keylen < 20)
94ec8c6b 451 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
9459414c 452 if (keylen > SSH_SESSION_KEY_LENGTH)
453 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
454 memcpy(ssh1_key, key, keylen);
455 ssh1_keylen = keylen;
3ee832e5 456 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
457 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
8efc0c15 458}
459
9459414c 460u_int
461packet_get_encryption_key(u_char *key)
462{
463 if (key == NULL)
464 return (ssh1_keylen);
465 memcpy(key, ssh1_key, ssh1_keylen);
466 return (ssh1_keylen);
467}
468
08dcb5d7 469/* Start constructing a packet to send. */
8efc0c15 470void
08dcb5d7 471packet_start(u_char type)
8efc0c15 472{
08dcb5d7 473 u_char buf[9];
474 int len;
7e7327a1 475
12bf85ed 476 DBG(debug("packet_start[%d]", type));
08dcb5d7 477 len = compat20 ? 6 : 9;
478 memset(buf, 0, len - 1);
479 buf[len - 1] = type;
480 buffer_clear(&outgoing_packet);
481 buffer_append(&outgoing_packet, buf, len);
7e7327a1 482}
483
08dcb5d7 484/* Append payload. */
8efc0c15 485void
486packet_put_char(int value)
487{
5260325f 488 char ch = value;
e1feb9bf 489
5260325f 490 buffer_append(&outgoing_packet, &ch, 1);
8efc0c15 491}
a7137f6b 492
8efc0c15 493void
1e3b8b07 494packet_put_int(u_int value)
8efc0c15 495{
5260325f 496 buffer_put_int(&outgoing_packet, value);
8efc0c15 497}
a7137f6b 498
8efc0c15 499void
6c0fa2b1 500packet_put_string(const void *buf, u_int len)
8efc0c15 501{
5260325f 502 buffer_put_string(&outgoing_packet, buf, len);
8efc0c15 503}
a7137f6b 504
7e7327a1 505void
506packet_put_cstring(const char *str)
507{
449c5ba5 508 buffer_put_cstring(&outgoing_packet, str);
7e7327a1 509}
a7137f6b 510
7e7327a1 511void
6c0fa2b1 512packet_put_raw(const void *buf, u_int len)
7e7327a1 513{
514 buffer_append(&outgoing_packet, buf, len);
515}
a7137f6b 516
8efc0c15 517void
5260325f 518packet_put_bignum(BIGNUM * value)
8efc0c15 519{
5260325f 520 buffer_put_bignum(&outgoing_packet, value);
8efc0c15 521}
a7137f6b 522
7e7327a1 523void
524packet_put_bignum2(BIGNUM * value)
525{
526 buffer_put_bignum2(&outgoing_packet, value);
527}
8efc0c15 528
aa3378df 529/*
530 * Finalizes and sends the packet. If the encryption key has been set,
531 * encrypts the packet before sending.
532 */
5260325f 533
396c147e 534static void
5ca51e19 535packet_send1(void)
8efc0c15 536{
a318bbf4 537 u_char buf[8], *cp;
5260325f 538 int i, padding, len;
1e3b8b07 539 u_int checksum;
ca75d7de 540 u_int32_t rnd = 0;
5260325f 541
aa3378df 542 /*
543 * If using packet compression, compress the payload of the outgoing
544 * packet.
545 */
5260325f 546 if (packet_compression) {
547 buffer_clear(&compression_buffer);
548 /* Skip padding. */
549 buffer_consume(&outgoing_packet, 8);
550 /* padding */
551 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
552 buffer_compress(&outgoing_packet, &compression_buffer);
553 buffer_clear(&outgoing_packet);
554 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
184eed6a 555 buffer_len(&compression_buffer));
5260325f 556 }
557 /* Compute packet length without padding (add checksum, remove padding). */
558 len = buffer_len(&outgoing_packet) + 4 - 8;
559
1d1ffb87 560 /* Insert padding. Initialized to zero in packet_start1() */
5260325f 561 padding = 8 - len % 8;
3ee832e5 562 if (!send_context.plaintext) {
5260325f 563 cp = buffer_ptr(&outgoing_packet);
564 for (i = 0; i < padding; i++) {
565 if (i % 4 == 0)
ca75d7de 566 rnd = arc4random();
567 cp[7 - i] = rnd & 0xff;
568 rnd >>= 8;
5260325f 569 }
570 }
571 buffer_consume(&outgoing_packet, 8 - padding);
572
573 /* Add check bytes. */
20905a8e 574 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
2e73a022 575 buffer_len(&outgoing_packet));
51e7a012 576 put_u32(buf, checksum);
5260325f 577 buffer_append(&outgoing_packet, buf, 4);
8efc0c15 578
579#ifdef PACKET_DEBUG
5260325f 580 fprintf(stderr, "packet_send plain: ");
581 buffer_dump(&outgoing_packet);
8efc0c15 582#endif
583
5260325f 584 /* Append to output. */
51e7a012 585 put_u32(buf, len);
5260325f 586 buffer_append(&output, buf, 4);
6c0fa2b1 587 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
3ee832e5 588 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
184eed6a 589 buffer_len(&outgoing_packet));
5260325f 590
8efc0c15 591#ifdef PACKET_DEBUG
5260325f 592 fprintf(stderr, "encrypted: ");
593 buffer_dump(&output);
8efc0c15 594#endif
595
5260325f 596 buffer_clear(&outgoing_packet);
8efc0c15 597
aa3378df 598 /*
7b9b0103 599 * Note that the packet is now only buffered in output. It won't be
aa3378df 600 * actually sent until packet_write_wait or packet_write_poll is
601 * called.
602 */
8efc0c15 603}
604
e050d348 605void
d1ac6175 606set_newkeys(int mode)
607{
608 Enc *enc;
609 Mac *mac;
610 Comp *comp;
611 CipherContext *cc;
ffd7b36b 612 u_int64_t *max_blocks;
ca75d7de 613 int crypt_type;
d1ac6175 614
a77673cc 615 debug2("set_newkeys: mode %d", mode);
d1ac6175 616
3ee832e5 617 if (mode == MODE_OUT) {
618 cc = &send_context;
ca75d7de 619 crypt_type = CIPHER_ENCRYPT;
ffd7b36b 620 p_send.packets = p_send.blocks = 0;
621 max_blocks = &max_blocks_out;
3ee832e5 622 } else {
623 cc = &receive_context;
ca75d7de 624 crypt_type = CIPHER_DECRYPT;
ffd7b36b 625 p_read.packets = p_read.blocks = 0;
626 max_blocks = &max_blocks_in;
3ee832e5 627 }
d1ac6175 628 if (newkeys[mode] != NULL) {
a77673cc 629 debug("set_newkeys: rekeying");
3ee832e5 630 cipher_cleanup(cc);
a7ca6275 631 enc = &newkeys[mode]->enc;
632 mac = &newkeys[mode]->mac;
633 comp = &newkeys[mode]->comp;
f444d0f8 634 mac_clear(mac);
a7ca6275 635 xfree(enc->name);
636 xfree(enc->iv);
637 xfree(enc->key);
638 xfree(mac->name);
639 xfree(mac->key);
640 xfree(comp->name);
d8ee838b 641 xfree(newkeys[mode]);
d1ac6175 642 }
643 newkeys[mode] = kex_get_newkeys(mode);
644 if (newkeys[mode] == NULL)
645 fatal("newkeys: no keys for mode %d", mode);
646 enc = &newkeys[mode]->enc;
647 mac = &newkeys[mode]->mac;
648 comp = &newkeys[mode]->comp;
f444d0f8 649 if (mac_init(mac) == 0)
d1ac6175 650 mac->enabled = 1;
651 DBG(debug("cipher_init_context: %d", mode));
3ee832e5 652 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
ca75d7de 653 enc->iv, enc->block_size, crypt_type);
e050d348 654 /* Deleting the keys does not gain extra security */
655 /* memset(enc->iv, 0, enc->block_size);
a3de8da1 656 memset(enc->key, 0, enc->key_len);
657 memset(mac->key, 0, mac->key_len); */
07200973 658 if ((comp->type == COMP_ZLIB ||
659 (comp->type == COMP_DELAYED && after_authentication)) &&
660 comp->enabled == 0) {
6ba22c93 661 packet_init_compression();
662 if (mode == MODE_OUT)
663 buffer_compress_init_send(6);
664 else
665 buffer_compress_init_recv();
d1ac6175 666 comp->enabled = 1;
d1ac6175 667 }
40729edd 668 /*
669 * The 2^(blocksize*2) limit is too expensive for 3DES,
670 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
671 */
672 if (enc->block_size >= 16)
673 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
674 else
675 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
ffd7b36b 676 if (rekey_limit)
677 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
d1ac6175 678}
679
07200973 680/*
681 * Delayed compression for SSH2 is enabled after authentication:
0e13ec0f 682 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
07200973 683 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
684 */
685static void
686packet_enable_delayed_compress(void)
687{
688 Comp *comp = NULL;
689 int mode;
690
691 /*
692 * Remember that we are past the authentication step, so rekeying
693 * with COMP_DELAYED will turn on compression immediately.
694 */
695 after_authentication = 1;
696 for (mode = 0; mode < MODE_MAX; mode++) {
a29c9898 697 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
698 if (newkeys[mode] == NULL)
699 continue;
07200973 700 comp = &newkeys[mode]->comp;
701 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
4145cbfa 702 packet_init_compression();
07200973 703 if (mode == MODE_OUT)
704 buffer_compress_init_send(6);
705 else
706 buffer_compress_init_recv();
707 comp->enabled = 1;
708 }
709 }
710}
711
7e7327a1 712/*
713 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
714 */
396c147e 715static void
ffd7b36b 716packet_send2_wrapped(void)
7e7327a1 717{
a318bbf4 718 u_char type, *cp, *macbuf = NULL;
b4b701be 719 u_char padlen, pad;
1e3b8b07 720 u_int packet_length = 0;
b4b701be 721 u_int i, len;
ca75d7de 722 u_int32_t rnd = 0;
7e7327a1 723 Enc *enc = NULL;
724 Mac *mac = NULL;
725 Comp *comp = NULL;
726 int block_size;
727
d1ac6175 728 if (newkeys[MODE_OUT] != NULL) {
729 enc = &newkeys[MODE_OUT]->enc;
730 mac = &newkeys[MODE_OUT]->mac;
731 comp = &newkeys[MODE_OUT]->comp;
7e7327a1 732 }
3ee832e5 733 block_size = enc ? enc->block_size : 8;
7e7327a1 734
a318bbf4 735 cp = buffer_ptr(&outgoing_packet);
736 type = cp[5];
7e7327a1 737
738#ifdef PACKET_DEBUG
739 fprintf(stderr, "plain: ");
740 buffer_dump(&outgoing_packet);
741#endif
742
743 if (comp && comp->enabled) {
744 len = buffer_len(&outgoing_packet);
745 /* skip header, compress only payload */
746 buffer_consume(&outgoing_packet, 5);
747 buffer_clear(&compression_buffer);
748 buffer_compress(&outgoing_packet, &compression_buffer);
749 buffer_clear(&outgoing_packet);
750 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
751 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
752 buffer_len(&compression_buffer));
753 DBG(debug("compression: raw %d compressed %d", len,
754 buffer_len(&outgoing_packet)));
755 }
756
757 /* sizeof (packet_len + pad_len + payload) */
758 len = buffer_len(&outgoing_packet);
759
760 /*
761 * calc size of padding, alloc space, get random data,
762 * minimum padding is 4 bytes
763 */
764 padlen = block_size - (len % block_size);
765 if (padlen < 4)
766 padlen += block_size;
b4b701be 767 if (extra_pad) {
768 /* will wrap if extra_pad+padlen > 255 */
769 extra_pad = roundup(extra_pad, block_size);
770 pad = extra_pad - ((len + padlen) % extra_pad);
a49dfdec 771 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
b4b701be 772 pad, len, padlen, extra_pad);
773 padlen += pad;
774 extra_pad = 0;
775 }
6c0fa2b1 776 cp = buffer_append_space(&outgoing_packet, padlen);
3ee832e5 777 if (enc && !send_context.plaintext) {
1d1ffb87 778 /* random padding */
7e7327a1 779 for (i = 0; i < padlen; i++) {
780 if (i % 4 == 0)
ca75d7de 781 rnd = arc4random();
782 cp[i] = rnd & 0xff;
783 rnd >>= 8;
7e7327a1 784 }
1d1ffb87 785 } else {
786 /* clear padding */
787 memset(cp, 0, padlen);
7e7327a1 788 }
789 /* packet_length includes payload, padding and padding length field */
790 packet_length = buffer_len(&outgoing_packet) - 4;
a318bbf4 791 cp = buffer_ptr(&outgoing_packet);
51e7a012 792 put_u32(cp, packet_length);
a318bbf4 793 cp[4] = padlen;
7e7327a1 794 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
795
796 /* compute MAC over seqnr and packet(length fields, payload, padding) */
797 if (mac && mac->enabled) {
ffd7b36b 798 macbuf = mac_compute(mac, p_send.seqnr,
20905a8e 799 buffer_ptr(&outgoing_packet),
b2552997 800 buffer_len(&outgoing_packet));
ffd7b36b 801 DBG(debug("done calc MAC out #%d", p_send.seqnr));
7e7327a1 802 }
803 /* encrypt packet and append to output buffer. */
6c0fa2b1 804 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
3ee832e5 805 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
7e7327a1 806 buffer_len(&outgoing_packet));
807 /* append unencrypted MAC */
808 if (mac && mac->enabled)
15dd2c4f 809 buffer_append(&output, macbuf, mac->mac_len);
7e7327a1 810#ifdef PACKET_DEBUG
811 fprintf(stderr, "encrypted: ");
812 buffer_dump(&output);
813#endif
6ae2364d 814 /* increment sequence number for outgoing packets */
ffd7b36b 815 if (++p_send.seqnr == 0)
bbe88b6d 816 logit("outgoing seqnr wraps around");
ffd7b36b 817 if (++p_send.packets == 0)
818 if (!(datafellows & SSH_BUG_NOREKEY))
819 fatal("XXX too many packets with same key");
820 p_send.blocks += (packet_length + 4) / block_size;
7e7327a1 821 buffer_clear(&outgoing_packet);
822
d1ac6175 823 if (type == SSH2_MSG_NEWKEYS)
824 set_newkeys(MODE_OUT);
07200973 825 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
826 packet_enable_delayed_compress();
7e7327a1 827}
828
ffd7b36b 829static void
830packet_send2(void)
831{
832 static int rekeying = 0;
833 struct packet *p;
834 u_char type, *cp;
835
836 cp = buffer_ptr(&outgoing_packet);
837 type = cp[5];
838
839 /* during rekeying we can only send key exchange messages */
840 if (rekeying) {
841 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
842 (type <= SSH2_MSG_TRANSPORT_MAX))) {
843 debug("enqueue packet: %u", type);
844 p = xmalloc(sizeof(*p));
845 p->type = type;
846 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
847 buffer_init(&outgoing_packet);
848 TAILQ_INSERT_TAIL(&outgoing, p, next);
849 return;
850 }
851 }
852
853 /* rekeying starts with sending KEXINIT */
854 if (type == SSH2_MSG_KEXINIT)
855 rekeying = 1;
856
857 packet_send2_wrapped();
858
859 /* after a NEWKEYS message we can send the complete queue */
860 if (type == SSH2_MSG_NEWKEYS) {
861 rekeying = 0;
862 while ((p = TAILQ_FIRST(&outgoing))) {
863 type = p->type;
864 debug("dequeue packet: %u", type);
865 buffer_free(&outgoing_packet);
866 memcpy(&outgoing_packet, &p->payload,
867 sizeof(Buffer));
868 TAILQ_REMOVE(&outgoing, p, next);
869 xfree(p);
870 packet_send2_wrapped();
871 }
872 }
873}
874
7e7327a1 875void
d5bb9418 876packet_send(void)
7e7327a1 877{
08dcb5d7 878 if (compat20)
7e7327a1 879 packet_send2();
880 else
881 packet_send1();
882 DBG(debug("packet_send done"));
883}
884
aa3378df 885/*
886 * Waits until a packet has been received, and returns its type. Note that
887 * no other data is processed until this returns, so this function should not
888 * be used during the interactive session.
889 */
8efc0c15 890
891int
54a5250f 892packet_read_seqnr(u_int32_t *seqnr_p)
8efc0c15 893{
5260325f 894 int type, len;
20e04e90 895 fd_set *setp;
5260325f 896 char buf[8192];
7e7327a1 897 DBG(debug("packet_read()"));
5260325f 898
52e3daed 899 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
20e04e90 900 sizeof(fd_mask));
901
5260325f 902 /* Since we are blocking, ensure that all written packets have been sent. */
903 packet_write_wait();
904
905 /* Stay in the loop until we have received a complete packet. */
906 for (;;) {
907 /* Try to read a packet from the buffer. */
54a5250f 908 type = packet_read_poll_seqnr(seqnr_p);
08dcb5d7 909 if (!compat20 && (
1d1ffb87 910 type == SSH_SMSG_SUCCESS
5260325f 911 || type == SSH_SMSG_FAILURE
912 || type == SSH_CMSG_EOF
1d1ffb87 913 || type == SSH_CMSG_EXIT_CONFIRMATION))
95500969 914 packet_check_eom();
5260325f 915 /* If we got a packet, return it. */
20e04e90 916 if (type != SSH_MSG_NONE) {
917 xfree(setp);
5260325f 918 return type;
20e04e90 919 }
aa3378df 920 /*
921 * Otherwise, wait for some data to arrive, add it to the
922 * buffer, and try again.
923 */
20e04e90 924 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
925 sizeof(fd_mask));
926 FD_SET(connection_in, setp);
aa3378df 927
5260325f 928 /* Wait for some data to arrive. */
20e04e90 929 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
fd193ca4 930 (errno == EAGAIN || errno == EINTR))
931 ;
aa3378df 932
5260325f 933 /* Read data from the socket. */
934 len = read(connection_in, buf, sizeof(buf));
89cafde6 935 if (len == 0) {
bbe88b6d 936 logit("Connection closed by %.200s", get_remote_ipaddr());
2362db19 937 cleanup_exit(255);
89cafde6 938 }
5260325f 939 if (len < 0)
940 fatal("Read from socket failed: %.100s", strerror(errno));
941 /* Append it to the buffer. */
942 packet_process_incoming(buf, len);
943 }
944 /* NOTREACHED */
8efc0c15 945}
946
24ca6821 947int
54a5250f 948packet_read(void)
24ca6821 949{
54a5250f 950 return packet_read_seqnr(NULL);
24ca6821 951}
952
aa3378df 953/*
954 * Waits until a packet has been received, verifies that its type matches
955 * that given, and gives a fatal error and exits if there is a mismatch.
956 */
8efc0c15 957
958void
54a5250f 959packet_read_expect(int expected_type)
8efc0c15 960{
5260325f 961 int type;
8efc0c15 962
54a5250f 963 type = packet_read();
5260325f 964 if (type != expected_type)
965 packet_disconnect("Protocol error: expected packet type %d, got %d",
7e7327a1 966 expected_type, type);
8efc0c15 967}
968
969/* Checks if a full packet is available in the data received so far via
5260325f 970 * packet_process_incoming. If so, reads the packet; otherwise returns
971 * SSH_MSG_NONE. This does not wait for data from the connection.
972 *
aacfb17b 973 * SSH_MSG_DISCONNECT is handled specially here. Also,
974 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
975 * to higher levels.
5260325f 976 */
8efc0c15 977
396c147e 978static int
54a5250f 979packet_read_poll1(void)
8efc0c15 980{
1e3b8b07 981 u_int len, padded_len;
a318bbf4 982 u_char *cp, type;
1e3b8b07 983 u_int checksum, stored_checksum;
5260325f 984
5260325f 985 /* Check if input size is less than minimum packet size. */
986 if (buffer_len(&input) < 4 + 8)
987 return SSH_MSG_NONE;
988 /* Get length of incoming packet. */
a318bbf4 989 cp = buffer_ptr(&input);
51e7a012 990 len = get_u32(cp);
5260325f 991 if (len < 1 + 2 + 2 || len > 256 * 1024)
2fe3c2db 992 packet_disconnect("Bad packet length %u.", len);
5260325f 993 padded_len = (len + 8) & ~7;
994
995 /* Check if the packet has been entirely received. */
996 if (buffer_len(&input) < 4 + padded_len)
997 return SSH_MSG_NONE;
998
999 /* The entire packet is in buffer. */
1000
1001 /* Consume packet length. */
1002 buffer_consume(&input, 4);
1003
08dcb5d7 1004 /*
1005 * Cryptographic attack detector for ssh
1006 * (C)1998 CORE-SDI, Buenos Aires Argentina
1007 * Ariel Futoransky(futo@core-sdi.com)
1008 */
f0d0e025 1009 if (!receive_context.plaintext) {
1010 switch (detect_attack(buffer_ptr(&input), padded_len)) {
1011 case DEATTACK_DETECTED:
1012 packet_disconnect("crc32 compensation attack: "
1013 "network attack detected");
1014 case DEATTACK_DOS_DETECTED:
1015 packet_disconnect("deattack denial of "
1016 "service detected");
1017 }
1018 }
08dcb5d7 1019
1020 /* Decrypt data to incoming_packet. */
5260325f 1021 buffer_clear(&incoming_packet);
6c0fa2b1 1022 cp = buffer_append_space(&incoming_packet, padded_len);
3ee832e5 1023 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
08dcb5d7 1024
5260325f 1025 buffer_consume(&input, padded_len);
8efc0c15 1026
1027#ifdef PACKET_DEBUG
5260325f 1028 fprintf(stderr, "read_poll plain: ");
1029 buffer_dump(&incoming_packet);
8efc0c15 1030#endif
5260325f 1031
1032 /* Compute packet checksum. */
20905a8e 1033 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
7e7327a1 1034 buffer_len(&incoming_packet) - 4);
5260325f 1035
1036 /* Skip padding. */
1037 buffer_consume(&incoming_packet, 8 - len % 8);
1038
1039 /* Test check bytes. */
5260325f 1040 if (len != buffer_len(&incoming_packet))
24ca6821 1041 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
7e7327a1 1042 len, buffer_len(&incoming_packet));
5260325f 1043
a318bbf4 1044 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
51e7a012 1045 stored_checksum = get_u32(cp);
5260325f 1046 if (checksum != stored_checksum)
1047 packet_disconnect("Corrupted check bytes on input.");
1048 buffer_consume_end(&incoming_packet, 4);
1049
5260325f 1050 if (packet_compression) {
1051 buffer_clear(&compression_buffer);
1052 buffer_uncompress(&incoming_packet, &compression_buffer);
1053 buffer_clear(&incoming_packet);
1054 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
7e7327a1 1055 buffer_len(&compression_buffer));
5260325f 1056 }
08dcb5d7 1057 type = buffer_get_char(&incoming_packet);
750bbb35 1058 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1059 packet_disconnect("Invalid ssh1 packet type: %d", type);
08dcb5d7 1060 return type;
5260325f 1061}
1062
396c147e 1063static int
54a5250f 1064packet_read_poll2(u_int32_t *seqnr_p)
7e7327a1 1065{
b2552997 1066 static u_int packet_length = 0;
1e3b8b07 1067 u_int padlen, need;
a318bbf4 1068 u_char *macbuf, *cp, type;
2ceb8101 1069 u_int maclen, block_size;
7e7327a1 1070 Enc *enc = NULL;
1071 Mac *mac = NULL;
1072 Comp *comp = NULL;
1073
d1ac6175 1074 if (newkeys[MODE_IN] != NULL) {
1075 enc = &newkeys[MODE_IN]->enc;
1076 mac = &newkeys[MODE_IN]->mac;
1077 comp = &newkeys[MODE_IN]->comp;
7e7327a1 1078 }
1079 maclen = mac && mac->enabled ? mac->mac_len : 0;
3ee832e5 1080 block_size = enc ? enc->block_size : 8;
7e7327a1 1081
1082 if (packet_length == 0) {
1083 /*
1084 * check if input size is less than the cipher block size,
1085 * decrypt first block and extract length of incoming packet
1086 */
1087 if (buffer_len(&input) < block_size)
1088 return SSH_MSG_NONE;
1089 buffer_clear(&incoming_packet);
6c0fa2b1 1090 cp = buffer_append_space(&incoming_packet, block_size);
3ee832e5 1091 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
7e7327a1 1092 block_size);
a318bbf4 1093 cp = buffer_ptr(&incoming_packet);
51e7a012 1094 packet_length = get_u32(cp);
7e7327a1 1095 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
9adbb4a4 1096#ifdef PACKET_DEBUG
7e7327a1 1097 buffer_dump(&incoming_packet);
9adbb4a4 1098#endif
2fe3c2db 1099 packet_disconnect("Bad packet length %u.", packet_length);
7e7327a1 1100 }
2fe3c2db 1101 DBG(debug("input: packet len %u", packet_length+4));
7e7327a1 1102 buffer_consume(&input, block_size);
1103 }
1104 /* we have a partial packet of block_size bytes */
1105 need = 4 + packet_length - block_size;
1106 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1107 need, maclen));
1108 if (need % block_size != 0)
1109 fatal("padding error: need %d block %d mod %d",
1110 need, block_size, need % block_size);
1111 /*
1112 * check if the entire packet has been received and
1113 * decrypt into incoming_packet
1114 */
1115 if (buffer_len(&input) < need + maclen)
1116 return SSH_MSG_NONE;
1117#ifdef PACKET_DEBUG
1118 fprintf(stderr, "read_poll enc/full: ");
1119 buffer_dump(&input);
1120#endif
6c0fa2b1 1121 cp = buffer_append_space(&incoming_packet, need);
3ee832e5 1122 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
7e7327a1 1123 buffer_consume(&input, need);
1124 /*
1125 * compute MAC over seqnr and packet,
1126 * increment sequence number for incoming packet
1127 */
6ae2364d 1128 if (mac && mac->enabled) {
ffd7b36b 1129 macbuf = mac_compute(mac, p_read.seqnr,
20905a8e 1130 buffer_ptr(&incoming_packet),
b2552997 1131 buffer_len(&incoming_packet));
7e7327a1 1132 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
94ec8c6b 1133 packet_disconnect("Corrupted MAC on input.");
ffd7b36b 1134 DBG(debug("MAC #%d ok", p_read.seqnr));
7e7327a1 1135 buffer_consume(&input, mac->mac_len);
1136 }
24ca6821 1137 if (seqnr_p != NULL)
ffd7b36b 1138 *seqnr_p = p_read.seqnr;
1139 if (++p_read.seqnr == 0)
bbe88b6d 1140 logit("incoming seqnr wraps around");
ffd7b36b 1141 if (++p_read.packets == 0)
1142 if (!(datafellows & SSH_BUG_NOREKEY))
1143 fatal("XXX too many packets with same key");
1144 p_read.blocks += (packet_length + 4) / block_size;
7e7327a1 1145
1146 /* get padlen */
6c0fa2b1 1147 cp = buffer_ptr(&incoming_packet);
a318bbf4 1148 padlen = cp[4];
7e7327a1 1149 DBG(debug("input: padlen %d", padlen));
1150 if (padlen < 4)
1151 packet_disconnect("Corrupted padlen %d on input.", padlen);
1152
1153 /* skip packet size + padlen, discard padding */
1154 buffer_consume(&incoming_packet, 4 + 1);
1155 buffer_consume_end(&incoming_packet, padlen);
1156
1157 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1158 if (comp && comp->enabled) {
1159 buffer_clear(&compression_buffer);
1160 buffer_uncompress(&incoming_packet, &compression_buffer);
1161 buffer_clear(&incoming_packet);
1162 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1163 buffer_len(&compression_buffer));
e1feb9bf 1164 DBG(debug("input: len after de-compress %d",
1165 buffer_len(&incoming_packet)));
7e7327a1 1166 }
1167 /*
1168 * get packet type, implies consume.
1169 * return length of payload (without type field)
1170 */
08dcb5d7 1171 type = buffer_get_char(&incoming_packet);
750bbb35 1172 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1173 packet_disconnect("Invalid ssh2 packet type: %d", type);
d1ac6175 1174 if (type == SSH2_MSG_NEWKEYS)
1175 set_newkeys(MODE_IN);
07200973 1176 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1177 packet_enable_delayed_compress();
7e7327a1 1178#ifdef PACKET_DEBUG
12bf85ed 1179 fprintf(stderr, "read/plain[%d]:\r\n", type);
7e7327a1 1180 buffer_dump(&incoming_packet);
1181#endif
08dcb5d7 1182 /* reset for next packet */
1183 packet_length = 0;
1184 return type;
7e7327a1 1185}
1186
1187int
54a5250f 1188packet_read_poll_seqnr(u_int32_t *seqnr_p)
7e7327a1 1189{
0bc50167 1190 u_int reason, seqnr;
08dcb5d7 1191 u_char type;
7e7327a1 1192 char *msg;
7e7327a1 1193
08dcb5d7 1194 for (;;) {
1195 if (compat20) {
54a5250f 1196 type = packet_read_poll2(seqnr_p);
ac47b626 1197 keep_alive_timeouts = 0;
08dcb5d7 1198 if (type)
7e7327a1 1199 DBG(debug("received packet type %d", type));
6aacefa7 1200 switch (type) {
aacfb17b 1201 case SSH2_MSG_IGNORE:
ac47b626 1202 debug3("Received SSH2_MSG_IGNORE");
aacfb17b 1203 break;
7e7327a1 1204 case SSH2_MSG_DEBUG:
1205 packet_get_char();
1206 msg = packet_get_string(NULL);
1207 debug("Remote: %.900s", msg);
1208 xfree(msg);
1209 msg = packet_get_string(NULL);
1210 xfree(msg);
1211 break;
1212 case SSH2_MSG_DISCONNECT:
1213 reason = packet_get_int();
1214 msg = packet_get_string(NULL);
bbe88b6d 1215 logit("Received disconnect from %s: %u: %.400s",
0bc50167 1216 get_remote_ipaddr(), reason, msg);
7e7327a1 1217 xfree(msg);
2362db19 1218 cleanup_exit(255);
7e7327a1 1219 break;
5a5f4c37 1220 case SSH2_MSG_UNIMPLEMENTED:
1221 seqnr = packet_get_int();
0bc50167 1222 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1223 seqnr);
aacfb17b 1224 break;
7e7327a1 1225 default:
1226 return type;
2b87da3b 1227 }
7e7327a1 1228 } else {
54a5250f 1229 type = packet_read_poll1();
6aacefa7 1230 switch (type) {
7e7327a1 1231 case SSH_MSG_IGNORE:
1232 break;
1233 case SSH_MSG_DEBUG:
1234 msg = packet_get_string(NULL);
1235 debug("Remote: %.900s", msg);
1236 xfree(msg);
1237 break;
1238 case SSH_MSG_DISCONNECT:
1239 msg = packet_get_string(NULL);
bbe88b6d 1240 logit("Received disconnect from %s: %.400s",
0bc50167 1241 get_remote_ipaddr(), msg);
2362db19 1242 cleanup_exit(255);
7e7327a1 1243 break;
1244 default:
08dcb5d7 1245 if (type)
7e7327a1 1246 DBG(debug("received packet type %d", type));
1247 return type;
2b87da3b 1248 }
7e7327a1 1249 }
1250 }
1251}
1252
24ca6821 1253int
54a5250f 1254packet_read_poll(void)
24ca6821 1255{
54a5250f 1256 return packet_read_poll_seqnr(NULL);
24ca6821 1257}
1258
aa3378df 1259/*
1260 * Buffers the given amount of input characters. This is intended to be used
1261 * together with packet_read_poll.
1262 */
8efc0c15 1263
1264void
1e3b8b07 1265packet_process_incoming(const char *buf, u_int len)
8efc0c15 1266{
5260325f 1267 buffer_append(&input, buf, len);
8efc0c15 1268}
1269
1270/* Returns a character from the packet. */
1271
1e3b8b07 1272u_int
d5bb9418 1273packet_get_char(void)
8efc0c15 1274{
5260325f 1275 char ch;
e1feb9bf 1276
5260325f 1277 buffer_get(&incoming_packet, &ch, 1);
1e3b8b07 1278 return (u_char) ch;
8efc0c15 1279}
1280
1281/* Returns an integer from the packet data. */
1282
1e3b8b07 1283u_int
d5bb9418 1284packet_get_int(void)
8efc0c15 1285{
5260325f 1286 return buffer_get_int(&incoming_packet);
8efc0c15 1287}
1288
aa3378df 1289/*
1290 * Returns an arbitrary precision integer from the packet data. The integer
1291 * must have been initialized before this call.
1292 */
8efc0c15 1293
1294void
20b279e6 1295packet_get_bignum(BIGNUM * value)
8efc0c15 1296{
4ef6f649 1297 buffer_get_bignum(&incoming_packet, value);
8efc0c15 1298}
1299
7e7327a1 1300void
20b279e6 1301packet_get_bignum2(BIGNUM * value)
7e7327a1 1302{
4ef6f649 1303 buffer_get_bignum2(&incoming_packet, value);
7e7327a1 1304}
1305
6c0fa2b1 1306void *
2ceb8101 1307packet_get_raw(u_int *length_ptr)
7e7327a1 1308{
2ceb8101 1309 u_int bytes = buffer_len(&incoming_packet);
e1feb9bf 1310
7e7327a1 1311 if (length_ptr != NULL)
1312 *length_ptr = bytes;
1313 return buffer_ptr(&incoming_packet);
1314}
1315
6ae2364d 1316int
1317packet_remaining(void)
1318{
1319 return buffer_len(&incoming_packet);
1320}
1321
aa3378df 1322/*
1323 * Returns a string from the packet data. The string is allocated using
1324 * xmalloc; it is the responsibility of the calling program to free it when
1325 * no longer needed. The length_ptr argument may be NULL, or point to an
1326 * integer into which the length of the string is stored.
1327 */
8efc0c15 1328
6c0fa2b1 1329void *
1e3b8b07 1330packet_get_string(u_int *length_ptr)
8efc0c15 1331{
5260325f 1332 return buffer_get_string(&incoming_packet, length_ptr);
17f02f0a 1333}
1334
1335void *
1336packet_get_string_ptr(u_int *length_ptr)
1337{
1338 return buffer_get_string_ptr(&incoming_packet, length_ptr);
8efc0c15 1339}
1340
aa3378df 1341/*
1342 * Sends a diagnostic message from the server to the client. This message
1343 * can be sent at any time (but not while constructing another message). The
1344 * message is printed immediately, but only if the client is being executed
1345 * in verbose mode. These messages are primarily intended to ease debugging
1346 * authentication problems. The length of the formatted message must not
1347 * exceed 1024 bytes. This will automatically call packet_write_wait.
1348 */
8efc0c15 1349
1350void
5260325f 1351packet_send_debug(const char *fmt,...)
8efc0c15 1352{
5260325f 1353 char buf[1024];
1354 va_list args;
1355
f72fc97f 1356 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1357 return;
1358
5260325f 1359 va_start(args, fmt);
1360 vsnprintf(buf, sizeof(buf), fmt, args);
1361 va_end(args);
1362
c4bc58eb 1363 if (compat20) {
1364 packet_start(SSH2_MSG_DEBUG);
1365 packet_put_char(0); /* bool: always display */
1366 packet_put_cstring(buf);
1367 packet_put_cstring("");
1368 } else {
1369 packet_start(SSH_MSG_DEBUG);
1370 packet_put_cstring(buf);
1371 }
5260325f 1372 packet_send();
1373 packet_write_wait();
8efc0c15 1374}
1375
aa3378df 1376/*
1377 * Logs the error plus constructs and sends a disconnect packet, closes the
1378 * connection, and exits. This function never returns. The error message
1379 * should not contain a newline. The length of the formatted message must
1380 * not exceed 1024 bytes.
1381 */
8efc0c15 1382
1383void
5260325f 1384packet_disconnect(const char *fmt,...)
1385{
1386 char buf[1024];
1387 va_list args;
1388 static int disconnecting = 0;
e1feb9bf 1389
5260325f 1390 if (disconnecting) /* Guard against recursive invocations. */
1391 fatal("packet_disconnect called recursively.");
1392 disconnecting = 1;
1393
aa3378df 1394 /*
1395 * Format the message. Note that the caller must make sure the
1396 * message is of limited size.
1397 */
5260325f 1398 va_start(args, fmt);
1399 vsnprintf(buf, sizeof(buf), fmt, args);
1400 va_end(args);
1401
2ccb7bde 1402 /* Display the error locally */
bbe88b6d 1403 logit("Disconnecting: %.100s", buf);
2ccb7bde 1404
5260325f 1405 /* Send the disconnect message to the other side, and wait for it to get sent. */
7e7327a1 1406 if (compat20) {
1407 packet_start(SSH2_MSG_DISCONNECT);
1408 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1409 packet_put_cstring(buf);
1410 packet_put_cstring("");
1411 } else {
1412 packet_start(SSH_MSG_DISCONNECT);
449c5ba5 1413 packet_put_cstring(buf);
7e7327a1 1414 }
5260325f 1415 packet_send();
1416 packet_write_wait();
1417
1418 /* Stop listening for connections. */
d6746a0b 1419 channel_close_all();
5260325f 1420
1421 /* Close the connection. */
1422 packet_close();
2362db19 1423 cleanup_exit(255);
8efc0c15 1424}
1425
aa3378df 1426/* Checks if there is any buffered output, and tries to write some of the output. */
8efc0c15 1427
1428void
d5bb9418 1429packet_write_poll(void)
8efc0c15 1430{
5260325f 1431 int len = buffer_len(&output);
e1feb9bf 1432
5260325f 1433 if (len > 0) {
1434 len = write(connection_out, buffer_ptr(&output), len);
1435 if (len <= 0) {
1436 if (errno == EAGAIN)
1437 return;
1438 else
1439 fatal("Write failed: %.100s", strerror(errno));
1440 }
1441 buffer_consume(&output, len);
1442 }
8efc0c15 1443}
1444
aa3378df 1445/*
1446 * Calls packet_write_poll repeatedly until all pending output data has been
1447 * written.
1448 */
8efc0c15 1449
1450void
d5bb9418 1451packet_write_wait(void)
8efc0c15 1452{
20e04e90 1453 fd_set *setp;
1454
52e3daed 1455 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
20e04e90 1456 sizeof(fd_mask));
5260325f 1457 packet_write_poll();
1458 while (packet_have_data_to_write()) {
20e04e90 1459 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1460 sizeof(fd_mask));
1461 FD_SET(connection_out, setp);
1462 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
fd193ca4 1463 (errno == EAGAIN || errno == EINTR))
1464 ;
5260325f 1465 packet_write_poll();
1466 }
20e04e90 1467 xfree(setp);
8efc0c15 1468}
1469
1470/* Returns true if there is buffered data to write to the connection. */
1471
1472int
d5bb9418 1473packet_have_data_to_write(void)
8efc0c15 1474{
5260325f 1475 return buffer_len(&output) != 0;
8efc0c15 1476}
1477
1478/* Returns true if there is not too much data to write to the connection. */
1479
1480int
d5bb9418 1481packet_not_very_much_data_to_write(void)
8efc0c15 1482{
5260325f 1483 if (interactive_mode)
1484 return buffer_len(&output) < 16384;
1485 else
1486 return buffer_len(&output) < 128 * 1024;
8efc0c15 1487}
1488
4d0cb2e5 1489
9e637910 1490static void
48f636b2 1491packet_set_tos(int interactive)
1492{
00df6acd 1493#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
48f636b2 1494 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1495
1496 if (!packet_connection_is_on_socket() ||
1497 !packet_connection_is_ipv4())
1498 return;
1499 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1500 sizeof(tos)) < 0)
1501 error("setsockopt IP_TOS %d: %.100s:",
1502 tos, strerror(errno));
4d0cb2e5 1503#endif
00df6acd 1504}
48f636b2 1505
8efc0c15 1506/* Informs that the current session is interactive. Sets IP flags for that. */
1507
1508void
b5c334cc 1509packet_set_interactive(int interactive)
8efc0c15 1510{
b5c334cc 1511 static int called = 0;
5260325f 1512
b5c334cc 1513 if (called)
1514 return;
1515 called = 1;
1516
5260325f 1517 /* Record that we are in interactive mode. */
1518 interactive_mode = interactive;
1519
48e671d5 1520 /* Only set socket options if using a socket. */
1521 if (!packet_connection_is_on_socket())
aceb0423 1522 return;
71f0de56 1523 set_nodelay(connection_in);
48f636b2 1524 packet_set_tos(interactive);
8efc0c15 1525}
1526
1527/* Returns true if the current connection is interactive. */
1528
1529int
d5bb9418 1530packet_is_interactive(void)
8efc0c15 1531{
5260325f 1532 return interactive_mode;
8efc0c15 1533}
9d6b7add 1534
f6be21a0 1535int
a27002e5 1536packet_set_maxsize(u_int s)
9d6b7add 1537{
5260325f 1538 static int called = 0;
e1feb9bf 1539
5260325f 1540 if (called) {
bbe88b6d 1541 logit("packet_set_maxsize: called twice: old %d new %d",
7e7327a1 1542 max_packet_size, s);
5260325f 1543 return -1;
1544 }
1545 if (s < 4 * 1024 || s > 1024 * 1024) {
bbe88b6d 1546 logit("packet_set_maxsize: bad size %d", s);
5260325f 1547 return -1;
1548 }
b6350327 1549 called = 1;
76735fe3 1550 debug("packet_set_maxsize: setting to %d", s);
5260325f 1551 max_packet_size = s;
1552 return s;
9d6b7add 1553}
a6215e53 1554
b4b701be 1555/* roundup current message to pad bytes */
1556void
1557packet_add_padding(u_char pad)
1558{
1559 extra_pad = pad;
1560}
1561
a6215e53 1562/*
1563 * 9.2. Ignored Data Message
cd332296 1564 *
a6215e53 1565 * byte SSH_MSG_IGNORE
1566 * string data
cd332296 1567 *
a6215e53 1568 * All implementations MUST understand (and ignore) this message at any
1569 * time (after receiving the protocol version). No implementation is
1570 * required to send them. This message can be used as an additional
1571 * protection measure against advanced traffic analysis techniques.
1572 */
95ce5599 1573void
1574packet_send_ignore(int nbytes)
1575{
ca75d7de 1576 u_int32_t rnd = 0;
95ce5599 1577 int i;
1578
1579 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
a6215e53 1580 packet_put_int(nbytes);
184eed6a 1581 for (i = 0; i < nbytes; i++) {
a6215e53 1582 if (i % 4 == 0)
ca75d7de 1583 rnd = arc4random();
febd6f21 1584 packet_put_char((u_char)rnd & 0xff);
ca75d7de 1585 rnd >>= 8;
a6215e53 1586 }
1587}
ffd7b36b 1588
f6be21a0 1589#define MAX_PACKETS (1U<<31)
ffd7b36b 1590int
1591packet_need_rekeying(void)
1592{
1593 if (datafellows & SSH_BUG_NOREKEY)
1594 return 0;
1595 return
1596 (p_send.packets > MAX_PACKETS) ||
1597 (p_read.packets > MAX_PACKETS) ||
1598 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1599 (max_blocks_in && (p_read.blocks > max_blocks_in));
1600}
1601
1602void
1603packet_set_rekey_limit(u_int32_t bytes)
1604{
1605 rekey_limit = bytes;
1606}
07200973 1607
1608void
1609packet_set_server(void)
1610{
1611 server_side = 1;
1612}
1613
1614void
1615packet_set_authenticated(void)
1616{
1617 after_authentication = 1;
1618}
This page took 0.688828 seconds and 5 git commands to generate.