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