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