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