blob: 8d9bcd8db52e2cff00af8a3efc72ef586e06c07f [file] [log] [blame]
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001/* $OpenBSD: packet.c,v 1.220 2015/12/11 03:24:25 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"
Darren Tuckerc5564e12009-06-21 18:53:53 +100086#include "roaming.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000087#include "ssherr.h"
88#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100089
90#ifdef PACKET_DEBUG
91#define DBG(x) x
92#else
93#define DBG(x)
94#endif
95
Damien Miller13ae44c2009-01-28 16:38:41 +110096#define PACKET_MAX_SIZE (256 * 1024)
97
Darren Tuckerf7288d72009-06-21 18:12:20 +100098struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100099 u_int32_t seqnr;
100 u_int32_t packets;
101 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000102 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000103};
Damien Miller13ae44c2009-01-28 16:38:41 +1100104
Damien Millera5539d22003-04-09 20:50:06 +1000105struct packet {
106 TAILQ_ENTRY(packet) next;
107 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000108 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000109};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000110
111struct session_state {
112 /*
113 * This variable contains the file descriptors used for
114 * communicating with the other side. connection_in is used for
115 * reading; connection_out for writing. These can be the same
116 * descriptor, in which case it is assumed to be a socket.
117 */
118 int connection_in;
119 int connection_out;
120
121 /* Protocol flags for the remote side. */
122 u_int remote_protocol_flags;
123
124 /* Encryption context for receiving data. Only used for decryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000125 struct sshcipher_ctx receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000126
127 /* Encryption context for sending data. Only used for encryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000128 struct sshcipher_ctx send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000129
130 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000131 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000132
133 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000134 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000135
136 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000137 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000138
139 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000140 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000141
142 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000143 struct sshbuf *compression_buffer;
144
145 /* Incoming/outgoing compression dictionaries */
146 z_stream compression_in_stream;
147 z_stream compression_out_stream;
148 int compression_in_started;
149 int compression_out_started;
150 int compression_in_failures;
151 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000152
153 /*
154 * Flag indicating whether packet compression/decompression is
155 * enabled.
156 */
157 int packet_compression;
158
159 /* default maximum packet size */
160 u_int max_packet_size;
161
162 /* Flag indicating whether this module has been initialized. */
163 int initialized;
164
165 /* Set to true if the connection is interactive. */
166 int interactive_mode;
167
168 /* Set to true if we are the server side. */
169 int server_side;
170
171 /* Set to true if we are authenticated. */
172 int after_authentication;
173
174 int keep_alive_timeouts;
175
176 /* The maximum time that we will wait to send or receive a packet */
177 int packet_timeout_ms;
178
179 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000180 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000181 struct packet_state p_read, p_send;
182
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000183 /* Volume-based rekeying */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000184 u_int64_t max_blocks_in, max_blocks_out;
185 u_int32_t rekey_limit;
186
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000187 /* Time-based rekeying */
markus@openbsd.org02db4682015-02-13 18:57:00 +0000188 u_int32_t rekey_interval; /* how often in seconds */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000189 time_t rekey_time; /* time of last rekeying */
190
Darren Tuckerf7288d72009-06-21 18:12:20 +1000191 /* Session key for protocol v1 */
192 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
193 u_int ssh1_keylen;
194
195 /* roundup current message to extra_pad bytes */
196 u_char extra_pad;
197
198 /* XXX discard incoming data after MAC error */
199 u_int packet_discard;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000200 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000201
202 /* Used in packet_read_poll2() */
203 u_int packlen;
204
Darren Tucker7b935c72009-06-21 18:59:36 +1000205 /* Used in packet_send2 */
206 int rekeying;
207
208 /* Used in packet_set_interactive */
209 int set_interactive_called;
210
211 /* Used in packet_set_maxsize */
212 int set_maxsize_called;
213
markus@openbsd.org091c3022015-01-19 19:52:16 +0000214 /* One-off warning about weak ciphers */
215 int cipher_warning_done;
216
217 /* SSH1 CRC compensation attack detector */
218 struct deattack_ctx deattack;
219
Darren Tuckerf7288d72009-06-21 18:12:20 +1000220 TAILQ_HEAD(, packet) outgoing;
221};
222
markus@openbsd.org091c3022015-01-19 19:52:16 +0000223struct ssh *
224ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000225{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000226 struct ssh *ssh = NULL;
227 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000228
markus@openbsd.org091c3022015-01-19 19:52:16 +0000229 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
230 (state = calloc(1, sizeof(*state))) == NULL ||
231 (state->input = sshbuf_new()) == NULL ||
232 (state->output = sshbuf_new()) == NULL ||
233 (state->outgoing_packet = sshbuf_new()) == NULL ||
234 (state->incoming_packet = sshbuf_new()) == NULL)
235 goto fail;
236 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000237 TAILQ_INIT(&ssh->private_keys);
238 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000239 state->connection_in = -1;
240 state->connection_out = -1;
241 state->max_packet_size = 32768;
242 state->packet_timeout_ms = -1;
243 state->p_send.packets = state->p_read.packets = 0;
244 state->initialized = 1;
245 /*
246 * ssh_packet_send2() needs to queue packets until
247 * we've done the initial key exchange.
248 */
249 state->rekeying = 1;
250 ssh->state = state;
251 return ssh;
252 fail:
253 if (state) {
254 sshbuf_free(state->input);
255 sshbuf_free(state->output);
256 sshbuf_free(state->incoming_packet);
257 sshbuf_free(state->outgoing_packet);
258 free(state);
259 }
260 free(ssh);
261 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000262}
Damien Millera5539d22003-04-09 20:50:06 +1000263
Damien Miller5428f641999-11-25 11:54:57 +1100264/*
265 * Sets the descriptors used for communication. Disables encryption until
266 * packet_set_encryption_key is called.
267 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000268struct ssh *
269ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000271 struct session_state *state;
272 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000273 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000274
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000275 if (none == NULL) {
276 error("%s: cannot load cipher 'none'", __func__);
277 return NULL;
278 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000279 if (ssh == NULL)
280 ssh = ssh_alloc_session_state();
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000281 if (ssh == NULL) {
282 error("%s: cound not allocate state", __func__);
283 return NULL;
284 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000285 state = ssh->state;
286 state->connection_in = fd_in;
287 state->connection_out = fd_out;
288 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000289 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000290 (r = cipher_init(&state->receive_context, none,
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000291 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
292 error("%s: cipher_init failed: %s", __func__, ssh_err(r));
jsg@openbsd.org1cb30162015-03-11 00:48:39 +0000293 free(ssh);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000294 return NULL;
295 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000296 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
297 deattack_init(&state->deattack);
djm@openbsd.orgd4c02952015-02-11 01:20:38 +0000298 /*
299 * Cache the IP address of the remote connection for use in error
300 * messages that might be generated after the connection has closed.
301 */
302 (void)ssh_remote_ipaddr(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000303 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304}
305
Darren Tucker3fc464e2008-06-13 06:42:45 +1000306void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000307ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000308{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000309 struct session_state *state = ssh->state;
310
Damien Miller8ed4de82011-12-19 10:52:50 +1100311 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000312 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000313 return;
314 }
315 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000316 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000317 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000318 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000319}
320
markus@openbsd.org091c3022015-01-19 19:52:16 +0000321int
322ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100323{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000324 struct session_state *state = ssh->state;
325 int r;
326
327 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100328 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000329
Damien Miller13ae44c2009-01-28 16:38:41 +1100330 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000331 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000332 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000333 if ((r = sshbuf_put(state->incoming_packet, buf,
334 sizeof(buf))) != 0)
335 return r;
336 (void) mac_compute(state->packet_discard_mac,
337 state->p_read.seqnr,
338 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
339 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100340 }
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000341 logit("Finished discarding for %.200s port %d",
342 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000343 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100344}
345
markus@openbsd.org091c3022015-01-19 19:52:16 +0000346static int
347ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
348 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100349{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000350 struct session_state *state = ssh->state;
351 int r;
352
353 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
354 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
355 return r;
356 return SSH_ERR_MAC_INVALID;
357 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100358 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000359 state->packet_discard_mac = mac;
360 if (sshbuf_len(state->input) >= discard &&
361 (r = ssh_packet_stop_discard(ssh)) != 0)
362 return r;
363 state->packet_discard = discard - sshbuf_len(state->input);
364 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100365}
366
Damien Miller34132e52000-01-14 15:45:46 +1100367/* Returns 1 if remote host is connected via socket, 0 if not. */
368
369int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000370ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100371{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000372 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100373 struct sockaddr_storage from, to;
374 socklen_t fromlen, tolen;
375
376 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000377 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100378 return 1;
379 fromlen = sizeof(from);
380 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000381 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000382 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100383 return 0;
384 tolen = sizeof(to);
385 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000386 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000387 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100388 return 0;
389 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
390 return 0;
391 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
392 return 0;
393 return 1;
394}
395
Ben Lindstromf6027d32002-03-22 01:42:04 +0000396void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000397ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000398{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000399 if (ibytes)
400 *ibytes = ssh->state->p_read.bytes;
401 if (obytes)
402 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000403}
404
405int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000406ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100407{
408 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100409 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100410
411 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000412 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000413 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100414 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000415#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100416 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000417 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000418 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000419#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000420 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100421}
422
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423/* Sets the connection into non-blocking mode. */
424
425void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000426ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427{
Damien Miller95def091999-11-25 00:26:21 +1100428 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000429 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430
markus@openbsd.org091c3022015-01-19 19:52:16 +0000431 if (ssh->state->connection_out != ssh->state->connection_in)
432 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433}
434
435/* Returns the socket used for reading. */
436
437int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000438ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000440 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441}
442
443/* Returns the descriptor used for writing. */
444
445int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000446ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000448 return ssh->state->connection_out;
449}
450
451/*
452 * Returns the IP-address of the remote host as a string. The returned
453 * string must not be freed.
454 */
455
456const char *
457ssh_remote_ipaddr(struct ssh *ssh)
458{
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000459 const int sock = ssh->state->connection_in;
460
markus@openbsd.org091c3022015-01-19 19:52:16 +0000461 /* Check whether we have cached the ipaddr. */
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000462 if (ssh->remote_ipaddr == NULL) {
463 if (ssh_packet_connection_is_on_socket(ssh)) {
464 ssh->remote_ipaddr = get_peer_ipaddr(sock);
465 ssh->remote_port = get_sock_port(sock, 0);
466 } else {
467 ssh->remote_ipaddr = strdup("UNKNOWN");
468 ssh->remote_port = 0;
469 }
470 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000471 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472}
473
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000474/* Returns the port number of the remote host. */
475
476int
477ssh_remote_port(struct ssh *ssh)
478{
479 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
480 return ssh->remote_port;
481}
482
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483/* Closes the connection and clears and frees internal data structures. */
484
485void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000486ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000488 struct session_state *state = ssh->state;
489 int r;
490 u_int mode;
491
492 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100493 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000494 state->initialized = 0;
495 if (state->connection_in == state->connection_out) {
496 shutdown(state->connection_out, SHUT_RDWR);
497 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100498 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000499 close(state->connection_in);
500 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100501 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000502 sshbuf_free(state->input);
503 sshbuf_free(state->output);
504 sshbuf_free(state->outgoing_packet);
505 sshbuf_free(state->incoming_packet);
506 for (mode = 0; mode < MODE_MAX; mode++)
507 kex_free_newkeys(state->newkeys[mode]);
508 if (state->compression_buffer) {
509 sshbuf_free(state->compression_buffer);
510 if (state->compression_out_started) {
511 z_streamp stream = &state->compression_out_stream;
512 debug("compress outgoing: "
513 "raw data %llu, compressed %llu, factor %.2f",
514 (unsigned long long)stream->total_in,
515 (unsigned long long)stream->total_out,
516 stream->total_in == 0 ? 0.0 :
517 (double) stream->total_out / stream->total_in);
518 if (state->compression_out_failures == 0)
519 deflateEnd(stream);
520 }
521 if (state->compression_in_started) {
522 z_streamp stream = &state->compression_out_stream;
523 debug("compress incoming: "
524 "raw data %llu, compressed %llu, factor %.2f",
525 (unsigned long long)stream->total_out,
526 (unsigned long long)stream->total_in,
527 stream->total_out == 0 ? 0.0 :
528 (double) stream->total_in / stream->total_out);
529 if (state->compression_in_failures == 0)
530 inflateEnd(stream);
531 }
Damien Miller95def091999-11-25 00:26:21 +1100532 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000533 if ((r = cipher_cleanup(&state->send_context)) != 0)
534 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
535 if ((r = cipher_cleanup(&state->receive_context)) != 0)
536 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000537 free(ssh->remote_ipaddr);
538 ssh->remote_ipaddr = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000539 free(ssh->state);
540 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541}
542
543/* Sets remote side protocol flags. */
544
545void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000546ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000548 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549}
550
551/* Returns the remote protocol flags set earlier by the above function. */
552
Ben Lindstrom46c16222000-12-22 01:43:59 +0000553u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000554ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000556 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557}
558
Damien Miller5428f641999-11-25 11:54:57 +1100559/*
560 * Starts packet compression from the next packet on in both directions.
561 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
562 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563
markus@openbsd.org091c3022015-01-19 19:52:16 +0000564static int
565ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000566{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000567 if (!ssh->state->compression_buffer &&
568 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
569 return SSH_ERR_ALLOC_FAIL;
570 return 0;
571}
572
573static int
574start_compression_out(struct ssh *ssh, int level)
575{
576 if (level < 1 || level > 9)
577 return SSH_ERR_INVALID_ARGUMENT;
578 debug("Enabling compression at level %d.", level);
579 if (ssh->state->compression_out_started == 1)
580 deflateEnd(&ssh->state->compression_out_stream);
581 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
582 case Z_OK:
583 ssh->state->compression_out_started = 1;
584 break;
585 case Z_MEM_ERROR:
586 return SSH_ERR_ALLOC_FAIL;
587 default:
588 return SSH_ERR_INTERNAL_ERROR;
589 }
590 return 0;
591}
592
593static int
594start_compression_in(struct ssh *ssh)
595{
596 if (ssh->state->compression_in_started == 1)
597 inflateEnd(&ssh->state->compression_in_stream);
598 switch (inflateInit(&ssh->state->compression_in_stream)) {
599 case Z_OK:
600 ssh->state->compression_in_started = 1;
601 break;
602 case Z_MEM_ERROR:
603 return SSH_ERR_ALLOC_FAIL;
604 default:
605 return SSH_ERR_INTERNAL_ERROR;
606 }
607 return 0;
608}
609
610int
611ssh_packet_start_compression(struct ssh *ssh, int level)
612{
613 int r;
614
615 if (ssh->state->packet_compression && !compat20)
616 return SSH_ERR_INTERNAL_ERROR;
617 ssh->state->packet_compression = 1;
618 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
619 (r = start_compression_in(ssh)) != 0 ||
620 (r = start_compression_out(ssh, level)) != 0)
621 return r;
622 return 0;
623}
624
625/* XXX remove need for separate compression buffer */
626static int
627compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
628{
629 u_char buf[4096];
630 int r, status;
631
632 if (ssh->state->compression_out_started != 1)
633 return SSH_ERR_INTERNAL_ERROR;
634
635 /* This case is not handled below. */
636 if (sshbuf_len(in) == 0)
637 return 0;
638
639 /* Input is the contents of the input buffer. */
640 if ((ssh->state->compression_out_stream.next_in =
641 sshbuf_mutable_ptr(in)) == NULL)
642 return SSH_ERR_INTERNAL_ERROR;
643 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
644
645 /* Loop compressing until deflate() returns with avail_out != 0. */
646 do {
647 /* Set up fixed-size output buffer. */
648 ssh->state->compression_out_stream.next_out = buf;
649 ssh->state->compression_out_stream.avail_out = sizeof(buf);
650
651 /* Compress as much data into the buffer as possible. */
652 status = deflate(&ssh->state->compression_out_stream,
653 Z_PARTIAL_FLUSH);
654 switch (status) {
655 case Z_MEM_ERROR:
656 return SSH_ERR_ALLOC_FAIL;
657 case Z_OK:
658 /* Append compressed data to output_buffer. */
659 if ((r = sshbuf_put(out, buf, sizeof(buf) -
660 ssh->state->compression_out_stream.avail_out)) != 0)
661 return r;
662 break;
663 case Z_STREAM_ERROR:
664 default:
665 ssh->state->compression_out_failures++;
666 return SSH_ERR_INVALID_FORMAT;
667 }
668 } while (ssh->state->compression_out_stream.avail_out == 0);
669 return 0;
670}
671
672static int
673uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
674{
675 u_char buf[4096];
676 int r, status;
677
678 if (ssh->state->compression_in_started != 1)
679 return SSH_ERR_INTERNAL_ERROR;
680
681 if ((ssh->state->compression_in_stream.next_in =
682 sshbuf_mutable_ptr(in)) == NULL)
683 return SSH_ERR_INTERNAL_ERROR;
684 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
685
686 for (;;) {
687 /* Set up fixed-size output buffer. */
688 ssh->state->compression_in_stream.next_out = buf;
689 ssh->state->compression_in_stream.avail_out = sizeof(buf);
690
691 status = inflate(&ssh->state->compression_in_stream,
692 Z_PARTIAL_FLUSH);
693 switch (status) {
694 case Z_OK:
695 if ((r = sshbuf_put(out, buf, sizeof(buf) -
696 ssh->state->compression_in_stream.avail_out)) != 0)
697 return r;
698 break;
699 case Z_BUF_ERROR:
700 /*
701 * Comments in zlib.h say that we should keep calling
702 * inflate() until we get an error. This appears to
703 * be the error that we get.
704 */
705 return 0;
706 case Z_DATA_ERROR:
707 return SSH_ERR_INVALID_FORMAT;
708 case Z_MEM_ERROR:
709 return SSH_ERR_ALLOC_FAIL;
710 case Z_STREAM_ERROR:
711 default:
712 ssh->state->compression_in_failures++;
713 return SSH_ERR_INTERNAL_ERROR;
714 }
715 }
716 /* NOTREACHED */
717}
718
719/* Serialise compression state into a blob for privsep */
720static int
721ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
722{
723 struct session_state *state = ssh->state;
724 struct sshbuf *b;
725 int r;
726
727 if ((b = sshbuf_new()) == NULL)
728 return SSH_ERR_ALLOC_FAIL;
729 if (state->compression_in_started) {
730 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
731 sizeof(state->compression_in_stream))) != 0)
732 goto out;
733 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
734 goto out;
735 if (state->compression_out_started) {
736 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
737 sizeof(state->compression_out_stream))) != 0)
738 goto out;
739 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
740 goto out;
741 r = sshbuf_put_stringb(m, b);
742 out:
743 sshbuf_free(b);
744 return r;
745}
746
747/* Deserialise compression state from a blob for privsep */
748static int
749ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
750{
751 struct session_state *state = ssh->state;
752 struct sshbuf *b = NULL;
753 int r;
754 const u_char *inblob, *outblob;
755 size_t inl, outl;
756
757 if ((r = sshbuf_froms(m, &b)) != 0)
758 goto out;
759 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
760 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
761 goto out;
762 if (inl == 0)
763 state->compression_in_started = 0;
764 else if (inl != sizeof(state->compression_in_stream)) {
765 r = SSH_ERR_INTERNAL_ERROR;
766 goto out;
767 } else {
768 state->compression_in_started = 1;
769 memcpy(&state->compression_in_stream, inblob, inl);
770 }
771 if (outl == 0)
772 state->compression_out_started = 0;
773 else if (outl != sizeof(state->compression_out_stream)) {
774 r = SSH_ERR_INTERNAL_ERROR;
775 goto out;
776 } else {
777 state->compression_out_started = 1;
778 memcpy(&state->compression_out_stream, outblob, outl);
779 }
780 r = 0;
781 out:
782 sshbuf_free(b);
783 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000784}
785
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000787ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
788 void *(*allocfunc)(void *, u_int, u_int),
789 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000791 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
792 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
793 ssh->state->compression_out_stream.opaque = ctx;
794 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
795 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
796 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797}
798
Damien Miller5428f641999-11-25 11:54:57 +1100799/*
Damien Miller5428f641999-11-25 11:54:57 +1100800 * Causes any further packets to be encrypted using the given key. The same
801 * key is used for both sending and reception. However, both directions are
802 * encrypted independently of each other.
803 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000804
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000806ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000808#ifndef WITH_SSH1
809 fatal("no SSH protocol 1 support");
810#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000811 struct session_state *state = ssh->state;
812 const struct sshcipher *cipher = cipher_by_number(number);
813 int r;
814 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100815
markus@openbsd.org091c3022015-01-19 19:52:16 +0000816 if (cipher == NULL)
817 fatal("%s: unknown cipher number %d", __func__, number);
818 if (keylen < 20)
819 fatal("%s: keylen too small: %d", __func__, keylen);
820 if (keylen > SSH_SESSION_KEY_LENGTH)
821 fatal("%s: keylen too big: %d", __func__, keylen);
822 memcpy(state->ssh1_key, key, keylen);
823 state->ssh1_keylen = keylen;
824 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
825 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
826 (r = cipher_init(&state->receive_context, cipher, key, keylen,
827 NULL, 0, CIPHER_DECRYPT) != 0))
828 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
829 if (!state->cipher_warning_done &&
830 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
831 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
832 error("Warning: %s", wmsg);
833 state->cipher_warning_done = 1;
834 }
Damien Miller773dda22015-01-30 23:10:17 +1100835#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000836}
837
Damien Miller5428f641999-11-25 11:54:57 +1100838/*
839 * Finalizes and sends the packet. If the encryption key has been set,
840 * encrypts the packet before sending.
841 */
Damien Miller95def091999-11-25 00:26:21 +1100842
markus@openbsd.org091c3022015-01-19 19:52:16 +0000843int
844ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000845{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000846 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000847 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000848 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000849 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850
Damien Miller5428f641999-11-25 11:54:57 +1100851 /*
852 * If using packet compression, compress the payload of the outgoing
853 * packet.
854 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000855 if (state->packet_compression) {
856 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100857 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000858 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
859 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100860 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000861 if ((r = sshbuf_put(state->compression_buffer,
862 "\0\0\0\0\0\0\0\0", 8)) != 0)
863 goto out;
864 if ((r = compress_buffer(ssh, state->outgoing_packet,
865 state->compression_buffer)) != 0)
866 goto out;
867 sshbuf_reset(state->outgoing_packet);
868 if ((r = sshbuf_putb(state->outgoing_packet,
869 state->compression_buffer)) != 0)
870 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100871 }
872 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000873 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874
Damien Millere247cc42000-05-07 12:03:14 +1000875 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100876 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000877 if (!state->send_context.plaintext) {
878 cp = sshbuf_mutable_ptr(state->outgoing_packet);
879 if (cp == NULL) {
880 r = SSH_ERR_INTERNAL_ERROR;
881 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100882 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000883 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100884 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000885 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
886 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100887
888 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000889 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
890 sshbuf_len(state->outgoing_packet));
891 POKE_U32(buf, checksum);
892 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
893 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894
895#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100896 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000897 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898#endif
899
Damien Miller95def091999-11-25 00:26:21 +1100900 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000901 POKE_U32(buf, len);
902 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
903 goto out;
904 if ((r = sshbuf_reserve(state->output,
905 sshbuf_len(state->outgoing_packet), &cp)) != 0)
906 goto out;
907 if ((r = cipher_crypt(&state->send_context, 0, cp,
908 sshbuf_ptr(state->outgoing_packet),
909 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
910 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100911
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000912#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100913 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000914 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000916 state->p_send.packets++;
917 state->p_send.bytes += len +
918 sshbuf_len(state->outgoing_packet);
919 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920
Damien Miller5428f641999-11-25 11:54:57 +1100921 /*
Damien Miller788f2122005-11-05 15:14:59 +1100922 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000923 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
924 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100925 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000926 r = 0;
927 out:
928 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929}
930
markus@openbsd.org091c3022015-01-19 19:52:16 +0000931int
932ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000933{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000934 struct session_state *state = ssh->state;
935 struct sshenc *enc;
936 struct sshmac *mac;
937 struct sshcomp *comp;
938 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000939 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000940 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000941 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000942
Ben Lindstrom064496f2002-12-23 02:04:22 +0000943 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000944
Damien Miller963f6b22002-02-19 15:21:23 +1100945 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000946 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000947 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000948 state->p_send.packets = state->p_send.blocks = 0;
949 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100950 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000951 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000952 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000953 state->p_read.packets = state->p_read.blocks = 0;
954 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100955 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000956 if (state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000957 debug("set_newkeys: rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000958 if ((r = cipher_cleanup(cc)) != 0)
959 return r;
960 enc = &state->newkeys[mode]->enc;
961 mac = &state->newkeys[mode]->mac;
962 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000963 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100964 explicit_bzero(enc->iv, enc->iv_len);
965 explicit_bzero(enc->key, enc->key_len);
966 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000967 free(enc->name);
968 free(enc->iv);
969 free(enc->key);
970 free(mac->name);
971 free(mac->key);
972 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000973 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000974 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000975 /* move newkeys from kex to state */
976 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
977 return SSH_ERR_INTERNAL_ERROR;
978 ssh->kex->newkeys[mode] = NULL;
979 enc = &state->newkeys[mode]->enc;
980 mac = &state->newkeys[mode]->mac;
981 comp = &state->newkeys[mode]->comp;
982 if (cipher_authlen(enc->cipher) == 0) {
983 if ((r = mac_init(mac)) != 0)
984 return r;
985 }
986 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000987 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000988 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
989 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000990 return r;
991 if (!state->cipher_warning_done &&
992 (wmsg = cipher_warning_message(cc)) != NULL) {
993 error("Warning: %s", wmsg);
994 state->cipher_warning_done = 1;
995 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000996 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100997 /* explicit_bzero(enc->iv, enc->block_size);
998 explicit_bzero(enc->key, enc->key_len);
999 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +10001000 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +10001001 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +00001002 state->after_authentication)) && comp->enabled == 0) {
1003 if ((r = ssh_packet_init_compression(ssh)) < 0)
1004 return r;
1005 if (mode == MODE_OUT) {
1006 if ((r = start_compression_out(ssh, 6)) != 0)
1007 return r;
1008 } else {
1009 if ((r = start_compression_in(ssh)) != 0)
1010 return r;
1011 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001012 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001013 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001014 /*
1015 * The 2^(blocksize*2) limit is too expensive for 3DES,
1016 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1017 */
1018 if (enc->block_size >= 16)
1019 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1020 else
1021 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001022 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001023 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001024 state->rekey_limit / enc->block_size);
1025 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001026}
1027
Damien Miller5428f641999-11-25 11:54:57 +11001028/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001029 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001030 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001031 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1032 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001033static int
1034ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001035{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001036 struct session_state *state = ssh->state;
1037 struct sshcomp *comp = NULL;
1038 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001039
1040 /*
1041 * Remember that we are past the authentication step, so rekeying
1042 * with COMP_DELAYED will turn on compression immediately.
1043 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001044 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001045 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001046 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001047 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001048 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001049 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001050 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001051 if ((r = ssh_packet_init_compression(ssh)) != 0)
1052 return r;
1053 if (mode == MODE_OUT) {
1054 if ((r = start_compression_out(ssh, 6)) != 0)
1055 return r;
1056 } else {
1057 if ((r = start_compression_in(ssh)) != 0)
1058 return r;
1059 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001060 comp->enabled = 1;
1061 }
1062 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001063 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001064}
1065
1066/*
Damien Miller33b13562000-04-04 14:38:59 +10001067 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1068 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001069int
1070ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001071{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001072 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001073 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001074 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001075 u_int authlen = 0, aadlen = 0;
1076 u_int len;
1077 struct sshenc *enc = NULL;
1078 struct sshmac *mac = NULL;
1079 struct sshcomp *comp = NULL;
1080 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001081
markus@openbsd.org091c3022015-01-19 19:52:16 +00001082 if (state->newkeys[MODE_OUT] != NULL) {
1083 enc = &state->newkeys[MODE_OUT]->enc;
1084 mac = &state->newkeys[MODE_OUT]->mac;
1085 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001086 /* disable mac for authenticated encryption */
1087 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1088 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001089 }
Damien Miller963f6b22002-02-19 15:21:23 +11001090 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001091 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001092
markus@openbsd.org091c3022015-01-19 19:52:16 +00001093 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001094
1095#ifdef PACKET_DEBUG
1096 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001097 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001098#endif
1099
1100 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001101 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001102 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001103 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1104 goto out;
1105 sshbuf_reset(state->compression_buffer);
1106 if ((r = compress_buffer(ssh, state->outgoing_packet,
1107 state->compression_buffer)) != 0)
1108 goto out;
1109 sshbuf_reset(state->outgoing_packet);
1110 if ((r = sshbuf_put(state->outgoing_packet,
1111 "\0\0\0\0\0", 5)) != 0 ||
1112 (r = sshbuf_putb(state->outgoing_packet,
1113 state->compression_buffer)) != 0)
1114 goto out;
1115 DBG(debug("compression: raw %d compressed %zd", len,
1116 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001117 }
1118
1119 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001120 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001121
1122 /*
1123 * calc size of padding, alloc space, get random data,
1124 * minimum padding is 4 bytes
1125 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001126 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001127 padlen = block_size - (len % block_size);
1128 if (padlen < 4)
1129 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001130 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001131 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001132 state->extra_pad =
1133 roundup(state->extra_pad, block_size);
1134 pad = state->extra_pad -
1135 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001136 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001137 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001138 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001139 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001140 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001141 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1142 goto out;
1143 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001144 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001145 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001146 } else {
1147 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001148 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001149 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001150 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001151 len = sshbuf_len(state->outgoing_packet);
1152 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1153 if (cp == NULL) {
1154 r = SSH_ERR_INTERNAL_ERROR;
1155 goto out;
1156 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001157 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001158 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001159 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001160 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1161 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001162
1163 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001164 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001165 if ((r = mac_compute(mac, state->p_send.seqnr,
1166 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001167 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001168 goto out;
1169 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001170 }
1171 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001172 if ((r = sshbuf_reserve(state->output,
1173 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1174 goto out;
1175 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1176 sshbuf_ptr(state->outgoing_packet),
1177 len - aadlen, aadlen, authlen)) != 0)
1178 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001179 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001180 if (mac && mac->enabled) {
1181 if (mac->etm) {
1182 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001183 if ((r = mac_compute(mac, state->p_send.seqnr,
1184 cp, len, macbuf, sizeof(macbuf))) != 0)
1185 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001186 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001187 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001188 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001189 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1190 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001191 }
Damien Miller33b13562000-04-04 14:38:59 +10001192#ifdef PACKET_DEBUG
1193 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001194 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001195#endif
Damien Miller4af51302000-04-16 11:18:38 +10001196 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001197 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001198 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001199 if (++state->p_send.packets == 0)
1200 if (!(ssh->compat & SSH_BUG_NOREKEY))
1201 return SSH_ERR_NEED_REKEY;
1202 state->p_send.blocks += len / block_size;
1203 state->p_send.bytes += len;
1204 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001205
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001206 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001207 r = ssh_set_newkeys(ssh, MODE_OUT);
1208 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1209 r = ssh_packet_enable_delayed_compress(ssh);
1210 else
1211 r = 0;
1212 out:
1213 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001214}
1215
markus@openbsd.org091c3022015-01-19 19:52:16 +00001216int
1217ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001218{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001219 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001220 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001221 u_char type;
1222 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001223
markus@openbsd.org091c3022015-01-19 19:52:16 +00001224 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001225
1226 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001227 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001228 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1229 (type > SSH2_MSG_TRANSPORT_MAX) ||
1230 (type == SSH2_MSG_SERVICE_REQUEST) ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001231 (type == SSH2_MSG_SERVICE_ACCEPT) ||
1232 (type == SSH2_MSG_EXT_INFO)) {
Damien Millera5539d22003-04-09 20:50:06 +10001233 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001234 p = calloc(1, sizeof(*p));
1235 if (p == NULL)
1236 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001237 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001238 p->payload = state->outgoing_packet;
1239 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1240 state->outgoing_packet = sshbuf_new();
1241 if (state->outgoing_packet == NULL)
1242 return SSH_ERR_ALLOC_FAIL;
1243 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001244 }
1245 }
1246
1247 /* rekeying starts with sending KEXINIT */
1248 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001249 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001250
markus@openbsd.org091c3022015-01-19 19:52:16 +00001251 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1252 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001253
1254 /* after a NEWKEYS message we can send the complete queue */
1255 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001256 state->rekeying = 0;
1257 state->rekey_time = monotime();
1258 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001259 type = p->type;
1260 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001261 sshbuf_free(state->outgoing_packet);
1262 state->outgoing_packet = p->payload;
1263 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001264 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001265 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1266 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001267 }
1268 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001269 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001270}
1271
Damien Miller33b13562000-04-04 14:38:59 +10001272/*
Damien Miller5428f641999-11-25 11:54:57 +11001273 * Waits until a packet has been received, and returns its type. Note that
1274 * no other data is processed until this returns, so this function should not
1275 * be used during the interactive session.
1276 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001277
1278int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001279ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001280{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001281 struct session_state *state = ssh->state;
1282 int len, r, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001283 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001284 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001285 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001286
Darren Tucker99bb7612008-06-13 22:02:50 +10001287 DBG(debug("packet_read()"));
1288
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001289 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001290 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001291 if (setp == NULL)
1292 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001293
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001294 /*
1295 * Since we are blocking, ensure that all written packets have
1296 * been sent.
1297 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001298 if ((r = ssh_packet_write_wait(ssh)) != 0)
1299 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001300
Damien Miller95def091999-11-25 00:26:21 +11001301 /* Stay in the loop until we have received a complete packet. */
1302 for (;;) {
1303 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001304 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1305 if (r != 0)
1306 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001307 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001308 *typep == SSH_SMSG_SUCCESS
1309 || *typep == SSH_SMSG_FAILURE
1310 || *typep == SSH_CMSG_EOF
1311 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1312 if ((r = sshpkt_get_end(ssh)) != 0)
1313 break;
Damien Miller95def091999-11-25 00:26:21 +11001314 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001315 if (*typep != SSH_MSG_NONE)
1316 break;
Damien Miller5428f641999-11-25 11:54:57 +11001317 /*
1318 * Otherwise, wait for some data to arrive, add it to the
1319 * buffer, and try again.
1320 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001321 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001322 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001323 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001324
markus@openbsd.org091c3022015-01-19 19:52:16 +00001325 if (state->packet_timeout_ms > 0) {
1326 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001327 timeoutp = &timeout;
1328 }
Damien Miller95def091999-11-25 00:26:21 +11001329 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001330 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001331 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001332 ms_to_timeval(&timeout, ms_remain);
1333 gettimeofday(&start, NULL);
1334 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001335 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001336 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001337 break;
Damien Millerea437422009-10-02 11:49:03 +10001338 if (errno != EAGAIN && errno != EINTR &&
1339 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001340 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001341 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001342 continue;
1343 ms_subtract_diff(&start, &ms_remain);
1344 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001345 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001346 break;
1347 }
1348 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001349 if (r == 0)
1350 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001351 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001352 do {
1353 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001354 len = roaming_read(state->connection_in, buf,
Darren Tuckerc5564e12009-06-21 18:53:53 +10001355 sizeof(buf), &cont);
1356 } while (len == 0 && cont);
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001357 if (len == 0) {
1358 r = SSH_ERR_CONN_CLOSED;
1359 goto out;
1360 }
1361 if (len < 0) {
1362 r = SSH_ERR_SYSTEM_ERROR;
1363 goto out;
1364 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001365
Damien Miller95def091999-11-25 00:26:21 +11001366 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001367 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001368 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001369 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001370 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001371 free(setp);
1372 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001373}
1374
Damien Miller278f9072001-12-21 15:00:19 +11001375int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001376ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001377{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001378 u_char type;
1379 int r;
1380
1381 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1382 fatal("%s: %s", __func__, ssh_err(r));
1383 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001384}
1385
Damien Miller5428f641999-11-25 11:54:57 +11001386/*
1387 * Waits until a packet has been received, verifies that its type matches
1388 * that given, and gives a fatal error and exits if there is a mismatch.
1389 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001391int
1392ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001394 int r;
1395 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001397 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1398 return r;
1399 if (type != expected_type) {
1400 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001401 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001402 expected_type, type)) != 0)
1403 return r;
1404 return SSH_ERR_PROTOCOL_ERROR;
1405 }
1406 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407}
1408
1409/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001410 * packet_process_incoming. If so, reads the packet; otherwise returns
1411 * SSH_MSG_NONE. This does not wait for data from the connection.
1412 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001413 * SSH_MSG_DISCONNECT is handled specially here. Also,
1414 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1415 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001416 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417
markus@openbsd.org091c3022015-01-19 19:52:16 +00001418int
1419ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001421 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001422 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001423 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001424 const u_char *cp;
1425 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001426 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001427 int r;
1428
1429 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430
Damien Miller95def091999-11-25 00:26:21 +11001431 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001432 if (sshbuf_len(state->input) < 4 + 8)
1433 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001434 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001435 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001436 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1437 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1438 len)) != 0)
1439 return r;
1440 return SSH_ERR_CONN_CORRUPT;
1441 }
Damien Miller95def091999-11-25 00:26:21 +11001442 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443
Damien Miller95def091999-11-25 00:26:21 +11001444 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001445 if (sshbuf_len(state->input) < 4 + padded_len)
1446 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
Damien Miller95def091999-11-25 00:26:21 +11001448 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449
Damien Miller95def091999-11-25 00:26:21 +11001450 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001451 if ((r = sshbuf_consume(state->input, 4)) != 0)
1452 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001453
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001454 /*
1455 * Cryptographic attack detector for ssh
1456 * (C)1998 CORE-SDI, Buenos Aires Argentina
1457 * Ariel Futoransky(futo@core-sdi.com)
1458 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001459 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001460 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001461 switch (detect_attack(&state->deattack,
1462 sshbuf_ptr(state->input), padded_len)) {
1463 case DEATTACK_OK:
1464 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001465 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001466 emsg = "crc32 compensation attack detected";
1467 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001468 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001469 emsg = "deattack denial of service detected";
1470 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001471 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001472 emsg = "deattack error";
1473 break;
1474 }
1475 if (emsg != NULL) {
1476 error("%s", emsg);
1477 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1478 (r = ssh_packet_write_wait(ssh)) != 0)
1479 return r;
1480 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001481 }
1482 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001483
1484 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001485 sshbuf_reset(state->incoming_packet);
1486 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1487 goto out;
1488 if ((r = cipher_crypt(&state->receive_context, 0, p,
1489 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1490 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001491
markus@openbsd.org091c3022015-01-19 19:52:16 +00001492 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1493 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494
1495#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001496 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001497 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499
Damien Miller95def091999-11-25 00:26:21 +11001500 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001501 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1502 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001503
Damien Miller95def091999-11-25 00:26:21 +11001504 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001505 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1506 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001507
Damien Miller95def091999-11-25 00:26:21 +11001508 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001509 if (len != sshbuf_len(state->incoming_packet)) {
1510 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001511 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001512 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1513 (r = ssh_packet_write_wait(ssh)) != 0)
1514 return r;
1515 return SSH_ERR_CONN_CORRUPT;
1516 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517
markus@openbsd.org091c3022015-01-19 19:52:16 +00001518 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1519 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001520 if (checksum != stored_checksum) {
1521 error("Corrupted check bytes on input");
1522 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1523 (r = ssh_packet_write_wait(ssh)) != 0)
1524 return r;
1525 return SSH_ERR_CONN_CORRUPT;
1526 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001527 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1528 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001529
markus@openbsd.org091c3022015-01-19 19:52:16 +00001530 if (state->packet_compression) {
1531 sshbuf_reset(state->compression_buffer);
1532 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1533 state->compression_buffer)) != 0)
1534 goto out;
1535 sshbuf_reset(state->incoming_packet);
1536 if ((r = sshbuf_putb(state->incoming_packet,
1537 state->compression_buffer)) != 0)
1538 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001539 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001540 state->p_read.packets++;
1541 state->p_read.bytes += padded_len + 4;
1542 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1543 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001544 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1545 error("Invalid ssh1 packet type: %d", *typep);
1546 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1547 (r = ssh_packet_write_wait(ssh)) != 0)
1548 return r;
1549 return SSH_ERR_PROTOCOL_ERROR;
1550 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001551 r = 0;
1552 out:
1553 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001554}
Damien Miller95def091999-11-25 00:26:21 +11001555
markus@openbsd.org091c3022015-01-19 19:52:16 +00001556int
1557ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001558{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001559 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001560 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001561 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1562 u_int maclen, aadlen = 0, authlen = 0, block_size;
1563 struct sshenc *enc = NULL;
1564 struct sshmac *mac = NULL;
1565 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001566 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001567
markus@openbsd.org091c3022015-01-19 19:52:16 +00001568 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001569
markus@openbsd.org091c3022015-01-19 19:52:16 +00001570 if (state->packet_discard)
1571 return 0;
1572
1573 if (state->newkeys[MODE_IN] != NULL) {
1574 enc = &state->newkeys[MODE_IN]->enc;
1575 mac = &state->newkeys[MODE_IN]->mac;
1576 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001577 /* disable mac for authenticated encryption */
1578 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1579 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001580 }
1581 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001582 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001583 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001584
markus@openbsd.org091c3022015-01-19 19:52:16 +00001585 if (aadlen && state->packlen == 0) {
1586 if (cipher_get_length(&state->receive_context,
1587 &state->packlen, state->p_read.seqnr,
1588 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1589 return 0;
1590 if (state->packlen < 1 + 4 ||
1591 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001592#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001593 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001594#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001595 logit("Bad packet length %u.", state->packlen);
1596 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1597 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001598 return SSH_ERR_CONN_CORRUPT;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001599 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001600 sshbuf_reset(state->incoming_packet);
1601 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001602 /*
1603 * check if input size is less than the cipher block size,
1604 * decrypt first block and extract length of incoming packet
1605 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001606 if (sshbuf_len(state->input) < block_size)
1607 return 0;
1608 sshbuf_reset(state->incoming_packet);
1609 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1610 &cp)) != 0)
1611 goto out;
1612 if ((r = cipher_crypt(&state->receive_context,
1613 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1614 block_size, 0, 0)) != 0)
1615 goto out;
1616 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1617 if (state->packlen < 1 + 4 ||
1618 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001619#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001620 fprintf(stderr, "input: \n");
1621 sshbuf_dump(state->input, stderr);
1622 fprintf(stderr, "incoming_packet: \n");
1623 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001624#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001625 logit("Bad packet length %u.", state->packlen);
1626 return ssh_packet_start_discard(ssh, enc, mac,
1627 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001628 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001629 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1630 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001631 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001632 DBG(debug("input: packet len %u", state->packlen+4));
1633
Damien Milleraf43a7a2012-12-12 10:46:31 +11001634 if (aadlen) {
1635 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001636 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001637 } else {
1638 /*
1639 * the payload size and the payload are encrypted, but we
1640 * have a partial packet of block_size bytes
1641 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001642 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001643 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001644 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1645 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001646 if (need % block_size != 0) {
1647 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001648 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001649 return ssh_packet_start_discard(ssh, enc, mac,
1650 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001651 }
Damien Miller33b13562000-04-04 14:38:59 +10001652 /*
1653 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001654 * decrypt into incoming_packet:
1655 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001656 * 'need' bytes are encrypted, followed by either
1657 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001658 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001659 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001660 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1661 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001662#ifdef PACKET_DEBUG
1663 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001664 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001665#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001666 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001667 if (mac && mac->enabled && mac->etm) {
1668 if ((r = mac_compute(mac, state->p_read.seqnr,
1669 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001670 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001671 goto out;
1672 }
1673 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1674 &cp)) != 0)
1675 goto out;
1676 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1677 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1678 goto out;
1679 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1680 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001681 /*
1682 * compute MAC over seqnr and packet,
1683 * increment sequence number for incoming packet
1684 */
Damien Miller4af51302000-04-16 11:18:38 +10001685 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001686 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001687 if ((r = mac_compute(mac, state->p_read.seqnr,
1688 sshbuf_ptr(state->incoming_packet),
1689 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001690 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001691 goto out;
1692 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001693 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001694 logit("Corrupted MAC on input.");
1695 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001696 return SSH_ERR_INTERNAL_ERROR;
1697 return ssh_packet_start_discard(ssh, enc, mac,
1698 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001699 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001700
1701 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1702 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1703 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001704 }
Damien Miller278f9072001-12-21 15:00:19 +11001705 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001706 *seqnr_p = state->p_read.seqnr;
1707 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001708 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001709 if (++state->p_read.packets == 0)
1710 if (!(ssh->compat & SSH_BUG_NOREKEY))
1711 return SSH_ERR_NEED_REKEY;
1712 state->p_read.blocks += (state->packlen + 4) / block_size;
1713 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001714
1715 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001716 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001717 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001718 if (padlen < 4) {
1719 if ((r = sshpkt_disconnect(ssh,
1720 "Corrupted padlen %d on input.", padlen)) != 0 ||
1721 (r = ssh_packet_write_wait(ssh)) != 0)
1722 return r;
1723 return SSH_ERR_CONN_CORRUPT;
1724 }
Damien Miller33b13562000-04-04 14:38:59 +10001725
1726 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001727 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1728 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1729 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001730
markus@openbsd.org091c3022015-01-19 19:52:16 +00001731 DBG(debug("input: len before de-compress %zd",
1732 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001733 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001734 sshbuf_reset(state->compression_buffer);
1735 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1736 state->compression_buffer)) != 0)
1737 goto out;
1738 sshbuf_reset(state->incoming_packet);
1739 if ((r = sshbuf_putb(state->incoming_packet,
1740 state->compression_buffer)) != 0)
1741 goto out;
1742 DBG(debug("input: len after de-compress %zd",
1743 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001744 }
1745 /*
1746 * get packet type, implies consume.
1747 * return length of payload (without type field)
1748 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001749 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1750 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001751 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1752 if ((r = sshpkt_disconnect(ssh,
1753 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1754 (r = ssh_packet_write_wait(ssh)) != 0)
1755 return r;
1756 return SSH_ERR_PROTOCOL_ERROR;
1757 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001758 if (*typep == SSH2_MSG_NEWKEYS)
1759 r = ssh_set_newkeys(ssh, MODE_IN);
1760 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1761 r = ssh_packet_enable_delayed_compress(ssh);
1762 else
1763 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001764#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001765 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1766 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001767#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001768 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001769 state->packlen = 0;
1770 out:
1771 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001772}
1773
1774int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001775ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001776{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001777 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001778 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001779 int r;
1780 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001781
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001782 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001783 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001784 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001785 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1786 if (r != 0)
1787 return r;
1788 if (*typep) {
1789 state->keep_alive_timeouts = 0;
1790 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001791 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001792 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001793 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001794 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001795 break;
Damien Miller33b13562000-04-04 14:38:59 +10001796 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001797 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1798 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1799 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001800 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001801 return r;
1802 }
Damien Miller33b13562000-04-04 14:38:59 +10001803 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001804 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001805 break;
1806 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001807 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1808 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1809 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001810 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001811 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001812 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1813 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001814 "Received disconnect from %s port %d:"
1815 "%u: %.400s", ssh_remote_ipaddr(ssh),
1816 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001817 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001818 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001819 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001820 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1821 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001822 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1823 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001824 break;
Damien Miller33b13562000-04-04 14:38:59 +10001825 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001826 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001827 }
Damien Miller33b13562000-04-04 14:38:59 +10001828 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001829 r = ssh_packet_read_poll1(ssh, typep);
1830 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001831 case SSH_MSG_NONE:
1832 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001833 case SSH_MSG_IGNORE:
1834 break;
1835 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001836 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1837 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001838 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001839 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001840 break;
1841 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001842 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1843 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001844 error("Received disconnect from %s port %d: "
1845 "%.400s", ssh_remote_ipaddr(ssh),
1846 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001847 free(msg);
1848 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001849 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001850 DBG(debug("received packet type %d", *typep));
1851 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001852 }
Damien Miller33b13562000-04-04 14:38:59 +10001853 }
1854 }
1855}
1856
Damien Miller5428f641999-11-25 11:54:57 +11001857/*
1858 * Buffers the given amount of input characters. This is intended to be used
1859 * together with packet_read_poll.
1860 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001861
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001862int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001863ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001864{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001865 struct session_state *state = ssh->state;
1866 int r;
1867
1868 if (state->packet_discard) {
1869 state->keep_alive_timeouts = 0; /* ?? */
1870 if (len >= state->packet_discard) {
1871 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001872 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001873 }
1874 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001875 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001876 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001877 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001878 return r;
1879
1880 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001881}
1882
Damien Miller4af51302000-04-16 11:18:38 +10001883int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001884ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001885{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001886 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001887}
1888
Damien Miller5428f641999-11-25 11:54:57 +11001889/*
1890 * Sends a diagnostic message from the server to the client. This message
1891 * can be sent at any time (but not while constructing another message). The
1892 * message is printed immediately, but only if the client is being executed
1893 * in verbose mode. These messages are primarily intended to ease debugging
1894 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001895 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11001896 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001897void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001898ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001899{
Damien Miller95def091999-11-25 00:26:21 +11001900 char buf[1024];
1901 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001902 int r;
Damien Miller95def091999-11-25 00:26:21 +11001903
markus@openbsd.org091c3022015-01-19 19:52:16 +00001904 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001905 return;
1906
Damien Miller95def091999-11-25 00:26:21 +11001907 va_start(args, fmt);
1908 vsnprintf(buf, sizeof(buf), fmt, args);
1909 va_end(args);
1910
Damien Miller7c8af4f2000-05-01 08:24:07 +10001911 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001912 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1913 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1914 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1915 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1916 (r = sshpkt_send(ssh)) != 0)
1917 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001918 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001919 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1920 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1921 (r = sshpkt_send(ssh)) != 0)
1922 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001923 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001924 if ((r = ssh_packet_write_wait(ssh)) != 0)
1925 fatal("%s: %s", __func__, ssh_err(r));
1926}
1927
1928/*
1929 * Pretty-print connection-terminating errors and exit.
1930 */
1931void
1932sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
1933{
1934 switch (r) {
1935 case SSH_ERR_CONN_CLOSED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001936 logit("Connection closed by %.200s port %d",
1937 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001938 cleanup_exit(255);
1939 case SSH_ERR_CONN_TIMEOUT:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001940 logit("Connection %s %.200s port %d timed out",
1941 ssh->state->server_side ? "from" : "to",
1942 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001943 cleanup_exit(255);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001944 case SSH_ERR_DISCONNECTED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001945 logit("Disconnected from %.200s port %d",
1946 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001947 cleanup_exit(255);
1948 case SSH_ERR_SYSTEM_ERROR:
1949 if (errno == ECONNRESET) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001950 logit("Connection reset by %.200s port %d",
1951 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001952 cleanup_exit(255);
1953 }
1954 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001955 case SSH_ERR_NO_CIPHER_ALG_MATCH:
1956 case SSH_ERR_NO_MAC_ALG_MATCH:
1957 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
1958 case SSH_ERR_NO_KEX_ALG_MATCH:
1959 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
1960 if (ssh && ssh->kex && ssh->kex->failed_choice) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001961 fatal("Unable to negotiate with %.200s port %d: %s. "
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001962 "Their offer: %s", ssh_remote_ipaddr(ssh),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001963 ssh_remote_port(ssh), ssh_err(r),
1964 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001965 }
1966 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001967 default:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001968 fatal("%s%sConnection %s %.200s port %d: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001969 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001970 ssh->state->server_side ? "from" : "to",
1971 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001972 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001973}
1974
Damien Miller5428f641999-11-25 11:54:57 +11001975/*
1976 * Logs the error plus constructs and sends a disconnect packet, closes the
1977 * connection, and exits. This function never returns. The error message
1978 * should not contain a newline. The length of the formatted message must
1979 * not exceed 1024 bytes.
1980 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001981void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001982ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001983{
Damien Miller95def091999-11-25 00:26:21 +11001984 char buf[1024];
1985 va_list args;
1986 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001987 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001988
Damien Miller95def091999-11-25 00:26:21 +11001989 if (disconnecting) /* Guard against recursive invocations. */
1990 fatal("packet_disconnect called recursively.");
1991 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001992
Damien Miller5428f641999-11-25 11:54:57 +11001993 /*
1994 * Format the message. Note that the caller must make sure the
1995 * message is of limited size.
1996 */
Damien Miller95def091999-11-25 00:26:21 +11001997 va_start(args, fmt);
1998 vsnprintf(buf, sizeof(buf), fmt, args);
1999 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002000
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002001 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10002002 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002003
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002004 /*
2005 * Send the disconnect message to the other side, and wait
2006 * for it to get sent.
2007 */
2008 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2009 sshpkt_fatal(ssh, __func__, r);
2010
2011 if ((r = ssh_packet_write_wait(ssh)) != 0)
2012 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002013
Damien Miller95def091999-11-25 00:26:21 +11002014 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002015 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002016 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002017}
2018
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002019/*
2020 * Checks if there is any buffered output, and tries to write some of
2021 * the output.
2022 */
2023int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002024ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002025{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002026 struct session_state *state = ssh->state;
2027 int len = sshbuf_len(state->output);
2028 int cont, r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002029
Damien Miller95def091999-11-25 00:26:21 +11002030 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10002031 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002032 len = roaming_write(state->connection_out,
2033 sshbuf_ptr(state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10002034 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002035 if (errno == EINTR || errno == EAGAIN ||
2036 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002037 return 0;
2038 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002039 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10002040 if (len == 0 && !cont)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002041 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002042 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002043 return r;
Damien Miller95def091999-11-25 00:26:21 +11002044 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002045 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002046}
2047
Damien Miller5428f641999-11-25 11:54:57 +11002048/*
2049 * Calls packet_write_poll repeatedly until all pending output data has been
2050 * written.
2051 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002052int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002053ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002054{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002055 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002056 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002057 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002058 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002059
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002060 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002061 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002062 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002063 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002064 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2065 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002066 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002067 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002068 while (ssh_packet_have_data_to_write(ssh)) {
2069 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002070 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002071 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002072
markus@openbsd.org091c3022015-01-19 19:52:16 +00002073 if (state->packet_timeout_ms > 0) {
2074 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002075 timeoutp = &timeout;
2076 }
2077 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002078 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002079 ms_to_timeval(&timeout, ms_remain);
2080 gettimeofday(&start, NULL);
2081 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002082 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002083 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002084 break;
Damien Millerea437422009-10-02 11:49:03 +10002085 if (errno != EAGAIN && errno != EINTR &&
2086 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002087 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002088 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002089 continue;
2090 ms_subtract_diff(&start, &ms_remain);
2091 if (ms_remain <= 0) {
2092 ret = 0;
2093 break;
2094 }
2095 }
2096 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002097 free(setp);
2098 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002099 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002100 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2101 free(setp);
2102 return r;
2103 }
Damien Miller95def091999-11-25 00:26:21 +11002104 }
Darren Tuckera627d422013-06-02 07:31:17 +10002105 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002106 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002107}
2108
2109/* Returns true if there is buffered data to write to the connection. */
2110
2111int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002112ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002113{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002114 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002115}
2116
2117/* Returns true if there is not too much data to write to the connection. */
2118
2119int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002120ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002121{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002122 if (ssh->state->interactive_mode)
2123 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002124 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002125 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002126}
2127
markus@openbsd.org091c3022015-01-19 19:52:16 +00002128void
2129ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002130{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002131#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002132 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002133 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002134 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002135# ifdef IP_TOS
2136 case AF_INET:
2137 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002138 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002139 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2140 error("setsockopt IP_TOS %d: %.100s:",
2141 tos, strerror(errno));
2142 break;
2143# endif /* IP_TOS */
2144# ifdef IPV6_TCLASS
2145 case AF_INET6:
2146 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002147 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002148 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2149 error("setsockopt IPV6_TCLASS %d: %.100s:",
2150 tos, strerror(errno));
2151 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002152# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002153 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002154#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002155}
Ben Lindstroma7433982002-12-23 02:41:41 +00002156
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002157/* Informs that the current session is interactive. Sets IP flags for that. */
2158
2159void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002160ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002161{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002162 struct session_state *state = ssh->state;
2163
2164 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002165 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002166 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002167
Damien Miller95def091999-11-25 00:26:21 +11002168 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002169 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002170
Damien Miller34132e52000-01-14 15:45:46 +11002171 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002172 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002173 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002174 set_nodelay(state->connection_in);
2175 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2176 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002177}
2178
2179/* Returns true if the current connection is interactive. */
2180
2181int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002182ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002183{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002184 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002185}
Damien Miller6162d121999-11-21 13:23:52 +11002186
Darren Tucker1f8311c2004-05-13 16:39:33 +10002187int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002188ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002189{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002190 struct session_state *state = ssh->state;
2191
2192 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002193 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002194 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002195 return -1;
2196 }
2197 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002198 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002199 return -1;
2200 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002201 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002202 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002203 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002204 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002205}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002206
Darren Tuckerf7288d72009-06-21 18:12:20 +10002207int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002208ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002209{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002210 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002211}
2212
2213void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002214ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002215{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002216 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002217}
2218
2219u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002220ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002221{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002222 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002223}
2224
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002225/*
2226 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002227 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002228 * byte SSH_MSG_IGNORE
2229 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002230 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002231 * All implementations MUST understand (and ignore) this message at any
2232 * time (after receiving the protocol version). No implementation is
2233 * required to send them. This message can be used as an additional
2234 * protection measure against advanced traffic analysis techniques.
2235 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002236void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002237ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002238{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002239 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002240 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002241
markus@openbsd.org091c3022015-01-19 19:52:16 +00002242 if ((r = sshpkt_start(ssh, compat20 ?
2243 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2244 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2245 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002246 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002247 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002248 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002249 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2250 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002251 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002252 }
2253}
Damien Millera5539d22003-04-09 20:50:06 +10002254
Darren Tucker1f8311c2004-05-13 16:39:33 +10002255#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002256int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002257ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002258{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002259 struct session_state *state = ssh->state;
2260
2261 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002262 return 0;
2263 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002264 (state->p_send.packets > MAX_PACKETS) ||
2265 (state->p_read.packets > MAX_PACKETS) ||
2266 (state->max_blocks_out &&
2267 (state->p_send.blocks > state->max_blocks_out)) ||
2268 (state->max_blocks_in &&
2269 (state->p_read.blocks > state->max_blocks_in)) ||
2270 (state->rekey_interval != 0 && state->rekey_time +
2271 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002272}
2273
2274void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002275ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002276{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002277 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
2278 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002279 ssh->state->rekey_limit = bytes;
2280 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002281}
2282
2283time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002284ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002285{
2286 time_t seconds;
2287
markus@openbsd.org091c3022015-01-19 19:52:16 +00002288 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002289 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002290 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002291}
Damien Miller9786e6e2005-07-26 21:54:56 +10002292
2293void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002294ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002295{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002296 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002297}
2298
2299void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002300ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002301{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002302 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002303}
2304
2305void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002306ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002307{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002308 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002309}
2310
2311void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002312ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002313{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002314 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002315}
2316
markus@openbsd.org091c3022015-01-19 19:52:16 +00002317/* XXX TODO update roaming to new API (does not work anyway) */
Darren Tuckere841eb02009-07-06 07:11:13 +10002318/*
2319 * Save the state for the real connection, and use a separate state when
2320 * resuming a suspended connection.
2321 */
2322void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002323ssh_packet_backup_state(struct ssh *ssh,
2324 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002325{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002326 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002327
markus@openbsd.org091c3022015-01-19 19:52:16 +00002328 close(ssh->state->connection_in);
2329 ssh->state->connection_in = -1;
2330 close(ssh->state->connection_out);
2331 ssh->state->connection_out = -1;
Darren Tuckere841eb02009-07-06 07:11:13 +10002332 if (backup_state)
2333 tmp = backup_state;
2334 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002335 tmp = ssh_alloc_session_state();
2336 backup_state = ssh;
2337 ssh = tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002338}
2339
markus@openbsd.org091c3022015-01-19 19:52:16 +00002340/* XXX FIXME FIXME FIXME */
Darren Tuckere841eb02009-07-06 07:11:13 +10002341/*
2342 * Swap in the old state when resuming a connecion.
2343 */
2344void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002345ssh_packet_restore_state(struct ssh *ssh,
2346 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002347{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002348 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002349 u_int len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002350 int r;
Darren Tuckere841eb02009-07-06 07:11:13 +10002351
2352 tmp = backup_state;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002353 backup_state = ssh;
2354 ssh = tmp;
2355 ssh->state->connection_in = backup_state->state->connection_in;
2356 backup_state->state->connection_in = -1;
2357 ssh->state->connection_out = backup_state->state->connection_out;
2358 backup_state->state->connection_out = -1;
2359 len = sshbuf_len(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002360 if (len > 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002361 if ((r = sshbuf_putb(ssh->state->input,
2362 backup_state->state->input)) != 0)
2363 fatal("%s: %s", __func__, ssh_err(r));
2364 sshbuf_reset(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002365 add_recv_bytes(len);
2366 }
2367}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002368
2369/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002370static int
2371ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002372{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002373 struct sshcomp *comp;
2374 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002375
2376 debug("%s: called", __func__);
2377 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002378 ssh->state->after_authentication = 1;
2379 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002380 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002381 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002382 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002383 comp = &ssh->state->newkeys[mode]->comp;
2384 if (comp && comp->enabled &&
2385 (r = ssh_packet_init_compression(ssh)) != 0)
2386 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002387 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002388 return 0;
2389}
2390
2391/* Packet state (de-)serialization for privsep */
2392
2393/* turn kex into a blob for packet state serialization */
2394static int
2395kex_to_blob(struct sshbuf *m, struct kex *kex)
2396{
2397 int r;
2398
2399 if ((r = sshbuf_put_string(m, kex->session_id,
2400 kex->session_id_len)) != 0 ||
2401 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2402 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2403 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2404 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2405 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2406 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2407 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2408 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2409 return r;
2410 return 0;
2411}
2412
2413/* turn key exchange results into a blob for packet state serialization */
2414static int
2415newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2416{
2417 struct sshbuf *b;
2418 struct sshcipher_ctx *cc;
2419 struct sshcomp *comp;
2420 struct sshenc *enc;
2421 struct sshmac *mac;
2422 struct newkeys *newkey;
2423 int r;
2424
2425 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2426 return SSH_ERR_INTERNAL_ERROR;
2427 enc = &newkey->enc;
2428 mac = &newkey->mac;
2429 comp = &newkey->comp;
2430 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2431 &ssh->state->receive_context;
2432 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2433 return r;
2434 if ((b = sshbuf_new()) == NULL)
2435 return SSH_ERR_ALLOC_FAIL;
2436 /* The cipher struct is constant and shared, you export pointer */
2437 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2438 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2439 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2440 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2441 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2442 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2443 goto out;
2444 if (cipher_authlen(enc->cipher) == 0) {
2445 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2446 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2447 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2448 goto out;
2449 }
2450 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2451 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2452 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2453 goto out;
2454 r = sshbuf_put_stringb(m, b);
2455 out:
2456 if (b != NULL)
2457 sshbuf_free(b);
2458 return r;
2459}
2460
2461/* serialize packet state into a blob */
2462int
2463ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2464{
2465 struct session_state *state = ssh->state;
2466 u_char *p;
2467 size_t slen, rlen;
2468 int r, ssh1cipher;
2469
2470 if (!compat20) {
2471 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2472 slen = cipher_get_keyiv_len(&state->send_context);
2473 rlen = cipher_get_keyiv_len(&state->receive_context);
2474 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2475 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2476 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2477 (r = sshbuf_put_u32(m, slen)) != 0 ||
2478 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2479 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2480 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2481 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2482 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2483 return r;
2484 } else {
2485 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2486 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2487 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002488 (r = sshbuf_put_u32(m, state->rekey_limit)) != 0 ||
2489 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002490 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2491 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2492 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2493 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2494 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2495 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2496 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2497 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2498 return r;
2499 }
2500
2501 slen = cipher_get_keycontext(&state->send_context, NULL);
2502 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2503 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2504 (r = sshbuf_reserve(m, slen, &p)) != 0)
2505 return r;
2506 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2507 return SSH_ERR_INTERNAL_ERROR;
2508 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2509 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2510 return r;
2511 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2512 return SSH_ERR_INTERNAL_ERROR;
2513
2514 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2515 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2516 (r = sshbuf_put_stringb(m, state->output)) != 0)
2517 return r;
2518
2519 if (compat20) {
2520 if ((r = sshbuf_put_u64(m, get_sent_bytes())) != 0 ||
2521 (r = sshbuf_put_u64(m, get_recv_bytes())) != 0)
2522 return r;
2523 }
2524 return 0;
2525}
2526
2527/* restore key exchange results from blob for packet state de-serialization */
2528static int
2529newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2530{
2531 struct sshbuf *b = NULL;
2532 struct sshcomp *comp;
2533 struct sshenc *enc;
2534 struct sshmac *mac;
2535 struct newkeys *newkey = NULL;
2536 size_t keylen, ivlen, maclen;
2537 int r;
2538
2539 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2540 r = SSH_ERR_ALLOC_FAIL;
2541 goto out;
2542 }
2543 if ((r = sshbuf_froms(m, &b)) != 0)
2544 goto out;
2545#ifdef DEBUG_PK
2546 sshbuf_dump(b, stderr);
2547#endif
2548 enc = &newkey->enc;
2549 mac = &newkey->mac;
2550 comp = &newkey->comp;
2551
2552 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2553 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2554 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2555 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2556 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2557 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2558 goto out;
2559 if (cipher_authlen(enc->cipher) == 0) {
2560 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2561 goto out;
2562 if ((r = mac_setup(mac, mac->name)) != 0)
2563 goto out;
2564 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2565 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2566 goto out;
2567 if (maclen > mac->key_len) {
2568 r = SSH_ERR_INVALID_FORMAT;
2569 goto out;
2570 }
2571 mac->key_len = maclen;
2572 }
2573 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2574 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2575 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2576 goto out;
2577 if (enc->name == NULL ||
2578 cipher_by_name(enc->name) != enc->cipher) {
2579 r = SSH_ERR_INVALID_FORMAT;
2580 goto out;
2581 }
2582 if (sshbuf_len(b) != 0) {
2583 r = SSH_ERR_INVALID_FORMAT;
2584 goto out;
2585 }
2586 enc->key_len = keylen;
2587 enc->iv_len = ivlen;
2588 ssh->kex->newkeys[mode] = newkey;
2589 newkey = NULL;
2590 r = 0;
2591 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002592 free(newkey);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002593 if (b != NULL)
2594 sshbuf_free(b);
2595 return r;
2596}
2597
2598/* restore kex from blob for packet state de-serialization */
2599static int
2600kex_from_blob(struct sshbuf *m, struct kex **kexp)
2601{
2602 struct kex *kex;
2603 int r;
2604
2605 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2606 (kex->my = sshbuf_new()) == NULL ||
2607 (kex->peer = sshbuf_new()) == NULL) {
2608 r = SSH_ERR_ALLOC_FAIL;
2609 goto out;
2610 }
2611 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2612 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2613 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2614 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2615 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2616 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2617 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2618 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2619 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2620 goto out;
2621 kex->server = 1;
2622 kex->done = 1;
2623 r = 0;
2624 out:
2625 if (r != 0 || kexp == NULL) {
2626 if (kex != NULL) {
2627 if (kex->my != NULL)
2628 sshbuf_free(kex->my);
2629 if (kex->peer != NULL)
2630 sshbuf_free(kex->peer);
2631 free(kex);
2632 }
2633 if (kexp != NULL)
2634 *kexp = NULL;
2635 } else {
2636 *kexp = kex;
2637 }
2638 return r;
2639}
2640
2641/*
2642 * Restore packet state from content of blob 'm' (de-serialization).
2643 * Note that 'm' will be partially consumed on parsing or any other errors.
2644 */
2645int
2646ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2647{
2648 struct session_state *state = ssh->state;
2649 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2650 size_t ssh1keylen, rlen, slen, ilen, olen;
2651 int r;
2652 u_int ssh1cipher = 0;
2653 u_int64_t sent_bytes = 0, recv_bytes = 0;
2654
2655 if (!compat20) {
2656 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2657 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2658 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2659 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2660 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2661 return r;
2662 if (ssh1cipher > INT_MAX)
2663 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2664 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2665 (int)ssh1cipher);
2666 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2667 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2668 return SSH_ERR_INVALID_FORMAT;
2669 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2670 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2671 return r;
2672 } else {
2673 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2674 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2675 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002676 (r = sshbuf_get_u32(m, &state->rekey_limit)) != 0 ||
2677 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002678 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2679 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2680 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2681 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2682 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2683 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2684 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2685 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2686 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002687 /*
2688 * We set the time here so that in post-auth privsep slave we
2689 * count from the completion of the authentication.
2690 */
2691 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002692 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2693 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2694 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2695 return r;
2696 }
2697 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2698 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2699 return r;
2700 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2701 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2702 return SSH_ERR_INVALID_FORMAT;
2703 cipher_set_keycontext(&state->send_context, keyout);
2704 cipher_set_keycontext(&state->receive_context, keyin);
2705
2706 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2707 (r = ssh_packet_set_postauth(ssh)) != 0)
2708 return r;
2709
2710 sshbuf_reset(state->input);
2711 sshbuf_reset(state->output);
2712 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2713 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2714 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2715 (r = sshbuf_put(state->output, output, olen)) != 0)
2716 return r;
2717
2718 if (compat20) {
2719 if ((r = sshbuf_get_u64(m, &sent_bytes)) != 0 ||
2720 (r = sshbuf_get_u64(m, &recv_bytes)) != 0)
2721 return r;
2722 roam_set_bytes(sent_bytes, recv_bytes);
2723 }
2724 if (sshbuf_len(m))
2725 return SSH_ERR_INVALID_FORMAT;
2726 debug3("%s: done", __func__);
2727 return 0;
2728}
2729
2730/* NEW API */
2731
2732/* put data to the outgoing packet */
2733
2734int
2735sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2736{
2737 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2738}
2739
2740int
2741sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2742{
2743 return sshbuf_putb(ssh->state->outgoing_packet, b);
2744}
2745
2746int
2747sshpkt_put_u8(struct ssh *ssh, u_char val)
2748{
2749 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2750}
2751
2752int
2753sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2754{
2755 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2756}
2757
2758int
2759sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2760{
2761 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2762}
2763
2764int
2765sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2766{
2767 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2768}
2769
2770int
2771sshpkt_put_cstring(struct ssh *ssh, const void *v)
2772{
2773 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2774}
2775
2776int
2777sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2778{
2779 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2780}
2781
djm@openbsd.org734226b2015-04-27 01:52:30 +00002782#ifdef WITH_OPENSSL
2783#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002784int
2785sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2786{
2787 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2788}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002789#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002790
Damien Miller773dda22015-01-30 23:10:17 +11002791#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002792int
2793sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2794{
2795 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2796}
Damien Miller773dda22015-01-30 23:10:17 +11002797#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002798
2799int
2800sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2801{
2802 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2803}
Damien Miller773dda22015-01-30 23:10:17 +11002804#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002805
2806/* fetch data from the incoming packet */
2807
2808int
2809sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2810{
2811 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2812}
2813
2814int
2815sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2816{
2817 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2818}
2819
2820int
2821sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2822{
2823 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2824}
2825
2826int
2827sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2828{
2829 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2830}
2831
2832int
2833sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2834{
2835 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2836}
2837
2838int
2839sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2840{
2841 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2842}
2843
2844int
2845sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2846{
2847 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2848}
2849
djm@openbsd.org734226b2015-04-27 01:52:30 +00002850#ifdef WITH_OPENSSL
2851#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002852int
2853sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2854{
2855 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2856}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002857#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002858
Damien Miller773dda22015-01-30 23:10:17 +11002859#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002860int
2861sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2862{
2863 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2864}
Damien Miller773dda22015-01-30 23:10:17 +11002865#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002866
2867int
2868sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2869{
2870 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2871}
Damien Miller773dda22015-01-30 23:10:17 +11002872#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002873
2874int
2875sshpkt_get_end(struct ssh *ssh)
2876{
2877 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2878 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2879 return 0;
2880}
2881
2882const u_char *
2883sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2884{
2885 if (lenp != NULL)
2886 *lenp = sshbuf_len(ssh->state->incoming_packet);
2887 return sshbuf_ptr(ssh->state->incoming_packet);
2888}
2889
2890/* start a new packet */
2891
2892int
2893sshpkt_start(struct ssh *ssh, u_char type)
2894{
2895 u_char buf[9];
2896 int len;
2897
2898 DBG(debug("packet_start[%d]", type));
2899 len = compat20 ? 6 : 9;
2900 memset(buf, 0, len - 1);
2901 buf[len - 1] = type;
2902 sshbuf_reset(ssh->state->outgoing_packet);
2903 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2904}
2905
2906/* send it */
2907
2908int
2909sshpkt_send(struct ssh *ssh)
2910{
2911 if (compat20)
2912 return ssh_packet_send2(ssh);
2913 else
2914 return ssh_packet_send1(ssh);
2915}
2916
2917int
2918sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2919{
2920 char buf[1024];
2921 va_list args;
2922 int r;
2923
2924 va_start(args, fmt);
2925 vsnprintf(buf, sizeof(buf), fmt, args);
2926 va_end(args);
2927
2928 if (compat20) {
2929 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2930 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2931 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2932 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2933 (r = sshpkt_send(ssh)) != 0)
2934 return r;
2935 } else {
2936 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2937 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2938 (r = sshpkt_send(ssh)) != 0)
2939 return r;
2940 }
2941 return 0;
2942}
2943
2944/* roundup current message to pad bytes */
2945int
2946sshpkt_add_padding(struct ssh *ssh, u_char pad)
2947{
2948 ssh->state->extra_pad = pad;
2949 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002950}