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