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