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