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