blob: 3306e4d6c560427cd74a9a08ee162ea31151f311 [file] [log] [blame]
markus@openbsd.org091c3022015-01-19 19:52:16 +00001/* $OpenBSD: packet.c,v 1.201 2015/01/19 19:52:16 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);
236 state->connection_in = -1;
237 state->connection_out = -1;
238 state->max_packet_size = 32768;
239 state->packet_timeout_ms = -1;
240 state->p_send.packets = state->p_read.packets = 0;
241 state->initialized = 1;
242 /*
243 * ssh_packet_send2() needs to queue packets until
244 * we've done the initial key exchange.
245 */
246 state->rekeying = 1;
247 ssh->state = state;
248 return ssh;
249 fail:
250 if (state) {
251 sshbuf_free(state->input);
252 sshbuf_free(state->output);
253 sshbuf_free(state->incoming_packet);
254 sshbuf_free(state->outgoing_packet);
255 free(state);
256 }
257 free(ssh);
258 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000259}
Damien Millera5539d22003-04-09 20:50:06 +1000260
Damien Miller5428f641999-11-25 11:54:57 +1100261/*
262 * Sets the descriptors used for communication. Disables encryption until
263 * packet_set_encryption_key is called.
264 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000265struct ssh *
266ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000268 struct session_state *state;
269 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000270 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000271
Damien Miller874d77b2000-10-14 16:23:11 +1100272 if (none == NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000273 fatal("%s: cannot load cipher 'none'", __func__);
274 if (ssh == NULL)
275 ssh = ssh_alloc_session_state();
276 if (ssh == NULL)
277 fatal("%s: cound not allocate state", __func__);
278 state = ssh->state;
279 state->connection_in = fd_in;
280 state->connection_out = fd_out;
281 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000282 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000283 (r = cipher_init(&state->receive_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000284 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000285 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
286 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
287 deattack_init(&state->deattack);
288 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289}
290
Darren Tucker3fc464e2008-06-13 06:42:45 +1000291void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000292ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000293{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000294 struct session_state *state = ssh->state;
295
Damien Miller8ed4de82011-12-19 10:52:50 +1100296 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000297 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000298 return;
299 }
300 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000301 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000302 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000303 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000304}
305
markus@openbsd.org091c3022015-01-19 19:52:16 +0000306int
307ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100308{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000309 struct session_state *state = ssh->state;
310 int r;
311
312 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100313 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000314
Damien Miller13ae44c2009-01-28 16:38:41 +1100315 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000316 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000317 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000318 if ((r = sshbuf_put(state->incoming_packet, buf,
319 sizeof(buf))) != 0)
320 return r;
321 (void) mac_compute(state->packet_discard_mac,
322 state->p_read.seqnr,
323 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
324 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100325 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000326 logit("Finished discarding for %.200s", ssh_remote_ipaddr(ssh));
327 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100328}
329
markus@openbsd.org091c3022015-01-19 19:52:16 +0000330static int
331ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
332 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100333{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000334 struct session_state *state = ssh->state;
335 int r;
336
337 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
338 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
339 return r;
340 return SSH_ERR_MAC_INVALID;
341 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100342 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000343 state->packet_discard_mac = mac;
344 if (sshbuf_len(state->input) >= discard &&
345 (r = ssh_packet_stop_discard(ssh)) != 0)
346 return r;
347 state->packet_discard = discard - sshbuf_len(state->input);
348 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100349}
350
Damien Miller34132e52000-01-14 15:45:46 +1100351/* Returns 1 if remote host is connected via socket, 0 if not. */
352
353int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000354ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100355{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000356 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100357 struct sockaddr_storage from, to;
358 socklen_t fromlen, tolen;
359
360 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000361 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100362 return 1;
363 fromlen = sizeof(from);
364 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000365 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000366 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100367 return 0;
368 tolen = sizeof(to);
369 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000370 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000371 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100372 return 0;
373 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
374 return 0;
375 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
376 return 0;
377 return 1;
378}
379
Ben Lindstromf6027d32002-03-22 01:42:04 +0000380void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000381ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000382{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000383 if (ibytes)
384 *ibytes = ssh->state->p_read.bytes;
385 if (obytes)
386 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000387}
388
389int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000390ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100391{
392 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100393 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100394
395 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000396 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000397 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100398 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000399#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100400 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000401 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000402 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000403#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000404 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100405}
406
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407/* Sets the connection into non-blocking mode. */
408
409void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000410ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411{
Damien Miller95def091999-11-25 00:26:21 +1100412 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000413 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
markus@openbsd.org091c3022015-01-19 19:52:16 +0000415 if (ssh->state->connection_out != ssh->state->connection_in)
416 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417}
418
419/* Returns the socket used for reading. */
420
421int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000422ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000424 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425}
426
427/* Returns the descriptor used for writing. */
428
429int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000430ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000432 return ssh->state->connection_out;
433}
434
435/*
436 * Returns the IP-address of the remote host as a string. The returned
437 * string must not be freed.
438 */
439
440const char *
441ssh_remote_ipaddr(struct ssh *ssh)
442{
443 /* Check whether we have cached the ipaddr. */
444 if (ssh->remote_ipaddr == NULL)
445 ssh->remote_ipaddr = ssh_packet_connection_is_on_socket(ssh) ?
446 get_peer_ipaddr(ssh->state->connection_in) :
447 strdup("UNKNOWN");
448 if (ssh->remote_ipaddr == NULL)
449 return "UNKNOWN";
450 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451}
452
453/* Closes the connection and clears and frees internal data structures. */
454
455void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000456ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000458 struct session_state *state = ssh->state;
459 int r;
460 u_int mode;
461
462 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100463 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000464 state->initialized = 0;
465 if (state->connection_in == state->connection_out) {
466 shutdown(state->connection_out, SHUT_RDWR);
467 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100468 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000469 close(state->connection_in);
470 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100471 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000472 sshbuf_free(state->input);
473 sshbuf_free(state->output);
474 sshbuf_free(state->outgoing_packet);
475 sshbuf_free(state->incoming_packet);
476 for (mode = 0; mode < MODE_MAX; mode++)
477 kex_free_newkeys(state->newkeys[mode]);
478 if (state->compression_buffer) {
479 sshbuf_free(state->compression_buffer);
480 if (state->compression_out_started) {
481 z_streamp stream = &state->compression_out_stream;
482 debug("compress outgoing: "
483 "raw data %llu, compressed %llu, factor %.2f",
484 (unsigned long long)stream->total_in,
485 (unsigned long long)stream->total_out,
486 stream->total_in == 0 ? 0.0 :
487 (double) stream->total_out / stream->total_in);
488 if (state->compression_out_failures == 0)
489 deflateEnd(stream);
490 }
491 if (state->compression_in_started) {
492 z_streamp stream = &state->compression_out_stream;
493 debug("compress incoming: "
494 "raw data %llu, compressed %llu, factor %.2f",
495 (unsigned long long)stream->total_out,
496 (unsigned long long)stream->total_in,
497 stream->total_out == 0 ? 0.0 :
498 (double) stream->total_in / stream->total_out);
499 if (state->compression_in_failures == 0)
500 inflateEnd(stream);
501 }
Damien Miller95def091999-11-25 00:26:21 +1100502 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000503 if ((r = cipher_cleanup(&state->send_context)) != 0)
504 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
505 if ((r = cipher_cleanup(&state->receive_context)) != 0)
506 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
507 if (ssh->remote_ipaddr) {
508 free(ssh->remote_ipaddr);
509 ssh->remote_ipaddr = NULL;
510 }
511 free(ssh->state);
512 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513}
514
515/* Sets remote side protocol flags. */
516
517void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000518ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000520 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521}
522
523/* Returns the remote protocol flags set earlier by the above function. */
524
Ben Lindstrom46c16222000-12-22 01:43:59 +0000525u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000526ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000528 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529}
530
Damien Miller5428f641999-11-25 11:54:57 +1100531/*
532 * Starts packet compression from the next packet on in both directions.
533 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
534 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535
markus@openbsd.org091c3022015-01-19 19:52:16 +0000536static int
537ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000538{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000539 if (!ssh->state->compression_buffer &&
540 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
541 return SSH_ERR_ALLOC_FAIL;
542 return 0;
543}
544
545static int
546start_compression_out(struct ssh *ssh, int level)
547{
548 if (level < 1 || level > 9)
549 return SSH_ERR_INVALID_ARGUMENT;
550 debug("Enabling compression at level %d.", level);
551 if (ssh->state->compression_out_started == 1)
552 deflateEnd(&ssh->state->compression_out_stream);
553 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
554 case Z_OK:
555 ssh->state->compression_out_started = 1;
556 break;
557 case Z_MEM_ERROR:
558 return SSH_ERR_ALLOC_FAIL;
559 default:
560 return SSH_ERR_INTERNAL_ERROR;
561 }
562 return 0;
563}
564
565static int
566start_compression_in(struct ssh *ssh)
567{
568 if (ssh->state->compression_in_started == 1)
569 inflateEnd(&ssh->state->compression_in_stream);
570 switch (inflateInit(&ssh->state->compression_in_stream)) {
571 case Z_OK:
572 ssh->state->compression_in_started = 1;
573 break;
574 case Z_MEM_ERROR:
575 return SSH_ERR_ALLOC_FAIL;
576 default:
577 return SSH_ERR_INTERNAL_ERROR;
578 }
579 return 0;
580}
581
582int
583ssh_packet_start_compression(struct ssh *ssh, int level)
584{
585 int r;
586
587 if (ssh->state->packet_compression && !compat20)
588 return SSH_ERR_INTERNAL_ERROR;
589 ssh->state->packet_compression = 1;
590 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
591 (r = start_compression_in(ssh)) != 0 ||
592 (r = start_compression_out(ssh, level)) != 0)
593 return r;
594 return 0;
595}
596
597/* XXX remove need for separate compression buffer */
598static int
599compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
600{
601 u_char buf[4096];
602 int r, status;
603
604 if (ssh->state->compression_out_started != 1)
605 return SSH_ERR_INTERNAL_ERROR;
606
607 /* This case is not handled below. */
608 if (sshbuf_len(in) == 0)
609 return 0;
610
611 /* Input is the contents of the input buffer. */
612 if ((ssh->state->compression_out_stream.next_in =
613 sshbuf_mutable_ptr(in)) == NULL)
614 return SSH_ERR_INTERNAL_ERROR;
615 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
616
617 /* Loop compressing until deflate() returns with avail_out != 0. */
618 do {
619 /* Set up fixed-size output buffer. */
620 ssh->state->compression_out_stream.next_out = buf;
621 ssh->state->compression_out_stream.avail_out = sizeof(buf);
622
623 /* Compress as much data into the buffer as possible. */
624 status = deflate(&ssh->state->compression_out_stream,
625 Z_PARTIAL_FLUSH);
626 switch (status) {
627 case Z_MEM_ERROR:
628 return SSH_ERR_ALLOC_FAIL;
629 case Z_OK:
630 /* Append compressed data to output_buffer. */
631 if ((r = sshbuf_put(out, buf, sizeof(buf) -
632 ssh->state->compression_out_stream.avail_out)) != 0)
633 return r;
634 break;
635 case Z_STREAM_ERROR:
636 default:
637 ssh->state->compression_out_failures++;
638 return SSH_ERR_INVALID_FORMAT;
639 }
640 } while (ssh->state->compression_out_stream.avail_out == 0);
641 return 0;
642}
643
644static int
645uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
646{
647 u_char buf[4096];
648 int r, status;
649
650 if (ssh->state->compression_in_started != 1)
651 return SSH_ERR_INTERNAL_ERROR;
652
653 if ((ssh->state->compression_in_stream.next_in =
654 sshbuf_mutable_ptr(in)) == NULL)
655 return SSH_ERR_INTERNAL_ERROR;
656 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
657
658 for (;;) {
659 /* Set up fixed-size output buffer. */
660 ssh->state->compression_in_stream.next_out = buf;
661 ssh->state->compression_in_stream.avail_out = sizeof(buf);
662
663 status = inflate(&ssh->state->compression_in_stream,
664 Z_PARTIAL_FLUSH);
665 switch (status) {
666 case Z_OK:
667 if ((r = sshbuf_put(out, buf, sizeof(buf) -
668 ssh->state->compression_in_stream.avail_out)) != 0)
669 return r;
670 break;
671 case Z_BUF_ERROR:
672 /*
673 * Comments in zlib.h say that we should keep calling
674 * inflate() until we get an error. This appears to
675 * be the error that we get.
676 */
677 return 0;
678 case Z_DATA_ERROR:
679 return SSH_ERR_INVALID_FORMAT;
680 case Z_MEM_ERROR:
681 return SSH_ERR_ALLOC_FAIL;
682 case Z_STREAM_ERROR:
683 default:
684 ssh->state->compression_in_failures++;
685 return SSH_ERR_INTERNAL_ERROR;
686 }
687 }
688 /* NOTREACHED */
689}
690
691/* Serialise compression state into a blob for privsep */
692static int
693ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
694{
695 struct session_state *state = ssh->state;
696 struct sshbuf *b;
697 int r;
698
699 if ((b = sshbuf_new()) == NULL)
700 return SSH_ERR_ALLOC_FAIL;
701 if (state->compression_in_started) {
702 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
703 sizeof(state->compression_in_stream))) != 0)
704 goto out;
705 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
706 goto out;
707 if (state->compression_out_started) {
708 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
709 sizeof(state->compression_out_stream))) != 0)
710 goto out;
711 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
712 goto out;
713 r = sshbuf_put_stringb(m, b);
714 out:
715 sshbuf_free(b);
716 return r;
717}
718
719/* Deserialise compression state from a blob for privsep */
720static int
721ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
722{
723 struct session_state *state = ssh->state;
724 struct sshbuf *b = NULL;
725 int r;
726 const u_char *inblob, *outblob;
727 size_t inl, outl;
728
729 if ((r = sshbuf_froms(m, &b)) != 0)
730 goto out;
731 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
732 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
733 goto out;
734 if (inl == 0)
735 state->compression_in_started = 0;
736 else if (inl != sizeof(state->compression_in_stream)) {
737 r = SSH_ERR_INTERNAL_ERROR;
738 goto out;
739 } else {
740 state->compression_in_started = 1;
741 memcpy(&state->compression_in_stream, inblob, inl);
742 }
743 if (outl == 0)
744 state->compression_out_started = 0;
745 else if (outl != sizeof(state->compression_out_stream)) {
746 r = SSH_ERR_INTERNAL_ERROR;
747 goto out;
748 } else {
749 state->compression_out_started = 1;
750 memcpy(&state->compression_out_stream, outblob, outl);
751 }
752 r = 0;
753 out:
754 sshbuf_free(b);
755 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000756}
757
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000758void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000759ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
760 void *(*allocfunc)(void *, u_int, u_int),
761 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000762{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000763 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
764 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
765 ssh->state->compression_out_stream.opaque = ctx;
766 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
767 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
768 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769}
770
Damien Miller5428f641999-11-25 11:54:57 +1100771/*
Damien Miller5428f641999-11-25 11:54:57 +1100772 * Causes any further packets to be encrypted using the given key. The same
773 * key is used for both sending and reception. However, both directions are
774 * encrypted independently of each other.
775 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000776
Damien Miller1f0311c2014-05-15 14:24:09 +1000777#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000779ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000781 struct session_state *state = ssh->state;
782 const struct sshcipher *cipher = cipher_by_number(number);
783 int r;
784 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100785
markus@openbsd.org091c3022015-01-19 19:52:16 +0000786 if (cipher == NULL)
787 fatal("%s: unknown cipher number %d", __func__, number);
788 if (keylen < 20)
789 fatal("%s: keylen too small: %d", __func__, keylen);
790 if (keylen > SSH_SESSION_KEY_LENGTH)
791 fatal("%s: keylen too big: %d", __func__, keylen);
792 memcpy(state->ssh1_key, key, keylen);
793 state->ssh1_keylen = keylen;
794 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
795 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
796 (r = cipher_init(&state->receive_context, cipher, key, keylen,
797 NULL, 0, CIPHER_DECRYPT) != 0))
798 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
799 if (!state->cipher_warning_done &&
800 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
801 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
802 error("Warning: %s", wmsg);
803 state->cipher_warning_done = 1;
804 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000805}
Damien Miller6af914a2010-09-10 11:39:26 +1000806#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000807
Damien Miller5428f641999-11-25 11:54:57 +1100808/*
809 * Finalizes and sends the packet. If the encryption key has been set,
810 * encrypts the packet before sending.
811 */
Damien Miller95def091999-11-25 00:26:21 +1100812
markus@openbsd.org091c3022015-01-19 19:52:16 +0000813int
814ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000816 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000817 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000818 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000819 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820
Damien Miller5428f641999-11-25 11:54:57 +1100821 /*
822 * If using packet compression, compress the payload of the outgoing
823 * packet.
824 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000825 if (state->packet_compression) {
826 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100827 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000828 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
829 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100830 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000831 if ((r = sshbuf_put(state->compression_buffer,
832 "\0\0\0\0\0\0\0\0", 8)) != 0)
833 goto out;
834 if ((r = compress_buffer(ssh, state->outgoing_packet,
835 state->compression_buffer)) != 0)
836 goto out;
837 sshbuf_reset(state->outgoing_packet);
838 if ((r = sshbuf_putb(state->outgoing_packet,
839 state->compression_buffer)) != 0)
840 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100841 }
842 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000843 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844
Damien Millere247cc42000-05-07 12:03:14 +1000845 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100846 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000847 if (!state->send_context.plaintext) {
848 cp = sshbuf_mutable_ptr(state->outgoing_packet);
849 if (cp == NULL) {
850 r = SSH_ERR_INTERNAL_ERROR;
851 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100852 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000853 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100854 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000855 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
856 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100857
858 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000859 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
860 sshbuf_len(state->outgoing_packet));
861 POKE_U32(buf, checksum);
862 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
863 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864
865#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100866 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000867 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868#endif
869
Damien Miller95def091999-11-25 00:26:21 +1100870 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000871 POKE_U32(buf, len);
872 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
873 goto out;
874 if ((r = sshbuf_reserve(state->output,
875 sshbuf_len(state->outgoing_packet), &cp)) != 0)
876 goto out;
877 if ((r = cipher_crypt(&state->send_context, 0, cp,
878 sshbuf_ptr(state->outgoing_packet),
879 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
880 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100881
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100883 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000884 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000886 state->p_send.packets++;
887 state->p_send.bytes += len +
888 sshbuf_len(state->outgoing_packet);
889 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890
Damien Miller5428f641999-11-25 11:54:57 +1100891 /*
Damien Miller788f2122005-11-05 15:14:59 +1100892 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100893 * actually sent until packet_write_wait or packet_write_poll is
894 * called.
895 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000896 r = 0;
897 out:
898 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899}
900
markus@openbsd.org091c3022015-01-19 19:52:16 +0000901int
902ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000903{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000904 struct session_state *state = ssh->state;
905 struct sshenc *enc;
906 struct sshmac *mac;
907 struct sshcomp *comp;
908 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000909 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000910 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000911 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000912
Ben Lindstrom064496f2002-12-23 02:04:22 +0000913 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000914
Damien Miller963f6b22002-02-19 15:21:23 +1100915 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000916 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000917 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000918 state->p_send.packets = state->p_send.blocks = 0;
919 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100920 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000921 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000922 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000923 state->p_read.packets = state->p_read.blocks = 0;
924 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100925 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000926 if (state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000927 debug("set_newkeys: rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000928 if ((r = cipher_cleanup(cc)) != 0)
929 return r;
930 enc = &state->newkeys[mode]->enc;
931 mac = &state->newkeys[mode]->mac;
932 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000933 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100934 explicit_bzero(enc->iv, enc->iv_len);
935 explicit_bzero(enc->key, enc->key_len);
936 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000937 free(enc->name);
938 free(enc->iv);
939 free(enc->key);
940 free(mac->name);
941 free(mac->key);
942 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000943 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000944 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000945 /* move newkeys from kex to state */
946 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
947 return SSH_ERR_INTERNAL_ERROR;
948 ssh->kex->newkeys[mode] = NULL;
949 enc = &state->newkeys[mode]->enc;
950 mac = &state->newkeys[mode]->mac;
951 comp = &state->newkeys[mode]->comp;
952 if (cipher_authlen(enc->cipher) == 0) {
953 if ((r = mac_init(mac)) != 0)
954 return r;
955 }
956 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000957 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000958 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
959 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000960 return r;
961 if (!state->cipher_warning_done &&
962 (wmsg = cipher_warning_message(cc)) != NULL) {
963 error("Warning: %s", wmsg);
964 state->cipher_warning_done = 1;
965 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000966 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100967 /* explicit_bzero(enc->iv, enc->block_size);
968 explicit_bzero(enc->key, enc->key_len);
969 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000970 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000971 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +0000972 state->after_authentication)) && comp->enabled == 0) {
973 if ((r = ssh_packet_init_compression(ssh)) < 0)
974 return r;
975 if (mode == MODE_OUT) {
976 if ((r = start_compression_out(ssh, 6)) != 0)
977 return r;
978 } else {
979 if ((r = start_compression_in(ssh)) != 0)
980 return r;
981 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000982 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000983 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000984 /*
985 * The 2^(blocksize*2) limit is too expensive for 3DES,
986 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
987 */
988 if (enc->block_size >= 16)
989 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
990 else
991 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000992 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000993 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +0000994 state->rekey_limit / enc->block_size);
995 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000996}
997
Damien Miller5428f641999-11-25 11:54:57 +1100998/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000999 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001000 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001001 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1002 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001003static int
1004ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001005{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001006 struct session_state *state = ssh->state;
1007 struct sshcomp *comp = NULL;
1008 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001009
1010 /*
1011 * Remember that we are past the authentication step, so rekeying
1012 * with COMP_DELAYED will turn on compression immediately.
1013 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001014 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001015 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001016 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001017 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001018 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001019 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001020 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001021 if ((r = ssh_packet_init_compression(ssh)) != 0)
1022 return r;
1023 if (mode == MODE_OUT) {
1024 if ((r = start_compression_out(ssh, 6)) != 0)
1025 return r;
1026 } else {
1027 if ((r = start_compression_in(ssh)) != 0)
1028 return r;
1029 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001030 comp->enabled = 1;
1031 }
1032 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001033 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001034}
1035
1036/*
Damien Miller33b13562000-04-04 14:38:59 +10001037 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1038 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001039int
1040ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001041{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001042 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001043 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001044 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001045 u_int authlen = 0, aadlen = 0;
1046 u_int len;
1047 struct sshenc *enc = NULL;
1048 struct sshmac *mac = NULL;
1049 struct sshcomp *comp = NULL;
1050 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001051
markus@openbsd.org091c3022015-01-19 19:52:16 +00001052 if (state->newkeys[MODE_OUT] != NULL) {
1053 enc = &state->newkeys[MODE_OUT]->enc;
1054 mac = &state->newkeys[MODE_OUT]->mac;
1055 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001056 /* disable mac for authenticated encryption */
1057 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1058 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001059 }
Damien Miller963f6b22002-02-19 15:21:23 +11001060 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001061 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001062
markus@openbsd.org091c3022015-01-19 19:52:16 +00001063 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001064
1065#ifdef PACKET_DEBUG
1066 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001067 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001068#endif
1069
1070 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001071 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001072 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001073 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1074 goto out;
1075 sshbuf_reset(state->compression_buffer);
1076 if ((r = compress_buffer(ssh, state->outgoing_packet,
1077 state->compression_buffer)) != 0)
1078 goto out;
1079 sshbuf_reset(state->outgoing_packet);
1080 if ((r = sshbuf_put(state->outgoing_packet,
1081 "\0\0\0\0\0", 5)) != 0 ||
1082 (r = sshbuf_putb(state->outgoing_packet,
1083 state->compression_buffer)) != 0)
1084 goto out;
1085 DBG(debug("compression: raw %d compressed %zd", len,
1086 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001087 }
1088
1089 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001090 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001091
1092 /*
1093 * calc size of padding, alloc space, get random data,
1094 * minimum padding is 4 bytes
1095 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001096 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001097 padlen = block_size - (len % block_size);
1098 if (padlen < 4)
1099 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001100 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001101 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001102 state->extra_pad =
1103 roundup(state->extra_pad, block_size);
1104 pad = state->extra_pad -
1105 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001106 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001107 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001108 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001109 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001110 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001111 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1112 goto out;
1113 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001114 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001115 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001116 } else {
1117 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001118 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001119 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001120 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001121 len = sshbuf_len(state->outgoing_packet);
1122 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1123 if (cp == NULL) {
1124 r = SSH_ERR_INTERNAL_ERROR;
1125 goto out;
1126 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001127 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001128 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001129 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001130 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1131 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001132
1133 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001134 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001135 if ((r = mac_compute(mac, state->p_send.seqnr,
1136 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001137 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001138 goto out;
1139 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001140 }
1141 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001142 if ((r = sshbuf_reserve(state->output,
1143 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1144 goto out;
1145 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1146 sshbuf_ptr(state->outgoing_packet),
1147 len - aadlen, aadlen, authlen)) != 0)
1148 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001149 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001150 if (mac && mac->enabled) {
1151 if (mac->etm) {
1152 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001153 if ((r = mac_compute(mac, state->p_send.seqnr,
1154 cp, len, macbuf, sizeof(macbuf))) != 0)
1155 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001156 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001157 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001158 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001159 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1160 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001161 }
Damien Miller33b13562000-04-04 14:38:59 +10001162#ifdef PACKET_DEBUG
1163 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001164 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001165#endif
Damien Miller4af51302000-04-16 11:18:38 +10001166 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001167 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001168 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001169 if (++state->p_send.packets == 0)
1170 if (!(ssh->compat & SSH_BUG_NOREKEY))
1171 return SSH_ERR_NEED_REKEY;
1172 state->p_send.blocks += len / block_size;
1173 state->p_send.bytes += len;
1174 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001175
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001176 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001177 r = ssh_set_newkeys(ssh, MODE_OUT);
1178 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1179 r = ssh_packet_enable_delayed_compress(ssh);
1180 else
1181 r = 0;
1182 out:
1183 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001184}
1185
markus@openbsd.org091c3022015-01-19 19:52:16 +00001186int
1187ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001188{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001189 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001190 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001191 u_char type;
1192 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001193
markus@openbsd.org091c3022015-01-19 19:52:16 +00001194 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001195
1196 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001197 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001198 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1199 (type > SSH2_MSG_TRANSPORT_MAX) ||
1200 (type == SSH2_MSG_SERVICE_REQUEST) ||
1201 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +10001202 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001203 p = calloc(1, sizeof(*p));
1204 if (p == NULL)
1205 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001206 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001207 p->payload = state->outgoing_packet;
1208 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1209 state->outgoing_packet = sshbuf_new();
1210 if (state->outgoing_packet == NULL)
1211 return SSH_ERR_ALLOC_FAIL;
1212 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001213 }
1214 }
1215
1216 /* rekeying starts with sending KEXINIT */
1217 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001218 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001219
markus@openbsd.org091c3022015-01-19 19:52:16 +00001220 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1221 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001222
1223 /* after a NEWKEYS message we can send the complete queue */
1224 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001225 state->rekeying = 0;
1226 state->rekey_time = monotime();
1227 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001228 type = p->type;
1229 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001230 sshbuf_free(state->outgoing_packet);
1231 state->outgoing_packet = p->payload;
1232 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001233 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001234 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1235 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001236 }
1237 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001238 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001239}
1240
Damien Miller33b13562000-04-04 14:38:59 +10001241/*
Damien Miller5428f641999-11-25 11:54:57 +11001242 * Waits until a packet has been received, and returns its type. Note that
1243 * no other data is processed until this returns, so this function should not
1244 * be used during the interactive session.
1245 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246
1247int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001248ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001250 struct session_state *state = ssh->state;
1251 int len, r, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001252 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001253 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001254 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255
Darren Tucker99bb7612008-06-13 22:02:50 +10001256 DBG(debug("packet_read()"));
1257
markus@openbsd.org091c3022015-01-19 19:52:16 +00001258 setp = (fd_set *)calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001259 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001260 if (setp == NULL)
1261 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001262
Damien Miller95def091999-11-25 00:26:21 +11001263 /* Since we are blocking, ensure that all written packets have been sent. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001264 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265
Damien Miller95def091999-11-25 00:26:21 +11001266 /* Stay in the loop until we have received a complete packet. */
1267 for (;;) {
1268 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001269 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1270 if (r != 0)
1271 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001272 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001273 *typep == SSH_SMSG_SUCCESS
1274 || *typep == SSH_SMSG_FAILURE
1275 || *typep == SSH_CMSG_EOF
1276 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1277 if ((r = sshpkt_get_end(ssh)) != 0)
1278 break;
Damien Miller95def091999-11-25 00:26:21 +11001279 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001280 if (*typep != SSH_MSG_NONE)
1281 break;
Damien Miller5428f641999-11-25 11:54:57 +11001282 /*
1283 * Otherwise, wait for some data to arrive, add it to the
1284 * buffer, and try again.
1285 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001286 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001287 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001288 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001289
markus@openbsd.org091c3022015-01-19 19:52:16 +00001290 if (state->packet_timeout_ms > 0) {
1291 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001292 timeoutp = &timeout;
1293 }
Damien Miller95def091999-11-25 00:26:21 +11001294 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001295 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001296 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001297 ms_to_timeval(&timeout, ms_remain);
1298 gettimeofday(&start, NULL);
1299 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001300 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001301 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001302 break;
Damien Millerea437422009-10-02 11:49:03 +10001303 if (errno != EAGAIN && errno != EINTR &&
1304 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001305 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001306 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001307 continue;
1308 ms_subtract_diff(&start, &ms_remain);
1309 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001310 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001311 break;
1312 }
1313 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001314 if (r == 0) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001315 logit("Connection to %.200s timed out while "
markus@openbsd.org091c3022015-01-19 19:52:16 +00001316 "waiting to read", ssh_remote_ipaddr(ssh));
Darren Tucker3fc464e2008-06-13 06:42:45 +10001317 cleanup_exit(255);
1318 }
Damien Miller95def091999-11-25 00:26:21 +11001319 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001320 do {
1321 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001322 len = roaming_read(state->connection_in, buf,
Darren Tuckerc5564e12009-06-21 18:53:53 +10001323 sizeof(buf), &cont);
1324 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001325 if (len == 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001326 logit("Connection closed by %.200s",
1327 ssh_remote_ipaddr(ssh));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001328 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001329 }
Damien Miller95def091999-11-25 00:26:21 +11001330 if (len < 0)
1331 fatal("Read from socket failed: %.100s", strerror(errno));
1332 /* Append it to the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001333 ssh_packet_process_incoming(ssh, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001334 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001335 free(setp);
1336 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001337}
1338
Damien Miller278f9072001-12-21 15:00:19 +11001339int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001340ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001341{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001342 u_char type;
1343 int r;
1344
1345 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1346 fatal("%s: %s", __func__, ssh_err(r));
1347 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001348}
1349
Damien Miller5428f641999-11-25 11:54:57 +11001350/*
1351 * Waits until a packet has been received, verifies that its type matches
1352 * that given, and gives a fatal error and exits if there is a mismatch.
1353 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001354
1355void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001356ssh_packet_read_expect(struct ssh *ssh, int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357{
Damien Miller95def091999-11-25 00:26:21 +11001358 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359
markus@openbsd.org091c3022015-01-19 19:52:16 +00001360 type = ssh_packet_read(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001361 if (type != expected_type)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001362 ssh_packet_disconnect(ssh,
1363 "Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001364 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365}
1366
1367/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001368 * packet_process_incoming. If so, reads the packet; otherwise returns
1369 * SSH_MSG_NONE. This does not wait for data from the connection.
1370 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001371 * SSH_MSG_DISCONNECT is handled specially here. Also,
1372 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1373 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001374 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375
markus@openbsd.org091c3022015-01-19 19:52:16 +00001376int
1377ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001379 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001380 u_int len, padded_len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001381 const u_char *cp;
1382 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001383 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001384 int r;
1385
1386 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Damien Miller95def091999-11-25 00:26:21 +11001388 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001389 if (sshbuf_len(state->input) < 4 + 8)
1390 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001391 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001392 len = PEEK_U32(sshbuf_ptr(state->input));
Damien Miller95def091999-11-25 00:26:21 +11001393 if (len < 1 + 2 + 2 || len > 256 * 1024)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001394 ssh_packet_disconnect(ssh, "Bad packet length %u.",
1395 len);
Damien Miller95def091999-11-25 00:26:21 +11001396 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397
Damien Miller95def091999-11-25 00:26:21 +11001398 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001399 if (sshbuf_len(state->input) < 4 + padded_len)
1400 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401
Damien Miller95def091999-11-25 00:26:21 +11001402 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
Damien Miller95def091999-11-25 00:26:21 +11001404 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001405 if ((r = sshbuf_consume(state->input, 4)) != 0)
1406 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001407
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001408 /*
1409 * Cryptographic attack detector for ssh
1410 * (C)1998 CORE-SDI, Buenos Aires Argentina
1411 * Ariel Futoransky(futo@core-sdi.com)
1412 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001413 if (!state->receive_context.plaintext) {
1414 switch (detect_attack(&state->deattack,
1415 sshbuf_ptr(state->input), padded_len)) {
1416 case DEATTACK_OK:
1417 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001418 case DEATTACK_DETECTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001419 ssh_packet_disconnect(ssh,
1420 "crc32 compensation attack: network attack detected"
1421 );
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001422 case DEATTACK_DOS_DETECTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001423 ssh_packet_disconnect(ssh,
1424 "deattack denial of service detected");
1425 default:
1426 ssh_packet_disconnect(ssh, "deattack error");
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001427 }
1428 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001429
1430 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001431 sshbuf_reset(state->incoming_packet);
1432 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1433 goto out;
1434 if ((r = cipher_crypt(&state->receive_context, 0, p,
1435 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1436 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001437
markus@openbsd.org091c3022015-01-19 19:52:16 +00001438 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1439 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001440
1441#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001442 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001443 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445
Damien Miller95def091999-11-25 00:26:21 +11001446 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001447 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1448 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449
Damien Miller95def091999-11-25 00:26:21 +11001450 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001451 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1452 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001453
Damien Miller95def091999-11-25 00:26:21 +11001454 /* Test check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001455 if (len != sshbuf_len(state->incoming_packet))
1456 ssh_packet_disconnect(ssh,
1457 "packet_read_poll1: len %d != sshbuf_len %zd.",
1458 len, sshbuf_len(state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001459
markus@openbsd.org091c3022015-01-19 19:52:16 +00001460 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1461 stored_checksum = PEEK_U32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001462 if (checksum != stored_checksum)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001463 ssh_packet_disconnect(ssh,
1464 "Corrupted check bytes on input.");
1465 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1466 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467
markus@openbsd.org091c3022015-01-19 19:52:16 +00001468 if (state->packet_compression) {
1469 sshbuf_reset(state->compression_buffer);
1470 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1471 state->compression_buffer)) != 0)
1472 goto out;
1473 sshbuf_reset(state->incoming_packet);
1474 if ((r = sshbuf_putb(state->incoming_packet,
1475 state->compression_buffer)) != 0)
1476 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001477 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001478 state->p_read.packets++;
1479 state->p_read.bytes += padded_len + 4;
1480 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1481 goto out;
1482 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX)
1483 ssh_packet_disconnect(ssh,
1484 "Invalid ssh1 packet type: %d", *typep);
1485 r = 0;
1486 out:
1487 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488}
Damien Miller95def091999-11-25 00:26:21 +11001489
markus@openbsd.org091c3022015-01-19 19:52:16 +00001490int
1491ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001492{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001493 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001494 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001495 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1496 u_int maclen, aadlen = 0, authlen = 0, block_size;
1497 struct sshenc *enc = NULL;
1498 struct sshmac *mac = NULL;
1499 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001500 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001501
markus@openbsd.org091c3022015-01-19 19:52:16 +00001502 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001503
markus@openbsd.org091c3022015-01-19 19:52:16 +00001504 if (state->packet_discard)
1505 return 0;
1506
1507 if (state->newkeys[MODE_IN] != NULL) {
1508 enc = &state->newkeys[MODE_IN]->enc;
1509 mac = &state->newkeys[MODE_IN]->mac;
1510 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001511 /* disable mac for authenticated encryption */
1512 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1513 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001514 }
1515 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001516 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001517 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001518
markus@openbsd.org091c3022015-01-19 19:52:16 +00001519 if (aadlen && state->packlen == 0) {
1520 if (cipher_get_length(&state->receive_context,
1521 &state->packlen, state->p_read.seqnr,
1522 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1523 return 0;
1524 if (state->packlen < 1 + 4 ||
1525 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001526#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001527 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001528#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001529 logit("Bad packet length %u.", state->packlen);
1530 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1531 return r;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001532 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001533 sshbuf_reset(state->incoming_packet);
1534 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001535 /*
1536 * check if input size is less than the cipher block size,
1537 * decrypt first block and extract length of incoming packet
1538 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001539 if (sshbuf_len(state->input) < block_size)
1540 return 0;
1541 sshbuf_reset(state->incoming_packet);
1542 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1543 &cp)) != 0)
1544 goto out;
1545 if ((r = cipher_crypt(&state->receive_context,
1546 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1547 block_size, 0, 0)) != 0)
1548 goto out;
1549 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1550 if (state->packlen < 1 + 4 ||
1551 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001552#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001553 fprintf(stderr, "input: \n");
1554 sshbuf_dump(state->input, stderr);
1555 fprintf(stderr, "incoming_packet: \n");
1556 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001557#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001558 logit("Bad packet length %u.", state->packlen);
1559 return ssh_packet_start_discard(ssh, enc, mac,
1560 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001561 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001562 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1563 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001564 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001565 DBG(debug("input: packet len %u", state->packlen+4));
1566
Damien Milleraf43a7a2012-12-12 10:46:31 +11001567 if (aadlen) {
1568 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001569 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001570 } else {
1571 /*
1572 * the payload size and the payload are encrypted, but we
1573 * have a partial packet of block_size bytes
1574 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001575 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001576 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001577 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1578 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001579 if (need % block_size != 0) {
1580 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001581 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001582 return ssh_packet_start_discard(ssh, enc, mac,
1583 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001584 }
Damien Miller33b13562000-04-04 14:38:59 +10001585 /*
1586 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001587 * decrypt into incoming_packet:
1588 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001589 * 'need' bytes are encrypted, followed by either
1590 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001591 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001592 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001593 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1594 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001595#ifdef PACKET_DEBUG
1596 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001597 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001598#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001599 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001600 if (mac && mac->enabled && mac->etm) {
1601 if ((r = mac_compute(mac, state->p_read.seqnr,
1602 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001603 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001604 goto out;
1605 }
1606 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1607 &cp)) != 0)
1608 goto out;
1609 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1610 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1611 goto out;
1612 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1613 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001614 /*
1615 * compute MAC over seqnr and packet,
1616 * increment sequence number for incoming packet
1617 */
Damien Miller4af51302000-04-16 11:18:38 +10001618 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001619 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001620 if ((r = mac_compute(mac, state->p_read.seqnr,
1621 sshbuf_ptr(state->incoming_packet),
1622 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001623 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001624 goto out;
1625 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001626 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001627 logit("Corrupted MAC on input.");
1628 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001629 return SSH_ERR_INTERNAL_ERROR;
1630 return ssh_packet_start_discard(ssh, enc, mac,
1631 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001632 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001633
1634 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1635 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1636 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001637 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001638 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001639 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001640 *seqnr_p = state->p_read.seqnr;
1641 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001642 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001643 if (++state->p_read.packets == 0)
1644 if (!(ssh->compat & SSH_BUG_NOREKEY))
1645 return SSH_ERR_NEED_REKEY;
1646 state->p_read.blocks += (state->packlen + 4) / block_size;
1647 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001648
1649 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001650 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001651 DBG(debug("input: padlen %d", padlen));
1652 if (padlen < 4)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001653 ssh_packet_disconnect(ssh,
1654 "Corrupted padlen %d on input.", padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001655
1656 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001657 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1658 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1659 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001660
markus@openbsd.org091c3022015-01-19 19:52:16 +00001661 DBG(debug("input: len before de-compress %zd",
1662 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001663 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001664 sshbuf_reset(state->compression_buffer);
1665 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1666 state->compression_buffer)) != 0)
1667 goto out;
1668 sshbuf_reset(state->incoming_packet);
1669 if ((r = sshbuf_putb(state->incoming_packet,
1670 state->compression_buffer)) != 0)
1671 goto out;
1672 DBG(debug("input: len after de-compress %zd",
1673 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001674 }
1675 /*
1676 * get packet type, implies consume.
1677 * return length of payload (without type field)
1678 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001679 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1680 goto out;
1681 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN)
1682 ssh_packet_disconnect(ssh,
1683 "Invalid ssh2 packet type: %d", *typep);
1684 if (*typep == SSH2_MSG_NEWKEYS)
1685 r = ssh_set_newkeys(ssh, MODE_IN);
1686 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1687 r = ssh_packet_enable_delayed_compress(ssh);
1688 else
1689 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001690#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001691 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1692 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001693#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001694 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001695 state->packlen = 0;
1696 out:
1697 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001698}
1699
1700int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001701ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001702{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001703 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001704 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001705 int r;
1706 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001707
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001708 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001709 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001710 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001711 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1712 if (r != 0)
1713 return r;
1714 if (*typep) {
1715 state->keep_alive_timeouts = 0;
1716 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001717 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001718 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001719 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001720 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001721 break;
Damien Miller33b13562000-04-04 14:38:59 +10001722 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001723 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1724 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1725 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1726 if (msg)
1727 free(msg);
1728 return r;
1729 }
Damien Miller33b13562000-04-04 14:38:59 +10001730 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001731 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001732 break;
1733 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001734 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1735 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1736 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001737 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001738 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001739 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1740 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1741 "Received disconnect from %s: %u: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001742 ssh_remote_ipaddr(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001743 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001744 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001745 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001746 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1747 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001748 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1749 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001750 break;
Damien Miller33b13562000-04-04 14:38:59 +10001751 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001752 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001753 }
Damien Miller33b13562000-04-04 14:38:59 +10001754 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001755 r = ssh_packet_read_poll1(ssh, typep);
1756 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001757 case SSH_MSG_NONE:
1758 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001759 case SSH_MSG_IGNORE:
1760 break;
1761 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001762 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1763 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001764 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001765 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001766 break;
1767 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001768 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1769 return r;
Damien Miller894926e2013-02-12 11:03:58 +11001770 error("Received disconnect from %s: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001771 ssh_remote_ipaddr(ssh), msg);
1772 free(msg);
1773 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001774 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001775 DBG(debug("received packet type %d", *typep));
1776 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001777 }
Damien Miller33b13562000-04-04 14:38:59 +10001778 }
1779 }
1780}
1781
Damien Miller5428f641999-11-25 11:54:57 +11001782/*
1783 * Buffers the given amount of input characters. This is intended to be used
1784 * together with packet_read_poll.
1785 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001786
1787void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001788ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001789{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001790 struct session_state *state = ssh->state;
1791 int r;
1792
1793 if (state->packet_discard) {
1794 state->keep_alive_timeouts = 0; /* ?? */
1795 if (len >= state->packet_discard) {
1796 if ((r = ssh_packet_stop_discard(ssh)) != 0)
1797 fatal("%s: %s", __func__, ssh_err(r));
1798 cleanup_exit(255);
1799 }
1800 state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001801 return;
1802 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001803 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
1804 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001805}
1806
Damien Miller4af51302000-04-16 11:18:38 +10001807int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001808ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001809{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001810 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001811}
1812
Damien Miller5428f641999-11-25 11:54:57 +11001813/*
1814 * Sends a diagnostic message from the server to the client. This message
1815 * can be sent at any time (but not while constructing another message). The
1816 * message is printed immediately, but only if the client is being executed
1817 * in verbose mode. These messages are primarily intended to ease debugging
1818 * authentication problems. The length of the formatted message must not
1819 * exceed 1024 bytes. This will automatically call packet_write_wait.
1820 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001821
1822void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001823ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001824{
Damien Miller95def091999-11-25 00:26:21 +11001825 char buf[1024];
1826 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001827 int r;
Damien Miller95def091999-11-25 00:26:21 +11001828
markus@openbsd.org091c3022015-01-19 19:52:16 +00001829 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001830 return;
1831
Damien Miller95def091999-11-25 00:26:21 +11001832 va_start(args, fmt);
1833 vsnprintf(buf, sizeof(buf), fmt, args);
1834 va_end(args);
1835
Damien Miller7c8af4f2000-05-01 08:24:07 +10001836 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001837 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1838 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1839 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1840 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1841 (r = sshpkt_send(ssh)) != 0)
1842 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001843 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001844 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1845 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1846 (r = sshpkt_send(ssh)) != 0)
1847 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001848 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001849 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001850}
1851
Damien Miller5428f641999-11-25 11:54:57 +11001852/*
1853 * Logs the error plus constructs and sends a disconnect packet, closes the
1854 * connection, and exits. This function never returns. The error message
1855 * should not contain a newline. The length of the formatted message must
1856 * not exceed 1024 bytes.
1857 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001858
1859void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001860ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001861{
Damien Miller95def091999-11-25 00:26:21 +11001862 char buf[1024];
1863 va_list args;
1864 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001865 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001866
Damien Miller95def091999-11-25 00:26:21 +11001867 if (disconnecting) /* Guard against recursive invocations. */
1868 fatal("packet_disconnect called recursively.");
1869 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001870
Damien Miller5428f641999-11-25 11:54:57 +11001871 /*
1872 * Format the message. Note that the caller must make sure the
1873 * message is of limited size.
1874 */
Damien Miller95def091999-11-25 00:26:21 +11001875 va_start(args, fmt);
1876 vsnprintf(buf, sizeof(buf), fmt, args);
1877 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001879 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001880 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001881
Damien Miller95def091999-11-25 00:26:21 +11001882 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001883 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001884 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
1885 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
1886 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1887 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1888 (r = sshpkt_send(ssh)) != 0)
1889 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001890 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001891 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
1892 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1893 (r = sshpkt_send(ssh)) != 0)
1894 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001895 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001896 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001897
Damien Miller95def091999-11-25 00:26:21 +11001898 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001899 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001900 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001901}
1902
Damien Miller5428f641999-11-25 11:54:57 +11001903/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001904
1905void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001906ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001907{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001908 struct session_state *state = ssh->state;
1909 int len = sshbuf_len(state->output);
1910 int cont, r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001911
Damien Miller95def091999-11-25 00:26:21 +11001912 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001913 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001914 len = roaming_write(state->connection_out,
1915 sshbuf_ptr(state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001916 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001917 if (errno == EINTR || errno == EAGAIN ||
1918 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001919 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001920 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001921 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001922 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001923 fatal("Write connection closed");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001924 if ((r = sshbuf_consume(state->output, len)) != 0)
1925 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001926 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001927}
1928
Damien Miller5428f641999-11-25 11:54:57 +11001929/*
1930 * Calls packet_write_poll repeatedly until all pending output data has been
1931 * written.
1932 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001933
1934void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001935ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001936{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001937 fd_set *setp;
Damien Millerce986542013-07-18 16:12:44 +10001938 int ret, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001939 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001940 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001941
markus@openbsd.org091c3022015-01-19 19:52:16 +00001942 setp = (fd_set *)calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001943 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001944 if (setp == NULL)
1945 fatal("%s: calloc failed", __func__);
1946 ssh_packet_write_poll(ssh);
1947 while (ssh_packet_have_data_to_write(ssh)) {
1948 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001949 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001950 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001951
markus@openbsd.org091c3022015-01-19 19:52:16 +00001952 if (state->packet_timeout_ms > 0) {
1953 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001954 timeoutp = &timeout;
1955 }
1956 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001957 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001958 ms_to_timeval(&timeout, ms_remain);
1959 gettimeofday(&start, NULL);
1960 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001961 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001962 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001963 break;
Damien Millerea437422009-10-02 11:49:03 +10001964 if (errno != EAGAIN && errno != EINTR &&
1965 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001966 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001967 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001968 continue;
1969 ms_subtract_diff(&start, &ms_remain);
1970 if (ms_remain <= 0) {
1971 ret = 0;
1972 break;
1973 }
1974 }
1975 if (ret == 0) {
1976 logit("Connection to %.200s timed out while "
markus@openbsd.org091c3022015-01-19 19:52:16 +00001977 "waiting to write", ssh_remote_ipaddr(ssh));
Darren Tucker3fc464e2008-06-13 06:42:45 +10001978 cleanup_exit(255);
1979 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001980 ssh_packet_write_poll(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001981 }
Darren Tuckera627d422013-06-02 07:31:17 +10001982 free(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001983}
1984
1985/* Returns true if there is buffered data to write to the connection. */
1986
1987int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001988ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001989{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001990 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001991}
1992
1993/* Returns true if there is not too much data to write to the connection. */
1994
1995int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001996ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001997{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001998 if (ssh->state->interactive_mode)
1999 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002000 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002001 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002002}
2003
markus@openbsd.org091c3022015-01-19 19:52:16 +00002004void
2005ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002006{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002007#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002008 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002009 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002010 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002011# ifdef IP_TOS
2012 case AF_INET:
2013 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002014 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002015 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2016 error("setsockopt IP_TOS %d: %.100s:",
2017 tos, strerror(errno));
2018 break;
2019# endif /* IP_TOS */
2020# ifdef IPV6_TCLASS
2021 case AF_INET6:
2022 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002023 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002024 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2025 error("setsockopt IPV6_TCLASS %d: %.100s:",
2026 tos, strerror(errno));
2027 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002028# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002029 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002030#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002031}
Ben Lindstroma7433982002-12-23 02:41:41 +00002032
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002033/* Informs that the current session is interactive. Sets IP flags for that. */
2034
2035void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002036ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002037{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002038 struct session_state *state = ssh->state;
2039
2040 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002041 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002042 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002043
Damien Miller95def091999-11-25 00:26:21 +11002044 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002045 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002046
Damien Miller34132e52000-01-14 15:45:46 +11002047 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002048 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002049 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002050 set_nodelay(state->connection_in);
2051 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2052 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002053}
2054
2055/* Returns true if the current connection is interactive. */
2056
2057int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002058ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002059{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002060 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002061}
Damien Miller6162d121999-11-21 13:23:52 +11002062
Darren Tucker1f8311c2004-05-13 16:39:33 +10002063int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002064ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002065{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002066 struct session_state *state = ssh->state;
2067
2068 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002069 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002070 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002071 return -1;
2072 }
2073 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002074 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002075 return -1;
2076 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002077 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002078 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002079 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002080 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002081}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002082
Darren Tuckerf7288d72009-06-21 18:12:20 +10002083int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002084ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002085{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002086 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002087}
2088
2089void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002090ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002091{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002092 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002093}
2094
2095u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002096ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002097{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002098 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002099}
2100
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002101/*
2102 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002103 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002104 * byte SSH_MSG_IGNORE
2105 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002106 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002107 * All implementations MUST understand (and ignore) this message at any
2108 * time (after receiving the protocol version). No implementation is
2109 * required to send them. This message can be used as an additional
2110 * protection measure against advanced traffic analysis techniques.
2111 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002112void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002113ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002114{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002115 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002116 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002117
markus@openbsd.org091c3022015-01-19 19:52:16 +00002118 if ((r = sshpkt_start(ssh, compat20 ?
2119 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2120 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2121 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002122 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002123 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002124 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002125 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2126 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002127 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002128 }
2129}
Damien Millera5539d22003-04-09 20:50:06 +10002130
Darren Tucker1f8311c2004-05-13 16:39:33 +10002131#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002132int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002133ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002134{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002135 struct session_state *state = ssh->state;
2136
2137 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002138 return 0;
2139 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002140 (state->p_send.packets > MAX_PACKETS) ||
2141 (state->p_read.packets > MAX_PACKETS) ||
2142 (state->max_blocks_out &&
2143 (state->p_send.blocks > state->max_blocks_out)) ||
2144 (state->max_blocks_in &&
2145 (state->p_read.blocks > state->max_blocks_in)) ||
2146 (state->rekey_interval != 0 && state->rekey_time +
2147 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002148}
2149
2150void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002151ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002152{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002153 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
2154 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002155 ssh->state->rekey_limit = bytes;
2156 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002157 /*
2158 * We set the time here so that in post-auth privsep slave we count
2159 * from the completion of the authentication.
2160 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002161 ssh->state->rekey_time = monotime();
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002162}
2163
2164time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002165ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002166{
2167 time_t seconds;
2168
markus@openbsd.org091c3022015-01-19 19:52:16 +00002169 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002170 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002171 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002172}
Damien Miller9786e6e2005-07-26 21:54:56 +10002173
2174void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002175ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002176{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002177 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002178}
2179
2180void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002181ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002182{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002183 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002184}
2185
2186void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002187ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002188{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002189 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002190}
2191
2192void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002193ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002194{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002195 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002196}
2197
markus@openbsd.org091c3022015-01-19 19:52:16 +00002198/* XXX TODO update roaming to new API (does not work anyway) */
Darren Tuckere841eb02009-07-06 07:11:13 +10002199/*
2200 * Save the state for the real connection, and use a separate state when
2201 * resuming a suspended connection.
2202 */
2203void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002204ssh_packet_backup_state(struct ssh *ssh,
2205 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002206{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002207 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002208
markus@openbsd.org091c3022015-01-19 19:52:16 +00002209 close(ssh->state->connection_in);
2210 ssh->state->connection_in = -1;
2211 close(ssh->state->connection_out);
2212 ssh->state->connection_out = -1;
Darren Tuckere841eb02009-07-06 07:11:13 +10002213 if (backup_state)
2214 tmp = backup_state;
2215 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002216 tmp = ssh_alloc_session_state();
2217 backup_state = ssh;
2218 ssh = tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002219}
2220
markus@openbsd.org091c3022015-01-19 19:52:16 +00002221/* XXX FIXME FIXME FIXME */
Darren Tuckere841eb02009-07-06 07:11:13 +10002222/*
2223 * Swap in the old state when resuming a connecion.
2224 */
2225void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002226ssh_packet_restore_state(struct ssh *ssh,
2227 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002228{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002229 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002230 u_int len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002231 int r;
Darren Tuckere841eb02009-07-06 07:11:13 +10002232
2233 tmp = backup_state;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002234 backup_state = ssh;
2235 ssh = tmp;
2236 ssh->state->connection_in = backup_state->state->connection_in;
2237 backup_state->state->connection_in = -1;
2238 ssh->state->connection_out = backup_state->state->connection_out;
2239 backup_state->state->connection_out = -1;
2240 len = sshbuf_len(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002241 if (len > 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002242 if ((r = sshbuf_putb(ssh->state->input,
2243 backup_state->state->input)) != 0)
2244 fatal("%s: %s", __func__, ssh_err(r));
2245 sshbuf_reset(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002246 add_recv_bytes(len);
2247 }
2248}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002249
2250/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002251static int
2252ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002253{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002254 struct sshcomp *comp;
2255 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002256
2257 debug("%s: called", __func__);
2258 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002259 ssh->state->after_authentication = 1;
2260 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002261 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002262 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002263 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002264 comp = &ssh->state->newkeys[mode]->comp;
2265 if (comp && comp->enabled &&
2266 (r = ssh_packet_init_compression(ssh)) != 0)
2267 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002268 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002269 return 0;
2270}
2271
2272/* Packet state (de-)serialization for privsep */
2273
2274/* turn kex into a blob for packet state serialization */
2275static int
2276kex_to_blob(struct sshbuf *m, struct kex *kex)
2277{
2278 int r;
2279
2280 if ((r = sshbuf_put_string(m, kex->session_id,
2281 kex->session_id_len)) != 0 ||
2282 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2283 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2284 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2285 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2286 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2287 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2288 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2289 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2290 return r;
2291 return 0;
2292}
2293
2294/* turn key exchange results into a blob for packet state serialization */
2295static int
2296newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2297{
2298 struct sshbuf *b;
2299 struct sshcipher_ctx *cc;
2300 struct sshcomp *comp;
2301 struct sshenc *enc;
2302 struct sshmac *mac;
2303 struct newkeys *newkey;
2304 int r;
2305
2306 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2307 return SSH_ERR_INTERNAL_ERROR;
2308 enc = &newkey->enc;
2309 mac = &newkey->mac;
2310 comp = &newkey->comp;
2311 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2312 &ssh->state->receive_context;
2313 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2314 return r;
2315 if ((b = sshbuf_new()) == NULL)
2316 return SSH_ERR_ALLOC_FAIL;
2317 /* The cipher struct is constant and shared, you export pointer */
2318 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2319 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2320 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2321 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2322 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2323 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2324 goto out;
2325 if (cipher_authlen(enc->cipher) == 0) {
2326 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2327 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2328 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2329 goto out;
2330 }
2331 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2332 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2333 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2334 goto out;
2335 r = sshbuf_put_stringb(m, b);
2336 out:
2337 if (b != NULL)
2338 sshbuf_free(b);
2339 return r;
2340}
2341
2342/* serialize packet state into a blob */
2343int
2344ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2345{
2346 struct session_state *state = ssh->state;
2347 u_char *p;
2348 size_t slen, rlen;
2349 int r, ssh1cipher;
2350
2351 if (!compat20) {
2352 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2353 slen = cipher_get_keyiv_len(&state->send_context);
2354 rlen = cipher_get_keyiv_len(&state->receive_context);
2355 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2356 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2357 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2358 (r = sshbuf_put_u32(m, slen)) != 0 ||
2359 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2360 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2361 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2362 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2363 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2364 return r;
2365 } else {
2366 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2367 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2368 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
2369 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2370 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2371 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2372 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2373 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2374 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2375 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2376 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2377 return r;
2378 }
2379
2380 slen = cipher_get_keycontext(&state->send_context, NULL);
2381 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2382 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2383 (r = sshbuf_reserve(m, slen, &p)) != 0)
2384 return r;
2385 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2386 return SSH_ERR_INTERNAL_ERROR;
2387 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2388 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2389 return r;
2390 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2391 return SSH_ERR_INTERNAL_ERROR;
2392
2393 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2394 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2395 (r = sshbuf_put_stringb(m, state->output)) != 0)
2396 return r;
2397
2398 if (compat20) {
2399 if ((r = sshbuf_put_u64(m, get_sent_bytes())) != 0 ||
2400 (r = sshbuf_put_u64(m, get_recv_bytes())) != 0)
2401 return r;
2402 }
2403 return 0;
2404}
2405
2406/* restore key exchange results from blob for packet state de-serialization */
2407static int
2408newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2409{
2410 struct sshbuf *b = NULL;
2411 struct sshcomp *comp;
2412 struct sshenc *enc;
2413 struct sshmac *mac;
2414 struct newkeys *newkey = NULL;
2415 size_t keylen, ivlen, maclen;
2416 int r;
2417
2418 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2419 r = SSH_ERR_ALLOC_FAIL;
2420 goto out;
2421 }
2422 if ((r = sshbuf_froms(m, &b)) != 0)
2423 goto out;
2424#ifdef DEBUG_PK
2425 sshbuf_dump(b, stderr);
2426#endif
2427 enc = &newkey->enc;
2428 mac = &newkey->mac;
2429 comp = &newkey->comp;
2430
2431 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2432 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2433 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2434 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2435 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2436 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2437 goto out;
2438 if (cipher_authlen(enc->cipher) == 0) {
2439 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2440 goto out;
2441 if ((r = mac_setup(mac, mac->name)) != 0)
2442 goto out;
2443 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2444 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2445 goto out;
2446 if (maclen > mac->key_len) {
2447 r = SSH_ERR_INVALID_FORMAT;
2448 goto out;
2449 }
2450 mac->key_len = maclen;
2451 }
2452 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2453 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2454 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2455 goto out;
2456 if (enc->name == NULL ||
2457 cipher_by_name(enc->name) != enc->cipher) {
2458 r = SSH_ERR_INVALID_FORMAT;
2459 goto out;
2460 }
2461 if (sshbuf_len(b) != 0) {
2462 r = SSH_ERR_INVALID_FORMAT;
2463 goto out;
2464 }
2465 enc->key_len = keylen;
2466 enc->iv_len = ivlen;
2467 ssh->kex->newkeys[mode] = newkey;
2468 newkey = NULL;
2469 r = 0;
2470 out:
2471 if (newkey != NULL)
2472 free(newkey);
2473 if (b != NULL)
2474 sshbuf_free(b);
2475 return r;
2476}
2477
2478/* restore kex from blob for packet state de-serialization */
2479static int
2480kex_from_blob(struct sshbuf *m, struct kex **kexp)
2481{
2482 struct kex *kex;
2483 int r;
2484
2485 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2486 (kex->my = sshbuf_new()) == NULL ||
2487 (kex->peer = sshbuf_new()) == NULL) {
2488 r = SSH_ERR_ALLOC_FAIL;
2489 goto out;
2490 }
2491 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2492 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2493 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2494 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2495 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2496 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2497 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2498 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2499 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2500 goto out;
2501 kex->server = 1;
2502 kex->done = 1;
2503 r = 0;
2504 out:
2505 if (r != 0 || kexp == NULL) {
2506 if (kex != NULL) {
2507 if (kex->my != NULL)
2508 sshbuf_free(kex->my);
2509 if (kex->peer != NULL)
2510 sshbuf_free(kex->peer);
2511 free(kex);
2512 }
2513 if (kexp != NULL)
2514 *kexp = NULL;
2515 } else {
2516 *kexp = kex;
2517 }
2518 return r;
2519}
2520
2521/*
2522 * Restore packet state from content of blob 'm' (de-serialization).
2523 * Note that 'm' will be partially consumed on parsing or any other errors.
2524 */
2525int
2526ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2527{
2528 struct session_state *state = ssh->state;
2529 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2530 size_t ssh1keylen, rlen, slen, ilen, olen;
2531 int r;
2532 u_int ssh1cipher = 0;
2533 u_int64_t sent_bytes = 0, recv_bytes = 0;
2534
2535 if (!compat20) {
2536 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2537 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2538 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2539 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2540 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2541 return r;
2542 if (ssh1cipher > INT_MAX)
2543 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2544 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2545 (int)ssh1cipher);
2546 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2547 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2548 return SSH_ERR_INVALID_FORMAT;
2549 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2550 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2551 return r;
2552 } else {
2553 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2554 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2555 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
2556 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2557 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2558 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2559 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2560 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2561 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2562 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2563 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2564 return r;
2565 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2566 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2567 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2568 return r;
2569 }
2570 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2571 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2572 return r;
2573 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2574 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2575 return SSH_ERR_INVALID_FORMAT;
2576 cipher_set_keycontext(&state->send_context, keyout);
2577 cipher_set_keycontext(&state->receive_context, keyin);
2578
2579 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2580 (r = ssh_packet_set_postauth(ssh)) != 0)
2581 return r;
2582
2583 sshbuf_reset(state->input);
2584 sshbuf_reset(state->output);
2585 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2586 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2587 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2588 (r = sshbuf_put(state->output, output, olen)) != 0)
2589 return r;
2590
2591 if (compat20) {
2592 if ((r = sshbuf_get_u64(m, &sent_bytes)) != 0 ||
2593 (r = sshbuf_get_u64(m, &recv_bytes)) != 0)
2594 return r;
2595 roam_set_bytes(sent_bytes, recv_bytes);
2596 }
2597 if (sshbuf_len(m))
2598 return SSH_ERR_INVALID_FORMAT;
2599 debug3("%s: done", __func__);
2600 return 0;
2601}
2602
2603/* NEW API */
2604
2605/* put data to the outgoing packet */
2606
2607int
2608sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2609{
2610 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2611}
2612
2613int
2614sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2615{
2616 return sshbuf_putb(ssh->state->outgoing_packet, b);
2617}
2618
2619int
2620sshpkt_put_u8(struct ssh *ssh, u_char val)
2621{
2622 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2623}
2624
2625int
2626sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2627{
2628 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2629}
2630
2631int
2632sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2633{
2634 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2635}
2636
2637int
2638sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2639{
2640 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2641}
2642
2643int
2644sshpkt_put_cstring(struct ssh *ssh, const void *v)
2645{
2646 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2647}
2648
2649int
2650sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2651{
2652 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2653}
2654
2655int
2656sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2657{
2658 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2659}
2660
2661int
2662sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2663{
2664 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2665}
2666
2667int
2668sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2669{
2670 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2671}
2672
2673/* fetch data from the incoming packet */
2674
2675int
2676sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2677{
2678 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2679}
2680
2681int
2682sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2683{
2684 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2685}
2686
2687int
2688sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2689{
2690 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2691}
2692
2693int
2694sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2695{
2696 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2697}
2698
2699int
2700sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2701{
2702 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2703}
2704
2705int
2706sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2707{
2708 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2709}
2710
2711int
2712sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2713{
2714 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2715}
2716
2717int
2718sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2719{
2720 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2721}
2722
2723int
2724sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2725{
2726 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2727}
2728
2729int
2730sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2731{
2732 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2733}
2734
2735int
2736sshpkt_get_end(struct ssh *ssh)
2737{
2738 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2739 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2740 return 0;
2741}
2742
2743const u_char *
2744sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2745{
2746 if (lenp != NULL)
2747 *lenp = sshbuf_len(ssh->state->incoming_packet);
2748 return sshbuf_ptr(ssh->state->incoming_packet);
2749}
2750
2751/* start a new packet */
2752
2753int
2754sshpkt_start(struct ssh *ssh, u_char type)
2755{
2756 u_char buf[9];
2757 int len;
2758
2759 DBG(debug("packet_start[%d]", type));
2760 len = compat20 ? 6 : 9;
2761 memset(buf, 0, len - 1);
2762 buf[len - 1] = type;
2763 sshbuf_reset(ssh->state->outgoing_packet);
2764 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2765}
2766
2767/* send it */
2768
2769int
2770sshpkt_send(struct ssh *ssh)
2771{
2772 if (compat20)
2773 return ssh_packet_send2(ssh);
2774 else
2775 return ssh_packet_send1(ssh);
2776}
2777
2778int
2779sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2780{
2781 char buf[1024];
2782 va_list args;
2783 int r;
2784
2785 va_start(args, fmt);
2786 vsnprintf(buf, sizeof(buf), fmt, args);
2787 va_end(args);
2788
2789 if (compat20) {
2790 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2791 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2792 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2793 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2794 (r = sshpkt_send(ssh)) != 0)
2795 return r;
2796 } else {
2797 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2798 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2799 (r = sshpkt_send(ssh)) != 0)
2800 return r;
2801 }
2802 return 0;
2803}
2804
2805/* roundup current message to pad bytes */
2806int
2807sshpkt_add_padding(struct ssh *ssh, u_char pad)
2808{
2809 ssh->state->extra_pad = pad;
2810 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002811}