]> andersk Git - openssh.git/blame - packet.c
- markus@cvs.openbsd.org 2001/04/03 23:32:12
[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.
bcbf86ec 16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
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"
d1ac6175 40RCSID("$OpenBSD: packet.c,v 1.57 2001/04/03 23:32:12 markus Exp $");
8efc0c15 41
42#include "xmalloc.h"
43#include "buffer.h"
44#include "packet.h"
45#include "bufaux.h"
8efc0c15 46#include "crc32.h"
8efc0c15 47#include "getput.h"
48
49#include "compress.h"
50#include "deattack.h"
7368a6c8 51#include "channels.h"
8efc0c15 52
7e7327a1 53#include "compat.h"
42f11eb2 54#include "ssh1.h"
7e7327a1 55#include "ssh2.h"
56
94ec8c6b 57#include "cipher.h"
7e7327a1 58#include "kex.h"
b2552997 59#include "mac.h"
42f11eb2 60#include "log.h"
61#include "canohost.h"
7e7327a1 62
63#ifdef PACKET_DEBUG
64#define DBG(x) x
65#else
66#define DBG(x)
67#endif
68
aa3378df 69/*
70 * This variable contains the file descriptors used for communicating with
71 * the other side. connection_in is used for reading; connection_out for
72 * writing. These can be the same descriptor, in which case it is assumed to
73 * be a socket.
74 */
8efc0c15 75static int connection_in = -1;
76static int connection_out = -1;
77
aa3378df 78/*
79 * Cipher type. This value is only used to determine whether to pad the
80 * packets with zeroes or random data.
81 */
8efc0c15 82static int cipher_type = SSH_CIPHER_NONE;
83
84/* Protocol flags for the remote side. */
1e3b8b07 85static u_int remote_protocol_flags = 0;
8efc0c15 86
87/* Encryption context for receiving data. This is only used for decryption. */
88static CipherContext receive_context;
5260325f 89
90/* Encryption context for sending data. This is only used for encryption. */
8efc0c15 91static CipherContext send_context;
92
93/* Buffer for raw input data from the socket. */
94static Buffer input;
95
96/* Buffer for raw output data going to the socket. */
97static Buffer output;
98
99/* Buffer for the partial outgoing packet being constructed. */
100static Buffer outgoing_packet;
101
102/* Buffer for the incoming packet currently being processed. */
103static Buffer incoming_packet;
104
105/* Scratch buffer for packet compression/decompression. */
106static Buffer compression_buffer;
107
108/* Flag indicating whether packet compression/decompression is enabled. */
109static int packet_compression = 0;
110
9d6b7add 111/* default maximum packet size */
112int max_packet_size = 32768;
113
8efc0c15 114/* Flag indicating whether this module has been initialized. */
115static int initialized = 0;
116
117/* Set to true if the connection is interactive. */
118static int interactive_mode = 0;
119
7e7327a1 120/* True if SSH2 packet format is used */
121int use_ssh2_packet_format = 0;
122
123/* Session key information for Encryption and MAC */
d1ac6175 124Newkeys *newkeys[MODE_MAX];
7e7327a1 125
7e7327a1 126void
127clear_enc_keys(Enc *enc, int len)
128{
129 memset(enc->iv, 0, len);
130 memset(enc->key, 0, len);
131 xfree(enc->iv);
132 xfree(enc->key);
133 enc->iv = NULL;
134 enc->key = NULL;
135}
136void
137packet_set_ssh2_format(void)
138{
3189621b 139 DBG(debug("use_ssh2_packet_format"));
7e7327a1 140 use_ssh2_packet_format = 1;
d1ac6175 141 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
7e7327a1 142}
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");
152 if (none == NULL)
153 fatal("packet_set_connection: cannot load cipher 'none'");
5260325f 154 connection_in = fd_in;
155 connection_out = fd_out;
156 cipher_type = SSH_CIPHER_NONE;
1e3b8b07 157 cipher_init(&send_context, none, (u_char *) "", 0, NULL, 0);
158 cipher_init(&receive_context, none, (u_char *) "", 0, NULL, 0);
5260325f 159 if (!initialized) {
160 initialized = 1;
161 buffer_init(&input);
162 buffer_init(&output);
163 buffer_init(&outgoing_packet);
164 buffer_init(&incoming_packet);
165 }
166 /* Kludge: arrange the close function to be called from fatal(). */
167 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
8efc0c15 168}
169
48e671d5 170/* Returns 1 if remote host is connected via socket, 0 if not. */
171
172int
173packet_connection_is_on_socket()
174{
175 struct sockaddr_storage from, to;
176 socklen_t fromlen, tolen;
177
178 /* filedescriptors in and out are the same, so it's a socket */
179 if (connection_in == connection_out)
180 return 1;
181 fromlen = sizeof(from);
182 memset(&from, 0, sizeof(from));
9bc5ddfe 183 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
48e671d5 184 return 0;
185 tolen = sizeof(to);
186 memset(&to, 0, sizeof(to));
9bc5ddfe 187 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
48e671d5 188 return 0;
189 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
190 return 0;
191 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
192 return 0;
193 return 1;
194}
195
196/* returns 1 if connection is via ipv4 */
197
198int
199packet_connection_is_ipv4()
200{
201 struct sockaddr_storage to;
d45317d8 202 socklen_t tolen = sizeof(to);
48e671d5 203
204 memset(&to, 0, sizeof(to));
205 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
206 return 0;
207 if (to.ss_family != AF_INET)
208 return 0;
209 return 1;
210}
211
8efc0c15 212/* Sets the connection into non-blocking mode. */
213
214void
215packet_set_nonblocking()
216{
5260325f 217 /* Set the socket into non-blocking mode. */
218 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
219 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
8efc0c15 220
5260325f 221 if (connection_out != connection_in) {
222 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
223 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
224 }
8efc0c15 225}
226
227/* Returns the socket used for reading. */
228
229int
230packet_get_connection_in()
231{
5260325f 232 return connection_in;
8efc0c15 233}
234
235/* Returns the descriptor used for writing. */
236
237int
238packet_get_connection_out()
239{
5260325f 240 return connection_out;
8efc0c15 241}
242
243/* Closes the connection and clears and frees internal data structures. */
244
245void
246packet_close()
247{
5260325f 248 if (!initialized)
249 return;
250 initialized = 0;
251 if (connection_in == connection_out) {
252 shutdown(connection_out, SHUT_RDWR);
253 close(connection_out);
254 } else {
255 close(connection_in);
256 close(connection_out);
257 }
258 buffer_free(&input);
259 buffer_free(&output);
260 buffer_free(&outgoing_packet);
261 buffer_free(&incoming_packet);
262 if (packet_compression) {
263 buffer_free(&compression_buffer);
264 buffer_compress_uninit();
265 }
8efc0c15 266}
267
268/* Sets remote side protocol flags. */
269
270void
1e3b8b07 271packet_set_protocol_flags(u_int protocol_flags)
8efc0c15 272{
5260325f 273 remote_protocol_flags = protocol_flags;
274 channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0);
8efc0c15 275}
276
277/* Returns the remote protocol flags set earlier by the above function. */
278
1e3b8b07 279u_int
8efc0c15 280packet_get_protocol_flags()
281{
5260325f 282 return remote_protocol_flags;
8efc0c15 283}
284
aa3378df 285/*
286 * Starts packet compression from the next packet on in both directions.
287 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
288 */
8efc0c15 289
7e7327a1 290/*** XXXXX todo: kex means re-init */
8efc0c15 291void
292packet_start_compression(int level)
293{
5260325f 294 if (packet_compression)
295 fatal("Compression already enabled.");
296 packet_compression = 1;
297 buffer_init(&compression_buffer);
298 buffer_compress_init(level);
8efc0c15 299}
300
aa3378df 301/*
302 * Encrypts the given number of bytes, copying from src to dest. bytes is
303 * known to be a multiple of 8.
304 */
8efc0c15 305
306void
5260325f 307packet_encrypt(CipherContext * cc, void *dest, void *src,
1e3b8b07 308 u_int bytes)
8efc0c15 309{
5260325f 310 cipher_encrypt(cc, dest, src, bytes);
8efc0c15 311}
312
aa3378df 313/*
314 * Decrypts the given number of bytes, copying from src to dest. bytes is
315 * known to be a multiple of 8.
316 */
8efc0c15 317
318void
1e3b8b07 319packet_decrypt(CipherContext *context, void *dest, void *src, u_int bytes)
8efc0c15 320{
aa3378df 321 /*
322 * Cryptographic attack detector for ssh - Modifications for packet.c
323 * (C)1998 CORE-SDI, Buenos Aires Argentina Ariel Futoransky(futo@core-sdi.com)
324 */
94ec8c6b 325 if (!compat20 &&
326 context->cipher->number != SSH_CIPHER_NONE &&
327 detect_attack(src, bytes, NULL) == DEATTACK_DETECTED)
5260325f 328 packet_disconnect("crc32 compensation attack: network attack detected");
329
94ec8c6b 330 cipher_decrypt(context, dest, src, bytes);
8efc0c15 331}
332
aa3378df 333/*
334 * Causes any further packets to be encrypted using the given key. The same
335 * key is used for both sending and reception. However, both directions are
336 * encrypted independently of each other.
337 */
8efc0c15 338
339void
1e3b8b07 340packet_set_encryption_key(const u_char *key, u_int keylen,
94ec8c6b 341 int number)
8efc0c15 342{
94ec8c6b 343 Cipher *cipher = cipher_by_number(number);
344 if (cipher == NULL)
345 fatal("packet_set_encryption_key: unknown cipher number %d", number);
7e7327a1 346 if (keylen < 20)
94ec8c6b 347 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
348 cipher_init(&receive_context, cipher, key, keylen, NULL, 0);
349 cipher_init(&send_context, cipher, key, keylen, NULL, 0);
8efc0c15 350}
351
352/* Starts constructing a packet to send. */
353
354void
7e7327a1 355packet_start1(int type)
8efc0c15 356{
5260325f 357 char buf[9];
8efc0c15 358
5260325f 359 buffer_clear(&outgoing_packet);
360 memset(buf, 0, 8);
361 buf[8] = type;
362 buffer_append(&outgoing_packet, buf, 9);
8efc0c15 363}
364
7e7327a1 365void
366packet_start2(int type)
367{
368 char buf[4+1+1];
369
370 buffer_clear(&outgoing_packet);
371 memset(buf, 0, sizeof buf);
372 /* buf[0..3] = payload_len; */
373 /* buf[4] = pad_len; */
374 buf[5] = type & 0xff;
375 buffer_append(&outgoing_packet, buf, sizeof buf);
376}
377
378void
379packet_start(int type)
380{
12bf85ed 381 DBG(debug("packet_start[%d]", type));
7e7327a1 382 if (use_ssh2_packet_format)
383 packet_start2(type);
384 else
385 packet_start1(type);
386}
387
8efc0c15 388/* Appends a character to the packet data. */
389
390void
391packet_put_char(int value)
392{
5260325f 393 char ch = value;
394 buffer_append(&outgoing_packet, &ch, 1);
8efc0c15 395}
396
397/* Appends an integer to the packet data. */
398
399void
1e3b8b07 400packet_put_int(u_int value)
8efc0c15 401{
5260325f 402 buffer_put_int(&outgoing_packet, value);
8efc0c15 403}
404
405/* Appends a string to packet data. */
406
407void
1e3b8b07 408packet_put_string(const char *buf, u_int len)
8efc0c15 409{
5260325f 410 buffer_put_string(&outgoing_packet, buf, len);
8efc0c15 411}
7e7327a1 412void
413packet_put_cstring(const char *str)
414{
415 buffer_put_string(&outgoing_packet, str, strlen(str));
416}
417
418void
1e3b8b07 419packet_put_raw(const char *buf, u_int len)
7e7327a1 420{
421 buffer_append(&outgoing_packet, buf, len);
422}
423
8efc0c15 424
425/* Appends an arbitrary precision integer to packet data. */
426
427void
5260325f 428packet_put_bignum(BIGNUM * value)
8efc0c15 429{
5260325f 430 buffer_put_bignum(&outgoing_packet, value);
8efc0c15 431}
7e7327a1 432void
433packet_put_bignum2(BIGNUM * value)
434{
435 buffer_put_bignum2(&outgoing_packet, value);
436}
8efc0c15 437
aa3378df 438/*
439 * Finalizes and sends the packet. If the encryption key has been set,
440 * encrypts the packet before sending.
441 */
5260325f 442
8efc0c15 443void
5ca51e19 444packet_send1(void)
8efc0c15 445{
5260325f 446 char buf[8], *cp;
447 int i, padding, len;
1e3b8b07 448 u_int checksum;
5260325f 449 u_int32_t rand = 0;
450
aa3378df 451 /*
452 * If using packet compression, compress the payload of the outgoing
453 * packet.
454 */
5260325f 455 if (packet_compression) {
456 buffer_clear(&compression_buffer);
457 /* Skip padding. */
458 buffer_consume(&outgoing_packet, 8);
459 /* padding */
460 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
461 buffer_compress(&outgoing_packet, &compression_buffer);
462 buffer_clear(&outgoing_packet);
463 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
464 buffer_len(&compression_buffer));
465 }
466 /* Compute packet length without padding (add checksum, remove padding). */
467 len = buffer_len(&outgoing_packet) + 4 - 8;
468
1d1ffb87 469 /* Insert padding. Initialized to zero in packet_start1() */
5260325f 470 padding = 8 - len % 8;
471 if (cipher_type != SSH_CIPHER_NONE) {
472 cp = buffer_ptr(&outgoing_packet);
473 for (i = 0; i < padding; i++) {
474 if (i % 4 == 0)
475 rand = arc4random();
476 cp[7 - i] = rand & 0xff;
477 rand >>= 8;
478 }
479 }
480 buffer_consume(&outgoing_packet, 8 - padding);
481
482 /* Add check bytes. */
1e3b8b07 483 checksum = ssh_crc32((u_char *) buffer_ptr(&outgoing_packet),
2e73a022 484 buffer_len(&outgoing_packet));
5260325f 485 PUT_32BIT(buf, checksum);
486 buffer_append(&outgoing_packet, buf, 4);
8efc0c15 487
488#ifdef PACKET_DEBUG
5260325f 489 fprintf(stderr, "packet_send plain: ");
490 buffer_dump(&outgoing_packet);
8efc0c15 491#endif
492
5260325f 493 /* Append to output. */
494 PUT_32BIT(buf, len);
495 buffer_append(&output, buf, 4);
496 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
497 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
498 buffer_len(&outgoing_packet));
499
8efc0c15 500#ifdef PACKET_DEBUG
5260325f 501 fprintf(stderr, "encrypted: ");
502 buffer_dump(&output);
8efc0c15 503#endif
504
5260325f 505 buffer_clear(&outgoing_packet);
8efc0c15 506
aa3378df 507 /*
508 * Note that the packet is now only buffered in output. It won\'t be
509 * actually sent until packet_write_wait or packet_write_poll is
510 * called.
511 */
8efc0c15 512}
513
d1ac6175 514void
515set_newkeys(int mode)
516{
517 Enc *enc;
518 Mac *mac;
519 Comp *comp;
520 CipherContext *cc;
521
522 debug("newkeys: mode %d", mode);
523
524 cc = (mode == MODE_OUT) ? &send_context : &receive_context;
525 if (newkeys[mode] != NULL) {
526 debug("newkeys: rekeying");
527 memset(cc, 0, sizeof(*cc));
528 // free old keys, reset compression cipher-contexts;
529 }
530 newkeys[mode] = kex_get_newkeys(mode);
531 if (newkeys[mode] == NULL)
532 fatal("newkeys: no keys for mode %d", mode);
533 enc = &newkeys[mode]->enc;
534 mac = &newkeys[mode]->mac;
535 comp = &newkeys[mode]->comp;
536 if (mac->md != NULL)
537 mac->enabled = 1;
538 DBG(debug("cipher_init_context: %d", mode));
539 cipher_init(cc, enc->cipher, enc->key, enc->cipher->key_len,
540 enc->iv, enc->cipher->block_size);
541 clear_enc_keys(enc, enc->cipher->key_len);
542 if (comp->type != 0 && comp->enabled == 0) {
543 comp->enabled = 1;
544 if (! packet_compression)
545 packet_start_compression(6);
546 }
547}
548
7e7327a1 549/*
550 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
551 */
552void
5ca51e19 553packet_send2(void)
7e7327a1 554{
b2552997 555 static u_int32_t seqnr = 0;
1e3b8b07 556 u_char *macbuf = NULL;
7e7327a1 557 char *cp;
1e3b8b07 558 u_int packet_length = 0;
559 u_int i, padlen, len;
7e7327a1 560 u_int32_t rand = 0;
7e7327a1 561 int type;
562 Enc *enc = NULL;
563 Mac *mac = NULL;
564 Comp *comp = NULL;
565 int block_size;
566
d1ac6175 567 if (newkeys[MODE_OUT] != NULL) {
568 enc = &newkeys[MODE_OUT]->enc;
569 mac = &newkeys[MODE_OUT]->mac;
570 comp = &newkeys[MODE_OUT]->comp;
7e7327a1 571 }
94ec8c6b 572 block_size = enc ? enc->cipher->block_size : 8;
7e7327a1 573
574 cp = buffer_ptr(&outgoing_packet);
575 type = cp[5] & 0xff;
576
577#ifdef PACKET_DEBUG
578 fprintf(stderr, "plain: ");
579 buffer_dump(&outgoing_packet);
580#endif
581
582 if (comp && comp->enabled) {
583 len = buffer_len(&outgoing_packet);
584 /* skip header, compress only payload */
585 buffer_consume(&outgoing_packet, 5);
586 buffer_clear(&compression_buffer);
587 buffer_compress(&outgoing_packet, &compression_buffer);
588 buffer_clear(&outgoing_packet);
589 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
590 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
591 buffer_len(&compression_buffer));
592 DBG(debug("compression: raw %d compressed %d", len,
593 buffer_len(&outgoing_packet)));
594 }
595
596 /* sizeof (packet_len + pad_len + payload) */
597 len = buffer_len(&outgoing_packet);
598
599 /*
600 * calc size of padding, alloc space, get random data,
601 * minimum padding is 4 bytes
602 */
603 padlen = block_size - (len % block_size);
604 if (padlen < 4)
605 padlen += block_size;
606 buffer_append_space(&outgoing_packet, &cp, padlen);
94ec8c6b 607 if (enc && enc->cipher->number != SSH_CIPHER_NONE) {
1d1ffb87 608 /* random padding */
7e7327a1 609 for (i = 0; i < padlen; i++) {
610 if (i % 4 == 0)
611 rand = arc4random();
612 cp[i] = rand & 0xff;
6e18cb71 613 rand >>= 8;
7e7327a1 614 }
1d1ffb87 615 } else {
616 /* clear padding */
617 memset(cp, 0, padlen);
7e7327a1 618 }
619 /* packet_length includes payload, padding and padding length field */
620 packet_length = buffer_len(&outgoing_packet) - 4;
621 cp = buffer_ptr(&outgoing_packet);
622 PUT_32BIT(cp, packet_length);
623 cp[4] = padlen & 0xff;
624 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
625
626 /* compute MAC over seqnr and packet(length fields, payload, padding) */
627 if (mac && mac->enabled) {
b2552997 628 macbuf = mac_compute(mac, seqnr,
1e3b8b07 629 (u_char *) buffer_ptr(&outgoing_packet),
b2552997 630 buffer_len(&outgoing_packet));
94ec8c6b 631 DBG(debug("done calc MAC out #%d", seqnr));
7e7327a1 632 }
633 /* encrypt packet and append to output buffer. */
634 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
635 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
636 buffer_len(&outgoing_packet));
637 /* append unencrypted MAC */
638 if (mac && mac->enabled)
639 buffer_append(&output, (char *)macbuf, mac->mac_len);
640#ifdef PACKET_DEBUG
641 fprintf(stderr, "encrypted: ");
642 buffer_dump(&output);
643#endif
6ae2364d 644 /* increment sequence number for outgoing packets */
645 if (++seqnr == 0)
646 log("outgoing seqnr wraps around");
7e7327a1 647 buffer_clear(&outgoing_packet);
648
d1ac6175 649 if (type == SSH2_MSG_NEWKEYS)
650 set_newkeys(MODE_OUT);
7e7327a1 651}
652
653void
654packet_send()
655{
656 if (use_ssh2_packet_format)
657 packet_send2();
658 else
659 packet_send1();
660 DBG(debug("packet_send done"));
661}
662
aa3378df 663/*
664 * Waits until a packet has been received, and returns its type. Note that
665 * no other data is processed until this returns, so this function should not
666 * be used during the interactive session.
667 */
8efc0c15 668
669int
670packet_read(int *payload_len_ptr)
671{
5260325f 672 int type, len;
20e04e90 673 fd_set *setp;
5260325f 674 char buf[8192];
7e7327a1 675 DBG(debug("packet_read()"));
5260325f 676
20e04e90 677 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
678 sizeof(fd_mask));
679
5260325f 680 /* Since we are blocking, ensure that all written packets have been sent. */
681 packet_write_wait();
682
683 /* Stay in the loop until we have received a complete packet. */
684 for (;;) {
685 /* Try to read a packet from the buffer. */
686 type = packet_read_poll(payload_len_ptr);
1d1ffb87 687 if (!use_ssh2_packet_format && (
688 type == SSH_SMSG_SUCCESS
5260325f 689 || type == SSH_SMSG_FAILURE
690 || type == SSH_CMSG_EOF
1d1ffb87 691 || type == SSH_CMSG_EXIT_CONFIRMATION))
5260325f 692 packet_integrity_check(*payload_len_ptr, 0, type);
693 /* If we got a packet, return it. */
20e04e90 694 if (type != SSH_MSG_NONE) {
695 xfree(setp);
5260325f 696 return type;
20e04e90 697 }
aa3378df 698 /*
699 * Otherwise, wait for some data to arrive, add it to the
700 * buffer, and try again.
701 */
20e04e90 702 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
703 sizeof(fd_mask));
704 FD_SET(connection_in, setp);
aa3378df 705
5260325f 706 /* Wait for some data to arrive. */
20e04e90 707 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
fd193ca4 708 (errno == EAGAIN || errno == EINTR))
709 ;
aa3378df 710
5260325f 711 /* Read data from the socket. */
712 len = read(connection_in, buf, sizeof(buf));
89cafde6 713 if (len == 0) {
714 log("Connection closed by %.200s", get_remote_ipaddr());
715 fatal_cleanup();
716 }
5260325f 717 if (len < 0)
718 fatal("Read from socket failed: %.100s", strerror(errno));
719 /* Append it to the buffer. */
720 packet_process_incoming(buf, len);
721 }
722 /* NOTREACHED */
8efc0c15 723}
724
aa3378df 725/*
726 * Waits until a packet has been received, verifies that its type matches
727 * that given, and gives a fatal error and exits if there is a mismatch.
728 */
8efc0c15 729
730void
731packet_read_expect(int *payload_len_ptr, int expected_type)
732{
5260325f 733 int type;
8efc0c15 734
5260325f 735 type = packet_read(payload_len_ptr);
736 if (type != expected_type)
737 packet_disconnect("Protocol error: expected packet type %d, got %d",
7e7327a1 738 expected_type, type);
8efc0c15 739}
740
741/* Checks if a full packet is available in the data received so far via
5260325f 742 * packet_process_incoming. If so, reads the packet; otherwise returns
743 * SSH_MSG_NONE. This does not wait for data from the connection.
744 *
745 * SSH_MSG_DISCONNECT is handled specially here. Also,
746 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
747 * to higher levels.
748 *
749 * The returned payload_len does include space consumed by:
750 * Packet length
751 * Padding
752 * Packet type
753 * Check bytes
754 */
8efc0c15 755
756int
7e7327a1 757packet_read_poll1(int *payload_len_ptr)
8efc0c15 758{
1e3b8b07 759 u_int len, padded_len;
760 u_char *ucp;
7e7327a1 761 char buf[8], *cp;
1e3b8b07 762 u_int checksum, stored_checksum;
5260325f 763
5260325f 764 /* Check if input size is less than minimum packet size. */
765 if (buffer_len(&input) < 4 + 8)
766 return SSH_MSG_NONE;
767 /* Get length of incoming packet. */
1e3b8b07 768 ucp = (u_char *) buffer_ptr(&input);
5260325f 769 len = GET_32BIT(ucp);
770 if (len < 1 + 2 + 2 || len > 256 * 1024)
771 packet_disconnect("Bad packet length %d.", len);
772 padded_len = (len + 8) & ~7;
773
774 /* Check if the packet has been entirely received. */
775 if (buffer_len(&input) < 4 + padded_len)
776 return SSH_MSG_NONE;
777
778 /* The entire packet is in buffer. */
779
780 /* Consume packet length. */
781 buffer_consume(&input, 4);
782
783 /* Copy data to incoming_packet. */
784 buffer_clear(&incoming_packet);
785 buffer_append_space(&incoming_packet, &cp, padded_len);
786 packet_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
787 buffer_consume(&input, padded_len);
8efc0c15 788
789#ifdef PACKET_DEBUG
5260325f 790 fprintf(stderr, "read_poll plain: ");
791 buffer_dump(&incoming_packet);
8efc0c15 792#endif
5260325f 793
794 /* Compute packet checksum. */
1e3b8b07 795 checksum = ssh_crc32((u_char *) buffer_ptr(&incoming_packet),
7e7327a1 796 buffer_len(&incoming_packet) - 4);
5260325f 797
798 /* Skip padding. */
799 buffer_consume(&incoming_packet, 8 - len % 8);
800
801 /* Test check bytes. */
802
803 if (len != buffer_len(&incoming_packet))
804 packet_disconnect("packet_read_poll: len %d != buffer_len %d.",
7e7327a1 805 len, buffer_len(&incoming_packet));
5260325f 806
1e3b8b07 807 ucp = (u_char *) buffer_ptr(&incoming_packet) + len - 4;
5260325f 808 stored_checksum = GET_32BIT(ucp);
809 if (checksum != stored_checksum)
810 packet_disconnect("Corrupted check bytes on input.");
811 buffer_consume_end(&incoming_packet, 4);
812
813 /* If using packet compression, decompress the packet. */
814 if (packet_compression) {
815 buffer_clear(&compression_buffer);
816 buffer_uncompress(&incoming_packet, &compression_buffer);
817 buffer_clear(&incoming_packet);
818 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
7e7327a1 819 buffer_len(&compression_buffer));
5260325f 820 }
821 /* Get packet type. */
822 buffer_get(&incoming_packet, &buf[0], 1);
823
824 /* Return length of payload (without type field). */
825 *payload_len_ptr = buffer_len(&incoming_packet);
826
5260325f 827 /* Return type. */
1e3b8b07 828 return (u_char) buf[0];
5260325f 829}
830
7e7327a1 831int
832packet_read_poll2(int *payload_len_ptr)
833{
b2552997 834 static u_int32_t seqnr = 0;
835 static u_int packet_length = 0;
1e3b8b07 836 u_int padlen, need;
837 u_char buf[8], *macbuf;
838 u_char *ucp;
7e7327a1 839 char *cp;
7e7327a1 840 int type;
841 int maclen, block_size;
842 Enc *enc = NULL;
843 Mac *mac = NULL;
844 Comp *comp = NULL;
845
d1ac6175 846 if (newkeys[MODE_IN] != NULL) {
847 enc = &newkeys[MODE_IN]->enc;
848 mac = &newkeys[MODE_IN]->mac;
849 comp = &newkeys[MODE_IN]->comp;
7e7327a1 850 }
851 maclen = mac && mac->enabled ? mac->mac_len : 0;
94ec8c6b 852 block_size = enc ? enc->cipher->block_size : 8;
7e7327a1 853
854 if (packet_length == 0) {
855 /*
856 * check if input size is less than the cipher block size,
857 * decrypt first block and extract length of incoming packet
858 */
859 if (buffer_len(&input) < block_size)
860 return SSH_MSG_NONE;
861 buffer_clear(&incoming_packet);
862 buffer_append_space(&incoming_packet, &cp, block_size);
863 packet_decrypt(&receive_context, cp, buffer_ptr(&input),
864 block_size);
1e3b8b07 865 ucp = (u_char *) buffer_ptr(&incoming_packet);
7e7327a1 866 packet_length = GET_32BIT(ucp);
867 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
868 buffer_dump(&incoming_packet);
869 packet_disconnect("Bad packet length %d.", packet_length);
870 }
871 DBG(debug("input: packet len %d", packet_length+4));
872 buffer_consume(&input, block_size);
873 }
874 /* we have a partial packet of block_size bytes */
875 need = 4 + packet_length - block_size;
876 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
877 need, maclen));
878 if (need % block_size != 0)
879 fatal("padding error: need %d block %d mod %d",
880 need, block_size, need % block_size);
881 /*
882 * check if the entire packet has been received and
883 * decrypt into incoming_packet
884 */
885 if (buffer_len(&input) < need + maclen)
886 return SSH_MSG_NONE;
887#ifdef PACKET_DEBUG
888 fprintf(stderr, "read_poll enc/full: ");
889 buffer_dump(&input);
890#endif
891 buffer_append_space(&incoming_packet, &cp, need);
892 packet_decrypt(&receive_context, cp, buffer_ptr(&input), need);
893 buffer_consume(&input, need);
894 /*
895 * compute MAC over seqnr and packet,
896 * increment sequence number for incoming packet
897 */
6ae2364d 898 if (mac && mac->enabled) {
b2552997 899 macbuf = mac_compute(mac, seqnr,
1e3b8b07 900 (u_char *) buffer_ptr(&incoming_packet),
b2552997 901 buffer_len(&incoming_packet));
7e7327a1 902 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
94ec8c6b 903 packet_disconnect("Corrupted MAC on input.");
904 DBG(debug("MAC #%d ok", seqnr));
7e7327a1 905 buffer_consume(&input, mac->mac_len);
906 }
6ae2364d 907 if (++seqnr == 0)
908 log("incoming seqnr wraps around");
7e7327a1 909
910 /* get padlen */
911 cp = buffer_ptr(&incoming_packet) + 4;
912 padlen = *cp & 0xff;
913 DBG(debug("input: padlen %d", padlen));
914 if (padlen < 4)
915 packet_disconnect("Corrupted padlen %d on input.", padlen);
916
917 /* skip packet size + padlen, discard padding */
918 buffer_consume(&incoming_packet, 4 + 1);
919 buffer_consume_end(&incoming_packet, padlen);
920
921 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
922 if (comp && comp->enabled) {
923 buffer_clear(&compression_buffer);
924 buffer_uncompress(&incoming_packet, &compression_buffer);
925 buffer_clear(&incoming_packet);
926 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
927 buffer_len(&compression_buffer));
928 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
929 }
930 /*
931 * get packet type, implies consume.
932 * return length of payload (without type field)
933 */
934 buffer_get(&incoming_packet, (char *)&buf[0], 1);
935 *payload_len_ptr = buffer_len(&incoming_packet);
936
937 /* reset for next packet */
938 packet_length = 0;
939
940 /* extract packet type */
1e3b8b07 941 type = (u_char)buf[0];
7e7327a1 942
d1ac6175 943 if (type == SSH2_MSG_NEWKEYS)
944 set_newkeys(MODE_IN);
7e7327a1 945
946#ifdef PACKET_DEBUG
12bf85ed 947 fprintf(stderr, "read/plain[%d]:\r\n", type);
7e7327a1 948 buffer_dump(&incoming_packet);
949#endif
1e3b8b07 950 return (u_char)type;
7e7327a1 951}
952
953int
954packet_read_poll(int *payload_len_ptr)
955{
956 char *msg;
957 for (;;) {
958 int type = use_ssh2_packet_format ?
959 packet_read_poll2(payload_len_ptr):
960 packet_read_poll1(payload_len_ptr);
961
962 if(compat20) {
963 int reason;
964 if (type != 0)
965 DBG(debug("received packet type %d", type));
966 switch(type) {
967 case SSH2_MSG_IGNORE:
968 break;
969 case SSH2_MSG_DEBUG:
970 packet_get_char();
971 msg = packet_get_string(NULL);
972 debug("Remote: %.900s", msg);
973 xfree(msg);
974 msg = packet_get_string(NULL);
975 xfree(msg);
976 break;
977 case SSH2_MSG_DISCONNECT:
978 reason = packet_get_int();
979 msg = packet_get_string(NULL);
7c49df64 980 log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
981 reason, msg);
7e7327a1 982 xfree(msg);
983 fatal_cleanup();
984 break;
985 default:
986 return type;
987 break;
2b87da3b 988 }
7e7327a1 989 } else {
990 switch(type) {
991 case SSH_MSG_IGNORE:
992 break;
993 case SSH_MSG_DEBUG:
994 msg = packet_get_string(NULL);
995 debug("Remote: %.900s", msg);
996 xfree(msg);
997 break;
998 case SSH_MSG_DISCONNECT:
999 msg = packet_get_string(NULL);
7c49df64 1000 log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
1001 msg);
7e7327a1 1002 fatal_cleanup();
1003 xfree(msg);
1004 break;
1005 default:
1006 if (type != 0)
1007 DBG(debug("received packet type %d", type));
1008 return type;
1009 break;
2b87da3b 1010 }
7e7327a1 1011 }
1012 }
1013}
1014
aa3378df 1015/*
1016 * Buffers the given amount of input characters. This is intended to be used
1017 * together with packet_read_poll.
1018 */
8efc0c15 1019
1020void
1e3b8b07 1021packet_process_incoming(const char *buf, u_int len)
8efc0c15 1022{
5260325f 1023 buffer_append(&input, buf, len);
8efc0c15 1024}
1025
1026/* Returns a character from the packet. */
1027
1e3b8b07 1028u_int
8efc0c15 1029packet_get_char()
1030{
5260325f 1031 char ch;
1032 buffer_get(&incoming_packet, &ch, 1);
1e3b8b07 1033 return (u_char) ch;
8efc0c15 1034}
1035
1036/* Returns an integer from the packet data. */
1037
1e3b8b07 1038u_int
8efc0c15 1039packet_get_int()
1040{
5260325f 1041 return buffer_get_int(&incoming_packet);
8efc0c15 1042}
1043
aa3378df 1044/*
1045 * Returns an arbitrary precision integer from the packet data. The integer
1046 * must have been initialized before this call.
1047 */
8efc0c15 1048
1049void
5260325f 1050packet_get_bignum(BIGNUM * value, int *length_ptr)
8efc0c15 1051{
5260325f 1052 *length_ptr = buffer_get_bignum(&incoming_packet, value);
8efc0c15 1053}
1054
7e7327a1 1055void
1056packet_get_bignum2(BIGNUM * value, int *length_ptr)
1057{
1058 *length_ptr = buffer_get_bignum2(&incoming_packet, value);
1059}
1060
1061char *
1062packet_get_raw(int *length_ptr)
1063{
1064 int bytes = buffer_len(&incoming_packet);
1065 if (length_ptr != NULL)
1066 *length_ptr = bytes;
1067 return buffer_ptr(&incoming_packet);
1068}
1069
6ae2364d 1070int
1071packet_remaining(void)
1072{
1073 return buffer_len(&incoming_packet);
1074}
1075
aa3378df 1076/*
1077 * Returns a string from the packet data. The string is allocated using
1078 * xmalloc; it is the responsibility of the calling program to free it when
1079 * no longer needed. The length_ptr argument may be NULL, or point to an
1080 * integer into which the length of the string is stored.
1081 */
8efc0c15 1082
aa3378df 1083char *
1e3b8b07 1084packet_get_string(u_int *length_ptr)
8efc0c15 1085{
5260325f 1086 return buffer_get_string(&incoming_packet, length_ptr);
8efc0c15 1087}
1088
aa3378df 1089/*
1090 * Sends a diagnostic message from the server to the client. This message
1091 * can be sent at any time (but not while constructing another message). The
1092 * message is printed immediately, but only if the client is being executed
1093 * in verbose mode. These messages are primarily intended to ease debugging
1094 * authentication problems. The length of the formatted message must not
1095 * exceed 1024 bytes. This will automatically call packet_write_wait.
1096 */
8efc0c15 1097
1098void
5260325f 1099packet_send_debug(const char *fmt,...)
8efc0c15 1100{
5260325f 1101 char buf[1024];
1102 va_list args;
1103
f72fc97f 1104 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1105 return;
1106
5260325f 1107 va_start(args, fmt);
1108 vsnprintf(buf, sizeof(buf), fmt, args);
1109 va_end(args);
1110
c4bc58eb 1111 if (compat20) {
1112 packet_start(SSH2_MSG_DEBUG);
1113 packet_put_char(0); /* bool: always display */
1114 packet_put_cstring(buf);
1115 packet_put_cstring("");
1116 } else {
1117 packet_start(SSH_MSG_DEBUG);
1118 packet_put_cstring(buf);
1119 }
5260325f 1120 packet_send();
1121 packet_write_wait();
8efc0c15 1122}
1123
aa3378df 1124/*
1125 * Logs the error plus constructs and sends a disconnect packet, closes the
1126 * connection, and exits. This function never returns. The error message
1127 * should not contain a newline. The length of the formatted message must
1128 * not exceed 1024 bytes.
1129 */
8efc0c15 1130
1131void
5260325f 1132packet_disconnect(const char *fmt,...)
1133{
1134 char buf[1024];
1135 va_list args;
1136 static int disconnecting = 0;
1137 if (disconnecting) /* Guard against recursive invocations. */
1138 fatal("packet_disconnect called recursively.");
1139 disconnecting = 1;
1140
aa3378df 1141 /*
1142 * Format the message. Note that the caller must make sure the
1143 * message is of limited size.
1144 */
5260325f 1145 va_start(args, fmt);
1146 vsnprintf(buf, sizeof(buf), fmt, args);
1147 va_end(args);
1148
1149 /* Send the disconnect message to the other side, and wait for it to get sent. */
7e7327a1 1150 if (compat20) {
1151 packet_start(SSH2_MSG_DISCONNECT);
1152 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1153 packet_put_cstring(buf);
1154 packet_put_cstring("");
1155 } else {
1156 packet_start(SSH_MSG_DISCONNECT);
1157 packet_put_string(buf, strlen(buf));
1158 }
5260325f 1159 packet_send();
1160 packet_write_wait();
1161
1162 /* Stop listening for connections. */
1163 channel_stop_listening();
1164
1165 /* Close the connection. */
1166 packet_close();
1167
1168 /* Display the error locally and exit. */
57112b5a 1169 log("Disconnecting: %.100s", buf);
1170 fatal_cleanup();
8efc0c15 1171}
1172
aa3378df 1173/* Checks if there is any buffered output, and tries to write some of the output. */
8efc0c15 1174
1175void
1176packet_write_poll()
1177{
5260325f 1178 int len = buffer_len(&output);
1179 if (len > 0) {
1180 len = write(connection_out, buffer_ptr(&output), len);
1181 if (len <= 0) {
1182 if (errno == EAGAIN)
1183 return;
1184 else
1185 fatal("Write failed: %.100s", strerror(errno));
1186 }
1187 buffer_consume(&output, len);
1188 }
8efc0c15 1189}
1190
aa3378df 1191/*
1192 * Calls packet_write_poll repeatedly until all pending output data has been
1193 * written.
1194 */
8efc0c15 1195
1196void
1197packet_write_wait()
1198{
20e04e90 1199 fd_set *setp;
1200
1201 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1202 sizeof(fd_mask));
5260325f 1203 packet_write_poll();
1204 while (packet_have_data_to_write()) {
20e04e90 1205 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1206 sizeof(fd_mask));
1207 FD_SET(connection_out, setp);
1208 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
fd193ca4 1209 (errno == EAGAIN || errno == EINTR))
1210 ;
5260325f 1211 packet_write_poll();
1212 }
20e04e90 1213 xfree(setp);
8efc0c15 1214}
1215
1216/* Returns true if there is buffered data to write to the connection. */
1217
1218int
1219packet_have_data_to_write()
1220{
5260325f 1221 return buffer_len(&output) != 0;
8efc0c15 1222}
1223
1224/* Returns true if there is not too much data to write to the connection. */
1225
1226int
1227packet_not_very_much_data_to_write()
1228{
5260325f 1229 if (interactive_mode)
1230 return buffer_len(&output) < 16384;
1231 else
1232 return buffer_len(&output) < 128 * 1024;
8efc0c15 1233}
1234
1235/* Informs that the current session is interactive. Sets IP flags for that. */
1236
1237void
b5c334cc 1238packet_set_interactive(int interactive)
8efc0c15 1239{
b5c334cc 1240 static int called = 0;
64e0e67e 1241#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
b5c334cc 1242 int lowdelay = IPTOS_LOWDELAY;
1243 int throughput = IPTOS_THROUGHPUT;
64e0e67e 1244#endif
5260325f 1245 int on = 1;
1246
b5c334cc 1247 if (called)
1248 return;
1249 called = 1;
1250
5260325f 1251 /* Record that we are in interactive mode. */
1252 interactive_mode = interactive;
1253
48e671d5 1254 /* Only set socket options if using a socket. */
1255 if (!packet_connection_is_on_socket())
5260325f 1256 return;
48e671d5 1257 /*
f546c780 1258 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
48e671d5 1259 */
5260325f 1260 if (interactive) {
aa3378df 1261 /*
1262 * Set IP options for an interactive connection. Use
1263 * IPTOS_LOWDELAY and TCP_NODELAY.
1264 */
132dd316 1265#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
f546c780 1266 if (packet_connection_is_ipv4()) {
0e3c1f95 1267 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
f546c780 1268 (void *) &lowdelay, sizeof(lowdelay)) < 0)
0e3c1f95 1269 error("setsockopt IPTOS_LOWDELAY: %.100s",
f546c780 1270 strerror(errno));
1271 }
c04f75f1 1272#endif
5260325f 1273 if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on,
48e671d5 1274 sizeof(on)) < 0)
5260325f 1275 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
f546c780 1276 } else if (packet_connection_is_ipv4()) {
aa3378df 1277 /*
1278 * Set IP options for a non-interactive connection. Use
1279 * IPTOS_THROUGHPUT.
1280 */
132dd316 1281#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
5260325f 1282 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
48e671d5 1283 sizeof(throughput)) < 0)
5260325f 1284 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
c04f75f1 1285#endif
5260325f 1286 }
8efc0c15 1287}
1288
1289/* Returns true if the current connection is interactive. */
1290
1291int
1292packet_is_interactive()
1293{
5260325f 1294 return interactive_mode;
8efc0c15 1295}
9d6b7add 1296
1297int
1298packet_set_maxsize(int s)
1299{
5260325f 1300 static int called = 0;
1301 if (called) {
7e7327a1 1302 log("packet_set_maxsize: called twice: old %d new %d",
1303 max_packet_size, s);
5260325f 1304 return -1;
1305 }
1306 if (s < 4 * 1024 || s > 1024 * 1024) {
1307 log("packet_set_maxsize: bad size %d", s);
1308 return -1;
1309 }
1310 log("packet_set_maxsize: setting to %d", s);
1311 max_packet_size = s;
1312 return s;
9d6b7add 1313}
a6215e53 1314
1315/*
1316 * 9.2. Ignored Data Message
1317 *
1318 * byte SSH_MSG_IGNORE
1319 * string data
1320 *
1321 * All implementations MUST understand (and ignore) this message at any
1322 * time (after receiving the protocol version). No implementation is
1323 * required to send them. This message can be used as an additional
1324 * protection measure against advanced traffic analysis techniques.
1325 */
1326/* size of current + ignore message should be n*sumlen bytes (w/o mac) */
1327void
1328packet_inject_ignore(int sumlen)
1329{
95ce5599 1330 int blocksize, padlen, have, need, nb, mini, nbytes;
a6215e53 1331 Enc *enc = NULL;
1332
1333 if (use_ssh2_packet_format == 0)
1334 return;
1335
1336 have = buffer_len(&outgoing_packet);
1337 debug2("packet_inject_ignore: current %d", have);
d1ac6175 1338 if (newkeys[MODE_OUT] != NULL)
1339 enc = &newkeys[MODE_OUT]->enc;
a6215e53 1340 blocksize = enc ? enc->cipher->block_size : 8;
1341 padlen = blocksize - (have % blocksize);
1342 if (padlen < 4)
1343 padlen += blocksize;
1344 have += padlen;
1345 have /= blocksize; /* # of blocks for current message */
1346
1347 nb = roundup(sumlen, blocksize) / blocksize; /* blocks for both */
1348 mini = roundup(5+1+4+4, blocksize) / blocksize; /* minsize ignore msg */
1349 need = nb - (have % nb); /* blocks for ignore */
1350 if (need <= mini)
1351 need += nb;
1352 nbytes = (need - mini) * blocksize; /* size of ignore payload */
1353 debug2("packet_inject_ignore: block %d have %d nb %d mini %d need %d",
1354 blocksize, have, nb, mini, need);
1355
1356 /* enqueue current message and append a ignore message */
1357 packet_send();
95ce5599 1358 packet_send_ignore(nbytes);
1359}
1360
1361void
1362packet_send_ignore(int nbytes)
1363{
1364 u_int32_t rand = 0;
1365 int i;
1366
1367 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
a6215e53 1368 packet_put_int(nbytes);
1369 for(i = 0; i < nbytes; i++) {
1370 if (i % 4 == 0)
1371 rand = arc4random();
1372 packet_put_char(rand & 0xff);
1373 rand >>= 8;
1374 }
1375}
This page took 0.464673 seconds and 5 git commands to generate.