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