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