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