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