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