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