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