blob: 6e7b87757f5f9865e9a5a6da92839d99ddb5cb31 [file] [log] [blame]
Damien Miller7acefbb2014-07-18 14:11:24 +10001/* $OpenBSD: packet.c,v 1.198 2014/07/15 15:54:14 millert Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * 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.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * 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".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
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.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
Damien Millere3b60b52006-07-10 21:08:03 +100042#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100043#include "openbsd-compat/sys-queue.h"
Damien Miller8dbffe72006-08-05 11:02:17 +100044#include <sys/param.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100046#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049
Damien Miller8ec8c3e2006-07-10 20:35:38 +100050#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110051#include <netinet/ip.h>
Darren Tuckerdace2332006-09-22 19:22:17 +100052#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100055#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100056#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100057#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100058#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100059#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100060#include <signal.h>
Darren Tuckerc53c2af2013-05-16 20:28:16 +100061#include <time.h>
Darren Tucker5d196262006-07-12 22:15:16 +100062
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063#include "xmalloc.h"
64#include "buffer.h"
65#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067#include "compress.h"
68#include "deattack.h"
Damien Miller33b13562000-04-04 14:38:59 +100069#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100071#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110072#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100073#include "key.h"
Damien Miller33b13562000-04-04 14:38:59 +100074#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000075#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
77#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110078#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100079#include "channels.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000080#include "ssh.h"
Damien Miller86687062014-07-02 15:28:02 +100081#include "ssherr.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100082#include "roaming.h"
Damien Miller33b13562000-04-04 14:38:59 +100083
84#ifdef PACKET_DEBUG
85#define DBG(x) x
86#else
87#define DBG(x)
88#endif
89
Damien Miller13ae44c2009-01-28 16:38:41 +110090#define PACKET_MAX_SIZE (256 * 1024)
91
Darren Tuckerf7288d72009-06-21 18:12:20 +100092struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100093 u_int32_t seqnr;
94 u_int32_t packets;
95 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +100096 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +100097};
Damien Miller13ae44c2009-01-28 16:38:41 +110098
Damien Millera5539d22003-04-09 20:50:06 +100099struct packet {
100 TAILQ_ENTRY(packet) next;
101 u_char type;
102 Buffer payload;
103};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000104
105struct session_state {
106 /*
107 * This variable contains the file descriptors used for
108 * communicating with the other side. connection_in is used for
109 * reading; connection_out for writing. These can be the same
110 * descriptor, in which case it is assumed to be a socket.
111 */
112 int connection_in;
113 int connection_out;
114
115 /* Protocol flags for the remote side. */
116 u_int remote_protocol_flags;
117
118 /* Encryption context for receiving data. Only used for decryption. */
119 CipherContext receive_context;
120
121 /* Encryption context for sending data. Only used for encryption. */
122 CipherContext send_context;
123
124 /* Buffer for raw input data from the socket. */
125 Buffer input;
126
127 /* Buffer for raw output data going to the socket. */
128 Buffer output;
129
130 /* Buffer for the partial outgoing packet being constructed. */
131 Buffer outgoing_packet;
132
133 /* Buffer for the incoming packet currently being processed. */
134 Buffer incoming_packet;
135
136 /* Scratch buffer for packet compression/decompression. */
137 Buffer compression_buffer;
138 int compression_buffer_ready;
139
140 /*
141 * Flag indicating whether packet compression/decompression is
142 * enabled.
143 */
144 int packet_compression;
145
146 /* default maximum packet size */
147 u_int max_packet_size;
148
149 /* Flag indicating whether this module has been initialized. */
150 int initialized;
151
152 /* Set to true if the connection is interactive. */
153 int interactive_mode;
154
155 /* Set to true if we are the server side. */
156 int server_side;
157
158 /* Set to true if we are authenticated. */
159 int after_authentication;
160
161 int keep_alive_timeouts;
162
163 /* The maximum time that we will wait to send or receive a packet */
164 int packet_timeout_ms;
165
166 /* Session key information for Encryption and MAC */
167 Newkeys *newkeys[MODE_MAX];
168 struct packet_state p_read, p_send;
169
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000170 /* Volume-based rekeying */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000171 u_int64_t max_blocks_in, max_blocks_out;
172 u_int32_t rekey_limit;
173
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000174 /* Time-based rekeying */
175 time_t rekey_interval; /* how often in seconds */
176 time_t rekey_time; /* time of last rekeying */
177
Darren Tuckerf7288d72009-06-21 18:12:20 +1000178 /* Session key for protocol v1 */
179 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
180 u_int ssh1_keylen;
181
182 /* roundup current message to extra_pad bytes */
183 u_char extra_pad;
184
185 /* XXX discard incoming data after MAC error */
186 u_int packet_discard;
187 Mac *packet_discard_mac;
188
189 /* Used in packet_read_poll2() */
190 u_int packlen;
191
Darren Tucker7b935c72009-06-21 18:59:36 +1000192 /* Used in packet_send2 */
193 int rekeying;
194
195 /* Used in packet_set_interactive */
196 int set_interactive_called;
197
198 /* Used in packet_set_maxsize */
199 int set_maxsize_called;
200
Darren Tuckerf7288d72009-06-21 18:12:20 +1000201 TAILQ_HEAD(, packet) outgoing;
202};
203
Darren Tuckere841eb02009-07-06 07:11:13 +1000204static struct session_state *active_state, *backup_state;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000205
206static struct session_state *
Darren Tuckerb422afa2009-06-21 18:58:46 +1000207alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000208{
Damien Miller7a221a12010-11-20 15:14:29 +1100209 struct session_state *s = xcalloc(1, sizeof(*s));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000210
Damien Miller7a221a12010-11-20 15:14:29 +1100211 s->connection_in = -1;
212 s->connection_out = -1;
213 s->max_packet_size = 32768;
214 s->packet_timeout_ms = -1;
215 return s;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000216}
Damien Millera5539d22003-04-09 20:50:06 +1000217
Damien Miller5428f641999-11-25 11:54:57 +1100218/*
219 * Sets the descriptors used for communication. Disables encryption until
220 * packet_set_encryption_key is called.
221 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222void
223packet_set_connection(int fd_in, int fd_out)
224{
Damien Millerea111192013-04-23 19:24:32 +1000225 const Cipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000226 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000227
Damien Miller874d77b2000-10-14 16:23:11 +1100228 if (none == NULL)
229 fatal("packet_set_connection: cannot load cipher 'none'");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000230 if (active_state == NULL)
231 active_state = alloc_session_state();
232 active_state->connection_in = fd_in;
233 active_state->connection_out = fd_out;
Damien Miller86687062014-07-02 15:28:02 +1000234 if ((r = cipher_init(&active_state->send_context, none,
235 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
236 (r = cipher_init(&active_state->receive_context, none,
237 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0)
238 fatal("%s: cipher_init: %s", __func__, ssh_err(r));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000239 active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
240 if (!active_state->initialized) {
241 active_state->initialized = 1;
242 buffer_init(&active_state->input);
243 buffer_init(&active_state->output);
244 buffer_init(&active_state->outgoing_packet);
245 buffer_init(&active_state->incoming_packet);
246 TAILQ_INIT(&active_state->outgoing);
247 active_state->p_send.packets = active_state->p_read.packets = 0;
Damien Miller95def091999-11-25 00:26:21 +1100248 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249}
250
Darren Tucker3fc464e2008-06-13 06:42:45 +1000251void
252packet_set_timeout(int timeout, int count)
253{
Damien Miller8ed4de82011-12-19 10:52:50 +1100254 if (timeout <= 0 || count <= 0) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000255 active_state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000256 return;
257 }
258 if ((INT_MAX / 1000) / count < timeout)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000259 active_state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000260 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000261 active_state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000262}
263
Damien Miller13ae44c2009-01-28 16:38:41 +1100264static void
265packet_stop_discard(void)
266{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000267 if (active_state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100268 char buf[1024];
269
270 memset(buf, 'a', sizeof(buf));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000271 while (buffer_len(&active_state->incoming_packet) <
272 PACKET_MAX_SIZE)
273 buffer_append(&active_state->incoming_packet, buf,
274 sizeof(buf));
275 (void) mac_compute(active_state->packet_discard_mac,
276 active_state->p_read.seqnr,
277 buffer_ptr(&active_state->incoming_packet),
Damien Miller13ae44c2009-01-28 16:38:41 +1100278 PACKET_MAX_SIZE);
279 }
280 logit("Finished discarding for %.200s", get_remote_ipaddr());
281 cleanup_exit(255);
282}
283
284static void
285packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
286{
Damien Milleraf43a7a2012-12-12 10:46:31 +1100287 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm))
Damien Miller13ae44c2009-01-28 16:38:41 +1100288 packet_disconnect("Packet corrupt");
289 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000290 active_state->packet_discard_mac = mac;
291 if (buffer_len(&active_state->input) >= discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100292 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +1000293 active_state->packet_discard = discard -
294 buffer_len(&active_state->input);
Damien Miller13ae44c2009-01-28 16:38:41 +1100295}
296
Damien Miller34132e52000-01-14 15:45:46 +1100297/* Returns 1 if remote host is connected via socket, 0 if not. */
298
299int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000300packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100301{
302 struct sockaddr_storage from, to;
303 socklen_t fromlen, tolen;
304
305 /* filedescriptors in and out are the same, so it's a socket */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000306 if (active_state->connection_in == active_state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100307 return 1;
308 fromlen = sizeof(from);
309 memset(&from, 0, sizeof(from));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000310 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
311 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100312 return 0;
313 tolen = sizeof(to);
314 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000315 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
316 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100317 return 0;
318 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
319 return 0;
320 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
321 return 0;
322 return 1;
323}
324
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000325/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000326 * Exports an IV from the CipherContext required to export the key
327 * state back from the unprivileged child to the privileged parent
328 * process.
329 */
330
331void
332packet_get_keyiv(int mode, u_char *iv, u_int len)
333{
334 CipherContext *cc;
Damien Miller86687062014-07-02 15:28:02 +1000335 int r;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000336
337 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000338 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000339 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000340 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000341
Damien Miller86687062014-07-02 15:28:02 +1000342 if ((r = cipher_get_keyiv(cc, iv, len)) != 0)
343 fatal("%s: cipher_get_keyiv: %s", __func__, ssh_err(r));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000344}
345
346int
347packet_get_keycontext(int mode, u_char *dat)
348{
349 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000350
Ben Lindstromf6027d32002-03-22 01:42:04 +0000351 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000352 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000353 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000354 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000355
356 return (cipher_get_keycontext(cc, dat));
357}
358
359void
360packet_set_keycontext(int mode, u_char *dat)
361{
362 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000363
Ben Lindstromf6027d32002-03-22 01:42:04 +0000364 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000365 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000366 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000367 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000368
369 cipher_set_keycontext(cc, dat);
370}
371
372int
373packet_get_keyiv_len(int mode)
374{
375 CipherContext *cc;
376
377 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000378 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000379 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000380 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000381
382 return (cipher_get_keyiv_len(cc));
383}
Damien Miller4f7becb2006-03-26 14:10:14 +1100384
Ben Lindstromf6027d32002-03-22 01:42:04 +0000385void
386packet_set_iv(int mode, u_char *dat)
387{
388 CipherContext *cc;
Damien Miller86687062014-07-02 15:28:02 +1000389 int r;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000390
391 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000392 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000393 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000394 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000395
Damien Miller86687062014-07-02 15:28:02 +1000396 if ((r = cipher_set_keyiv(cc, dat)) != 0)
397 fatal("%s: cipher_set_keyiv: %s", __func__, ssh_err(r));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000398}
Damien Miller4f7becb2006-03-26 14:10:14 +1100399
Ben Lindstromf6027d32002-03-22 01:42:04 +0000400int
Damien Miller2b92d322003-06-11 22:05:06 +1000401packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000402{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000403 return (cipher_get_number(active_state->receive_context.cipher));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000404}
405
Damien Millera5539d22003-04-09 20:50:06 +1000406void
Damien Miller7a221a12010-11-20 15:14:29 +1100407packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks,
408 u_int32_t *packets, u_int64_t *bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000409{
Damien Millera5539d22003-04-09 20:50:06 +1000410 struct packet_state *state;
411
Darren Tuckerf7288d72009-06-21 18:12:20 +1000412 state = (mode == MODE_IN) ?
413 &active_state->p_read : &active_state->p_send;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000414 if (seqnr)
415 *seqnr = state->seqnr;
416 if (blocks)
417 *blocks = state->blocks;
418 if (packets)
419 *packets = state->packets;
420 if (bytes)
421 *bytes = state->bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000422}
423
424void
Damien Millerb61f3fc2008-07-11 17:36:48 +1000425packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
426 u_int64_t bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000427{
Damien Millera5539d22003-04-09 20:50:06 +1000428 struct packet_state *state;
429
Darren Tuckerf7288d72009-06-21 18:12:20 +1000430 state = (mode == MODE_IN) ?
431 &active_state->p_read : &active_state->p_send;
Damien Millera5539d22003-04-09 20:50:06 +1000432 state->seqnr = seqnr;
433 state->blocks = blocks;
434 state->packets = packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000435 state->bytes = bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000436}
437
Damien Millerd2ac5d72011-05-15 08:43:13 +1000438static int
439packet_connection_af(void)
Damien Miller34132e52000-01-14 15:45:46 +1100440{
441 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100442 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100443
444 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000445 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
446 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100447 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000448#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100449 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000450 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000451 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000452#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000453 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100454}
455
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456/* Sets the connection into non-blocking mode. */
457
458void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000459packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460{
Damien Miller95def091999-11-25 00:26:21 +1100461 /* Set the socket into non-blocking mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000462 set_nonblock(active_state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
Darren Tuckerf7288d72009-06-21 18:12:20 +1000464 if (active_state->connection_out != active_state->connection_in)
465 set_nonblock(active_state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466}
467
468/* Returns the socket used for reading. */
469
470int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000471packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000473 return active_state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474}
475
476/* Returns the descriptor used for writing. */
477
478int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000479packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000481 return active_state->connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482}
483
484/* Closes the connection and clears and frees internal data structures. */
485
486void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000487packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000489 if (!active_state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100490 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000491 active_state->initialized = 0;
492 if (active_state->connection_in == active_state->connection_out) {
493 shutdown(active_state->connection_out, SHUT_RDWR);
494 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100495 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000496 close(active_state->connection_in);
497 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100498 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000499 buffer_free(&active_state->input);
500 buffer_free(&active_state->output);
501 buffer_free(&active_state->outgoing_packet);
502 buffer_free(&active_state->incoming_packet);
503 if (active_state->compression_buffer_ready) {
504 buffer_free(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100505 buffer_compress_uninit();
506 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000507 cipher_cleanup(&active_state->send_context);
508 cipher_cleanup(&active_state->receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509}
510
511/* Sets remote side protocol flags. */
512
513void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000514packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000516 active_state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517}
518
519/* Returns the remote protocol flags set earlier by the above function. */
520
Ben Lindstrom46c16222000-12-22 01:43:59 +0000521u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000522packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000524 return active_state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525}
526
Damien Miller5428f641999-11-25 11:54:57 +1100527/*
528 * Starts packet compression from the next packet on in both directions.
529 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
530 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531
Ben Lindstrombba81212001-06-25 05:01:22 +0000532static void
533packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000534{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000535 if (active_state->compression_buffer_ready == 1)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000536 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000537 active_state->compression_buffer_ready = 1;
538 buffer_init(&active_state->compression_buffer);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000539}
540
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541void
542packet_start_compression(int level)
543{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000544 if (active_state->packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100545 fatal("Compression already enabled.");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000546 active_state->packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000547 packet_init_compression();
548 buffer_compress_init_send(level);
549 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550}
551
Damien Miller5428f641999-11-25 11:54:57 +1100552/*
Damien Miller5428f641999-11-25 11:54:57 +1100553 * Causes any further packets to be encrypted using the given key. The same
554 * key is used for both sending and reception. However, both directions are
555 * encrypted independently of each other.
556 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000557
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558void
Damien Miller7a221a12010-11-20 15:14:29 +1100559packet_set_encryption_key(const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560{
Damien Millerea111192013-04-23 19:24:32 +1000561 const Cipher *cipher = cipher_by_number(number);
Damien Miller86687062014-07-02 15:28:02 +1000562 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000563
Damien Miller874d77b2000-10-14 16:23:11 +1100564 if (cipher == NULL)
565 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000566 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100567 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000568 if (keylen > SSH_SESSION_KEY_LENGTH)
569 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000570 memcpy(active_state->ssh1_key, key, keylen);
571 active_state->ssh1_keylen = keylen;
Damien Miller86687062014-07-02 15:28:02 +1000572 if ((r = cipher_init(&active_state->send_context, cipher,
573 key, keylen, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
574 (r = cipher_init(&active_state->receive_context, cipher,
575 key, keylen, NULL, 0, CIPHER_DECRYPT)) != 0)
576 fatal("%s: cipher_init: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577}
578
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000579u_int
580packet_get_encryption_key(u_char *key)
581{
582 if (key == NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000583 return (active_state->ssh1_keylen);
584 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
585 return (active_state->ssh1_keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000586}
587
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000588/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000590packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000592 u_char buf[9];
593 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594
Ben Lindstrom204e4882001-03-05 06:47:00 +0000595 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000596 len = compat20 ? 6 : 9;
597 memset(buf, 0, len - 1);
598 buf[len - 1] = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000599 buffer_clear(&active_state->outgoing_packet);
600 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000601}
602
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000603/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604void
605packet_put_char(int value)
606{
Damien Miller95def091999-11-25 00:26:21 +1100607 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000608
Darren Tuckerf7288d72009-06-21 18:12:20 +1000609 buffer_append(&active_state->outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610}
Damien Miller4f7becb2006-03-26 14:10:14 +1100611
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000613packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000615 buffer_put_int(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616}
Damien Miller4f7becb2006-03-26 14:10:14 +1100617
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618void
Darren Tucker761c3892009-06-21 18:16:26 +1000619packet_put_int64(u_int64_t value)
620{
621 buffer_put_int64(&active_state->outgoing_packet, value);
622}
623
624void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100625packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000627 buffer_put_string(&active_state->outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628}
Damien Miller4f7becb2006-03-26 14:10:14 +1100629
Damien Miller33b13562000-04-04 14:38:59 +1000630void
631packet_put_cstring(const char *str)
632{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000633 buffer_put_cstring(&active_state->outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000634}
Damien Miller4f7becb2006-03-26 14:10:14 +1100635
Damien Miller33b13562000-04-04 14:38:59 +1000636void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100637packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000638{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000639 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000640}
Damien Miller4f7becb2006-03-26 14:10:14 +1100641
Damien Miller1f0311c2014-05-15 14:24:09 +1000642#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643void
Damien Miller95def091999-11-25 00:26:21 +1100644packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000645{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000646 buffer_put_bignum(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647}
Damien Miller4f7becb2006-03-26 14:10:14 +1100648
Damien Miller33b13562000-04-04 14:38:59 +1000649void
650packet_put_bignum2(BIGNUM * value)
651{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000652 buffer_put_bignum2(&active_state->outgoing_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +1000653}
Damien Miller1f0311c2014-05-15 14:24:09 +1000654#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655
Damien Miller6af914a2010-09-10 11:39:26 +1000656#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000657void
658packet_put_ecpoint(const EC_GROUP *curve, const EC_POINT *point)
659{
660 buffer_put_ecpoint(&active_state->outgoing_packet, curve, point);
661}
Damien Miller6af914a2010-09-10 11:39:26 +1000662#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000663
Damien Miller5428f641999-11-25 11:54:57 +1100664/*
665 * Finalizes and sends the packet. If the encryption key has been set,
666 * encrypts the packet before sending.
667 */
Damien Miller95def091999-11-25 00:26:21 +1100668
Ben Lindstrombba81212001-06-25 05:01:22 +0000669static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000670packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000672 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100673 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000674 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000675 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676
Damien Miller5428f641999-11-25 11:54:57 +1100677 /*
678 * If using packet compression, compress the payload of the outgoing
679 * packet.
680 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000681 if (active_state->packet_compression) {
682 buffer_clear(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100683 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000684 buffer_consume(&active_state->outgoing_packet, 8);
Damien Miller95def091999-11-25 00:26:21 +1100685 /* padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000686 buffer_append(&active_state->compression_buffer,
687 "\0\0\0\0\0\0\0\0", 8);
688 buffer_compress(&active_state->outgoing_packet,
689 &active_state->compression_buffer);
690 buffer_clear(&active_state->outgoing_packet);
691 buffer_append(&active_state->outgoing_packet,
692 buffer_ptr(&active_state->compression_buffer),
693 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100694 }
695 /* Compute packet length without padding (add checksum, remove padding). */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000696 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697
Damien Millere247cc42000-05-07 12:03:14 +1000698 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100699 padding = 8 - len % 8;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000700 if (!active_state->send_context.plaintext) {
701 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller95def091999-11-25 00:26:21 +1100702 for (i = 0; i < padding; i++) {
703 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000704 rnd = arc4random();
705 cp[7 - i] = rnd & 0xff;
706 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100707 }
708 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000709 buffer_consume(&active_state->outgoing_packet, 8 - padding);
Damien Miller95def091999-11-25 00:26:21 +1100710
711 /* Add check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000712 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
713 buffer_len(&active_state->outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100714 put_u32(buf, checksum);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000715 buffer_append(&active_state->outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716
717#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100718 fprintf(stderr, "packet_send plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000719 buffer_dump(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720#endif
721
Damien Miller95def091999-11-25 00:26:21 +1100722 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100723 put_u32(buf, len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000724 buffer_append(&active_state->output, buf, 4);
725 cp = buffer_append_space(&active_state->output,
726 buffer_len(&active_state->outgoing_packet));
Damien Millerbcd00ab2013-12-07 10:41:55 +1100727 if (cipher_crypt(&active_state->send_context, 0, cp,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000728 buffer_ptr(&active_state->outgoing_packet),
Damien Millerbcd00ab2013-12-07 10:41:55 +1100729 buffer_len(&active_state->outgoing_packet), 0, 0) != 0)
730 fatal("%s: cipher_crypt failed", __func__);
Damien Miller95def091999-11-25 00:26:21 +1100731
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100733 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000734 buffer_dump(&active_state->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +1000736 active_state->p_send.packets++;
737 active_state->p_send.bytes += len +
738 buffer_len(&active_state->outgoing_packet);
739 buffer_clear(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740
Damien Miller5428f641999-11-25 11:54:57 +1100741 /*
Damien Miller788f2122005-11-05 15:14:59 +1100742 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100743 * actually sent until packet_write_wait or packet_write_poll is
744 * called.
745 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746}
747
Ben Lindstromf6027d32002-03-22 01:42:04 +0000748void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000749set_newkeys(int mode)
750{
751 Enc *enc;
752 Mac *mac;
753 Comp *comp;
754 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000755 u_int64_t *max_blocks;
Damien Miller86687062014-07-02 15:28:02 +1000756 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000757
Ben Lindstrom064496f2002-12-23 02:04:22 +0000758 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000759
Damien Miller963f6b22002-02-19 15:21:23 +1100760 if (mode == MODE_OUT) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000761 cc = &active_state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000762 crypt_type = CIPHER_ENCRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000763 active_state->p_send.packets = active_state->p_send.blocks = 0;
764 max_blocks = &active_state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100765 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000766 cc = &active_state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000767 crypt_type = CIPHER_DECRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000768 active_state->p_read.packets = active_state->p_read.blocks = 0;
769 max_blocks = &active_state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100770 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000771 if (active_state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000772 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100773 cipher_cleanup(cc);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000774 enc = &active_state->newkeys[mode]->enc;
775 mac = &active_state->newkeys[mode]->mac;
776 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000777 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100778 explicit_bzero(enc->iv, enc->iv_len);
779 explicit_bzero(enc->key, enc->key_len);
780 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000781 free(enc->name);
782 free(enc->iv);
783 free(enc->key);
784 free(mac->name);
785 free(mac->key);
786 free(comp->name);
787 free(active_state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000788 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000789 active_state->newkeys[mode] = kex_get_newkeys(mode);
790 if (active_state->newkeys[mode] == NULL)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000791 fatal("newkeys: no keys for mode %d", mode);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000792 enc = &active_state->newkeys[mode]->enc;
793 mac = &active_state->newkeys[mode]->mac;
794 comp = &active_state->newkeys[mode]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100795 if (cipher_authlen(enc->cipher) == 0 && mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000796 mac->enabled = 1;
797 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000798 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
799 enc->iv, enc->iv_len, crypt_type)) != 0)
800 fatal("%s: cipher_init: %s", __func__, ssh_err(r));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000801 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100802 /* explicit_bzero(enc->iv, enc->block_size);
803 explicit_bzero(enc->key, enc->key_len);
804 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000805 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000806 (comp->type == COMP_DELAYED &&
807 active_state->after_authentication)) && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000808 packet_init_compression();
809 if (mode == MODE_OUT)
810 buffer_compress_init_send(6);
811 else
812 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000813 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000814 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000815 /*
816 * The 2^(blocksize*2) limit is too expensive for 3DES,
817 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
818 */
819 if (enc->block_size >= 16)
820 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
821 else
822 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000823 if (active_state->rekey_limit)
824 *max_blocks = MIN(*max_blocks,
825 active_state->rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000826}
827
Damien Miller5428f641999-11-25 11:54:57 +1100828/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000829 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000830 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000831 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
832 */
833static void
834packet_enable_delayed_compress(void)
835{
836 Comp *comp = NULL;
837 int mode;
838
839 /*
840 * Remember that we are past the authentication step, so rekeying
841 * with COMP_DELAYED will turn on compression immediately.
842 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000843 active_state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +1000844 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000845 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000846 if (active_state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +1000847 continue;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000848 comp = &active_state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +1000849 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000850 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000851 if (mode == MODE_OUT)
852 buffer_compress_init_send(6);
853 else
854 buffer_compress_init_recv();
855 comp->enabled = 1;
856 }
857 }
858}
859
860/*
Damien Miller33b13562000-04-04 14:38:59 +1000861 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
862 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000863static void
Damien Millera5539d22003-04-09 20:50:06 +1000864packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000865{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000866 u_char type, *cp, *macbuf = NULL;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100867 u_char padlen, pad = 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100868 u_int i, len, authlen = 0, aadlen = 0;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000869 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000870 Enc *enc = NULL;
871 Mac *mac = NULL;
872 Comp *comp = NULL;
873 int block_size;
874
Darren Tuckerf7288d72009-06-21 18:12:20 +1000875 if (active_state->newkeys[MODE_OUT] != NULL) {
876 enc = &active_state->newkeys[MODE_OUT]->enc;
877 mac = &active_state->newkeys[MODE_OUT]->mac;
878 comp = &active_state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100879 /* disable mac for authenticated encryption */
880 if ((authlen = cipher_authlen(enc->cipher)) != 0)
881 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +1000882 }
Damien Miller963f6b22002-02-19 15:21:23 +1100883 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +1100884 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +1000885
Darren Tuckerf7288d72009-06-21 18:12:20 +1000886 cp = buffer_ptr(&active_state->outgoing_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000887 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000888
889#ifdef PACKET_DEBUG
890 fprintf(stderr, "plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000891 buffer_dump(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000892#endif
893
894 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000895 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000896 /* skip header, compress only payload */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000897 buffer_consume(&active_state->outgoing_packet, 5);
898 buffer_clear(&active_state->compression_buffer);
899 buffer_compress(&active_state->outgoing_packet,
900 &active_state->compression_buffer);
901 buffer_clear(&active_state->outgoing_packet);
902 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
903 buffer_append(&active_state->outgoing_packet,
904 buffer_ptr(&active_state->compression_buffer),
905 buffer_len(&active_state->compression_buffer));
Damien Miller33b13562000-04-04 14:38:59 +1000906 DBG(debug("compression: raw %d compressed %d", len,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000907 buffer_len(&active_state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +1000908 }
909
910 /* sizeof (packet_len + pad_len + payload) */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000911 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000912
913 /*
914 * calc size of padding, alloc space, get random data,
915 * minimum padding is 4 bytes
916 */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100917 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +1000918 padlen = block_size - (len % block_size);
919 if (padlen < 4)
920 padlen += block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000921 if (active_state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +1100922 /* will wrap if extra_pad+padlen > 255 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000923 active_state->extra_pad =
924 roundup(active_state->extra_pad, block_size);
925 pad = active_state->extra_pad -
926 ((len + padlen) % active_state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +1000927 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
928 __func__, pad, len, padlen, active_state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +1100929 padlen += pad;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000930 active_state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100931 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000932 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
933 if (enc && !active_state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000934 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000935 for (i = 0; i < padlen; i++) {
936 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000937 rnd = arc4random();
938 cp[i] = rnd & 0xff;
939 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000940 }
Damien Millere247cc42000-05-07 12:03:14 +1000941 } else {
942 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +1100943 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000944 }
Damien Milleraf43a7a2012-12-12 10:46:31 +1100945 /* sizeof (packet_len + pad_len + payload + padding) */
946 len = buffer_len(&active_state->outgoing_packet);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000947 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100948 /* packet_length includes payload, padding and padding length field */
949 put_u32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000950 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100951 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
952 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +1000953
954 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100955 if (mac && mac->enabled && !mac->etm) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000956 macbuf = mac_compute(mac, active_state->p_send.seqnr,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100957 buffer_ptr(&active_state->outgoing_packet), len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000958 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000959 }
960 /* encrypt packet and append to output buffer. */
Damien Miller1d75abf2013-01-09 16:12:19 +1100961 cp = buffer_append_space(&active_state->output, len + authlen);
Damien Millerbcd00ab2013-12-07 10:41:55 +1100962 if (cipher_crypt(&active_state->send_context, active_state->p_send.seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100963 cp, buffer_ptr(&active_state->outgoing_packet),
Damien Millerbcd00ab2013-12-07 10:41:55 +1100964 len - aadlen, aadlen, authlen) != 0)
965 fatal("%s: cipher_crypt failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +1000966 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100967 if (mac && mac->enabled) {
968 if (mac->etm) {
969 /* EtM: compute mac over aadlen + cipher text */
970 macbuf = mac_compute(mac,
971 active_state->p_send.seqnr, cp, len);
972 DBG(debug("done calc MAC(EtM) out #%d",
973 active_state->p_send.seqnr));
974 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000975 buffer_append(&active_state->output, macbuf, mac->mac_len);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100976 }
Damien Miller33b13562000-04-04 14:38:59 +1000977#ifdef PACKET_DEBUG
978 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000979 buffer_dump(&active_state->output);
Damien Miller33b13562000-04-04 14:38:59 +1000980#endif
Damien Miller4af51302000-04-16 11:18:38 +1000981 /* increment sequence number for outgoing packets */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000982 if (++active_state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000983 logit("outgoing seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000984 if (++active_state->p_send.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +1000985 if (!(datafellows & SSH_BUG_NOREKEY))
986 fatal("XXX too many packets with same key");
Damien Milleraf43a7a2012-12-12 10:46:31 +1100987 active_state->p_send.blocks += len / block_size;
988 active_state->p_send.bytes += len;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000989 buffer_clear(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000990
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000991 if (type == SSH2_MSG_NEWKEYS)
992 set_newkeys(MODE_OUT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000993 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +1000994 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000995}
996
Damien Millera5539d22003-04-09 20:50:06 +1000997static void
998packet_send2(void)
999{
Damien Millera5539d22003-04-09 20:50:06 +10001000 struct packet *p;
1001 u_char type, *cp;
1002
Darren Tuckerf7288d72009-06-21 18:12:20 +10001003 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Millera5539d22003-04-09 20:50:06 +10001004 type = cp[5];
1005
1006 /* during rekeying we can only send key exchange messages */
Darren Tucker7b935c72009-06-21 18:59:36 +10001007 if (active_state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001008 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1009 (type > SSH2_MSG_TRANSPORT_MAX) ||
1010 (type == SSH2_MSG_SERVICE_REQUEST) ||
1011 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +10001012 debug("enqueue packet: %u", type);
Damien Miller6c81fee2013-11-08 12:19:55 +11001013 p = xcalloc(1, sizeof(*p));
Damien Millera5539d22003-04-09 20:50:06 +10001014 p->type = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001015 memcpy(&p->payload, &active_state->outgoing_packet,
1016 sizeof(Buffer));
1017 buffer_init(&active_state->outgoing_packet);
1018 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +10001019 return;
1020 }
1021 }
1022
1023 /* rekeying starts with sending KEXINIT */
1024 if (type == SSH2_MSG_KEXINIT)
Darren Tucker7b935c72009-06-21 18:59:36 +10001025 active_state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001026
1027 packet_send2_wrapped();
1028
1029 /* after a NEWKEYS message we can send the complete queue */
1030 if (type == SSH2_MSG_NEWKEYS) {
Darren Tucker7b935c72009-06-21 18:59:36 +10001031 active_state->rekeying = 0;
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001032 active_state->rekey_time = monotime();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001033 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001034 type = p->type;
1035 debug("dequeue packet: %u", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001036 buffer_free(&active_state->outgoing_packet);
1037 memcpy(&active_state->outgoing_packet, &p->payload,
Damien Millera5539d22003-04-09 20:50:06 +10001038 sizeof(Buffer));
Darren Tuckerf7288d72009-06-21 18:12:20 +10001039 TAILQ_REMOVE(&active_state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001040 free(p);
Damien Millera5539d22003-04-09 20:50:06 +10001041 packet_send2_wrapped();
1042 }
1043 }
1044}
1045
Damien Miller33b13562000-04-04 14:38:59 +10001046void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001047packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +10001048{
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001049 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +10001050 packet_send2();
1051 else
1052 packet_send1();
1053 DBG(debug("packet_send done"));
1054}
1055
Damien Miller33b13562000-04-04 14:38:59 +10001056/*
Damien Miller5428f641999-11-25 11:54:57 +11001057 * Waits until a packet has been received, and returns its type. Note that
1058 * no other data is processed until this returns, so this function should not
1059 * be used during the interactive session.
1060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001061
1062int
Damien Millerdff50992002-01-22 23:16:32 +11001063packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001064{
Damien Millerce986542013-07-18 16:12:44 +10001065 int type, len, ret, cont, ms_remain = 0;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001066 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001067 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001068 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069
Darren Tucker99bb7612008-06-13 22:02:50 +10001070 DBG(debug("packet_read()"));
1071
Darren Tuckerf7288d72009-06-21 18:12:20 +10001072 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1073 NFDBITS), sizeof(fd_mask));
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001074
Damien Miller95def091999-11-25 00:26:21 +11001075 /* Since we are blocking, ensure that all written packets have been sent. */
1076 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077
Damien Miller95def091999-11-25 00:26:21 +11001078 /* Stay in the loop until we have received a complete packet. */
1079 for (;;) {
1080 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +11001081 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001082 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +10001083 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +11001084 || type == SSH_SMSG_FAILURE
1085 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +10001086 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +11001087 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001088 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001089 if (type != SSH_MSG_NONE) {
Darren Tuckera627d422013-06-02 07:31:17 +10001090 free(setp);
Damien Miller95def091999-11-25 00:26:21 +11001091 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001092 }
Damien Miller5428f641999-11-25 11:54:57 +11001093 /*
1094 * Otherwise, wait for some data to arrive, add it to the
1095 * buffer, and try again.
1096 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001097 memset(setp, 0, howmany(active_state->connection_in + 1,
1098 NFDBITS) * sizeof(fd_mask));
1099 FD_SET(active_state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001100
Darren Tuckerf7288d72009-06-21 18:12:20 +10001101 if (active_state->packet_timeout_ms > 0) {
1102 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001103 timeoutp = &timeout;
1104 }
Damien Miller95def091999-11-25 00:26:21 +11001105 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001106 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001107 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001108 ms_to_timeval(&timeout, ms_remain);
1109 gettimeofday(&start, NULL);
1110 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001111 if ((ret = select(active_state->connection_in + 1, setp,
1112 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001113 break;
Damien Millerea437422009-10-02 11:49:03 +10001114 if (errno != EAGAIN && errno != EINTR &&
1115 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001116 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001117 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001118 continue;
1119 ms_subtract_diff(&start, &ms_remain);
1120 if (ms_remain <= 0) {
1121 ret = 0;
1122 break;
1123 }
1124 }
1125 if (ret == 0) {
1126 logit("Connection to %.200s timed out while "
1127 "waiting to read", get_remote_ipaddr());
1128 cleanup_exit(255);
1129 }
Damien Miller95def091999-11-25 00:26:21 +11001130 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001131 do {
1132 cont = 0;
1133 len = roaming_read(active_state->connection_in, buf,
1134 sizeof(buf), &cont);
1135 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001136 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001137 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +10001138 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001139 }
Damien Miller95def091999-11-25 00:26:21 +11001140 if (len < 0)
1141 fatal("Read from socket failed: %.100s", strerror(errno));
1142 /* Append it to the buffer. */
1143 packet_process_incoming(buf, len);
1144 }
1145 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146}
1147
Damien Miller278f9072001-12-21 15:00:19 +11001148int
Damien Millerdff50992002-01-22 23:16:32 +11001149packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +11001150{
Damien Millerdff50992002-01-22 23:16:32 +11001151 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001152}
1153
Damien Miller5428f641999-11-25 11:54:57 +11001154/*
1155 * Waits until a packet has been received, verifies that its type matches
1156 * that given, and gives a fatal error and exits if there is a mismatch.
1157 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158
1159void
Damien Millerdff50992002-01-22 23:16:32 +11001160packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001161{
Damien Miller95def091999-11-25 00:26:21 +11001162 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001163
Damien Millerdff50992002-01-22 23:16:32 +11001164 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001165 if (type != expected_type)
1166 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001167 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168}
1169
1170/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001171 * packet_process_incoming. If so, reads the packet; otherwise returns
1172 * SSH_MSG_NONE. This does not wait for data from the connection.
1173 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001174 * SSH_MSG_DISCONNECT is handled specially here. Also,
1175 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1176 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001177 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178
Ben Lindstrombba81212001-06-25 05:01:22 +00001179static int
Damien Millerdff50992002-01-22 23:16:32 +11001180packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001182 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001183 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001184 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185
Damien Miller95def091999-11-25 00:26:21 +11001186 /* Check if input size is less than minimum packet size. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001187 if (buffer_len(&active_state->input) < 4 + 8)
Damien Miller95def091999-11-25 00:26:21 +11001188 return SSH_MSG_NONE;
1189 /* Get length of incoming packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001190 cp = buffer_ptr(&active_state->input);
Damien Miller3f941882006-03-31 23:13:02 +11001191 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001192 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001193 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001194 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001195
Damien Miller95def091999-11-25 00:26:21 +11001196 /* Check if the packet has been entirely received. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001197 if (buffer_len(&active_state->input) < 4 + padded_len)
Damien Miller95def091999-11-25 00:26:21 +11001198 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199
Damien Miller95def091999-11-25 00:26:21 +11001200 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201
Damien Miller95def091999-11-25 00:26:21 +11001202 /* Consume packet length. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001203 buffer_consume(&active_state->input, 4);
Damien Miller95def091999-11-25 00:26:21 +11001204
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001205 /*
1206 * Cryptographic attack detector for ssh
1207 * (C)1998 CORE-SDI, Buenos Aires Argentina
1208 * Ariel Futoransky(futo@core-sdi.com)
1209 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001210 if (!active_state->receive_context.plaintext) {
1211 switch (detect_attack(buffer_ptr(&active_state->input),
1212 padded_len)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001213 case DEATTACK_DETECTED:
1214 packet_disconnect("crc32 compensation attack: "
1215 "network attack detected");
1216 case DEATTACK_DOS_DETECTED:
1217 packet_disconnect("deattack denial of "
1218 "service detected");
1219 }
1220 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001221
1222 /* Decrypt data to incoming_packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001223 buffer_clear(&active_state->incoming_packet);
1224 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
Damien Millerbcd00ab2013-12-07 10:41:55 +11001225 if (cipher_crypt(&active_state->receive_context, 0, cp,
1226 buffer_ptr(&active_state->input), padded_len, 0, 0) != 0)
1227 fatal("%s: cipher_crypt failed", __func__);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001228
Darren Tuckerf7288d72009-06-21 18:12:20 +10001229 buffer_consume(&active_state->input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230
1231#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001232 fprintf(stderr, "read_poll plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001233 buffer_dump(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235
Damien Miller95def091999-11-25 00:26:21 +11001236 /* Compute packet checksum. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001237 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1238 buffer_len(&active_state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001239
Damien Miller95def091999-11-25 00:26:21 +11001240 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001241 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001242
Damien Miller95def091999-11-25 00:26:21 +11001243 /* Test check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001244 if (len != buffer_len(&active_state->incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001245 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001246 len, buffer_len(&active_state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247
Darren Tuckerf7288d72009-06-21 18:12:20 +10001248 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001249 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001250 if (checksum != stored_checksum)
1251 packet_disconnect("Corrupted check bytes on input.");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001252 buffer_consume_end(&active_state->incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253
Darren Tuckerf7288d72009-06-21 18:12:20 +10001254 if (active_state->packet_compression) {
1255 buffer_clear(&active_state->compression_buffer);
1256 buffer_uncompress(&active_state->incoming_packet,
1257 &active_state->compression_buffer);
1258 buffer_clear(&active_state->incoming_packet);
1259 buffer_append(&active_state->incoming_packet,
1260 buffer_ptr(&active_state->compression_buffer),
1261 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001262 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001263 active_state->p_read.packets++;
1264 active_state->p_read.bytes += padded_len + 4;
1265 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001266 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1267 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001268 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001269}
Damien Miller95def091999-11-25 00:26:21 +11001270
Ben Lindstrombba81212001-06-25 05:01:22 +00001271static int
Damien Millerdff50992002-01-22 23:16:32 +11001272packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001273{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001274 u_int padlen, need;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001275 u_char *macbuf = NULL, *cp, type;
Damien Miller1d75abf2013-01-09 16:12:19 +11001276 u_int maclen, authlen = 0, aadlen = 0, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001277 Enc *enc = NULL;
1278 Mac *mac = NULL;
1279 Comp *comp = NULL;
1280
Darren Tuckerf7288d72009-06-21 18:12:20 +10001281 if (active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001282 return SSH_MSG_NONE;
1283
Darren Tuckerf7288d72009-06-21 18:12:20 +10001284 if (active_state->newkeys[MODE_IN] != NULL) {
1285 enc = &active_state->newkeys[MODE_IN]->enc;
1286 mac = &active_state->newkeys[MODE_IN]->mac;
1287 comp = &active_state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001288 /* disable mac for authenticated encryption */
1289 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1290 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001291 }
1292 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001293 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001294 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001295
Damien Milleraf43a7a2012-12-12 10:46:31 +11001296 if (aadlen && active_state->packlen == 0) {
Damien Miller0fde8ac2013-11-21 14:12:23 +11001297 if (cipher_get_length(&active_state->receive_context,
1298 &active_state->packlen,
1299 active_state->p_read.seqnr,
1300 buffer_ptr(&active_state->input),
1301 buffer_len(&active_state->input)) != 0)
Damien Milleraf43a7a2012-12-12 10:46:31 +11001302 return SSH_MSG_NONE;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001303 if (active_state->packlen < 1 + 4 ||
1304 active_state->packlen > PACKET_MAX_SIZE) {
1305#ifdef PACKET_DEBUG
1306 buffer_dump(&active_state->input);
1307#endif
1308 logit("Bad packet length %u.", active_state->packlen);
1309 packet_disconnect("Packet corrupt");
1310 }
Damien Miller8c05da32012-12-13 07:18:59 +11001311 buffer_clear(&active_state->incoming_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001312 } else if (active_state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001313 /*
1314 * check if input size is less than the cipher block size,
1315 * decrypt first block and extract length of incoming packet
1316 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001317 if (buffer_len(&active_state->input) < block_size)
Damien Miller33b13562000-04-04 14:38:59 +10001318 return SSH_MSG_NONE;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001319 buffer_clear(&active_state->incoming_packet);
1320 cp = buffer_append_space(&active_state->incoming_packet,
Damien Miller33b13562000-04-04 14:38:59 +10001321 block_size);
Damien Millerbcd00ab2013-12-07 10:41:55 +11001322 if (cipher_crypt(&active_state->receive_context,
Damien Miller0fde8ac2013-11-21 14:12:23 +11001323 active_state->p_read.seqnr, cp,
Damien Millerbcd00ab2013-12-07 10:41:55 +11001324 buffer_ptr(&active_state->input), block_size, 0, 0) != 0)
1325 fatal("Decryption integrity check failed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001326 cp = buffer_ptr(&active_state->incoming_packet);
1327 active_state->packlen = get_u32(cp);
1328 if (active_state->packlen < 1 + 4 ||
1329 active_state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001330#ifdef PACKET_DEBUG
Darren Tuckerf7288d72009-06-21 18:12:20 +10001331 buffer_dump(&active_state->incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001332#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001333 logit("Bad packet length %u.", active_state->packlen);
1334 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001335 PACKET_MAX_SIZE);
1336 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001337 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001338 buffer_consume(&active_state->input, block_size);
Damien Miller33b13562000-04-04 14:38:59 +10001339 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001340 DBG(debug("input: packet len %u", active_state->packlen+4));
1341 if (aadlen) {
1342 /* only the payload is encrypted */
1343 need = active_state->packlen;
1344 } else {
1345 /*
1346 * the payload size and the payload are encrypted, but we
1347 * have a partial packet of block_size bytes
1348 */
1349 need = 4 + active_state->packlen - block_size;
1350 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001351 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1352 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001353 if (need % block_size != 0) {
1354 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001355 need, block_size, need % block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001356 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001357 PACKET_MAX_SIZE - block_size);
1358 return SSH_MSG_NONE;
Darren Tucker99d11a32008-12-01 21:40:48 +11001359 }
Damien Miller33b13562000-04-04 14:38:59 +10001360 /*
1361 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001362 * decrypt into incoming_packet:
1363 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001364 * 'need' bytes are encrypted, followed by either
1365 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001366 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001367 */
Damien Miller1d75abf2013-01-09 16:12:19 +11001368 if (buffer_len(&active_state->input) < aadlen + need + authlen + maclen)
Damien Miller33b13562000-04-04 14:38:59 +10001369 return SSH_MSG_NONE;
1370#ifdef PACKET_DEBUG
1371 fprintf(stderr, "read_poll enc/full: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001372 buffer_dump(&active_state->input);
Damien Miller33b13562000-04-04 14:38:59 +10001373#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001374 /* EtM: compute mac over encrypted input */
1375 if (mac && mac->enabled && mac->etm)
1376 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1377 buffer_ptr(&active_state->input), aadlen + need);
1378 cp = buffer_append_space(&active_state->incoming_packet, aadlen + need);
Damien Millerbcd00ab2013-12-07 10:41:55 +11001379 if (cipher_crypt(&active_state->receive_context,
Damien Miller0fde8ac2013-11-21 14:12:23 +11001380 active_state->p_read.seqnr, cp,
Damien Millerbcd00ab2013-12-07 10:41:55 +11001381 buffer_ptr(&active_state->input), need, aadlen, authlen) != 0)
1382 fatal("Decryption integrity check failed");
Damien Miller1d75abf2013-01-09 16:12:19 +11001383 buffer_consume(&active_state->input, aadlen + need + authlen);
Damien Miller33b13562000-04-04 14:38:59 +10001384 /*
1385 * compute MAC over seqnr and packet,
1386 * increment sequence number for incoming packet
1387 */
Damien Miller4af51302000-04-16 11:18:38 +10001388 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001389 if (!mac->etm)
1390 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1391 buffer_ptr(&active_state->incoming_packet),
1392 buffer_len(&active_state->incoming_packet));
Damien Millerea1651c2010-07-16 13:58:37 +10001393 if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001394 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001395 logit("Corrupted MAC on input.");
1396 if (need > PACKET_MAX_SIZE)
1397 fatal("internal error need %d", need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001398 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001399 PACKET_MAX_SIZE - need);
1400 return SSH_MSG_NONE;
1401 }
1402
Darren Tuckerf7288d72009-06-21 18:12:20 +10001403 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1404 buffer_consume(&active_state->input, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +10001405 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001406 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001407 if (seqnr_p != NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001408 *seqnr_p = active_state->p_read.seqnr;
1409 if (++active_state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001410 logit("incoming seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001411 if (++active_state->p_read.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +10001412 if (!(datafellows & SSH_BUG_NOREKEY))
1413 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001414 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1415 active_state->p_read.bytes += active_state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001416
1417 /* get padlen */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001418 cp = buffer_ptr(&active_state->incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001419 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001420 DBG(debug("input: padlen %d", padlen));
1421 if (padlen < 4)
1422 packet_disconnect("Corrupted padlen %d on input.", padlen);
1423
1424 /* skip packet size + padlen, discard padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001425 buffer_consume(&active_state->incoming_packet, 4 + 1);
1426 buffer_consume_end(&active_state->incoming_packet, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001427
Darren Tuckerf7288d72009-06-21 18:12:20 +10001428 DBG(debug("input: len before de-compress %d",
1429 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001430 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001431 buffer_clear(&active_state->compression_buffer);
1432 buffer_uncompress(&active_state->incoming_packet,
1433 &active_state->compression_buffer);
1434 buffer_clear(&active_state->incoming_packet);
1435 buffer_append(&active_state->incoming_packet,
1436 buffer_ptr(&active_state->compression_buffer),
1437 buffer_len(&active_state->compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001438 DBG(debug("input: len after de-compress %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001439 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001440 }
1441 /*
1442 * get packet type, implies consume.
1443 * return length of payload (without type field)
1444 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001445 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001446 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1447 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001448 if (type == SSH2_MSG_NEWKEYS)
1449 set_newkeys(MODE_IN);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001450 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1451 !active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001452 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001453#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001454 fprintf(stderr, "read/plain[%d]:\r\n", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001455 buffer_dump(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001456#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001457 /* reset for next packet */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001458 active_state->packlen = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001459 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001460}
1461
1462int
Damien Millerdff50992002-01-22 23:16:32 +11001463packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001464{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001465 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001466 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001467 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001468
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001469 for (;;) {
1470 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001471 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001472 if (type) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001473 active_state->keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001474 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001475 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001476 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001477 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001478 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001479 break;
Damien Miller33b13562000-04-04 14:38:59 +10001480 case SSH2_MSG_DEBUG:
1481 packet_get_char();
1482 msg = packet_get_string(NULL);
1483 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001484 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001485 msg = packet_get_string(NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001486 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001487 break;
1488 case SSH2_MSG_DISCONNECT:
1489 reason = packet_get_int();
1490 msg = packet_get_string(NULL);
Damien Millerd5edefd2013-04-23 15:21:39 +10001491 /* Ignore normal client exit notifications */
1492 do_log2(active_state->server_side &&
1493 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1494 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1495 "Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001496 get_remote_ipaddr(), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001497 free(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001498 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001499 break;
Damien Miller659811f2002-01-22 23:23:11 +11001500 case SSH2_MSG_UNIMPLEMENTED:
1501 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001502 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1503 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001504 break;
Damien Miller33b13562000-04-04 14:38:59 +10001505 default:
1506 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001507 }
Damien Miller33b13562000-04-04 14:38:59 +10001508 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001509 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001510 switch (type) {
Damien Millerce986542013-07-18 16:12:44 +10001511 case SSH_MSG_NONE:
1512 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001513 case SSH_MSG_IGNORE:
1514 break;
1515 case SSH_MSG_DEBUG:
1516 msg = packet_get_string(NULL);
1517 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001518 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001519 break;
1520 case SSH_MSG_DISCONNECT:
1521 msg = packet_get_string(NULL);
Damien Miller894926e2013-02-12 11:03:58 +11001522 error("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001523 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001524 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001525 break;
1526 default:
Damien Millerce986542013-07-18 16:12:44 +10001527 DBG(debug("received packet type %d", type));
Damien Miller33b13562000-04-04 14:38:59 +10001528 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001529 }
Damien Miller33b13562000-04-04 14:38:59 +10001530 }
1531 }
1532}
1533
Damien Miller5428f641999-11-25 11:54:57 +11001534/*
1535 * Buffers the given amount of input characters. This is intended to be used
1536 * together with packet_read_poll.
1537 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001538
1539void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001540packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001541{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001542 if (active_state->packet_discard) {
1543 active_state->keep_alive_timeouts = 0; /* ?? */
1544 if (len >= active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001545 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001546 active_state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001547 return;
1548 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001549 buffer_append(&active_state->input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001550}
1551
1552/* Returns a character from the packet. */
1553
Ben Lindstrom46c16222000-12-22 01:43:59 +00001554u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001555packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001556{
Damien Miller95def091999-11-25 00:26:21 +11001557 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001558
Darren Tuckerf7288d72009-06-21 18:12:20 +10001559 buffer_get(&active_state->incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001560 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001561}
1562
1563/* Returns an integer from the packet data. */
1564
Ben Lindstrom46c16222000-12-22 01:43:59 +00001565u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001566packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001568 return buffer_get_int(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001569}
1570
Darren Tucker761c3892009-06-21 18:16:26 +10001571/* Returns an 64 bit integer from the packet data. */
1572
1573u_int64_t
1574packet_get_int64(void)
1575{
1576 return buffer_get_int64(&active_state->incoming_packet);
1577}
1578
Damien Miller5428f641999-11-25 11:54:57 +11001579/*
1580 * Returns an arbitrary precision integer from the packet data. The integer
1581 * must have been initialized before this call.
1582 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583
Damien Miller1f0311c2014-05-15 14:24:09 +10001584#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001585void
Damien Millerd432ccf2002-01-22 23:14:44 +11001586packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001587{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001588 buffer_get_bignum(&active_state->incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001589}
1590
Damien Miller33b13562000-04-04 14:38:59 +10001591void
Damien Millerd432ccf2002-01-22 23:14:44 +11001592packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001593{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001594 buffer_get_bignum2(&active_state->incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001595}
1596
Damien Miller6af914a2010-09-10 11:39:26 +10001597#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001598void
1599packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point)
1600{
1601 buffer_get_ecpoint(&active_state->incoming_packet, curve, point);
1602}
Damien Miller6af914a2010-09-10 11:39:26 +10001603#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001604
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001605void *
Damien Millereccb9de2005-06-17 12:59:34 +10001606packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001607{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001608 u_int bytes = buffer_len(&active_state->incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001609
Damien Miller33b13562000-04-04 14:38:59 +10001610 if (length_ptr != NULL)
1611 *length_ptr = bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001612 return buffer_ptr(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001613}
Damien Miller1f0311c2014-05-15 14:24:09 +10001614#endif
Damien Miller33b13562000-04-04 14:38:59 +10001615
Damien Miller4af51302000-04-16 11:18:38 +10001616int
1617packet_remaining(void)
1618{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001619 return buffer_len(&active_state->incoming_packet);
Damien Miller4af51302000-04-16 11:18:38 +10001620}
1621
Damien Miller5428f641999-11-25 11:54:57 +11001622/*
1623 * Returns a string from the packet data. The string is allocated using
1624 * xmalloc; it is the responsibility of the calling program to free it when
1625 * no longer needed. The length_ptr argument may be NULL, or point to an
1626 * integer into which the length of the string is stored.
1627 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001628
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001629void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001630packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001631{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001632 return buffer_get_string(&active_state->incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001633}
1634
Damien Miller633de332014-05-15 13:48:26 +10001635const void *
Damien Millerdb255ca2008-05-19 14:59:37 +10001636packet_get_string_ptr(u_int *length_ptr)
1637{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001638 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
Damien Millerdb255ca2008-05-19 14:59:37 +10001639}
1640
Damien Millerda108ec2010-08-31 22:36:39 +10001641/* Ensures the returned string has no embedded \0 characters in it. */
1642char *
1643packet_get_cstring(u_int *length_ptr)
1644{
1645 return buffer_get_cstring(&active_state->incoming_packet, length_ptr);
1646}
1647
Damien Miller5428f641999-11-25 11:54:57 +11001648/*
1649 * Sends a diagnostic message from the server to the client. This message
1650 * can be sent at any time (but not while constructing another message). The
1651 * message is printed immediately, but only if the client is being executed
1652 * in verbose mode. These messages are primarily intended to ease debugging
1653 * authentication problems. The length of the formatted message must not
1654 * exceed 1024 bytes. This will automatically call packet_write_wait.
1655 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001656
1657void
Damien Miller95def091999-11-25 00:26:21 +11001658packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001659{
Damien Miller95def091999-11-25 00:26:21 +11001660 char buf[1024];
1661 va_list args;
1662
Ben Lindstroma14ee472000-12-07 01:24:58 +00001663 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1664 return;
1665
Damien Miller95def091999-11-25 00:26:21 +11001666 va_start(args, fmt);
1667 vsnprintf(buf, sizeof(buf), fmt, args);
1668 va_end(args);
1669
Damien Miller7c8af4f2000-05-01 08:24:07 +10001670 if (compat20) {
1671 packet_start(SSH2_MSG_DEBUG);
1672 packet_put_char(0); /* bool: always display */
1673 packet_put_cstring(buf);
1674 packet_put_cstring("");
1675 } else {
1676 packet_start(SSH_MSG_DEBUG);
1677 packet_put_cstring(buf);
1678 }
Damien Miller95def091999-11-25 00:26:21 +11001679 packet_send();
1680 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001681}
1682
Damien Miller5428f641999-11-25 11:54:57 +11001683/*
1684 * Logs the error plus constructs and sends a disconnect packet, closes the
1685 * connection, and exits. This function never returns. The error message
1686 * should not contain a newline. The length of the formatted message must
1687 * not exceed 1024 bytes.
1688 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001689
1690void
Damien Miller95def091999-11-25 00:26:21 +11001691packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001692{
Damien Miller95def091999-11-25 00:26:21 +11001693 char buf[1024];
1694 va_list args;
1695 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001696
Damien Miller95def091999-11-25 00:26:21 +11001697 if (disconnecting) /* Guard against recursive invocations. */
1698 fatal("packet_disconnect called recursively.");
1699 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001700
Damien Miller5428f641999-11-25 11:54:57 +11001701 /*
1702 * Format the message. Note that the caller must make sure the
1703 * message is of limited size.
1704 */
Damien Miller95def091999-11-25 00:26:21 +11001705 va_start(args, fmt);
1706 vsnprintf(buf, sizeof(buf), fmt, args);
1707 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001708
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001709 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001710 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001711
Damien Miller95def091999-11-25 00:26:21 +11001712 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001713 if (compat20) {
1714 packet_start(SSH2_MSG_DISCONNECT);
1715 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1716 packet_put_cstring(buf);
1717 packet_put_cstring("");
1718 } else {
1719 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001720 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001721 }
Damien Miller95def091999-11-25 00:26:21 +11001722 packet_send();
1723 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001724
Damien Miller95def091999-11-25 00:26:21 +11001725 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001726 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001727
Damien Miller95def091999-11-25 00:26:21 +11001728 /* Close the connection. */
1729 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001730 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001731}
1732
Damien Miller5428f641999-11-25 11:54:57 +11001733/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001734
1735void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001736packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001737{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001738 int len = buffer_len(&active_state->output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001739 int cont;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001740
Damien Miller95def091999-11-25 00:26:21 +11001741 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001742 cont = 0;
1743 len = roaming_write(active_state->connection_out,
1744 buffer_ptr(&active_state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001745 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001746 if (errno == EINTR || errno == EAGAIN ||
1747 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001748 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001749 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001750 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001751 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001752 fatal("Write connection closed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001753 buffer_consume(&active_state->output, len);
Damien Miller95def091999-11-25 00:26:21 +11001754 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001755}
1756
Damien Miller5428f641999-11-25 11:54:57 +11001757/*
1758 * Calls packet_write_poll repeatedly until all pending output data has been
1759 * written.
1760 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001761
1762void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001763packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001764{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001765 fd_set *setp;
Damien Millerce986542013-07-18 16:12:44 +10001766 int ret, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001767 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001768
Darren Tuckerf7288d72009-06-21 18:12:20 +10001769 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1770 NFDBITS), sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001771 packet_write_poll();
1772 while (packet_have_data_to_write()) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001773 memset(setp, 0, howmany(active_state->connection_out + 1,
1774 NFDBITS) * sizeof(fd_mask));
1775 FD_SET(active_state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001776
Darren Tuckerf7288d72009-06-21 18:12:20 +10001777 if (active_state->packet_timeout_ms > 0) {
1778 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001779 timeoutp = &timeout;
1780 }
1781 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001782 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001783 ms_to_timeval(&timeout, ms_remain);
1784 gettimeofday(&start, NULL);
1785 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001786 if ((ret = select(active_state->connection_out + 1,
1787 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001788 break;
Damien Millerea437422009-10-02 11:49:03 +10001789 if (errno != EAGAIN && errno != EINTR &&
1790 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001791 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001792 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001793 continue;
1794 ms_subtract_diff(&start, &ms_remain);
1795 if (ms_remain <= 0) {
1796 ret = 0;
1797 break;
1798 }
1799 }
1800 if (ret == 0) {
1801 logit("Connection to %.200s timed out while "
1802 "waiting to write", get_remote_ipaddr());
1803 cleanup_exit(255);
1804 }
Damien Miller95def091999-11-25 00:26:21 +11001805 packet_write_poll();
1806 }
Darren Tuckera627d422013-06-02 07:31:17 +10001807 free(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001808}
1809
1810/* Returns true if there is buffered data to write to the connection. */
1811
1812int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001813packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001814{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001815 return buffer_len(&active_state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001816}
1817
1818/* Returns true if there is not too much data to write to the connection. */
1819
1820int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001821packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001822{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001823 if (active_state->interactive_mode)
1824 return buffer_len(&active_state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11001825 else
Darren Tuckerf7288d72009-06-21 18:12:20 +10001826 return buffer_len(&active_state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001827}
1828
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001829static void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001830packet_set_tos(int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00001831{
Damien Millerd2ac5d72011-05-15 08:43:13 +10001832#ifndef IP_TOS_IS_BROKEN
1833 if (!packet_connection_is_on_socket())
Ben Lindstroma7433982002-12-23 02:41:41 +00001834 return;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001835 switch (packet_connection_af()) {
1836# ifdef IP_TOS
1837 case AF_INET:
1838 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1839 if (setsockopt(active_state->connection_in,
1840 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
1841 error("setsockopt IP_TOS %d: %.100s:",
1842 tos, strerror(errno));
1843 break;
1844# endif /* IP_TOS */
1845# ifdef IPV6_TCLASS
1846 case AF_INET6:
1847 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1848 if (setsockopt(active_state->connection_in,
1849 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
1850 error("setsockopt IPV6_TCLASS %d: %.100s:",
1851 tos, strerror(errno));
1852 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001853# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10001854 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10001855#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11001856}
Ben Lindstroma7433982002-12-23 02:41:41 +00001857
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001858/* Informs that the current session is interactive. Sets IP flags for that. */
1859
1860void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001861packet_set_interactive(int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001862{
Darren Tucker7b935c72009-06-21 18:59:36 +10001863 if (active_state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001864 return;
Darren Tucker7b935c72009-06-21 18:59:36 +10001865 active_state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001866
Damien Miller95def091999-11-25 00:26:21 +11001867 /* Record that we are in interactive mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001868 active_state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001869
Damien Miller34132e52000-01-14 15:45:46 +11001870 /* Only set socket options if using a socket. */
1871 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001872 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001873 set_nodelay(active_state->connection_in);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001874 packet_set_tos(interactive ? qos_interactive : qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001875}
1876
1877/* Returns true if the current connection is interactive. */
1878
1879int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001880packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001881{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001882 return active_state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001883}
Damien Miller6162d121999-11-21 13:23:52 +11001884
Darren Tucker1f8311c2004-05-13 16:39:33 +10001885int
Darren Tucker502d3842003-06-28 12:38:01 +10001886packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001887{
Darren Tucker7b935c72009-06-21 18:59:36 +10001888 if (active_state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10001889 logit("packet_set_maxsize: called twice: old %d new %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001890 active_state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001891 return -1;
1892 }
1893 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001894 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001895 return -1;
1896 }
Darren Tucker7b935c72009-06-21 18:59:36 +10001897 active_state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001898 debug("packet_set_maxsize: setting to %d", s);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001899 active_state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11001900 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001901}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001902
Darren Tuckerf7288d72009-06-21 18:12:20 +10001903int
1904packet_inc_alive_timeouts(void)
1905{
1906 return ++active_state->keep_alive_timeouts;
1907}
1908
1909void
1910packet_set_alive_timeouts(int ka)
1911{
1912 active_state->keep_alive_timeouts = ka;
1913}
1914
1915u_int
1916packet_get_maxsize(void)
1917{
1918 return active_state->max_packet_size;
1919}
1920
Damien Miller9f643902001-11-12 11:02:52 +11001921/* roundup current message to pad bytes */
1922void
1923packet_add_padding(u_char pad)
1924{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001925 active_state->extra_pad = pad;
Damien Miller9f643902001-11-12 11:02:52 +11001926}
1927
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001928/*
1929 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001930 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001931 * byte SSH_MSG_IGNORE
1932 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001933 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001934 * All implementations MUST understand (and ignore) this message at any
1935 * time (after receiving the protocol version). No implementation is
1936 * required to send them. This message can be used as an additional
1937 * protection measure against advanced traffic analysis techniques.
1938 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001939void
1940packet_send_ignore(int nbytes)
1941{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001942 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001943 int i;
1944
1945 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001946 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001947 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001948 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001949 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001950 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001951 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001952 }
1953}
Damien Millera5539d22003-04-09 20:50:06 +10001954
Darren Tucker1f8311c2004-05-13 16:39:33 +10001955#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001956int
1957packet_need_rekeying(void)
1958{
1959 if (datafellows & SSH_BUG_NOREKEY)
1960 return 0;
1961 return
Darren Tuckerf7288d72009-06-21 18:12:20 +10001962 (active_state->p_send.packets > MAX_PACKETS) ||
1963 (active_state->p_read.packets > MAX_PACKETS) ||
1964 (active_state->max_blocks_out &&
1965 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1966 (active_state->max_blocks_in &&
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001967 (active_state->p_read.blocks > active_state->max_blocks_in)) ||
1968 (active_state->rekey_interval != 0 && active_state->rekey_time +
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001969 active_state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10001970}
1971
1972void
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001973packet_set_rekey_limits(u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10001974{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001975 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
1976 (int)seconds);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001977 active_state->rekey_limit = bytes;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001978 active_state->rekey_interval = seconds;
1979 /*
1980 * We set the time here so that in post-auth privsep slave we count
1981 * from the completion of the authentication.
1982 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001983 active_state->rekey_time = monotime();
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001984}
1985
1986time_t
1987packet_get_rekey_timeout(void)
1988{
1989 time_t seconds;
1990
1991 seconds = active_state->rekey_time + active_state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001992 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001993 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10001994}
Damien Miller9786e6e2005-07-26 21:54:56 +10001995
1996void
1997packet_set_server(void)
1998{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001999 active_state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002000}
2001
2002void
2003packet_set_authenticated(void)
2004{
Darren Tuckerf7288d72009-06-21 18:12:20 +10002005 active_state->after_authentication = 1;
2006}
2007
2008void *
2009packet_get_input(void)
2010{
2011 return (void *)&active_state->input;
2012}
2013
2014void *
2015packet_get_output(void)
2016{
2017 return (void *)&active_state->output;
2018}
2019
2020void *
2021packet_get_newkeys(int mode)
2022{
2023 return (void *)active_state->newkeys[mode];
Damien Miller9786e6e2005-07-26 21:54:56 +10002024}
Darren Tuckere841eb02009-07-06 07:11:13 +10002025
2026/*
2027 * Save the state for the real connection, and use a separate state when
2028 * resuming a suspended connection.
2029 */
2030void
2031packet_backup_state(void)
2032{
2033 struct session_state *tmp;
2034
2035 close(active_state->connection_in);
2036 active_state->connection_in = -1;
2037 close(active_state->connection_out);
2038 active_state->connection_out = -1;
2039 if (backup_state)
2040 tmp = backup_state;
2041 else
2042 tmp = alloc_session_state();
2043 backup_state = active_state;
2044 active_state = tmp;
2045}
2046
2047/*
2048 * Swap in the old state when resuming a connecion.
2049 */
2050void
2051packet_restore_state(void)
2052{
2053 struct session_state *tmp;
2054 void *buf;
2055 u_int len;
2056
2057 tmp = backup_state;
2058 backup_state = active_state;
2059 active_state = tmp;
2060 active_state->connection_in = backup_state->connection_in;
2061 backup_state->connection_in = -1;
2062 active_state->connection_out = backup_state->connection_out;
2063 backup_state->connection_out = -1;
2064 len = buffer_len(&backup_state->input);
2065 if (len > 0) {
2066 buf = buffer_ptr(&backup_state->input);
2067 buffer_append(&active_state->input, buf, len);
2068 buffer_clear(&backup_state->input);
2069 add_recv_bytes(len);
2070 }
2071}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002072
2073/* Reset after_authentication and reset compression in post-auth privsep */
2074void
2075packet_set_postauth(void)
2076{
2077 Comp *comp;
2078 int mode;
2079
2080 debug("%s: called", __func__);
2081 /* This was set in net child, but is not visible in user child */
2082 active_state->after_authentication = 1;
2083 active_state->rekeying = 0;
2084 for (mode = 0; mode < MODE_MAX; mode++) {
2085 if (active_state->newkeys[mode] == NULL)
2086 continue;
2087 comp = &active_state->newkeys[mode]->comp;
2088 if (comp && comp->enabled)
2089 packet_init_compression();
2090 }
2091}