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