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