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