blob: 4922573ab7325617fc0871ab3690efa6f978911c [file] [log] [blame]
djm@openbsd.org734226b2015-04-27 01:52:30 +00001/* $OpenBSD: packet.c,v 1.211 2015/04/27 01:52:30 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 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000341 logit("Finished discarding for %.200s", ssh_remote_ipaddr(ssh));
342 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100343}
344
markus@openbsd.org091c3022015-01-19 19:52:16 +0000345static int
346ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
347 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100348{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000349 struct session_state *state = ssh->state;
350 int r;
351
352 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
353 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
354 return r;
355 return SSH_ERR_MAC_INVALID;
356 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100357 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000358 state->packet_discard_mac = mac;
359 if (sshbuf_len(state->input) >= discard &&
360 (r = ssh_packet_stop_discard(ssh)) != 0)
361 return r;
362 state->packet_discard = discard - sshbuf_len(state->input);
363 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100364}
365
Damien Miller34132e52000-01-14 15:45:46 +1100366/* Returns 1 if remote host is connected via socket, 0 if not. */
367
368int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000369ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100370{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000371 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100372 struct sockaddr_storage from, to;
373 socklen_t fromlen, tolen;
374
375 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000376 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100377 return 1;
378 fromlen = sizeof(from);
379 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000380 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000381 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100382 return 0;
383 tolen = sizeof(to);
384 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000385 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000386 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100387 return 0;
388 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
389 return 0;
390 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
391 return 0;
392 return 1;
393}
394
Ben Lindstromf6027d32002-03-22 01:42:04 +0000395void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000396ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000397{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000398 if (ibytes)
399 *ibytes = ssh->state->p_read.bytes;
400 if (obytes)
401 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000402}
403
404int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000405ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100406{
407 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100408 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100409
410 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000411 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000412 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100413 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000414#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100415 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000416 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000417 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000418#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000419 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100420}
421
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422/* Sets the connection into non-blocking mode. */
423
424void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000425ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426{
Damien Miller95def091999-11-25 00:26:21 +1100427 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000428 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429
markus@openbsd.org091c3022015-01-19 19:52:16 +0000430 if (ssh->state->connection_out != ssh->state->connection_in)
431 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
434/* Returns the socket used for reading. */
435
436int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000437ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000439 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440}
441
442/* Returns the descriptor used for writing. */
443
444int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000445ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000447 return ssh->state->connection_out;
448}
449
450/*
451 * Returns the IP-address of the remote host as a string. The returned
452 * string must not be freed.
453 */
454
455const char *
456ssh_remote_ipaddr(struct ssh *ssh)
457{
458 /* Check whether we have cached the ipaddr. */
459 if (ssh->remote_ipaddr == NULL)
460 ssh->remote_ipaddr = ssh_packet_connection_is_on_socket(ssh) ?
461 get_peer_ipaddr(ssh->state->connection_in) :
462 strdup("UNKNOWN");
463 if (ssh->remote_ipaddr == NULL)
464 return "UNKNOWN";
465 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466}
467
468/* Closes the connection and clears and frees internal data structures. */
469
470void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000471ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000473 struct session_state *state = ssh->state;
474 int r;
475 u_int mode;
476
477 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100478 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000479 state->initialized = 0;
480 if (state->connection_in == state->connection_out) {
481 shutdown(state->connection_out, SHUT_RDWR);
482 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100483 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000484 close(state->connection_in);
485 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100486 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000487 sshbuf_free(state->input);
488 sshbuf_free(state->output);
489 sshbuf_free(state->outgoing_packet);
490 sshbuf_free(state->incoming_packet);
491 for (mode = 0; mode < MODE_MAX; mode++)
492 kex_free_newkeys(state->newkeys[mode]);
493 if (state->compression_buffer) {
494 sshbuf_free(state->compression_buffer);
495 if (state->compression_out_started) {
496 z_streamp stream = &state->compression_out_stream;
497 debug("compress outgoing: "
498 "raw data %llu, compressed %llu, factor %.2f",
499 (unsigned long long)stream->total_in,
500 (unsigned long long)stream->total_out,
501 stream->total_in == 0 ? 0.0 :
502 (double) stream->total_out / stream->total_in);
503 if (state->compression_out_failures == 0)
504 deflateEnd(stream);
505 }
506 if (state->compression_in_started) {
507 z_streamp stream = &state->compression_out_stream;
508 debug("compress incoming: "
509 "raw data %llu, compressed %llu, factor %.2f",
510 (unsigned long long)stream->total_out,
511 (unsigned long long)stream->total_in,
512 stream->total_out == 0 ? 0.0 :
513 (double) stream->total_in / stream->total_out);
514 if (state->compression_in_failures == 0)
515 inflateEnd(stream);
516 }
Damien Miller95def091999-11-25 00:26:21 +1100517 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000518 if ((r = cipher_cleanup(&state->send_context)) != 0)
519 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
520 if ((r = cipher_cleanup(&state->receive_context)) != 0)
521 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
522 if (ssh->remote_ipaddr) {
523 free(ssh->remote_ipaddr);
524 ssh->remote_ipaddr = NULL;
525 }
526 free(ssh->state);
527 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528}
529
530/* Sets remote side protocol flags. */
531
532void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000533ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000535 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536}
537
538/* Returns the remote protocol flags set earlier by the above function. */
539
Ben Lindstrom46c16222000-12-22 01:43:59 +0000540u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000541ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000543 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544}
545
Damien Miller5428f641999-11-25 11:54:57 +1100546/*
547 * Starts packet compression from the next packet on in both directions.
548 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
549 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550
markus@openbsd.org091c3022015-01-19 19:52:16 +0000551static int
552ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000553{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000554 if (!ssh->state->compression_buffer &&
555 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
556 return SSH_ERR_ALLOC_FAIL;
557 return 0;
558}
559
560static int
561start_compression_out(struct ssh *ssh, int level)
562{
563 if (level < 1 || level > 9)
564 return SSH_ERR_INVALID_ARGUMENT;
565 debug("Enabling compression at level %d.", level);
566 if (ssh->state->compression_out_started == 1)
567 deflateEnd(&ssh->state->compression_out_stream);
568 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
569 case Z_OK:
570 ssh->state->compression_out_started = 1;
571 break;
572 case Z_MEM_ERROR:
573 return SSH_ERR_ALLOC_FAIL;
574 default:
575 return SSH_ERR_INTERNAL_ERROR;
576 }
577 return 0;
578}
579
580static int
581start_compression_in(struct ssh *ssh)
582{
583 if (ssh->state->compression_in_started == 1)
584 inflateEnd(&ssh->state->compression_in_stream);
585 switch (inflateInit(&ssh->state->compression_in_stream)) {
586 case Z_OK:
587 ssh->state->compression_in_started = 1;
588 break;
589 case Z_MEM_ERROR:
590 return SSH_ERR_ALLOC_FAIL;
591 default:
592 return SSH_ERR_INTERNAL_ERROR;
593 }
594 return 0;
595}
596
597int
598ssh_packet_start_compression(struct ssh *ssh, int level)
599{
600 int r;
601
602 if (ssh->state->packet_compression && !compat20)
603 return SSH_ERR_INTERNAL_ERROR;
604 ssh->state->packet_compression = 1;
605 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
606 (r = start_compression_in(ssh)) != 0 ||
607 (r = start_compression_out(ssh, level)) != 0)
608 return r;
609 return 0;
610}
611
612/* XXX remove need for separate compression buffer */
613static int
614compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
615{
616 u_char buf[4096];
617 int r, status;
618
619 if (ssh->state->compression_out_started != 1)
620 return SSH_ERR_INTERNAL_ERROR;
621
622 /* This case is not handled below. */
623 if (sshbuf_len(in) == 0)
624 return 0;
625
626 /* Input is the contents of the input buffer. */
627 if ((ssh->state->compression_out_stream.next_in =
628 sshbuf_mutable_ptr(in)) == NULL)
629 return SSH_ERR_INTERNAL_ERROR;
630 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
631
632 /* Loop compressing until deflate() returns with avail_out != 0. */
633 do {
634 /* Set up fixed-size output buffer. */
635 ssh->state->compression_out_stream.next_out = buf;
636 ssh->state->compression_out_stream.avail_out = sizeof(buf);
637
638 /* Compress as much data into the buffer as possible. */
639 status = deflate(&ssh->state->compression_out_stream,
640 Z_PARTIAL_FLUSH);
641 switch (status) {
642 case Z_MEM_ERROR:
643 return SSH_ERR_ALLOC_FAIL;
644 case Z_OK:
645 /* Append compressed data to output_buffer. */
646 if ((r = sshbuf_put(out, buf, sizeof(buf) -
647 ssh->state->compression_out_stream.avail_out)) != 0)
648 return r;
649 break;
650 case Z_STREAM_ERROR:
651 default:
652 ssh->state->compression_out_failures++;
653 return SSH_ERR_INVALID_FORMAT;
654 }
655 } while (ssh->state->compression_out_stream.avail_out == 0);
656 return 0;
657}
658
659static int
660uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
661{
662 u_char buf[4096];
663 int r, status;
664
665 if (ssh->state->compression_in_started != 1)
666 return SSH_ERR_INTERNAL_ERROR;
667
668 if ((ssh->state->compression_in_stream.next_in =
669 sshbuf_mutable_ptr(in)) == NULL)
670 return SSH_ERR_INTERNAL_ERROR;
671 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
672
673 for (;;) {
674 /* Set up fixed-size output buffer. */
675 ssh->state->compression_in_stream.next_out = buf;
676 ssh->state->compression_in_stream.avail_out = sizeof(buf);
677
678 status = inflate(&ssh->state->compression_in_stream,
679 Z_PARTIAL_FLUSH);
680 switch (status) {
681 case Z_OK:
682 if ((r = sshbuf_put(out, buf, sizeof(buf) -
683 ssh->state->compression_in_stream.avail_out)) != 0)
684 return r;
685 break;
686 case Z_BUF_ERROR:
687 /*
688 * Comments in zlib.h say that we should keep calling
689 * inflate() until we get an error. This appears to
690 * be the error that we get.
691 */
692 return 0;
693 case Z_DATA_ERROR:
694 return SSH_ERR_INVALID_FORMAT;
695 case Z_MEM_ERROR:
696 return SSH_ERR_ALLOC_FAIL;
697 case Z_STREAM_ERROR:
698 default:
699 ssh->state->compression_in_failures++;
700 return SSH_ERR_INTERNAL_ERROR;
701 }
702 }
703 /* NOTREACHED */
704}
705
706/* Serialise compression state into a blob for privsep */
707static int
708ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
709{
710 struct session_state *state = ssh->state;
711 struct sshbuf *b;
712 int r;
713
714 if ((b = sshbuf_new()) == NULL)
715 return SSH_ERR_ALLOC_FAIL;
716 if (state->compression_in_started) {
717 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
718 sizeof(state->compression_in_stream))) != 0)
719 goto out;
720 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
721 goto out;
722 if (state->compression_out_started) {
723 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
724 sizeof(state->compression_out_stream))) != 0)
725 goto out;
726 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
727 goto out;
728 r = sshbuf_put_stringb(m, b);
729 out:
730 sshbuf_free(b);
731 return r;
732}
733
734/* Deserialise compression state from a blob for privsep */
735static int
736ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
737{
738 struct session_state *state = ssh->state;
739 struct sshbuf *b = NULL;
740 int r;
741 const u_char *inblob, *outblob;
742 size_t inl, outl;
743
744 if ((r = sshbuf_froms(m, &b)) != 0)
745 goto out;
746 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
747 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
748 goto out;
749 if (inl == 0)
750 state->compression_in_started = 0;
751 else if (inl != sizeof(state->compression_in_stream)) {
752 r = SSH_ERR_INTERNAL_ERROR;
753 goto out;
754 } else {
755 state->compression_in_started = 1;
756 memcpy(&state->compression_in_stream, inblob, inl);
757 }
758 if (outl == 0)
759 state->compression_out_started = 0;
760 else if (outl != sizeof(state->compression_out_stream)) {
761 r = SSH_ERR_INTERNAL_ERROR;
762 goto out;
763 } else {
764 state->compression_out_started = 1;
765 memcpy(&state->compression_out_stream, outblob, outl);
766 }
767 r = 0;
768 out:
769 sshbuf_free(b);
770 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000771}
772
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000773void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000774ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
775 void *(*allocfunc)(void *, u_int, u_int),
776 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000778 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
779 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
780 ssh->state->compression_out_stream.opaque = ctx;
781 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
782 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
783 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784}
785
Damien Miller5428f641999-11-25 11:54:57 +1100786/*
Damien Miller5428f641999-11-25 11:54:57 +1100787 * Causes any further packets to be encrypted using the given key. The same
788 * key is used for both sending and reception. However, both directions are
789 * encrypted independently of each other.
790 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000791
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000793ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000795#ifndef WITH_SSH1
796 fatal("no SSH protocol 1 support");
797#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000798 struct session_state *state = ssh->state;
799 const struct sshcipher *cipher = cipher_by_number(number);
800 int r;
801 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100802
markus@openbsd.org091c3022015-01-19 19:52:16 +0000803 if (cipher == NULL)
804 fatal("%s: unknown cipher number %d", __func__, number);
805 if (keylen < 20)
806 fatal("%s: keylen too small: %d", __func__, keylen);
807 if (keylen > SSH_SESSION_KEY_LENGTH)
808 fatal("%s: keylen too big: %d", __func__, keylen);
809 memcpy(state->ssh1_key, key, keylen);
810 state->ssh1_keylen = keylen;
811 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
812 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
813 (r = cipher_init(&state->receive_context, cipher, key, keylen,
814 NULL, 0, CIPHER_DECRYPT) != 0))
815 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
816 if (!state->cipher_warning_done &&
817 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
818 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
819 error("Warning: %s", wmsg);
820 state->cipher_warning_done = 1;
821 }
Damien Miller773dda22015-01-30 23:10:17 +1100822#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000823}
824
Damien Miller5428f641999-11-25 11:54:57 +1100825/*
826 * Finalizes and sends the packet. If the encryption key has been set,
827 * encrypts the packet before sending.
828 */
Damien Miller95def091999-11-25 00:26:21 +1100829
markus@openbsd.org091c3022015-01-19 19:52:16 +0000830int
831ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000833 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000834 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000835 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000836 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000837
Damien Miller5428f641999-11-25 11:54:57 +1100838 /*
839 * If using packet compression, compress the payload of the outgoing
840 * packet.
841 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000842 if (state->packet_compression) {
843 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100844 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000845 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
846 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100847 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000848 if ((r = sshbuf_put(state->compression_buffer,
849 "\0\0\0\0\0\0\0\0", 8)) != 0)
850 goto out;
851 if ((r = compress_buffer(ssh, state->outgoing_packet,
852 state->compression_buffer)) != 0)
853 goto out;
854 sshbuf_reset(state->outgoing_packet);
855 if ((r = sshbuf_putb(state->outgoing_packet,
856 state->compression_buffer)) != 0)
857 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100858 }
859 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000860 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861
Damien Millere247cc42000-05-07 12:03:14 +1000862 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100863 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000864 if (!state->send_context.plaintext) {
865 cp = sshbuf_mutable_ptr(state->outgoing_packet);
866 if (cp == NULL) {
867 r = SSH_ERR_INTERNAL_ERROR;
868 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100869 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000870 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100871 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000872 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
873 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100874
875 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000876 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
877 sshbuf_len(state->outgoing_packet));
878 POKE_U32(buf, checksum);
879 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
880 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881
882#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100883 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000884 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885#endif
886
Damien Miller95def091999-11-25 00:26:21 +1100887 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000888 POKE_U32(buf, len);
889 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
890 goto out;
891 if ((r = sshbuf_reserve(state->output,
892 sshbuf_len(state->outgoing_packet), &cp)) != 0)
893 goto out;
894 if ((r = cipher_crypt(&state->send_context, 0, cp,
895 sshbuf_ptr(state->outgoing_packet),
896 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
897 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100898
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100900 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000901 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000903 state->p_send.packets++;
904 state->p_send.bytes += len +
905 sshbuf_len(state->outgoing_packet);
906 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907
Damien Miller5428f641999-11-25 11:54:57 +1100908 /*
Damien Miller788f2122005-11-05 15:14:59 +1100909 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000910 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
911 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100912 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000913 r = 0;
914 out:
915 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916}
917
markus@openbsd.org091c3022015-01-19 19:52:16 +0000918int
919ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000920{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000921 struct session_state *state = ssh->state;
922 struct sshenc *enc;
923 struct sshmac *mac;
924 struct sshcomp *comp;
925 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000926 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000927 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000928 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000929
Ben Lindstrom064496f2002-12-23 02:04:22 +0000930 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000931
Damien Miller963f6b22002-02-19 15:21:23 +1100932 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000933 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000934 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000935 state->p_send.packets = state->p_send.blocks = 0;
936 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100937 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000938 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000939 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000940 state->p_read.packets = state->p_read.blocks = 0;
941 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100942 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000943 if (state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000944 debug("set_newkeys: rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000945 if ((r = cipher_cleanup(cc)) != 0)
946 return r;
947 enc = &state->newkeys[mode]->enc;
948 mac = &state->newkeys[mode]->mac;
949 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000950 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100951 explicit_bzero(enc->iv, enc->iv_len);
952 explicit_bzero(enc->key, enc->key_len);
953 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000954 free(enc->name);
955 free(enc->iv);
956 free(enc->key);
957 free(mac->name);
958 free(mac->key);
959 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000960 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000961 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000962 /* move newkeys from kex to state */
963 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
964 return SSH_ERR_INTERNAL_ERROR;
965 ssh->kex->newkeys[mode] = NULL;
966 enc = &state->newkeys[mode]->enc;
967 mac = &state->newkeys[mode]->mac;
968 comp = &state->newkeys[mode]->comp;
969 if (cipher_authlen(enc->cipher) == 0) {
970 if ((r = mac_init(mac)) != 0)
971 return r;
972 }
973 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000974 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000975 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
976 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000977 return r;
978 if (!state->cipher_warning_done &&
979 (wmsg = cipher_warning_message(cc)) != NULL) {
980 error("Warning: %s", wmsg);
981 state->cipher_warning_done = 1;
982 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000983 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100984 /* explicit_bzero(enc->iv, enc->block_size);
985 explicit_bzero(enc->key, enc->key_len);
986 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000987 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000988 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +0000989 state->after_authentication)) && comp->enabled == 0) {
990 if ((r = ssh_packet_init_compression(ssh)) < 0)
991 return r;
992 if (mode == MODE_OUT) {
993 if ((r = start_compression_out(ssh, 6)) != 0)
994 return r;
995 } else {
996 if ((r = start_compression_in(ssh)) != 0)
997 return r;
998 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000999 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001000 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001001 /*
1002 * The 2^(blocksize*2) limit is too expensive for 3DES,
1003 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1004 */
1005 if (enc->block_size >= 16)
1006 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1007 else
1008 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001009 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001010 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001011 state->rekey_limit / enc->block_size);
1012 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001013}
1014
Damien Miller5428f641999-11-25 11:54:57 +11001015/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001016 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001017 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001018 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1019 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001020static int
1021ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001022{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001023 struct session_state *state = ssh->state;
1024 struct sshcomp *comp = NULL;
1025 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001026
1027 /*
1028 * Remember that we are past the authentication step, so rekeying
1029 * with COMP_DELAYED will turn on compression immediately.
1030 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001031 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001032 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001033 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001034 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001035 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001036 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001037 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001038 if ((r = ssh_packet_init_compression(ssh)) != 0)
1039 return r;
1040 if (mode == MODE_OUT) {
1041 if ((r = start_compression_out(ssh, 6)) != 0)
1042 return r;
1043 } else {
1044 if ((r = start_compression_in(ssh)) != 0)
1045 return r;
1046 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001047 comp->enabled = 1;
1048 }
1049 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001050 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001051}
1052
1053/*
Damien Miller33b13562000-04-04 14:38:59 +10001054 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1055 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001056int
1057ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001058{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001059 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001060 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001061 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001062 u_int authlen = 0, aadlen = 0;
1063 u_int len;
1064 struct sshenc *enc = NULL;
1065 struct sshmac *mac = NULL;
1066 struct sshcomp *comp = NULL;
1067 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001068
markus@openbsd.org091c3022015-01-19 19:52:16 +00001069 if (state->newkeys[MODE_OUT] != NULL) {
1070 enc = &state->newkeys[MODE_OUT]->enc;
1071 mac = &state->newkeys[MODE_OUT]->mac;
1072 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001073 /* disable mac for authenticated encryption */
1074 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1075 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001076 }
Damien Miller963f6b22002-02-19 15:21:23 +11001077 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001078 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001079
markus@openbsd.org091c3022015-01-19 19:52:16 +00001080 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001081
1082#ifdef PACKET_DEBUG
1083 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001084 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001085#endif
1086
1087 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001088 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001089 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001090 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1091 goto out;
1092 sshbuf_reset(state->compression_buffer);
1093 if ((r = compress_buffer(ssh, state->outgoing_packet,
1094 state->compression_buffer)) != 0)
1095 goto out;
1096 sshbuf_reset(state->outgoing_packet);
1097 if ((r = sshbuf_put(state->outgoing_packet,
1098 "\0\0\0\0\0", 5)) != 0 ||
1099 (r = sshbuf_putb(state->outgoing_packet,
1100 state->compression_buffer)) != 0)
1101 goto out;
1102 DBG(debug("compression: raw %d compressed %zd", len,
1103 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001104 }
1105
1106 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001107 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001108
1109 /*
1110 * calc size of padding, alloc space, get random data,
1111 * minimum padding is 4 bytes
1112 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001113 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001114 padlen = block_size - (len % block_size);
1115 if (padlen < 4)
1116 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001117 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001118 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001119 state->extra_pad =
1120 roundup(state->extra_pad, block_size);
1121 pad = state->extra_pad -
1122 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001123 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001124 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001125 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001126 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001127 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001128 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1129 goto out;
1130 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001131 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001132 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001133 } else {
1134 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001135 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001136 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001137 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001138 len = sshbuf_len(state->outgoing_packet);
1139 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1140 if (cp == NULL) {
1141 r = SSH_ERR_INTERNAL_ERROR;
1142 goto out;
1143 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001144 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001145 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001146 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001147 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1148 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001149
1150 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001151 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001152 if ((r = mac_compute(mac, state->p_send.seqnr,
1153 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001154 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001155 goto out;
1156 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001157 }
1158 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001159 if ((r = sshbuf_reserve(state->output,
1160 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1161 goto out;
1162 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1163 sshbuf_ptr(state->outgoing_packet),
1164 len - aadlen, aadlen, authlen)) != 0)
1165 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001166 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001167 if (mac && mac->enabled) {
1168 if (mac->etm) {
1169 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001170 if ((r = mac_compute(mac, state->p_send.seqnr,
1171 cp, len, macbuf, sizeof(macbuf))) != 0)
1172 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001173 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001174 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001175 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001176 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1177 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001178 }
Damien Miller33b13562000-04-04 14:38:59 +10001179#ifdef PACKET_DEBUG
1180 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001181 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001182#endif
Damien Miller4af51302000-04-16 11:18:38 +10001183 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001184 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001185 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001186 if (++state->p_send.packets == 0)
1187 if (!(ssh->compat & SSH_BUG_NOREKEY))
1188 return SSH_ERR_NEED_REKEY;
1189 state->p_send.blocks += len / block_size;
1190 state->p_send.bytes += len;
1191 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001192
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001193 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001194 r = ssh_set_newkeys(ssh, MODE_OUT);
1195 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1196 r = ssh_packet_enable_delayed_compress(ssh);
1197 else
1198 r = 0;
1199 out:
1200 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001201}
1202
markus@openbsd.org091c3022015-01-19 19:52:16 +00001203int
1204ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001205{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001206 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001207 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001208 u_char type;
1209 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001210
markus@openbsd.org091c3022015-01-19 19:52:16 +00001211 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001212
1213 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001214 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001215 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1216 (type > SSH2_MSG_TRANSPORT_MAX) ||
1217 (type == SSH2_MSG_SERVICE_REQUEST) ||
1218 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +10001219 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001220 p = calloc(1, sizeof(*p));
1221 if (p == NULL)
1222 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001223 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001224 p->payload = state->outgoing_packet;
1225 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1226 state->outgoing_packet = sshbuf_new();
1227 if (state->outgoing_packet == NULL)
1228 return SSH_ERR_ALLOC_FAIL;
1229 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001230 }
1231 }
1232
1233 /* rekeying starts with sending KEXINIT */
1234 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001235 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001236
markus@openbsd.org091c3022015-01-19 19:52:16 +00001237 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1238 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001239
1240 /* after a NEWKEYS message we can send the complete queue */
1241 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001242 state->rekeying = 0;
1243 state->rekey_time = monotime();
1244 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001245 type = p->type;
1246 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001247 sshbuf_free(state->outgoing_packet);
1248 state->outgoing_packet = p->payload;
1249 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001250 free(p);
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 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001255 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001256}
1257
Damien Miller33b13562000-04-04 14:38:59 +10001258/*
Damien Miller5428f641999-11-25 11:54:57 +11001259 * Waits until a packet has been received, and returns its type. Note that
1260 * no other data is processed until this returns, so this function should not
1261 * be used during the interactive session.
1262 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001263
1264int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001265ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001267 struct session_state *state = ssh->state;
1268 int len, r, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001269 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001270 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001271 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272
Darren Tucker99bb7612008-06-13 22:02:50 +10001273 DBG(debug("packet_read()"));
1274
markus@openbsd.org091c3022015-01-19 19:52:16 +00001275 setp = (fd_set *)calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001276 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001277 if (setp == NULL)
1278 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001279
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001280 /*
1281 * Since we are blocking, ensure that all written packets have
1282 * been sent.
1283 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001284 if ((r = ssh_packet_write_wait(ssh)) != 0)
1285 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001286
Damien Miller95def091999-11-25 00:26:21 +11001287 /* Stay in the loop until we have received a complete packet. */
1288 for (;;) {
1289 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001290 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1291 if (r != 0)
1292 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001293 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001294 *typep == SSH_SMSG_SUCCESS
1295 || *typep == SSH_SMSG_FAILURE
1296 || *typep == SSH_CMSG_EOF
1297 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1298 if ((r = sshpkt_get_end(ssh)) != 0)
1299 break;
Damien Miller95def091999-11-25 00:26:21 +11001300 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001301 if (*typep != SSH_MSG_NONE)
1302 break;
Damien Miller5428f641999-11-25 11:54:57 +11001303 /*
1304 * Otherwise, wait for some data to arrive, add it to the
1305 * buffer, and try again.
1306 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001307 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001308 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001309 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001310
markus@openbsd.org091c3022015-01-19 19:52:16 +00001311 if (state->packet_timeout_ms > 0) {
1312 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001313 timeoutp = &timeout;
1314 }
Damien Miller95def091999-11-25 00:26:21 +11001315 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001316 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001317 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001318 ms_to_timeval(&timeout, ms_remain);
1319 gettimeofday(&start, NULL);
1320 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001321 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001322 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001323 break;
Damien Millerea437422009-10-02 11:49:03 +10001324 if (errno != EAGAIN && errno != EINTR &&
1325 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001326 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001327 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001328 continue;
1329 ms_subtract_diff(&start, &ms_remain);
1330 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001331 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001332 break;
1333 }
1334 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001335 if (r == 0)
1336 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001337 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001338 do {
1339 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001340 len = roaming_read(state->connection_in, buf,
Darren Tuckerc5564e12009-06-21 18:53:53 +10001341 sizeof(buf), &cont);
1342 } while (len == 0 && cont);
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001343 if (len == 0) {
1344 r = SSH_ERR_CONN_CLOSED;
1345 goto out;
1346 }
1347 if (len < 0) {
1348 r = SSH_ERR_SYSTEM_ERROR;
1349 goto out;
1350 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001351
Damien Miller95def091999-11-25 00:26:21 +11001352 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001353 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001354 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001355 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001356 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001357 free(setp);
1358 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359}
1360
Damien Miller278f9072001-12-21 15:00:19 +11001361int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001362ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001363{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001364 u_char type;
1365 int r;
1366
1367 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1368 fatal("%s: %s", __func__, ssh_err(r));
1369 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001370}
1371
Damien Miller5428f641999-11-25 11:54:57 +11001372/*
1373 * Waits until a packet has been received, verifies that its type matches
1374 * that given, and gives a fatal error and exits if there is a mismatch.
1375 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001376
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001377int
1378ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001380 int r;
1381 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001382
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001383 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1384 return r;
1385 if (type != expected_type) {
1386 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001387 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001388 expected_type, type)) != 0)
1389 return r;
1390 return SSH_ERR_PROTOCOL_ERROR;
1391 }
1392 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393}
1394
1395/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001396 * packet_process_incoming. If so, reads the packet; otherwise returns
1397 * SSH_MSG_NONE. This does not wait for data from the connection.
1398 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001399 * SSH_MSG_DISCONNECT is handled specially here. Also,
1400 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1401 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001402 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
markus@openbsd.org091c3022015-01-19 19:52:16 +00001404int
1405ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001407 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001408 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001409 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001410 const u_char *cp;
1411 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001412 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001413 int r;
1414
1415 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416
Damien Miller95def091999-11-25 00:26:21 +11001417 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001418 if (sshbuf_len(state->input) < 4 + 8)
1419 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001420 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001421 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001422 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1423 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1424 len)) != 0)
1425 return r;
1426 return SSH_ERR_CONN_CORRUPT;
1427 }
Damien Miller95def091999-11-25 00:26:21 +11001428 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429
Damien Miller95def091999-11-25 00:26:21 +11001430 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001431 if (sshbuf_len(state->input) < 4 + padded_len)
1432 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433
Damien Miller95def091999-11-25 00:26:21 +11001434 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435
Damien Miller95def091999-11-25 00:26:21 +11001436 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001437 if ((r = sshbuf_consume(state->input, 4)) != 0)
1438 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001439
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001440 /*
1441 * Cryptographic attack detector for ssh
1442 * (C)1998 CORE-SDI, Buenos Aires Argentina
1443 * Ariel Futoransky(futo@core-sdi.com)
1444 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001445 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001446 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001447 switch (detect_attack(&state->deattack,
1448 sshbuf_ptr(state->input), padded_len)) {
1449 case DEATTACK_OK:
1450 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001451 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001452 emsg = "crc32 compensation attack detected";
1453 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001454 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001455 emsg = "deattack denial of service detected";
1456 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001457 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001458 emsg = "deattack error";
1459 break;
1460 }
1461 if (emsg != NULL) {
1462 error("%s", emsg);
1463 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1464 (r = ssh_packet_write_wait(ssh)) != 0)
1465 return r;
1466 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001467 }
1468 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001469
1470 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001471 sshbuf_reset(state->incoming_packet);
1472 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1473 goto out;
1474 if ((r = cipher_crypt(&state->receive_context, 0, p,
1475 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1476 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001477
markus@openbsd.org091c3022015-01-19 19:52:16 +00001478 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1479 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480
1481#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001482 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001483 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001484#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485
Damien Miller95def091999-11-25 00:26:21 +11001486 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001487 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1488 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001489
Damien Miller95def091999-11-25 00:26:21 +11001490 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001491 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1492 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001493
Damien Miller95def091999-11-25 00:26:21 +11001494 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001495 if (len != sshbuf_len(state->incoming_packet)) {
1496 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001497 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001498 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1499 (r = ssh_packet_write_wait(ssh)) != 0)
1500 return r;
1501 return SSH_ERR_CONN_CORRUPT;
1502 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001503
markus@openbsd.org091c3022015-01-19 19:52:16 +00001504 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1505 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001506 if (checksum != stored_checksum) {
1507 error("Corrupted check bytes on input");
1508 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1509 (r = ssh_packet_write_wait(ssh)) != 0)
1510 return r;
1511 return SSH_ERR_CONN_CORRUPT;
1512 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001513 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1514 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515
markus@openbsd.org091c3022015-01-19 19:52:16 +00001516 if (state->packet_compression) {
1517 sshbuf_reset(state->compression_buffer);
1518 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1519 state->compression_buffer)) != 0)
1520 goto out;
1521 sshbuf_reset(state->incoming_packet);
1522 if ((r = sshbuf_putb(state->incoming_packet,
1523 state->compression_buffer)) != 0)
1524 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001525 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001526 state->p_read.packets++;
1527 state->p_read.bytes += padded_len + 4;
1528 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1529 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001530 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1531 error("Invalid ssh1 packet type: %d", *typep);
1532 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1533 (r = ssh_packet_write_wait(ssh)) != 0)
1534 return r;
1535 return SSH_ERR_PROTOCOL_ERROR;
1536 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001537 r = 0;
1538 out:
1539 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001540}
Damien Miller95def091999-11-25 00:26:21 +11001541
markus@openbsd.org091c3022015-01-19 19:52:16 +00001542int
1543ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001544{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001545 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001546 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001547 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1548 u_int maclen, aadlen = 0, authlen = 0, block_size;
1549 struct sshenc *enc = NULL;
1550 struct sshmac *mac = NULL;
1551 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001552 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001553
markus@openbsd.org091c3022015-01-19 19:52:16 +00001554 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001555
markus@openbsd.org091c3022015-01-19 19:52:16 +00001556 if (state->packet_discard)
1557 return 0;
1558
1559 if (state->newkeys[MODE_IN] != NULL) {
1560 enc = &state->newkeys[MODE_IN]->enc;
1561 mac = &state->newkeys[MODE_IN]->mac;
1562 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001563 /* disable mac for authenticated encryption */
1564 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1565 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001566 }
1567 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001568 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001569 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001570
markus@openbsd.org091c3022015-01-19 19:52:16 +00001571 if (aadlen && state->packlen == 0) {
1572 if (cipher_get_length(&state->receive_context,
1573 &state->packlen, state->p_read.seqnr,
1574 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1575 return 0;
1576 if (state->packlen < 1 + 4 ||
1577 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001578#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001579 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001580#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001581 logit("Bad packet length %u.", state->packlen);
1582 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1583 return r;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001584 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001585 sshbuf_reset(state->incoming_packet);
1586 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001587 /*
1588 * check if input size is less than the cipher block size,
1589 * decrypt first block and extract length of incoming packet
1590 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001591 if (sshbuf_len(state->input) < block_size)
1592 return 0;
1593 sshbuf_reset(state->incoming_packet);
1594 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1595 &cp)) != 0)
1596 goto out;
1597 if ((r = cipher_crypt(&state->receive_context,
1598 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1599 block_size, 0, 0)) != 0)
1600 goto out;
1601 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1602 if (state->packlen < 1 + 4 ||
1603 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001604#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001605 fprintf(stderr, "input: \n");
1606 sshbuf_dump(state->input, stderr);
1607 fprintf(stderr, "incoming_packet: \n");
1608 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001609#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001610 logit("Bad packet length %u.", state->packlen);
1611 return ssh_packet_start_discard(ssh, enc, mac,
1612 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001613 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001614 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1615 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001616 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001617 DBG(debug("input: packet len %u", state->packlen+4));
1618
Damien Milleraf43a7a2012-12-12 10:46:31 +11001619 if (aadlen) {
1620 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001621 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001622 } else {
1623 /*
1624 * the payload size and the payload are encrypted, but we
1625 * have a partial packet of block_size bytes
1626 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001627 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001628 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001629 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1630 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001631 if (need % block_size != 0) {
1632 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001633 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001634 return ssh_packet_start_discard(ssh, enc, mac,
1635 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001636 }
Damien Miller33b13562000-04-04 14:38:59 +10001637 /*
1638 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001639 * decrypt into incoming_packet:
1640 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001641 * 'need' bytes are encrypted, followed by either
1642 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001643 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001644 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001645 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1646 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001647#ifdef PACKET_DEBUG
1648 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001649 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001650#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001651 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001652 if (mac && mac->enabled && mac->etm) {
1653 if ((r = mac_compute(mac, state->p_read.seqnr,
1654 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001655 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001656 goto out;
1657 }
1658 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1659 &cp)) != 0)
1660 goto out;
1661 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1662 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1663 goto out;
1664 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1665 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001666 /*
1667 * compute MAC over seqnr and packet,
1668 * increment sequence number for incoming packet
1669 */
Damien Miller4af51302000-04-16 11:18:38 +10001670 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001671 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001672 if ((r = mac_compute(mac, state->p_read.seqnr,
1673 sshbuf_ptr(state->incoming_packet),
1674 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001675 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001676 goto out;
1677 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001678 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001679 logit("Corrupted MAC on input.");
1680 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001681 return SSH_ERR_INTERNAL_ERROR;
1682 return ssh_packet_start_discard(ssh, enc, mac,
1683 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001684 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001685
1686 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1687 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1688 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001689 }
Damien Miller278f9072001-12-21 15:00:19 +11001690 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001691 *seqnr_p = state->p_read.seqnr;
1692 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001693 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001694 if (++state->p_read.packets == 0)
1695 if (!(ssh->compat & SSH_BUG_NOREKEY))
1696 return SSH_ERR_NEED_REKEY;
1697 state->p_read.blocks += (state->packlen + 4) / block_size;
1698 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001699
1700 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001701 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001702 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001703 if (padlen < 4) {
1704 if ((r = sshpkt_disconnect(ssh,
1705 "Corrupted padlen %d on input.", padlen)) != 0 ||
1706 (r = ssh_packet_write_wait(ssh)) != 0)
1707 return r;
1708 return SSH_ERR_CONN_CORRUPT;
1709 }
Damien Miller33b13562000-04-04 14:38:59 +10001710
1711 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001712 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1713 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1714 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001715
markus@openbsd.org091c3022015-01-19 19:52:16 +00001716 DBG(debug("input: len before de-compress %zd",
1717 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001718 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001719 sshbuf_reset(state->compression_buffer);
1720 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1721 state->compression_buffer)) != 0)
1722 goto out;
1723 sshbuf_reset(state->incoming_packet);
1724 if ((r = sshbuf_putb(state->incoming_packet,
1725 state->compression_buffer)) != 0)
1726 goto out;
1727 DBG(debug("input: len after de-compress %zd",
1728 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001729 }
1730 /*
1731 * get packet type, implies consume.
1732 * return length of payload (without type field)
1733 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001734 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1735 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001736 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1737 if ((r = sshpkt_disconnect(ssh,
1738 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1739 (r = ssh_packet_write_wait(ssh)) != 0)
1740 return r;
1741 return SSH_ERR_PROTOCOL_ERROR;
1742 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001743 if (*typep == SSH2_MSG_NEWKEYS)
1744 r = ssh_set_newkeys(ssh, MODE_IN);
1745 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1746 r = ssh_packet_enable_delayed_compress(ssh);
1747 else
1748 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001749#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001750 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1751 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001752#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001753 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001754 state->packlen = 0;
1755 out:
1756 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001757}
1758
1759int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001760ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001761{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001762 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001763 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001764 int r;
1765 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001766
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001767 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001768 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001769 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001770 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1771 if (r != 0)
1772 return r;
1773 if (*typep) {
1774 state->keep_alive_timeouts = 0;
1775 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001776 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001777 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001778 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001779 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001780 break;
Damien Miller33b13562000-04-04 14:38:59 +10001781 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001782 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1783 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1784 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1785 if (msg)
1786 free(msg);
1787 return r;
1788 }
Damien Miller33b13562000-04-04 14:38:59 +10001789 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001790 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001791 break;
1792 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001793 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1794 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1795 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001796 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001797 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001798 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1799 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1800 "Received disconnect from %s: %u: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001801 ssh_remote_ipaddr(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001802 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001803 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001804 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001805 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1806 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001807 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1808 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001809 break;
Damien Miller33b13562000-04-04 14:38:59 +10001810 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001811 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001812 }
Damien Miller33b13562000-04-04 14:38:59 +10001813 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001814 r = ssh_packet_read_poll1(ssh, typep);
1815 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001816 case SSH_MSG_NONE:
1817 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001818 case SSH_MSG_IGNORE:
1819 break;
1820 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001821 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1822 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001823 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001824 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001825 break;
1826 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001827 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1828 return r;
Damien Miller894926e2013-02-12 11:03:58 +11001829 error("Received disconnect from %s: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001830 ssh_remote_ipaddr(ssh), msg);
1831 free(msg);
1832 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001833 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001834 DBG(debug("received packet type %d", *typep));
1835 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001836 }
Damien Miller33b13562000-04-04 14:38:59 +10001837 }
1838 }
1839}
1840
Damien Miller5428f641999-11-25 11:54:57 +11001841/*
1842 * Buffers the given amount of input characters. This is intended to be used
1843 * together with packet_read_poll.
1844 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001845
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001846int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001847ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001848{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001849 struct session_state *state = ssh->state;
1850 int r;
1851
1852 if (state->packet_discard) {
1853 state->keep_alive_timeouts = 0; /* ?? */
1854 if (len >= state->packet_discard) {
1855 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001856 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001857 }
1858 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001859 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001860 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001861 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001862 return r;
1863
1864 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001865}
1866
Damien Miller4af51302000-04-16 11:18:38 +10001867int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001868ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001869{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001870 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001871}
1872
Damien Miller5428f641999-11-25 11:54:57 +11001873/*
1874 * Sends a diagnostic message from the server to the client. This message
1875 * can be sent at any time (but not while constructing another message). The
1876 * message is printed immediately, but only if the client is being executed
1877 * in verbose mode. These messages are primarily intended to ease debugging
1878 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001879 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11001880 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001881void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001882ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001883{
Damien Miller95def091999-11-25 00:26:21 +11001884 char buf[1024];
1885 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001886 int r;
Damien Miller95def091999-11-25 00:26:21 +11001887
markus@openbsd.org091c3022015-01-19 19:52:16 +00001888 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001889 return;
1890
Damien Miller95def091999-11-25 00:26:21 +11001891 va_start(args, fmt);
1892 vsnprintf(buf, sizeof(buf), fmt, args);
1893 va_end(args);
1894
Damien Miller7c8af4f2000-05-01 08:24:07 +10001895 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001896 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1897 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1898 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1899 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1900 (r = sshpkt_send(ssh)) != 0)
1901 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001902 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001903 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1904 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1905 (r = sshpkt_send(ssh)) != 0)
1906 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001907 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001908 if ((r = ssh_packet_write_wait(ssh)) != 0)
1909 fatal("%s: %s", __func__, ssh_err(r));
1910}
1911
1912/*
1913 * Pretty-print connection-terminating errors and exit.
1914 */
1915void
1916sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
1917{
1918 switch (r) {
1919 case SSH_ERR_CONN_CLOSED:
1920 logit("Connection closed by %.200s", ssh_remote_ipaddr(ssh));
1921 cleanup_exit(255);
1922 case SSH_ERR_CONN_TIMEOUT:
1923 logit("Connection to %.200s timed out while "
1924 "waiting to write", ssh_remote_ipaddr(ssh));
1925 cleanup_exit(255);
1926 default:
1927 fatal("%s%sConnection to %.200s: %s",
1928 tag != NULL ? tag : "", tag != NULL ? ": " : "",
1929 ssh_remote_ipaddr(ssh), ssh_err(r));
1930 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001931}
1932
Damien Miller5428f641999-11-25 11:54:57 +11001933/*
1934 * Logs the error plus constructs and sends a disconnect packet, closes the
1935 * connection, and exits. This function never returns. The error message
1936 * should not contain a newline. The length of the formatted message must
1937 * not exceed 1024 bytes.
1938 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001939void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001940ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001941{
Damien Miller95def091999-11-25 00:26:21 +11001942 char buf[1024];
1943 va_list args;
1944 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001945 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001946
Damien Miller95def091999-11-25 00:26:21 +11001947 if (disconnecting) /* Guard against recursive invocations. */
1948 fatal("packet_disconnect called recursively.");
1949 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001950
Damien Miller5428f641999-11-25 11:54:57 +11001951 /*
1952 * Format the message. Note that the caller must make sure the
1953 * message is of limited size.
1954 */
Damien Miller95def091999-11-25 00:26:21 +11001955 va_start(args, fmt);
1956 vsnprintf(buf, sizeof(buf), fmt, args);
1957 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001958
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001959 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001960 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001961
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001962 /*
1963 * Send the disconnect message to the other side, and wait
1964 * for it to get sent.
1965 */
1966 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
1967 sshpkt_fatal(ssh, __func__, r);
1968
1969 if ((r = ssh_packet_write_wait(ssh)) != 0)
1970 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001971
Damien Miller95def091999-11-25 00:26:21 +11001972 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001973 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001974 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001975}
1976
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001977/*
1978 * Checks if there is any buffered output, and tries to write some of
1979 * the output.
1980 */
1981int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001982ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001983{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001984 struct session_state *state = ssh->state;
1985 int len = sshbuf_len(state->output);
1986 int cont, r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001987
Damien Miller95def091999-11-25 00:26:21 +11001988 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001989 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001990 len = roaming_write(state->connection_out,
1991 sshbuf_ptr(state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001992 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001993 if (errno == EINTR || errno == EAGAIN ||
1994 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001995 return 0;
1996 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11001997 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001998 if (len == 0 && !cont)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001999 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002000 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002001 return r;
Damien Miller95def091999-11-25 00:26:21 +11002002 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002003 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002004}
2005
Damien Miller5428f641999-11-25 11:54:57 +11002006/*
2007 * Calls packet_write_poll repeatedly until all pending output data has been
2008 * written.
2009 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002010int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002011ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002012{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002013 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002014 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002015 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002016 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002017
markus@openbsd.org091c3022015-01-19 19:52:16 +00002018 setp = (fd_set *)calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002019 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002020 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002021 return SSH_ERR_ALLOC_FAIL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002022 ssh_packet_write_poll(ssh);
2023 while (ssh_packet_have_data_to_write(ssh)) {
2024 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002025 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002026 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002027
markus@openbsd.org091c3022015-01-19 19:52:16 +00002028 if (state->packet_timeout_ms > 0) {
2029 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002030 timeoutp = &timeout;
2031 }
2032 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002033 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002034 ms_to_timeval(&timeout, ms_remain);
2035 gettimeofday(&start, NULL);
2036 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002037 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002038 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002039 break;
Damien Millerea437422009-10-02 11:49:03 +10002040 if (errno != EAGAIN && errno != EINTR &&
2041 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002042 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002043 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002044 continue;
2045 ms_subtract_diff(&start, &ms_remain);
2046 if (ms_remain <= 0) {
2047 ret = 0;
2048 break;
2049 }
2050 }
2051 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002052 free(setp);
2053 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002054 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002055 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2056 free(setp);
2057 return r;
2058 }
Damien Miller95def091999-11-25 00:26:21 +11002059 }
Darren Tuckera627d422013-06-02 07:31:17 +10002060 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002061 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002062}
2063
2064/* Returns true if there is buffered data to write to the connection. */
2065
2066int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002067ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002068{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002069 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002070}
2071
2072/* Returns true if there is not too much data to write to the connection. */
2073
2074int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002075ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002076{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002077 if (ssh->state->interactive_mode)
2078 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002079 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002080 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002081}
2082
markus@openbsd.org091c3022015-01-19 19:52:16 +00002083void
2084ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002085{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002086#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002087 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002088 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002089 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002090# ifdef IP_TOS
2091 case AF_INET:
2092 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002093 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002094 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2095 error("setsockopt IP_TOS %d: %.100s:",
2096 tos, strerror(errno));
2097 break;
2098# endif /* IP_TOS */
2099# ifdef IPV6_TCLASS
2100 case AF_INET6:
2101 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002102 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002103 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2104 error("setsockopt IPV6_TCLASS %d: %.100s:",
2105 tos, strerror(errno));
2106 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002107# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002108 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002109#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002110}
Ben Lindstroma7433982002-12-23 02:41:41 +00002111
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002112/* Informs that the current session is interactive. Sets IP flags for that. */
2113
2114void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002115ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002116{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002117 struct session_state *state = ssh->state;
2118
2119 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002120 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002121 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002122
Damien Miller95def091999-11-25 00:26:21 +11002123 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002124 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002125
Damien Miller34132e52000-01-14 15:45:46 +11002126 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002127 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002128 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002129 set_nodelay(state->connection_in);
2130 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2131 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002132}
2133
2134/* Returns true if the current connection is interactive. */
2135
2136int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002137ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002138{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002139 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002140}
Damien Miller6162d121999-11-21 13:23:52 +11002141
Darren Tucker1f8311c2004-05-13 16:39:33 +10002142int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002143ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002144{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002145 struct session_state *state = ssh->state;
2146
2147 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002148 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002149 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002150 return -1;
2151 }
2152 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002153 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002154 return -1;
2155 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002156 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002157 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002158 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002159 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002160}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002161
Darren Tuckerf7288d72009-06-21 18:12:20 +10002162int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002163ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002164{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002165 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002166}
2167
2168void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002169ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002170{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002171 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002172}
2173
2174u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002175ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002176{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002177 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002178}
2179
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002180/*
2181 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002182 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002183 * byte SSH_MSG_IGNORE
2184 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002185 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002186 * All implementations MUST understand (and ignore) this message at any
2187 * time (after receiving the protocol version). No implementation is
2188 * required to send them. This message can be used as an additional
2189 * protection measure against advanced traffic analysis techniques.
2190 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002191void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002192ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002193{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002194 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002195 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002196
markus@openbsd.org091c3022015-01-19 19:52:16 +00002197 if ((r = sshpkt_start(ssh, compat20 ?
2198 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2199 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2200 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002201 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002202 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002203 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002204 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2205 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002206 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002207 }
2208}
Damien Millera5539d22003-04-09 20:50:06 +10002209
Darren Tucker1f8311c2004-05-13 16:39:33 +10002210#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002211int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002212ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002213{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002214 struct session_state *state = ssh->state;
2215
2216 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002217 return 0;
2218 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002219 (state->p_send.packets > MAX_PACKETS) ||
2220 (state->p_read.packets > MAX_PACKETS) ||
2221 (state->max_blocks_out &&
2222 (state->p_send.blocks > state->max_blocks_out)) ||
2223 (state->max_blocks_in &&
2224 (state->p_read.blocks > state->max_blocks_in)) ||
2225 (state->rekey_interval != 0 && state->rekey_time +
2226 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002227}
2228
2229void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002230ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002231{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002232 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
2233 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002234 ssh->state->rekey_limit = bytes;
2235 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002236}
2237
2238time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002239ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002240{
2241 time_t seconds;
2242
markus@openbsd.org091c3022015-01-19 19:52:16 +00002243 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002244 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002245 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002246}
Damien Miller9786e6e2005-07-26 21:54:56 +10002247
2248void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002249ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002250{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002251 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002252}
2253
2254void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002255ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002256{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002257 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002258}
2259
2260void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002261ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002262{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002263 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002264}
2265
2266void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002267ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002268{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002269 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002270}
2271
markus@openbsd.org091c3022015-01-19 19:52:16 +00002272/* XXX TODO update roaming to new API (does not work anyway) */
Darren Tuckere841eb02009-07-06 07:11:13 +10002273/*
2274 * Save the state for the real connection, and use a separate state when
2275 * resuming a suspended connection.
2276 */
2277void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002278ssh_packet_backup_state(struct ssh *ssh,
2279 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002280{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002281 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002282
markus@openbsd.org091c3022015-01-19 19:52:16 +00002283 close(ssh->state->connection_in);
2284 ssh->state->connection_in = -1;
2285 close(ssh->state->connection_out);
2286 ssh->state->connection_out = -1;
Darren Tuckere841eb02009-07-06 07:11:13 +10002287 if (backup_state)
2288 tmp = backup_state;
2289 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002290 tmp = ssh_alloc_session_state();
2291 backup_state = ssh;
2292 ssh = tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002293}
2294
markus@openbsd.org091c3022015-01-19 19:52:16 +00002295/* XXX FIXME FIXME FIXME */
Darren Tuckere841eb02009-07-06 07:11:13 +10002296/*
2297 * Swap in the old state when resuming a connecion.
2298 */
2299void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002300ssh_packet_restore_state(struct ssh *ssh,
2301 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002302{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002303 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002304 u_int len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002305 int r;
Darren Tuckere841eb02009-07-06 07:11:13 +10002306
2307 tmp = backup_state;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002308 backup_state = ssh;
2309 ssh = tmp;
2310 ssh->state->connection_in = backup_state->state->connection_in;
2311 backup_state->state->connection_in = -1;
2312 ssh->state->connection_out = backup_state->state->connection_out;
2313 backup_state->state->connection_out = -1;
2314 len = sshbuf_len(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002315 if (len > 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002316 if ((r = sshbuf_putb(ssh->state->input,
2317 backup_state->state->input)) != 0)
2318 fatal("%s: %s", __func__, ssh_err(r));
2319 sshbuf_reset(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002320 add_recv_bytes(len);
2321 }
2322}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002323
2324/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002325static int
2326ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002327{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002328 struct sshcomp *comp;
2329 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002330
2331 debug("%s: called", __func__);
2332 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002333 ssh->state->after_authentication = 1;
2334 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002335 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002336 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002337 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002338 comp = &ssh->state->newkeys[mode]->comp;
2339 if (comp && comp->enabled &&
2340 (r = ssh_packet_init_compression(ssh)) != 0)
2341 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002342 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002343 return 0;
2344}
2345
2346/* Packet state (de-)serialization for privsep */
2347
2348/* turn kex into a blob for packet state serialization */
2349static int
2350kex_to_blob(struct sshbuf *m, struct kex *kex)
2351{
2352 int r;
2353
2354 if ((r = sshbuf_put_string(m, kex->session_id,
2355 kex->session_id_len)) != 0 ||
2356 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2357 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2358 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2359 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2360 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2361 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2362 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2363 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2364 return r;
2365 return 0;
2366}
2367
2368/* turn key exchange results into a blob for packet state serialization */
2369static int
2370newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2371{
2372 struct sshbuf *b;
2373 struct sshcipher_ctx *cc;
2374 struct sshcomp *comp;
2375 struct sshenc *enc;
2376 struct sshmac *mac;
2377 struct newkeys *newkey;
2378 int r;
2379
2380 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2381 return SSH_ERR_INTERNAL_ERROR;
2382 enc = &newkey->enc;
2383 mac = &newkey->mac;
2384 comp = &newkey->comp;
2385 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2386 &ssh->state->receive_context;
2387 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2388 return r;
2389 if ((b = sshbuf_new()) == NULL)
2390 return SSH_ERR_ALLOC_FAIL;
2391 /* The cipher struct is constant and shared, you export pointer */
2392 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2393 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2394 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2395 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2396 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2397 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2398 goto out;
2399 if (cipher_authlen(enc->cipher) == 0) {
2400 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2401 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2402 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2403 goto out;
2404 }
2405 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2406 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2407 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2408 goto out;
2409 r = sshbuf_put_stringb(m, b);
2410 out:
2411 if (b != NULL)
2412 sshbuf_free(b);
2413 return r;
2414}
2415
2416/* serialize packet state into a blob */
2417int
2418ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2419{
2420 struct session_state *state = ssh->state;
2421 u_char *p;
2422 size_t slen, rlen;
2423 int r, ssh1cipher;
2424
2425 if (!compat20) {
2426 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2427 slen = cipher_get_keyiv_len(&state->send_context);
2428 rlen = cipher_get_keyiv_len(&state->receive_context);
2429 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2430 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2431 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2432 (r = sshbuf_put_u32(m, slen)) != 0 ||
2433 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2434 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2435 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2436 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2437 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2438 return r;
2439 } else {
2440 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2441 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2442 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002443 (r = sshbuf_put_u32(m, state->rekey_limit)) != 0 ||
2444 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002445 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2446 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2447 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2448 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2449 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2450 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2451 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2452 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2453 return r;
2454 }
2455
2456 slen = cipher_get_keycontext(&state->send_context, NULL);
2457 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2458 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2459 (r = sshbuf_reserve(m, slen, &p)) != 0)
2460 return r;
2461 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2462 return SSH_ERR_INTERNAL_ERROR;
2463 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2464 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2465 return r;
2466 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2467 return SSH_ERR_INTERNAL_ERROR;
2468
2469 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2470 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2471 (r = sshbuf_put_stringb(m, state->output)) != 0)
2472 return r;
2473
2474 if (compat20) {
2475 if ((r = sshbuf_put_u64(m, get_sent_bytes())) != 0 ||
2476 (r = sshbuf_put_u64(m, get_recv_bytes())) != 0)
2477 return r;
2478 }
2479 return 0;
2480}
2481
2482/* restore key exchange results from blob for packet state de-serialization */
2483static int
2484newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2485{
2486 struct sshbuf *b = NULL;
2487 struct sshcomp *comp;
2488 struct sshenc *enc;
2489 struct sshmac *mac;
2490 struct newkeys *newkey = NULL;
2491 size_t keylen, ivlen, maclen;
2492 int r;
2493
2494 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2495 r = SSH_ERR_ALLOC_FAIL;
2496 goto out;
2497 }
2498 if ((r = sshbuf_froms(m, &b)) != 0)
2499 goto out;
2500#ifdef DEBUG_PK
2501 sshbuf_dump(b, stderr);
2502#endif
2503 enc = &newkey->enc;
2504 mac = &newkey->mac;
2505 comp = &newkey->comp;
2506
2507 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2508 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2509 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2510 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2511 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2512 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2513 goto out;
2514 if (cipher_authlen(enc->cipher) == 0) {
2515 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2516 goto out;
2517 if ((r = mac_setup(mac, mac->name)) != 0)
2518 goto out;
2519 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2520 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2521 goto out;
2522 if (maclen > mac->key_len) {
2523 r = SSH_ERR_INVALID_FORMAT;
2524 goto out;
2525 }
2526 mac->key_len = maclen;
2527 }
2528 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2529 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2530 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2531 goto out;
2532 if (enc->name == NULL ||
2533 cipher_by_name(enc->name) != enc->cipher) {
2534 r = SSH_ERR_INVALID_FORMAT;
2535 goto out;
2536 }
2537 if (sshbuf_len(b) != 0) {
2538 r = SSH_ERR_INVALID_FORMAT;
2539 goto out;
2540 }
2541 enc->key_len = keylen;
2542 enc->iv_len = ivlen;
2543 ssh->kex->newkeys[mode] = newkey;
2544 newkey = NULL;
2545 r = 0;
2546 out:
2547 if (newkey != NULL)
2548 free(newkey);
2549 if (b != NULL)
2550 sshbuf_free(b);
2551 return r;
2552}
2553
2554/* restore kex from blob for packet state de-serialization */
2555static int
2556kex_from_blob(struct sshbuf *m, struct kex **kexp)
2557{
2558 struct kex *kex;
2559 int r;
2560
2561 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2562 (kex->my = sshbuf_new()) == NULL ||
2563 (kex->peer = sshbuf_new()) == NULL) {
2564 r = SSH_ERR_ALLOC_FAIL;
2565 goto out;
2566 }
2567 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2568 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2569 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2570 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2571 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2572 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2573 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2574 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2575 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2576 goto out;
2577 kex->server = 1;
2578 kex->done = 1;
2579 r = 0;
2580 out:
2581 if (r != 0 || kexp == NULL) {
2582 if (kex != NULL) {
2583 if (kex->my != NULL)
2584 sshbuf_free(kex->my);
2585 if (kex->peer != NULL)
2586 sshbuf_free(kex->peer);
2587 free(kex);
2588 }
2589 if (kexp != NULL)
2590 *kexp = NULL;
2591 } else {
2592 *kexp = kex;
2593 }
2594 return r;
2595}
2596
2597/*
2598 * Restore packet state from content of blob 'm' (de-serialization).
2599 * Note that 'm' will be partially consumed on parsing or any other errors.
2600 */
2601int
2602ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2603{
2604 struct session_state *state = ssh->state;
2605 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2606 size_t ssh1keylen, rlen, slen, ilen, olen;
2607 int r;
2608 u_int ssh1cipher = 0;
2609 u_int64_t sent_bytes = 0, recv_bytes = 0;
2610
2611 if (!compat20) {
2612 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2613 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2614 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2615 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2616 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2617 return r;
2618 if (ssh1cipher > INT_MAX)
2619 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2620 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2621 (int)ssh1cipher);
2622 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2623 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2624 return SSH_ERR_INVALID_FORMAT;
2625 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2626 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2627 return r;
2628 } else {
2629 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2630 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2631 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002632 (r = sshbuf_get_u32(m, &state->rekey_limit)) != 0 ||
2633 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002634 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2635 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2636 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2637 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2638 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2639 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2640 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2641 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2642 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002643 /*
2644 * We set the time here so that in post-auth privsep slave we
2645 * count from the completion of the authentication.
2646 */
2647 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002648 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2649 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2650 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2651 return r;
2652 }
2653 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2654 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2655 return r;
2656 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2657 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2658 return SSH_ERR_INVALID_FORMAT;
2659 cipher_set_keycontext(&state->send_context, keyout);
2660 cipher_set_keycontext(&state->receive_context, keyin);
2661
2662 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2663 (r = ssh_packet_set_postauth(ssh)) != 0)
2664 return r;
2665
2666 sshbuf_reset(state->input);
2667 sshbuf_reset(state->output);
2668 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2669 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2670 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2671 (r = sshbuf_put(state->output, output, olen)) != 0)
2672 return r;
2673
2674 if (compat20) {
2675 if ((r = sshbuf_get_u64(m, &sent_bytes)) != 0 ||
2676 (r = sshbuf_get_u64(m, &recv_bytes)) != 0)
2677 return r;
2678 roam_set_bytes(sent_bytes, recv_bytes);
2679 }
2680 if (sshbuf_len(m))
2681 return SSH_ERR_INVALID_FORMAT;
2682 debug3("%s: done", __func__);
2683 return 0;
2684}
2685
2686/* NEW API */
2687
2688/* put data to the outgoing packet */
2689
2690int
2691sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2692{
2693 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2694}
2695
2696int
2697sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2698{
2699 return sshbuf_putb(ssh->state->outgoing_packet, b);
2700}
2701
2702int
2703sshpkt_put_u8(struct ssh *ssh, u_char val)
2704{
2705 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2706}
2707
2708int
2709sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2710{
2711 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2712}
2713
2714int
2715sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2716{
2717 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2718}
2719
2720int
2721sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2722{
2723 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2724}
2725
2726int
2727sshpkt_put_cstring(struct ssh *ssh, const void *v)
2728{
2729 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2730}
2731
2732int
2733sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2734{
2735 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2736}
2737
djm@openbsd.org734226b2015-04-27 01:52:30 +00002738#ifdef WITH_OPENSSL
2739#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002740int
2741sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2742{
2743 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2744}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002745#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002746
Damien Miller773dda22015-01-30 23:10:17 +11002747#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002748int
2749sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2750{
2751 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2752}
Damien Miller773dda22015-01-30 23:10:17 +11002753#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002754
2755int
2756sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2757{
2758 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2759}
Damien Miller773dda22015-01-30 23:10:17 +11002760#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002761
2762/* fetch data from the incoming packet */
2763
2764int
2765sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2766{
2767 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2768}
2769
2770int
2771sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2772{
2773 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2774}
2775
2776int
2777sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2778{
2779 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2780}
2781
2782int
2783sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2784{
2785 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2786}
2787
2788int
2789sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2790{
2791 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2792}
2793
2794int
2795sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2796{
2797 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2798}
2799
2800int
2801sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2802{
2803 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2804}
2805
djm@openbsd.org734226b2015-04-27 01:52:30 +00002806#ifdef WITH_OPENSSL
2807#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002808int
2809sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2810{
2811 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2812}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002813#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002814
Damien Miller773dda22015-01-30 23:10:17 +11002815#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002816int
2817sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2818{
2819 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2820}
Damien Miller773dda22015-01-30 23:10:17 +11002821#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002822
2823int
2824sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2825{
2826 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2827}
Damien Miller773dda22015-01-30 23:10:17 +11002828#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002829
2830int
2831sshpkt_get_end(struct ssh *ssh)
2832{
2833 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2834 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2835 return 0;
2836}
2837
2838const u_char *
2839sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2840{
2841 if (lenp != NULL)
2842 *lenp = sshbuf_len(ssh->state->incoming_packet);
2843 return sshbuf_ptr(ssh->state->incoming_packet);
2844}
2845
2846/* start a new packet */
2847
2848int
2849sshpkt_start(struct ssh *ssh, u_char type)
2850{
2851 u_char buf[9];
2852 int len;
2853
2854 DBG(debug("packet_start[%d]", type));
2855 len = compat20 ? 6 : 9;
2856 memset(buf, 0, len - 1);
2857 buf[len - 1] = type;
2858 sshbuf_reset(ssh->state->outgoing_packet);
2859 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2860}
2861
2862/* send it */
2863
2864int
2865sshpkt_send(struct ssh *ssh)
2866{
2867 if (compat20)
2868 return ssh_packet_send2(ssh);
2869 else
2870 return ssh_packet_send1(ssh);
2871}
2872
2873int
2874sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2875{
2876 char buf[1024];
2877 va_list args;
2878 int r;
2879
2880 va_start(args, fmt);
2881 vsnprintf(buf, sizeof(buf), fmt, args);
2882 va_end(args);
2883
2884 if (compat20) {
2885 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2886 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2887 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2888 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2889 (r = sshpkt_send(ssh)) != 0)
2890 return r;
2891 } else {
2892 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2893 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2894 (r = sshpkt_send(ssh)) != 0)
2895 return r;
2896 }
2897 return 0;
2898}
2899
2900/* roundup current message to pad bytes */
2901int
2902sshpkt_add_padding(struct ssh *ssh, u_char pad)
2903{
2904 ssh->state->extra_pad = pad;
2905 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002906}