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