blob: cf9d3648b1d88e1f7a9ef9717f0ece2d53573cfa [file] [log] [blame]
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +00001/* $OpenBSD: packet.c,v 1.202 2015/01/19 20:30:23 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
markus@openbsd.org091c3022015-01-19 19:52:16 +000063#include <zlib.h>
64
65#include "buffer.h" /* typedefs XXX */
66#include "key.h" /* typedefs XXX */
67
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "deattack.h"
Damien Miller33b13562000-04-04 14:38:59 +100071#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100073#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110074#include "cipher.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000075#include "sshkey.h"
Damien Miller33b13562000-04-04 14:38:59 +100076#include "kex.h"
markus@openbsd.org128343b2015-01-13 19:31:40 +000077#include "digest.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000078#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000079#include "log.h"
80#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110081#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100082#include "channels.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000083#include "ssh.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000084#include "packet.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100085#include "roaming.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000086#include "ssherr.h"
87#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100088
89#ifdef PACKET_DEBUG
90#define DBG(x) x
91#else
92#define DBG(x)
93#endif
94
Damien Miller13ae44c2009-01-28 16:38:41 +110095#define PACKET_MAX_SIZE (256 * 1024)
96
Darren Tuckerf7288d72009-06-21 18:12:20 +100097struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100098 u_int32_t seqnr;
99 u_int32_t packets;
100 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000101 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000102};
Damien Miller13ae44c2009-01-28 16:38:41 +1100103
Damien Millera5539d22003-04-09 20:50:06 +1000104struct packet {
105 TAILQ_ENTRY(packet) next;
106 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000107 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000108};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000109
110struct session_state {
111 /*
112 * This variable contains the file descriptors used for
113 * communicating with the other side. connection_in is used for
114 * reading; connection_out for writing. These can be the same
115 * descriptor, in which case it is assumed to be a socket.
116 */
117 int connection_in;
118 int connection_out;
119
120 /* Protocol flags for the remote side. */
121 u_int remote_protocol_flags;
122
123 /* Encryption context for receiving data. Only used for decryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000124 struct sshcipher_ctx receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000125
126 /* Encryption context for sending data. Only used for encryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000127 struct sshcipher_ctx send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000128
129 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000130 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000131
132 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000133 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000134
135 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000136 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000137
138 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000139 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000140
141 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000142 struct sshbuf *compression_buffer;
143
144 /* Incoming/outgoing compression dictionaries */
145 z_stream compression_in_stream;
146 z_stream compression_out_stream;
147 int compression_in_started;
148 int compression_out_started;
149 int compression_in_failures;
150 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000151
152 /*
153 * Flag indicating whether packet compression/decompression is
154 * enabled.
155 */
156 int packet_compression;
157
158 /* default maximum packet size */
159 u_int max_packet_size;
160
161 /* Flag indicating whether this module has been initialized. */
162 int initialized;
163
164 /* Set to true if the connection is interactive. */
165 int interactive_mode;
166
167 /* Set to true if we are the server side. */
168 int server_side;
169
170 /* Set to true if we are authenticated. */
171 int after_authentication;
172
173 int keep_alive_timeouts;
174
175 /* The maximum time that we will wait to send or receive a packet */
176 int packet_timeout_ms;
177
178 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000179 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000180 struct packet_state p_read, p_send;
181
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000182 /* Volume-based rekeying */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000183 u_int64_t max_blocks_in, max_blocks_out;
184 u_int32_t rekey_limit;
185
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000186 /* Time-based rekeying */
187 time_t rekey_interval; /* how often in seconds */
188 time_t rekey_time; /* time of last rekeying */
189
Darren Tuckerf7288d72009-06-21 18:12:20 +1000190 /* Session key for protocol v1 */
191 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
192 u_int ssh1_keylen;
193
194 /* roundup current message to extra_pad bytes */
195 u_char extra_pad;
196
197 /* XXX discard incoming data after MAC error */
198 u_int packet_discard;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000199 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000200
201 /* Used in packet_read_poll2() */
202 u_int packlen;
203
Darren Tucker7b935c72009-06-21 18:59:36 +1000204 /* Used in packet_send2 */
205 int rekeying;
206
207 /* Used in packet_set_interactive */
208 int set_interactive_called;
209
210 /* Used in packet_set_maxsize */
211 int set_maxsize_called;
212
markus@openbsd.org091c3022015-01-19 19:52:16 +0000213 /* One-off warning about weak ciphers */
214 int cipher_warning_done;
215
216 /* SSH1 CRC compensation attack detector */
217 struct deattack_ctx deattack;
218
Darren Tuckerf7288d72009-06-21 18:12:20 +1000219 TAILQ_HEAD(, packet) outgoing;
220};
221
markus@openbsd.org091c3022015-01-19 19:52:16 +0000222struct ssh *
223ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000224{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000225 struct ssh *ssh = NULL;
226 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000227
markus@openbsd.org091c3022015-01-19 19:52:16 +0000228 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
229 (state = calloc(1, sizeof(*state))) == NULL ||
230 (state->input = sshbuf_new()) == NULL ||
231 (state->output = sshbuf_new()) == NULL ||
232 (state->outgoing_packet = sshbuf_new()) == NULL ||
233 (state->incoming_packet = sshbuf_new()) == NULL)
234 goto fail;
235 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000236 TAILQ_INIT(&ssh->private_keys);
237 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000238 state->connection_in = -1;
239 state->connection_out = -1;
240 state->max_packet_size = 32768;
241 state->packet_timeout_ms = -1;
242 state->p_send.packets = state->p_read.packets = 0;
243 state->initialized = 1;
244 /*
245 * ssh_packet_send2() needs to queue packets until
246 * we've done the initial key exchange.
247 */
248 state->rekeying = 1;
249 ssh->state = state;
250 return ssh;
251 fail:
252 if (state) {
253 sshbuf_free(state->input);
254 sshbuf_free(state->output);
255 sshbuf_free(state->incoming_packet);
256 sshbuf_free(state->outgoing_packet);
257 free(state);
258 }
259 free(ssh);
260 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000261}
Damien Millera5539d22003-04-09 20:50:06 +1000262
Damien Miller5428f641999-11-25 11:54:57 +1100263/*
264 * Sets the descriptors used for communication. Disables encryption until
265 * packet_set_encryption_key is called.
266 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000267struct ssh *
268ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000270 struct session_state *state;
271 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000272 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000273
Damien Miller874d77b2000-10-14 16:23:11 +1100274 if (none == NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000275 fatal("%s: cannot load cipher 'none'", __func__);
276 if (ssh == NULL)
277 ssh = ssh_alloc_session_state();
278 if (ssh == NULL)
279 fatal("%s: cound not allocate state", __func__);
280 state = ssh->state;
281 state->connection_in = fd_in;
282 state->connection_out = fd_out;
283 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000284 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000285 (r = cipher_init(&state->receive_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000286 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000287 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
288 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
289 deattack_init(&state->deattack);
290 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291}
292
Darren Tucker3fc464e2008-06-13 06:42:45 +1000293void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000294ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000295{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000296 struct session_state *state = ssh->state;
297
Damien Miller8ed4de82011-12-19 10:52:50 +1100298 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000299 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000300 return;
301 }
302 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000303 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000304 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000305 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000306}
307
markus@openbsd.org091c3022015-01-19 19:52:16 +0000308int
309ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100310{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000311 struct session_state *state = ssh->state;
312 int r;
313
314 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100315 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000316
Damien Miller13ae44c2009-01-28 16:38:41 +1100317 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000318 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000319 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000320 if ((r = sshbuf_put(state->incoming_packet, buf,
321 sizeof(buf))) != 0)
322 return r;
323 (void) mac_compute(state->packet_discard_mac,
324 state->p_read.seqnr,
325 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
326 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100327 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000328 logit("Finished discarding for %.200s", ssh_remote_ipaddr(ssh));
329 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100330}
331
markus@openbsd.org091c3022015-01-19 19:52:16 +0000332static int
333ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
334 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100335{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000336 struct session_state *state = ssh->state;
337 int r;
338
339 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
340 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
341 return r;
342 return SSH_ERR_MAC_INVALID;
343 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100344 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000345 state->packet_discard_mac = mac;
346 if (sshbuf_len(state->input) >= discard &&
347 (r = ssh_packet_stop_discard(ssh)) != 0)
348 return r;
349 state->packet_discard = discard - sshbuf_len(state->input);
350 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100351}
352
Damien Miller34132e52000-01-14 15:45:46 +1100353/* Returns 1 if remote host is connected via socket, 0 if not. */
354
355int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000356ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100357{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000358 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100359 struct sockaddr_storage from, to;
360 socklen_t fromlen, tolen;
361
362 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000363 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100364 return 1;
365 fromlen = sizeof(from);
366 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000367 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000368 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100369 return 0;
370 tolen = sizeof(to);
371 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000372 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000373 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100374 return 0;
375 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
376 return 0;
377 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
378 return 0;
379 return 1;
380}
381
Ben Lindstromf6027d32002-03-22 01:42:04 +0000382void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000383ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000384{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000385 if (ibytes)
386 *ibytes = ssh->state->p_read.bytes;
387 if (obytes)
388 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000389}
390
391int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000392ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100393{
394 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100395 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100396
397 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000398 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000399 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100400 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000401#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100402 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000403 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000404 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000405#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000406 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100407}
408
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409/* Sets the connection into non-blocking mode. */
410
411void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000412ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413{
Damien Miller95def091999-11-25 00:26:21 +1100414 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000415 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
markus@openbsd.org091c3022015-01-19 19:52:16 +0000417 if (ssh->state->connection_out != ssh->state->connection_in)
418 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419}
420
421/* Returns the socket used for reading. */
422
423int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000424ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000426 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427}
428
429/* Returns the descriptor used for writing. */
430
431int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000432ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000434 return ssh->state->connection_out;
435}
436
437/*
438 * Returns the IP-address of the remote host as a string. The returned
439 * string must not be freed.
440 */
441
442const char *
443ssh_remote_ipaddr(struct ssh *ssh)
444{
445 /* Check whether we have cached the ipaddr. */
446 if (ssh->remote_ipaddr == NULL)
447 ssh->remote_ipaddr = ssh_packet_connection_is_on_socket(ssh) ?
448 get_peer_ipaddr(ssh->state->connection_in) :
449 strdup("UNKNOWN");
450 if (ssh->remote_ipaddr == NULL)
451 return "UNKNOWN";
452 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453}
454
455/* Closes the connection and clears and frees internal data structures. */
456
457void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000458ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000460 struct session_state *state = ssh->state;
461 int r;
462 u_int mode;
463
464 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100465 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000466 state->initialized = 0;
467 if (state->connection_in == state->connection_out) {
468 shutdown(state->connection_out, SHUT_RDWR);
469 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100470 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000471 close(state->connection_in);
472 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100473 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000474 sshbuf_free(state->input);
475 sshbuf_free(state->output);
476 sshbuf_free(state->outgoing_packet);
477 sshbuf_free(state->incoming_packet);
478 for (mode = 0; mode < MODE_MAX; mode++)
479 kex_free_newkeys(state->newkeys[mode]);
480 if (state->compression_buffer) {
481 sshbuf_free(state->compression_buffer);
482 if (state->compression_out_started) {
483 z_streamp stream = &state->compression_out_stream;
484 debug("compress outgoing: "
485 "raw data %llu, compressed %llu, factor %.2f",
486 (unsigned long long)stream->total_in,
487 (unsigned long long)stream->total_out,
488 stream->total_in == 0 ? 0.0 :
489 (double) stream->total_out / stream->total_in);
490 if (state->compression_out_failures == 0)
491 deflateEnd(stream);
492 }
493 if (state->compression_in_started) {
494 z_streamp stream = &state->compression_out_stream;
495 debug("compress incoming: "
496 "raw data %llu, compressed %llu, factor %.2f",
497 (unsigned long long)stream->total_out,
498 (unsigned long long)stream->total_in,
499 stream->total_out == 0 ? 0.0 :
500 (double) stream->total_in / stream->total_out);
501 if (state->compression_in_failures == 0)
502 inflateEnd(stream);
503 }
Damien Miller95def091999-11-25 00:26:21 +1100504 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000505 if ((r = cipher_cleanup(&state->send_context)) != 0)
506 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
507 if ((r = cipher_cleanup(&state->receive_context)) != 0)
508 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
509 if (ssh->remote_ipaddr) {
510 free(ssh->remote_ipaddr);
511 ssh->remote_ipaddr = NULL;
512 }
513 free(ssh->state);
514 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515}
516
517/* Sets remote side protocol flags. */
518
519void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000520ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000522 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523}
524
525/* Returns the remote protocol flags set earlier by the above function. */
526
Ben Lindstrom46c16222000-12-22 01:43:59 +0000527u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000528ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000530 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531}
532
Damien Miller5428f641999-11-25 11:54:57 +1100533/*
534 * Starts packet compression from the next packet on in both directions.
535 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
536 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537
markus@openbsd.org091c3022015-01-19 19:52:16 +0000538static int
539ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000540{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000541 if (!ssh->state->compression_buffer &&
542 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
543 return SSH_ERR_ALLOC_FAIL;
544 return 0;
545}
546
547static int
548start_compression_out(struct ssh *ssh, int level)
549{
550 if (level < 1 || level > 9)
551 return SSH_ERR_INVALID_ARGUMENT;
552 debug("Enabling compression at level %d.", level);
553 if (ssh->state->compression_out_started == 1)
554 deflateEnd(&ssh->state->compression_out_stream);
555 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
556 case Z_OK:
557 ssh->state->compression_out_started = 1;
558 break;
559 case Z_MEM_ERROR:
560 return SSH_ERR_ALLOC_FAIL;
561 default:
562 return SSH_ERR_INTERNAL_ERROR;
563 }
564 return 0;
565}
566
567static int
568start_compression_in(struct ssh *ssh)
569{
570 if (ssh->state->compression_in_started == 1)
571 inflateEnd(&ssh->state->compression_in_stream);
572 switch (inflateInit(&ssh->state->compression_in_stream)) {
573 case Z_OK:
574 ssh->state->compression_in_started = 1;
575 break;
576 case Z_MEM_ERROR:
577 return SSH_ERR_ALLOC_FAIL;
578 default:
579 return SSH_ERR_INTERNAL_ERROR;
580 }
581 return 0;
582}
583
584int
585ssh_packet_start_compression(struct ssh *ssh, int level)
586{
587 int r;
588
589 if (ssh->state->packet_compression && !compat20)
590 return SSH_ERR_INTERNAL_ERROR;
591 ssh->state->packet_compression = 1;
592 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
593 (r = start_compression_in(ssh)) != 0 ||
594 (r = start_compression_out(ssh, level)) != 0)
595 return r;
596 return 0;
597}
598
599/* XXX remove need for separate compression buffer */
600static int
601compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
602{
603 u_char buf[4096];
604 int r, status;
605
606 if (ssh->state->compression_out_started != 1)
607 return SSH_ERR_INTERNAL_ERROR;
608
609 /* This case is not handled below. */
610 if (sshbuf_len(in) == 0)
611 return 0;
612
613 /* Input is the contents of the input buffer. */
614 if ((ssh->state->compression_out_stream.next_in =
615 sshbuf_mutable_ptr(in)) == NULL)
616 return SSH_ERR_INTERNAL_ERROR;
617 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
618
619 /* Loop compressing until deflate() returns with avail_out != 0. */
620 do {
621 /* Set up fixed-size output buffer. */
622 ssh->state->compression_out_stream.next_out = buf;
623 ssh->state->compression_out_stream.avail_out = sizeof(buf);
624
625 /* Compress as much data into the buffer as possible. */
626 status = deflate(&ssh->state->compression_out_stream,
627 Z_PARTIAL_FLUSH);
628 switch (status) {
629 case Z_MEM_ERROR:
630 return SSH_ERR_ALLOC_FAIL;
631 case Z_OK:
632 /* Append compressed data to output_buffer. */
633 if ((r = sshbuf_put(out, buf, sizeof(buf) -
634 ssh->state->compression_out_stream.avail_out)) != 0)
635 return r;
636 break;
637 case Z_STREAM_ERROR:
638 default:
639 ssh->state->compression_out_failures++;
640 return SSH_ERR_INVALID_FORMAT;
641 }
642 } while (ssh->state->compression_out_stream.avail_out == 0);
643 return 0;
644}
645
646static int
647uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
648{
649 u_char buf[4096];
650 int r, status;
651
652 if (ssh->state->compression_in_started != 1)
653 return SSH_ERR_INTERNAL_ERROR;
654
655 if ((ssh->state->compression_in_stream.next_in =
656 sshbuf_mutable_ptr(in)) == NULL)
657 return SSH_ERR_INTERNAL_ERROR;
658 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
659
660 for (;;) {
661 /* Set up fixed-size output buffer. */
662 ssh->state->compression_in_stream.next_out = buf;
663 ssh->state->compression_in_stream.avail_out = sizeof(buf);
664
665 status = inflate(&ssh->state->compression_in_stream,
666 Z_PARTIAL_FLUSH);
667 switch (status) {
668 case Z_OK:
669 if ((r = sshbuf_put(out, buf, sizeof(buf) -
670 ssh->state->compression_in_stream.avail_out)) != 0)
671 return r;
672 break;
673 case Z_BUF_ERROR:
674 /*
675 * Comments in zlib.h say that we should keep calling
676 * inflate() until we get an error. This appears to
677 * be the error that we get.
678 */
679 return 0;
680 case Z_DATA_ERROR:
681 return SSH_ERR_INVALID_FORMAT;
682 case Z_MEM_ERROR:
683 return SSH_ERR_ALLOC_FAIL;
684 case Z_STREAM_ERROR:
685 default:
686 ssh->state->compression_in_failures++;
687 return SSH_ERR_INTERNAL_ERROR;
688 }
689 }
690 /* NOTREACHED */
691}
692
693/* Serialise compression state into a blob for privsep */
694static int
695ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
696{
697 struct session_state *state = ssh->state;
698 struct sshbuf *b;
699 int r;
700
701 if ((b = sshbuf_new()) == NULL)
702 return SSH_ERR_ALLOC_FAIL;
703 if (state->compression_in_started) {
704 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
705 sizeof(state->compression_in_stream))) != 0)
706 goto out;
707 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
708 goto out;
709 if (state->compression_out_started) {
710 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
711 sizeof(state->compression_out_stream))) != 0)
712 goto out;
713 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
714 goto out;
715 r = sshbuf_put_stringb(m, b);
716 out:
717 sshbuf_free(b);
718 return r;
719}
720
721/* Deserialise compression state from a blob for privsep */
722static int
723ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
724{
725 struct session_state *state = ssh->state;
726 struct sshbuf *b = NULL;
727 int r;
728 const u_char *inblob, *outblob;
729 size_t inl, outl;
730
731 if ((r = sshbuf_froms(m, &b)) != 0)
732 goto out;
733 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
734 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
735 goto out;
736 if (inl == 0)
737 state->compression_in_started = 0;
738 else if (inl != sizeof(state->compression_in_stream)) {
739 r = SSH_ERR_INTERNAL_ERROR;
740 goto out;
741 } else {
742 state->compression_in_started = 1;
743 memcpy(&state->compression_in_stream, inblob, inl);
744 }
745 if (outl == 0)
746 state->compression_out_started = 0;
747 else if (outl != sizeof(state->compression_out_stream)) {
748 r = SSH_ERR_INTERNAL_ERROR;
749 goto out;
750 } else {
751 state->compression_out_started = 1;
752 memcpy(&state->compression_out_stream, outblob, outl);
753 }
754 r = 0;
755 out:
756 sshbuf_free(b);
757 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000758}
759
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000760void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000761ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
762 void *(*allocfunc)(void *, u_int, u_int),
763 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000764{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000765 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
766 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
767 ssh->state->compression_out_stream.opaque = ctx;
768 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
769 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
770 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771}
772
Damien Miller5428f641999-11-25 11:54:57 +1100773/*
Damien Miller5428f641999-11-25 11:54:57 +1100774 * Causes any further packets to be encrypted using the given key. The same
775 * key is used for both sending and reception. However, both directions are
776 * encrypted independently of each other.
777 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000778
Damien Miller1f0311c2014-05-15 14:24:09 +1000779#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000781ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000783 struct session_state *state = ssh->state;
784 const struct sshcipher *cipher = cipher_by_number(number);
785 int r;
786 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100787
markus@openbsd.org091c3022015-01-19 19:52:16 +0000788 if (cipher == NULL)
789 fatal("%s: unknown cipher number %d", __func__, number);
790 if (keylen < 20)
791 fatal("%s: keylen too small: %d", __func__, keylen);
792 if (keylen > SSH_SESSION_KEY_LENGTH)
793 fatal("%s: keylen too big: %d", __func__, keylen);
794 memcpy(state->ssh1_key, key, keylen);
795 state->ssh1_keylen = keylen;
796 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
797 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
798 (r = cipher_init(&state->receive_context, cipher, key, keylen,
799 NULL, 0, CIPHER_DECRYPT) != 0))
800 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
801 if (!state->cipher_warning_done &&
802 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
803 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
804 error("Warning: %s", wmsg);
805 state->cipher_warning_done = 1;
806 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000807}
Damien Miller6af914a2010-09-10 11:39:26 +1000808#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000809
Damien Miller5428f641999-11-25 11:54:57 +1100810/*
811 * Finalizes and sends the packet. If the encryption key has been set,
812 * encrypts the packet before sending.
813 */
Damien Miller95def091999-11-25 00:26:21 +1100814
markus@openbsd.org091c3022015-01-19 19:52:16 +0000815int
816ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000818 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000819 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000820 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000821 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822
Damien Miller5428f641999-11-25 11:54:57 +1100823 /*
824 * If using packet compression, compress the payload of the outgoing
825 * packet.
826 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000827 if (state->packet_compression) {
828 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100829 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000830 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
831 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100832 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000833 if ((r = sshbuf_put(state->compression_buffer,
834 "\0\0\0\0\0\0\0\0", 8)) != 0)
835 goto out;
836 if ((r = compress_buffer(ssh, state->outgoing_packet,
837 state->compression_buffer)) != 0)
838 goto out;
839 sshbuf_reset(state->outgoing_packet);
840 if ((r = sshbuf_putb(state->outgoing_packet,
841 state->compression_buffer)) != 0)
842 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100843 }
844 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000845 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846
Damien Millere247cc42000-05-07 12:03:14 +1000847 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100848 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000849 if (!state->send_context.plaintext) {
850 cp = sshbuf_mutable_ptr(state->outgoing_packet);
851 if (cp == NULL) {
852 r = SSH_ERR_INTERNAL_ERROR;
853 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100854 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000855 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100856 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000857 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
858 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100859
860 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000861 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
862 sshbuf_len(state->outgoing_packet));
863 POKE_U32(buf, checksum);
864 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
865 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866
867#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100868 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000869 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870#endif
871
Damien Miller95def091999-11-25 00:26:21 +1100872 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000873 POKE_U32(buf, len);
874 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
875 goto out;
876 if ((r = sshbuf_reserve(state->output,
877 sshbuf_len(state->outgoing_packet), &cp)) != 0)
878 goto out;
879 if ((r = cipher_crypt(&state->send_context, 0, cp,
880 sshbuf_ptr(state->outgoing_packet),
881 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
882 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100883
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100885 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000886 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000888 state->p_send.packets++;
889 state->p_send.bytes += len +
890 sshbuf_len(state->outgoing_packet);
891 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller5428f641999-11-25 11:54:57 +1100893 /*
Damien Miller788f2122005-11-05 15:14:59 +1100894 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100895 * actually sent until packet_write_wait or packet_write_poll is
896 * called.
897 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000898 r = 0;
899 out:
900 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901}
902
markus@openbsd.org091c3022015-01-19 19:52:16 +0000903int
904ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000905{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000906 struct session_state *state = ssh->state;
907 struct sshenc *enc;
908 struct sshmac *mac;
909 struct sshcomp *comp;
910 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000911 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000912 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000913 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000914
Ben Lindstrom064496f2002-12-23 02:04:22 +0000915 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000916
Damien Miller963f6b22002-02-19 15:21:23 +1100917 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000918 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000919 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000920 state->p_send.packets = state->p_send.blocks = 0;
921 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100922 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000923 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000924 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000925 state->p_read.packets = state->p_read.blocks = 0;
926 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100927 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000928 if (state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000929 debug("set_newkeys: rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000930 if ((r = cipher_cleanup(cc)) != 0)
931 return r;
932 enc = &state->newkeys[mode]->enc;
933 mac = &state->newkeys[mode]->mac;
934 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000935 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100936 explicit_bzero(enc->iv, enc->iv_len);
937 explicit_bzero(enc->key, enc->key_len);
938 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000939 free(enc->name);
940 free(enc->iv);
941 free(enc->key);
942 free(mac->name);
943 free(mac->key);
944 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000945 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000946 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000947 /* move newkeys from kex to state */
948 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
949 return SSH_ERR_INTERNAL_ERROR;
950 ssh->kex->newkeys[mode] = NULL;
951 enc = &state->newkeys[mode]->enc;
952 mac = &state->newkeys[mode]->mac;
953 comp = &state->newkeys[mode]->comp;
954 if (cipher_authlen(enc->cipher) == 0) {
955 if ((r = mac_init(mac)) != 0)
956 return r;
957 }
958 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000959 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000960 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
961 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000962 return r;
963 if (!state->cipher_warning_done &&
964 (wmsg = cipher_warning_message(cc)) != NULL) {
965 error("Warning: %s", wmsg);
966 state->cipher_warning_done = 1;
967 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000968 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100969 /* explicit_bzero(enc->iv, enc->block_size);
970 explicit_bzero(enc->key, enc->key_len);
971 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000972 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000973 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +0000974 state->after_authentication)) && comp->enabled == 0) {
975 if ((r = ssh_packet_init_compression(ssh)) < 0)
976 return r;
977 if (mode == MODE_OUT) {
978 if ((r = start_compression_out(ssh, 6)) != 0)
979 return r;
980 } else {
981 if ((r = start_compression_in(ssh)) != 0)
982 return r;
983 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000984 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000985 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000986 /*
987 * The 2^(blocksize*2) limit is too expensive for 3DES,
988 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
989 */
990 if (enc->block_size >= 16)
991 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
992 else
993 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000994 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000995 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +0000996 state->rekey_limit / enc->block_size);
997 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000998}
999
Damien Miller5428f641999-11-25 11:54:57 +11001000/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001001 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001002 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001003 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1004 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001005static int
1006ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001007{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001008 struct session_state *state = ssh->state;
1009 struct sshcomp *comp = NULL;
1010 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001011
1012 /*
1013 * Remember that we are past the authentication step, so rekeying
1014 * with COMP_DELAYED will turn on compression immediately.
1015 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001016 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001017 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001018 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001019 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001020 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001021 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001022 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001023 if ((r = ssh_packet_init_compression(ssh)) != 0)
1024 return r;
1025 if (mode == MODE_OUT) {
1026 if ((r = start_compression_out(ssh, 6)) != 0)
1027 return r;
1028 } else {
1029 if ((r = start_compression_in(ssh)) != 0)
1030 return r;
1031 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001032 comp->enabled = 1;
1033 }
1034 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001035 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001036}
1037
1038/*
Damien Miller33b13562000-04-04 14:38:59 +10001039 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1040 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001041int
1042ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001043{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001044 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001045 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001046 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001047 u_int authlen = 0, aadlen = 0;
1048 u_int len;
1049 struct sshenc *enc = NULL;
1050 struct sshmac *mac = NULL;
1051 struct sshcomp *comp = NULL;
1052 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001053
markus@openbsd.org091c3022015-01-19 19:52:16 +00001054 if (state->newkeys[MODE_OUT] != NULL) {
1055 enc = &state->newkeys[MODE_OUT]->enc;
1056 mac = &state->newkeys[MODE_OUT]->mac;
1057 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001058 /* disable mac for authenticated encryption */
1059 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1060 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001061 }
Damien Miller963f6b22002-02-19 15:21:23 +11001062 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001063 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001064
markus@openbsd.org091c3022015-01-19 19:52:16 +00001065 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001066
1067#ifdef PACKET_DEBUG
1068 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001069 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001070#endif
1071
1072 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001073 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001074 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001075 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1076 goto out;
1077 sshbuf_reset(state->compression_buffer);
1078 if ((r = compress_buffer(ssh, state->outgoing_packet,
1079 state->compression_buffer)) != 0)
1080 goto out;
1081 sshbuf_reset(state->outgoing_packet);
1082 if ((r = sshbuf_put(state->outgoing_packet,
1083 "\0\0\0\0\0", 5)) != 0 ||
1084 (r = sshbuf_putb(state->outgoing_packet,
1085 state->compression_buffer)) != 0)
1086 goto out;
1087 DBG(debug("compression: raw %d compressed %zd", len,
1088 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001089 }
1090
1091 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001092 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001093
1094 /*
1095 * calc size of padding, alloc space, get random data,
1096 * minimum padding is 4 bytes
1097 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001098 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001099 padlen = block_size - (len % block_size);
1100 if (padlen < 4)
1101 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001102 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001103 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001104 state->extra_pad =
1105 roundup(state->extra_pad, block_size);
1106 pad = state->extra_pad -
1107 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001108 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001109 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001110 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001111 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001112 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001113 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1114 goto out;
1115 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001116 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001117 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001118 } else {
1119 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001120 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001121 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001122 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001123 len = sshbuf_len(state->outgoing_packet);
1124 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1125 if (cp == NULL) {
1126 r = SSH_ERR_INTERNAL_ERROR;
1127 goto out;
1128 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001129 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001130 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001131 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001132 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1133 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001134
1135 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001136 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001137 if ((r = mac_compute(mac, state->p_send.seqnr,
1138 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001139 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001140 goto out;
1141 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001142 }
1143 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001144 if ((r = sshbuf_reserve(state->output,
1145 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1146 goto out;
1147 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1148 sshbuf_ptr(state->outgoing_packet),
1149 len - aadlen, aadlen, authlen)) != 0)
1150 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001151 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001152 if (mac && mac->enabled) {
1153 if (mac->etm) {
1154 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001155 if ((r = mac_compute(mac, state->p_send.seqnr,
1156 cp, len, macbuf, sizeof(macbuf))) != 0)
1157 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001158 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001159 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001160 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001161 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1162 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001163 }
Damien Miller33b13562000-04-04 14:38:59 +10001164#ifdef PACKET_DEBUG
1165 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001166 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001167#endif
Damien Miller4af51302000-04-16 11:18:38 +10001168 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001169 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001170 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001171 if (++state->p_send.packets == 0)
1172 if (!(ssh->compat & SSH_BUG_NOREKEY))
1173 return SSH_ERR_NEED_REKEY;
1174 state->p_send.blocks += len / block_size;
1175 state->p_send.bytes += len;
1176 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001177
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001178 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001179 r = ssh_set_newkeys(ssh, MODE_OUT);
1180 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1181 r = ssh_packet_enable_delayed_compress(ssh);
1182 else
1183 r = 0;
1184 out:
1185 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001186}
1187
markus@openbsd.org091c3022015-01-19 19:52:16 +00001188int
1189ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001190{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001191 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001192 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001193 u_char type;
1194 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001195
markus@openbsd.org091c3022015-01-19 19:52:16 +00001196 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001197
1198 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001199 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001200 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1201 (type > SSH2_MSG_TRANSPORT_MAX) ||
1202 (type == SSH2_MSG_SERVICE_REQUEST) ||
1203 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +10001204 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001205 p = calloc(1, sizeof(*p));
1206 if (p == NULL)
1207 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001208 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001209 p->payload = state->outgoing_packet;
1210 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1211 state->outgoing_packet = sshbuf_new();
1212 if (state->outgoing_packet == NULL)
1213 return SSH_ERR_ALLOC_FAIL;
1214 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001215 }
1216 }
1217
1218 /* rekeying starts with sending KEXINIT */
1219 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001220 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001221
markus@openbsd.org091c3022015-01-19 19:52:16 +00001222 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1223 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001224
1225 /* after a NEWKEYS message we can send the complete queue */
1226 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001227 state->rekeying = 0;
1228 state->rekey_time = monotime();
1229 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001230 type = p->type;
1231 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001232 sshbuf_free(state->outgoing_packet);
1233 state->outgoing_packet = p->payload;
1234 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001235 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001236 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1237 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001238 }
1239 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001240 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001241}
1242
Damien Miller33b13562000-04-04 14:38:59 +10001243/*
Damien Miller5428f641999-11-25 11:54:57 +11001244 * Waits until a packet has been received, and returns its type. Note that
1245 * no other data is processed until this returns, so this function should not
1246 * be used during the interactive session.
1247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248
1249int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001250ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001252 struct session_state *state = ssh->state;
1253 int len, r, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001254 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001255 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001256 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257
Darren Tucker99bb7612008-06-13 22:02:50 +10001258 DBG(debug("packet_read()"));
1259
markus@openbsd.org091c3022015-01-19 19:52:16 +00001260 setp = (fd_set *)calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001261 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001262 if (setp == NULL)
1263 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001264
Damien Miller95def091999-11-25 00:26:21 +11001265 /* Since we are blocking, ensure that all written packets have been sent. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001266 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267
Damien Miller95def091999-11-25 00:26:21 +11001268 /* Stay in the loop until we have received a complete packet. */
1269 for (;;) {
1270 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001271 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1272 if (r != 0)
1273 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001274 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001275 *typep == SSH_SMSG_SUCCESS
1276 || *typep == SSH_SMSG_FAILURE
1277 || *typep == SSH_CMSG_EOF
1278 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1279 if ((r = sshpkt_get_end(ssh)) != 0)
1280 break;
Damien Miller95def091999-11-25 00:26:21 +11001281 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001282 if (*typep != SSH_MSG_NONE)
1283 break;
Damien Miller5428f641999-11-25 11:54:57 +11001284 /*
1285 * Otherwise, wait for some data to arrive, add it to the
1286 * buffer, and try again.
1287 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001288 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001289 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001290 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001291
markus@openbsd.org091c3022015-01-19 19:52:16 +00001292 if (state->packet_timeout_ms > 0) {
1293 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001294 timeoutp = &timeout;
1295 }
Damien Miller95def091999-11-25 00:26:21 +11001296 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001297 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001298 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001299 ms_to_timeval(&timeout, ms_remain);
1300 gettimeofday(&start, NULL);
1301 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001302 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001303 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001304 break;
Damien Millerea437422009-10-02 11:49:03 +10001305 if (errno != EAGAIN && errno != EINTR &&
1306 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001307 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001308 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001309 continue;
1310 ms_subtract_diff(&start, &ms_remain);
1311 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001312 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001313 break;
1314 }
1315 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001316 if (r == 0) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001317 logit("Connection to %.200s timed out while "
markus@openbsd.org091c3022015-01-19 19:52:16 +00001318 "waiting to read", ssh_remote_ipaddr(ssh));
Darren Tucker3fc464e2008-06-13 06:42:45 +10001319 cleanup_exit(255);
1320 }
Damien Miller95def091999-11-25 00:26:21 +11001321 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001322 do {
1323 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001324 len = roaming_read(state->connection_in, buf,
Darren Tuckerc5564e12009-06-21 18:53:53 +10001325 sizeof(buf), &cont);
1326 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001327 if (len == 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001328 logit("Connection closed by %.200s",
1329 ssh_remote_ipaddr(ssh));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001330 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001331 }
Damien Miller95def091999-11-25 00:26:21 +11001332 if (len < 0)
1333 fatal("Read from socket failed: %.100s", strerror(errno));
1334 /* Append it to the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001335 ssh_packet_process_incoming(ssh, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001336 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001337 free(setp);
1338 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001339}
1340
Damien Miller278f9072001-12-21 15:00:19 +11001341int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001342ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001343{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001344 u_char type;
1345 int r;
1346
1347 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1348 fatal("%s: %s", __func__, ssh_err(r));
1349 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001350}
1351
Damien Miller5428f641999-11-25 11:54:57 +11001352/*
1353 * Waits until a packet has been received, verifies that its type matches
1354 * that given, and gives a fatal error and exits if there is a mismatch.
1355 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001356
1357void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001358ssh_packet_read_expect(struct ssh *ssh, int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359{
Damien Miller95def091999-11-25 00:26:21 +11001360 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361
markus@openbsd.org091c3022015-01-19 19:52:16 +00001362 type = ssh_packet_read(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001363 if (type != expected_type)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001364 ssh_packet_disconnect(ssh,
1365 "Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001366 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001367}
1368
1369/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001370 * packet_process_incoming. If so, reads the packet; otherwise returns
1371 * SSH_MSG_NONE. This does not wait for data from the connection.
1372 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001373 * SSH_MSG_DISCONNECT is handled specially here. Also,
1374 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1375 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001376 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001377
markus@openbsd.org091c3022015-01-19 19:52:16 +00001378int
1379ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001380{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001381 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001382 u_int len, padded_len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001383 const u_char *cp;
1384 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001385 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001386 int r;
1387
1388 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389
Damien Miller95def091999-11-25 00:26:21 +11001390 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001391 if (sshbuf_len(state->input) < 4 + 8)
1392 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001393 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001394 len = PEEK_U32(sshbuf_ptr(state->input));
Damien Miller95def091999-11-25 00:26:21 +11001395 if (len < 1 + 2 + 2 || len > 256 * 1024)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001396 ssh_packet_disconnect(ssh, "Bad packet length %u.",
1397 len);
Damien Miller95def091999-11-25 00:26:21 +11001398 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001399
Damien Miller95def091999-11-25 00:26:21 +11001400 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001401 if (sshbuf_len(state->input) < 4 + padded_len)
1402 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
Damien Miller95def091999-11-25 00:26:21 +11001404 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405
Damien Miller95def091999-11-25 00:26:21 +11001406 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001407 if ((r = sshbuf_consume(state->input, 4)) != 0)
1408 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001409
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001410 /*
1411 * Cryptographic attack detector for ssh
1412 * (C)1998 CORE-SDI, Buenos Aires Argentina
1413 * Ariel Futoransky(futo@core-sdi.com)
1414 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001415 if (!state->receive_context.plaintext) {
1416 switch (detect_attack(&state->deattack,
1417 sshbuf_ptr(state->input), padded_len)) {
1418 case DEATTACK_OK:
1419 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001420 case DEATTACK_DETECTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001421 ssh_packet_disconnect(ssh,
1422 "crc32 compensation attack: network attack detected"
1423 );
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001424 case DEATTACK_DOS_DETECTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001425 ssh_packet_disconnect(ssh,
1426 "deattack denial of service detected");
1427 default:
1428 ssh_packet_disconnect(ssh, "deattack error");
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001429 }
1430 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001431
1432 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001433 sshbuf_reset(state->incoming_packet);
1434 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1435 goto out;
1436 if ((r = cipher_crypt(&state->receive_context, 0, p,
1437 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1438 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001439
markus@openbsd.org091c3022015-01-19 19:52:16 +00001440 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1441 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001442
1443#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001444 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001445 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
Damien Miller95def091999-11-25 00:26:21 +11001448 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001449 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1450 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451
Damien Miller95def091999-11-25 00:26:21 +11001452 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001453 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1454 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001455
Damien Miller95def091999-11-25 00:26:21 +11001456 /* Test check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001457 if (len != sshbuf_len(state->incoming_packet))
1458 ssh_packet_disconnect(ssh,
1459 "packet_read_poll1: len %d != sshbuf_len %zd.",
1460 len, sshbuf_len(state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461
markus@openbsd.org091c3022015-01-19 19:52:16 +00001462 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1463 stored_checksum = PEEK_U32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001464 if (checksum != stored_checksum)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001465 ssh_packet_disconnect(ssh,
1466 "Corrupted check bytes on input.");
1467 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1468 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469
markus@openbsd.org091c3022015-01-19 19:52:16 +00001470 if (state->packet_compression) {
1471 sshbuf_reset(state->compression_buffer);
1472 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1473 state->compression_buffer)) != 0)
1474 goto out;
1475 sshbuf_reset(state->incoming_packet);
1476 if ((r = sshbuf_putb(state->incoming_packet,
1477 state->compression_buffer)) != 0)
1478 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001479 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001480 state->p_read.packets++;
1481 state->p_read.bytes += padded_len + 4;
1482 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1483 goto out;
1484 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX)
1485 ssh_packet_disconnect(ssh,
1486 "Invalid ssh1 packet type: %d", *typep);
1487 r = 0;
1488 out:
1489 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490}
Damien Miller95def091999-11-25 00:26:21 +11001491
markus@openbsd.org091c3022015-01-19 19:52:16 +00001492int
1493ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001494{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001495 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001496 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001497 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1498 u_int maclen, aadlen = 0, authlen = 0, block_size;
1499 struct sshenc *enc = NULL;
1500 struct sshmac *mac = NULL;
1501 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001502 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001503
markus@openbsd.org091c3022015-01-19 19:52:16 +00001504 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001505
markus@openbsd.org091c3022015-01-19 19:52:16 +00001506 if (state->packet_discard)
1507 return 0;
1508
1509 if (state->newkeys[MODE_IN] != NULL) {
1510 enc = &state->newkeys[MODE_IN]->enc;
1511 mac = &state->newkeys[MODE_IN]->mac;
1512 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001513 /* disable mac for authenticated encryption */
1514 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1515 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001516 }
1517 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001518 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001519 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001520
markus@openbsd.org091c3022015-01-19 19:52:16 +00001521 if (aadlen && state->packlen == 0) {
1522 if (cipher_get_length(&state->receive_context,
1523 &state->packlen, state->p_read.seqnr,
1524 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1525 return 0;
1526 if (state->packlen < 1 + 4 ||
1527 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001528#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001529 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001530#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001531 logit("Bad packet length %u.", state->packlen);
1532 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1533 return r;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001534 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001535 sshbuf_reset(state->incoming_packet);
1536 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001537 /*
1538 * check if input size is less than the cipher block size,
1539 * decrypt first block and extract length of incoming packet
1540 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001541 if (sshbuf_len(state->input) < block_size)
1542 return 0;
1543 sshbuf_reset(state->incoming_packet);
1544 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1545 &cp)) != 0)
1546 goto out;
1547 if ((r = cipher_crypt(&state->receive_context,
1548 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1549 block_size, 0, 0)) != 0)
1550 goto out;
1551 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1552 if (state->packlen < 1 + 4 ||
1553 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001554#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001555 fprintf(stderr, "input: \n");
1556 sshbuf_dump(state->input, stderr);
1557 fprintf(stderr, "incoming_packet: \n");
1558 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001559#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001560 logit("Bad packet length %u.", state->packlen);
1561 return ssh_packet_start_discard(ssh, enc, mac,
1562 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001563 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001564 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1565 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001566 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001567 DBG(debug("input: packet len %u", state->packlen+4));
1568
Damien Milleraf43a7a2012-12-12 10:46:31 +11001569 if (aadlen) {
1570 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001571 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001572 } else {
1573 /*
1574 * the payload size and the payload are encrypted, but we
1575 * have a partial packet of block_size bytes
1576 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001577 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001578 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001579 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1580 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001581 if (need % block_size != 0) {
1582 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001583 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001584 return ssh_packet_start_discard(ssh, enc, mac,
1585 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001586 }
Damien Miller33b13562000-04-04 14:38:59 +10001587 /*
1588 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001589 * decrypt into incoming_packet:
1590 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001591 * 'need' bytes are encrypted, followed by either
1592 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001593 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001594 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001595 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1596 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001597#ifdef PACKET_DEBUG
1598 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001599 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001600#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001601 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001602 if (mac && mac->enabled && mac->etm) {
1603 if ((r = mac_compute(mac, state->p_read.seqnr,
1604 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001605 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001606 goto out;
1607 }
1608 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1609 &cp)) != 0)
1610 goto out;
1611 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1612 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1613 goto out;
1614 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1615 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001616 /*
1617 * compute MAC over seqnr and packet,
1618 * increment sequence number for incoming packet
1619 */
Damien Miller4af51302000-04-16 11:18:38 +10001620 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001621 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001622 if ((r = mac_compute(mac, state->p_read.seqnr,
1623 sshbuf_ptr(state->incoming_packet),
1624 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001625 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001626 goto out;
1627 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001628 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001629 logit("Corrupted MAC on input.");
1630 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001631 return SSH_ERR_INTERNAL_ERROR;
1632 return ssh_packet_start_discard(ssh, enc, mac,
1633 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001634 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001635
1636 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1637 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1638 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001639 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001640 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001641 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001642 *seqnr_p = state->p_read.seqnr;
1643 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001644 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001645 if (++state->p_read.packets == 0)
1646 if (!(ssh->compat & SSH_BUG_NOREKEY))
1647 return SSH_ERR_NEED_REKEY;
1648 state->p_read.blocks += (state->packlen + 4) / block_size;
1649 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001650
1651 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001652 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001653 DBG(debug("input: padlen %d", padlen));
1654 if (padlen < 4)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001655 ssh_packet_disconnect(ssh,
1656 "Corrupted padlen %d on input.", padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001657
1658 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001659 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1660 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1661 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001662
markus@openbsd.org091c3022015-01-19 19:52:16 +00001663 DBG(debug("input: len before de-compress %zd",
1664 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001665 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001666 sshbuf_reset(state->compression_buffer);
1667 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1668 state->compression_buffer)) != 0)
1669 goto out;
1670 sshbuf_reset(state->incoming_packet);
1671 if ((r = sshbuf_putb(state->incoming_packet,
1672 state->compression_buffer)) != 0)
1673 goto out;
1674 DBG(debug("input: len after de-compress %zd",
1675 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001676 }
1677 /*
1678 * get packet type, implies consume.
1679 * return length of payload (without type field)
1680 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001681 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1682 goto out;
1683 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN)
1684 ssh_packet_disconnect(ssh,
1685 "Invalid ssh2 packet type: %d", *typep);
1686 if (*typep == SSH2_MSG_NEWKEYS)
1687 r = ssh_set_newkeys(ssh, MODE_IN);
1688 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1689 r = ssh_packet_enable_delayed_compress(ssh);
1690 else
1691 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001692#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001693 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1694 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001695#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001696 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001697 state->packlen = 0;
1698 out:
1699 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001700}
1701
1702int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001703ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001704{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001705 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001706 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001707 int r;
1708 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001709
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001710 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001711 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001712 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001713 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1714 if (r != 0)
1715 return r;
1716 if (*typep) {
1717 state->keep_alive_timeouts = 0;
1718 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001719 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001720 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001721 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001722 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001723 break;
Damien Miller33b13562000-04-04 14:38:59 +10001724 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001725 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1726 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1727 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1728 if (msg)
1729 free(msg);
1730 return r;
1731 }
Damien Miller33b13562000-04-04 14:38:59 +10001732 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001733 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001734 break;
1735 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001736 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1737 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1738 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001739 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001740 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001741 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1742 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1743 "Received disconnect from %s: %u: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001744 ssh_remote_ipaddr(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001745 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001746 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001747 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001748 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1749 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001750 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1751 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001752 break;
Damien Miller33b13562000-04-04 14:38:59 +10001753 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001754 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001755 }
Damien Miller33b13562000-04-04 14:38:59 +10001756 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001757 r = ssh_packet_read_poll1(ssh, typep);
1758 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001759 case SSH_MSG_NONE:
1760 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001761 case SSH_MSG_IGNORE:
1762 break;
1763 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001764 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1765 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001766 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001767 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001768 break;
1769 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001770 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1771 return r;
Damien Miller894926e2013-02-12 11:03:58 +11001772 error("Received disconnect from %s: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001773 ssh_remote_ipaddr(ssh), msg);
1774 free(msg);
1775 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001776 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001777 DBG(debug("received packet type %d", *typep));
1778 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001779 }
Damien Miller33b13562000-04-04 14:38:59 +10001780 }
1781 }
1782}
1783
Damien Miller5428f641999-11-25 11:54:57 +11001784/*
1785 * Buffers the given amount of input characters. This is intended to be used
1786 * together with packet_read_poll.
1787 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001788
1789void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001790ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001791{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001792 struct session_state *state = ssh->state;
1793 int r;
1794
1795 if (state->packet_discard) {
1796 state->keep_alive_timeouts = 0; /* ?? */
1797 if (len >= state->packet_discard) {
1798 if ((r = ssh_packet_stop_discard(ssh)) != 0)
1799 fatal("%s: %s", __func__, ssh_err(r));
1800 cleanup_exit(255);
1801 }
1802 state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001803 return;
1804 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001805 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
1806 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001807}
1808
Damien Miller4af51302000-04-16 11:18:38 +10001809int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001810ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001811{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001812 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001813}
1814
Damien Miller5428f641999-11-25 11:54:57 +11001815/*
1816 * Sends a diagnostic message from the server to the client. This message
1817 * can be sent at any time (but not while constructing another message). The
1818 * message is printed immediately, but only if the client is being executed
1819 * in verbose mode. These messages are primarily intended to ease debugging
1820 * authentication problems. The length of the formatted message must not
1821 * exceed 1024 bytes. This will automatically call packet_write_wait.
1822 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001823
1824void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001825ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001826{
Damien Miller95def091999-11-25 00:26:21 +11001827 char buf[1024];
1828 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001829 int r;
Damien Miller95def091999-11-25 00:26:21 +11001830
markus@openbsd.org091c3022015-01-19 19:52:16 +00001831 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001832 return;
1833
Damien Miller95def091999-11-25 00:26:21 +11001834 va_start(args, fmt);
1835 vsnprintf(buf, sizeof(buf), fmt, args);
1836 va_end(args);
1837
Damien Miller7c8af4f2000-05-01 08:24:07 +10001838 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001839 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1840 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1841 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1842 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1843 (r = sshpkt_send(ssh)) != 0)
1844 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001845 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001846 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1847 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1848 (r = sshpkt_send(ssh)) != 0)
1849 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001850 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001851 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001852}
1853
Damien Miller5428f641999-11-25 11:54:57 +11001854/*
1855 * Logs the error plus constructs and sends a disconnect packet, closes the
1856 * connection, and exits. This function never returns. The error message
1857 * should not contain a newline. The length of the formatted message must
1858 * not exceed 1024 bytes.
1859 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001860
1861void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001862ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001863{
Damien Miller95def091999-11-25 00:26:21 +11001864 char buf[1024];
1865 va_list args;
1866 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001867 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001868
Damien Miller95def091999-11-25 00:26:21 +11001869 if (disconnecting) /* Guard against recursive invocations. */
1870 fatal("packet_disconnect called recursively.");
1871 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001872
Damien Miller5428f641999-11-25 11:54:57 +11001873 /*
1874 * Format the message. Note that the caller must make sure the
1875 * message is of limited size.
1876 */
Damien Miller95def091999-11-25 00:26:21 +11001877 va_start(args, fmt);
1878 vsnprintf(buf, sizeof(buf), fmt, args);
1879 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001880
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001881 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001882 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001883
Damien Miller95def091999-11-25 00:26:21 +11001884 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001885 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001886 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
1887 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
1888 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1889 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1890 (r = sshpkt_send(ssh)) != 0)
1891 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001892 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001893 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
1894 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1895 (r = sshpkt_send(ssh)) != 0)
1896 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001897 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001898 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001899
Damien Miller95def091999-11-25 00:26:21 +11001900 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001901 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001902 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001903}
1904
Damien Miller5428f641999-11-25 11:54:57 +11001905/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001906
1907void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001908ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001909{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001910 struct session_state *state = ssh->state;
1911 int len = sshbuf_len(state->output);
1912 int cont, r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001913
Damien Miller95def091999-11-25 00:26:21 +11001914 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001915 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001916 len = roaming_write(state->connection_out,
1917 sshbuf_ptr(state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001918 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001919 if (errno == EINTR || errno == EAGAIN ||
1920 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001921 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001922 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001923 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001924 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001925 fatal("Write connection closed");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001926 if ((r = sshbuf_consume(state->output, len)) != 0)
1927 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001928 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001929}
1930
Damien Miller5428f641999-11-25 11:54:57 +11001931/*
1932 * Calls packet_write_poll repeatedly until all pending output data has been
1933 * written.
1934 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001935
1936void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001937ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001938{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001939 fd_set *setp;
Damien Millerce986542013-07-18 16:12:44 +10001940 int ret, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001941 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001942 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001943
markus@openbsd.org091c3022015-01-19 19:52:16 +00001944 setp = (fd_set *)calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001945 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001946 if (setp == NULL)
1947 fatal("%s: calloc failed", __func__);
1948 ssh_packet_write_poll(ssh);
1949 while (ssh_packet_have_data_to_write(ssh)) {
1950 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001951 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001952 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001953
markus@openbsd.org091c3022015-01-19 19:52:16 +00001954 if (state->packet_timeout_ms > 0) {
1955 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001956 timeoutp = &timeout;
1957 }
1958 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001959 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001960 ms_to_timeval(&timeout, ms_remain);
1961 gettimeofday(&start, NULL);
1962 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001963 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001964 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001965 break;
Damien Millerea437422009-10-02 11:49:03 +10001966 if (errno != EAGAIN && errno != EINTR &&
1967 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001968 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001969 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001970 continue;
1971 ms_subtract_diff(&start, &ms_remain);
1972 if (ms_remain <= 0) {
1973 ret = 0;
1974 break;
1975 }
1976 }
1977 if (ret == 0) {
1978 logit("Connection to %.200s timed out while "
markus@openbsd.org091c3022015-01-19 19:52:16 +00001979 "waiting to write", ssh_remote_ipaddr(ssh));
Darren Tucker3fc464e2008-06-13 06:42:45 +10001980 cleanup_exit(255);
1981 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001982 ssh_packet_write_poll(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001983 }
Darren Tuckera627d422013-06-02 07:31:17 +10001984 free(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001985}
1986
1987/* Returns true if there is buffered data to write to the connection. */
1988
1989int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001990ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001991{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001992 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001993}
1994
1995/* Returns true if there is not too much data to write to the connection. */
1996
1997int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001998ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001999{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002000 if (ssh->state->interactive_mode)
2001 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002002 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002003 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002004}
2005
markus@openbsd.org091c3022015-01-19 19:52:16 +00002006void
2007ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002008{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002009#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002010 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002011 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002012 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002013# ifdef IP_TOS
2014 case AF_INET:
2015 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002016 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002017 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2018 error("setsockopt IP_TOS %d: %.100s:",
2019 tos, strerror(errno));
2020 break;
2021# endif /* IP_TOS */
2022# ifdef IPV6_TCLASS
2023 case AF_INET6:
2024 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002025 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002026 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2027 error("setsockopt IPV6_TCLASS %d: %.100s:",
2028 tos, strerror(errno));
2029 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002030# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002031 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002032#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002033}
Ben Lindstroma7433982002-12-23 02:41:41 +00002034
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002035/* Informs that the current session is interactive. Sets IP flags for that. */
2036
2037void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002038ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002039{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002040 struct session_state *state = ssh->state;
2041
2042 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002043 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002044 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002045
Damien Miller95def091999-11-25 00:26:21 +11002046 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002047 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002048
Damien Miller34132e52000-01-14 15:45:46 +11002049 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002050 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002051 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002052 set_nodelay(state->connection_in);
2053 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2054 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002055}
2056
2057/* Returns true if the current connection is interactive. */
2058
2059int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002060ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002061{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002062 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002063}
Damien Miller6162d121999-11-21 13:23:52 +11002064
Darren Tucker1f8311c2004-05-13 16:39:33 +10002065int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002066ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002067{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002068 struct session_state *state = ssh->state;
2069
2070 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002071 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002072 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002073 return -1;
2074 }
2075 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002076 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002077 return -1;
2078 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002079 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002080 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002081 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002082 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002083}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002084
Darren Tuckerf7288d72009-06-21 18:12:20 +10002085int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002086ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002087{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002088 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002089}
2090
2091void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002092ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002093{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002094 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002095}
2096
2097u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002098ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002099{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002100 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002101}
2102
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002103/*
2104 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002105 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002106 * byte SSH_MSG_IGNORE
2107 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002108 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002109 * All implementations MUST understand (and ignore) this message at any
2110 * time (after receiving the protocol version). No implementation is
2111 * required to send them. This message can be used as an additional
2112 * protection measure against advanced traffic analysis techniques.
2113 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002114void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002115ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002116{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002117 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002118 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002119
markus@openbsd.org091c3022015-01-19 19:52:16 +00002120 if ((r = sshpkt_start(ssh, compat20 ?
2121 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2122 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2123 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002124 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002125 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002126 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002127 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2128 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002129 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002130 }
2131}
Damien Millera5539d22003-04-09 20:50:06 +10002132
Darren Tucker1f8311c2004-05-13 16:39:33 +10002133#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002134int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002135ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002136{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002137 struct session_state *state = ssh->state;
2138
2139 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002140 return 0;
2141 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002142 (state->p_send.packets > MAX_PACKETS) ||
2143 (state->p_read.packets > MAX_PACKETS) ||
2144 (state->max_blocks_out &&
2145 (state->p_send.blocks > state->max_blocks_out)) ||
2146 (state->max_blocks_in &&
2147 (state->p_read.blocks > state->max_blocks_in)) ||
2148 (state->rekey_interval != 0 && state->rekey_time +
2149 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002150}
2151
2152void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002153ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002154{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002155 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
2156 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002157 ssh->state->rekey_limit = bytes;
2158 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002159 /*
2160 * We set the time here so that in post-auth privsep slave we count
2161 * from the completion of the authentication.
2162 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002163 ssh->state->rekey_time = monotime();
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002164}
2165
2166time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002167ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002168{
2169 time_t seconds;
2170
markus@openbsd.org091c3022015-01-19 19:52:16 +00002171 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002172 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002173 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002174}
Damien Miller9786e6e2005-07-26 21:54:56 +10002175
2176void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002177ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002178{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002179 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002180}
2181
2182void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002183ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002184{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002185 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002186}
2187
2188void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002189ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002190{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002191 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002192}
2193
2194void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002195ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002196{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002197 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002198}
2199
markus@openbsd.org091c3022015-01-19 19:52:16 +00002200/* XXX TODO update roaming to new API (does not work anyway) */
Darren Tuckere841eb02009-07-06 07:11:13 +10002201/*
2202 * Save the state for the real connection, and use a separate state when
2203 * resuming a suspended connection.
2204 */
2205void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002206ssh_packet_backup_state(struct ssh *ssh,
2207 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002208{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002209 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002210
markus@openbsd.org091c3022015-01-19 19:52:16 +00002211 close(ssh->state->connection_in);
2212 ssh->state->connection_in = -1;
2213 close(ssh->state->connection_out);
2214 ssh->state->connection_out = -1;
Darren Tuckere841eb02009-07-06 07:11:13 +10002215 if (backup_state)
2216 tmp = backup_state;
2217 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002218 tmp = ssh_alloc_session_state();
2219 backup_state = ssh;
2220 ssh = tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002221}
2222
markus@openbsd.org091c3022015-01-19 19:52:16 +00002223/* XXX FIXME FIXME FIXME */
Darren Tuckere841eb02009-07-06 07:11:13 +10002224/*
2225 * Swap in the old state when resuming a connecion.
2226 */
2227void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002228ssh_packet_restore_state(struct ssh *ssh,
2229 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002230{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002231 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002232 u_int len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002233 int r;
Darren Tuckere841eb02009-07-06 07:11:13 +10002234
2235 tmp = backup_state;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002236 backup_state = ssh;
2237 ssh = tmp;
2238 ssh->state->connection_in = backup_state->state->connection_in;
2239 backup_state->state->connection_in = -1;
2240 ssh->state->connection_out = backup_state->state->connection_out;
2241 backup_state->state->connection_out = -1;
2242 len = sshbuf_len(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002243 if (len > 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002244 if ((r = sshbuf_putb(ssh->state->input,
2245 backup_state->state->input)) != 0)
2246 fatal("%s: %s", __func__, ssh_err(r));
2247 sshbuf_reset(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002248 add_recv_bytes(len);
2249 }
2250}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002251
2252/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002253static int
2254ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002255{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002256 struct sshcomp *comp;
2257 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002258
2259 debug("%s: called", __func__);
2260 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002261 ssh->state->after_authentication = 1;
2262 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002263 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002264 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002265 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002266 comp = &ssh->state->newkeys[mode]->comp;
2267 if (comp && comp->enabled &&
2268 (r = ssh_packet_init_compression(ssh)) != 0)
2269 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002270 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002271 return 0;
2272}
2273
2274/* Packet state (de-)serialization for privsep */
2275
2276/* turn kex into a blob for packet state serialization */
2277static int
2278kex_to_blob(struct sshbuf *m, struct kex *kex)
2279{
2280 int r;
2281
2282 if ((r = sshbuf_put_string(m, kex->session_id,
2283 kex->session_id_len)) != 0 ||
2284 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2285 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2286 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2287 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2288 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2289 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2290 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2291 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2292 return r;
2293 return 0;
2294}
2295
2296/* turn key exchange results into a blob for packet state serialization */
2297static int
2298newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2299{
2300 struct sshbuf *b;
2301 struct sshcipher_ctx *cc;
2302 struct sshcomp *comp;
2303 struct sshenc *enc;
2304 struct sshmac *mac;
2305 struct newkeys *newkey;
2306 int r;
2307
2308 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2309 return SSH_ERR_INTERNAL_ERROR;
2310 enc = &newkey->enc;
2311 mac = &newkey->mac;
2312 comp = &newkey->comp;
2313 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2314 &ssh->state->receive_context;
2315 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2316 return r;
2317 if ((b = sshbuf_new()) == NULL)
2318 return SSH_ERR_ALLOC_FAIL;
2319 /* The cipher struct is constant and shared, you export pointer */
2320 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2321 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2322 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2323 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2324 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2325 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2326 goto out;
2327 if (cipher_authlen(enc->cipher) == 0) {
2328 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2329 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2330 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2331 goto out;
2332 }
2333 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2334 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2335 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2336 goto out;
2337 r = sshbuf_put_stringb(m, b);
2338 out:
2339 if (b != NULL)
2340 sshbuf_free(b);
2341 return r;
2342}
2343
2344/* serialize packet state into a blob */
2345int
2346ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2347{
2348 struct session_state *state = ssh->state;
2349 u_char *p;
2350 size_t slen, rlen;
2351 int r, ssh1cipher;
2352
2353 if (!compat20) {
2354 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2355 slen = cipher_get_keyiv_len(&state->send_context);
2356 rlen = cipher_get_keyiv_len(&state->receive_context);
2357 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2358 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2359 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2360 (r = sshbuf_put_u32(m, slen)) != 0 ||
2361 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2362 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2363 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2364 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2365 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2366 return r;
2367 } else {
2368 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2369 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2370 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
2371 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2372 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2373 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2374 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2375 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2376 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2377 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2378 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2379 return r;
2380 }
2381
2382 slen = cipher_get_keycontext(&state->send_context, NULL);
2383 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2384 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2385 (r = sshbuf_reserve(m, slen, &p)) != 0)
2386 return r;
2387 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2388 return SSH_ERR_INTERNAL_ERROR;
2389 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2390 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2391 return r;
2392 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2393 return SSH_ERR_INTERNAL_ERROR;
2394
2395 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2396 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2397 (r = sshbuf_put_stringb(m, state->output)) != 0)
2398 return r;
2399
2400 if (compat20) {
2401 if ((r = sshbuf_put_u64(m, get_sent_bytes())) != 0 ||
2402 (r = sshbuf_put_u64(m, get_recv_bytes())) != 0)
2403 return r;
2404 }
2405 return 0;
2406}
2407
2408/* restore key exchange results from blob for packet state de-serialization */
2409static int
2410newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2411{
2412 struct sshbuf *b = NULL;
2413 struct sshcomp *comp;
2414 struct sshenc *enc;
2415 struct sshmac *mac;
2416 struct newkeys *newkey = NULL;
2417 size_t keylen, ivlen, maclen;
2418 int r;
2419
2420 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2421 r = SSH_ERR_ALLOC_FAIL;
2422 goto out;
2423 }
2424 if ((r = sshbuf_froms(m, &b)) != 0)
2425 goto out;
2426#ifdef DEBUG_PK
2427 sshbuf_dump(b, stderr);
2428#endif
2429 enc = &newkey->enc;
2430 mac = &newkey->mac;
2431 comp = &newkey->comp;
2432
2433 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2434 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2435 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2436 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2437 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2438 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2439 goto out;
2440 if (cipher_authlen(enc->cipher) == 0) {
2441 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2442 goto out;
2443 if ((r = mac_setup(mac, mac->name)) != 0)
2444 goto out;
2445 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2446 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2447 goto out;
2448 if (maclen > mac->key_len) {
2449 r = SSH_ERR_INVALID_FORMAT;
2450 goto out;
2451 }
2452 mac->key_len = maclen;
2453 }
2454 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2455 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2456 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2457 goto out;
2458 if (enc->name == NULL ||
2459 cipher_by_name(enc->name) != enc->cipher) {
2460 r = SSH_ERR_INVALID_FORMAT;
2461 goto out;
2462 }
2463 if (sshbuf_len(b) != 0) {
2464 r = SSH_ERR_INVALID_FORMAT;
2465 goto out;
2466 }
2467 enc->key_len = keylen;
2468 enc->iv_len = ivlen;
2469 ssh->kex->newkeys[mode] = newkey;
2470 newkey = NULL;
2471 r = 0;
2472 out:
2473 if (newkey != NULL)
2474 free(newkey);
2475 if (b != NULL)
2476 sshbuf_free(b);
2477 return r;
2478}
2479
2480/* restore kex from blob for packet state de-serialization */
2481static int
2482kex_from_blob(struct sshbuf *m, struct kex **kexp)
2483{
2484 struct kex *kex;
2485 int r;
2486
2487 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2488 (kex->my = sshbuf_new()) == NULL ||
2489 (kex->peer = sshbuf_new()) == NULL) {
2490 r = SSH_ERR_ALLOC_FAIL;
2491 goto out;
2492 }
2493 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2494 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2495 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2496 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2497 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2498 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2499 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2500 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2501 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2502 goto out;
2503 kex->server = 1;
2504 kex->done = 1;
2505 r = 0;
2506 out:
2507 if (r != 0 || kexp == NULL) {
2508 if (kex != NULL) {
2509 if (kex->my != NULL)
2510 sshbuf_free(kex->my);
2511 if (kex->peer != NULL)
2512 sshbuf_free(kex->peer);
2513 free(kex);
2514 }
2515 if (kexp != NULL)
2516 *kexp = NULL;
2517 } else {
2518 *kexp = kex;
2519 }
2520 return r;
2521}
2522
2523/*
2524 * Restore packet state from content of blob 'm' (de-serialization).
2525 * Note that 'm' will be partially consumed on parsing or any other errors.
2526 */
2527int
2528ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2529{
2530 struct session_state *state = ssh->state;
2531 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2532 size_t ssh1keylen, rlen, slen, ilen, olen;
2533 int r;
2534 u_int ssh1cipher = 0;
2535 u_int64_t sent_bytes = 0, recv_bytes = 0;
2536
2537 if (!compat20) {
2538 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2539 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2540 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2541 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2542 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2543 return r;
2544 if (ssh1cipher > INT_MAX)
2545 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2546 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2547 (int)ssh1cipher);
2548 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2549 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2550 return SSH_ERR_INVALID_FORMAT;
2551 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2552 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2553 return r;
2554 } else {
2555 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2556 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2557 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
2558 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2559 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2560 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2561 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2562 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2563 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2564 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2565 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2566 return r;
2567 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2568 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2569 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2570 return r;
2571 }
2572 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2573 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2574 return r;
2575 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2576 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2577 return SSH_ERR_INVALID_FORMAT;
2578 cipher_set_keycontext(&state->send_context, keyout);
2579 cipher_set_keycontext(&state->receive_context, keyin);
2580
2581 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2582 (r = ssh_packet_set_postauth(ssh)) != 0)
2583 return r;
2584
2585 sshbuf_reset(state->input);
2586 sshbuf_reset(state->output);
2587 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2588 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2589 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2590 (r = sshbuf_put(state->output, output, olen)) != 0)
2591 return r;
2592
2593 if (compat20) {
2594 if ((r = sshbuf_get_u64(m, &sent_bytes)) != 0 ||
2595 (r = sshbuf_get_u64(m, &recv_bytes)) != 0)
2596 return r;
2597 roam_set_bytes(sent_bytes, recv_bytes);
2598 }
2599 if (sshbuf_len(m))
2600 return SSH_ERR_INVALID_FORMAT;
2601 debug3("%s: done", __func__);
2602 return 0;
2603}
2604
2605/* NEW API */
2606
2607/* put data to the outgoing packet */
2608
2609int
2610sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2611{
2612 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2613}
2614
2615int
2616sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2617{
2618 return sshbuf_putb(ssh->state->outgoing_packet, b);
2619}
2620
2621int
2622sshpkt_put_u8(struct ssh *ssh, u_char val)
2623{
2624 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2625}
2626
2627int
2628sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2629{
2630 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2631}
2632
2633int
2634sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2635{
2636 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2637}
2638
2639int
2640sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2641{
2642 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2643}
2644
2645int
2646sshpkt_put_cstring(struct ssh *ssh, const void *v)
2647{
2648 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2649}
2650
2651int
2652sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2653{
2654 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2655}
2656
2657int
2658sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2659{
2660 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2661}
2662
2663int
2664sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2665{
2666 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2667}
2668
2669int
2670sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2671{
2672 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2673}
2674
2675/* fetch data from the incoming packet */
2676
2677int
2678sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2679{
2680 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2681}
2682
2683int
2684sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2685{
2686 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2687}
2688
2689int
2690sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2691{
2692 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2693}
2694
2695int
2696sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2697{
2698 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2699}
2700
2701int
2702sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2703{
2704 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2705}
2706
2707int
2708sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2709{
2710 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2711}
2712
2713int
2714sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2715{
2716 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2717}
2718
2719int
2720sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2721{
2722 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2723}
2724
2725int
2726sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2727{
2728 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2729}
2730
2731int
2732sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2733{
2734 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2735}
2736
2737int
2738sshpkt_get_end(struct ssh *ssh)
2739{
2740 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2741 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2742 return 0;
2743}
2744
2745const u_char *
2746sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2747{
2748 if (lenp != NULL)
2749 *lenp = sshbuf_len(ssh->state->incoming_packet);
2750 return sshbuf_ptr(ssh->state->incoming_packet);
2751}
2752
2753/* start a new packet */
2754
2755int
2756sshpkt_start(struct ssh *ssh, u_char type)
2757{
2758 u_char buf[9];
2759 int len;
2760
2761 DBG(debug("packet_start[%d]", type));
2762 len = compat20 ? 6 : 9;
2763 memset(buf, 0, len - 1);
2764 buf[len - 1] = type;
2765 sshbuf_reset(ssh->state->outgoing_packet);
2766 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2767}
2768
2769/* send it */
2770
2771int
2772sshpkt_send(struct ssh *ssh)
2773{
2774 if (compat20)
2775 return ssh_packet_send2(ssh);
2776 else
2777 return ssh_packet_send1(ssh);
2778}
2779
2780int
2781sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2782{
2783 char buf[1024];
2784 va_list args;
2785 int r;
2786
2787 va_start(args, fmt);
2788 vsnprintf(buf, sizeof(buf), fmt, args);
2789 va_end(args);
2790
2791 if (compat20) {
2792 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2793 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2794 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2795 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2796 (r = sshpkt_send(ssh)) != 0)
2797 return r;
2798 } else {
2799 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2800 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2801 (r = sshpkt_send(ssh)) != 0)
2802 return r;
2803 }
2804 return 0;
2805}
2806
2807/* roundup current message to pad bytes */
2808int
2809sshpkt_add_padding(struct ssh *ssh, u_char pad)
2810{
2811 ssh->state->extra_pad = pad;
2812 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002813}