1 /* $OpenBSD: packet.c,v 1.162 2009/05/27 06:36:07 andreas Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
16 * SSH2 packet format added by Markus Friedl.
17 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/types.h>
43 #include "openbsd-compat/sys-queue.h"
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
50 #include <netinet/in.h>
51 #include <netinet/ip.h>
52 #include <arpa/inet.h>
87 #define PACKET_MAX_SIZE (256 * 1024)
97 TAILQ_ENTRY(packet) next;
102 struct session_state {
104 * This variable contains the file descriptors used for
105 * communicating with the other side. connection_in is used for
106 * reading; connection_out for writing. These can be the same
107 * descriptor, in which case it is assumed to be a socket.
112 /* Protocol flags for the remote side. */
113 u_int remote_protocol_flags;
115 /* Encryption context for receiving data. Only used for decryption. */
116 CipherContext receive_context;
118 /* Encryption context for sending data. Only used for encryption. */
119 CipherContext send_context;
121 /* Buffer for raw input data from the socket. */
124 /* Buffer for raw output data going to the socket. */
127 /* Buffer for the partial outgoing packet being constructed. */
128 Buffer outgoing_packet;
130 /* Buffer for the incoming packet currently being processed. */
131 Buffer incoming_packet;
133 /* Scratch buffer for packet compression/decompression. */
134 Buffer compression_buffer;
135 int compression_buffer_ready;
138 * Flag indicating whether packet compression/decompression is
141 int packet_compression;
143 /* default maximum packet size */
144 u_int max_packet_size;
146 /* Flag indicating whether this module has been initialized. */
149 /* Set to true if the connection is interactive. */
150 int interactive_mode;
152 /* Set to true if we are the server side. */
155 /* Set to true if we are authenticated. */
156 int after_authentication;
158 int keep_alive_timeouts;
160 /* The maximum time that we will wait to send or receive a packet */
161 int packet_timeout_ms;
163 /* Session key information for Encryption and MAC */
164 Newkeys *newkeys[MODE_MAX];
165 struct packet_state p_read, p_send;
167 u_int64_t max_blocks_in, max_blocks_out;
168 u_int32_t rekey_limit;
170 /* Session key for protocol v1 */
171 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
174 /* roundup current message to extra_pad bytes */
177 /* XXX discard incoming data after MAC error */
178 u_int packet_discard;
179 Mac *packet_discard_mac;
181 /* Used in packet_read_poll2() */
184 TAILQ_HEAD(, packet) outgoing;
187 static struct session_state *active_state;
189 static struct session_state *
190 alloc_session_state()
192 struct session_state *s = xcalloc(1, sizeof(*s));
194 s->connection_in = -1;
195 s->connection_out = -1;
196 s->max_packet_size = 32768;
197 s->packet_timeout_ms = -1;
202 * Sets the descriptors used for communication. Disables encryption until
203 * packet_set_encryption_key is called.
206 packet_set_connection(int fd_in, int fd_out)
208 Cipher *none = cipher_by_name("none");
211 fatal("packet_set_connection: cannot load cipher 'none'");
212 if (active_state == NULL)
213 active_state = alloc_session_state();
214 active_state->connection_in = fd_in;
215 active_state->connection_out = fd_out;
216 cipher_init(&active_state->send_context, none, (const u_char *)"",
217 0, NULL, 0, CIPHER_ENCRYPT);
218 cipher_init(&active_state->receive_context, none, (const u_char *)"",
219 0, NULL, 0, CIPHER_DECRYPT);
220 active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
221 if (!active_state->initialized) {
222 active_state->initialized = 1;
223 buffer_init(&active_state->input);
224 buffer_init(&active_state->output);
225 buffer_init(&active_state->outgoing_packet);
226 buffer_init(&active_state->incoming_packet);
227 TAILQ_INIT(&active_state->outgoing);
228 active_state->p_send.packets = active_state->p_read.packets = 0;
233 packet_set_timeout(int timeout, int count)
235 if (timeout == 0 || count == 0) {
236 active_state->packet_timeout_ms = -1;
239 if ((INT_MAX / 1000) / count < timeout)
240 active_state->packet_timeout_ms = INT_MAX;
242 active_state->packet_timeout_ms = timeout * count * 1000;
246 packet_stop_discard(void)
248 if (active_state->packet_discard_mac) {
251 memset(buf, 'a', sizeof(buf));
252 while (buffer_len(&active_state->incoming_packet) <
254 buffer_append(&active_state->incoming_packet, buf,
256 (void) mac_compute(active_state->packet_discard_mac,
257 active_state->p_read.seqnr,
258 buffer_ptr(&active_state->incoming_packet),
261 logit("Finished discarding for %.200s", get_remote_ipaddr());
266 packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
268 if (enc == NULL || !cipher_is_cbc(enc->cipher))
269 packet_disconnect("Packet corrupt");
270 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
271 active_state->packet_discard_mac = mac;
272 if (buffer_len(&active_state->input) >= discard)
273 packet_stop_discard();
274 active_state->packet_discard = discard -
275 buffer_len(&active_state->input);
278 /* Returns 1 if remote host is connected via socket, 0 if not. */
281 packet_connection_is_on_socket(void)
283 struct sockaddr_storage from, to;
284 socklen_t fromlen, tolen;
286 /* filedescriptors in and out are the same, so it's a socket */
287 if (active_state->connection_in == active_state->connection_out)
289 fromlen = sizeof(from);
290 memset(&from, 0, sizeof(from));
291 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
295 memset(&to, 0, sizeof(to));
296 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
299 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
301 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
307 * Exports an IV from the CipherContext required to export the key
308 * state back from the unprivileged child to the privileged parent
313 packet_get_keyiv(int mode, u_char *iv, u_int len)
317 if (mode == MODE_OUT)
318 cc = &active_state->send_context;
320 cc = &active_state->receive_context;
322 cipher_get_keyiv(cc, iv, len);
326 packet_get_keycontext(int mode, u_char *dat)
330 if (mode == MODE_OUT)
331 cc = &active_state->send_context;
333 cc = &active_state->receive_context;
335 return (cipher_get_keycontext(cc, dat));
339 packet_set_keycontext(int mode, u_char *dat)
343 if (mode == MODE_OUT)
344 cc = &active_state->send_context;
346 cc = &active_state->receive_context;
348 cipher_set_keycontext(cc, dat);
352 packet_get_keyiv_len(int mode)
356 if (mode == MODE_OUT)
357 cc = &active_state->send_context;
359 cc = &active_state->receive_context;
361 return (cipher_get_keyiv_len(cc));
365 packet_set_iv(int mode, u_char *dat)
369 if (mode == MODE_OUT)
370 cc = &active_state->send_context;
372 cc = &active_state->receive_context;
374 cipher_set_keyiv(cc, dat);
378 packet_get_ssh1_cipher(void)
380 return (cipher_get_number(active_state->receive_context.cipher));
384 packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets,
387 struct packet_state *state;
389 state = (mode == MODE_IN) ?
390 &active_state->p_read : &active_state->p_send;
392 *seqnr = state->seqnr;
394 *blocks = state->blocks;
396 *packets = state->packets;
398 *bytes = state->bytes;
402 packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
405 struct packet_state *state;
407 state = (mode == MODE_IN) ?
408 &active_state->p_read : &active_state->p_send;
409 state->seqnr = seqnr;
410 state->blocks = blocks;
411 state->packets = packets;
412 state->bytes = bytes;
415 /* returns 1 if connection is via ipv4 */
418 packet_connection_is_ipv4(void)
420 struct sockaddr_storage to;
421 socklen_t tolen = sizeof(to);
423 memset(&to, 0, sizeof(to));
424 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
427 if (to.ss_family == AF_INET)
430 if (to.ss_family == AF_INET6 &&
431 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
437 /* Sets the connection into non-blocking mode. */
440 packet_set_nonblocking(void)
442 /* Set the socket into non-blocking mode. */
443 set_nonblock(active_state->connection_in);
445 if (active_state->connection_out != active_state->connection_in)
446 set_nonblock(active_state->connection_out);
449 /* Returns the socket used for reading. */
452 packet_get_connection_in(void)
454 return active_state->connection_in;
457 /* Returns the descriptor used for writing. */
460 packet_get_connection_out(void)
462 return active_state->connection_out;
465 /* Closes the connection and clears and frees internal data structures. */
470 if (!active_state->initialized)
472 active_state->initialized = 0;
473 if (active_state->connection_in == active_state->connection_out) {
474 shutdown(active_state->connection_out, SHUT_RDWR);
475 close(active_state->connection_out);
477 close(active_state->connection_in);
478 close(active_state->connection_out);
480 buffer_free(&active_state->input);
481 buffer_free(&active_state->output);
482 buffer_free(&active_state->outgoing_packet);
483 buffer_free(&active_state->incoming_packet);
484 if (active_state->compression_buffer_ready) {
485 buffer_free(&active_state->compression_buffer);
486 buffer_compress_uninit();
488 cipher_cleanup(&active_state->send_context);
489 cipher_cleanup(&active_state->receive_context);
492 /* Sets remote side protocol flags. */
495 packet_set_protocol_flags(u_int protocol_flags)
497 active_state->remote_protocol_flags = protocol_flags;
500 /* Returns the remote protocol flags set earlier by the above function. */
503 packet_get_protocol_flags(void)
505 return active_state->remote_protocol_flags;
509 * Starts packet compression from the next packet on in both directions.
510 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
514 packet_init_compression(void)
516 if (active_state->compression_buffer_ready == 1)
518 active_state->compression_buffer_ready = 1;
519 buffer_init(&active_state->compression_buffer);
523 packet_start_compression(int level)
525 if (active_state->packet_compression && !compat20)
526 fatal("Compression already enabled.");
527 active_state->packet_compression = 1;
528 packet_init_compression();
529 buffer_compress_init_send(level);
530 buffer_compress_init_recv();
534 * Causes any further packets to be encrypted using the given key. The same
535 * key is used for both sending and reception. However, both directions are
536 * encrypted independently of each other.
540 packet_set_encryption_key(const u_char *key, u_int keylen,
543 Cipher *cipher = cipher_by_number(number);
546 fatal("packet_set_encryption_key: unknown cipher number %d", number);
548 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
549 if (keylen > SSH_SESSION_KEY_LENGTH)
550 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
551 memcpy(active_state->ssh1_key, key, keylen);
552 active_state->ssh1_keylen = keylen;
553 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
555 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
560 packet_get_encryption_key(u_char *key)
563 return (active_state->ssh1_keylen);
564 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
565 return (active_state->ssh1_keylen);
568 /* Start constructing a packet to send. */
570 packet_start(u_char type)
575 DBG(debug("packet_start[%d]", type));
576 len = compat20 ? 6 : 9;
577 memset(buf, 0, len - 1);
579 buffer_clear(&active_state->outgoing_packet);
580 buffer_append(&active_state->outgoing_packet, buf, len);
583 /* Append payload. */
585 packet_put_char(int value)
589 buffer_append(&active_state->outgoing_packet, &ch, 1);
593 packet_put_int(u_int value)
595 buffer_put_int(&active_state->outgoing_packet, value);
599 packet_put_int64(u_int64_t value)
601 buffer_put_int64(&active_state->outgoing_packet, value);
605 packet_put_string(const void *buf, u_int len)
607 buffer_put_string(&active_state->outgoing_packet, buf, len);
611 packet_put_cstring(const char *str)
613 buffer_put_cstring(&active_state->outgoing_packet, str);
617 packet_put_raw(const void *buf, u_int len)
619 buffer_append(&active_state->outgoing_packet, buf, len);
623 packet_put_bignum(BIGNUM * value)
625 buffer_put_bignum(&active_state->outgoing_packet, value);
629 packet_put_bignum2(BIGNUM * value)
631 buffer_put_bignum2(&active_state->outgoing_packet, value);
635 * Finalizes and sends the packet. If the encryption key has been set,
636 * encrypts the packet before sending.
648 * If using packet compression, compress the payload of the outgoing
651 if (active_state->packet_compression) {
652 buffer_clear(&active_state->compression_buffer);
654 buffer_consume(&active_state->outgoing_packet, 8);
656 buffer_append(&active_state->compression_buffer,
657 "\0\0\0\0\0\0\0\0", 8);
658 buffer_compress(&active_state->outgoing_packet,
659 &active_state->compression_buffer);
660 buffer_clear(&active_state->outgoing_packet);
661 buffer_append(&active_state->outgoing_packet,
662 buffer_ptr(&active_state->compression_buffer),
663 buffer_len(&active_state->compression_buffer));
665 /* Compute packet length without padding (add checksum, remove padding). */
666 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
668 /* Insert padding. Initialized to zero in packet_start1() */
669 padding = 8 - len % 8;
670 if (!active_state->send_context.plaintext) {
671 cp = buffer_ptr(&active_state->outgoing_packet);
672 for (i = 0; i < padding; i++) {
675 cp[7 - i] = rnd & 0xff;
679 buffer_consume(&active_state->outgoing_packet, 8 - padding);
681 /* Add check bytes. */
682 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
683 buffer_len(&active_state->outgoing_packet));
684 put_u32(buf, checksum);
685 buffer_append(&active_state->outgoing_packet, buf, 4);
688 fprintf(stderr, "packet_send plain: ");
689 buffer_dump(&active_state->outgoing_packet);
692 /* Append to output. */
694 buffer_append(&active_state->output, buf, 4);
695 cp = buffer_append_space(&active_state->output,
696 buffer_len(&active_state->outgoing_packet));
697 cipher_crypt(&active_state->send_context, cp,
698 buffer_ptr(&active_state->outgoing_packet),
699 buffer_len(&active_state->outgoing_packet));
702 fprintf(stderr, "encrypted: ");
703 buffer_dump(&active_state->output);
705 active_state->p_send.packets++;
706 active_state->p_send.bytes += len +
707 buffer_len(&active_state->outgoing_packet);
708 buffer_clear(&active_state->outgoing_packet);
711 * Note that the packet is now only buffered in output. It won't be
712 * actually sent until packet_write_wait or packet_write_poll is
718 set_newkeys(int mode)
724 u_int64_t *max_blocks;
727 debug2("set_newkeys: mode %d", mode);
729 if (mode == MODE_OUT) {
730 cc = &active_state->send_context;
731 crypt_type = CIPHER_ENCRYPT;
732 active_state->p_send.packets = active_state->p_send.blocks = 0;
733 max_blocks = &active_state->max_blocks_out;
735 cc = &active_state->receive_context;
736 crypt_type = CIPHER_DECRYPT;
737 active_state->p_read.packets = active_state->p_read.blocks = 0;
738 max_blocks = &active_state->max_blocks_in;
740 if (active_state->newkeys[mode] != NULL) {
741 debug("set_newkeys: rekeying");
743 enc = &active_state->newkeys[mode]->enc;
744 mac = &active_state->newkeys[mode]->mac;
745 comp = &active_state->newkeys[mode]->comp;
753 xfree(active_state->newkeys[mode]);
755 active_state->newkeys[mode] = kex_get_newkeys(mode);
756 if (active_state->newkeys[mode] == NULL)
757 fatal("newkeys: no keys for mode %d", mode);
758 enc = &active_state->newkeys[mode]->enc;
759 mac = &active_state->newkeys[mode]->mac;
760 comp = &active_state->newkeys[mode]->comp;
761 if (mac_init(mac) == 0)
763 DBG(debug("cipher_init_context: %d", mode));
764 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
765 enc->iv, enc->block_size, crypt_type);
766 /* Deleting the keys does not gain extra security */
767 /* memset(enc->iv, 0, enc->block_size);
768 memset(enc->key, 0, enc->key_len);
769 memset(mac->key, 0, mac->key_len); */
770 if ((comp->type == COMP_ZLIB ||
771 (comp->type == COMP_DELAYED &&
772 active_state->after_authentication)) && comp->enabled == 0) {
773 packet_init_compression();
774 if (mode == MODE_OUT)
775 buffer_compress_init_send(6);
777 buffer_compress_init_recv();
781 * The 2^(blocksize*2) limit is too expensive for 3DES,
782 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
784 if (enc->block_size >= 16)
785 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
787 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
788 if (active_state->rekey_limit)
789 *max_blocks = MIN(*max_blocks,
790 active_state->rekey_limit / enc->block_size);
794 * Delayed compression for SSH2 is enabled after authentication:
795 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
796 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
799 packet_enable_delayed_compress(void)
805 * Remember that we are past the authentication step, so rekeying
806 * with COMP_DELAYED will turn on compression immediately.
808 active_state->after_authentication = 1;
809 for (mode = 0; mode < MODE_MAX; mode++) {
810 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
811 if (active_state->newkeys[mode] == NULL)
813 comp = &active_state->newkeys[mode]->comp;
814 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
815 packet_init_compression();
816 if (mode == MODE_OUT)
817 buffer_compress_init_send(6);
819 buffer_compress_init_recv();
826 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
829 packet_send2_wrapped(void)
831 u_char type, *cp, *macbuf = NULL;
833 u_int packet_length = 0;
841 if (active_state->newkeys[MODE_OUT] != NULL) {
842 enc = &active_state->newkeys[MODE_OUT]->enc;
843 mac = &active_state->newkeys[MODE_OUT]->mac;
844 comp = &active_state->newkeys[MODE_OUT]->comp;
846 block_size = enc ? enc->block_size : 8;
848 cp = buffer_ptr(&active_state->outgoing_packet);
852 fprintf(stderr, "plain: ");
853 buffer_dump(&active_state->outgoing_packet);
856 if (comp && comp->enabled) {
857 len = buffer_len(&active_state->outgoing_packet);
858 /* skip header, compress only payload */
859 buffer_consume(&active_state->outgoing_packet, 5);
860 buffer_clear(&active_state->compression_buffer);
861 buffer_compress(&active_state->outgoing_packet,
862 &active_state->compression_buffer);
863 buffer_clear(&active_state->outgoing_packet);
864 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
865 buffer_append(&active_state->outgoing_packet,
866 buffer_ptr(&active_state->compression_buffer),
867 buffer_len(&active_state->compression_buffer));
868 DBG(debug("compression: raw %d compressed %d", len,
869 buffer_len(&active_state->outgoing_packet)));
872 /* sizeof (packet_len + pad_len + payload) */
873 len = buffer_len(&active_state->outgoing_packet);
876 * calc size of padding, alloc space, get random data,
877 * minimum padding is 4 bytes
879 padlen = block_size - (len % block_size);
881 padlen += block_size;
882 if (active_state->extra_pad) {
883 /* will wrap if extra_pad+padlen > 255 */
884 active_state->extra_pad =
885 roundup(active_state->extra_pad, block_size);
886 pad = active_state->extra_pad -
887 ((len + padlen) % active_state->extra_pad);
888 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
889 pad, len, padlen, active_state->extra_pad);
891 active_state->extra_pad = 0;
893 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
894 if (enc && !active_state->send_context.plaintext) {
896 for (i = 0; i < padlen; i++) {
904 memset(cp, 0, padlen);
906 /* packet_length includes payload, padding and padding length field */
907 packet_length = buffer_len(&active_state->outgoing_packet) - 4;
908 cp = buffer_ptr(&active_state->outgoing_packet);
909 put_u32(cp, packet_length);
911 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
913 /* compute MAC over seqnr and packet(length fields, payload, padding) */
914 if (mac && mac->enabled) {
915 macbuf = mac_compute(mac, active_state->p_send.seqnr,
916 buffer_ptr(&active_state->outgoing_packet),
917 buffer_len(&active_state->outgoing_packet));
918 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
920 /* encrypt packet and append to output buffer. */
921 cp = buffer_append_space(&active_state->output,
922 buffer_len(&active_state->outgoing_packet));
923 cipher_crypt(&active_state->send_context, cp,
924 buffer_ptr(&active_state->outgoing_packet),
925 buffer_len(&active_state->outgoing_packet));
926 /* append unencrypted MAC */
927 if (mac && mac->enabled)
928 buffer_append(&active_state->output, macbuf, mac->mac_len);
930 fprintf(stderr, "encrypted: ");
931 buffer_dump(&active_state->output);
933 /* increment sequence number for outgoing packets */
934 if (++active_state->p_send.seqnr == 0)
935 logit("outgoing seqnr wraps around");
936 if (++active_state->p_send.packets == 0)
937 if (!(datafellows & SSH_BUG_NOREKEY))
938 fatal("XXX too many packets with same key");
939 active_state->p_send.blocks += (packet_length + 4) / block_size;
940 active_state->p_send.bytes += packet_length + 4;
941 buffer_clear(&active_state->outgoing_packet);
943 if (type == SSH2_MSG_NEWKEYS)
944 set_newkeys(MODE_OUT);
945 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
946 packet_enable_delayed_compress();
952 static int rekeying = 0;
956 cp = buffer_ptr(&active_state->outgoing_packet);
959 /* during rekeying we can only send key exchange messages */
961 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
962 (type <= SSH2_MSG_TRANSPORT_MAX))) {
963 debug("enqueue packet: %u", type);
964 p = xmalloc(sizeof(*p));
966 memcpy(&p->payload, &active_state->outgoing_packet,
968 buffer_init(&active_state->outgoing_packet);
969 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
974 /* rekeying starts with sending KEXINIT */
975 if (type == SSH2_MSG_KEXINIT)
978 packet_send2_wrapped();
980 /* after a NEWKEYS message we can send the complete queue */
981 if (type == SSH2_MSG_NEWKEYS) {
983 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
985 debug("dequeue packet: %u", type);
986 buffer_free(&active_state->outgoing_packet);
987 memcpy(&active_state->outgoing_packet, &p->payload,
989 TAILQ_REMOVE(&active_state->outgoing, p, next);
991 packet_send2_wrapped();
1003 DBG(debug("packet_send done"));
1007 * Waits until a packet has been received, and returns its type. Note that
1008 * no other data is processed until this returns, so this function should not
1009 * be used during the interactive session.
1013 packet_read_seqnr(u_int32_t *seqnr_p)
1015 int type, len, ret, ms_remain;
1018 struct timeval timeout, start, *timeoutp = NULL;
1020 DBG(debug("packet_read()"));
1022 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1023 NFDBITS), sizeof(fd_mask));
1025 /* Since we are blocking, ensure that all written packets have been sent. */
1026 packet_write_wait();
1028 /* Stay in the loop until we have received a complete packet. */
1030 /* Try to read a packet from the buffer. */
1031 type = packet_read_poll_seqnr(seqnr_p);
1033 type == SSH_SMSG_SUCCESS
1034 || type == SSH_SMSG_FAILURE
1035 || type == SSH_CMSG_EOF
1036 || type == SSH_CMSG_EXIT_CONFIRMATION))
1038 /* If we got a packet, return it. */
1039 if (type != SSH_MSG_NONE) {
1044 * Otherwise, wait for some data to arrive, add it to the
1045 * buffer, and try again.
1047 memset(setp, 0, howmany(active_state->connection_in + 1,
1048 NFDBITS) * sizeof(fd_mask));
1049 FD_SET(active_state->connection_in, setp);
1051 if (active_state->packet_timeout_ms > 0) {
1052 ms_remain = active_state->packet_timeout_ms;
1053 timeoutp = &timeout;
1055 /* Wait for some data to arrive. */
1057 if (active_state->packet_timeout_ms != -1) {
1058 ms_to_timeval(&timeout, ms_remain);
1059 gettimeofday(&start, NULL);
1061 if ((ret = select(active_state->connection_in + 1, setp,
1062 NULL, NULL, timeoutp)) >= 0)
1064 if (errno != EAGAIN && errno != EINTR &&
1065 errno != EWOULDBLOCK)
1067 if (active_state->packet_timeout_ms == -1)
1069 ms_subtract_diff(&start, &ms_remain);
1070 if (ms_remain <= 0) {
1076 logit("Connection to %.200s timed out while "
1077 "waiting to read", get_remote_ipaddr());
1080 /* Read data from the socket. */
1081 len = read(active_state->connection_in, buf, sizeof(buf));
1083 logit("Connection closed by %.200s", get_remote_ipaddr());
1087 fatal("Read from socket failed: %.100s", strerror(errno));
1088 /* Append it to the buffer. */
1089 packet_process_incoming(buf, len);
1097 return packet_read_seqnr(NULL);
1101 * Waits until a packet has been received, verifies that its type matches
1102 * that given, and gives a fatal error and exits if there is a mismatch.
1106 packet_read_expect(int expected_type)
1110 type = packet_read();
1111 if (type != expected_type)
1112 packet_disconnect("Protocol error: expected packet type %d, got %d",
1113 expected_type, type);
1116 /* Checks if a full packet is available in the data received so far via
1117 * packet_process_incoming. If so, reads the packet; otherwise returns
1118 * SSH_MSG_NONE. This does not wait for data from the connection.
1120 * SSH_MSG_DISCONNECT is handled specially here. Also,
1121 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1126 packet_read_poll1(void)
1128 u_int len, padded_len;
1130 u_int checksum, stored_checksum;
1132 /* Check if input size is less than minimum packet size. */
1133 if (buffer_len(&active_state->input) < 4 + 8)
1134 return SSH_MSG_NONE;
1135 /* Get length of incoming packet. */
1136 cp = buffer_ptr(&active_state->input);
1138 if (len < 1 + 2 + 2 || len > 256 * 1024)
1139 packet_disconnect("Bad packet length %u.", len);
1140 padded_len = (len + 8) & ~7;
1142 /* Check if the packet has been entirely received. */
1143 if (buffer_len(&active_state->input) < 4 + padded_len)
1144 return SSH_MSG_NONE;
1146 /* The entire packet is in buffer. */
1148 /* Consume packet length. */
1149 buffer_consume(&active_state->input, 4);
1152 * Cryptographic attack detector for ssh
1153 * (C)1998 CORE-SDI, Buenos Aires Argentina
1154 * Ariel Futoransky(futo@core-sdi.com)
1156 if (!active_state->receive_context.plaintext) {
1157 switch (detect_attack(buffer_ptr(&active_state->input),
1159 case DEATTACK_DETECTED:
1160 packet_disconnect("crc32 compensation attack: "
1161 "network attack detected");
1162 case DEATTACK_DOS_DETECTED:
1163 packet_disconnect("deattack denial of "
1164 "service detected");
1168 /* Decrypt data to incoming_packet. */
1169 buffer_clear(&active_state->incoming_packet);
1170 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1171 cipher_crypt(&active_state->receive_context, cp,
1172 buffer_ptr(&active_state->input), padded_len);
1174 buffer_consume(&active_state->input, padded_len);
1177 fprintf(stderr, "read_poll plain: ");
1178 buffer_dump(&active_state->incoming_packet);
1181 /* Compute packet checksum. */
1182 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1183 buffer_len(&active_state->incoming_packet) - 4);
1186 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
1188 /* Test check bytes. */
1189 if (len != buffer_len(&active_state->incoming_packet))
1190 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
1191 len, buffer_len(&active_state->incoming_packet));
1193 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
1194 stored_checksum = get_u32(cp);
1195 if (checksum != stored_checksum)
1196 packet_disconnect("Corrupted check bytes on input.");
1197 buffer_consume_end(&active_state->incoming_packet, 4);
1199 if (active_state->packet_compression) {
1200 buffer_clear(&active_state->compression_buffer);
1201 buffer_uncompress(&active_state->incoming_packet,
1202 &active_state->compression_buffer);
1203 buffer_clear(&active_state->incoming_packet);
1204 buffer_append(&active_state->incoming_packet,
1205 buffer_ptr(&active_state->compression_buffer),
1206 buffer_len(&active_state->compression_buffer));
1208 active_state->p_read.packets++;
1209 active_state->p_read.bytes += padded_len + 4;
1210 type = buffer_get_char(&active_state->incoming_packet);
1211 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1212 packet_disconnect("Invalid ssh1 packet type: %d", type);
1217 packet_read_poll2(u_int32_t *seqnr_p)
1220 u_char *macbuf, *cp, type;
1221 u_int maclen, block_size;
1226 if (active_state->packet_discard)
1227 return SSH_MSG_NONE;
1229 if (active_state->newkeys[MODE_IN] != NULL) {
1230 enc = &active_state->newkeys[MODE_IN]->enc;
1231 mac = &active_state->newkeys[MODE_IN]->mac;
1232 comp = &active_state->newkeys[MODE_IN]->comp;
1234 maclen = mac && mac->enabled ? mac->mac_len : 0;
1235 block_size = enc ? enc->block_size : 8;
1237 if (active_state->packlen == 0) {
1239 * check if input size is less than the cipher block size,
1240 * decrypt first block and extract length of incoming packet
1242 if (buffer_len(&active_state->input) < block_size)
1243 return SSH_MSG_NONE;
1244 buffer_clear(&active_state->incoming_packet);
1245 cp = buffer_append_space(&active_state->incoming_packet,
1247 cipher_crypt(&active_state->receive_context, cp,
1248 buffer_ptr(&active_state->input), block_size);
1249 cp = buffer_ptr(&active_state->incoming_packet);
1250 active_state->packlen = get_u32(cp);
1251 if (active_state->packlen < 1 + 4 ||
1252 active_state->packlen > PACKET_MAX_SIZE) {
1254 buffer_dump(&active_state->incoming_packet);
1256 logit("Bad packet length %u.", active_state->packlen);
1257 packet_start_discard(enc, mac, active_state->packlen,
1259 return SSH_MSG_NONE;
1261 DBG(debug("input: packet len %u", active_state->packlen+4));
1262 buffer_consume(&active_state->input, block_size);
1264 /* we have a partial packet of block_size bytes */
1265 need = 4 + active_state->packlen - block_size;
1266 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1268 if (need % block_size != 0) {
1269 logit("padding error: need %d block %d mod %d",
1270 need, block_size, need % block_size);
1271 packet_start_discard(enc, mac, active_state->packlen,
1272 PACKET_MAX_SIZE - block_size);
1273 return SSH_MSG_NONE;
1276 * check if the entire packet has been received and
1277 * decrypt into incoming_packet
1279 if (buffer_len(&active_state->input) < need + maclen)
1280 return SSH_MSG_NONE;
1282 fprintf(stderr, "read_poll enc/full: ");
1283 buffer_dump(&active_state->input);
1285 cp = buffer_append_space(&active_state->incoming_packet, need);
1286 cipher_crypt(&active_state->receive_context, cp,
1287 buffer_ptr(&active_state->input), need);
1288 buffer_consume(&active_state->input, need);
1290 * compute MAC over seqnr and packet,
1291 * increment sequence number for incoming packet
1293 if (mac && mac->enabled) {
1294 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1295 buffer_ptr(&active_state->incoming_packet),
1296 buffer_len(&active_state->incoming_packet));
1297 if (memcmp(macbuf, buffer_ptr(&active_state->input),
1298 mac->mac_len) != 0) {
1299 logit("Corrupted MAC on input.");
1300 if (need > PACKET_MAX_SIZE)
1301 fatal("internal error need %d", need);
1302 packet_start_discard(enc, mac, active_state->packlen,
1303 PACKET_MAX_SIZE - need);
1304 return SSH_MSG_NONE;
1307 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1308 buffer_consume(&active_state->input, mac->mac_len);
1310 /* XXX now it's safe to use fatal/packet_disconnect */
1311 if (seqnr_p != NULL)
1312 *seqnr_p = active_state->p_read.seqnr;
1313 if (++active_state->p_read.seqnr == 0)
1314 logit("incoming seqnr wraps around");
1315 if (++active_state->p_read.packets == 0)
1316 if (!(datafellows & SSH_BUG_NOREKEY))
1317 fatal("XXX too many packets with same key");
1318 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1319 active_state->p_read.bytes += active_state->packlen + 4;
1322 cp = buffer_ptr(&active_state->incoming_packet);
1324 DBG(debug("input: padlen %d", padlen));
1326 packet_disconnect("Corrupted padlen %d on input.", padlen);
1328 /* skip packet size + padlen, discard padding */
1329 buffer_consume(&active_state->incoming_packet, 4 + 1);
1330 buffer_consume_end(&active_state->incoming_packet, padlen);
1332 DBG(debug("input: len before de-compress %d",
1333 buffer_len(&active_state->incoming_packet)));
1334 if (comp && comp->enabled) {
1335 buffer_clear(&active_state->compression_buffer);
1336 buffer_uncompress(&active_state->incoming_packet,
1337 &active_state->compression_buffer);
1338 buffer_clear(&active_state->incoming_packet);
1339 buffer_append(&active_state->incoming_packet,
1340 buffer_ptr(&active_state->compression_buffer),
1341 buffer_len(&active_state->compression_buffer));
1342 DBG(debug("input: len after de-compress %d",
1343 buffer_len(&active_state->incoming_packet)));
1346 * get packet type, implies consume.
1347 * return length of payload (without type field)
1349 type = buffer_get_char(&active_state->incoming_packet);
1350 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1351 packet_disconnect("Invalid ssh2 packet type: %d", type);
1352 if (type == SSH2_MSG_NEWKEYS)
1353 set_newkeys(MODE_IN);
1354 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1355 !active_state->server_side)
1356 packet_enable_delayed_compress();
1358 fprintf(stderr, "read/plain[%d]:\r\n", type);
1359 buffer_dump(&active_state->incoming_packet);
1361 /* reset for next packet */
1362 active_state->packlen = 0;
1367 packet_read_poll_seqnr(u_int32_t *seqnr_p)
1369 u_int reason, seqnr;
1375 type = packet_read_poll2(seqnr_p);
1377 active_state->keep_alive_timeouts = 0;
1378 DBG(debug("received packet type %d", type));
1381 case SSH2_MSG_IGNORE:
1382 debug3("Received SSH2_MSG_IGNORE");
1384 case SSH2_MSG_DEBUG:
1386 msg = packet_get_string(NULL);
1387 debug("Remote: %.900s", msg);
1389 msg = packet_get_string(NULL);
1392 case SSH2_MSG_DISCONNECT:
1393 reason = packet_get_int();
1394 msg = packet_get_string(NULL);
1395 logit("Received disconnect from %s: %u: %.400s",
1396 get_remote_ipaddr(), reason, msg);
1400 case SSH2_MSG_UNIMPLEMENTED:
1401 seqnr = packet_get_int();
1402 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1409 type = packet_read_poll1();
1411 case SSH_MSG_IGNORE:
1414 msg = packet_get_string(NULL);
1415 debug("Remote: %.900s", msg);
1418 case SSH_MSG_DISCONNECT:
1419 msg = packet_get_string(NULL);
1420 logit("Received disconnect from %s: %.400s",
1421 get_remote_ipaddr(), msg);
1426 DBG(debug("received packet type %d", type));
1434 packet_read_poll(void)
1436 return packet_read_poll_seqnr(NULL);
1440 * Buffers the given amount of input characters. This is intended to be used
1441 * together with packet_read_poll.
1445 packet_process_incoming(const char *buf, u_int len)
1447 if (active_state->packet_discard) {
1448 active_state->keep_alive_timeouts = 0; /* ?? */
1449 if (len >= active_state->packet_discard)
1450 packet_stop_discard();
1451 active_state->packet_discard -= len;
1454 buffer_append(&active_state->input, buf, len);
1457 /* Returns a character from the packet. */
1460 packet_get_char(void)
1464 buffer_get(&active_state->incoming_packet, &ch, 1);
1468 /* Returns an integer from the packet data. */
1471 packet_get_int(void)
1473 return buffer_get_int(&active_state->incoming_packet);
1476 /* Returns an 64 bit integer from the packet data. */
1479 packet_get_int64(void)
1481 return buffer_get_int64(&active_state->incoming_packet);
1485 * Returns an arbitrary precision integer from the packet data. The integer
1486 * must have been initialized before this call.
1490 packet_get_bignum(BIGNUM * value)
1492 buffer_get_bignum(&active_state->incoming_packet, value);
1496 packet_get_bignum2(BIGNUM * value)
1498 buffer_get_bignum2(&active_state->incoming_packet, value);
1502 packet_get_raw(u_int *length_ptr)
1504 u_int bytes = buffer_len(&active_state->incoming_packet);
1506 if (length_ptr != NULL)
1507 *length_ptr = bytes;
1508 return buffer_ptr(&active_state->incoming_packet);
1512 packet_remaining(void)
1514 return buffer_len(&active_state->incoming_packet);
1518 * Returns a string from the packet data. The string is allocated using
1519 * xmalloc; it is the responsibility of the calling program to free it when
1520 * no longer needed. The length_ptr argument may be NULL, or point to an
1521 * integer into which the length of the string is stored.
1525 packet_get_string(u_int *length_ptr)
1527 return buffer_get_string(&active_state->incoming_packet, length_ptr);
1531 packet_get_string_ptr(u_int *length_ptr)
1533 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
1537 * Sends a diagnostic message from the server to the client. This message
1538 * can be sent at any time (but not while constructing another message). The
1539 * message is printed immediately, but only if the client is being executed
1540 * in verbose mode. These messages are primarily intended to ease debugging
1541 * authentication problems. The length of the formatted message must not
1542 * exceed 1024 bytes. This will automatically call packet_write_wait.
1546 packet_send_debug(const char *fmt,...)
1551 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1554 va_start(args, fmt);
1555 vsnprintf(buf, sizeof(buf), fmt, args);
1559 packet_start(SSH2_MSG_DEBUG);
1560 packet_put_char(0); /* bool: always display */
1561 packet_put_cstring(buf);
1562 packet_put_cstring("");
1564 packet_start(SSH_MSG_DEBUG);
1565 packet_put_cstring(buf);
1568 packet_write_wait();
1572 * Logs the error plus constructs and sends a disconnect packet, closes the
1573 * connection, and exits. This function never returns. The error message
1574 * should not contain a newline. The length of the formatted message must
1575 * not exceed 1024 bytes.
1579 packet_disconnect(const char *fmt,...)
1583 static int disconnecting = 0;
1585 if (disconnecting) /* Guard against recursive invocations. */
1586 fatal("packet_disconnect called recursively.");
1590 * Format the message. Note that the caller must make sure the
1591 * message is of limited size.
1593 va_start(args, fmt);
1594 vsnprintf(buf, sizeof(buf), fmt, args);
1597 /* Display the error locally */
1598 logit("Disconnecting: %.100s", buf);
1600 /* Send the disconnect message to the other side, and wait for it to get sent. */
1602 packet_start(SSH2_MSG_DISCONNECT);
1603 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1604 packet_put_cstring(buf);
1605 packet_put_cstring("");
1607 packet_start(SSH_MSG_DISCONNECT);
1608 packet_put_cstring(buf);
1611 packet_write_wait();
1613 /* Stop listening for connections. */
1614 channel_close_all();
1616 /* Close the connection. */
1621 /* Checks if there is any buffered output, and tries to write some of the output. */
1624 packet_write_poll(void)
1626 int len = buffer_len(&active_state->output);
1629 len = write(active_state->connection_out,
1630 buffer_ptr(&active_state->output), len);
1632 if (errno == EINTR || errno == EAGAIN ||
1633 errno == EWOULDBLOCK)
1635 fatal("Write failed: %.100s", strerror(errno));
1638 fatal("Write connection closed");
1639 buffer_consume(&active_state->output, len);
1645 * Calls packet_write_poll repeatedly until all pending output data has been
1650 packet_write_wait(void)
1654 struct timeval start, timeout, *timeoutp = NULL;
1656 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1657 NFDBITS), sizeof(fd_mask));
1658 packet_write_poll();
1659 while (packet_have_data_to_write()) {
1660 memset(setp, 0, howmany(active_state->connection_out + 1,
1661 NFDBITS) * sizeof(fd_mask));
1662 FD_SET(active_state->connection_out, setp);
1664 if (active_state->packet_timeout_ms > 0) {
1665 ms_remain = active_state->packet_timeout_ms;
1666 timeoutp = &timeout;
1669 if (active_state->packet_timeout_ms != -1) {
1670 ms_to_timeval(&timeout, ms_remain);
1671 gettimeofday(&start, NULL);
1673 if ((ret = select(active_state->connection_out + 1,
1674 NULL, setp, NULL, timeoutp)) >= 0)
1676 if (errno != EAGAIN && errno != EINTR &&
1677 errno != EWOULDBLOCK)
1679 if (active_state->packet_timeout_ms == -1)
1681 ms_subtract_diff(&start, &ms_remain);
1682 if (ms_remain <= 0) {
1688 logit("Connection to %.200s timed out while "
1689 "waiting to write", get_remote_ipaddr());
1692 packet_write_poll();
1697 /* Returns true if there is buffered data to write to the connection. */
1700 packet_have_data_to_write(void)
1702 return buffer_len(&active_state->output) != 0;
1705 /* Returns true if there is not too much data to write to the connection. */
1708 packet_not_very_much_data_to_write(void)
1710 if (active_state->interactive_mode)
1711 return buffer_len(&active_state->output) < 16384;
1713 return buffer_len(&active_state->output) < 128 * 1024;
1718 packet_set_tos(int interactive)
1720 #if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
1721 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1723 if (!packet_connection_is_on_socket() ||
1724 !packet_connection_is_ipv4())
1726 if (setsockopt(active_state->connection_in, IPPROTO_IP, IP_TOS, &tos,
1728 error("setsockopt IP_TOS %d: %.100s:",
1729 tos, strerror(errno));
1733 /* Informs that the current session is interactive. Sets IP flags for that. */
1736 packet_set_interactive(int interactive)
1738 static int called = 0;
1744 /* Record that we are in interactive mode. */
1745 active_state->interactive_mode = interactive;
1747 /* Only set socket options if using a socket. */
1748 if (!packet_connection_is_on_socket())
1750 set_nodelay(active_state->connection_in);
1751 packet_set_tos(interactive);
1754 /* Returns true if the current connection is interactive. */
1757 packet_is_interactive(void)
1759 return active_state->interactive_mode;
1763 packet_set_maxsize(u_int s)
1765 static int called = 0;
1768 logit("packet_set_maxsize: called twice: old %d new %d",
1769 active_state->max_packet_size, s);
1772 if (s < 4 * 1024 || s > 1024 * 1024) {
1773 logit("packet_set_maxsize: bad size %d", s);
1777 debug("packet_set_maxsize: setting to %d", s);
1778 active_state->max_packet_size = s;
1783 packet_inc_alive_timeouts(void)
1785 return ++active_state->keep_alive_timeouts;
1789 packet_set_alive_timeouts(int ka)
1791 active_state->keep_alive_timeouts = ka;
1795 packet_get_maxsize(void)
1797 return active_state->max_packet_size;
1800 /* roundup current message to pad bytes */
1802 packet_add_padding(u_char pad)
1804 active_state->extra_pad = pad;
1808 * 9.2. Ignored Data Message
1810 * byte SSH_MSG_IGNORE
1813 * All implementations MUST understand (and ignore) this message at any
1814 * time (after receiving the protocol version). No implementation is
1815 * required to send them. This message can be used as an additional
1816 * protection measure against advanced traffic analysis techniques.
1819 packet_send_ignore(int nbytes)
1824 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
1825 packet_put_int(nbytes);
1826 for (i = 0; i < nbytes; i++) {
1829 packet_put_char((u_char)rnd & 0xff);
1834 #define MAX_PACKETS (1U<<31)
1836 packet_need_rekeying(void)
1838 if (datafellows & SSH_BUG_NOREKEY)
1841 (active_state->p_send.packets > MAX_PACKETS) ||
1842 (active_state->p_read.packets > MAX_PACKETS) ||
1843 (active_state->max_blocks_out &&
1844 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1845 (active_state->max_blocks_in &&
1846 (active_state->p_read.blocks > active_state->max_blocks_in));
1850 packet_set_rekey_limit(u_int32_t bytes)
1852 active_state->rekey_limit = bytes;
1856 packet_set_server(void)
1858 active_state->server_side = 1;
1862 packet_set_authenticated(void)
1864 active_state->after_authentication = 1;
1868 packet_get_input(void)
1870 return (void *)&active_state->input;
1874 packet_get_output(void)
1876 return (void *)&active_state->output;
1880 packet_get_newkeys(int mode)
1882 return (void *)active_state->newkeys[mode];