blob: 7ddebeb716102ca86c46fd2b456d290bb69821e4 [file] [log] [blame]
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001/* $OpenBSD: packet.c,v 1.228 2016/02/08 10:57:07 djm 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"
markus@openbsd.org091c3022015-01-19 19:52:16 +000086#include "ssherr.h"
87#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100088
89#ifdef PACKET_DEBUG
90#define DBG(x) x
91#else
92#define DBG(x)
93#endif
94
Damien Miller13ae44c2009-01-28 16:38:41 +110095#define PACKET_MAX_SIZE (256 * 1024)
96
Darren Tuckerf7288d72009-06-21 18:12:20 +100097struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100098 u_int32_t seqnr;
99 u_int32_t packets;
100 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000101 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000102};
Damien Miller13ae44c2009-01-28 16:38:41 +1100103
Damien Millera5539d22003-04-09 20:50:06 +1000104struct packet {
105 TAILQ_ENTRY(packet) next;
106 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000107 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000108};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000109
110struct session_state {
111 /*
112 * This variable contains the file descriptors used for
113 * communicating with the other side. connection_in is used for
114 * reading; connection_out for writing. These can be the same
115 * descriptor, in which case it is assumed to be a socket.
116 */
117 int connection_in;
118 int connection_out;
119
120 /* Protocol flags for the remote side. */
121 u_int remote_protocol_flags;
122
123 /* Encryption context for receiving data. Only used for decryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000124 struct sshcipher_ctx receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000125
126 /* Encryption context for sending data. Only used for encryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000127 struct sshcipher_ctx send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000128
129 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000130 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000131
132 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000133 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000134
135 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000136 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000137
138 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000139 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000140
141 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000142 struct sshbuf *compression_buffer;
143
144 /* Incoming/outgoing compression dictionaries */
145 z_stream compression_in_stream;
146 z_stream compression_out_stream;
147 int compression_in_started;
148 int compression_out_started;
149 int compression_in_failures;
150 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000151
152 /*
153 * Flag indicating whether packet compression/decompression is
154 * enabled.
155 */
156 int packet_compression;
157
158 /* default maximum packet size */
159 u_int max_packet_size;
160
161 /* Flag indicating whether this module has been initialized. */
162 int initialized;
163
164 /* Set to true if the connection is interactive. */
165 int interactive_mode;
166
167 /* Set to true if we are the server side. */
168 int server_side;
169
170 /* Set to true if we are authenticated. */
171 int after_authentication;
172
173 int keep_alive_timeouts;
174
175 /* The maximum time that we will wait to send or receive a packet */
176 int packet_timeout_ms;
177
178 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000179 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000180 struct packet_state p_read, p_send;
181
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000182 /* Volume-based rekeying */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000183 u_int64_t max_blocks_in, max_blocks_out, rekey_limit;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000184
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000185 /* Time-based rekeying */
markus@openbsd.org02db4682015-02-13 18:57:00 +0000186 u_int32_t rekey_interval; /* how often in seconds */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000187 time_t rekey_time; /* time of last rekeying */
188
Darren Tuckerf7288d72009-06-21 18:12:20 +1000189 /* Session key for protocol v1 */
190 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
191 u_int ssh1_keylen;
192
193 /* roundup current message to extra_pad bytes */
194 u_char extra_pad;
195
196 /* XXX discard incoming data after MAC error */
197 u_int packet_discard;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000198 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000199
200 /* Used in packet_read_poll2() */
201 u_int packlen;
202
Darren Tucker7b935c72009-06-21 18:59:36 +1000203 /* Used in packet_send2 */
204 int rekeying;
205
206 /* Used in packet_set_interactive */
207 int set_interactive_called;
208
209 /* Used in packet_set_maxsize */
210 int set_maxsize_called;
211
markus@openbsd.org091c3022015-01-19 19:52:16 +0000212 /* One-off warning about weak ciphers */
213 int cipher_warning_done;
214
215 /* SSH1 CRC compensation attack detector */
216 struct deattack_ctx deattack;
217
Darren Tuckerf7288d72009-06-21 18:12:20 +1000218 TAILQ_HEAD(, packet) outgoing;
219};
220
markus@openbsd.org091c3022015-01-19 19:52:16 +0000221struct ssh *
222ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000223{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000224 struct ssh *ssh = NULL;
225 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000226
markus@openbsd.org091c3022015-01-19 19:52:16 +0000227 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
228 (state = calloc(1, sizeof(*state))) == NULL ||
229 (state->input = sshbuf_new()) == NULL ||
230 (state->output = sshbuf_new()) == NULL ||
231 (state->outgoing_packet = sshbuf_new()) == NULL ||
232 (state->incoming_packet = sshbuf_new()) == NULL)
233 goto fail;
234 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000235 TAILQ_INIT(&ssh->private_keys);
236 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000237 state->connection_in = -1;
238 state->connection_out = -1;
239 state->max_packet_size = 32768;
240 state->packet_timeout_ms = -1;
241 state->p_send.packets = state->p_read.packets = 0;
242 state->initialized = 1;
243 /*
244 * ssh_packet_send2() needs to queue packets until
245 * we've done the initial key exchange.
246 */
247 state->rekeying = 1;
248 ssh->state = state;
249 return ssh;
250 fail:
251 if (state) {
252 sshbuf_free(state->input);
253 sshbuf_free(state->output);
254 sshbuf_free(state->incoming_packet);
255 sshbuf_free(state->outgoing_packet);
256 free(state);
257 }
258 free(ssh);
259 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000260}
Damien Millera5539d22003-04-09 20:50:06 +1000261
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000262/* Returns nonzero if rekeying is in progress */
263int
264ssh_packet_is_rekeying(struct ssh *ssh)
265{
266 return ssh->state->rekeying ||
267 (ssh->kex != NULL && ssh->kex->done == 0);
268}
269
Damien Miller5428f641999-11-25 11:54:57 +1100270/*
271 * Sets the descriptors used for communication. Disables encryption until
272 * packet_set_encryption_key is called.
273 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000274struct ssh *
275ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000277 struct session_state *state;
278 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000279 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000280
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000281 if (none == NULL) {
282 error("%s: cannot load cipher 'none'", __func__);
283 return NULL;
284 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000285 if (ssh == NULL)
286 ssh = ssh_alloc_session_state();
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000287 if (ssh == NULL) {
288 error("%s: cound not allocate state", __func__);
289 return NULL;
290 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000291 state = ssh->state;
292 state->connection_in = fd_in;
293 state->connection_out = fd_out;
294 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000295 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000296 (r = cipher_init(&state->receive_context, none,
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000297 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
298 error("%s: cipher_init failed: %s", __func__, ssh_err(r));
jsg@openbsd.org1cb30162015-03-11 00:48:39 +0000299 free(ssh);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000300 return NULL;
301 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000302 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
303 deattack_init(&state->deattack);
djm@openbsd.orgd4c02952015-02-11 01:20:38 +0000304 /*
305 * Cache the IP address of the remote connection for use in error
306 * messages that might be generated after the connection has closed.
307 */
308 (void)ssh_remote_ipaddr(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000309 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310}
311
Darren Tucker3fc464e2008-06-13 06:42:45 +1000312void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000313ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000314{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000315 struct session_state *state = ssh->state;
316
Damien Miller8ed4de82011-12-19 10:52:50 +1100317 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000318 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000319 return;
320 }
321 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000322 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000323 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000324 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000325}
326
markus@openbsd.org091c3022015-01-19 19:52:16 +0000327int
328ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100329{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000330 struct session_state *state = ssh->state;
331 int r;
332
333 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100334 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000335
Damien Miller13ae44c2009-01-28 16:38:41 +1100336 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000337 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000338 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000339 if ((r = sshbuf_put(state->incoming_packet, buf,
340 sizeof(buf))) != 0)
341 return r;
342 (void) mac_compute(state->packet_discard_mac,
343 state->p_read.seqnr,
344 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
345 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100346 }
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000347 logit("Finished discarding for %.200s port %d",
348 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000349 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100350}
351
markus@openbsd.org091c3022015-01-19 19:52:16 +0000352static int
353ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
354 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100355{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000356 struct session_state *state = ssh->state;
357 int r;
358
359 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
360 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
361 return r;
362 return SSH_ERR_MAC_INVALID;
363 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100364 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000365 state->packet_discard_mac = mac;
366 if (sshbuf_len(state->input) >= discard &&
367 (r = ssh_packet_stop_discard(ssh)) != 0)
368 return r;
369 state->packet_discard = discard - sshbuf_len(state->input);
370 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100371}
372
Damien Miller34132e52000-01-14 15:45:46 +1100373/* Returns 1 if remote host is connected via socket, 0 if not. */
374
375int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000376ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100377{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000378 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100379 struct sockaddr_storage from, to;
380 socklen_t fromlen, tolen;
381
382 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000383 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100384 return 1;
385 fromlen = sizeof(from);
386 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000387 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000388 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100389 return 0;
390 tolen = sizeof(to);
391 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000392 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000393 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100394 return 0;
395 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
396 return 0;
397 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
398 return 0;
399 return 1;
400}
401
Ben Lindstromf6027d32002-03-22 01:42:04 +0000402void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000403ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000404{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000405 if (ibytes)
406 *ibytes = ssh->state->p_read.bytes;
407 if (obytes)
408 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000409}
410
411int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000412ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100413{
414 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100415 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100416
417 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000418 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000419 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100420 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000421#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100422 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000423 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000424 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000425#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000426 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100427}
428
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429/* Sets the connection into non-blocking mode. */
430
431void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000432ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433{
Damien Miller95def091999-11-25 00:26:21 +1100434 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000435 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
markus@openbsd.org091c3022015-01-19 19:52:16 +0000437 if (ssh->state->connection_out != ssh->state->connection_in)
438 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439}
440
441/* Returns the socket used for reading. */
442
443int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000444ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000446 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447}
448
449/* Returns the descriptor used for writing. */
450
451int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000452ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000454 return ssh->state->connection_out;
455}
456
457/*
458 * Returns the IP-address of the remote host as a string. The returned
459 * string must not be freed.
460 */
461
462const char *
463ssh_remote_ipaddr(struct ssh *ssh)
464{
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000465 const int sock = ssh->state->connection_in;
466
markus@openbsd.org091c3022015-01-19 19:52:16 +0000467 /* Check whether we have cached the ipaddr. */
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000468 if (ssh->remote_ipaddr == NULL) {
469 if (ssh_packet_connection_is_on_socket(ssh)) {
470 ssh->remote_ipaddr = get_peer_ipaddr(sock);
471 ssh->remote_port = get_sock_port(sock, 0);
472 } else {
473 ssh->remote_ipaddr = strdup("UNKNOWN");
474 ssh->remote_port = 0;
475 }
476 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000477 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478}
479
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000480/* Returns the port number of the remote host. */
481
482int
483ssh_remote_port(struct ssh *ssh)
484{
485 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
486 return ssh->remote_port;
487}
488
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489/* Closes the connection and clears and frees internal data structures. */
490
491void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000492ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000494 struct session_state *state = ssh->state;
495 int r;
496 u_int mode;
497
498 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100499 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000500 state->initialized = 0;
501 if (state->connection_in == state->connection_out) {
502 shutdown(state->connection_out, SHUT_RDWR);
503 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100504 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000505 close(state->connection_in);
506 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100507 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000508 sshbuf_free(state->input);
509 sshbuf_free(state->output);
510 sshbuf_free(state->outgoing_packet);
511 sshbuf_free(state->incoming_packet);
512 for (mode = 0; mode < MODE_MAX; mode++)
513 kex_free_newkeys(state->newkeys[mode]);
514 if (state->compression_buffer) {
515 sshbuf_free(state->compression_buffer);
516 if (state->compression_out_started) {
517 z_streamp stream = &state->compression_out_stream;
518 debug("compress outgoing: "
519 "raw data %llu, compressed %llu, factor %.2f",
520 (unsigned long long)stream->total_in,
521 (unsigned long long)stream->total_out,
522 stream->total_in == 0 ? 0.0 :
523 (double) stream->total_out / stream->total_in);
524 if (state->compression_out_failures == 0)
525 deflateEnd(stream);
526 }
527 if (state->compression_in_started) {
528 z_streamp stream = &state->compression_out_stream;
529 debug("compress incoming: "
530 "raw data %llu, compressed %llu, factor %.2f",
531 (unsigned long long)stream->total_out,
532 (unsigned long long)stream->total_in,
533 stream->total_out == 0 ? 0.0 :
534 (double) stream->total_in / stream->total_out);
535 if (state->compression_in_failures == 0)
536 inflateEnd(stream);
537 }
Damien Miller95def091999-11-25 00:26:21 +1100538 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000539 if ((r = cipher_cleanup(&state->send_context)) != 0)
540 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
541 if ((r = cipher_cleanup(&state->receive_context)) != 0)
542 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000543 free(ssh->remote_ipaddr);
544 ssh->remote_ipaddr = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000545 free(ssh->state);
546 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547}
548
549/* Sets remote side protocol flags. */
550
551void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000552ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000554 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555}
556
557/* Returns the remote protocol flags set earlier by the above function. */
558
Ben Lindstrom46c16222000-12-22 01:43:59 +0000559u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000560ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000562 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563}
564
Damien Miller5428f641999-11-25 11:54:57 +1100565/*
566 * Starts packet compression from the next packet on in both directions.
567 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
568 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
markus@openbsd.org091c3022015-01-19 19:52:16 +0000570static int
571ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000572{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000573 if (!ssh->state->compression_buffer &&
574 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
575 return SSH_ERR_ALLOC_FAIL;
576 return 0;
577}
578
579static int
580start_compression_out(struct ssh *ssh, int level)
581{
582 if (level < 1 || level > 9)
583 return SSH_ERR_INVALID_ARGUMENT;
584 debug("Enabling compression at level %d.", level);
585 if (ssh->state->compression_out_started == 1)
586 deflateEnd(&ssh->state->compression_out_stream);
587 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
588 case Z_OK:
589 ssh->state->compression_out_started = 1;
590 break;
591 case Z_MEM_ERROR:
592 return SSH_ERR_ALLOC_FAIL;
593 default:
594 return SSH_ERR_INTERNAL_ERROR;
595 }
596 return 0;
597}
598
599static int
600start_compression_in(struct ssh *ssh)
601{
602 if (ssh->state->compression_in_started == 1)
603 inflateEnd(&ssh->state->compression_in_stream);
604 switch (inflateInit(&ssh->state->compression_in_stream)) {
605 case Z_OK:
606 ssh->state->compression_in_started = 1;
607 break;
608 case Z_MEM_ERROR:
609 return SSH_ERR_ALLOC_FAIL;
610 default:
611 return SSH_ERR_INTERNAL_ERROR;
612 }
613 return 0;
614}
615
616int
617ssh_packet_start_compression(struct ssh *ssh, int level)
618{
619 int r;
620
621 if (ssh->state->packet_compression && !compat20)
622 return SSH_ERR_INTERNAL_ERROR;
623 ssh->state->packet_compression = 1;
624 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
625 (r = start_compression_in(ssh)) != 0 ||
626 (r = start_compression_out(ssh, level)) != 0)
627 return r;
628 return 0;
629}
630
631/* XXX remove need for separate compression buffer */
632static int
633compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
634{
635 u_char buf[4096];
636 int r, status;
637
638 if (ssh->state->compression_out_started != 1)
639 return SSH_ERR_INTERNAL_ERROR;
640
641 /* This case is not handled below. */
642 if (sshbuf_len(in) == 0)
643 return 0;
644
645 /* Input is the contents of the input buffer. */
646 if ((ssh->state->compression_out_stream.next_in =
647 sshbuf_mutable_ptr(in)) == NULL)
648 return SSH_ERR_INTERNAL_ERROR;
649 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
650
651 /* Loop compressing until deflate() returns with avail_out != 0. */
652 do {
653 /* Set up fixed-size output buffer. */
654 ssh->state->compression_out_stream.next_out = buf;
655 ssh->state->compression_out_stream.avail_out = sizeof(buf);
656
657 /* Compress as much data into the buffer as possible. */
658 status = deflate(&ssh->state->compression_out_stream,
659 Z_PARTIAL_FLUSH);
660 switch (status) {
661 case Z_MEM_ERROR:
662 return SSH_ERR_ALLOC_FAIL;
663 case Z_OK:
664 /* Append compressed data to output_buffer. */
665 if ((r = sshbuf_put(out, buf, sizeof(buf) -
666 ssh->state->compression_out_stream.avail_out)) != 0)
667 return r;
668 break;
669 case Z_STREAM_ERROR:
670 default:
671 ssh->state->compression_out_failures++;
672 return SSH_ERR_INVALID_FORMAT;
673 }
674 } while (ssh->state->compression_out_stream.avail_out == 0);
675 return 0;
676}
677
678static int
679uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
680{
681 u_char buf[4096];
682 int r, status;
683
684 if (ssh->state->compression_in_started != 1)
685 return SSH_ERR_INTERNAL_ERROR;
686
687 if ((ssh->state->compression_in_stream.next_in =
688 sshbuf_mutable_ptr(in)) == NULL)
689 return SSH_ERR_INTERNAL_ERROR;
690 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
691
692 for (;;) {
693 /* Set up fixed-size output buffer. */
694 ssh->state->compression_in_stream.next_out = buf;
695 ssh->state->compression_in_stream.avail_out = sizeof(buf);
696
697 status = inflate(&ssh->state->compression_in_stream,
698 Z_PARTIAL_FLUSH);
699 switch (status) {
700 case Z_OK:
701 if ((r = sshbuf_put(out, buf, sizeof(buf) -
702 ssh->state->compression_in_stream.avail_out)) != 0)
703 return r;
704 break;
705 case Z_BUF_ERROR:
706 /*
707 * Comments in zlib.h say that we should keep calling
708 * inflate() until we get an error. This appears to
709 * be the error that we get.
710 */
711 return 0;
712 case Z_DATA_ERROR:
713 return SSH_ERR_INVALID_FORMAT;
714 case Z_MEM_ERROR:
715 return SSH_ERR_ALLOC_FAIL;
716 case Z_STREAM_ERROR:
717 default:
718 ssh->state->compression_in_failures++;
719 return SSH_ERR_INTERNAL_ERROR;
720 }
721 }
722 /* NOTREACHED */
723}
724
725/* Serialise compression state into a blob for privsep */
726static int
727ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
728{
729 struct session_state *state = ssh->state;
730 struct sshbuf *b;
731 int r;
732
733 if ((b = sshbuf_new()) == NULL)
734 return SSH_ERR_ALLOC_FAIL;
735 if (state->compression_in_started) {
736 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
737 sizeof(state->compression_in_stream))) != 0)
738 goto out;
739 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
740 goto out;
741 if (state->compression_out_started) {
742 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
743 sizeof(state->compression_out_stream))) != 0)
744 goto out;
745 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
746 goto out;
747 r = sshbuf_put_stringb(m, b);
748 out:
749 sshbuf_free(b);
750 return r;
751}
752
753/* Deserialise compression state from a blob for privsep */
754static int
755ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
756{
757 struct session_state *state = ssh->state;
758 struct sshbuf *b = NULL;
759 int r;
760 const u_char *inblob, *outblob;
761 size_t inl, outl;
762
763 if ((r = sshbuf_froms(m, &b)) != 0)
764 goto out;
765 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
766 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
767 goto out;
768 if (inl == 0)
769 state->compression_in_started = 0;
770 else if (inl != sizeof(state->compression_in_stream)) {
771 r = SSH_ERR_INTERNAL_ERROR;
772 goto out;
773 } else {
774 state->compression_in_started = 1;
775 memcpy(&state->compression_in_stream, inblob, inl);
776 }
777 if (outl == 0)
778 state->compression_out_started = 0;
779 else if (outl != sizeof(state->compression_out_stream)) {
780 r = SSH_ERR_INTERNAL_ERROR;
781 goto out;
782 } else {
783 state->compression_out_started = 1;
784 memcpy(&state->compression_out_stream, outblob, outl);
785 }
786 r = 0;
787 out:
788 sshbuf_free(b);
789 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000790}
791
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000793ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
794 void *(*allocfunc)(void *, u_int, u_int),
795 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000797 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
798 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
799 ssh->state->compression_out_stream.opaque = ctx;
800 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
801 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
802 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803}
804
Damien Miller5428f641999-11-25 11:54:57 +1100805/*
Damien Miller5428f641999-11-25 11:54:57 +1100806 * Causes any further packets to be encrypted using the given key. The same
807 * key is used for both sending and reception. However, both directions are
808 * encrypted independently of each other.
809 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000810
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000812ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000813{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000814#ifndef WITH_SSH1
815 fatal("no SSH protocol 1 support");
816#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000817 struct session_state *state = ssh->state;
818 const struct sshcipher *cipher = cipher_by_number(number);
819 int r;
820 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100821
markus@openbsd.org091c3022015-01-19 19:52:16 +0000822 if (cipher == NULL)
823 fatal("%s: unknown cipher number %d", __func__, number);
824 if (keylen < 20)
825 fatal("%s: keylen too small: %d", __func__, keylen);
826 if (keylen > SSH_SESSION_KEY_LENGTH)
827 fatal("%s: keylen too big: %d", __func__, keylen);
828 memcpy(state->ssh1_key, key, keylen);
829 state->ssh1_keylen = keylen;
830 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
831 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
832 (r = cipher_init(&state->receive_context, cipher, key, keylen,
833 NULL, 0, CIPHER_DECRYPT) != 0))
834 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
835 if (!state->cipher_warning_done &&
836 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
837 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
838 error("Warning: %s", wmsg);
839 state->cipher_warning_done = 1;
840 }
Damien Miller773dda22015-01-30 23:10:17 +1100841#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000842}
843
Damien Miller5428f641999-11-25 11:54:57 +1100844/*
845 * Finalizes and sends the packet. If the encryption key has been set,
846 * encrypts the packet before sending.
847 */
Damien Miller95def091999-11-25 00:26:21 +1100848
markus@openbsd.org091c3022015-01-19 19:52:16 +0000849int
850ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000852 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000853 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000854 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000855 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856
Damien Miller5428f641999-11-25 11:54:57 +1100857 /*
858 * If using packet compression, compress the payload of the outgoing
859 * packet.
860 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000861 if (state->packet_compression) {
862 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100863 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000864 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
865 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100866 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000867 if ((r = sshbuf_put(state->compression_buffer,
868 "\0\0\0\0\0\0\0\0", 8)) != 0)
869 goto out;
870 if ((r = compress_buffer(ssh, state->outgoing_packet,
871 state->compression_buffer)) != 0)
872 goto out;
873 sshbuf_reset(state->outgoing_packet);
874 if ((r = sshbuf_putb(state->outgoing_packet,
875 state->compression_buffer)) != 0)
876 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100877 }
878 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000879 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880
Damien Millere247cc42000-05-07 12:03:14 +1000881 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100882 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000883 if (!state->send_context.plaintext) {
884 cp = sshbuf_mutable_ptr(state->outgoing_packet);
885 if (cp == NULL) {
886 r = SSH_ERR_INTERNAL_ERROR;
887 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100888 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000889 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100890 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000891 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
892 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100893
894 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000895 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
896 sshbuf_len(state->outgoing_packet));
897 POKE_U32(buf, checksum);
898 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
899 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900
901#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100902 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000903 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904#endif
905
Damien Miller95def091999-11-25 00:26:21 +1100906 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000907 POKE_U32(buf, len);
908 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
909 goto out;
910 if ((r = sshbuf_reserve(state->output,
911 sshbuf_len(state->outgoing_packet), &cp)) != 0)
912 goto out;
913 if ((r = cipher_crypt(&state->send_context, 0, cp,
914 sshbuf_ptr(state->outgoing_packet),
915 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
916 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100917
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100919 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000920 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000921#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000922 state->p_send.packets++;
923 state->p_send.bytes += len +
924 sshbuf_len(state->outgoing_packet);
925 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926
Damien Miller5428f641999-11-25 11:54:57 +1100927 /*
Damien Miller788f2122005-11-05 15:14:59 +1100928 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000929 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
930 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100931 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000932 r = 0;
933 out:
934 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935}
936
markus@openbsd.org091c3022015-01-19 19:52:16 +0000937int
938ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000939{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000940 struct session_state *state = ssh->state;
941 struct sshenc *enc;
942 struct sshmac *mac;
943 struct sshcomp *comp;
944 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000945 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000946 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000947 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000948
Ben Lindstrom064496f2002-12-23 02:04:22 +0000949 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000950
Damien Miller963f6b22002-02-19 15:21:23 +1100951 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000952 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000953 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000954 state->p_send.packets = state->p_send.blocks = 0;
955 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100956 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000957 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000958 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000959 state->p_read.packets = state->p_read.blocks = 0;
960 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100961 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000962 if (state->newkeys[mode] != NULL) {
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000963 debug("set_newkeys: rekeying, input %llu bytes %llu blocks, "
964 "output %llu bytes %llu blocks",
djm@openbsd.org696d1262016-02-04 23:43:48 +0000965 (unsigned long long)state->p_read.bytes,
966 (unsigned long long)state->p_read.blocks,
967 (unsigned long long)state->p_send.bytes,
968 (unsigned long long)state->p_send.blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000969 if ((r = cipher_cleanup(cc)) != 0)
970 return r;
971 enc = &state->newkeys[mode]->enc;
972 mac = &state->newkeys[mode]->mac;
973 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000974 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100975 explicit_bzero(enc->iv, enc->iv_len);
976 explicit_bzero(enc->key, enc->key_len);
977 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000978 free(enc->name);
979 free(enc->iv);
980 free(enc->key);
981 free(mac->name);
982 free(mac->key);
983 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000984 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000985 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000986 /* move newkeys from kex to state */
987 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
988 return SSH_ERR_INTERNAL_ERROR;
989 ssh->kex->newkeys[mode] = NULL;
990 enc = &state->newkeys[mode]->enc;
991 mac = &state->newkeys[mode]->mac;
992 comp = &state->newkeys[mode]->comp;
993 if (cipher_authlen(enc->cipher) == 0) {
994 if ((r = mac_init(mac)) != 0)
995 return r;
996 }
997 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000998 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000999 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
1000 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001001 return r;
1002 if (!state->cipher_warning_done &&
1003 (wmsg = cipher_warning_message(cc)) != NULL) {
1004 error("Warning: %s", wmsg);
1005 state->cipher_warning_done = 1;
1006 }
Ben Lindstromf6027d32002-03-22 01:42:04 +00001007 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +11001008 /* explicit_bzero(enc->iv, enc->block_size);
1009 explicit_bzero(enc->key, enc->key_len);
1010 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +10001011 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +10001012 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +00001013 state->after_authentication)) && comp->enabled == 0) {
1014 if ((r = ssh_packet_init_compression(ssh)) < 0)
1015 return r;
1016 if (mode == MODE_OUT) {
1017 if ((r = start_compression_out(ssh, 6)) != 0)
1018 return r;
1019 } else {
1020 if ((r = start_compression_in(ssh)) != 0)
1021 return r;
1022 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001023 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001024 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001025 /*
1026 * The 2^(blocksize*2) limit is too expensive for 3DES,
1027 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1028 */
1029 if (enc->block_size >= 16)
1030 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1031 else
1032 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001033 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001034 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001035 state->rekey_limit / enc->block_size);
djm@openbsd.org696d1262016-02-04 23:43:48 +00001036 debug("rekey after %llu blocks", (unsigned long long)*max_blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001037 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001038}
1039
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001040#define MAX_PACKETS (1U<<31)
1041static int
1042ssh_packet_need_rekeying(struct ssh *ssh, u_int outbound_packet_len)
1043{
1044 struct session_state *state = ssh->state;
1045 u_int32_t out_blocks;
1046
1047 /* XXX client can't cope with rekeying pre-auth */
1048 if (!state->after_authentication)
1049 return 0;
1050
1051 /* Haven't keyed yet or KEX in progress. */
1052 if (ssh->kex == NULL || ssh_packet_is_rekeying(ssh))
1053 return 0;
1054
1055 /* Peer can't rekey */
1056 if (ssh->compat & SSH_BUG_NOREKEY)
1057 return 0;
1058
1059 /*
1060 * Permit one packet in or out per rekey - this allows us to
1061 * make progress when rekey limits are very small.
1062 */
1063 if (state->p_send.packets == 0 && state->p_read.packets == 0)
1064 return 0;
1065
1066 /* Time-based rekeying */
1067 if (state->rekey_interval != 0 &&
1068 state->rekey_time + state->rekey_interval <= monotime())
1069 return 1;
1070
1071 /* Always rekey when MAX_PACKETS sent in either direction */
1072 if (state->p_send.packets > MAX_PACKETS ||
1073 state->p_read.packets > MAX_PACKETS)
1074 return 1;
1075
1076 /* Rekey after (cipher-specific) maxiumum blocks */
1077 out_blocks = roundup(outbound_packet_len,
1078 state->newkeys[MODE_OUT]->enc.block_size);
1079 return (state->max_blocks_out &&
1080 (state->p_send.blocks + out_blocks > state->max_blocks_out)) ||
1081 (state->max_blocks_in &&
1082 (state->p_read.blocks > state->max_blocks_in));
1083}
1084
Damien Miller5428f641999-11-25 11:54:57 +11001085/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001086 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001087 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001088 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1089 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001090static int
1091ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001092{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001093 struct session_state *state = ssh->state;
1094 struct sshcomp *comp = NULL;
1095 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001096
1097 /*
1098 * Remember that we are past the authentication step, so rekeying
1099 * with COMP_DELAYED will turn on compression immediately.
1100 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001101 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001102 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001103 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001104 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001105 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001106 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001107 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001108 if ((r = ssh_packet_init_compression(ssh)) != 0)
1109 return r;
1110 if (mode == MODE_OUT) {
1111 if ((r = start_compression_out(ssh, 6)) != 0)
1112 return r;
1113 } else {
1114 if ((r = start_compression_in(ssh)) != 0)
1115 return r;
1116 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001117 comp->enabled = 1;
1118 }
1119 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001120 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001121}
1122
djm@openbsd.org28136472016-01-29 05:46:01 +00001123/* Used to mute debug logging for noisy packet types */
1124static int
1125ssh_packet_log_type(u_char type)
1126{
1127 switch (type) {
1128 case SSH2_MSG_CHANNEL_DATA:
1129 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
1130 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
1131 return 0;
1132 default:
1133 return 1;
1134 }
1135}
1136
Damien Miller9786e6e2005-07-26 21:54:56 +10001137/*
Damien Miller33b13562000-04-04 14:38:59 +10001138 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1139 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001140int
1141ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001142{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001143 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001144 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001145 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001146 u_int authlen = 0, aadlen = 0;
1147 u_int len;
1148 struct sshenc *enc = NULL;
1149 struct sshmac *mac = NULL;
1150 struct sshcomp *comp = NULL;
1151 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001152
markus@openbsd.org091c3022015-01-19 19:52:16 +00001153 if (state->newkeys[MODE_OUT] != NULL) {
1154 enc = &state->newkeys[MODE_OUT]->enc;
1155 mac = &state->newkeys[MODE_OUT]->mac;
1156 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001157 /* disable mac for authenticated encryption */
1158 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1159 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001160 }
Damien Miller963f6b22002-02-19 15:21:23 +11001161 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001162 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001163
markus@openbsd.org091c3022015-01-19 19:52:16 +00001164 type = (sshbuf_ptr(state->outgoing_packet))[5];
djm@openbsd.org28136472016-01-29 05:46:01 +00001165 if (ssh_packet_log_type(type))
1166 debug3("send packet: type %u", type);
Damien Miller33b13562000-04-04 14:38:59 +10001167#ifdef PACKET_DEBUG
1168 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001169 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001170#endif
1171
1172 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001173 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001174 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001175 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1176 goto out;
1177 sshbuf_reset(state->compression_buffer);
1178 if ((r = compress_buffer(ssh, state->outgoing_packet,
1179 state->compression_buffer)) != 0)
1180 goto out;
1181 sshbuf_reset(state->outgoing_packet);
1182 if ((r = sshbuf_put(state->outgoing_packet,
1183 "\0\0\0\0\0", 5)) != 0 ||
1184 (r = sshbuf_putb(state->outgoing_packet,
1185 state->compression_buffer)) != 0)
1186 goto out;
1187 DBG(debug("compression: raw %d compressed %zd", len,
1188 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001189 }
1190
1191 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001192 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001193
1194 /*
1195 * calc size of padding, alloc space, get random data,
1196 * minimum padding is 4 bytes
1197 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001198 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001199 padlen = block_size - (len % block_size);
1200 if (padlen < 4)
1201 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001202 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001203 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001204 state->extra_pad =
1205 roundup(state->extra_pad, block_size);
1206 pad = state->extra_pad -
1207 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001208 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001209 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001210 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001211 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001212 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001213 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1214 goto out;
1215 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001216 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001217 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001218 } else {
1219 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001220 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001221 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001222 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001223 len = sshbuf_len(state->outgoing_packet);
1224 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1225 if (cp == NULL) {
1226 r = SSH_ERR_INTERNAL_ERROR;
1227 goto out;
1228 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001229 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001230 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001231 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001232 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1233 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001234
1235 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001236 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001237 if ((r = mac_compute(mac, state->p_send.seqnr,
1238 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001239 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001240 goto out;
1241 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001242 }
1243 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001244 if ((r = sshbuf_reserve(state->output,
1245 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1246 goto out;
1247 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1248 sshbuf_ptr(state->outgoing_packet),
1249 len - aadlen, aadlen, authlen)) != 0)
1250 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001251 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001252 if (mac && mac->enabled) {
1253 if (mac->etm) {
1254 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001255 if ((r = mac_compute(mac, state->p_send.seqnr,
1256 cp, len, macbuf, sizeof(macbuf))) != 0)
1257 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001258 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001259 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001260 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001261 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1262 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001263 }
Damien Miller33b13562000-04-04 14:38:59 +10001264#ifdef PACKET_DEBUG
1265 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001266 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001267#endif
Damien Miller4af51302000-04-16 11:18:38 +10001268 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001269 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001270 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001271 if (++state->p_send.packets == 0)
1272 if (!(ssh->compat & SSH_BUG_NOREKEY))
1273 return SSH_ERR_NEED_REKEY;
1274 state->p_send.blocks += len / block_size;
1275 state->p_send.bytes += len;
1276 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001277
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001278 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001279 r = ssh_set_newkeys(ssh, MODE_OUT);
1280 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1281 r = ssh_packet_enable_delayed_compress(ssh);
1282 else
1283 r = 0;
1284 out:
1285 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001286}
1287
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001288/* returns non-zero if the specified packet type is usec by KEX */
1289static int
1290ssh_packet_type_is_kex(u_char type)
1291{
1292 return
1293 type >= SSH2_MSG_TRANSPORT_MIN &&
1294 type <= SSH2_MSG_TRANSPORT_MAX &&
1295 type != SSH2_MSG_SERVICE_REQUEST &&
1296 type != SSH2_MSG_SERVICE_ACCEPT &&
1297 type != SSH2_MSG_EXT_INFO;
1298}
1299
markus@openbsd.org091c3022015-01-19 19:52:16 +00001300int
1301ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001302{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001303 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001304 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001305 u_char type;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001306 int r, need_rekey;
Damien Millera5539d22003-04-09 20:50:06 +10001307
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001308 if (sshbuf_len(state->outgoing_packet) < 6)
1309 return SSH_ERR_INTERNAL_ERROR;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001310 type = sshbuf_ptr(state->outgoing_packet)[5];
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001311 need_rekey = !ssh_packet_type_is_kex(type) &&
1312 ssh_packet_need_rekeying(ssh, sshbuf_len(state->outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +10001313
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001314 /*
1315 * During rekeying we can only send key exchange messages.
1316 * Queue everything else.
1317 */
1318 if ((need_rekey || state->rekeying) && !ssh_packet_type_is_kex(type)) {
1319 if (need_rekey)
1320 debug3("%s: rekex triggered", __func__);
1321 debug("enqueue packet: %u", type);
1322 p = calloc(1, sizeof(*p));
1323 if (p == NULL)
1324 return SSH_ERR_ALLOC_FAIL;
1325 p->type = type;
1326 p->payload = state->outgoing_packet;
1327 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1328 state->outgoing_packet = sshbuf_new();
1329 if (state->outgoing_packet == NULL)
1330 return SSH_ERR_ALLOC_FAIL;
1331 if (need_rekey) {
1332 /*
1333 * This packet triggered a rekey, so send the
1334 * KEXINIT now.
1335 * NB. reenters this function via kex_start_rekex().
1336 */
1337 return kex_start_rekex(ssh);
Damien Millera5539d22003-04-09 20:50:06 +10001338 }
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001339 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001340 }
1341
1342 /* rekeying starts with sending KEXINIT */
1343 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001344 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001345
markus@openbsd.org091c3022015-01-19 19:52:16 +00001346 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1347 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001348
1349 /* after a NEWKEYS message we can send the complete queue */
1350 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001351 state->rekeying = 0;
1352 state->rekey_time = monotime();
1353 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001354 type = p->type;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001355 /*
1356 * If this packet triggers a rekex, then skip the
1357 * remaining packets in the queue for now.
1358 * NB. re-enters this function via kex_start_rekex.
1359 */
1360 if (ssh_packet_need_rekeying(ssh,
1361 sshbuf_len(p->payload))) {
1362 debug3("%s: queued packet triggered rekex",
1363 __func__);
1364 return kex_start_rekex(ssh);
1365 }
Damien Millera5539d22003-04-09 20:50:06 +10001366 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001367 sshbuf_free(state->outgoing_packet);
1368 state->outgoing_packet = p->payload;
1369 TAILQ_REMOVE(&state->outgoing, p, next);
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001370 memset(p, 0, sizeof(*p));
Darren Tuckera627d422013-06-02 07:31:17 +10001371 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001372 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1373 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001374 }
1375 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001376 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001377}
1378
Damien Miller33b13562000-04-04 14:38:59 +10001379/*
Damien Miller5428f641999-11-25 11:54:57 +11001380 * Waits until a packet has been received, and returns its type. Note that
1381 * no other data is processed until this returns, so this function should not
1382 * be used during the interactive session.
1383 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384
1385int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001386ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001388 struct session_state *state = ssh->state;
markus@openbsd.orga3068632016-01-14 16:17:39 +00001389 int len, r, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001390 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001391 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001392 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393
Darren Tucker99bb7612008-06-13 22:02:50 +10001394 DBG(debug("packet_read()"));
1395
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001396 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001397 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001398 if (setp == NULL)
1399 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001400
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001401 /*
1402 * Since we are blocking, ensure that all written packets have
1403 * been sent.
1404 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001405 if ((r = ssh_packet_write_wait(ssh)) != 0)
1406 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407
Damien Miller95def091999-11-25 00:26:21 +11001408 /* Stay in the loop until we have received a complete packet. */
1409 for (;;) {
1410 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001411 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1412 if (r != 0)
1413 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001414 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001415 *typep == SSH_SMSG_SUCCESS
1416 || *typep == SSH_SMSG_FAILURE
1417 || *typep == SSH_CMSG_EOF
1418 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1419 if ((r = sshpkt_get_end(ssh)) != 0)
1420 break;
Damien Miller95def091999-11-25 00:26:21 +11001421 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001422 if (*typep != SSH_MSG_NONE)
1423 break;
Damien Miller5428f641999-11-25 11:54:57 +11001424 /*
1425 * Otherwise, wait for some data to arrive, add it to the
1426 * buffer, and try again.
1427 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001428 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001429 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001430 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001431
markus@openbsd.org091c3022015-01-19 19:52:16 +00001432 if (state->packet_timeout_ms > 0) {
1433 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001434 timeoutp = &timeout;
1435 }
Damien Miller95def091999-11-25 00:26:21 +11001436 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001437 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001438 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001439 ms_to_timeval(&timeout, ms_remain);
1440 gettimeofday(&start, NULL);
1441 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001442 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001443 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001444 break;
Damien Millerea437422009-10-02 11:49:03 +10001445 if (errno != EAGAIN && errno != EINTR &&
1446 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001447 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001448 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001449 continue;
1450 ms_subtract_diff(&start, &ms_remain);
1451 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001452 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001453 break;
1454 }
1455 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001456 if (r == 0)
1457 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001458 /* Read data from the socket. */
markus@openbsd.orga3068632016-01-14 16:17:39 +00001459 len = read(state->connection_in, buf, sizeof(buf));
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001460 if (len == 0) {
1461 r = SSH_ERR_CONN_CLOSED;
1462 goto out;
1463 }
1464 if (len < 0) {
1465 r = SSH_ERR_SYSTEM_ERROR;
1466 goto out;
1467 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001468
Damien Miller95def091999-11-25 00:26:21 +11001469 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001470 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001471 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001472 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001473 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001474 free(setp);
1475 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476}
1477
Damien Miller278f9072001-12-21 15:00:19 +11001478int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001479ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001480{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001481 u_char type;
1482 int r;
1483
1484 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1485 fatal("%s: %s", __func__, ssh_err(r));
1486 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001487}
1488
Damien Miller5428f641999-11-25 11:54:57 +11001489/*
1490 * Waits until a packet has been received, verifies that its type matches
1491 * that given, and gives a fatal error and exits if there is a mismatch.
1492 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001494int
1495ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001496{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001497 int r;
1498 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001500 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1501 return r;
1502 if (type != expected_type) {
1503 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001504 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001505 expected_type, type)) != 0)
1506 return r;
1507 return SSH_ERR_PROTOCOL_ERROR;
1508 }
1509 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001510}
1511
1512/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001513 * packet_process_incoming. If so, reads the packet; otherwise returns
1514 * SSH_MSG_NONE. This does not wait for data from the connection.
1515 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001516 * SSH_MSG_DISCONNECT is handled specially here. Also,
1517 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1518 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001519 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001520
markus@openbsd.org091c3022015-01-19 19:52:16 +00001521int
1522ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001523{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001524 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001525 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001526 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001527 const u_char *cp;
1528 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001529 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001530 int r;
1531
1532 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001533
Damien Miller95def091999-11-25 00:26:21 +11001534 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001535 if (sshbuf_len(state->input) < 4 + 8)
1536 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001537 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001538 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001539 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1540 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1541 len)) != 0)
1542 return r;
1543 return SSH_ERR_CONN_CORRUPT;
1544 }
Damien Miller95def091999-11-25 00:26:21 +11001545 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001546
Damien Miller95def091999-11-25 00:26:21 +11001547 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001548 if (sshbuf_len(state->input) < 4 + padded_len)
1549 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001550
Damien Miller95def091999-11-25 00:26:21 +11001551 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552
Damien Miller95def091999-11-25 00:26:21 +11001553 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001554 if ((r = sshbuf_consume(state->input, 4)) != 0)
1555 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001556
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001557 /*
1558 * Cryptographic attack detector for ssh
1559 * (C)1998 CORE-SDI, Buenos Aires Argentina
1560 * Ariel Futoransky(futo@core-sdi.com)
1561 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001562 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001563 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001564 switch (detect_attack(&state->deattack,
1565 sshbuf_ptr(state->input), padded_len)) {
1566 case DEATTACK_OK:
1567 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001568 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001569 emsg = "crc32 compensation attack detected";
1570 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001571 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001572 emsg = "deattack denial of service detected";
1573 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001574 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001575 emsg = "deattack error";
1576 break;
1577 }
1578 if (emsg != NULL) {
1579 error("%s", emsg);
1580 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1581 (r = ssh_packet_write_wait(ssh)) != 0)
1582 return r;
1583 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001584 }
1585 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001586
1587 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001588 sshbuf_reset(state->incoming_packet);
1589 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1590 goto out;
1591 if ((r = cipher_crypt(&state->receive_context, 0, p,
1592 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1593 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001594
markus@openbsd.org091c3022015-01-19 19:52:16 +00001595 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1596 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001597
1598#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001599 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001600 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001601#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001602
Damien Miller95def091999-11-25 00:26:21 +11001603 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001604 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1605 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001606
Damien Miller95def091999-11-25 00:26:21 +11001607 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001608 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1609 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001610
Damien Miller95def091999-11-25 00:26:21 +11001611 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001612 if (len != sshbuf_len(state->incoming_packet)) {
1613 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001614 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001615 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1616 (r = ssh_packet_write_wait(ssh)) != 0)
1617 return r;
1618 return SSH_ERR_CONN_CORRUPT;
1619 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001620
markus@openbsd.org091c3022015-01-19 19:52:16 +00001621 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1622 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001623 if (checksum != stored_checksum) {
1624 error("Corrupted check bytes on input");
1625 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1626 (r = ssh_packet_write_wait(ssh)) != 0)
1627 return r;
1628 return SSH_ERR_CONN_CORRUPT;
1629 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001630 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1631 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001632
markus@openbsd.org091c3022015-01-19 19:52:16 +00001633 if (state->packet_compression) {
1634 sshbuf_reset(state->compression_buffer);
1635 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1636 state->compression_buffer)) != 0)
1637 goto out;
1638 sshbuf_reset(state->incoming_packet);
1639 if ((r = sshbuf_putb(state->incoming_packet,
1640 state->compression_buffer)) != 0)
1641 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001642 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001643 state->p_read.packets++;
1644 state->p_read.bytes += padded_len + 4;
1645 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1646 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001647 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1648 error("Invalid ssh1 packet type: %d", *typep);
1649 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1650 (r = ssh_packet_write_wait(ssh)) != 0)
1651 return r;
1652 return SSH_ERR_PROTOCOL_ERROR;
1653 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001654 r = 0;
1655 out:
1656 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001657}
Damien Miller95def091999-11-25 00:26:21 +11001658
markus@openbsd.org091c3022015-01-19 19:52:16 +00001659int
1660ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001661{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001662 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001663 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001664 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1665 u_int maclen, aadlen = 0, authlen = 0, block_size;
1666 struct sshenc *enc = NULL;
1667 struct sshmac *mac = NULL;
1668 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001669 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001670
markus@openbsd.org091c3022015-01-19 19:52:16 +00001671 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001672
markus@openbsd.org091c3022015-01-19 19:52:16 +00001673 if (state->packet_discard)
1674 return 0;
1675
1676 if (state->newkeys[MODE_IN] != NULL) {
1677 enc = &state->newkeys[MODE_IN]->enc;
1678 mac = &state->newkeys[MODE_IN]->mac;
1679 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001680 /* disable mac for authenticated encryption */
1681 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1682 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001683 }
1684 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001685 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001686 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001687
markus@openbsd.org091c3022015-01-19 19:52:16 +00001688 if (aadlen && state->packlen == 0) {
1689 if (cipher_get_length(&state->receive_context,
1690 &state->packlen, state->p_read.seqnr,
1691 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1692 return 0;
1693 if (state->packlen < 1 + 4 ||
1694 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001695#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001696 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001697#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001698 logit("Bad packet length %u.", state->packlen);
1699 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1700 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001701 return SSH_ERR_CONN_CORRUPT;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001702 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001703 sshbuf_reset(state->incoming_packet);
1704 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001705 /*
1706 * check if input size is less than the cipher block size,
1707 * decrypt first block and extract length of incoming packet
1708 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001709 if (sshbuf_len(state->input) < block_size)
1710 return 0;
1711 sshbuf_reset(state->incoming_packet);
1712 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1713 &cp)) != 0)
1714 goto out;
1715 if ((r = cipher_crypt(&state->receive_context,
1716 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1717 block_size, 0, 0)) != 0)
1718 goto out;
1719 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1720 if (state->packlen < 1 + 4 ||
1721 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001722#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001723 fprintf(stderr, "input: \n");
1724 sshbuf_dump(state->input, stderr);
1725 fprintf(stderr, "incoming_packet: \n");
1726 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001727#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001728 logit("Bad packet length %u.", state->packlen);
1729 return ssh_packet_start_discard(ssh, enc, mac,
1730 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001731 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001732 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1733 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001734 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001735 DBG(debug("input: packet len %u", state->packlen+4));
1736
Damien Milleraf43a7a2012-12-12 10:46:31 +11001737 if (aadlen) {
1738 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001739 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001740 } else {
1741 /*
1742 * the payload size and the payload are encrypted, but we
1743 * have a partial packet of block_size bytes
1744 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001745 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001746 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001747 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1748 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001749 if (need % block_size != 0) {
1750 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001751 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001752 return ssh_packet_start_discard(ssh, enc, mac,
1753 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001754 }
Damien Miller33b13562000-04-04 14:38:59 +10001755 /*
1756 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001757 * decrypt into incoming_packet:
1758 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001759 * 'need' bytes are encrypted, followed by either
1760 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001761 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001762 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001763 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1764 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001765#ifdef PACKET_DEBUG
1766 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001767 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001768#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001769 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001770 if (mac && mac->enabled && mac->etm) {
1771 if ((r = mac_compute(mac, state->p_read.seqnr,
1772 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001773 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001774 goto out;
1775 }
1776 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1777 &cp)) != 0)
1778 goto out;
1779 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1780 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1781 goto out;
1782 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1783 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001784 /*
1785 * compute MAC over seqnr and packet,
1786 * increment sequence number for incoming packet
1787 */
Damien Miller4af51302000-04-16 11:18:38 +10001788 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001789 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001790 if ((r = mac_compute(mac, state->p_read.seqnr,
1791 sshbuf_ptr(state->incoming_packet),
1792 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001793 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001794 goto out;
1795 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001796 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001797 logit("Corrupted MAC on input.");
1798 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001799 return SSH_ERR_INTERNAL_ERROR;
1800 return ssh_packet_start_discard(ssh, enc, mac,
1801 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001802 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001803
1804 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1805 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1806 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001807 }
Damien Miller278f9072001-12-21 15:00:19 +11001808 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001809 *seqnr_p = state->p_read.seqnr;
1810 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001811 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001812 if (++state->p_read.packets == 0)
1813 if (!(ssh->compat & SSH_BUG_NOREKEY))
1814 return SSH_ERR_NEED_REKEY;
1815 state->p_read.blocks += (state->packlen + 4) / block_size;
1816 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001817
1818 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001819 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001820 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001821 if (padlen < 4) {
1822 if ((r = sshpkt_disconnect(ssh,
1823 "Corrupted padlen %d on input.", padlen)) != 0 ||
1824 (r = ssh_packet_write_wait(ssh)) != 0)
1825 return r;
1826 return SSH_ERR_CONN_CORRUPT;
1827 }
Damien Miller33b13562000-04-04 14:38:59 +10001828
1829 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001830 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1831 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1832 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001833
markus@openbsd.org091c3022015-01-19 19:52:16 +00001834 DBG(debug("input: len before de-compress %zd",
1835 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001836 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001837 sshbuf_reset(state->compression_buffer);
1838 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1839 state->compression_buffer)) != 0)
1840 goto out;
1841 sshbuf_reset(state->incoming_packet);
1842 if ((r = sshbuf_putb(state->incoming_packet,
1843 state->compression_buffer)) != 0)
1844 goto out;
1845 DBG(debug("input: len after de-compress %zd",
1846 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001847 }
1848 /*
1849 * get packet type, implies consume.
1850 * return length of payload (without type field)
1851 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001852 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1853 goto out;
djm@openbsd.org28136472016-01-29 05:46:01 +00001854 if (ssh_packet_log_type(*typep))
1855 debug3("receive packet: type %u", *typep);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001856 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1857 if ((r = sshpkt_disconnect(ssh,
1858 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1859 (r = ssh_packet_write_wait(ssh)) != 0)
1860 return r;
1861 return SSH_ERR_PROTOCOL_ERROR;
1862 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001863 if (*typep == SSH2_MSG_NEWKEYS)
1864 r = ssh_set_newkeys(ssh, MODE_IN);
1865 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1866 r = ssh_packet_enable_delayed_compress(ssh);
1867 else
1868 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001869#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001870 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1871 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001872#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001873 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001874 state->packlen = 0;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001875
1876 /* do we need to rekey? */
1877 if (ssh_packet_need_rekeying(ssh, 0)) {
1878 debug3("%s: rekex triggered", __func__);
1879 if ((r = kex_start_rekex(ssh)) != 0)
1880 return r;
1881 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001882 out:
1883 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001884}
1885
1886int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001887ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001888{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001889 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001890 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001891 int r;
1892 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001893
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001894 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001895 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001896 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001897 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1898 if (r != 0)
1899 return r;
1900 if (*typep) {
1901 state->keep_alive_timeouts = 0;
1902 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001903 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001904 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001905 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001906 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001907 break;
Damien Miller33b13562000-04-04 14:38:59 +10001908 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001909 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1910 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1911 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001912 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001913 return r;
1914 }
Damien Miller33b13562000-04-04 14:38:59 +10001915 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001916 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001917 break;
1918 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001919 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1920 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1921 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001922 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001923 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001924 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1925 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001926 "Received disconnect from %s port %d:"
1927 "%u: %.400s", ssh_remote_ipaddr(ssh),
1928 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001929 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001930 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001931 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001932 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1933 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001934 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1935 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001936 break;
Damien Miller33b13562000-04-04 14:38:59 +10001937 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001938 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001939 }
Damien Miller33b13562000-04-04 14:38:59 +10001940 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001941 r = ssh_packet_read_poll1(ssh, typep);
1942 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001943 case SSH_MSG_NONE:
1944 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001945 case SSH_MSG_IGNORE:
1946 break;
1947 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001948 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1949 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001950 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001951 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001952 break;
1953 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001954 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1955 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001956 error("Received disconnect from %s port %d: "
1957 "%.400s", ssh_remote_ipaddr(ssh),
1958 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001959 free(msg);
1960 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001961 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001962 DBG(debug("received packet type %d", *typep));
1963 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001964 }
Damien Miller33b13562000-04-04 14:38:59 +10001965 }
1966 }
1967}
1968
Damien Miller5428f641999-11-25 11:54:57 +11001969/*
1970 * Buffers the given amount of input characters. This is intended to be used
1971 * together with packet_read_poll.
1972 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001973
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001974int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001975ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001976{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001977 struct session_state *state = ssh->state;
1978 int r;
1979
1980 if (state->packet_discard) {
1981 state->keep_alive_timeouts = 0; /* ?? */
1982 if (len >= state->packet_discard) {
1983 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001984 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001985 }
1986 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001987 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001988 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001989 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001990 return r;
1991
1992 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001993}
1994
Damien Miller4af51302000-04-16 11:18:38 +10001995int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001996ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001997{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001998 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001999}
2000
Damien Miller5428f641999-11-25 11:54:57 +11002001/*
2002 * Sends a diagnostic message from the server to the client. This message
2003 * can be sent at any time (but not while constructing another message). The
2004 * message is printed immediately, but only if the client is being executed
2005 * in verbose mode. These messages are primarily intended to ease debugging
2006 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002007 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11002008 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002009void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002010ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002011{
Damien Miller95def091999-11-25 00:26:21 +11002012 char buf[1024];
2013 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002014 int r;
Damien Miller95def091999-11-25 00:26:21 +11002015
markus@openbsd.org091c3022015-01-19 19:52:16 +00002016 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00002017 return;
2018
Damien Miller95def091999-11-25 00:26:21 +11002019 va_start(args, fmt);
2020 vsnprintf(buf, sizeof(buf), fmt, args);
2021 va_end(args);
2022
Damien Miller7c8af4f2000-05-01 08:24:07 +10002023 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002024 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
2025 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
2026 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2027 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2028 (r = sshpkt_send(ssh)) != 0)
2029 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002030 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002031 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
2032 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2033 (r = sshpkt_send(ssh)) != 0)
2034 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002035 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002036 if ((r = ssh_packet_write_wait(ssh)) != 0)
2037 fatal("%s: %s", __func__, ssh_err(r));
2038}
2039
2040/*
2041 * Pretty-print connection-terminating errors and exit.
2042 */
2043void
2044sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
2045{
2046 switch (r) {
2047 case SSH_ERR_CONN_CLOSED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002048 logit("Connection closed by %.200s port %d",
2049 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002050 cleanup_exit(255);
2051 case SSH_ERR_CONN_TIMEOUT:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002052 logit("Connection %s %.200s port %d timed out",
2053 ssh->state->server_side ? "from" : "to",
2054 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002055 cleanup_exit(255);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002056 case SSH_ERR_DISCONNECTED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002057 logit("Disconnected from %.200s port %d",
2058 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002059 cleanup_exit(255);
2060 case SSH_ERR_SYSTEM_ERROR:
2061 if (errno == ECONNRESET) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002062 logit("Connection reset by %.200s port %d",
2063 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002064 cleanup_exit(255);
2065 }
2066 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002067 case SSH_ERR_NO_CIPHER_ALG_MATCH:
2068 case SSH_ERR_NO_MAC_ALG_MATCH:
2069 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
2070 case SSH_ERR_NO_KEX_ALG_MATCH:
2071 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
2072 if (ssh && ssh->kex && ssh->kex->failed_choice) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002073 fatal("Unable to negotiate with %.200s port %d: %s. "
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002074 "Their offer: %s", ssh_remote_ipaddr(ssh),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002075 ssh_remote_port(ssh), ssh_err(r),
2076 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002077 }
2078 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002079 default:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002080 fatal("%s%sConnection %s %.200s port %d: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002081 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002082 ssh->state->server_side ? "from" : "to",
2083 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002084 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002085}
2086
Damien Miller5428f641999-11-25 11:54:57 +11002087/*
2088 * Logs the error plus constructs and sends a disconnect packet, closes the
2089 * connection, and exits. This function never returns. The error message
2090 * should not contain a newline. The length of the formatted message must
2091 * not exceed 1024 bytes.
2092 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002093void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002094ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002095{
Damien Miller95def091999-11-25 00:26:21 +11002096 char buf[1024];
2097 va_list args;
2098 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002099 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002100
Damien Miller95def091999-11-25 00:26:21 +11002101 if (disconnecting) /* Guard against recursive invocations. */
2102 fatal("packet_disconnect called recursively.");
2103 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002104
Damien Miller5428f641999-11-25 11:54:57 +11002105 /*
2106 * Format the message. Note that the caller must make sure the
2107 * message is of limited size.
2108 */
Damien Miller95def091999-11-25 00:26:21 +11002109 va_start(args, fmt);
2110 vsnprintf(buf, sizeof(buf), fmt, args);
2111 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002112
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002113 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10002114 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002115
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002116 /*
2117 * Send the disconnect message to the other side, and wait
2118 * for it to get sent.
2119 */
2120 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2121 sshpkt_fatal(ssh, __func__, r);
2122
2123 if ((r = ssh_packet_write_wait(ssh)) != 0)
2124 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002125
Damien Miller95def091999-11-25 00:26:21 +11002126 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002127 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002128 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002129}
2130
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002131/*
2132 * Checks if there is any buffered output, and tries to write some of
2133 * the output.
2134 */
2135int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002136ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002137{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002138 struct session_state *state = ssh->state;
2139 int len = sshbuf_len(state->output);
markus@openbsd.orga3068632016-01-14 16:17:39 +00002140 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002141
Damien Miller95def091999-11-25 00:26:21 +11002142 if (len > 0) {
markus@openbsd.orga3068632016-01-14 16:17:39 +00002143 len = write(state->connection_out,
2144 sshbuf_ptr(state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10002145 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002146 if (errno == EINTR || errno == EAGAIN ||
2147 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002148 return 0;
2149 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002150 }
markus@openbsd.orga3068632016-01-14 16:17:39 +00002151 if (len == 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002152 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002153 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002154 return r;
Damien Miller95def091999-11-25 00:26:21 +11002155 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002156 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002157}
2158
Damien Miller5428f641999-11-25 11:54:57 +11002159/*
2160 * Calls packet_write_poll repeatedly until all pending output data has been
2161 * written.
2162 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002163int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002164ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002165{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002166 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002167 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002168 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002169 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002170
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002171 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002172 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002173 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002174 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002175 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2176 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002177 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002178 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002179 while (ssh_packet_have_data_to_write(ssh)) {
2180 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002181 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002182 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002183
markus@openbsd.org091c3022015-01-19 19:52:16 +00002184 if (state->packet_timeout_ms > 0) {
2185 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002186 timeoutp = &timeout;
2187 }
2188 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002189 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002190 ms_to_timeval(&timeout, ms_remain);
2191 gettimeofday(&start, NULL);
2192 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002193 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002194 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002195 break;
Damien Millerea437422009-10-02 11:49:03 +10002196 if (errno != EAGAIN && errno != EINTR &&
2197 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002198 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002199 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002200 continue;
2201 ms_subtract_diff(&start, &ms_remain);
2202 if (ms_remain <= 0) {
2203 ret = 0;
2204 break;
2205 }
2206 }
2207 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002208 free(setp);
2209 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002210 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002211 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2212 free(setp);
2213 return r;
2214 }
Damien Miller95def091999-11-25 00:26:21 +11002215 }
Darren Tuckera627d422013-06-02 07:31:17 +10002216 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002217 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002218}
2219
2220/* Returns true if there is buffered data to write to the connection. */
2221
2222int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002223ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002224{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002225 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002226}
2227
2228/* Returns true if there is not too much data to write to the connection. */
2229
2230int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002231ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002232{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002233 if (ssh->state->interactive_mode)
2234 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002235 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002236 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002237}
2238
markus@openbsd.org091c3022015-01-19 19:52:16 +00002239void
2240ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002241{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002242#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002243 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002244 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002245 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002246# ifdef IP_TOS
2247 case AF_INET:
2248 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002249 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002250 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2251 error("setsockopt IP_TOS %d: %.100s:",
2252 tos, strerror(errno));
2253 break;
2254# endif /* IP_TOS */
2255# ifdef IPV6_TCLASS
2256 case AF_INET6:
2257 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002258 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002259 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2260 error("setsockopt IPV6_TCLASS %d: %.100s:",
2261 tos, strerror(errno));
2262 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002263# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002264 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002265#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002266}
Ben Lindstroma7433982002-12-23 02:41:41 +00002267
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002268/* Informs that the current session is interactive. Sets IP flags for that. */
2269
2270void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002271ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002272{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002273 struct session_state *state = ssh->state;
2274
2275 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002276 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002277 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002278
Damien Miller95def091999-11-25 00:26:21 +11002279 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002280 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002281
Damien Miller34132e52000-01-14 15:45:46 +11002282 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002283 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002284 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002285 set_nodelay(state->connection_in);
2286 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2287 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002288}
2289
2290/* Returns true if the current connection is interactive. */
2291
2292int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002293ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002294{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002295 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002296}
Damien Miller6162d121999-11-21 13:23:52 +11002297
Darren Tucker1f8311c2004-05-13 16:39:33 +10002298int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002299ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002300{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002301 struct session_state *state = ssh->state;
2302
2303 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002304 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002305 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002306 return -1;
2307 }
2308 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002309 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002310 return -1;
2311 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002312 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002313 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002314 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002315 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002316}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002317
Darren Tuckerf7288d72009-06-21 18:12:20 +10002318int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002319ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002320{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002321 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002322}
2323
2324void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002325ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002326{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002327 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002328}
2329
2330u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002331ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002332{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002333 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002334}
2335
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002336/*
2337 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002338 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002339 * byte SSH_MSG_IGNORE
2340 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002341 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002342 * All implementations MUST understand (and ignore) this message at any
2343 * time (after receiving the protocol version). No implementation is
2344 * required to send them. This message can be used as an additional
2345 * protection measure against advanced traffic analysis techniques.
2346 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002347void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002348ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002349{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002350 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002351 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002352
markus@openbsd.org091c3022015-01-19 19:52:16 +00002353 if ((r = sshpkt_start(ssh, compat20 ?
2354 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2355 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2356 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002357 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002358 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002359 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002360 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2361 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002362 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002363 }
2364}
Damien Millera5539d22003-04-09 20:50:06 +10002365
Damien Millera5539d22003-04-09 20:50:06 +10002366void
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002367ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002368{
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002369 debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes,
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002370 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002371 ssh->state->rekey_limit = bytes;
2372 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002373}
2374
2375time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002376ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002377{
2378 time_t seconds;
2379
markus@openbsd.org091c3022015-01-19 19:52:16 +00002380 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002381 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002382 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002383}
Damien Miller9786e6e2005-07-26 21:54:56 +10002384
2385void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002386ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002387{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002388 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002389}
2390
2391void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002392ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002393{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002394 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002395}
2396
2397void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002398ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002399{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002400 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002401}
2402
2403void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002404ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002405{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002406 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002407}
2408
Damien Millerc31a0cd2014-05-15 14:37:39 +10002409/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002410static int
2411ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002412{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002413 struct sshcomp *comp;
2414 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002415
2416 debug("%s: called", __func__);
2417 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002418 ssh->state->after_authentication = 1;
2419 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002420 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002421 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002422 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002423 comp = &ssh->state->newkeys[mode]->comp;
2424 if (comp && comp->enabled &&
2425 (r = ssh_packet_init_compression(ssh)) != 0)
2426 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002427 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002428 return 0;
2429}
2430
2431/* Packet state (de-)serialization for privsep */
2432
2433/* turn kex into a blob for packet state serialization */
2434static int
2435kex_to_blob(struct sshbuf *m, struct kex *kex)
2436{
2437 int r;
2438
2439 if ((r = sshbuf_put_string(m, kex->session_id,
2440 kex->session_id_len)) != 0 ||
2441 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2442 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2443 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2444 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2445 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2446 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2447 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2448 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2449 return r;
2450 return 0;
2451}
2452
2453/* turn key exchange results into a blob for packet state serialization */
2454static int
2455newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2456{
2457 struct sshbuf *b;
2458 struct sshcipher_ctx *cc;
2459 struct sshcomp *comp;
2460 struct sshenc *enc;
2461 struct sshmac *mac;
2462 struct newkeys *newkey;
2463 int r;
2464
2465 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2466 return SSH_ERR_INTERNAL_ERROR;
2467 enc = &newkey->enc;
2468 mac = &newkey->mac;
2469 comp = &newkey->comp;
2470 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2471 &ssh->state->receive_context;
2472 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2473 return r;
2474 if ((b = sshbuf_new()) == NULL)
2475 return SSH_ERR_ALLOC_FAIL;
2476 /* The cipher struct is constant and shared, you export pointer */
2477 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2478 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2479 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2480 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2481 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2482 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2483 goto out;
2484 if (cipher_authlen(enc->cipher) == 0) {
2485 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2486 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2487 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2488 goto out;
2489 }
2490 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2491 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2492 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2493 goto out;
2494 r = sshbuf_put_stringb(m, b);
2495 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002496 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002497 return r;
2498}
2499
2500/* serialize packet state into a blob */
2501int
2502ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2503{
2504 struct session_state *state = ssh->state;
2505 u_char *p;
2506 size_t slen, rlen;
2507 int r, ssh1cipher;
2508
2509 if (!compat20) {
2510 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2511 slen = cipher_get_keyiv_len(&state->send_context);
2512 rlen = cipher_get_keyiv_len(&state->receive_context);
2513 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2514 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2515 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2516 (r = sshbuf_put_u32(m, slen)) != 0 ||
2517 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2518 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2519 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2520 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2521 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2522 return r;
2523 } else {
2524 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2525 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2526 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002527 (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002528 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002529 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2530 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2531 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2532 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2533 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2534 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2535 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2536 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2537 return r;
2538 }
2539
2540 slen = cipher_get_keycontext(&state->send_context, NULL);
2541 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2542 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2543 (r = sshbuf_reserve(m, slen, &p)) != 0)
2544 return r;
2545 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2546 return SSH_ERR_INTERNAL_ERROR;
2547 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2548 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2549 return r;
2550 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2551 return SSH_ERR_INTERNAL_ERROR;
2552
2553 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2554 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2555 (r = sshbuf_put_stringb(m, state->output)) != 0)
2556 return r;
2557
markus@openbsd.org091c3022015-01-19 19:52:16 +00002558 return 0;
2559}
2560
2561/* restore key exchange results from blob for packet state de-serialization */
2562static int
2563newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2564{
2565 struct sshbuf *b = NULL;
2566 struct sshcomp *comp;
2567 struct sshenc *enc;
2568 struct sshmac *mac;
2569 struct newkeys *newkey = NULL;
2570 size_t keylen, ivlen, maclen;
2571 int r;
2572
2573 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2574 r = SSH_ERR_ALLOC_FAIL;
2575 goto out;
2576 }
2577 if ((r = sshbuf_froms(m, &b)) != 0)
2578 goto out;
2579#ifdef DEBUG_PK
2580 sshbuf_dump(b, stderr);
2581#endif
2582 enc = &newkey->enc;
2583 mac = &newkey->mac;
2584 comp = &newkey->comp;
2585
2586 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2587 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2588 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2589 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2590 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2591 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2592 goto out;
2593 if (cipher_authlen(enc->cipher) == 0) {
2594 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2595 goto out;
2596 if ((r = mac_setup(mac, mac->name)) != 0)
2597 goto out;
2598 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2599 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2600 goto out;
2601 if (maclen > mac->key_len) {
2602 r = SSH_ERR_INVALID_FORMAT;
2603 goto out;
2604 }
2605 mac->key_len = maclen;
2606 }
2607 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2608 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2609 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2610 goto out;
2611 if (enc->name == NULL ||
2612 cipher_by_name(enc->name) != enc->cipher) {
2613 r = SSH_ERR_INVALID_FORMAT;
2614 goto out;
2615 }
2616 if (sshbuf_len(b) != 0) {
2617 r = SSH_ERR_INVALID_FORMAT;
2618 goto out;
2619 }
2620 enc->key_len = keylen;
2621 enc->iv_len = ivlen;
2622 ssh->kex->newkeys[mode] = newkey;
2623 newkey = NULL;
2624 r = 0;
2625 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002626 free(newkey);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002627 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002628 return r;
2629}
2630
2631/* restore kex from blob for packet state de-serialization */
2632static int
2633kex_from_blob(struct sshbuf *m, struct kex **kexp)
2634{
2635 struct kex *kex;
2636 int r;
2637
2638 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2639 (kex->my = sshbuf_new()) == NULL ||
2640 (kex->peer = sshbuf_new()) == NULL) {
2641 r = SSH_ERR_ALLOC_FAIL;
2642 goto out;
2643 }
2644 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2645 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2646 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2647 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2648 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2649 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2650 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2651 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2652 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2653 goto out;
2654 kex->server = 1;
2655 kex->done = 1;
2656 r = 0;
2657 out:
2658 if (r != 0 || kexp == NULL) {
2659 if (kex != NULL) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002660 sshbuf_free(kex->my);
2661 sshbuf_free(kex->peer);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002662 free(kex);
2663 }
2664 if (kexp != NULL)
2665 *kexp = NULL;
2666 } else {
2667 *kexp = kex;
2668 }
2669 return r;
2670}
2671
2672/*
2673 * Restore packet state from content of blob 'm' (de-serialization).
2674 * Note that 'm' will be partially consumed on parsing or any other errors.
2675 */
2676int
2677ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2678{
2679 struct session_state *state = ssh->state;
2680 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2681 size_t ssh1keylen, rlen, slen, ilen, olen;
2682 int r;
2683 u_int ssh1cipher = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002684
2685 if (!compat20) {
2686 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2687 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2688 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2689 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2690 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2691 return r;
2692 if (ssh1cipher > INT_MAX)
2693 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2694 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2695 (int)ssh1cipher);
2696 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2697 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2698 return SSH_ERR_INVALID_FORMAT;
2699 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2700 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2701 return r;
2702 } else {
2703 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2704 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2705 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002706 (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002707 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002708 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2709 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2710 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2711 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2712 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2713 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2714 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2715 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2716 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002717 /*
2718 * We set the time here so that in post-auth privsep slave we
2719 * count from the completion of the authentication.
2720 */
2721 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002722 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2723 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2724 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2725 return r;
2726 }
2727 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2728 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2729 return r;
2730 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2731 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2732 return SSH_ERR_INVALID_FORMAT;
2733 cipher_set_keycontext(&state->send_context, keyout);
2734 cipher_set_keycontext(&state->receive_context, keyin);
2735
2736 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2737 (r = ssh_packet_set_postauth(ssh)) != 0)
2738 return r;
2739
2740 sshbuf_reset(state->input);
2741 sshbuf_reset(state->output);
2742 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2743 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2744 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2745 (r = sshbuf_put(state->output, output, olen)) != 0)
2746 return r;
2747
markus@openbsd.org091c3022015-01-19 19:52:16 +00002748 if (sshbuf_len(m))
2749 return SSH_ERR_INVALID_FORMAT;
2750 debug3("%s: done", __func__);
2751 return 0;
2752}
2753
2754/* NEW API */
2755
2756/* put data to the outgoing packet */
2757
2758int
2759sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2760{
2761 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2762}
2763
2764int
2765sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2766{
2767 return sshbuf_putb(ssh->state->outgoing_packet, b);
2768}
2769
2770int
2771sshpkt_put_u8(struct ssh *ssh, u_char val)
2772{
2773 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2774}
2775
2776int
2777sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2778{
2779 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2780}
2781
2782int
2783sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2784{
2785 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2786}
2787
2788int
2789sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2790{
2791 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2792}
2793
2794int
2795sshpkt_put_cstring(struct ssh *ssh, const void *v)
2796{
2797 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2798}
2799
2800int
2801sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2802{
2803 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2804}
2805
djm@openbsd.org734226b2015-04-27 01:52:30 +00002806#ifdef WITH_OPENSSL
2807#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002808int
2809sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2810{
2811 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2812}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002813#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002814
Damien Miller773dda22015-01-30 23:10:17 +11002815#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002816int
2817sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2818{
2819 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2820}
Damien Miller773dda22015-01-30 23:10:17 +11002821#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002822
2823int
2824sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2825{
2826 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2827}
Damien Miller773dda22015-01-30 23:10:17 +11002828#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002829
2830/* fetch data from the incoming packet */
2831
2832int
2833sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2834{
2835 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2836}
2837
2838int
2839sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2840{
2841 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2842}
2843
2844int
2845sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2846{
2847 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2848}
2849
2850int
2851sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2852{
2853 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2854}
2855
2856int
2857sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2858{
2859 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2860}
2861
2862int
2863sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2864{
2865 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2866}
2867
2868int
2869sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2870{
2871 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2872}
2873
djm@openbsd.org734226b2015-04-27 01:52:30 +00002874#ifdef WITH_OPENSSL
2875#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002876int
2877sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2878{
2879 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2880}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002881#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002882
Damien Miller773dda22015-01-30 23:10:17 +11002883#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002884int
2885sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2886{
2887 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2888}
Damien Miller773dda22015-01-30 23:10:17 +11002889#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002890
2891int
2892sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2893{
2894 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2895}
Damien Miller773dda22015-01-30 23:10:17 +11002896#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002897
2898int
2899sshpkt_get_end(struct ssh *ssh)
2900{
2901 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2902 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2903 return 0;
2904}
2905
2906const u_char *
2907sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2908{
2909 if (lenp != NULL)
2910 *lenp = sshbuf_len(ssh->state->incoming_packet);
2911 return sshbuf_ptr(ssh->state->incoming_packet);
2912}
2913
2914/* start a new packet */
2915
2916int
2917sshpkt_start(struct ssh *ssh, u_char type)
2918{
2919 u_char buf[9];
2920 int len;
2921
2922 DBG(debug("packet_start[%d]", type));
2923 len = compat20 ? 6 : 9;
2924 memset(buf, 0, len - 1);
2925 buf[len - 1] = type;
2926 sshbuf_reset(ssh->state->outgoing_packet);
2927 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2928}
2929
2930/* send it */
2931
2932int
2933sshpkt_send(struct ssh *ssh)
2934{
2935 if (compat20)
2936 return ssh_packet_send2(ssh);
2937 else
2938 return ssh_packet_send1(ssh);
2939}
2940
2941int
2942sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2943{
2944 char buf[1024];
2945 va_list args;
2946 int r;
2947
2948 va_start(args, fmt);
2949 vsnprintf(buf, sizeof(buf), fmt, args);
2950 va_end(args);
2951
2952 if (compat20) {
2953 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2954 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2955 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2956 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2957 (r = sshpkt_send(ssh)) != 0)
2958 return r;
2959 } else {
2960 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2961 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2962 (r = sshpkt_send(ssh)) != 0)
2963 return r;
2964 }
2965 return 0;
2966}
2967
2968/* roundup current message to pad bytes */
2969int
2970sshpkt_add_padding(struct ssh *ssh, u_char pad)
2971{
2972 ssh->state->extra_pad = pad;
2973 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002974}