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