Annotation of src/usr.bin/ssh/packet.c, Revision 1.200
1.200 ! markus 1: /* $OpenBSD: packet.c,v 1.199 2014/10/24 02:01:20 lteo 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:
1.132 stevesk 46: #include <netinet/in.h>
1.121 stevesk 47: #include <netinet/ip.h>
1.134 stevesk 48:
1.135 stevesk 49: #include <errno.h>
1.134 stevesk 50: #include <stdarg.h>
1.141 stevesk 51: #include <stdio.h>
1.140 stevesk 52: #include <stdlib.h>
1.137 stevesk 53: #include <string.h>
1.136 stevesk 54: #include <unistd.h>
1.142 deraadt 55: #include <signal.h>
1.184 dtucker 56: #include <time.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.25 markus 64: #include "compat.h"
1.45 markus 65: #include "ssh1.h"
1.25 markus 66: #include "ssh2.h"
1.37 markus 67: #include "cipher.h"
1.142 deraadt 68: #include "key.h"
1.25 markus 69: #include "kex.h"
1.200 ! markus 70: #include "digest.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.200 ! markus 274: PACKET_MAX_SIZE, NULL, 0);
1.159 markus 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.200 ! markus 855: u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
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;
1.200 ! markus 863: int r;
1.25 markus 864:
1.161 andreas 865: if (active_state->newkeys[MODE_OUT] != NULL) {
866: enc = &active_state->newkeys[MODE_OUT]->enc;
867: mac = &active_state->newkeys[MODE_OUT]->mac;
868: comp = &active_state->newkeys[MODE_OUT]->comp;
1.180 markus 869: /* disable mac for authenticated encryption */
870: if ((authlen = cipher_authlen(enc->cipher)) != 0)
871: mac = NULL;
1.25 markus 872: }
1.88 markus 873: block_size = enc ? enc->block_size : 8;
1.180 markus 874: aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
1.25 markus 875:
1.161 andreas 876: cp = buffer_ptr(&active_state->outgoing_packet);
1.89 markus 877: type = cp[5];
1.25 markus 878:
879: #ifdef PACKET_DEBUG
880: fprintf(stderr, "plain: ");
1.161 andreas 881: buffer_dump(&active_state->outgoing_packet);
1.25 markus 882: #endif
883:
884: if (comp && comp->enabled) {
1.161 andreas 885: len = buffer_len(&active_state->outgoing_packet);
1.25 markus 886: /* skip header, compress only payload */
1.161 andreas 887: buffer_consume(&active_state->outgoing_packet, 5);
888: buffer_clear(&active_state->compression_buffer);
889: buffer_compress(&active_state->outgoing_packet,
890: &active_state->compression_buffer);
891: buffer_clear(&active_state->outgoing_packet);
892: buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
893: buffer_append(&active_state->outgoing_packet,
894: buffer_ptr(&active_state->compression_buffer),
895: buffer_len(&active_state->compression_buffer));
1.25 markus 896: DBG(debug("compression: raw %d compressed %d", len,
1.161 andreas 897: buffer_len(&active_state->outgoing_packet)));
1.25 markus 898: }
899:
900: /* sizeof (packet_len + pad_len + payload) */
1.161 andreas 901: len = buffer_len(&active_state->outgoing_packet);
1.25 markus 902:
903: /*
904: * calc size of padding, alloc space, get random data,
905: * minimum padding is 4 bytes
906: */
1.178 markus 907: len -= aadlen; /* packet length is not encrypted for EtM modes */
1.25 markus 908: padlen = block_size - (len % block_size);
909: if (padlen < 4)
910: padlen += block_size;
1.161 andreas 911: if (active_state->extra_pad) {
1.71 markus 912: /* will wrap if extra_pad+padlen > 255 */
1.161 andreas 913: active_state->extra_pad =
914: roundup(active_state->extra_pad, block_size);
915: pad = active_state->extra_pad -
916: ((len + padlen) % active_state->extra_pad);
1.193 djm 917: DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
918: __func__, pad, len, padlen, active_state->extra_pad));
1.71 markus 919: padlen += pad;
1.161 andreas 920: active_state->extra_pad = 0;
1.71 markus 921: }
1.161 andreas 922: cp = buffer_append_space(&active_state->outgoing_packet, padlen);
923: if (enc && !active_state->send_context.plaintext) {
1.32 markus 924: /* random padding */
1.25 markus 925: for (i = 0; i < padlen; i++) {
926: if (i % 4 == 0)
1.115 avsm 927: rnd = arc4random();
928: cp[i] = rnd & 0xff;
929: rnd >>= 8;
1.25 markus 930: }
1.32 markus 931: } else {
932: /* clear padding */
1.192 djm 933: explicit_bzero(cp, padlen);
1.25 markus 934: }
1.178 markus 935: /* sizeof (packet_len + pad_len + payload + padding) */
936: len = buffer_len(&active_state->outgoing_packet);
937: cp = buffer_ptr(&active_state->outgoing_packet);
1.25 markus 938: /* packet_length includes payload, padding and padding length field */
1.178 markus 939: put_u32(cp, len - 4);
1.89 markus 940: cp[4] = padlen;
1.178 markus 941: DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
942: len, padlen, aadlen));
1.25 markus 943:
944: /* compute MAC over seqnr and packet(length fields, payload, padding) */
1.178 markus 945: if (mac && mac->enabled && !mac->etm) {
1.200 ! markus 946: if ((r = mac_compute(mac, active_state->p_send.seqnr,
! 947: buffer_ptr(&active_state->outgoing_packet), len,
! 948: macbuf, sizeof(macbuf))) != 0)
! 949: fatal("%s: mac_compute: %s", __func__, ssh_err(r));
1.161 andreas 950: DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
1.25 markus 951: }
952: /* encrypt packet and append to output buffer. */
1.180 markus 953: cp = buffer_append_space(&active_state->output, len + authlen);
1.191 markus 954: if (cipher_crypt(&active_state->send_context, active_state->p_send.seqnr,
1.190 djm 955: cp, buffer_ptr(&active_state->outgoing_packet),
1.191 markus 956: len - aadlen, aadlen, authlen) != 0)
957: fatal("%s: cipher_crypt failed", __func__);
1.25 markus 958: /* append unencrypted MAC */
1.178 markus 959: if (mac && mac->enabled) {
960: if (mac->etm) {
961: /* EtM: compute mac over aadlen + cipher text */
1.200 ! markus 962: if ((r = mac_compute(mac,
! 963: active_state->p_send.seqnr, cp, len,
! 964: macbuf, sizeof(macbuf))) != 0)
! 965: fatal("%s: mac_compute: %s", __func__, ssh_err(r));
1.178 markus 966: DBG(debug("done calc MAC(EtM) out #%d",
967: active_state->p_send.seqnr));
968: }
1.161 andreas 969: buffer_append(&active_state->output, macbuf, mac->mac_len);
1.178 markus 970: }
1.25 markus 971: #ifdef PACKET_DEBUG
972: fprintf(stderr, "encrypted: ");
1.161 andreas 973: buffer_dump(&active_state->output);
1.25 markus 974: #endif
1.29 markus 975: /* increment sequence number for outgoing packets */
1.161 andreas 976: if (++active_state->p_send.seqnr == 0)
1.106 itojun 977: logit("outgoing seqnr wraps around");
1.161 andreas 978: if (++active_state->p_send.packets == 0)
1.105 markus 979: if (!(datafellows & SSH_BUG_NOREKEY))
980: fatal("XXX too many packets with same key");
1.178 markus 981: active_state->p_send.blocks += len / block_size;
982: active_state->p_send.bytes += len;
1.161 andreas 983: buffer_clear(&active_state->outgoing_packet);
1.25 markus 984:
1.57 markus 985: if (type == SSH2_MSG_NEWKEYS)
986: set_newkeys(MODE_OUT);
1.161 andreas 987: else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
1.118 markus 988: packet_enable_delayed_compress();
1.25 markus 989: }
990:
1.105 markus 991: static void
992: packet_send2(void)
993: {
994: struct packet *p;
995: u_char type, *cp;
996:
1.161 andreas 997: cp = buffer_ptr(&active_state->outgoing_packet);
1.105 markus 998: type = cp[5];
999:
1000: /* during rekeying we can only send key exchange messages */
1.165 andreas 1001: if (active_state->rekeying) {
1.175 markus 1002: if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1003: (type > SSH2_MSG_TRANSPORT_MAX) ||
1004: (type == SSH2_MSG_SERVICE_REQUEST) ||
1005: (type == SSH2_MSG_SERVICE_ACCEPT)) {
1.105 markus 1006: debug("enqueue packet: %u", type);
1.189 djm 1007: p = xcalloc(1, sizeof(*p));
1.105 markus 1008: p->type = type;
1.161 andreas 1009: memcpy(&p->payload, &active_state->outgoing_packet,
1010: sizeof(Buffer));
1011: buffer_init(&active_state->outgoing_packet);
1012: TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
1.105 markus 1013: return;
1014: }
1015: }
1016:
1017: /* rekeying starts with sending KEXINIT */
1018: if (type == SSH2_MSG_KEXINIT)
1.165 andreas 1019: active_state->rekeying = 1;
1.105 markus 1020:
1021: packet_send2_wrapped();
1022:
1023: /* after a NEWKEYS message we can send the complete queue */
1024: if (type == SSH2_MSG_NEWKEYS) {
1.165 andreas 1025: active_state->rekeying = 0;
1.187 dtucker 1026: active_state->rekey_time = monotime();
1.161 andreas 1027: while ((p = TAILQ_FIRST(&active_state->outgoing))) {
1.105 markus 1028: type = p->type;
1029: debug("dequeue packet: %u", type);
1.161 andreas 1030: buffer_free(&active_state->outgoing_packet);
1031: memcpy(&active_state->outgoing_packet, &p->payload,
1.105 markus 1032: sizeof(Buffer));
1.161 andreas 1033: TAILQ_REMOVE(&active_state->outgoing, p, next);
1.186 djm 1034: free(p);
1.105 markus 1035: packet_send2_wrapped();
1036: }
1037: }
1038: }
1039:
1.25 markus 1040: void
1.73 itojun 1041: packet_send(void)
1.25 markus 1042: {
1.62 markus 1043: if (compat20)
1.25 markus 1044: packet_send2();
1045: else
1046: packet_send1();
1047: DBG(debug("packet_send done"));
1048: }
1049:
1050: /*
1.16 markus 1051: * Waits until a packet has been received, and returns its type. Note that
1052: * no other data is processed until this returns, so this function should not
1053: * be used during the interactive session.
1054: */
1.1 deraadt 1055:
1.2 provos 1056: int
1.82 markus 1057: packet_read_seqnr(u_int32_t *seqnr_p)
1.1 deraadt 1058: {
1.188 djm 1059: int type, len, ret, cont, ms_remain = 0;
1.56 millert 1060: fd_set *setp;
1.14 markus 1061: char buf[8192];
1.155 deraadt 1062: struct timeval timeout, start, *timeoutp = NULL;
1063:
1.25 markus 1064: DBG(debug("packet_read()"));
1.14 markus 1065:
1.161 andreas 1066: setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1067: NFDBITS), sizeof(fd_mask));
1.56 millert 1068:
1.14 markus 1069: /* Since we are blocking, ensure that all written packets have been sent. */
1070: packet_write_wait();
1071:
1072: /* Stay in the loop until we have received a complete packet. */
1073: for (;;) {
1074: /* Try to read a packet from the buffer. */
1.82 markus 1075: type = packet_read_poll_seqnr(seqnr_p);
1.62 markus 1076: if (!compat20 && (
1.32 markus 1077: type == SSH_SMSG_SUCCESS
1.14 markus 1078: || type == SSH_SMSG_FAILURE
1079: || type == SSH_CMSG_EOF
1.32 markus 1080: || type == SSH_CMSG_EXIT_CONFIRMATION))
1.79 markus 1081: packet_check_eom();
1.14 markus 1082: /* If we got a packet, return it. */
1.56 millert 1083: if (type != SSH_MSG_NONE) {
1.186 djm 1084: free(setp);
1.14 markus 1085: return type;
1.56 millert 1086: }
1.16 markus 1087: /*
1088: * Otherwise, wait for some data to arrive, add it to the
1089: * buffer, and try again.
1090: */
1.161 andreas 1091: memset(setp, 0, howmany(active_state->connection_in + 1,
1092: NFDBITS) * sizeof(fd_mask));
1093: FD_SET(active_state->connection_in, setp);
1.16 markus 1094:
1.161 andreas 1095: if (active_state->packet_timeout_ms > 0) {
1096: ms_remain = active_state->packet_timeout_ms;
1.154 dtucker 1097: timeoutp = &timeout;
1098: }
1.14 markus 1099: /* Wait for some data to arrive. */
1.154 dtucker 1100: for (;;) {
1.161 andreas 1101: if (active_state->packet_timeout_ms != -1) {
1.154 dtucker 1102: ms_to_timeval(&timeout, ms_remain);
1103: gettimeofday(&start, NULL);
1104: }
1.161 andreas 1105: if ((ret = select(active_state->connection_in + 1, setp,
1106: NULL, NULL, timeoutp)) >= 0)
1.154 dtucker 1107: break;
1.161 andreas 1108: if (errno != EAGAIN && errno != EINTR)
1.154 dtucker 1109: break;
1.161 andreas 1110: if (active_state->packet_timeout_ms == -1)
1.154 dtucker 1111: continue;
1112: ms_subtract_diff(&start, &ms_remain);
1113: if (ms_remain <= 0) {
1114: ret = 0;
1115: break;
1116: }
1117: }
1118: if (ret == 0) {
1119: logit("Connection to %.200s timed out while "
1120: "waiting to read", get_remote_ipaddr());
1121: cleanup_exit(255);
1122: }
1.14 markus 1123: /* Read data from the socket. */
1.163 andreas 1124: do {
1125: cont = 0;
1126: len = roaming_read(active_state->connection_in, buf,
1127: sizeof(buf), &cont);
1128: } while (len == 0 && cont);
1.18 markus 1129: if (len == 0) {
1.106 itojun 1130: logit("Connection closed by %.200s", get_remote_ipaddr());
1.112 markus 1131: cleanup_exit(255);
1.18 markus 1132: }
1.14 markus 1133: if (len < 0)
1134: fatal("Read from socket failed: %.100s", strerror(errno));
1135: /* Append it to the buffer. */
1136: packet_process_incoming(buf, len);
1137: }
1138: /* NOTREACHED */
1.1 deraadt 1139: }
1140:
1.77 djm 1141: int
1.82 markus 1142: packet_read(void)
1.77 djm 1143: {
1.82 markus 1144: return packet_read_seqnr(NULL);
1.77 djm 1145: }
1146:
1.16 markus 1147: /*
1148: * Waits until a packet has been received, verifies that its type matches
1149: * that given, and gives a fatal error and exits if there is a mismatch.
1150: */
1.1 deraadt 1151:
1.2 provos 1152: void
1.82 markus 1153: packet_read_expect(int expected_type)
1.1 deraadt 1154: {
1.14 markus 1155: int type;
1.1 deraadt 1156:
1.82 markus 1157: type = packet_read();
1.14 markus 1158: if (type != expected_type)
1159: packet_disconnect("Protocol error: expected packet type %d, got %d",
1.25 markus 1160: expected_type, type);
1.1 deraadt 1161: }
1162:
1163: /* Checks if a full packet is available in the data received so far via
1.14 markus 1164: * packet_process_incoming. If so, reads the packet; otherwise returns
1165: * SSH_MSG_NONE. This does not wait for data from the connection.
1166: *
1.150 dtucker 1167: * SSH_MSG_DISCONNECT is handled specially here. Also,
1168: * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1169: * to higher levels.
1.14 markus 1170: */
1.1 deraadt 1171:
1.68 itojun 1172: static int
1.82 markus 1173: packet_read_poll1(void)
1.1 deraadt 1174: {
1.40 markus 1175: u_int len, padded_len;
1.89 markus 1176: u_char *cp, type;
1.40 markus 1177: u_int checksum, stored_checksum;
1.14 markus 1178:
1179: /* Check if input size is less than minimum packet size. */
1.161 andreas 1180: if (buffer_len(&active_state->input) < 4 + 8)
1.14 markus 1181: return SSH_MSG_NONE;
1182: /* Get length of incoming packet. */
1.161 andreas 1183: cp = buffer_ptr(&active_state->input);
1.131 djm 1184: len = get_u32(cp);
1.14 markus 1185: if (len < 1 + 2 + 2 || len > 256 * 1024)
1.98 markus 1186: packet_disconnect("Bad packet length %u.", len);
1.14 markus 1187: padded_len = (len + 8) & ~7;
1188:
1189: /* Check if the packet has been entirely received. */
1.161 andreas 1190: if (buffer_len(&active_state->input) < 4 + padded_len)
1.14 markus 1191: return SSH_MSG_NONE;
1192:
1193: /* The entire packet is in buffer. */
1194:
1195: /* Consume packet length. */
1.161 andreas 1196: buffer_consume(&active_state->input, 4);
1.14 markus 1197:
1.62 markus 1198: /*
1199: * Cryptographic attack detector for ssh
1200: * (C)1998 CORE-SDI, Buenos Aires Argentina
1201: * Ariel Futoransky(futo@core-sdi.com)
1202: */
1.161 andreas 1203: if (!active_state->receive_context.plaintext) {
1204: switch (detect_attack(buffer_ptr(&active_state->input),
1205: padded_len)) {
1.144 djm 1206: case DEATTACK_DETECTED:
1207: packet_disconnect("crc32 compensation attack: "
1208: "network attack detected");
1209: case DEATTACK_DOS_DETECTED:
1210: packet_disconnect("deattack denial of "
1211: "service detected");
1212: }
1213: }
1.62 markus 1214:
1215: /* Decrypt data to incoming_packet. */
1.161 andreas 1216: buffer_clear(&active_state->incoming_packet);
1217: cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1.191 markus 1218: if (cipher_crypt(&active_state->receive_context, 0, cp,
1219: buffer_ptr(&active_state->input), padded_len, 0, 0) != 0)
1220: fatal("%s: cipher_crypt failed", __func__);
1.62 markus 1221:
1.161 andreas 1222: buffer_consume(&active_state->input, padded_len);
1.1 deraadt 1223:
1224: #ifdef PACKET_DEBUG
1.14 markus 1225: fprintf(stderr, "read_poll plain: ");
1.161 andreas 1226: buffer_dump(&active_state->incoming_packet);
1.1 deraadt 1227: #endif
1228:
1.14 markus 1229: /* Compute packet checksum. */
1.161 andreas 1230: checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1231: buffer_len(&active_state->incoming_packet) - 4);
1.14 markus 1232:
1233: /* Skip padding. */
1.161 andreas 1234: buffer_consume(&active_state->incoming_packet, 8 - len % 8);
1.14 markus 1235:
1236: /* Test check bytes. */
1.161 andreas 1237: if (len != buffer_len(&active_state->incoming_packet))
1.77 djm 1238: packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
1.161 andreas 1239: len, buffer_len(&active_state->incoming_packet));
1.14 markus 1240:
1.161 andreas 1241: cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
1.131 djm 1242: stored_checksum = get_u32(cp);
1.14 markus 1243: if (checksum != stored_checksum)
1244: packet_disconnect("Corrupted check bytes on input.");
1.161 andreas 1245: buffer_consume_end(&active_state->incoming_packet, 4);
1.14 markus 1246:
1.161 andreas 1247: if (active_state->packet_compression) {
1248: buffer_clear(&active_state->compression_buffer);
1249: buffer_uncompress(&active_state->incoming_packet,
1250: &active_state->compression_buffer);
1251: buffer_clear(&active_state->incoming_packet);
1252: buffer_append(&active_state->incoming_packet,
1253: buffer_ptr(&active_state->compression_buffer),
1254: buffer_len(&active_state->compression_buffer));
1255: }
1256: active_state->p_read.packets++;
1257: active_state->p_read.bytes += padded_len + 4;
1258: type = buffer_get_char(&active_state->incoming_packet);
1.116 markus 1259: if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1260: packet_disconnect("Invalid ssh1 packet type: %d", type);
1.62 markus 1261: return type;
1.1 deraadt 1262: }
1.14 markus 1263:
1.68 itojun 1264: static int
1.82 markus 1265: packet_read_poll2(u_int32_t *seqnr_p)
1.25 markus 1266: {
1.40 markus 1267: u_int padlen, need;
1.200 ! markus 1268: u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1.180 markus 1269: u_int maclen, authlen = 0, aadlen = 0, block_size;
1.200 ! markus 1270: int r;
1.25 markus 1271: Enc *enc = NULL;
1272: Mac *mac = NULL;
1273: Comp *comp = NULL;
1274:
1.161 andreas 1275: if (active_state->packet_discard)
1.159 markus 1276: return SSH_MSG_NONE;
1277:
1.161 andreas 1278: if (active_state->newkeys[MODE_IN] != NULL) {
1279: enc = &active_state->newkeys[MODE_IN]->enc;
1280: mac = &active_state->newkeys[MODE_IN]->mac;
1281: comp = &active_state->newkeys[MODE_IN]->comp;
1.180 markus 1282: /* disable mac for authenticated encryption */
1283: if ((authlen = cipher_authlen(enc->cipher)) != 0)
1284: mac = NULL;
1.25 markus 1285: }
1286: maclen = mac && mac->enabled ? mac->mac_len : 0;
1.88 markus 1287: block_size = enc ? enc->block_size : 8;
1.180 markus 1288: aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
1.25 markus 1289:
1.178 markus 1290: if (aadlen && active_state->packlen == 0) {
1.190 djm 1291: if (cipher_get_length(&active_state->receive_context,
1292: &active_state->packlen,
1293: active_state->p_read.seqnr,
1294: buffer_ptr(&active_state->input),
1295: buffer_len(&active_state->input)) != 0)
1.178 markus 1296: return SSH_MSG_NONE;
1297: if (active_state->packlen < 1 + 4 ||
1298: active_state->packlen > PACKET_MAX_SIZE) {
1299: #ifdef PACKET_DEBUG
1300: buffer_dump(&active_state->input);
1301: #endif
1302: logit("Bad packet length %u.", active_state->packlen);
1303: packet_disconnect("Packet corrupt");
1304: }
1.179 markus 1305: buffer_clear(&active_state->incoming_packet);
1.178 markus 1306: } else if (active_state->packlen == 0) {
1.25 markus 1307: /*
1308: * check if input size is less than the cipher block size,
1309: * decrypt first block and extract length of incoming packet
1310: */
1.161 andreas 1311: if (buffer_len(&active_state->input) < block_size)
1.25 markus 1312: return SSH_MSG_NONE;
1.161 andreas 1313: buffer_clear(&active_state->incoming_packet);
1314: cp = buffer_append_space(&active_state->incoming_packet,
1.25 markus 1315: block_size);
1.191 markus 1316: if (cipher_crypt(&active_state->receive_context,
1.190 djm 1317: active_state->p_read.seqnr, cp,
1.191 markus 1318: buffer_ptr(&active_state->input), block_size, 0, 0) != 0)
1319: fatal("Decryption integrity check failed");
1.161 andreas 1320: cp = buffer_ptr(&active_state->incoming_packet);
1321: active_state->packlen = get_u32(cp);
1322: if (active_state->packlen < 1 + 4 ||
1323: active_state->packlen > PACKET_MAX_SIZE) {
1.110 markus 1324: #ifdef PACKET_DEBUG
1.161 andreas 1325: buffer_dump(&active_state->incoming_packet);
1.110 markus 1326: #endif
1.161 andreas 1327: logit("Bad packet length %u.", active_state->packlen);
1328: packet_start_discard(enc, mac, active_state->packlen,
1.159 markus 1329: PACKET_MAX_SIZE);
1330: return SSH_MSG_NONE;
1.25 markus 1331: }
1.161 andreas 1332: buffer_consume(&active_state->input, block_size);
1.25 markus 1333: }
1.178 markus 1334: DBG(debug("input: packet len %u", active_state->packlen+4));
1335: if (aadlen) {
1336: /* only the payload is encrypted */
1337: need = active_state->packlen;
1338: } else {
1339: /*
1340: * the payload size and the payload are encrypted, but we
1341: * have a partial packet of block_size bytes
1342: */
1343: need = 4 + active_state->packlen - block_size;
1344: }
1.180 markus 1345: DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1346: " aadlen %d", block_size, need, maclen, authlen, aadlen));
1.158 markus 1347: if (need % block_size != 0) {
1348: logit("padding error: need %d block %d mod %d",
1.25 markus 1349: need, block_size, need % block_size);
1.161 andreas 1350: packet_start_discard(enc, mac, active_state->packlen,
1.159 markus 1351: PACKET_MAX_SIZE - block_size);
1352: return SSH_MSG_NONE;
1.158 markus 1353: }
1.25 markus 1354: /*
1355: * check if the entire packet has been received and
1.178 markus 1356: * decrypt into incoming_packet:
1357: * 'aadlen' bytes are unencrypted, but authenticated.
1.180 markus 1358: * 'need' bytes are encrypted, followed by either
1359: * 'authlen' bytes of authentication tag or
1.178 markus 1360: * 'maclen' bytes of message authentication code.
1.25 markus 1361: */
1.180 markus 1362: if (buffer_len(&active_state->input) < aadlen + need + authlen + maclen)
1.25 markus 1363: return SSH_MSG_NONE;
1364: #ifdef PACKET_DEBUG
1365: fprintf(stderr, "read_poll enc/full: ");
1.161 andreas 1366: buffer_dump(&active_state->input);
1.25 markus 1367: #endif
1.178 markus 1368: /* EtM: compute mac over encrypted input */
1369: if (mac && mac->enabled && mac->etm)
1.200 ! markus 1370: if ((r = mac_compute(mac, active_state->p_read.seqnr,
! 1371: buffer_ptr(&active_state->input), aadlen + need,
! 1372: macbuf, sizeof(macbuf))) != 0)
! 1373: fatal("%s: mac_compute: %s", __func__, ssh_err(r));
1.178 markus 1374: cp = buffer_append_space(&active_state->incoming_packet, aadlen + need);
1.191 markus 1375: if (cipher_crypt(&active_state->receive_context,
1.190 djm 1376: active_state->p_read.seqnr, cp,
1.191 markus 1377: buffer_ptr(&active_state->input), need, aadlen, authlen) != 0)
1378: fatal("Decryption integrity check failed");
1.180 markus 1379: buffer_consume(&active_state->input, aadlen + need + authlen);
1.25 markus 1380: /*
1381: * compute MAC over seqnr and packet,
1382: * increment sequence number for incoming packet
1383: */
1.29 markus 1384: if (mac && mac->enabled) {
1.178 markus 1385: if (!mac->etm)
1.200 ! markus 1386: if ((r = mac_compute(mac, active_state->p_read.seqnr,
1.178 markus 1387: buffer_ptr(&active_state->incoming_packet),
1.200 ! markus 1388: buffer_len(&active_state->incoming_packet),
! 1389: macbuf, sizeof(macbuf))) != 0)
! 1390: fatal("%s: mac_compute: %s", __func__, ssh_err(r));
1.168 djm 1391: if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input),
1.161 andreas 1392: mac->mac_len) != 0) {
1.159 markus 1393: logit("Corrupted MAC on input.");
1394: if (need > PACKET_MAX_SIZE)
1395: fatal("internal error need %d", need);
1.161 andreas 1396: packet_start_discard(enc, mac, active_state->packlen,
1.159 markus 1397: PACKET_MAX_SIZE - need);
1398: return SSH_MSG_NONE;
1399: }
1400:
1.161 andreas 1401: DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1402: buffer_consume(&active_state->input, mac->mac_len);
1.25 markus 1403: }
1.159 markus 1404: /* XXX now it's safe to use fatal/packet_disconnect */
1.77 djm 1405: if (seqnr_p != NULL)
1.161 andreas 1406: *seqnr_p = active_state->p_read.seqnr;
1407: if (++active_state->p_read.seqnr == 0)
1.106 itojun 1408: logit("incoming seqnr wraps around");
1.161 andreas 1409: if (++active_state->p_read.packets == 0)
1.105 markus 1410: if (!(datafellows & SSH_BUG_NOREKEY))
1411: fatal("XXX too many packets with same key");
1.161 andreas 1412: active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1413: active_state->p_read.bytes += active_state->packlen + 4;
1.25 markus 1414:
1415: /* get padlen */
1.161 andreas 1416: cp = buffer_ptr(&active_state->incoming_packet);
1.89 markus 1417: padlen = cp[4];
1.25 markus 1418: DBG(debug("input: padlen %d", padlen));
1419: if (padlen < 4)
1420: packet_disconnect("Corrupted padlen %d on input.", padlen);
1421:
1422: /* skip packet size + padlen, discard padding */
1.161 andreas 1423: buffer_consume(&active_state->incoming_packet, 4 + 1);
1424: buffer_consume_end(&active_state->incoming_packet, padlen);
1.25 markus 1425:
1.161 andreas 1426: DBG(debug("input: len before de-compress %d",
1427: buffer_len(&active_state->incoming_packet)));
1.25 markus 1428: if (comp && comp->enabled) {
1.161 andreas 1429: buffer_clear(&active_state->compression_buffer);
1430: buffer_uncompress(&active_state->incoming_packet,
1431: &active_state->compression_buffer);
1432: buffer_clear(&active_state->incoming_packet);
1433: buffer_append(&active_state->incoming_packet,
1434: buffer_ptr(&active_state->compression_buffer),
1435: buffer_len(&active_state->compression_buffer));
1.97 deraadt 1436: DBG(debug("input: len after de-compress %d",
1.161 andreas 1437: buffer_len(&active_state->incoming_packet)));
1.25 markus 1438: }
1439: /*
1440: * get packet type, implies consume.
1441: * return length of payload (without type field)
1442: */
1.161 andreas 1443: type = buffer_get_char(&active_state->incoming_packet);
1.116 markus 1444: if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1445: packet_disconnect("Invalid ssh2 packet type: %d", type);
1.57 markus 1446: if (type == SSH2_MSG_NEWKEYS)
1447: set_newkeys(MODE_IN);
1.161 andreas 1448: else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1449: !active_state->server_side)
1.118 markus 1450: packet_enable_delayed_compress();
1.25 markus 1451: #ifdef PACKET_DEBUG
1.55 deraadt 1452: fprintf(stderr, "read/plain[%d]:\r\n", type);
1.161 andreas 1453: buffer_dump(&active_state->incoming_packet);
1.25 markus 1454: #endif
1.62 markus 1455: /* reset for next packet */
1.161 andreas 1456: active_state->packlen = 0;
1.62 markus 1457: return type;
1.25 markus 1458: }
1459:
1460: int
1.82 markus 1461: packet_read_poll_seqnr(u_int32_t *seqnr_p)
1.25 markus 1462: {
1.96 deraadt 1463: u_int reason, seqnr;
1.62 markus 1464: u_char type;
1.25 markus 1465: char *msg;
1.62 markus 1466:
1.25 markus 1467: for (;;) {
1.62 markus 1468: if (compat20) {
1.82 markus 1469: type = packet_read_poll2(seqnr_p);
1.153 djm 1470: if (type) {
1.161 andreas 1471: active_state->keep_alive_timeouts = 0;
1.25 markus 1472: DBG(debug("received packet type %d", type));
1.153 djm 1473: }
1.74 deraadt 1474: switch (type) {
1.150 dtucker 1475: case SSH2_MSG_IGNORE:
1.151 dtucker 1476: debug3("Received SSH2_MSG_IGNORE");
1.150 dtucker 1477: break;
1.25 markus 1478: case SSH2_MSG_DEBUG:
1479: packet_get_char();
1480: msg = packet_get_string(NULL);
1481: debug("Remote: %.900s", msg);
1.186 djm 1482: free(msg);
1.25 markus 1483: msg = packet_get_string(NULL);
1.186 djm 1484: free(msg);
1.25 markus 1485: break;
1486: case SSH2_MSG_DISCONNECT:
1487: reason = packet_get_int();
1488: msg = packet_get_string(NULL);
1.182 djm 1489: /* Ignore normal client exit notifications */
1490: do_log2(active_state->server_side &&
1491: reason == SSH2_DISCONNECT_BY_APPLICATION ?
1492: SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1493: "Received disconnect from %s: %u: %.400s",
1.96 deraadt 1494: get_remote_ipaddr(), reason, msg);
1.186 djm 1495: free(msg);
1.112 markus 1496: cleanup_exit(255);
1.84 markus 1497: break;
1498: case SSH2_MSG_UNIMPLEMENTED:
1499: seqnr = packet_get_int();
1.96 deraadt 1500: debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1501: seqnr);
1.150 dtucker 1502: break;
1.25 markus 1503: default:
1504: return type;
1.48 stevesk 1505: }
1.25 markus 1506: } else {
1.82 markus 1507: type = packet_read_poll1();
1.74 deraadt 1508: switch (type) {
1.188 djm 1509: case SSH_MSG_NONE:
1510: return SSH_MSG_NONE;
1.25 markus 1511: case SSH_MSG_IGNORE:
1512: break;
1513: case SSH_MSG_DEBUG:
1514: msg = packet_get_string(NULL);
1515: debug("Remote: %.900s", msg);
1.186 djm 1516: free(msg);
1.25 markus 1517: break;
1518: case SSH_MSG_DISCONNECT:
1519: msg = packet_get_string(NULL);
1.181 djm 1520: error("Received disconnect from %s: %.400s",
1.96 deraadt 1521: get_remote_ipaddr(), msg);
1.112 markus 1522: cleanup_exit(255);
1.25 markus 1523: break;
1524: default:
1.188 djm 1525: DBG(debug("received packet type %d", type));
1.25 markus 1526: return type;
1.48 stevesk 1527: }
1.25 markus 1528: }
1529: }
1530: }
1531:
1.16 markus 1532: /*
1533: * Buffers the given amount of input characters. This is intended to be used
1534: * together with packet_read_poll.
1535: */
1.1 deraadt 1536:
1.2 provos 1537: void
1.40 markus 1538: packet_process_incoming(const char *buf, u_int len)
1.1 deraadt 1539: {
1.161 andreas 1540: if (active_state->packet_discard) {
1541: active_state->keep_alive_timeouts = 0; /* ?? */
1542: if (len >= active_state->packet_discard)
1.159 markus 1543: packet_stop_discard();
1.161 andreas 1544: active_state->packet_discard -= len;
1.159 markus 1545: return;
1546: }
1.161 andreas 1547: buffer_append(&active_state->input, buf, len);
1.1 deraadt 1548: }
1549:
1550: /* Returns a character from the packet. */
1551:
1.40 markus 1552: u_int
1.73 itojun 1553: packet_get_char(void)
1.1 deraadt 1554: {
1.14 markus 1555: char ch;
1.97 deraadt 1556:
1.161 andreas 1557: buffer_get(&active_state->incoming_packet, &ch, 1);
1.40 markus 1558: return (u_char) ch;
1.1 deraadt 1559: }
1560:
1561: /* Returns an integer from the packet data. */
1562:
1.40 markus 1563: u_int
1.73 itojun 1564: packet_get_int(void)
1.1 deraadt 1565: {
1.161 andreas 1566: return buffer_get_int(&active_state->incoming_packet);
1.162 andreas 1567: }
1568:
1569: /* Returns an 64 bit integer from the packet data. */
1570:
1571: u_int64_t
1572: packet_get_int64(void)
1573: {
1574: return buffer_get_int64(&active_state->incoming_packet);
1.1 deraadt 1575: }
1576:
1.16 markus 1577: /*
1578: * Returns an arbitrary precision integer from the packet data. The integer
1579: * must have been initialized before this call.
1580: */
1.1 deraadt 1581:
1.195 markus 1582: #ifdef WITH_OPENSSL
1.2 provos 1583: void
1.80 markus 1584: packet_get_bignum(BIGNUM * value)
1.1 deraadt 1585: {
1.161 andreas 1586: buffer_get_bignum(&active_state->incoming_packet, value);
1.24 markus 1587: }
1588:
1589: void
1.80 markus 1590: packet_get_bignum2(BIGNUM * value)
1.24 markus 1591: {
1.161 andreas 1592: buffer_get_bignum2(&active_state->incoming_packet, value);
1.170 djm 1593: }
1594:
1595: void
1596: packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point)
1597: {
1598: buffer_get_ecpoint(&active_state->incoming_packet, curve, point);
1.24 markus 1599: }
1.195 markus 1600: #endif
1.24 markus 1601:
1.76 stevesk 1602: void *
1.117 djm 1603: packet_get_raw(u_int *length_ptr)
1.24 markus 1604: {
1.161 andreas 1605: u_int bytes = buffer_len(&active_state->incoming_packet);
1.97 deraadt 1606:
1.24 markus 1607: if (length_ptr != NULL)
1608: *length_ptr = bytes;
1.161 andreas 1609: return buffer_ptr(&active_state->incoming_packet);
1.28 markus 1610: }
1611:
1612: int
1613: packet_remaining(void)
1614: {
1.161 andreas 1615: return buffer_len(&active_state->incoming_packet);
1.1 deraadt 1616: }
1617:
1.16 markus 1618: /*
1619: * Returns a string from the packet data. The string is allocated using
1620: * xmalloc; it is the responsibility of the calling program to free it when
1621: * no longer needed. The length_ptr argument may be NULL, or point to an
1622: * integer into which the length of the string is stored.
1623: */
1.1 deraadt 1624:
1.76 stevesk 1625: void *
1.40 markus 1626: packet_get_string(u_int *length_ptr)
1.1 deraadt 1627: {
1.161 andreas 1628: return buffer_get_string(&active_state->incoming_packet, length_ptr);
1.152 markus 1629: }
1630:
1.194 djm 1631: const void *
1.152 markus 1632: packet_get_string_ptr(u_int *length_ptr)
1633: {
1.161 andreas 1634: return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
1.169 djm 1635: }
1636:
1637: /* Ensures the returned string has no embedded \0 characters in it. */
1638: char *
1639: packet_get_cstring(u_int *length_ptr)
1640: {
1641: return buffer_get_cstring(&active_state->incoming_packet, length_ptr);
1.1 deraadt 1642: }
1643:
1.16 markus 1644: /*
1645: * Sends a diagnostic message from the server to the client. This message
1646: * can be sent at any time (but not while constructing another message). The
1647: * message is printed immediately, but only if the client is being executed
1648: * in verbose mode. These messages are primarily intended to ease debugging
1649: * authentication problems. The length of the formatted message must not
1650: * exceed 1024 bytes. This will automatically call packet_write_wait.
1651: */
1.1 deraadt 1652:
1.2 provos 1653: void
1.14 markus 1654: packet_send_debug(const char *fmt,...)
1.1 deraadt 1655: {
1.14 markus 1656: char buf[1024];
1657: va_list args;
1.39 markus 1658:
1659: if (compat20 && (datafellows & SSH_BUG_DEBUG))
1660: return;
1.14 markus 1661:
1662: va_start(args, fmt);
1663: vsnprintf(buf, sizeof(buf), fmt, args);
1664: va_end(args);
1665:
1.30 markus 1666: if (compat20) {
1667: packet_start(SSH2_MSG_DEBUG);
1668: packet_put_char(0); /* bool: always display */
1669: packet_put_cstring(buf);
1670: packet_put_cstring("");
1671: } else {
1672: packet_start(SSH_MSG_DEBUG);
1673: packet_put_cstring(buf);
1674: }
1.14 markus 1675: packet_send();
1676: packet_write_wait();
1.1 deraadt 1677: }
1678:
1.16 markus 1679: /*
1680: * Logs the error plus constructs and sends a disconnect packet, closes the
1681: * connection, and exits. This function never returns. The error message
1682: * should not contain a newline. The length of the formatted message must
1683: * not exceed 1024 bytes.
1684: */
1.1 deraadt 1685:
1.2 provos 1686: void
1.14 markus 1687: packet_disconnect(const char *fmt,...)
1.1 deraadt 1688: {
1.14 markus 1689: char buf[1024];
1690: va_list args;
1691: static int disconnecting = 0;
1.97 deraadt 1692:
1.14 markus 1693: if (disconnecting) /* Guard against recursive invocations. */
1694: fatal("packet_disconnect called recursively.");
1695: disconnecting = 1;
1696:
1.16 markus 1697: /*
1698: * Format the message. Note that the caller must make sure the
1699: * message is of limited size.
1700: */
1.14 markus 1701: va_start(args, fmt);
1702: vsnprintf(buf, sizeof(buf), fmt, args);
1703: va_end(args);
1704:
1.99 markus 1705: /* Display the error locally */
1.106 itojun 1706: logit("Disconnecting: %.100s", buf);
1.99 markus 1707:
1.14 markus 1708: /* Send the disconnect message to the other side, and wait for it to get sent. */
1.25 markus 1709: if (compat20) {
1710: packet_start(SSH2_MSG_DISCONNECT);
1711: packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1712: packet_put_cstring(buf);
1713: packet_put_cstring("");
1714: } else {
1715: packet_start(SSH_MSG_DISCONNECT);
1.65 markus 1716: packet_put_cstring(buf);
1.25 markus 1717: }
1.14 markus 1718: packet_send();
1719: packet_write_wait();
1720:
1721: /* Stop listening for connections. */
1.67 markus 1722: channel_close_all();
1.14 markus 1723:
1724: /* Close the connection. */
1725: packet_close();
1.112 markus 1726: cleanup_exit(255);
1.1 deraadt 1727: }
1728:
1.16 markus 1729: /* Checks if there is any buffered output, and tries to write some of the output. */
1.1 deraadt 1730:
1.2 provos 1731: void
1.73 itojun 1732: packet_write_poll(void)
1.1 deraadt 1733: {
1.161 andreas 1734: int len = buffer_len(&active_state->output);
1.163 andreas 1735: int cont;
1.97 deraadt 1736:
1.14 markus 1737: if (len > 0) {
1.163 andreas 1738: cont = 0;
1739: len = roaming_write(active_state->connection_out,
1740: buffer_ptr(&active_state->output), len, &cont);
1.156 djm 1741: if (len == -1) {
1742: if (errno == EINTR || errno == EAGAIN)
1.14 markus 1743: return;
1.156 djm 1744: fatal("Write failed: %.100s", strerror(errno));
1.14 markus 1745: }
1.163 andreas 1746: if (len == 0 && !cont)
1.156 djm 1747: fatal("Write connection closed");
1.161 andreas 1748: buffer_consume(&active_state->output, len);
1.14 markus 1749: }
1.1 deraadt 1750: }
1751:
1.16 markus 1752: /*
1753: * Calls packet_write_poll repeatedly until all pending output data has been
1754: * written.
1755: */
1.1 deraadt 1756:
1.2 provos 1757: void
1.73 itojun 1758: packet_write_wait(void)
1.1 deraadt 1759: {
1.56 millert 1760: fd_set *setp;
1.188 djm 1761: int ret, ms_remain = 0;
1.154 dtucker 1762: struct timeval start, timeout, *timeoutp = NULL;
1.56 millert 1763:
1.161 andreas 1764: setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1765: NFDBITS), sizeof(fd_mask));
1.14 markus 1766: packet_write_poll();
1767: while (packet_have_data_to_write()) {
1.161 andreas 1768: memset(setp, 0, howmany(active_state->connection_out + 1,
1769: NFDBITS) * sizeof(fd_mask));
1770: FD_SET(active_state->connection_out, setp);
1.154 dtucker 1771:
1.161 andreas 1772: if (active_state->packet_timeout_ms > 0) {
1773: ms_remain = active_state->packet_timeout_ms;
1.154 dtucker 1774: timeoutp = &timeout;
1775: }
1776: for (;;) {
1.161 andreas 1777: if (active_state->packet_timeout_ms != -1) {
1.154 dtucker 1778: ms_to_timeval(&timeout, ms_remain);
1779: gettimeofday(&start, NULL);
1780: }
1.161 andreas 1781: if ((ret = select(active_state->connection_out + 1,
1782: NULL, setp, NULL, timeoutp)) >= 0)
1.154 dtucker 1783: break;
1.161 andreas 1784: if (errno != EAGAIN && errno != EINTR)
1.154 dtucker 1785: break;
1.161 andreas 1786: if (active_state->packet_timeout_ms == -1)
1.154 dtucker 1787: continue;
1788: ms_subtract_diff(&start, &ms_remain);
1789: if (ms_remain <= 0) {
1790: ret = 0;
1791: break;
1792: }
1793: }
1794: if (ret == 0) {
1795: logit("Connection to %.200s timed out while "
1796: "waiting to write", get_remote_ipaddr());
1797: cleanup_exit(255);
1798: }
1.14 markus 1799: packet_write_poll();
1800: }
1.186 djm 1801: free(setp);
1.1 deraadt 1802: }
1803:
1804: /* Returns true if there is buffered data to write to the connection. */
1805:
1.2 provos 1806: int
1.73 itojun 1807: packet_have_data_to_write(void)
1.1 deraadt 1808: {
1.161 andreas 1809: return buffer_len(&active_state->output) != 0;
1.1 deraadt 1810: }
1811:
1812: /* Returns true if there is not too much data to write to the connection. */
1813:
1.2 provos 1814: int
1.73 itojun 1815: packet_not_very_much_data_to_write(void)
1.1 deraadt 1816: {
1.161 andreas 1817: if (active_state->interactive_mode)
1818: return buffer_len(&active_state->output) < 16384;
1.14 markus 1819: else
1.161 andreas 1820: return buffer_len(&active_state->output) < 128 * 1024;
1.1 deraadt 1821: }
1822:
1.102 markus 1823: static void
1.172 djm 1824: packet_set_tos(int tos)
1.101 markus 1825: {
1.173 djm 1826: if (!packet_connection_is_on_socket())
1.101 markus 1827: return;
1.173 djm 1828: switch (packet_connection_af()) {
1829: case AF_INET:
1830: debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1831: if (setsockopt(active_state->connection_in,
1832: IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
1833: error("setsockopt IP_TOS %d: %.100s:",
1834: tos, strerror(errno));
1835: break;
1836: case AF_INET6:
1837: debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1838: if (setsockopt(active_state->connection_in,
1839: IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
1840: error("setsockopt IPV6_TCLASS %d: %.100s:",
1841: tos, strerror(errno));
1842: break;
1843: }
1.101 markus 1844: }
1845:
1.1 deraadt 1846: /* Informs that the current session is interactive. Sets IP flags for that. */
1847:
1.2 provos 1848: void
1.172 djm 1849: packet_set_interactive(int interactive, int qos_interactive, int qos_bulk)
1.1 deraadt 1850: {
1.165 andreas 1851: if (active_state->set_interactive_called)
1.43 markus 1852: return;
1.165 andreas 1853: active_state->set_interactive_called = 1;
1.1 deraadt 1854:
1.14 markus 1855: /* Record that we are in interactive mode. */
1.161 andreas 1856: active_state->interactive_mode = interactive;
1.1 deraadt 1857:
1.19 markus 1858: /* Only set socket options if using a socket. */
1859: if (!packet_connection_is_on_socket())
1.14 markus 1860: return;
1.161 andreas 1861: set_nodelay(active_state->connection_in);
1.172 djm 1862: packet_set_tos(interactive ? qos_interactive : qos_bulk);
1.1 deraadt 1863: }
1864:
1865: /* Returns true if the current connection is interactive. */
1866:
1.2 provos 1867: int
1.73 itojun 1868: packet_is_interactive(void)
1.1 deraadt 1869: {
1.161 andreas 1870: return active_state->interactive_mode;
1.12 markus 1871: }
1872:
1.113 deraadt 1873: int
1.108 markus 1874: packet_set_maxsize(u_int s)
1.12 markus 1875: {
1.165 andreas 1876: if (active_state->set_maxsize_called) {
1.106 itojun 1877: logit("packet_set_maxsize: called twice: old %d new %d",
1.161 andreas 1878: active_state->max_packet_size, s);
1.14 markus 1879: return -1;
1880: }
1881: if (s < 4 * 1024 || s > 1024 * 1024) {
1.106 itojun 1882: logit("packet_set_maxsize: bad size %d", s);
1.14 markus 1883: return -1;
1884: }
1.165 andreas 1885: active_state->set_maxsize_called = 1;
1.66 markus 1886: debug("packet_set_maxsize: setting to %d", s);
1.161 andreas 1887: active_state->max_packet_size = s;
1.14 markus 1888: return s;
1.53 markus 1889: }
1890:
1.161 andreas 1891: int
1892: packet_inc_alive_timeouts(void)
1893: {
1894: return ++active_state->keep_alive_timeouts;
1895: }
1896:
1897: void
1898: packet_set_alive_timeouts(int ka)
1899: {
1900: active_state->keep_alive_timeouts = ka;
1901: }
1902:
1903: u_int
1904: packet_get_maxsize(void)
1905: {
1906: return active_state->max_packet_size;
1907: }
1908:
1.71 markus 1909: /* roundup current message to pad bytes */
1910: void
1911: packet_add_padding(u_char pad)
1912: {
1.161 andreas 1913: active_state->extra_pad = pad;
1.71 markus 1914: }
1915:
1.53 markus 1916: /*
1917: * 9.2. Ignored Data Message
1.61 markus 1918: *
1.53 markus 1919: * byte SSH_MSG_IGNORE
1920: * string data
1.61 markus 1921: *
1.53 markus 1922: * All implementations MUST understand (and ignore) this message at any
1923: * time (after receiving the protocol version). No implementation is
1924: * required to send them. This message can be used as an additional
1925: * protection measure against advanced traffic analysis techniques.
1926: */
1.54 markus 1927: void
1928: packet_send_ignore(int nbytes)
1929: {
1.115 avsm 1930: u_int32_t rnd = 0;
1.54 markus 1931: int i;
1932:
1933: packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
1.53 markus 1934: packet_put_int(nbytes);
1.75 deraadt 1935: for (i = 0; i < nbytes; i++) {
1.53 markus 1936: if (i % 4 == 0)
1.115 avsm 1937: rnd = arc4random();
1.129 deraadt 1938: packet_put_char((u_char)rnd & 0xff);
1.115 avsm 1939: rnd >>= 8;
1.53 markus 1940: }
1.105 markus 1941: }
1942:
1.113 deraadt 1943: #define MAX_PACKETS (1U<<31)
1.105 markus 1944: int
1945: packet_need_rekeying(void)
1946: {
1947: if (datafellows & SSH_BUG_NOREKEY)
1948: return 0;
1949: return
1.161 andreas 1950: (active_state->p_send.packets > MAX_PACKETS) ||
1951: (active_state->p_read.packets > MAX_PACKETS) ||
1952: (active_state->max_blocks_out &&
1953: (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1954: (active_state->max_blocks_in &&
1.184 dtucker 1955: (active_state->p_read.blocks > active_state->max_blocks_in)) ||
1956: (active_state->rekey_interval != 0 && active_state->rekey_time +
1.187 dtucker 1957: active_state->rekey_interval <= monotime());
1.105 markus 1958: }
1959:
1960: void
1.184 dtucker 1961: packet_set_rekey_limits(u_int32_t bytes, time_t seconds)
1.105 markus 1962: {
1.184 dtucker 1963: debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
1964: (int)seconds);
1.161 andreas 1965: active_state->rekey_limit = bytes;
1.184 dtucker 1966: active_state->rekey_interval = seconds;
1967: /*
1968: * We set the time here so that in post-auth privsep slave we count
1969: * from the completion of the authentication.
1970: */
1.187 dtucker 1971: active_state->rekey_time = monotime();
1.184 dtucker 1972: }
1973:
1974: time_t
1975: packet_get_rekey_timeout(void)
1976: {
1977: time_t seconds;
1978:
1979: seconds = active_state->rekey_time + active_state->rekey_interval -
1.187 dtucker 1980: monotime();
1.185 dtucker 1981: return (seconds <= 0 ? 1 : seconds);
1.118 markus 1982: }
1983:
1984: void
1985: packet_set_server(void)
1986: {
1.161 andreas 1987: active_state->server_side = 1;
1.118 markus 1988: }
1989:
1990: void
1991: packet_set_authenticated(void)
1992: {
1.161 andreas 1993: active_state->after_authentication = 1;
1994: }
1995:
1996: void *
1997: packet_get_input(void)
1998: {
1999: return (void *)&active_state->input;
2000: }
2001:
2002: void *
2003: packet_get_output(void)
2004: {
2005: return (void *)&active_state->output;
2006: }
2007:
2008: void *
2009: packet_get_newkeys(int mode)
2010: {
2011: return (void *)active_state->newkeys[mode];
1.166 andreas 2012: }
2013:
2014: /*
2015: * Save the state for the real connection, and use a separate state when
2016: * resuming a suspended connection.
2017: */
2018: void
2019: packet_backup_state(void)
2020: {
2021: struct session_state *tmp;
2022:
2023: close(active_state->connection_in);
2024: active_state->connection_in = -1;
2025: close(active_state->connection_out);
2026: active_state->connection_out = -1;
2027: if (backup_state)
2028: tmp = backup_state;
2029: else
2030: tmp = alloc_session_state();
2031: backup_state = active_state;
2032: active_state = tmp;
2033: }
2034:
2035: /*
2036: * Swap in the old state when resuming a connecion.
2037: */
2038: void
2039: packet_restore_state(void)
2040: {
2041: struct session_state *tmp;
2042: void *buf;
2043: u_int len;
2044:
2045: tmp = backup_state;
2046: backup_state = active_state;
2047: active_state = tmp;
2048: active_state->connection_in = backup_state->connection_in;
2049: backup_state->connection_in = -1;
2050: active_state->connection_out = backup_state->connection_out;
2051: backup_state->connection_out = -1;
2052: len = buffer_len(&backup_state->input);
2053: if (len > 0) {
2054: buf = buffer_ptr(&backup_state->input);
2055: buffer_append(&active_state->input, buf, len);
2056: buffer_clear(&backup_state->input);
2057: add_recv_bytes(len);
1.196 markus 2058: }
2059: }
2060:
2061: /* Reset after_authentication and reset compression in post-auth privsep */
2062: void
2063: packet_set_postauth(void)
2064: {
2065: Comp *comp;
2066: int mode;
2067:
2068: debug("%s: called", __func__);
2069: /* This was set in net child, but is not visible in user child */
2070: active_state->after_authentication = 1;
2071: active_state->rekeying = 0;
2072: for (mode = 0; mode < MODE_MAX; mode++) {
2073: if (active_state->newkeys[mode] == NULL)
2074: continue;
2075: comp = &active_state->newkeys[mode]->comp;
2076: if (comp && comp->enabled)
2077: packet_init_compression();
1.166 andreas 2078: }
1.1 deraadt 2079: }