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