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