blob: 2c8d8aa9b9c4324647cbb004de20e193040c8c88 [file] [log] [blame]
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001/* $OpenBSD: packet.c,v 1.203 2015/01/20 23:14:00 deraadt 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
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000042#include <sys/param.h> /* MIN roundup */
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100044#include "openbsd-compat/sys-queue.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>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000060#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100061#include <signal.h>
Darren Tuckerc53c2af2013-05-16 20:28:16 +100062#include <time.h>
Darren Tucker5d196262006-07-12 22:15:16 +100063
markus@openbsd.org091c3022015-01-19 19:52:16 +000064#include <zlib.h>
65
66#include "buffer.h" /* typedefs XXX */
67#include "key.h" /* typedefs XXX */
68
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "deattack.h"
Damien Miller33b13562000-04-04 14:38:59 +100072#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100074#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110075#include "cipher.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000076#include "sshkey.h"
Damien Miller33b13562000-04-04 14:38:59 +100077#include "kex.h"
markus@openbsd.org128343b2015-01-13 19:31:40 +000078#include "digest.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000079#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "log.h"
81#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110082#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100083#include "channels.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000084#include "ssh.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000085#include "packet.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100086#include "roaming.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000087#include "ssherr.h"
88#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100089
90#ifdef PACKET_DEBUG
91#define DBG(x) x
92#else
93#define DBG(x)
94#endif
95
Damien Miller13ae44c2009-01-28 16:38:41 +110096#define PACKET_MAX_SIZE (256 * 1024)
97
Darren Tuckerf7288d72009-06-21 18:12:20 +100098struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100099 u_int32_t seqnr;
100 u_int32_t packets;
101 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000102 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000103};
Damien Miller13ae44c2009-01-28 16:38:41 +1100104
Damien Millera5539d22003-04-09 20:50:06 +1000105struct packet {
106 TAILQ_ENTRY(packet) next;
107 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000108 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000109};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000110
111struct session_state {
112 /*
113 * This variable contains the file descriptors used for
114 * communicating with the other side. connection_in is used for
115 * reading; connection_out for writing. These can be the same
116 * descriptor, in which case it is assumed to be a socket.
117 */
118 int connection_in;
119 int connection_out;
120
121 /* Protocol flags for the remote side. */
122 u_int remote_protocol_flags;
123
124 /* Encryption context for receiving data. Only used for decryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000125 struct sshcipher_ctx receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000126
127 /* Encryption context for sending data. Only used for encryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000128 struct sshcipher_ctx send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000129
130 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000131 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000132
133 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000134 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000135
136 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000137 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000138
139 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000140 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000141
142 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000143 struct sshbuf *compression_buffer;
144
145 /* Incoming/outgoing compression dictionaries */
146 z_stream compression_in_stream;
147 z_stream compression_out_stream;
148 int compression_in_started;
149 int compression_out_started;
150 int compression_in_failures;
151 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000152
153 /*
154 * Flag indicating whether packet compression/decompression is
155 * enabled.
156 */
157 int packet_compression;
158
159 /* default maximum packet size */
160 u_int max_packet_size;
161
162 /* Flag indicating whether this module has been initialized. */
163 int initialized;
164
165 /* Set to true if the connection is interactive. */
166 int interactive_mode;
167
168 /* Set to true if we are the server side. */
169 int server_side;
170
171 /* Set to true if we are authenticated. */
172 int after_authentication;
173
174 int keep_alive_timeouts;
175
176 /* The maximum time that we will wait to send or receive a packet */
177 int packet_timeout_ms;
178
179 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000180 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000181 struct packet_state p_read, p_send;
182
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000183 /* Volume-based rekeying */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000184 u_int64_t max_blocks_in, max_blocks_out;
185 u_int32_t rekey_limit;
186
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000187 /* Time-based rekeying */
188 time_t rekey_interval; /* how often in seconds */
189 time_t rekey_time; /* time of last rekeying */
190
Darren Tuckerf7288d72009-06-21 18:12:20 +1000191 /* Session key for protocol v1 */
192 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
193 u_int ssh1_keylen;
194
195 /* roundup current message to extra_pad bytes */
196 u_char extra_pad;
197
198 /* XXX discard incoming data after MAC error */
199 u_int packet_discard;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000200 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000201
202 /* Used in packet_read_poll2() */
203 u_int packlen;
204
Darren Tucker7b935c72009-06-21 18:59:36 +1000205 /* Used in packet_send2 */
206 int rekeying;
207
208 /* Used in packet_set_interactive */
209 int set_interactive_called;
210
211 /* Used in packet_set_maxsize */
212 int set_maxsize_called;
213
markus@openbsd.org091c3022015-01-19 19:52:16 +0000214 /* One-off warning about weak ciphers */
215 int cipher_warning_done;
216
217 /* SSH1 CRC compensation attack detector */
218 struct deattack_ctx deattack;
219
Darren Tuckerf7288d72009-06-21 18:12:20 +1000220 TAILQ_HEAD(, packet) outgoing;
221};
222
markus@openbsd.org091c3022015-01-19 19:52:16 +0000223struct ssh *
224ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000225{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000226 struct ssh *ssh = NULL;
227 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000228
markus@openbsd.org091c3022015-01-19 19:52:16 +0000229 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
230 (state = calloc(1, sizeof(*state))) == NULL ||
231 (state->input = sshbuf_new()) == NULL ||
232 (state->output = sshbuf_new()) == NULL ||
233 (state->outgoing_packet = sshbuf_new()) == NULL ||
234 (state->incoming_packet = sshbuf_new()) == NULL)
235 goto fail;
236 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000237 TAILQ_INIT(&ssh->private_keys);
238 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000239 state->connection_in = -1;
240 state->connection_out = -1;
241 state->max_packet_size = 32768;
242 state->packet_timeout_ms = -1;
243 state->p_send.packets = state->p_read.packets = 0;
244 state->initialized = 1;
245 /*
246 * ssh_packet_send2() needs to queue packets until
247 * we've done the initial key exchange.
248 */
249 state->rekeying = 1;
250 ssh->state = state;
251 return ssh;
252 fail:
253 if (state) {
254 sshbuf_free(state->input);
255 sshbuf_free(state->output);
256 sshbuf_free(state->incoming_packet);
257 sshbuf_free(state->outgoing_packet);
258 free(state);
259 }
260 free(ssh);
261 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000262}
Damien Millera5539d22003-04-09 20:50:06 +1000263
Damien Miller5428f641999-11-25 11:54:57 +1100264/*
265 * Sets the descriptors used for communication. Disables encryption until
266 * packet_set_encryption_key is called.
267 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000268struct ssh *
269ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000271 struct session_state *state;
272 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000273 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000274
Damien Miller874d77b2000-10-14 16:23:11 +1100275 if (none == NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000276 fatal("%s: cannot load cipher 'none'", __func__);
277 if (ssh == NULL)
278 ssh = ssh_alloc_session_state();
279 if (ssh == NULL)
280 fatal("%s: cound not allocate state", __func__);
281 state = ssh->state;
282 state->connection_in = fd_in;
283 state->connection_out = fd_out;
284 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000285 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000286 (r = cipher_init(&state->receive_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000287 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000288 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
289 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
290 deattack_init(&state->deattack);
291 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292}
293
Darren Tucker3fc464e2008-06-13 06:42:45 +1000294void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000295ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000296{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000297 struct session_state *state = ssh->state;
298
Damien Miller8ed4de82011-12-19 10:52:50 +1100299 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000300 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000301 return;
302 }
303 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000304 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000305 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000306 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000307}
308
markus@openbsd.org091c3022015-01-19 19:52:16 +0000309int
310ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100311{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000312 struct session_state *state = ssh->state;
313 int r;
314
315 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100316 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000317
Damien Miller13ae44c2009-01-28 16:38:41 +1100318 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000319 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000320 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000321 if ((r = sshbuf_put(state->incoming_packet, buf,
322 sizeof(buf))) != 0)
323 return r;
324 (void) mac_compute(state->packet_discard_mac,
325 state->p_read.seqnr,
326 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
327 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100328 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000329 logit("Finished discarding for %.200s", ssh_remote_ipaddr(ssh));
330 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100331}
332
markus@openbsd.org091c3022015-01-19 19:52:16 +0000333static int
334ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
335 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100336{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000337 struct session_state *state = ssh->state;
338 int r;
339
340 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
341 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
342 return r;
343 return SSH_ERR_MAC_INVALID;
344 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100345 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000346 state->packet_discard_mac = mac;
347 if (sshbuf_len(state->input) >= discard &&
348 (r = ssh_packet_stop_discard(ssh)) != 0)
349 return r;
350 state->packet_discard = discard - sshbuf_len(state->input);
351 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100352}
353
Damien Miller34132e52000-01-14 15:45:46 +1100354/* Returns 1 if remote host is connected via socket, 0 if not. */
355
356int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000357ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100358{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000359 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100360 struct sockaddr_storage from, to;
361 socklen_t fromlen, tolen;
362
363 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000364 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100365 return 1;
366 fromlen = sizeof(from);
367 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000368 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000369 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100370 return 0;
371 tolen = sizeof(to);
372 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000373 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000374 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100375 return 0;
376 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
377 return 0;
378 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
379 return 0;
380 return 1;
381}
382
Ben Lindstromf6027d32002-03-22 01:42:04 +0000383void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000384ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000385{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000386 if (ibytes)
387 *ibytes = ssh->state->p_read.bytes;
388 if (obytes)
389 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000390}
391
392int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000393ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100394{
395 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100396 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100397
398 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000399 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000400 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100401 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000402#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100403 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000404 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000405 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000406#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000407 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100408}
409
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410/* Sets the connection into non-blocking mode. */
411
412void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000413ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414{
Damien Miller95def091999-11-25 00:26:21 +1100415 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000416 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
markus@openbsd.org091c3022015-01-19 19:52:16 +0000418 if (ssh->state->connection_out != ssh->state->connection_in)
419 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420}
421
422/* Returns the socket used for reading. */
423
424int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000425ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000427 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428}
429
430/* Returns the descriptor used for writing. */
431
432int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000433ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000435 return ssh->state->connection_out;
436}
437
438/*
439 * Returns the IP-address of the remote host as a string. The returned
440 * string must not be freed.
441 */
442
443const char *
444ssh_remote_ipaddr(struct ssh *ssh)
445{
446 /* Check whether we have cached the ipaddr. */
447 if (ssh->remote_ipaddr == NULL)
448 ssh->remote_ipaddr = ssh_packet_connection_is_on_socket(ssh) ?
449 get_peer_ipaddr(ssh->state->connection_in) :
450 strdup("UNKNOWN");
451 if (ssh->remote_ipaddr == NULL)
452 return "UNKNOWN";
453 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454}
455
456/* Closes the connection and clears and frees internal data structures. */
457
458void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000459ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000461 struct session_state *state = ssh->state;
462 int r;
463 u_int mode;
464
465 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100466 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000467 state->initialized = 0;
468 if (state->connection_in == state->connection_out) {
469 shutdown(state->connection_out, SHUT_RDWR);
470 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100471 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000472 close(state->connection_in);
473 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100474 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000475 sshbuf_free(state->input);
476 sshbuf_free(state->output);
477 sshbuf_free(state->outgoing_packet);
478 sshbuf_free(state->incoming_packet);
479 for (mode = 0; mode < MODE_MAX; mode++)
480 kex_free_newkeys(state->newkeys[mode]);
481 if (state->compression_buffer) {
482 sshbuf_free(state->compression_buffer);
483 if (state->compression_out_started) {
484 z_streamp stream = &state->compression_out_stream;
485 debug("compress outgoing: "
486 "raw data %llu, compressed %llu, factor %.2f",
487 (unsigned long long)stream->total_in,
488 (unsigned long long)stream->total_out,
489 stream->total_in == 0 ? 0.0 :
490 (double) stream->total_out / stream->total_in);
491 if (state->compression_out_failures == 0)
492 deflateEnd(stream);
493 }
494 if (state->compression_in_started) {
495 z_streamp stream = &state->compression_out_stream;
496 debug("compress incoming: "
497 "raw data %llu, compressed %llu, factor %.2f",
498 (unsigned long long)stream->total_out,
499 (unsigned long long)stream->total_in,
500 stream->total_out == 0 ? 0.0 :
501 (double) stream->total_in / stream->total_out);
502 if (state->compression_in_failures == 0)
503 inflateEnd(stream);
504 }
Damien Miller95def091999-11-25 00:26:21 +1100505 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000506 if ((r = cipher_cleanup(&state->send_context)) != 0)
507 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
508 if ((r = cipher_cleanup(&state->receive_context)) != 0)
509 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
510 if (ssh->remote_ipaddr) {
511 free(ssh->remote_ipaddr);
512 ssh->remote_ipaddr = NULL;
513 }
514 free(ssh->state);
515 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516}
517
518/* Sets remote side protocol flags. */
519
520void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000521ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000523 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524}
525
526/* Returns the remote protocol flags set earlier by the above function. */
527
Ben Lindstrom46c16222000-12-22 01:43:59 +0000528u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000529ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000531 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532}
533
Damien Miller5428f641999-11-25 11:54:57 +1100534/*
535 * Starts packet compression from the next packet on in both directions.
536 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
537 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
markus@openbsd.org091c3022015-01-19 19:52:16 +0000539static int
540ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000541{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000542 if (!ssh->state->compression_buffer &&
543 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
544 return SSH_ERR_ALLOC_FAIL;
545 return 0;
546}
547
548static int
549start_compression_out(struct ssh *ssh, int level)
550{
551 if (level < 1 || level > 9)
552 return SSH_ERR_INVALID_ARGUMENT;
553 debug("Enabling compression at level %d.", level);
554 if (ssh->state->compression_out_started == 1)
555 deflateEnd(&ssh->state->compression_out_stream);
556 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
557 case Z_OK:
558 ssh->state->compression_out_started = 1;
559 break;
560 case Z_MEM_ERROR:
561 return SSH_ERR_ALLOC_FAIL;
562 default:
563 return SSH_ERR_INTERNAL_ERROR;
564 }
565 return 0;
566}
567
568static int
569start_compression_in(struct ssh *ssh)
570{
571 if (ssh->state->compression_in_started == 1)
572 inflateEnd(&ssh->state->compression_in_stream);
573 switch (inflateInit(&ssh->state->compression_in_stream)) {
574 case Z_OK:
575 ssh->state->compression_in_started = 1;
576 break;
577 case Z_MEM_ERROR:
578 return SSH_ERR_ALLOC_FAIL;
579 default:
580 return SSH_ERR_INTERNAL_ERROR;
581 }
582 return 0;
583}
584
585int
586ssh_packet_start_compression(struct ssh *ssh, int level)
587{
588 int r;
589
590 if (ssh->state->packet_compression && !compat20)
591 return SSH_ERR_INTERNAL_ERROR;
592 ssh->state->packet_compression = 1;
593 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
594 (r = start_compression_in(ssh)) != 0 ||
595 (r = start_compression_out(ssh, level)) != 0)
596 return r;
597 return 0;
598}
599
600/* XXX remove need for separate compression buffer */
601static int
602compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
603{
604 u_char buf[4096];
605 int r, status;
606
607 if (ssh->state->compression_out_started != 1)
608 return SSH_ERR_INTERNAL_ERROR;
609
610 /* This case is not handled below. */
611 if (sshbuf_len(in) == 0)
612 return 0;
613
614 /* Input is the contents of the input buffer. */
615 if ((ssh->state->compression_out_stream.next_in =
616 sshbuf_mutable_ptr(in)) == NULL)
617 return SSH_ERR_INTERNAL_ERROR;
618 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
619
620 /* Loop compressing until deflate() returns with avail_out != 0. */
621 do {
622 /* Set up fixed-size output buffer. */
623 ssh->state->compression_out_stream.next_out = buf;
624 ssh->state->compression_out_stream.avail_out = sizeof(buf);
625
626 /* Compress as much data into the buffer as possible. */
627 status = deflate(&ssh->state->compression_out_stream,
628 Z_PARTIAL_FLUSH);
629 switch (status) {
630 case Z_MEM_ERROR:
631 return SSH_ERR_ALLOC_FAIL;
632 case Z_OK:
633 /* Append compressed data to output_buffer. */
634 if ((r = sshbuf_put(out, buf, sizeof(buf) -
635 ssh->state->compression_out_stream.avail_out)) != 0)
636 return r;
637 break;
638 case Z_STREAM_ERROR:
639 default:
640 ssh->state->compression_out_failures++;
641 return SSH_ERR_INVALID_FORMAT;
642 }
643 } while (ssh->state->compression_out_stream.avail_out == 0);
644 return 0;
645}
646
647static int
648uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
649{
650 u_char buf[4096];
651 int r, status;
652
653 if (ssh->state->compression_in_started != 1)
654 return SSH_ERR_INTERNAL_ERROR;
655
656 if ((ssh->state->compression_in_stream.next_in =
657 sshbuf_mutable_ptr(in)) == NULL)
658 return SSH_ERR_INTERNAL_ERROR;
659 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
660
661 for (;;) {
662 /* Set up fixed-size output buffer. */
663 ssh->state->compression_in_stream.next_out = buf;
664 ssh->state->compression_in_stream.avail_out = sizeof(buf);
665
666 status = inflate(&ssh->state->compression_in_stream,
667 Z_PARTIAL_FLUSH);
668 switch (status) {
669 case Z_OK:
670 if ((r = sshbuf_put(out, buf, sizeof(buf) -
671 ssh->state->compression_in_stream.avail_out)) != 0)
672 return r;
673 break;
674 case Z_BUF_ERROR:
675 /*
676 * Comments in zlib.h say that we should keep calling
677 * inflate() until we get an error. This appears to
678 * be the error that we get.
679 */
680 return 0;
681 case Z_DATA_ERROR:
682 return SSH_ERR_INVALID_FORMAT;
683 case Z_MEM_ERROR:
684 return SSH_ERR_ALLOC_FAIL;
685 case Z_STREAM_ERROR:
686 default:
687 ssh->state->compression_in_failures++;
688 return SSH_ERR_INTERNAL_ERROR;
689 }
690 }
691 /* NOTREACHED */
692}
693
694/* Serialise compression state into a blob for privsep */
695static int
696ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
697{
698 struct session_state *state = ssh->state;
699 struct sshbuf *b;
700 int r;
701
702 if ((b = sshbuf_new()) == NULL)
703 return SSH_ERR_ALLOC_FAIL;
704 if (state->compression_in_started) {
705 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
706 sizeof(state->compression_in_stream))) != 0)
707 goto out;
708 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
709 goto out;
710 if (state->compression_out_started) {
711 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
712 sizeof(state->compression_out_stream))) != 0)
713 goto out;
714 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
715 goto out;
716 r = sshbuf_put_stringb(m, b);
717 out:
718 sshbuf_free(b);
719 return r;
720}
721
722/* Deserialise compression state from a blob for privsep */
723static int
724ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
725{
726 struct session_state *state = ssh->state;
727 struct sshbuf *b = NULL;
728 int r;
729 const u_char *inblob, *outblob;
730 size_t inl, outl;
731
732 if ((r = sshbuf_froms(m, &b)) != 0)
733 goto out;
734 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
735 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
736 goto out;
737 if (inl == 0)
738 state->compression_in_started = 0;
739 else if (inl != sizeof(state->compression_in_stream)) {
740 r = SSH_ERR_INTERNAL_ERROR;
741 goto out;
742 } else {
743 state->compression_in_started = 1;
744 memcpy(&state->compression_in_stream, inblob, inl);
745 }
746 if (outl == 0)
747 state->compression_out_started = 0;
748 else if (outl != sizeof(state->compression_out_stream)) {
749 r = SSH_ERR_INTERNAL_ERROR;
750 goto out;
751 } else {
752 state->compression_out_started = 1;
753 memcpy(&state->compression_out_stream, outblob, outl);
754 }
755 r = 0;
756 out:
757 sshbuf_free(b);
758 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000759}
760
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000761void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000762ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
763 void *(*allocfunc)(void *, u_int, u_int),
764 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000766 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
767 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
768 ssh->state->compression_out_stream.opaque = ctx;
769 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
770 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
771 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000772}
773
Damien Miller5428f641999-11-25 11:54:57 +1100774/*
Damien Miller5428f641999-11-25 11:54:57 +1100775 * Causes any further packets to be encrypted using the given key. The same
776 * key is used for both sending and reception. However, both directions are
777 * encrypted independently of each other.
778 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000779
Damien Miller1f0311c2014-05-15 14:24:09 +1000780#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000782ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000784 struct session_state *state = ssh->state;
785 const struct sshcipher *cipher = cipher_by_number(number);
786 int r;
787 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100788
markus@openbsd.org091c3022015-01-19 19:52:16 +0000789 if (cipher == NULL)
790 fatal("%s: unknown cipher number %d", __func__, number);
791 if (keylen < 20)
792 fatal("%s: keylen too small: %d", __func__, keylen);
793 if (keylen > SSH_SESSION_KEY_LENGTH)
794 fatal("%s: keylen too big: %d", __func__, keylen);
795 memcpy(state->ssh1_key, key, keylen);
796 state->ssh1_keylen = keylen;
797 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
798 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
799 (r = cipher_init(&state->receive_context, cipher, key, keylen,
800 NULL, 0, CIPHER_DECRYPT) != 0))
801 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
802 if (!state->cipher_warning_done &&
803 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
804 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
805 error("Warning: %s", wmsg);
806 state->cipher_warning_done = 1;
807 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000808}
Damien Miller6af914a2010-09-10 11:39:26 +1000809#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000810
Damien Miller5428f641999-11-25 11:54:57 +1100811/*
812 * Finalizes and sends the packet. If the encryption key has been set,
813 * encrypts the packet before sending.
814 */
Damien Miller95def091999-11-25 00:26:21 +1100815
markus@openbsd.org091c3022015-01-19 19:52:16 +0000816int
817ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000819 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000820 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000821 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000822 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823
Damien Miller5428f641999-11-25 11:54:57 +1100824 /*
825 * If using packet compression, compress the payload of the outgoing
826 * packet.
827 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000828 if (state->packet_compression) {
829 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100830 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000831 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
832 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100833 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000834 if ((r = sshbuf_put(state->compression_buffer,
835 "\0\0\0\0\0\0\0\0", 8)) != 0)
836 goto out;
837 if ((r = compress_buffer(ssh, state->outgoing_packet,
838 state->compression_buffer)) != 0)
839 goto out;
840 sshbuf_reset(state->outgoing_packet);
841 if ((r = sshbuf_putb(state->outgoing_packet,
842 state->compression_buffer)) != 0)
843 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100844 }
845 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000846 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847
Damien Millere247cc42000-05-07 12:03:14 +1000848 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100849 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000850 if (!state->send_context.plaintext) {
851 cp = sshbuf_mutable_ptr(state->outgoing_packet);
852 if (cp == NULL) {
853 r = SSH_ERR_INTERNAL_ERROR;
854 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100855 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000856 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100857 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000858 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
859 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100860
861 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000862 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
863 sshbuf_len(state->outgoing_packet));
864 POKE_U32(buf, checksum);
865 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
866 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867
868#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100869 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000870 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871#endif
872
Damien Miller95def091999-11-25 00:26:21 +1100873 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000874 POKE_U32(buf, len);
875 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
876 goto out;
877 if ((r = sshbuf_reserve(state->output,
878 sshbuf_len(state->outgoing_packet), &cp)) != 0)
879 goto out;
880 if ((r = cipher_crypt(&state->send_context, 0, cp,
881 sshbuf_ptr(state->outgoing_packet),
882 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
883 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100884
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100886 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000887 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000889 state->p_send.packets++;
890 state->p_send.bytes += len +
891 sshbuf_len(state->outgoing_packet);
892 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller5428f641999-11-25 11:54:57 +1100894 /*
Damien Miller788f2122005-11-05 15:14:59 +1100895 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100896 * actually sent until packet_write_wait or packet_write_poll is
897 * called.
898 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000899 r = 0;
900 out:
901 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902}
903
markus@openbsd.org091c3022015-01-19 19:52:16 +0000904int
905ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000906{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000907 struct session_state *state = ssh->state;
908 struct sshenc *enc;
909 struct sshmac *mac;
910 struct sshcomp *comp;
911 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000912 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000913 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000914 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000915
Ben Lindstrom064496f2002-12-23 02:04:22 +0000916 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000917
Damien Miller963f6b22002-02-19 15:21:23 +1100918 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000919 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000920 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000921 state->p_send.packets = state->p_send.blocks = 0;
922 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100923 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000924 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000925 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000926 state->p_read.packets = state->p_read.blocks = 0;
927 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100928 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000929 if (state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000930 debug("set_newkeys: rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000931 if ((r = cipher_cleanup(cc)) != 0)
932 return r;
933 enc = &state->newkeys[mode]->enc;
934 mac = &state->newkeys[mode]->mac;
935 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000936 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100937 explicit_bzero(enc->iv, enc->iv_len);
938 explicit_bzero(enc->key, enc->key_len);
939 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000940 free(enc->name);
941 free(enc->iv);
942 free(enc->key);
943 free(mac->name);
944 free(mac->key);
945 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000946 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000947 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000948 /* move newkeys from kex to state */
949 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
950 return SSH_ERR_INTERNAL_ERROR;
951 ssh->kex->newkeys[mode] = NULL;
952 enc = &state->newkeys[mode]->enc;
953 mac = &state->newkeys[mode]->mac;
954 comp = &state->newkeys[mode]->comp;
955 if (cipher_authlen(enc->cipher) == 0) {
956 if ((r = mac_init(mac)) != 0)
957 return r;
958 }
959 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000960 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000961 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
962 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000963 return r;
964 if (!state->cipher_warning_done &&
965 (wmsg = cipher_warning_message(cc)) != NULL) {
966 error("Warning: %s", wmsg);
967 state->cipher_warning_done = 1;
968 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000969 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100970 /* explicit_bzero(enc->iv, enc->block_size);
971 explicit_bzero(enc->key, enc->key_len);
972 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000973 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000974 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +0000975 state->after_authentication)) && comp->enabled == 0) {
976 if ((r = ssh_packet_init_compression(ssh)) < 0)
977 return r;
978 if (mode == MODE_OUT) {
979 if ((r = start_compression_out(ssh, 6)) != 0)
980 return r;
981 } else {
982 if ((r = start_compression_in(ssh)) != 0)
983 return r;
984 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000985 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000986 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000987 /*
988 * The 2^(blocksize*2) limit is too expensive for 3DES,
989 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
990 */
991 if (enc->block_size >= 16)
992 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
993 else
994 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000995 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000996 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +0000997 state->rekey_limit / enc->block_size);
998 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000999}
1000
Damien Miller5428f641999-11-25 11:54:57 +11001001/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001002 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001003 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001004 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1005 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001006static int
1007ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001008{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001009 struct session_state *state = ssh->state;
1010 struct sshcomp *comp = NULL;
1011 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001012
1013 /*
1014 * Remember that we are past the authentication step, so rekeying
1015 * with COMP_DELAYED will turn on compression immediately.
1016 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001017 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001018 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001019 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001020 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001021 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001022 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001023 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001024 if ((r = ssh_packet_init_compression(ssh)) != 0)
1025 return r;
1026 if (mode == MODE_OUT) {
1027 if ((r = start_compression_out(ssh, 6)) != 0)
1028 return r;
1029 } else {
1030 if ((r = start_compression_in(ssh)) != 0)
1031 return r;
1032 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001033 comp->enabled = 1;
1034 }
1035 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001036 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001037}
1038
1039/*
Damien Miller33b13562000-04-04 14:38:59 +10001040 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1041 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001042int
1043ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001044{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001045 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001046 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001047 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001048 u_int authlen = 0, aadlen = 0;
1049 u_int len;
1050 struct sshenc *enc = NULL;
1051 struct sshmac *mac = NULL;
1052 struct sshcomp *comp = NULL;
1053 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001054
markus@openbsd.org091c3022015-01-19 19:52:16 +00001055 if (state->newkeys[MODE_OUT] != NULL) {
1056 enc = &state->newkeys[MODE_OUT]->enc;
1057 mac = &state->newkeys[MODE_OUT]->mac;
1058 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001059 /* disable mac for authenticated encryption */
1060 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1061 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001062 }
Damien Miller963f6b22002-02-19 15:21:23 +11001063 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001064 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001065
markus@openbsd.org091c3022015-01-19 19:52:16 +00001066 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001067
1068#ifdef PACKET_DEBUG
1069 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001070 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001071#endif
1072
1073 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001074 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001075 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001076 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1077 goto out;
1078 sshbuf_reset(state->compression_buffer);
1079 if ((r = compress_buffer(ssh, state->outgoing_packet,
1080 state->compression_buffer)) != 0)
1081 goto out;
1082 sshbuf_reset(state->outgoing_packet);
1083 if ((r = sshbuf_put(state->outgoing_packet,
1084 "\0\0\0\0\0", 5)) != 0 ||
1085 (r = sshbuf_putb(state->outgoing_packet,
1086 state->compression_buffer)) != 0)
1087 goto out;
1088 DBG(debug("compression: raw %d compressed %zd", len,
1089 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001090 }
1091
1092 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001093 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001094
1095 /*
1096 * calc size of padding, alloc space, get random data,
1097 * minimum padding is 4 bytes
1098 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001099 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001100 padlen = block_size - (len % block_size);
1101 if (padlen < 4)
1102 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001103 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001104 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001105 state->extra_pad =
1106 roundup(state->extra_pad, block_size);
1107 pad = state->extra_pad -
1108 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001109 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001110 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001111 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001112 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001113 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001114 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1115 goto out;
1116 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001117 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001118 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001119 } else {
1120 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001121 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001122 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001123 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001124 len = sshbuf_len(state->outgoing_packet);
1125 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1126 if (cp == NULL) {
1127 r = SSH_ERR_INTERNAL_ERROR;
1128 goto out;
1129 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001130 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001131 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001132 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001133 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1134 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001135
1136 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001137 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001138 if ((r = mac_compute(mac, state->p_send.seqnr,
1139 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001140 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001141 goto out;
1142 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001143 }
1144 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001145 if ((r = sshbuf_reserve(state->output,
1146 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1147 goto out;
1148 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1149 sshbuf_ptr(state->outgoing_packet),
1150 len - aadlen, aadlen, authlen)) != 0)
1151 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001152 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001153 if (mac && mac->enabled) {
1154 if (mac->etm) {
1155 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001156 if ((r = mac_compute(mac, state->p_send.seqnr,
1157 cp, len, macbuf, sizeof(macbuf))) != 0)
1158 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001159 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001160 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001161 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001162 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1163 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001164 }
Damien Miller33b13562000-04-04 14:38:59 +10001165#ifdef PACKET_DEBUG
1166 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001167 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001168#endif
Damien Miller4af51302000-04-16 11:18:38 +10001169 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001170 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001171 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001172 if (++state->p_send.packets == 0)
1173 if (!(ssh->compat & SSH_BUG_NOREKEY))
1174 return SSH_ERR_NEED_REKEY;
1175 state->p_send.blocks += len / block_size;
1176 state->p_send.bytes += len;
1177 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001178
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001179 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001180 r = ssh_set_newkeys(ssh, MODE_OUT);
1181 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1182 r = ssh_packet_enable_delayed_compress(ssh);
1183 else
1184 r = 0;
1185 out:
1186 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001187}
1188
markus@openbsd.org091c3022015-01-19 19:52:16 +00001189int
1190ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001191{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001192 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001193 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001194 u_char type;
1195 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001196
markus@openbsd.org091c3022015-01-19 19:52:16 +00001197 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001198
1199 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001200 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001201 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1202 (type > SSH2_MSG_TRANSPORT_MAX) ||
1203 (type == SSH2_MSG_SERVICE_REQUEST) ||
1204 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +10001205 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001206 p = calloc(1, sizeof(*p));
1207 if (p == NULL)
1208 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001209 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001210 p->payload = state->outgoing_packet;
1211 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1212 state->outgoing_packet = sshbuf_new();
1213 if (state->outgoing_packet == NULL)
1214 return SSH_ERR_ALLOC_FAIL;
1215 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001216 }
1217 }
1218
1219 /* rekeying starts with sending KEXINIT */
1220 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001221 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001222
markus@openbsd.org091c3022015-01-19 19:52:16 +00001223 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1224 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001225
1226 /* after a NEWKEYS message we can send the complete queue */
1227 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001228 state->rekeying = 0;
1229 state->rekey_time = monotime();
1230 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001231 type = p->type;
1232 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001233 sshbuf_free(state->outgoing_packet);
1234 state->outgoing_packet = p->payload;
1235 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001236 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001237 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1238 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001239 }
1240 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001241 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001242}
1243
Damien Miller33b13562000-04-04 14:38:59 +10001244/*
Damien Miller5428f641999-11-25 11:54:57 +11001245 * Waits until a packet has been received, and returns its type. Note that
1246 * no other data is processed until this returns, so this function should not
1247 * be used during the interactive session.
1248 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249
1250int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001251ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001253 struct session_state *state = ssh->state;
1254 int len, r, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001255 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001256 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001257 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258
Darren Tucker99bb7612008-06-13 22:02:50 +10001259 DBG(debug("packet_read()"));
1260
markus@openbsd.org091c3022015-01-19 19:52:16 +00001261 setp = (fd_set *)calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001262 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001263 if (setp == NULL)
1264 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001265
Damien Miller95def091999-11-25 00:26:21 +11001266 /* Since we are blocking, ensure that all written packets have been sent. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001267 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268
Damien Miller95def091999-11-25 00:26:21 +11001269 /* Stay in the loop until we have received a complete packet. */
1270 for (;;) {
1271 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001272 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1273 if (r != 0)
1274 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001275 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001276 *typep == SSH_SMSG_SUCCESS
1277 || *typep == SSH_SMSG_FAILURE
1278 || *typep == SSH_CMSG_EOF
1279 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1280 if ((r = sshpkt_get_end(ssh)) != 0)
1281 break;
Damien Miller95def091999-11-25 00:26:21 +11001282 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001283 if (*typep != SSH_MSG_NONE)
1284 break;
Damien Miller5428f641999-11-25 11:54:57 +11001285 /*
1286 * Otherwise, wait for some data to arrive, add it to the
1287 * buffer, and try again.
1288 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001289 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001290 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001291 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001292
markus@openbsd.org091c3022015-01-19 19:52:16 +00001293 if (state->packet_timeout_ms > 0) {
1294 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001295 timeoutp = &timeout;
1296 }
Damien Miller95def091999-11-25 00:26:21 +11001297 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001298 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001299 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001300 ms_to_timeval(&timeout, ms_remain);
1301 gettimeofday(&start, NULL);
1302 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001303 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001304 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001305 break;
Damien Millerea437422009-10-02 11:49:03 +10001306 if (errno != EAGAIN && errno != EINTR &&
1307 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001308 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001309 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001310 continue;
1311 ms_subtract_diff(&start, &ms_remain);
1312 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001313 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001314 break;
1315 }
1316 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001317 if (r == 0) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001318 logit("Connection to %.200s timed out while "
markus@openbsd.org091c3022015-01-19 19:52:16 +00001319 "waiting to read", ssh_remote_ipaddr(ssh));
Darren Tucker3fc464e2008-06-13 06:42:45 +10001320 cleanup_exit(255);
1321 }
Damien Miller95def091999-11-25 00:26:21 +11001322 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001323 do {
1324 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001325 len = roaming_read(state->connection_in, buf,
Darren Tuckerc5564e12009-06-21 18:53:53 +10001326 sizeof(buf), &cont);
1327 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001328 if (len == 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001329 logit("Connection closed by %.200s",
1330 ssh_remote_ipaddr(ssh));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001331 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001332 }
Damien Miller95def091999-11-25 00:26:21 +11001333 if (len < 0)
1334 fatal("Read from socket failed: %.100s", strerror(errno));
1335 /* Append it to the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001336 ssh_packet_process_incoming(ssh, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001337 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001338 free(setp);
1339 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340}
1341
Damien Miller278f9072001-12-21 15:00:19 +11001342int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001343ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001344{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001345 u_char type;
1346 int r;
1347
1348 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1349 fatal("%s: %s", __func__, ssh_err(r));
1350 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001351}
1352
Damien Miller5428f641999-11-25 11:54:57 +11001353/*
1354 * Waits until a packet has been received, verifies that its type matches
1355 * that given, and gives a fatal error and exits if there is a mismatch.
1356 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357
1358void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001359ssh_packet_read_expect(struct ssh *ssh, int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001360{
Damien Miller95def091999-11-25 00:26:21 +11001361 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001362
markus@openbsd.org091c3022015-01-19 19:52:16 +00001363 type = ssh_packet_read(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001364 if (type != expected_type)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001365 ssh_packet_disconnect(ssh,
1366 "Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001367 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368}
1369
1370/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001371 * packet_process_incoming. If so, reads the packet; otherwise returns
1372 * SSH_MSG_NONE. This does not wait for data from the connection.
1373 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001374 * SSH_MSG_DISCONNECT is handled specially here. Also,
1375 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1376 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001377 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378
markus@openbsd.org091c3022015-01-19 19:52:16 +00001379int
1380ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001382 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001383 u_int len, padded_len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001384 const u_char *cp;
1385 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001386 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001387 int r;
1388
1389 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390
Damien Miller95def091999-11-25 00:26:21 +11001391 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001392 if (sshbuf_len(state->input) < 4 + 8)
1393 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001394 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001395 len = PEEK_U32(sshbuf_ptr(state->input));
Damien Miller95def091999-11-25 00:26:21 +11001396 if (len < 1 + 2 + 2 || len > 256 * 1024)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001397 ssh_packet_disconnect(ssh, "Bad packet length %u.",
1398 len);
Damien Miller95def091999-11-25 00:26:21 +11001399 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400
Damien Miller95def091999-11-25 00:26:21 +11001401 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001402 if (sshbuf_len(state->input) < 4 + padded_len)
1403 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404
Damien Miller95def091999-11-25 00:26:21 +11001405 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406
Damien Miller95def091999-11-25 00:26:21 +11001407 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001408 if ((r = sshbuf_consume(state->input, 4)) != 0)
1409 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001410
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001411 /*
1412 * Cryptographic attack detector for ssh
1413 * (C)1998 CORE-SDI, Buenos Aires Argentina
1414 * Ariel Futoransky(futo@core-sdi.com)
1415 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001416 if (!state->receive_context.plaintext) {
1417 switch (detect_attack(&state->deattack,
1418 sshbuf_ptr(state->input), padded_len)) {
1419 case DEATTACK_OK:
1420 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001421 case DEATTACK_DETECTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001422 ssh_packet_disconnect(ssh,
1423 "crc32 compensation attack: network attack detected"
1424 );
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001425 case DEATTACK_DOS_DETECTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001426 ssh_packet_disconnect(ssh,
1427 "deattack denial of service detected");
1428 default:
1429 ssh_packet_disconnect(ssh, "deattack error");
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001430 }
1431 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001432
1433 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001434 sshbuf_reset(state->incoming_packet);
1435 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1436 goto out;
1437 if ((r = cipher_crypt(&state->receive_context, 0, p,
1438 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1439 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001440
markus@openbsd.org091c3022015-01-19 19:52:16 +00001441 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1442 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443
1444#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001445 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001446 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001448
Damien Miller95def091999-11-25 00:26:21 +11001449 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001450 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1451 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001452
Damien Miller95def091999-11-25 00:26:21 +11001453 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001454 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1455 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001456
Damien Miller95def091999-11-25 00:26:21 +11001457 /* Test check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001458 if (len != sshbuf_len(state->incoming_packet))
1459 ssh_packet_disconnect(ssh,
1460 "packet_read_poll1: len %d != sshbuf_len %zd.",
1461 len, sshbuf_len(state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462
markus@openbsd.org091c3022015-01-19 19:52:16 +00001463 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1464 stored_checksum = PEEK_U32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001465 if (checksum != stored_checksum)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001466 ssh_packet_disconnect(ssh,
1467 "Corrupted check bytes on input.");
1468 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1469 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470
markus@openbsd.org091c3022015-01-19 19:52:16 +00001471 if (state->packet_compression) {
1472 sshbuf_reset(state->compression_buffer);
1473 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1474 state->compression_buffer)) != 0)
1475 goto out;
1476 sshbuf_reset(state->incoming_packet);
1477 if ((r = sshbuf_putb(state->incoming_packet,
1478 state->compression_buffer)) != 0)
1479 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001480 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001481 state->p_read.packets++;
1482 state->p_read.bytes += padded_len + 4;
1483 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1484 goto out;
1485 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX)
1486 ssh_packet_disconnect(ssh,
1487 "Invalid ssh1 packet type: %d", *typep);
1488 r = 0;
1489 out:
1490 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491}
Damien Miller95def091999-11-25 00:26:21 +11001492
markus@openbsd.org091c3022015-01-19 19:52:16 +00001493int
1494ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001495{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001496 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001497 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001498 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1499 u_int maclen, aadlen = 0, authlen = 0, block_size;
1500 struct sshenc *enc = NULL;
1501 struct sshmac *mac = NULL;
1502 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001503 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001504
markus@openbsd.org091c3022015-01-19 19:52:16 +00001505 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001506
markus@openbsd.org091c3022015-01-19 19:52:16 +00001507 if (state->packet_discard)
1508 return 0;
1509
1510 if (state->newkeys[MODE_IN] != NULL) {
1511 enc = &state->newkeys[MODE_IN]->enc;
1512 mac = &state->newkeys[MODE_IN]->mac;
1513 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001514 /* disable mac for authenticated encryption */
1515 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1516 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001517 }
1518 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001519 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001520 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001521
markus@openbsd.org091c3022015-01-19 19:52:16 +00001522 if (aadlen && state->packlen == 0) {
1523 if (cipher_get_length(&state->receive_context,
1524 &state->packlen, state->p_read.seqnr,
1525 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1526 return 0;
1527 if (state->packlen < 1 + 4 ||
1528 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001529#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001530 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001531#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001532 logit("Bad packet length %u.", state->packlen);
1533 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1534 return r;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001535 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001536 sshbuf_reset(state->incoming_packet);
1537 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001538 /*
1539 * check if input size is less than the cipher block size,
1540 * decrypt first block and extract length of incoming packet
1541 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001542 if (sshbuf_len(state->input) < block_size)
1543 return 0;
1544 sshbuf_reset(state->incoming_packet);
1545 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1546 &cp)) != 0)
1547 goto out;
1548 if ((r = cipher_crypt(&state->receive_context,
1549 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1550 block_size, 0, 0)) != 0)
1551 goto out;
1552 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1553 if (state->packlen < 1 + 4 ||
1554 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001555#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001556 fprintf(stderr, "input: \n");
1557 sshbuf_dump(state->input, stderr);
1558 fprintf(stderr, "incoming_packet: \n");
1559 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001560#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001561 logit("Bad packet length %u.", state->packlen);
1562 return ssh_packet_start_discard(ssh, enc, mac,
1563 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001564 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001565 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1566 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001567 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001568 DBG(debug("input: packet len %u", state->packlen+4));
1569
Damien Milleraf43a7a2012-12-12 10:46:31 +11001570 if (aadlen) {
1571 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001572 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001573 } else {
1574 /*
1575 * the payload size and the payload are encrypted, but we
1576 * have a partial packet of block_size bytes
1577 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001578 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001579 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001580 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1581 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001582 if (need % block_size != 0) {
1583 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001584 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001585 return ssh_packet_start_discard(ssh, enc, mac,
1586 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001587 }
Damien Miller33b13562000-04-04 14:38:59 +10001588 /*
1589 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001590 * decrypt into incoming_packet:
1591 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001592 * 'need' bytes are encrypted, followed by either
1593 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001594 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001595 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001596 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1597 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001598#ifdef PACKET_DEBUG
1599 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001600 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001601#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001602 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001603 if (mac && mac->enabled && mac->etm) {
1604 if ((r = mac_compute(mac, state->p_read.seqnr,
1605 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001606 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001607 goto out;
1608 }
1609 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1610 &cp)) != 0)
1611 goto out;
1612 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1613 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1614 goto out;
1615 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1616 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001617 /*
1618 * compute MAC over seqnr and packet,
1619 * increment sequence number for incoming packet
1620 */
Damien Miller4af51302000-04-16 11:18:38 +10001621 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001622 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001623 if ((r = mac_compute(mac, state->p_read.seqnr,
1624 sshbuf_ptr(state->incoming_packet),
1625 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001626 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001627 goto out;
1628 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001629 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001630 logit("Corrupted MAC on input.");
1631 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001632 return SSH_ERR_INTERNAL_ERROR;
1633 return ssh_packet_start_discard(ssh, enc, mac,
1634 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001635 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001636
1637 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1638 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1639 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001640 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001641 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001642 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001643 *seqnr_p = state->p_read.seqnr;
1644 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001645 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001646 if (++state->p_read.packets == 0)
1647 if (!(ssh->compat & SSH_BUG_NOREKEY))
1648 return SSH_ERR_NEED_REKEY;
1649 state->p_read.blocks += (state->packlen + 4) / block_size;
1650 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001651
1652 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001653 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001654 DBG(debug("input: padlen %d", padlen));
1655 if (padlen < 4)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001656 ssh_packet_disconnect(ssh,
1657 "Corrupted padlen %d on input.", padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001658
1659 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001660 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1661 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1662 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001663
markus@openbsd.org091c3022015-01-19 19:52:16 +00001664 DBG(debug("input: len before de-compress %zd",
1665 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001666 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001667 sshbuf_reset(state->compression_buffer);
1668 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1669 state->compression_buffer)) != 0)
1670 goto out;
1671 sshbuf_reset(state->incoming_packet);
1672 if ((r = sshbuf_putb(state->incoming_packet,
1673 state->compression_buffer)) != 0)
1674 goto out;
1675 DBG(debug("input: len after de-compress %zd",
1676 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001677 }
1678 /*
1679 * get packet type, implies consume.
1680 * return length of payload (without type field)
1681 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001682 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1683 goto out;
1684 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN)
1685 ssh_packet_disconnect(ssh,
1686 "Invalid ssh2 packet type: %d", *typep);
1687 if (*typep == SSH2_MSG_NEWKEYS)
1688 r = ssh_set_newkeys(ssh, MODE_IN);
1689 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1690 r = ssh_packet_enable_delayed_compress(ssh);
1691 else
1692 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001693#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001694 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1695 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001696#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001697 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001698 state->packlen = 0;
1699 out:
1700 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001701}
1702
1703int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001704ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001705{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001706 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001707 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001708 int r;
1709 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001710
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001711 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001712 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001713 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001714 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1715 if (r != 0)
1716 return r;
1717 if (*typep) {
1718 state->keep_alive_timeouts = 0;
1719 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001720 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001721 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001722 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001723 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001724 break;
Damien Miller33b13562000-04-04 14:38:59 +10001725 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001726 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1727 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1728 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1729 if (msg)
1730 free(msg);
1731 return r;
1732 }
Damien Miller33b13562000-04-04 14:38:59 +10001733 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001734 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001735 break;
1736 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001737 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1738 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1739 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001740 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001741 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001742 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1743 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1744 "Received disconnect from %s: %u: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001745 ssh_remote_ipaddr(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001746 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001747 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001748 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001749 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1750 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001751 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1752 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001753 break;
Damien Miller33b13562000-04-04 14:38:59 +10001754 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001755 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001756 }
Damien Miller33b13562000-04-04 14:38:59 +10001757 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001758 r = ssh_packet_read_poll1(ssh, typep);
1759 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001760 case SSH_MSG_NONE:
1761 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001762 case SSH_MSG_IGNORE:
1763 break;
1764 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001765 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1766 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001767 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001768 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001769 break;
1770 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001771 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1772 return r;
Damien Miller894926e2013-02-12 11:03:58 +11001773 error("Received disconnect from %s: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001774 ssh_remote_ipaddr(ssh), msg);
1775 free(msg);
1776 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001777 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001778 DBG(debug("received packet type %d", *typep));
1779 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001780 }
Damien Miller33b13562000-04-04 14:38:59 +10001781 }
1782 }
1783}
1784
Damien Miller5428f641999-11-25 11:54:57 +11001785/*
1786 * Buffers the given amount of input characters. This is intended to be used
1787 * together with packet_read_poll.
1788 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001789
1790void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001791ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001792{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001793 struct session_state *state = ssh->state;
1794 int r;
1795
1796 if (state->packet_discard) {
1797 state->keep_alive_timeouts = 0; /* ?? */
1798 if (len >= state->packet_discard) {
1799 if ((r = ssh_packet_stop_discard(ssh)) != 0)
1800 fatal("%s: %s", __func__, ssh_err(r));
1801 cleanup_exit(255);
1802 }
1803 state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001804 return;
1805 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001806 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
1807 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001808}
1809
Damien Miller4af51302000-04-16 11:18:38 +10001810int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001811ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001812{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001813 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001814}
1815
Damien Miller5428f641999-11-25 11:54:57 +11001816/*
1817 * Sends a diagnostic message from the server to the client. This message
1818 * can be sent at any time (but not while constructing another message). The
1819 * message is printed immediately, but only if the client is being executed
1820 * in verbose mode. These messages are primarily intended to ease debugging
1821 * authentication problems. The length of the formatted message must not
1822 * exceed 1024 bytes. This will automatically call packet_write_wait.
1823 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001824
1825void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001826ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001827{
Damien Miller95def091999-11-25 00:26:21 +11001828 char buf[1024];
1829 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001830 int r;
Damien Miller95def091999-11-25 00:26:21 +11001831
markus@openbsd.org091c3022015-01-19 19:52:16 +00001832 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001833 return;
1834
Damien Miller95def091999-11-25 00:26:21 +11001835 va_start(args, fmt);
1836 vsnprintf(buf, sizeof(buf), fmt, args);
1837 va_end(args);
1838
Damien Miller7c8af4f2000-05-01 08:24:07 +10001839 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001840 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1841 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1842 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1843 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1844 (r = sshpkt_send(ssh)) != 0)
1845 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001846 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001847 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1848 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1849 (r = sshpkt_send(ssh)) != 0)
1850 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001851 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001852 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001853}
1854
Damien Miller5428f641999-11-25 11:54:57 +11001855/*
1856 * Logs the error plus constructs and sends a disconnect packet, closes the
1857 * connection, and exits. This function never returns. The error message
1858 * should not contain a newline. The length of the formatted message must
1859 * not exceed 1024 bytes.
1860 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001861
1862void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001863ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001864{
Damien Miller95def091999-11-25 00:26:21 +11001865 char buf[1024];
1866 va_list args;
1867 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001868 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001869
Damien Miller95def091999-11-25 00:26:21 +11001870 if (disconnecting) /* Guard against recursive invocations. */
1871 fatal("packet_disconnect called recursively.");
1872 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001873
Damien Miller5428f641999-11-25 11:54:57 +11001874 /*
1875 * Format the message. Note that the caller must make sure the
1876 * message is of limited size.
1877 */
Damien Miller95def091999-11-25 00:26:21 +11001878 va_start(args, fmt);
1879 vsnprintf(buf, sizeof(buf), fmt, args);
1880 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001881
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001882 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001883 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001884
Damien Miller95def091999-11-25 00:26:21 +11001885 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001886 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001887 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
1888 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
1889 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1890 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1891 (r = sshpkt_send(ssh)) != 0)
1892 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001893 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001894 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
1895 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1896 (r = sshpkt_send(ssh)) != 0)
1897 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001898 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001899 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001900
Damien Miller95def091999-11-25 00:26:21 +11001901 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001902 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001903 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001904}
1905
Damien Miller5428f641999-11-25 11:54:57 +11001906/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001907
1908void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001909ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001910{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001911 struct session_state *state = ssh->state;
1912 int len = sshbuf_len(state->output);
1913 int cont, r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001914
Damien Miller95def091999-11-25 00:26:21 +11001915 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001916 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001917 len = roaming_write(state->connection_out,
1918 sshbuf_ptr(state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001919 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001920 if (errno == EINTR || errno == EAGAIN ||
1921 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001922 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001923 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001924 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001925 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001926 fatal("Write connection closed");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001927 if ((r = sshbuf_consume(state->output, len)) != 0)
1928 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001929 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001930}
1931
Damien Miller5428f641999-11-25 11:54:57 +11001932/*
1933 * Calls packet_write_poll repeatedly until all pending output data has been
1934 * written.
1935 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001936
1937void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001938ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001939{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001940 fd_set *setp;
Damien Millerce986542013-07-18 16:12:44 +10001941 int ret, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001942 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001943 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001944
markus@openbsd.org091c3022015-01-19 19:52:16 +00001945 setp = (fd_set *)calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001946 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001947 if (setp == NULL)
1948 fatal("%s: calloc failed", __func__);
1949 ssh_packet_write_poll(ssh);
1950 while (ssh_packet_have_data_to_write(ssh)) {
1951 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001952 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001953 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001954
markus@openbsd.org091c3022015-01-19 19:52:16 +00001955 if (state->packet_timeout_ms > 0) {
1956 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001957 timeoutp = &timeout;
1958 }
1959 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001960 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001961 ms_to_timeval(&timeout, ms_remain);
1962 gettimeofday(&start, NULL);
1963 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001964 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001965 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001966 break;
Damien Millerea437422009-10-02 11:49:03 +10001967 if (errno != EAGAIN && errno != EINTR &&
1968 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001969 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001970 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001971 continue;
1972 ms_subtract_diff(&start, &ms_remain);
1973 if (ms_remain <= 0) {
1974 ret = 0;
1975 break;
1976 }
1977 }
1978 if (ret == 0) {
1979 logit("Connection to %.200s timed out while "
markus@openbsd.org091c3022015-01-19 19:52:16 +00001980 "waiting to write", ssh_remote_ipaddr(ssh));
Darren Tucker3fc464e2008-06-13 06:42:45 +10001981 cleanup_exit(255);
1982 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001983 ssh_packet_write_poll(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001984 }
Darren Tuckera627d422013-06-02 07:31:17 +10001985 free(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001986}
1987
1988/* Returns true if there is buffered data to write to the connection. */
1989
1990int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001991ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001992{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001993 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001994}
1995
1996/* Returns true if there is not too much data to write to the connection. */
1997
1998int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001999ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002000{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002001 if (ssh->state->interactive_mode)
2002 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002003 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002004 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002005}
2006
markus@openbsd.org091c3022015-01-19 19:52:16 +00002007void
2008ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002009{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002010#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002011 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002012 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002013 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002014# ifdef IP_TOS
2015 case AF_INET:
2016 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002017 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002018 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2019 error("setsockopt IP_TOS %d: %.100s:",
2020 tos, strerror(errno));
2021 break;
2022# endif /* IP_TOS */
2023# ifdef IPV6_TCLASS
2024 case AF_INET6:
2025 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002026 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002027 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2028 error("setsockopt IPV6_TCLASS %d: %.100s:",
2029 tos, strerror(errno));
2030 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002031# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002032 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002033#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002034}
Ben Lindstroma7433982002-12-23 02:41:41 +00002035
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002036/* Informs that the current session is interactive. Sets IP flags for that. */
2037
2038void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002039ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002040{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002041 struct session_state *state = ssh->state;
2042
2043 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002044 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002045 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002046
Damien Miller95def091999-11-25 00:26:21 +11002047 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002048 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002049
Damien Miller34132e52000-01-14 15:45:46 +11002050 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002051 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002052 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002053 set_nodelay(state->connection_in);
2054 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2055 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002056}
2057
2058/* Returns true if the current connection is interactive. */
2059
2060int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002061ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002062{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002063 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002064}
Damien Miller6162d121999-11-21 13:23:52 +11002065
Darren Tucker1f8311c2004-05-13 16:39:33 +10002066int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002067ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002068{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002069 struct session_state *state = ssh->state;
2070
2071 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002072 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002073 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002074 return -1;
2075 }
2076 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002077 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002078 return -1;
2079 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002080 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002081 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002082 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002083 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002084}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002085
Darren Tuckerf7288d72009-06-21 18:12:20 +10002086int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002087ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002088{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002089 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002090}
2091
2092void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002093ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002094{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002095 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002096}
2097
2098u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002099ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002100{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002101 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002102}
2103
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002104/*
2105 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002106 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002107 * byte SSH_MSG_IGNORE
2108 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002109 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002110 * All implementations MUST understand (and ignore) this message at any
2111 * time (after receiving the protocol version). No implementation is
2112 * required to send them. This message can be used as an additional
2113 * protection measure against advanced traffic analysis techniques.
2114 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002115void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002116ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002117{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002118 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002119 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002120
markus@openbsd.org091c3022015-01-19 19:52:16 +00002121 if ((r = sshpkt_start(ssh, compat20 ?
2122 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2123 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2124 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002125 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002126 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002127 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002128 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2129 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002130 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002131 }
2132}
Damien Millera5539d22003-04-09 20:50:06 +10002133
Darren Tucker1f8311c2004-05-13 16:39:33 +10002134#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002135int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002136ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002137{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002138 struct session_state *state = ssh->state;
2139
2140 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002141 return 0;
2142 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002143 (state->p_send.packets > MAX_PACKETS) ||
2144 (state->p_read.packets > MAX_PACKETS) ||
2145 (state->max_blocks_out &&
2146 (state->p_send.blocks > state->max_blocks_out)) ||
2147 (state->max_blocks_in &&
2148 (state->p_read.blocks > state->max_blocks_in)) ||
2149 (state->rekey_interval != 0 && state->rekey_time +
2150 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002151}
2152
2153void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002154ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002155{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002156 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
2157 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002158 ssh->state->rekey_limit = bytes;
2159 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002160 /*
2161 * We set the time here so that in post-auth privsep slave we count
2162 * from the completion of the authentication.
2163 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002164 ssh->state->rekey_time = monotime();
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002165}
2166
2167time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002168ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002169{
2170 time_t seconds;
2171
markus@openbsd.org091c3022015-01-19 19:52:16 +00002172 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002173 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002174 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002175}
Damien Miller9786e6e2005-07-26 21:54:56 +10002176
2177void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002178ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002179{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002180 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002181}
2182
2183void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002184ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002185{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002186 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002187}
2188
2189void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002190ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002191{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002192 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002193}
2194
2195void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002196ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002197{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002198 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002199}
2200
markus@openbsd.org091c3022015-01-19 19:52:16 +00002201/* XXX TODO update roaming to new API (does not work anyway) */
Darren Tuckere841eb02009-07-06 07:11:13 +10002202/*
2203 * Save the state for the real connection, and use a separate state when
2204 * resuming a suspended connection.
2205 */
2206void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002207ssh_packet_backup_state(struct ssh *ssh,
2208 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002209{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002210 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002211
markus@openbsd.org091c3022015-01-19 19:52:16 +00002212 close(ssh->state->connection_in);
2213 ssh->state->connection_in = -1;
2214 close(ssh->state->connection_out);
2215 ssh->state->connection_out = -1;
Darren Tuckere841eb02009-07-06 07:11:13 +10002216 if (backup_state)
2217 tmp = backup_state;
2218 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002219 tmp = ssh_alloc_session_state();
2220 backup_state = ssh;
2221 ssh = tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002222}
2223
markus@openbsd.org091c3022015-01-19 19:52:16 +00002224/* XXX FIXME FIXME FIXME */
Darren Tuckere841eb02009-07-06 07:11:13 +10002225/*
2226 * Swap in the old state when resuming a connecion.
2227 */
2228void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002229ssh_packet_restore_state(struct ssh *ssh,
2230 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002231{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002232 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002233 u_int len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002234 int r;
Darren Tuckere841eb02009-07-06 07:11:13 +10002235
2236 tmp = backup_state;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002237 backup_state = ssh;
2238 ssh = tmp;
2239 ssh->state->connection_in = backup_state->state->connection_in;
2240 backup_state->state->connection_in = -1;
2241 ssh->state->connection_out = backup_state->state->connection_out;
2242 backup_state->state->connection_out = -1;
2243 len = sshbuf_len(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002244 if (len > 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002245 if ((r = sshbuf_putb(ssh->state->input,
2246 backup_state->state->input)) != 0)
2247 fatal("%s: %s", __func__, ssh_err(r));
2248 sshbuf_reset(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002249 add_recv_bytes(len);
2250 }
2251}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002252
2253/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002254static int
2255ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002256{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002257 struct sshcomp *comp;
2258 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002259
2260 debug("%s: called", __func__);
2261 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002262 ssh->state->after_authentication = 1;
2263 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002264 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002265 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002266 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002267 comp = &ssh->state->newkeys[mode]->comp;
2268 if (comp && comp->enabled &&
2269 (r = ssh_packet_init_compression(ssh)) != 0)
2270 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002271 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002272 return 0;
2273}
2274
2275/* Packet state (de-)serialization for privsep */
2276
2277/* turn kex into a blob for packet state serialization */
2278static int
2279kex_to_blob(struct sshbuf *m, struct kex *kex)
2280{
2281 int r;
2282
2283 if ((r = sshbuf_put_string(m, kex->session_id,
2284 kex->session_id_len)) != 0 ||
2285 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2286 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2287 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2288 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2289 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2290 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2291 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2292 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2293 return r;
2294 return 0;
2295}
2296
2297/* turn key exchange results into a blob for packet state serialization */
2298static int
2299newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2300{
2301 struct sshbuf *b;
2302 struct sshcipher_ctx *cc;
2303 struct sshcomp *comp;
2304 struct sshenc *enc;
2305 struct sshmac *mac;
2306 struct newkeys *newkey;
2307 int r;
2308
2309 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2310 return SSH_ERR_INTERNAL_ERROR;
2311 enc = &newkey->enc;
2312 mac = &newkey->mac;
2313 comp = &newkey->comp;
2314 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2315 &ssh->state->receive_context;
2316 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2317 return r;
2318 if ((b = sshbuf_new()) == NULL)
2319 return SSH_ERR_ALLOC_FAIL;
2320 /* The cipher struct is constant and shared, you export pointer */
2321 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2322 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2323 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2324 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2325 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2326 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2327 goto out;
2328 if (cipher_authlen(enc->cipher) == 0) {
2329 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2330 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2331 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2332 goto out;
2333 }
2334 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2335 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2336 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2337 goto out;
2338 r = sshbuf_put_stringb(m, b);
2339 out:
2340 if (b != NULL)
2341 sshbuf_free(b);
2342 return r;
2343}
2344
2345/* serialize packet state into a blob */
2346int
2347ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2348{
2349 struct session_state *state = ssh->state;
2350 u_char *p;
2351 size_t slen, rlen;
2352 int r, ssh1cipher;
2353
2354 if (!compat20) {
2355 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2356 slen = cipher_get_keyiv_len(&state->send_context);
2357 rlen = cipher_get_keyiv_len(&state->receive_context);
2358 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2359 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2360 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2361 (r = sshbuf_put_u32(m, slen)) != 0 ||
2362 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2363 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2364 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2365 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2366 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2367 return r;
2368 } else {
2369 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2370 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2371 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
2372 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2373 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2374 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2375 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2376 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2377 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2378 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2379 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2380 return r;
2381 }
2382
2383 slen = cipher_get_keycontext(&state->send_context, NULL);
2384 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2385 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2386 (r = sshbuf_reserve(m, slen, &p)) != 0)
2387 return r;
2388 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2389 return SSH_ERR_INTERNAL_ERROR;
2390 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2391 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2392 return r;
2393 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2394 return SSH_ERR_INTERNAL_ERROR;
2395
2396 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2397 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2398 (r = sshbuf_put_stringb(m, state->output)) != 0)
2399 return r;
2400
2401 if (compat20) {
2402 if ((r = sshbuf_put_u64(m, get_sent_bytes())) != 0 ||
2403 (r = sshbuf_put_u64(m, get_recv_bytes())) != 0)
2404 return r;
2405 }
2406 return 0;
2407}
2408
2409/* restore key exchange results from blob for packet state de-serialization */
2410static int
2411newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2412{
2413 struct sshbuf *b = NULL;
2414 struct sshcomp *comp;
2415 struct sshenc *enc;
2416 struct sshmac *mac;
2417 struct newkeys *newkey = NULL;
2418 size_t keylen, ivlen, maclen;
2419 int r;
2420
2421 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2422 r = SSH_ERR_ALLOC_FAIL;
2423 goto out;
2424 }
2425 if ((r = sshbuf_froms(m, &b)) != 0)
2426 goto out;
2427#ifdef DEBUG_PK
2428 sshbuf_dump(b, stderr);
2429#endif
2430 enc = &newkey->enc;
2431 mac = &newkey->mac;
2432 comp = &newkey->comp;
2433
2434 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2435 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2436 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2437 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2438 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2439 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2440 goto out;
2441 if (cipher_authlen(enc->cipher) == 0) {
2442 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2443 goto out;
2444 if ((r = mac_setup(mac, mac->name)) != 0)
2445 goto out;
2446 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2447 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2448 goto out;
2449 if (maclen > mac->key_len) {
2450 r = SSH_ERR_INVALID_FORMAT;
2451 goto out;
2452 }
2453 mac->key_len = maclen;
2454 }
2455 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2456 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2457 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2458 goto out;
2459 if (enc->name == NULL ||
2460 cipher_by_name(enc->name) != enc->cipher) {
2461 r = SSH_ERR_INVALID_FORMAT;
2462 goto out;
2463 }
2464 if (sshbuf_len(b) != 0) {
2465 r = SSH_ERR_INVALID_FORMAT;
2466 goto out;
2467 }
2468 enc->key_len = keylen;
2469 enc->iv_len = ivlen;
2470 ssh->kex->newkeys[mode] = newkey;
2471 newkey = NULL;
2472 r = 0;
2473 out:
2474 if (newkey != NULL)
2475 free(newkey);
2476 if (b != NULL)
2477 sshbuf_free(b);
2478 return r;
2479}
2480
2481/* restore kex from blob for packet state de-serialization */
2482static int
2483kex_from_blob(struct sshbuf *m, struct kex **kexp)
2484{
2485 struct kex *kex;
2486 int r;
2487
2488 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2489 (kex->my = sshbuf_new()) == NULL ||
2490 (kex->peer = sshbuf_new()) == NULL) {
2491 r = SSH_ERR_ALLOC_FAIL;
2492 goto out;
2493 }
2494 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2495 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2496 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2497 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2498 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2499 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2500 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2501 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2502 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2503 goto out;
2504 kex->server = 1;
2505 kex->done = 1;
2506 r = 0;
2507 out:
2508 if (r != 0 || kexp == NULL) {
2509 if (kex != NULL) {
2510 if (kex->my != NULL)
2511 sshbuf_free(kex->my);
2512 if (kex->peer != NULL)
2513 sshbuf_free(kex->peer);
2514 free(kex);
2515 }
2516 if (kexp != NULL)
2517 *kexp = NULL;
2518 } else {
2519 *kexp = kex;
2520 }
2521 return r;
2522}
2523
2524/*
2525 * Restore packet state from content of blob 'm' (de-serialization).
2526 * Note that 'm' will be partially consumed on parsing or any other errors.
2527 */
2528int
2529ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2530{
2531 struct session_state *state = ssh->state;
2532 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2533 size_t ssh1keylen, rlen, slen, ilen, olen;
2534 int r;
2535 u_int ssh1cipher = 0;
2536 u_int64_t sent_bytes = 0, recv_bytes = 0;
2537
2538 if (!compat20) {
2539 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2540 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2541 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2542 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2543 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2544 return r;
2545 if (ssh1cipher > INT_MAX)
2546 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2547 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2548 (int)ssh1cipher);
2549 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2550 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2551 return SSH_ERR_INVALID_FORMAT;
2552 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2553 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2554 return r;
2555 } else {
2556 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2557 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2558 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
2559 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2560 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2561 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2562 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2563 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2564 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2565 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2566 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2567 return r;
2568 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2569 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2570 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2571 return r;
2572 }
2573 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2574 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2575 return r;
2576 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2577 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2578 return SSH_ERR_INVALID_FORMAT;
2579 cipher_set_keycontext(&state->send_context, keyout);
2580 cipher_set_keycontext(&state->receive_context, keyin);
2581
2582 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2583 (r = ssh_packet_set_postauth(ssh)) != 0)
2584 return r;
2585
2586 sshbuf_reset(state->input);
2587 sshbuf_reset(state->output);
2588 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2589 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2590 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2591 (r = sshbuf_put(state->output, output, olen)) != 0)
2592 return r;
2593
2594 if (compat20) {
2595 if ((r = sshbuf_get_u64(m, &sent_bytes)) != 0 ||
2596 (r = sshbuf_get_u64(m, &recv_bytes)) != 0)
2597 return r;
2598 roam_set_bytes(sent_bytes, recv_bytes);
2599 }
2600 if (sshbuf_len(m))
2601 return SSH_ERR_INVALID_FORMAT;
2602 debug3("%s: done", __func__);
2603 return 0;
2604}
2605
2606/* NEW API */
2607
2608/* put data to the outgoing packet */
2609
2610int
2611sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2612{
2613 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2614}
2615
2616int
2617sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2618{
2619 return sshbuf_putb(ssh->state->outgoing_packet, b);
2620}
2621
2622int
2623sshpkt_put_u8(struct ssh *ssh, u_char val)
2624{
2625 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2626}
2627
2628int
2629sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2630{
2631 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2632}
2633
2634int
2635sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2636{
2637 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2638}
2639
2640int
2641sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2642{
2643 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2644}
2645
2646int
2647sshpkt_put_cstring(struct ssh *ssh, const void *v)
2648{
2649 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2650}
2651
2652int
2653sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2654{
2655 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2656}
2657
2658int
2659sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2660{
2661 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2662}
2663
2664int
2665sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2666{
2667 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2668}
2669
2670int
2671sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2672{
2673 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2674}
2675
2676/* fetch data from the incoming packet */
2677
2678int
2679sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2680{
2681 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2682}
2683
2684int
2685sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2686{
2687 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2688}
2689
2690int
2691sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2692{
2693 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2694}
2695
2696int
2697sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2698{
2699 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2700}
2701
2702int
2703sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2704{
2705 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2706}
2707
2708int
2709sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2710{
2711 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2712}
2713
2714int
2715sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2716{
2717 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2718}
2719
2720int
2721sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2722{
2723 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2724}
2725
2726int
2727sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2728{
2729 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2730}
2731
2732int
2733sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2734{
2735 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2736}
2737
2738int
2739sshpkt_get_end(struct ssh *ssh)
2740{
2741 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2742 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2743 return 0;
2744}
2745
2746const u_char *
2747sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2748{
2749 if (lenp != NULL)
2750 *lenp = sshbuf_len(ssh->state->incoming_packet);
2751 return sshbuf_ptr(ssh->state->incoming_packet);
2752}
2753
2754/* start a new packet */
2755
2756int
2757sshpkt_start(struct ssh *ssh, u_char type)
2758{
2759 u_char buf[9];
2760 int len;
2761
2762 DBG(debug("packet_start[%d]", type));
2763 len = compat20 ? 6 : 9;
2764 memset(buf, 0, len - 1);
2765 buf[len - 1] = type;
2766 sshbuf_reset(ssh->state->outgoing_packet);
2767 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2768}
2769
2770/* send it */
2771
2772int
2773sshpkt_send(struct ssh *ssh)
2774{
2775 if (compat20)
2776 return ssh_packet_send2(ssh);
2777 else
2778 return ssh_packet_send1(ssh);
2779}
2780
2781int
2782sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2783{
2784 char buf[1024];
2785 va_list args;
2786 int r;
2787
2788 va_start(args, fmt);
2789 vsnprintf(buf, sizeof(buf), fmt, args);
2790 va_end(args);
2791
2792 if (compat20) {
2793 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2794 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2795 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2796 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2797 (r = sshpkt_send(ssh)) != 0)
2798 return r;
2799 } else {
2800 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2801 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2802 (r = sshpkt_send(ssh)) != 0)
2803 return r;
2804 }
2805 return 0;
2806}
2807
2808/* roundup current message to pad bytes */
2809int
2810sshpkt_add_padding(struct ssh *ssh, u_char pad)
2811{
2812 ssh->state->extra_pad = pad;
2813 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002814}