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