blob: 3dd66d7d9668bf3abf283570898a3d8c6cba4e75 [file] [log] [blame]
Damien Millerc31a0cd2014-05-15 14:37:39 +10001/* $OpenBSD: packet.c,v 1.196 2014/05/03 17:20:34 markus 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"
Ben Lindstromc7637672001-06-09 00:36:26 +000069#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100070#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100072#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110073#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100074#include "key.h"
Damien Miller33b13562000-04-04 14:38:59 +100075#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000076#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110079#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000080#include "ssh.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100081#include "roaming.h"
Damien Miller33b13562000-04-04 14:38:59 +100082
83#ifdef PACKET_DEBUG
84#define DBG(x) x
85#else
86#define DBG(x)
87#endif
88
Damien Miller13ae44c2009-01-28 16:38:41 +110089#define PACKET_MAX_SIZE (256 * 1024)
90
Darren Tuckerf7288d72009-06-21 18:12:20 +100091struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100092 u_int32_t seqnr;
93 u_int32_t packets;
94 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +100095 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +100096};
Damien Miller13ae44c2009-01-28 16:38:41 +110097
Damien Millera5539d22003-04-09 20:50:06 +100098struct packet {
99 TAILQ_ENTRY(packet) next;
100 u_char type;
101 Buffer payload;
102};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000103
104struct session_state {
105 /*
106 * This variable contains the file descriptors used for
107 * communicating with the other side. connection_in is used for
108 * reading; connection_out for writing. These can be the same
109 * descriptor, in which case it is assumed to be a socket.
110 */
111 int connection_in;
112 int connection_out;
113
114 /* Protocol flags for the remote side. */
115 u_int remote_protocol_flags;
116
117 /* Encryption context for receiving data. Only used for decryption. */
118 CipherContext receive_context;
119
120 /* Encryption context for sending data. Only used for encryption. */
121 CipherContext send_context;
122
123 /* Buffer for raw input data from the socket. */
124 Buffer input;
125
126 /* Buffer for raw output data going to the socket. */
127 Buffer output;
128
129 /* Buffer for the partial outgoing packet being constructed. */
130 Buffer outgoing_packet;
131
132 /* Buffer for the incoming packet currently being processed. */
133 Buffer incoming_packet;
134
135 /* Scratch buffer for packet compression/decompression. */
136 Buffer compression_buffer;
137 int compression_buffer_ready;
138
139 /*
140 * Flag indicating whether packet compression/decompression is
141 * enabled.
142 */
143 int packet_compression;
144
145 /* default maximum packet size */
146 u_int max_packet_size;
147
148 /* Flag indicating whether this module has been initialized. */
149 int initialized;
150
151 /* Set to true if the connection is interactive. */
152 int interactive_mode;
153
154 /* Set to true if we are the server side. */
155 int server_side;
156
157 /* Set to true if we are authenticated. */
158 int after_authentication;
159
160 int keep_alive_timeouts;
161
162 /* The maximum time that we will wait to send or receive a packet */
163 int packet_timeout_ms;
164
165 /* Session key information for Encryption and MAC */
166 Newkeys *newkeys[MODE_MAX];
167 struct packet_state p_read, p_send;
168
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000169 /* Volume-based rekeying */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000170 u_int64_t max_blocks_in, max_blocks_out;
171 u_int32_t rekey_limit;
172
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000173 /* Time-based rekeying */
174 time_t rekey_interval; /* how often in seconds */
175 time_t rekey_time; /* time of last rekeying */
176
Darren Tuckerf7288d72009-06-21 18:12:20 +1000177 /* Session key for protocol v1 */
178 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
179 u_int ssh1_keylen;
180
181 /* roundup current message to extra_pad bytes */
182 u_char extra_pad;
183
184 /* XXX discard incoming data after MAC error */
185 u_int packet_discard;
186 Mac *packet_discard_mac;
187
188 /* Used in packet_read_poll2() */
189 u_int packlen;
190
Darren Tucker7b935c72009-06-21 18:59:36 +1000191 /* Used in packet_send2 */
192 int rekeying;
193
194 /* Used in packet_set_interactive */
195 int set_interactive_called;
196
197 /* Used in packet_set_maxsize */
198 int set_maxsize_called;
199
Darren Tuckerf7288d72009-06-21 18:12:20 +1000200 TAILQ_HEAD(, packet) outgoing;
201};
202
Darren Tuckere841eb02009-07-06 07:11:13 +1000203static struct session_state *active_state, *backup_state;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000204
205static struct session_state *
Darren Tuckerb422afa2009-06-21 18:58:46 +1000206alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000207{
Damien Miller7a221a12010-11-20 15:14:29 +1100208 struct session_state *s = xcalloc(1, sizeof(*s));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000209
Damien Miller7a221a12010-11-20 15:14:29 +1100210 s->connection_in = -1;
211 s->connection_out = -1;
212 s->max_packet_size = 32768;
213 s->packet_timeout_ms = -1;
214 return s;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000215}
Damien Millera5539d22003-04-09 20:50:06 +1000216
Damien Miller5428f641999-11-25 11:54:57 +1100217/*
218 * Sets the descriptors used for communication. Disables encryption until
219 * packet_set_encryption_key is called.
220 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221void
222packet_set_connection(int fd_in, int fd_out)
223{
Damien Millerea111192013-04-23 19:24:32 +1000224 const Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000225
Damien Miller874d77b2000-10-14 16:23:11 +1100226 if (none == NULL)
227 fatal("packet_set_connection: cannot load cipher 'none'");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000228 if (active_state == NULL)
229 active_state = alloc_session_state();
230 active_state->connection_in = fd_in;
231 active_state->connection_out = fd_out;
232 cipher_init(&active_state->send_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000233 0, NULL, 0, CIPHER_ENCRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000234 cipher_init(&active_state->receive_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000235 0, NULL, 0, CIPHER_DECRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000236 active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
237 if (!active_state->initialized) {
238 active_state->initialized = 1;
239 buffer_init(&active_state->input);
240 buffer_init(&active_state->output);
241 buffer_init(&active_state->outgoing_packet);
242 buffer_init(&active_state->incoming_packet);
243 TAILQ_INIT(&active_state->outgoing);
244 active_state->p_send.packets = active_state->p_read.packets = 0;
Damien Miller95def091999-11-25 00:26:21 +1100245 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246}
247
Darren Tucker3fc464e2008-06-13 06:42:45 +1000248void
249packet_set_timeout(int timeout, int count)
250{
Damien Miller8ed4de82011-12-19 10:52:50 +1100251 if (timeout <= 0 || count <= 0) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000252 active_state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000253 return;
254 }
255 if ((INT_MAX / 1000) / count < timeout)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000256 active_state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000257 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000258 active_state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000259}
260
Damien Miller13ae44c2009-01-28 16:38:41 +1100261static void
262packet_stop_discard(void)
263{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000264 if (active_state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100265 char buf[1024];
266
267 memset(buf, 'a', sizeof(buf));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000268 while (buffer_len(&active_state->incoming_packet) <
269 PACKET_MAX_SIZE)
270 buffer_append(&active_state->incoming_packet, buf,
271 sizeof(buf));
272 (void) mac_compute(active_state->packet_discard_mac,
273 active_state->p_read.seqnr,
274 buffer_ptr(&active_state->incoming_packet),
Damien Miller13ae44c2009-01-28 16:38:41 +1100275 PACKET_MAX_SIZE);
276 }
277 logit("Finished discarding for %.200s", get_remote_ipaddr());
278 cleanup_exit(255);
279}
280
281static void
282packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
283{
Damien Milleraf43a7a2012-12-12 10:46:31 +1100284 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm))
Damien Miller13ae44c2009-01-28 16:38:41 +1100285 packet_disconnect("Packet corrupt");
286 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000287 active_state->packet_discard_mac = mac;
288 if (buffer_len(&active_state->input) >= discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100289 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +1000290 active_state->packet_discard = discard -
291 buffer_len(&active_state->input);
Damien Miller13ae44c2009-01-28 16:38:41 +1100292}
293
Damien Miller34132e52000-01-14 15:45:46 +1100294/* Returns 1 if remote host is connected via socket, 0 if not. */
295
296int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000297packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100298{
299 struct sockaddr_storage from, to;
300 socklen_t fromlen, tolen;
301
302 /* filedescriptors in and out are the same, so it's a socket */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000303 if (active_state->connection_in == active_state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100304 return 1;
305 fromlen = sizeof(from);
306 memset(&from, 0, sizeof(from));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000307 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
308 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100309 return 0;
310 tolen = sizeof(to);
311 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000312 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
313 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100314 return 0;
315 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
316 return 0;
317 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
318 return 0;
319 return 1;
320}
321
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000322/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000323 * Exports an IV from the CipherContext required to export the key
324 * state back from the unprivileged child to the privileged parent
325 * process.
326 */
327
328void
329packet_get_keyiv(int mode, u_char *iv, u_int len)
330{
331 CipherContext *cc;
332
333 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000334 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000335 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000336 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000337
338 cipher_get_keyiv(cc, iv, len);
339}
340
341int
342packet_get_keycontext(int mode, u_char *dat)
343{
344 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000345
Ben Lindstromf6027d32002-03-22 01:42:04 +0000346 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000347 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000348 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000349 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000350
351 return (cipher_get_keycontext(cc, dat));
352}
353
354void
355packet_set_keycontext(int mode, u_char *dat)
356{
357 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000358
Ben Lindstromf6027d32002-03-22 01:42:04 +0000359 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000360 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000361 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000362 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000363
364 cipher_set_keycontext(cc, dat);
365}
366
367int
368packet_get_keyiv_len(int mode)
369{
370 CipherContext *cc;
371
372 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000373 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000374 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000375 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000376
377 return (cipher_get_keyiv_len(cc));
378}
Damien Miller4f7becb2006-03-26 14:10:14 +1100379
Ben Lindstromf6027d32002-03-22 01:42:04 +0000380void
381packet_set_iv(int mode, u_char *dat)
382{
383 CipherContext *cc;
384
385 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000386 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000387 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000388 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000389
390 cipher_set_keyiv(cc, dat);
391}
Damien Miller4f7becb2006-03-26 14:10:14 +1100392
Ben Lindstromf6027d32002-03-22 01:42:04 +0000393int
Damien Miller2b92d322003-06-11 22:05:06 +1000394packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000395{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000396 return (cipher_get_number(active_state->receive_context.cipher));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000397}
398
Damien Millera5539d22003-04-09 20:50:06 +1000399void
Damien Miller7a221a12010-11-20 15:14:29 +1100400packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks,
401 u_int32_t *packets, u_int64_t *bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000402{
Damien Millera5539d22003-04-09 20:50:06 +1000403 struct packet_state *state;
404
Darren Tuckerf7288d72009-06-21 18:12:20 +1000405 state = (mode == MODE_IN) ?
406 &active_state->p_read : &active_state->p_send;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000407 if (seqnr)
408 *seqnr = state->seqnr;
409 if (blocks)
410 *blocks = state->blocks;
411 if (packets)
412 *packets = state->packets;
413 if (bytes)
414 *bytes = state->bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000415}
416
417void
Damien Millerb61f3fc2008-07-11 17:36:48 +1000418packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
419 u_int64_t bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000420{
Damien Millera5539d22003-04-09 20:50:06 +1000421 struct packet_state *state;
422
Darren Tuckerf7288d72009-06-21 18:12:20 +1000423 state = (mode == MODE_IN) ?
424 &active_state->p_read : &active_state->p_send;
Damien Millera5539d22003-04-09 20:50:06 +1000425 state->seqnr = seqnr;
426 state->blocks = blocks;
427 state->packets = packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000428 state->bytes = bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000429}
430
Damien Millerd2ac5d72011-05-15 08:43:13 +1000431static int
432packet_connection_af(void)
Damien Miller34132e52000-01-14 15:45:46 +1100433{
434 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100435 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100436
437 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000438 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
439 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100440 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000441#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100442 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000443 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000444 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000445#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000446 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100447}
448
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449/* Sets the connection into non-blocking mode. */
450
451void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000452packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453{
Damien Miller95def091999-11-25 00:26:21 +1100454 /* Set the socket into non-blocking mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000455 set_nonblock(active_state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456
Darren Tuckerf7288d72009-06-21 18:12:20 +1000457 if (active_state->connection_out != active_state->connection_in)
458 set_nonblock(active_state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459}
460
461/* Returns the socket used for reading. */
462
463int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000464packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000466 return active_state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467}
468
469/* Returns the descriptor used for writing. */
470
471int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000472packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000474 return active_state->connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
476
477/* Closes the connection and clears and frees internal data structures. */
478
479void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000480packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000482 if (!active_state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100483 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000484 active_state->initialized = 0;
485 if (active_state->connection_in == active_state->connection_out) {
486 shutdown(active_state->connection_out, SHUT_RDWR);
487 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100488 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000489 close(active_state->connection_in);
490 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100491 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000492 buffer_free(&active_state->input);
493 buffer_free(&active_state->output);
494 buffer_free(&active_state->outgoing_packet);
495 buffer_free(&active_state->incoming_packet);
496 if (active_state->compression_buffer_ready) {
497 buffer_free(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100498 buffer_compress_uninit();
499 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000500 cipher_cleanup(&active_state->send_context);
501 cipher_cleanup(&active_state->receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502}
503
504/* Sets remote side protocol flags. */
505
506void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000507packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000509 active_state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510}
511
512/* Returns the remote protocol flags set earlier by the above function. */
513
Ben Lindstrom46c16222000-12-22 01:43:59 +0000514u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000515packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000517 return active_state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518}
519
Damien Miller5428f641999-11-25 11:54:57 +1100520/*
521 * Starts packet compression from the next packet on in both directions.
522 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
523 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524
Ben Lindstrombba81212001-06-25 05:01:22 +0000525static void
526packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000527{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000528 if (active_state->compression_buffer_ready == 1)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000529 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000530 active_state->compression_buffer_ready = 1;
531 buffer_init(&active_state->compression_buffer);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000532}
533
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534void
535packet_start_compression(int level)
536{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000537 if (active_state->packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100538 fatal("Compression already enabled.");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000539 active_state->packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000540 packet_init_compression();
541 buffer_compress_init_send(level);
542 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543}
544
Damien Miller5428f641999-11-25 11:54:57 +1100545/*
Damien Miller5428f641999-11-25 11:54:57 +1100546 * Causes any further packets to be encrypted using the given key. The same
547 * key is used for both sending and reception. However, both directions are
548 * encrypted independently of each other.
549 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000550
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551void
Damien Miller7a221a12010-11-20 15:14:29 +1100552packet_set_encryption_key(const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553{
Damien Millerea111192013-04-23 19:24:32 +1000554 const Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000555
Damien Miller874d77b2000-10-14 16:23:11 +1100556 if (cipher == NULL)
557 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000558 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100559 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000560 if (keylen > SSH_SESSION_KEY_LENGTH)
561 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000562 memcpy(active_state->ssh1_key, key, keylen);
563 active_state->ssh1_keylen = keylen;
564 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
565 0, CIPHER_ENCRYPT);
566 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
567 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568}
569
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000570u_int
571packet_get_encryption_key(u_char *key)
572{
573 if (key == NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000574 return (active_state->ssh1_keylen);
575 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
576 return (active_state->ssh1_keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000577}
578
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000579/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000581packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000583 u_char buf[9];
584 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585
Ben Lindstrom204e4882001-03-05 06:47:00 +0000586 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000587 len = compat20 ? 6 : 9;
588 memset(buf, 0, len - 1);
589 buf[len - 1] = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000590 buffer_clear(&active_state->outgoing_packet);
591 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000592}
593
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000594/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595void
596packet_put_char(int value)
597{
Damien Miller95def091999-11-25 00:26:21 +1100598 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000599
Darren Tuckerf7288d72009-06-21 18:12:20 +1000600 buffer_append(&active_state->outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601}
Damien Miller4f7becb2006-03-26 14:10:14 +1100602
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000604packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000606 buffer_put_int(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607}
Damien Miller4f7becb2006-03-26 14:10:14 +1100608
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609void
Darren Tucker761c3892009-06-21 18:16:26 +1000610packet_put_int64(u_int64_t value)
611{
612 buffer_put_int64(&active_state->outgoing_packet, value);
613}
614
615void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100616packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000618 buffer_put_string(&active_state->outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619}
Damien Miller4f7becb2006-03-26 14:10:14 +1100620
Damien Miller33b13562000-04-04 14:38:59 +1000621void
622packet_put_cstring(const char *str)
623{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000624 buffer_put_cstring(&active_state->outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000625}
Damien Miller4f7becb2006-03-26 14:10:14 +1100626
Damien Miller33b13562000-04-04 14:38:59 +1000627void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100628packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000629{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000630 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000631}
Damien Miller4f7becb2006-03-26 14:10:14 +1100632
Damien Miller1f0311c2014-05-15 14:24:09 +1000633#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634void
Damien Miller95def091999-11-25 00:26:21 +1100635packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000637 buffer_put_bignum(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638}
Damien Miller4f7becb2006-03-26 14:10:14 +1100639
Damien Miller33b13562000-04-04 14:38:59 +1000640void
641packet_put_bignum2(BIGNUM * value)
642{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000643 buffer_put_bignum2(&active_state->outgoing_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +1000644}
Damien Miller1f0311c2014-05-15 14:24:09 +1000645#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646
Damien Miller6af914a2010-09-10 11:39:26 +1000647#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000648void
649packet_put_ecpoint(const EC_GROUP *curve, const EC_POINT *point)
650{
651 buffer_put_ecpoint(&active_state->outgoing_packet, curve, point);
652}
Damien Miller6af914a2010-09-10 11:39:26 +1000653#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000654
Damien Miller5428f641999-11-25 11:54:57 +1100655/*
656 * Finalizes and sends the packet. If the encryption key has been set,
657 * encrypts the packet before sending.
658 */
Damien Miller95def091999-11-25 00:26:21 +1100659
Ben Lindstrombba81212001-06-25 05:01:22 +0000660static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000661packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000663 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100664 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000665 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000666 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
Damien Miller5428f641999-11-25 11:54:57 +1100668 /*
669 * If using packet compression, compress the payload of the outgoing
670 * packet.
671 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000672 if (active_state->packet_compression) {
673 buffer_clear(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100674 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000675 buffer_consume(&active_state->outgoing_packet, 8);
Damien Miller95def091999-11-25 00:26:21 +1100676 /* padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000677 buffer_append(&active_state->compression_buffer,
678 "\0\0\0\0\0\0\0\0", 8);
679 buffer_compress(&active_state->outgoing_packet,
680 &active_state->compression_buffer);
681 buffer_clear(&active_state->outgoing_packet);
682 buffer_append(&active_state->outgoing_packet,
683 buffer_ptr(&active_state->compression_buffer),
684 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100685 }
686 /* Compute packet length without padding (add checksum, remove padding). */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000687 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688
Damien Millere247cc42000-05-07 12:03:14 +1000689 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100690 padding = 8 - len % 8;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000691 if (!active_state->send_context.plaintext) {
692 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller95def091999-11-25 00:26:21 +1100693 for (i = 0; i < padding; i++) {
694 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000695 rnd = arc4random();
696 cp[7 - i] = rnd & 0xff;
697 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100698 }
699 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000700 buffer_consume(&active_state->outgoing_packet, 8 - padding);
Damien Miller95def091999-11-25 00:26:21 +1100701
702 /* Add check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000703 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
704 buffer_len(&active_state->outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100705 put_u32(buf, checksum);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000706 buffer_append(&active_state->outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707
708#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100709 fprintf(stderr, "packet_send plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000710 buffer_dump(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711#endif
712
Damien Miller95def091999-11-25 00:26:21 +1100713 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100714 put_u32(buf, len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000715 buffer_append(&active_state->output, buf, 4);
716 cp = buffer_append_space(&active_state->output,
717 buffer_len(&active_state->outgoing_packet));
Damien Millerbcd00ab2013-12-07 10:41:55 +1100718 if (cipher_crypt(&active_state->send_context, 0, cp,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000719 buffer_ptr(&active_state->outgoing_packet),
Damien Millerbcd00ab2013-12-07 10:41:55 +1100720 buffer_len(&active_state->outgoing_packet), 0, 0) != 0)
721 fatal("%s: cipher_crypt failed", __func__);
Damien Miller95def091999-11-25 00:26:21 +1100722
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100724 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000725 buffer_dump(&active_state->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +1000727 active_state->p_send.packets++;
728 active_state->p_send.bytes += len +
729 buffer_len(&active_state->outgoing_packet);
730 buffer_clear(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731
Damien Miller5428f641999-11-25 11:54:57 +1100732 /*
Damien Miller788f2122005-11-05 15:14:59 +1100733 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100734 * actually sent until packet_write_wait or packet_write_poll is
735 * called.
736 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737}
738
Ben Lindstromf6027d32002-03-22 01:42:04 +0000739void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000740set_newkeys(int mode)
741{
742 Enc *enc;
743 Mac *mac;
744 Comp *comp;
745 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000746 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000747 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000748
Ben Lindstrom064496f2002-12-23 02:04:22 +0000749 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000750
Damien Miller963f6b22002-02-19 15:21:23 +1100751 if (mode == MODE_OUT) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000752 cc = &active_state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000753 crypt_type = CIPHER_ENCRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000754 active_state->p_send.packets = active_state->p_send.blocks = 0;
755 max_blocks = &active_state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100756 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000757 cc = &active_state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000758 crypt_type = CIPHER_DECRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000759 active_state->p_read.packets = active_state->p_read.blocks = 0;
760 max_blocks = &active_state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100761 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000762 if (active_state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000763 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100764 cipher_cleanup(cc);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000765 enc = &active_state->newkeys[mode]->enc;
766 mac = &active_state->newkeys[mode]->mac;
767 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000768 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100769 explicit_bzero(enc->iv, enc->iv_len);
770 explicit_bzero(enc->key, enc->key_len);
771 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000772 free(enc->name);
773 free(enc->iv);
774 free(enc->key);
775 free(mac->name);
776 free(mac->key);
777 free(comp->name);
778 free(active_state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000779 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000780 active_state->newkeys[mode] = kex_get_newkeys(mode);
781 if (active_state->newkeys[mode] == NULL)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000782 fatal("newkeys: no keys for mode %d", mode);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000783 enc = &active_state->newkeys[mode]->enc;
784 mac = &active_state->newkeys[mode]->mac;
785 comp = &active_state->newkeys[mode]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100786 if (cipher_authlen(enc->cipher) == 0 && mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000787 mac->enabled = 1;
788 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100789 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Damien Miller1d75abf2013-01-09 16:12:19 +1100790 enc->iv, enc->iv_len, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000791 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100792 /* explicit_bzero(enc->iv, enc->block_size);
793 explicit_bzero(enc->key, enc->key_len);
794 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000795 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000796 (comp->type == COMP_DELAYED &&
797 active_state->after_authentication)) && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000798 packet_init_compression();
799 if (mode == MODE_OUT)
800 buffer_compress_init_send(6);
801 else
802 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000803 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000804 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000805 /*
806 * The 2^(blocksize*2) limit is too expensive for 3DES,
807 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
808 */
809 if (enc->block_size >= 16)
810 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
811 else
812 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000813 if (active_state->rekey_limit)
814 *max_blocks = MIN(*max_blocks,
815 active_state->rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000816}
817
Damien Miller5428f641999-11-25 11:54:57 +1100818/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000819 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000820 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000821 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
822 */
823static void
824packet_enable_delayed_compress(void)
825{
826 Comp *comp = NULL;
827 int mode;
828
829 /*
830 * Remember that we are past the authentication step, so rekeying
831 * with COMP_DELAYED will turn on compression immediately.
832 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000833 active_state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +1000834 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000835 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000836 if (active_state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +1000837 continue;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000838 comp = &active_state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +1000839 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000840 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000841 if (mode == MODE_OUT)
842 buffer_compress_init_send(6);
843 else
844 buffer_compress_init_recv();
845 comp->enabled = 1;
846 }
847 }
848}
849
850/*
Damien Miller33b13562000-04-04 14:38:59 +1000851 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
852 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000853static void
Damien Millera5539d22003-04-09 20:50:06 +1000854packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000855{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000856 u_char type, *cp, *macbuf = NULL;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100857 u_char padlen, pad = 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100858 u_int i, len, authlen = 0, aadlen = 0;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000859 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000860 Enc *enc = NULL;
861 Mac *mac = NULL;
862 Comp *comp = NULL;
863 int block_size;
864
Darren Tuckerf7288d72009-06-21 18:12:20 +1000865 if (active_state->newkeys[MODE_OUT] != NULL) {
866 enc = &active_state->newkeys[MODE_OUT]->enc;
867 mac = &active_state->newkeys[MODE_OUT]->mac;
868 comp = &active_state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100869 /* disable mac for authenticated encryption */
870 if ((authlen = cipher_authlen(enc->cipher)) != 0)
871 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +1000872 }
Damien Miller963f6b22002-02-19 15:21:23 +1100873 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +1100874 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +1000875
Darren Tuckerf7288d72009-06-21 18:12:20 +1000876 cp = buffer_ptr(&active_state->outgoing_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000877 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000878
879#ifdef PACKET_DEBUG
880 fprintf(stderr, "plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000881 buffer_dump(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000882#endif
883
884 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000885 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000886 /* skip header, compress only payload */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000887 buffer_consume(&active_state->outgoing_packet, 5);
888 buffer_clear(&active_state->compression_buffer);
889 buffer_compress(&active_state->outgoing_packet,
890 &active_state->compression_buffer);
891 buffer_clear(&active_state->outgoing_packet);
892 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
893 buffer_append(&active_state->outgoing_packet,
894 buffer_ptr(&active_state->compression_buffer),
895 buffer_len(&active_state->compression_buffer));
Damien Miller33b13562000-04-04 14:38:59 +1000896 DBG(debug("compression: raw %d compressed %d", len,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000897 buffer_len(&active_state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +1000898 }
899
900 /* sizeof (packet_len + pad_len + payload) */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000901 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000902
903 /*
904 * calc size of padding, alloc space, get random data,
905 * minimum padding is 4 bytes
906 */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100907 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +1000908 padlen = block_size - (len % block_size);
909 if (padlen < 4)
910 padlen += block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000911 if (active_state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +1100912 /* will wrap if extra_pad+padlen > 255 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000913 active_state->extra_pad =
914 roundup(active_state->extra_pad, block_size);
915 pad = active_state->extra_pad -
916 ((len + padlen) % active_state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +1000917 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
918 __func__, pad, len, padlen, active_state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +1100919 padlen += pad;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000920 active_state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100921 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000922 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
923 if (enc && !active_state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000924 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000925 for (i = 0; i < padlen; i++) {
926 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000927 rnd = arc4random();
928 cp[i] = rnd & 0xff;
929 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000930 }
Damien Millere247cc42000-05-07 12:03:14 +1000931 } else {
932 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +1100933 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000934 }
Damien Milleraf43a7a2012-12-12 10:46:31 +1100935 /* sizeof (packet_len + pad_len + payload + padding) */
936 len = buffer_len(&active_state->outgoing_packet);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000937 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100938 /* packet_length includes payload, padding and padding length field */
939 put_u32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000940 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100941 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
942 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +1000943
944 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100945 if (mac && mac->enabled && !mac->etm) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000946 macbuf = mac_compute(mac, active_state->p_send.seqnr,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100947 buffer_ptr(&active_state->outgoing_packet), len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000948 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000949 }
950 /* encrypt packet and append to output buffer. */
Damien Miller1d75abf2013-01-09 16:12:19 +1100951 cp = buffer_append_space(&active_state->output, len + authlen);
Damien Millerbcd00ab2013-12-07 10:41:55 +1100952 if (cipher_crypt(&active_state->send_context, active_state->p_send.seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100953 cp, buffer_ptr(&active_state->outgoing_packet),
Damien Millerbcd00ab2013-12-07 10:41:55 +1100954 len - aadlen, aadlen, authlen) != 0)
955 fatal("%s: cipher_crypt failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +1000956 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100957 if (mac && mac->enabled) {
958 if (mac->etm) {
959 /* EtM: compute mac over aadlen + cipher text */
960 macbuf = mac_compute(mac,
961 active_state->p_send.seqnr, cp, len);
962 DBG(debug("done calc MAC(EtM) out #%d",
963 active_state->p_send.seqnr));
964 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000965 buffer_append(&active_state->output, macbuf, mac->mac_len);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100966 }
Damien Miller33b13562000-04-04 14:38:59 +1000967#ifdef PACKET_DEBUG
968 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000969 buffer_dump(&active_state->output);
Damien Miller33b13562000-04-04 14:38:59 +1000970#endif
Damien Miller4af51302000-04-16 11:18:38 +1000971 /* increment sequence number for outgoing packets */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000972 if (++active_state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000973 logit("outgoing seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000974 if (++active_state->p_send.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +1000975 if (!(datafellows & SSH_BUG_NOREKEY))
976 fatal("XXX too many packets with same key");
Damien Milleraf43a7a2012-12-12 10:46:31 +1100977 active_state->p_send.blocks += len / block_size;
978 active_state->p_send.bytes += len;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000979 buffer_clear(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000980
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000981 if (type == SSH2_MSG_NEWKEYS)
982 set_newkeys(MODE_OUT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000983 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +1000984 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000985}
986
Damien Millera5539d22003-04-09 20:50:06 +1000987static void
988packet_send2(void)
989{
Damien Millera5539d22003-04-09 20:50:06 +1000990 struct packet *p;
991 u_char type, *cp;
992
Darren Tuckerf7288d72009-06-21 18:12:20 +1000993 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000994 type = cp[5];
995
996 /* during rekeying we can only send key exchange messages */
Darren Tucker7b935c72009-06-21 18:59:36 +1000997 if (active_state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +1100998 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
999 (type > SSH2_MSG_TRANSPORT_MAX) ||
1000 (type == SSH2_MSG_SERVICE_REQUEST) ||
1001 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +10001002 debug("enqueue packet: %u", type);
Damien Miller6c81fee2013-11-08 12:19:55 +11001003 p = xcalloc(1, sizeof(*p));
Damien Millera5539d22003-04-09 20:50:06 +10001004 p->type = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001005 memcpy(&p->payload, &active_state->outgoing_packet,
1006 sizeof(Buffer));
1007 buffer_init(&active_state->outgoing_packet);
1008 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +10001009 return;
1010 }
1011 }
1012
1013 /* rekeying starts with sending KEXINIT */
1014 if (type == SSH2_MSG_KEXINIT)
Darren Tucker7b935c72009-06-21 18:59:36 +10001015 active_state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001016
1017 packet_send2_wrapped();
1018
1019 /* after a NEWKEYS message we can send the complete queue */
1020 if (type == SSH2_MSG_NEWKEYS) {
Darren Tucker7b935c72009-06-21 18:59:36 +10001021 active_state->rekeying = 0;
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001022 active_state->rekey_time = monotime();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001023 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001024 type = p->type;
1025 debug("dequeue packet: %u", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001026 buffer_free(&active_state->outgoing_packet);
1027 memcpy(&active_state->outgoing_packet, &p->payload,
Damien Millera5539d22003-04-09 20:50:06 +10001028 sizeof(Buffer));
Darren Tuckerf7288d72009-06-21 18:12:20 +10001029 TAILQ_REMOVE(&active_state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001030 free(p);
Damien Millera5539d22003-04-09 20:50:06 +10001031 packet_send2_wrapped();
1032 }
1033 }
1034}
1035
Damien Miller33b13562000-04-04 14:38:59 +10001036void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001037packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +10001038{
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001039 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +10001040 packet_send2();
1041 else
1042 packet_send1();
1043 DBG(debug("packet_send done"));
1044}
1045
Damien Miller33b13562000-04-04 14:38:59 +10001046/*
Damien Miller5428f641999-11-25 11:54:57 +11001047 * Waits until a packet has been received, and returns its type. Note that
1048 * no other data is processed until this returns, so this function should not
1049 * be used during the interactive session.
1050 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001051
1052int
Damien Millerdff50992002-01-22 23:16:32 +11001053packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001054{
Damien Millerce986542013-07-18 16:12:44 +10001055 int type, len, ret, cont, ms_remain = 0;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001056 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001057 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001058 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059
Darren Tucker99bb7612008-06-13 22:02:50 +10001060 DBG(debug("packet_read()"));
1061
Darren Tuckerf7288d72009-06-21 18:12:20 +10001062 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1063 NFDBITS), sizeof(fd_mask));
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001064
Damien Miller95def091999-11-25 00:26:21 +11001065 /* Since we are blocking, ensure that all written packets have been sent. */
1066 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
Damien Miller95def091999-11-25 00:26:21 +11001068 /* Stay in the loop until we have received a complete packet. */
1069 for (;;) {
1070 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +11001071 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001072 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +10001073 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +11001074 || type == SSH_SMSG_FAILURE
1075 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +10001076 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +11001077 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001078 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001079 if (type != SSH_MSG_NONE) {
Darren Tuckera627d422013-06-02 07:31:17 +10001080 free(setp);
Damien Miller95def091999-11-25 00:26:21 +11001081 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001082 }
Damien Miller5428f641999-11-25 11:54:57 +11001083 /*
1084 * Otherwise, wait for some data to arrive, add it to the
1085 * buffer, and try again.
1086 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001087 memset(setp, 0, howmany(active_state->connection_in + 1,
1088 NFDBITS) * sizeof(fd_mask));
1089 FD_SET(active_state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001090
Darren Tuckerf7288d72009-06-21 18:12:20 +10001091 if (active_state->packet_timeout_ms > 0) {
1092 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001093 timeoutp = &timeout;
1094 }
Damien Miller95def091999-11-25 00:26:21 +11001095 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001096 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001097 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001098 ms_to_timeval(&timeout, ms_remain);
1099 gettimeofday(&start, NULL);
1100 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001101 if ((ret = select(active_state->connection_in + 1, setp,
1102 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001103 break;
Damien Millerea437422009-10-02 11:49:03 +10001104 if (errno != EAGAIN && errno != EINTR &&
1105 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001106 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001107 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001108 continue;
1109 ms_subtract_diff(&start, &ms_remain);
1110 if (ms_remain <= 0) {
1111 ret = 0;
1112 break;
1113 }
1114 }
1115 if (ret == 0) {
1116 logit("Connection to %.200s timed out while "
1117 "waiting to read", get_remote_ipaddr());
1118 cleanup_exit(255);
1119 }
Damien Miller95def091999-11-25 00:26:21 +11001120 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001121 do {
1122 cont = 0;
1123 len = roaming_read(active_state->connection_in, buf,
1124 sizeof(buf), &cont);
1125 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001126 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001127 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +10001128 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001129 }
Damien Miller95def091999-11-25 00:26:21 +11001130 if (len < 0)
1131 fatal("Read from socket failed: %.100s", strerror(errno));
1132 /* Append it to the buffer. */
1133 packet_process_incoming(buf, len);
1134 }
1135 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136}
1137
Damien Miller278f9072001-12-21 15:00:19 +11001138int
Damien Millerdff50992002-01-22 23:16:32 +11001139packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +11001140{
Damien Millerdff50992002-01-22 23:16:32 +11001141 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001142}
1143
Damien Miller5428f641999-11-25 11:54:57 +11001144/*
1145 * Waits until a packet has been received, verifies that its type matches
1146 * that given, and gives a fatal error and exits if there is a mismatch.
1147 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001148
1149void
Damien Millerdff50992002-01-22 23:16:32 +11001150packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001151{
Damien Miller95def091999-11-25 00:26:21 +11001152 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153
Damien Millerdff50992002-01-22 23:16:32 +11001154 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001155 if (type != expected_type)
1156 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001157 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158}
1159
1160/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001161 * packet_process_incoming. If so, reads the packet; otherwise returns
1162 * SSH_MSG_NONE. This does not wait for data from the connection.
1163 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001164 * SSH_MSG_DISCONNECT is handled specially here. Also,
1165 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1166 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001167 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168
Ben Lindstrombba81212001-06-25 05:01:22 +00001169static int
Damien Millerdff50992002-01-22 23:16:32 +11001170packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001172 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001173 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001174 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175
Damien Miller95def091999-11-25 00:26:21 +11001176 /* Check if input size is less than minimum packet size. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001177 if (buffer_len(&active_state->input) < 4 + 8)
Damien Miller95def091999-11-25 00:26:21 +11001178 return SSH_MSG_NONE;
1179 /* Get length of incoming packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001180 cp = buffer_ptr(&active_state->input);
Damien Miller3f941882006-03-31 23:13:02 +11001181 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001182 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001183 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001184 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185
Damien Miller95def091999-11-25 00:26:21 +11001186 /* Check if the packet has been entirely received. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001187 if (buffer_len(&active_state->input) < 4 + padded_len)
Damien Miller95def091999-11-25 00:26:21 +11001188 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001189
Damien Miller95def091999-11-25 00:26:21 +11001190 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001191
Damien Miller95def091999-11-25 00:26:21 +11001192 /* Consume packet length. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001193 buffer_consume(&active_state->input, 4);
Damien Miller95def091999-11-25 00:26:21 +11001194
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001195 /*
1196 * Cryptographic attack detector for ssh
1197 * (C)1998 CORE-SDI, Buenos Aires Argentina
1198 * Ariel Futoransky(futo@core-sdi.com)
1199 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001200 if (!active_state->receive_context.plaintext) {
1201 switch (detect_attack(buffer_ptr(&active_state->input),
1202 padded_len)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001203 case DEATTACK_DETECTED:
1204 packet_disconnect("crc32 compensation attack: "
1205 "network attack detected");
1206 case DEATTACK_DOS_DETECTED:
1207 packet_disconnect("deattack denial of "
1208 "service detected");
1209 }
1210 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001211
1212 /* Decrypt data to incoming_packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001213 buffer_clear(&active_state->incoming_packet);
1214 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
Damien Millerbcd00ab2013-12-07 10:41:55 +11001215 if (cipher_crypt(&active_state->receive_context, 0, cp,
1216 buffer_ptr(&active_state->input), padded_len, 0, 0) != 0)
1217 fatal("%s: cipher_crypt failed", __func__);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001218
Darren Tuckerf7288d72009-06-21 18:12:20 +10001219 buffer_consume(&active_state->input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220
1221#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001222 fprintf(stderr, "read_poll plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001223 buffer_dump(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001224#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225
Damien Miller95def091999-11-25 00:26:21 +11001226 /* Compute packet checksum. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001227 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1228 buffer_len(&active_state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001229
Damien Miller95def091999-11-25 00:26:21 +11001230 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001231 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001232
Damien Miller95def091999-11-25 00:26:21 +11001233 /* Test check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001234 if (len != buffer_len(&active_state->incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001235 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001236 len, buffer_len(&active_state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237
Darren Tuckerf7288d72009-06-21 18:12:20 +10001238 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001239 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001240 if (checksum != stored_checksum)
1241 packet_disconnect("Corrupted check bytes on input.");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001242 buffer_consume_end(&active_state->incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243
Darren Tuckerf7288d72009-06-21 18:12:20 +10001244 if (active_state->packet_compression) {
1245 buffer_clear(&active_state->compression_buffer);
1246 buffer_uncompress(&active_state->incoming_packet,
1247 &active_state->compression_buffer);
1248 buffer_clear(&active_state->incoming_packet);
1249 buffer_append(&active_state->incoming_packet,
1250 buffer_ptr(&active_state->compression_buffer),
1251 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001252 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001253 active_state->p_read.packets++;
1254 active_state->p_read.bytes += padded_len + 4;
1255 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001256 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1257 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001258 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259}
Damien Miller95def091999-11-25 00:26:21 +11001260
Ben Lindstrombba81212001-06-25 05:01:22 +00001261static int
Damien Millerdff50992002-01-22 23:16:32 +11001262packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001263{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001264 u_int padlen, need;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001265 u_char *macbuf = NULL, *cp, type;
Damien Miller1d75abf2013-01-09 16:12:19 +11001266 u_int maclen, authlen = 0, aadlen = 0, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001267 Enc *enc = NULL;
1268 Mac *mac = NULL;
1269 Comp *comp = NULL;
1270
Darren Tuckerf7288d72009-06-21 18:12:20 +10001271 if (active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001272 return SSH_MSG_NONE;
1273
Darren Tuckerf7288d72009-06-21 18:12:20 +10001274 if (active_state->newkeys[MODE_IN] != NULL) {
1275 enc = &active_state->newkeys[MODE_IN]->enc;
1276 mac = &active_state->newkeys[MODE_IN]->mac;
1277 comp = &active_state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001278 /* disable mac for authenticated encryption */
1279 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1280 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001281 }
1282 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001283 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001284 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001285
Damien Milleraf43a7a2012-12-12 10:46:31 +11001286 if (aadlen && active_state->packlen == 0) {
Damien Miller0fde8ac2013-11-21 14:12:23 +11001287 if (cipher_get_length(&active_state->receive_context,
1288 &active_state->packlen,
1289 active_state->p_read.seqnr,
1290 buffer_ptr(&active_state->input),
1291 buffer_len(&active_state->input)) != 0)
Damien Milleraf43a7a2012-12-12 10:46:31 +11001292 return SSH_MSG_NONE;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001293 if (active_state->packlen < 1 + 4 ||
1294 active_state->packlen > PACKET_MAX_SIZE) {
1295#ifdef PACKET_DEBUG
1296 buffer_dump(&active_state->input);
1297#endif
1298 logit("Bad packet length %u.", active_state->packlen);
1299 packet_disconnect("Packet corrupt");
1300 }
Damien Miller8c05da32012-12-13 07:18:59 +11001301 buffer_clear(&active_state->incoming_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001302 } else if (active_state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001303 /*
1304 * check if input size is less than the cipher block size,
1305 * decrypt first block and extract length of incoming packet
1306 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001307 if (buffer_len(&active_state->input) < block_size)
Damien Miller33b13562000-04-04 14:38:59 +10001308 return SSH_MSG_NONE;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001309 buffer_clear(&active_state->incoming_packet);
1310 cp = buffer_append_space(&active_state->incoming_packet,
Damien Miller33b13562000-04-04 14:38:59 +10001311 block_size);
Damien Millerbcd00ab2013-12-07 10:41:55 +11001312 if (cipher_crypt(&active_state->receive_context,
Damien Miller0fde8ac2013-11-21 14:12:23 +11001313 active_state->p_read.seqnr, cp,
Damien Millerbcd00ab2013-12-07 10:41:55 +11001314 buffer_ptr(&active_state->input), block_size, 0, 0) != 0)
1315 fatal("Decryption integrity check failed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001316 cp = buffer_ptr(&active_state->incoming_packet);
1317 active_state->packlen = get_u32(cp);
1318 if (active_state->packlen < 1 + 4 ||
1319 active_state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001320#ifdef PACKET_DEBUG
Darren Tuckerf7288d72009-06-21 18:12:20 +10001321 buffer_dump(&active_state->incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001322#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001323 logit("Bad packet length %u.", active_state->packlen);
1324 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001325 PACKET_MAX_SIZE);
1326 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001327 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001328 buffer_consume(&active_state->input, block_size);
Damien Miller33b13562000-04-04 14:38:59 +10001329 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001330 DBG(debug("input: packet len %u", active_state->packlen+4));
1331 if (aadlen) {
1332 /* only the payload is encrypted */
1333 need = active_state->packlen;
1334 } else {
1335 /*
1336 * the payload size and the payload are encrypted, but we
1337 * have a partial packet of block_size bytes
1338 */
1339 need = 4 + active_state->packlen - block_size;
1340 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001341 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1342 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001343 if (need % block_size != 0) {
1344 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001345 need, block_size, need % block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001346 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001347 PACKET_MAX_SIZE - block_size);
1348 return SSH_MSG_NONE;
Darren Tucker99d11a32008-12-01 21:40:48 +11001349 }
Damien Miller33b13562000-04-04 14:38:59 +10001350 /*
1351 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001352 * decrypt into incoming_packet:
1353 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001354 * 'need' bytes are encrypted, followed by either
1355 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001356 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001357 */
Damien Miller1d75abf2013-01-09 16:12:19 +11001358 if (buffer_len(&active_state->input) < aadlen + need + authlen + maclen)
Damien Miller33b13562000-04-04 14:38:59 +10001359 return SSH_MSG_NONE;
1360#ifdef PACKET_DEBUG
1361 fprintf(stderr, "read_poll enc/full: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001362 buffer_dump(&active_state->input);
Damien Miller33b13562000-04-04 14:38:59 +10001363#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001364 /* EtM: compute mac over encrypted input */
1365 if (mac && mac->enabled && mac->etm)
1366 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1367 buffer_ptr(&active_state->input), aadlen + need);
1368 cp = buffer_append_space(&active_state->incoming_packet, aadlen + need);
Damien Millerbcd00ab2013-12-07 10:41:55 +11001369 if (cipher_crypt(&active_state->receive_context,
Damien Miller0fde8ac2013-11-21 14:12:23 +11001370 active_state->p_read.seqnr, cp,
Damien Millerbcd00ab2013-12-07 10:41:55 +11001371 buffer_ptr(&active_state->input), need, aadlen, authlen) != 0)
1372 fatal("Decryption integrity check failed");
Damien Miller1d75abf2013-01-09 16:12:19 +11001373 buffer_consume(&active_state->input, aadlen + need + authlen);
Damien Miller33b13562000-04-04 14:38:59 +10001374 /*
1375 * compute MAC over seqnr and packet,
1376 * increment sequence number for incoming packet
1377 */
Damien Miller4af51302000-04-16 11:18:38 +10001378 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001379 if (!mac->etm)
1380 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1381 buffer_ptr(&active_state->incoming_packet),
1382 buffer_len(&active_state->incoming_packet));
Damien Millerea1651c2010-07-16 13:58:37 +10001383 if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001384 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001385 logit("Corrupted MAC on input.");
1386 if (need > PACKET_MAX_SIZE)
1387 fatal("internal error need %d", need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001388 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001389 PACKET_MAX_SIZE - need);
1390 return SSH_MSG_NONE;
1391 }
1392
Darren Tuckerf7288d72009-06-21 18:12:20 +10001393 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1394 buffer_consume(&active_state->input, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +10001395 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001396 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001397 if (seqnr_p != NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001398 *seqnr_p = active_state->p_read.seqnr;
1399 if (++active_state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001400 logit("incoming seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001401 if (++active_state->p_read.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +10001402 if (!(datafellows & SSH_BUG_NOREKEY))
1403 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001404 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1405 active_state->p_read.bytes += active_state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001406
1407 /* get padlen */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001408 cp = buffer_ptr(&active_state->incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001409 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001410 DBG(debug("input: padlen %d", padlen));
1411 if (padlen < 4)
1412 packet_disconnect("Corrupted padlen %d on input.", padlen);
1413
1414 /* skip packet size + padlen, discard padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001415 buffer_consume(&active_state->incoming_packet, 4 + 1);
1416 buffer_consume_end(&active_state->incoming_packet, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001417
Darren Tuckerf7288d72009-06-21 18:12:20 +10001418 DBG(debug("input: len before de-compress %d",
1419 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001420 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001421 buffer_clear(&active_state->compression_buffer);
1422 buffer_uncompress(&active_state->incoming_packet,
1423 &active_state->compression_buffer);
1424 buffer_clear(&active_state->incoming_packet);
1425 buffer_append(&active_state->incoming_packet,
1426 buffer_ptr(&active_state->compression_buffer),
1427 buffer_len(&active_state->compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001428 DBG(debug("input: len after de-compress %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001429 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001430 }
1431 /*
1432 * get packet type, implies consume.
1433 * return length of payload (without type field)
1434 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001435 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001436 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1437 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001438 if (type == SSH2_MSG_NEWKEYS)
1439 set_newkeys(MODE_IN);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001440 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1441 !active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001442 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001443#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001444 fprintf(stderr, "read/plain[%d]:\r\n", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001445 buffer_dump(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001446#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001447 /* reset for next packet */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001448 active_state->packlen = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001449 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001450}
1451
1452int
Damien Millerdff50992002-01-22 23:16:32 +11001453packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001454{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001455 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001456 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001457 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001458
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001459 for (;;) {
1460 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001461 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001462 if (type) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001463 active_state->keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001464 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001465 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001466 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001467 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001468 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001469 break;
Damien Miller33b13562000-04-04 14:38:59 +10001470 case SSH2_MSG_DEBUG:
1471 packet_get_char();
1472 msg = packet_get_string(NULL);
1473 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001474 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001475 msg = packet_get_string(NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001476 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001477 break;
1478 case SSH2_MSG_DISCONNECT:
1479 reason = packet_get_int();
1480 msg = packet_get_string(NULL);
Damien Millerd5edefd2013-04-23 15:21:39 +10001481 /* Ignore normal client exit notifications */
1482 do_log2(active_state->server_side &&
1483 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1484 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1485 "Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001486 get_remote_ipaddr(), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001487 free(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001488 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001489 break;
Damien Miller659811f2002-01-22 23:23:11 +11001490 case SSH2_MSG_UNIMPLEMENTED:
1491 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001492 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1493 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001494 break;
Damien Miller33b13562000-04-04 14:38:59 +10001495 default:
1496 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001497 }
Damien Miller33b13562000-04-04 14:38:59 +10001498 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001499 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001500 switch (type) {
Damien Millerce986542013-07-18 16:12:44 +10001501 case SSH_MSG_NONE:
1502 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001503 case SSH_MSG_IGNORE:
1504 break;
1505 case SSH_MSG_DEBUG:
1506 msg = packet_get_string(NULL);
1507 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001508 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001509 break;
1510 case SSH_MSG_DISCONNECT:
1511 msg = packet_get_string(NULL);
Damien Miller894926e2013-02-12 11:03:58 +11001512 error("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001513 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001514 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001515 break;
1516 default:
Damien Millerce986542013-07-18 16:12:44 +10001517 DBG(debug("received packet type %d", type));
Damien Miller33b13562000-04-04 14:38:59 +10001518 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001519 }
Damien Miller33b13562000-04-04 14:38:59 +10001520 }
1521 }
1522}
1523
Damien Miller5428f641999-11-25 11:54:57 +11001524/*
1525 * Buffers the given amount of input characters. This is intended to be used
1526 * together with packet_read_poll.
1527 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001528
1529void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001530packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001531{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001532 if (active_state->packet_discard) {
1533 active_state->keep_alive_timeouts = 0; /* ?? */
1534 if (len >= active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001535 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001536 active_state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001537 return;
1538 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001539 buffer_append(&active_state->input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001540}
1541
1542/* Returns a character from the packet. */
1543
Ben Lindstrom46c16222000-12-22 01:43:59 +00001544u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001545packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001546{
Damien Miller95def091999-11-25 00:26:21 +11001547 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001548
Darren Tuckerf7288d72009-06-21 18:12:20 +10001549 buffer_get(&active_state->incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001550 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001551}
1552
1553/* Returns an integer from the packet data. */
1554
Ben Lindstrom46c16222000-12-22 01:43:59 +00001555u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001556packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001557{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001558 return buffer_get_int(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001559}
1560
Darren Tucker761c3892009-06-21 18:16:26 +10001561/* Returns an 64 bit integer from the packet data. */
1562
1563u_int64_t
1564packet_get_int64(void)
1565{
1566 return buffer_get_int64(&active_state->incoming_packet);
1567}
1568
Damien Miller5428f641999-11-25 11:54:57 +11001569/*
1570 * Returns an arbitrary precision integer from the packet data. The integer
1571 * must have been initialized before this call.
1572 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001573
Damien Miller1f0311c2014-05-15 14:24:09 +10001574#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001575void
Damien Millerd432ccf2002-01-22 23:14:44 +11001576packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001577{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001578 buffer_get_bignum(&active_state->incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001579}
1580
Damien Miller33b13562000-04-04 14:38:59 +10001581void
Damien Millerd432ccf2002-01-22 23:14:44 +11001582packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001583{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001584 buffer_get_bignum2(&active_state->incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001585}
1586
Damien Miller6af914a2010-09-10 11:39:26 +10001587#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001588void
1589packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point)
1590{
1591 buffer_get_ecpoint(&active_state->incoming_packet, curve, point);
1592}
Damien Miller6af914a2010-09-10 11:39:26 +10001593#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001594
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001595void *
Damien Millereccb9de2005-06-17 12:59:34 +10001596packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001597{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001598 u_int bytes = buffer_len(&active_state->incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001599
Damien Miller33b13562000-04-04 14:38:59 +10001600 if (length_ptr != NULL)
1601 *length_ptr = bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001602 return buffer_ptr(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001603}
Damien Miller1f0311c2014-05-15 14:24:09 +10001604#endif
Damien Miller33b13562000-04-04 14:38:59 +10001605
Damien Miller4af51302000-04-16 11:18:38 +10001606int
1607packet_remaining(void)
1608{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001609 return buffer_len(&active_state->incoming_packet);
Damien Miller4af51302000-04-16 11:18:38 +10001610}
1611
Damien Miller5428f641999-11-25 11:54:57 +11001612/*
1613 * Returns a string from the packet data. The string is allocated using
1614 * xmalloc; it is the responsibility of the calling program to free it when
1615 * no longer needed. The length_ptr argument may be NULL, or point to an
1616 * integer into which the length of the string is stored.
1617 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001618
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001619void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001620packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001622 return buffer_get_string(&active_state->incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001623}
1624
Damien Miller633de332014-05-15 13:48:26 +10001625const void *
Damien Millerdb255ca2008-05-19 14:59:37 +10001626packet_get_string_ptr(u_int *length_ptr)
1627{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001628 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
Damien Millerdb255ca2008-05-19 14:59:37 +10001629}
1630
Damien Millerda108ec2010-08-31 22:36:39 +10001631/* Ensures the returned string has no embedded \0 characters in it. */
1632char *
1633packet_get_cstring(u_int *length_ptr)
1634{
1635 return buffer_get_cstring(&active_state->incoming_packet, length_ptr);
1636}
1637
Damien Miller5428f641999-11-25 11:54:57 +11001638/*
1639 * Sends a diagnostic message from the server to the client. This message
1640 * can be sent at any time (but not while constructing another message). The
1641 * message is printed immediately, but only if the client is being executed
1642 * in verbose mode. These messages are primarily intended to ease debugging
1643 * authentication problems. The length of the formatted message must not
1644 * exceed 1024 bytes. This will automatically call packet_write_wait.
1645 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001646
1647void
Damien Miller95def091999-11-25 00:26:21 +11001648packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001649{
Damien Miller95def091999-11-25 00:26:21 +11001650 char buf[1024];
1651 va_list args;
1652
Ben Lindstroma14ee472000-12-07 01:24:58 +00001653 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1654 return;
1655
Damien Miller95def091999-11-25 00:26:21 +11001656 va_start(args, fmt);
1657 vsnprintf(buf, sizeof(buf), fmt, args);
1658 va_end(args);
1659
Damien Miller7c8af4f2000-05-01 08:24:07 +10001660 if (compat20) {
1661 packet_start(SSH2_MSG_DEBUG);
1662 packet_put_char(0); /* bool: always display */
1663 packet_put_cstring(buf);
1664 packet_put_cstring("");
1665 } else {
1666 packet_start(SSH_MSG_DEBUG);
1667 packet_put_cstring(buf);
1668 }
Damien Miller95def091999-11-25 00:26:21 +11001669 packet_send();
1670 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001671}
1672
Damien Miller5428f641999-11-25 11:54:57 +11001673/*
1674 * Logs the error plus constructs and sends a disconnect packet, closes the
1675 * connection, and exits. This function never returns. The error message
1676 * should not contain a newline. The length of the formatted message must
1677 * not exceed 1024 bytes.
1678 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001679
1680void
Damien Miller95def091999-11-25 00:26:21 +11001681packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001682{
Damien Miller95def091999-11-25 00:26:21 +11001683 char buf[1024];
1684 va_list args;
1685 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001686
Damien Miller95def091999-11-25 00:26:21 +11001687 if (disconnecting) /* Guard against recursive invocations. */
1688 fatal("packet_disconnect called recursively.");
1689 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001690
Damien Miller5428f641999-11-25 11:54:57 +11001691 /*
1692 * Format the message. Note that the caller must make sure the
1693 * message is of limited size.
1694 */
Damien Miller95def091999-11-25 00:26:21 +11001695 va_start(args, fmt);
1696 vsnprintf(buf, sizeof(buf), fmt, args);
1697 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001698
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001699 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001700 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001701
Damien Miller95def091999-11-25 00:26:21 +11001702 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001703 if (compat20) {
1704 packet_start(SSH2_MSG_DISCONNECT);
1705 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1706 packet_put_cstring(buf);
1707 packet_put_cstring("");
1708 } else {
1709 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001710 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001711 }
Damien Miller95def091999-11-25 00:26:21 +11001712 packet_send();
1713 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001714
Damien Miller95def091999-11-25 00:26:21 +11001715 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001716 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001717
Damien Miller95def091999-11-25 00:26:21 +11001718 /* Close the connection. */
1719 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001720 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001721}
1722
Damien Miller5428f641999-11-25 11:54:57 +11001723/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001724
1725void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001726packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001727{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001728 int len = buffer_len(&active_state->output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001729 int cont;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001730
Damien Miller95def091999-11-25 00:26:21 +11001731 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001732 cont = 0;
1733 len = roaming_write(active_state->connection_out,
1734 buffer_ptr(&active_state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001735 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001736 if (errno == EINTR || errno == EAGAIN ||
1737 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001738 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001739 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001740 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001741 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001742 fatal("Write connection closed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001743 buffer_consume(&active_state->output, len);
Damien Miller95def091999-11-25 00:26:21 +11001744 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001745}
1746
Damien Miller5428f641999-11-25 11:54:57 +11001747/*
1748 * Calls packet_write_poll repeatedly until all pending output data has been
1749 * written.
1750 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001751
1752void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001753packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001754{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001755 fd_set *setp;
Damien Millerce986542013-07-18 16:12:44 +10001756 int ret, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001757 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001758
Darren Tuckerf7288d72009-06-21 18:12:20 +10001759 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1760 NFDBITS), sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001761 packet_write_poll();
1762 while (packet_have_data_to_write()) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001763 memset(setp, 0, howmany(active_state->connection_out + 1,
1764 NFDBITS) * sizeof(fd_mask));
1765 FD_SET(active_state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001766
Darren Tuckerf7288d72009-06-21 18:12:20 +10001767 if (active_state->packet_timeout_ms > 0) {
1768 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001769 timeoutp = &timeout;
1770 }
1771 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001772 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001773 ms_to_timeval(&timeout, ms_remain);
1774 gettimeofday(&start, NULL);
1775 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001776 if ((ret = select(active_state->connection_out + 1,
1777 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001778 break;
Damien Millerea437422009-10-02 11:49:03 +10001779 if (errno != EAGAIN && errno != EINTR &&
1780 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001781 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001782 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001783 continue;
1784 ms_subtract_diff(&start, &ms_remain);
1785 if (ms_remain <= 0) {
1786 ret = 0;
1787 break;
1788 }
1789 }
1790 if (ret == 0) {
1791 logit("Connection to %.200s timed out while "
1792 "waiting to write", get_remote_ipaddr());
1793 cleanup_exit(255);
1794 }
Damien Miller95def091999-11-25 00:26:21 +11001795 packet_write_poll();
1796 }
Darren Tuckera627d422013-06-02 07:31:17 +10001797 free(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001798}
1799
1800/* Returns true if there is buffered data to write to the connection. */
1801
1802int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001803packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001804{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001805 return buffer_len(&active_state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001806}
1807
1808/* Returns true if there is not too much data to write to the connection. */
1809
1810int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001811packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001812{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001813 if (active_state->interactive_mode)
1814 return buffer_len(&active_state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11001815 else
Darren Tuckerf7288d72009-06-21 18:12:20 +10001816 return buffer_len(&active_state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001817}
1818
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001819static void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001820packet_set_tos(int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00001821{
Damien Millerd2ac5d72011-05-15 08:43:13 +10001822#ifndef IP_TOS_IS_BROKEN
1823 if (!packet_connection_is_on_socket())
Ben Lindstroma7433982002-12-23 02:41:41 +00001824 return;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001825 switch (packet_connection_af()) {
1826# ifdef IP_TOS
1827 case AF_INET:
1828 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1829 if (setsockopt(active_state->connection_in,
1830 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
1831 error("setsockopt IP_TOS %d: %.100s:",
1832 tos, strerror(errno));
1833 break;
1834# endif /* IP_TOS */
1835# ifdef IPV6_TCLASS
1836 case AF_INET6:
1837 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1838 if (setsockopt(active_state->connection_in,
1839 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
1840 error("setsockopt IPV6_TCLASS %d: %.100s:",
1841 tos, strerror(errno));
1842 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001843# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10001844 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10001845#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11001846}
Ben Lindstroma7433982002-12-23 02:41:41 +00001847
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001848/* Informs that the current session is interactive. Sets IP flags for that. */
1849
1850void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001851packet_set_interactive(int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001852{
Darren Tucker7b935c72009-06-21 18:59:36 +10001853 if (active_state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001854 return;
Darren Tucker7b935c72009-06-21 18:59:36 +10001855 active_state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001856
Damien Miller95def091999-11-25 00:26:21 +11001857 /* Record that we are in interactive mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001858 active_state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001859
Damien Miller34132e52000-01-14 15:45:46 +11001860 /* Only set socket options if using a socket. */
1861 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001862 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001863 set_nodelay(active_state->connection_in);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001864 packet_set_tos(interactive ? qos_interactive : qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001865}
1866
1867/* Returns true if the current connection is interactive. */
1868
1869int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001870packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001871{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001872 return active_state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001873}
Damien Miller6162d121999-11-21 13:23:52 +11001874
Darren Tucker1f8311c2004-05-13 16:39:33 +10001875int
Darren Tucker502d3842003-06-28 12:38:01 +10001876packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001877{
Darren Tucker7b935c72009-06-21 18:59:36 +10001878 if (active_state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10001879 logit("packet_set_maxsize: called twice: old %d new %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001880 active_state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001881 return -1;
1882 }
1883 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001884 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001885 return -1;
1886 }
Darren Tucker7b935c72009-06-21 18:59:36 +10001887 active_state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001888 debug("packet_set_maxsize: setting to %d", s);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001889 active_state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11001890 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001891}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001892
Darren Tuckerf7288d72009-06-21 18:12:20 +10001893int
1894packet_inc_alive_timeouts(void)
1895{
1896 return ++active_state->keep_alive_timeouts;
1897}
1898
1899void
1900packet_set_alive_timeouts(int ka)
1901{
1902 active_state->keep_alive_timeouts = ka;
1903}
1904
1905u_int
1906packet_get_maxsize(void)
1907{
1908 return active_state->max_packet_size;
1909}
1910
Damien Miller9f643902001-11-12 11:02:52 +11001911/* roundup current message to pad bytes */
1912void
1913packet_add_padding(u_char pad)
1914{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001915 active_state->extra_pad = pad;
Damien Miller9f643902001-11-12 11:02:52 +11001916}
1917
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001918/*
1919 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001920 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001921 * byte SSH_MSG_IGNORE
1922 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001923 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001924 * All implementations MUST understand (and ignore) this message at any
1925 * time (after receiving the protocol version). No implementation is
1926 * required to send them. This message can be used as an additional
1927 * protection measure against advanced traffic analysis techniques.
1928 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001929void
1930packet_send_ignore(int nbytes)
1931{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001932 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001933 int i;
1934
1935 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001936 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001937 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001938 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001939 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001940 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001941 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001942 }
1943}
Damien Millera5539d22003-04-09 20:50:06 +10001944
Darren Tucker1f8311c2004-05-13 16:39:33 +10001945#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001946int
1947packet_need_rekeying(void)
1948{
1949 if (datafellows & SSH_BUG_NOREKEY)
1950 return 0;
1951 return
Darren Tuckerf7288d72009-06-21 18:12:20 +10001952 (active_state->p_send.packets > MAX_PACKETS) ||
1953 (active_state->p_read.packets > MAX_PACKETS) ||
1954 (active_state->max_blocks_out &&
1955 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1956 (active_state->max_blocks_in &&
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001957 (active_state->p_read.blocks > active_state->max_blocks_in)) ||
1958 (active_state->rekey_interval != 0 && active_state->rekey_time +
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001959 active_state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10001960}
1961
1962void
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001963packet_set_rekey_limits(u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10001964{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001965 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
1966 (int)seconds);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001967 active_state->rekey_limit = bytes;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001968 active_state->rekey_interval = seconds;
1969 /*
1970 * We set the time here so that in post-auth privsep slave we count
1971 * from the completion of the authentication.
1972 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001973 active_state->rekey_time = monotime();
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001974}
1975
1976time_t
1977packet_get_rekey_timeout(void)
1978{
1979 time_t seconds;
1980
1981 seconds = active_state->rekey_time + active_state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001982 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001983 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10001984}
Damien Miller9786e6e2005-07-26 21:54:56 +10001985
1986void
1987packet_set_server(void)
1988{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001989 active_state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001990}
1991
1992void
1993packet_set_authenticated(void)
1994{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001995 active_state->after_authentication = 1;
1996}
1997
1998void *
1999packet_get_input(void)
2000{
2001 return (void *)&active_state->input;
2002}
2003
2004void *
2005packet_get_output(void)
2006{
2007 return (void *)&active_state->output;
2008}
2009
2010void *
2011packet_get_newkeys(int mode)
2012{
2013 return (void *)active_state->newkeys[mode];
Damien Miller9786e6e2005-07-26 21:54:56 +10002014}
Darren Tuckere841eb02009-07-06 07:11:13 +10002015
2016/*
2017 * Save the state for the real connection, and use a separate state when
2018 * resuming a suspended connection.
2019 */
2020void
2021packet_backup_state(void)
2022{
2023 struct session_state *tmp;
2024
2025 close(active_state->connection_in);
2026 active_state->connection_in = -1;
2027 close(active_state->connection_out);
2028 active_state->connection_out = -1;
2029 if (backup_state)
2030 tmp = backup_state;
2031 else
2032 tmp = alloc_session_state();
2033 backup_state = active_state;
2034 active_state = tmp;
2035}
2036
2037/*
2038 * Swap in the old state when resuming a connecion.
2039 */
2040void
2041packet_restore_state(void)
2042{
2043 struct session_state *tmp;
2044 void *buf;
2045 u_int len;
2046
2047 tmp = backup_state;
2048 backup_state = active_state;
2049 active_state = tmp;
2050 active_state->connection_in = backup_state->connection_in;
2051 backup_state->connection_in = -1;
2052 active_state->connection_out = backup_state->connection_out;
2053 backup_state->connection_out = -1;
2054 len = buffer_len(&backup_state->input);
2055 if (len > 0) {
2056 buf = buffer_ptr(&backup_state->input);
2057 buffer_append(&active_state->input, buf, len);
2058 buffer_clear(&backup_state->input);
2059 add_recv_bytes(len);
2060 }
2061}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002062
2063/* Reset after_authentication and reset compression in post-auth privsep */
2064void
2065packet_set_postauth(void)
2066{
2067 Comp *comp;
2068 int mode;
2069
2070 debug("%s: called", __func__);
2071 /* This was set in net child, but is not visible in user child */
2072 active_state->after_authentication = 1;
2073 active_state->rekeying = 0;
2074 for (mode = 0; mode < MODE_MAX; mode++) {
2075 if (active_state->newkeys[mode] == NULL)
2076 continue;
2077 comp = &active_state->newkeys[mode]->comp;
2078 if (comp && comp->enabled)
2079 packet_init_compression();
2080 }
2081}