blob: 06e16536cc8f0f1f598ed35db0027f0d33bd3cb5 [file] [log] [blame]
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001/* $OpenBSD: packet.c,v 1.219 2015/12/10 17:08:40 mmcc 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));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000522 free(ssh->remote_ipaddr);
523 ssh->remote_ipaddr = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000524 free(ssh->state);
525 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526}
527
528/* Sets remote side protocol flags. */
529
530void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000531ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000533 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534}
535
536/* Returns the remote protocol flags set earlier by the above function. */
537
Ben Lindstrom46c16222000-12-22 01:43:59 +0000538u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000539ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000541 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542}
543
Damien Miller5428f641999-11-25 11:54:57 +1100544/*
545 * Starts packet compression from the next packet on in both directions.
546 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
547 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000548
markus@openbsd.org091c3022015-01-19 19:52:16 +0000549static int
550ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000551{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000552 if (!ssh->state->compression_buffer &&
553 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
554 return SSH_ERR_ALLOC_FAIL;
555 return 0;
556}
557
558static int
559start_compression_out(struct ssh *ssh, int level)
560{
561 if (level < 1 || level > 9)
562 return SSH_ERR_INVALID_ARGUMENT;
563 debug("Enabling compression at level %d.", level);
564 if (ssh->state->compression_out_started == 1)
565 deflateEnd(&ssh->state->compression_out_stream);
566 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
567 case Z_OK:
568 ssh->state->compression_out_started = 1;
569 break;
570 case Z_MEM_ERROR:
571 return SSH_ERR_ALLOC_FAIL;
572 default:
573 return SSH_ERR_INTERNAL_ERROR;
574 }
575 return 0;
576}
577
578static int
579start_compression_in(struct ssh *ssh)
580{
581 if (ssh->state->compression_in_started == 1)
582 inflateEnd(&ssh->state->compression_in_stream);
583 switch (inflateInit(&ssh->state->compression_in_stream)) {
584 case Z_OK:
585 ssh->state->compression_in_started = 1;
586 break;
587 case Z_MEM_ERROR:
588 return SSH_ERR_ALLOC_FAIL;
589 default:
590 return SSH_ERR_INTERNAL_ERROR;
591 }
592 return 0;
593}
594
595int
596ssh_packet_start_compression(struct ssh *ssh, int level)
597{
598 int r;
599
600 if (ssh->state->packet_compression && !compat20)
601 return SSH_ERR_INTERNAL_ERROR;
602 ssh->state->packet_compression = 1;
603 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
604 (r = start_compression_in(ssh)) != 0 ||
605 (r = start_compression_out(ssh, level)) != 0)
606 return r;
607 return 0;
608}
609
610/* XXX remove need for separate compression buffer */
611static int
612compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
613{
614 u_char buf[4096];
615 int r, status;
616
617 if (ssh->state->compression_out_started != 1)
618 return SSH_ERR_INTERNAL_ERROR;
619
620 /* This case is not handled below. */
621 if (sshbuf_len(in) == 0)
622 return 0;
623
624 /* Input is the contents of the input buffer. */
625 if ((ssh->state->compression_out_stream.next_in =
626 sshbuf_mutable_ptr(in)) == NULL)
627 return SSH_ERR_INTERNAL_ERROR;
628 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
629
630 /* Loop compressing until deflate() returns with avail_out != 0. */
631 do {
632 /* Set up fixed-size output buffer. */
633 ssh->state->compression_out_stream.next_out = buf;
634 ssh->state->compression_out_stream.avail_out = sizeof(buf);
635
636 /* Compress as much data into the buffer as possible. */
637 status = deflate(&ssh->state->compression_out_stream,
638 Z_PARTIAL_FLUSH);
639 switch (status) {
640 case Z_MEM_ERROR:
641 return SSH_ERR_ALLOC_FAIL;
642 case Z_OK:
643 /* Append compressed data to output_buffer. */
644 if ((r = sshbuf_put(out, buf, sizeof(buf) -
645 ssh->state->compression_out_stream.avail_out)) != 0)
646 return r;
647 break;
648 case Z_STREAM_ERROR:
649 default:
650 ssh->state->compression_out_failures++;
651 return SSH_ERR_INVALID_FORMAT;
652 }
653 } while (ssh->state->compression_out_stream.avail_out == 0);
654 return 0;
655}
656
657static int
658uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
659{
660 u_char buf[4096];
661 int r, status;
662
663 if (ssh->state->compression_in_started != 1)
664 return SSH_ERR_INTERNAL_ERROR;
665
666 if ((ssh->state->compression_in_stream.next_in =
667 sshbuf_mutable_ptr(in)) == NULL)
668 return SSH_ERR_INTERNAL_ERROR;
669 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
670
671 for (;;) {
672 /* Set up fixed-size output buffer. */
673 ssh->state->compression_in_stream.next_out = buf;
674 ssh->state->compression_in_stream.avail_out = sizeof(buf);
675
676 status = inflate(&ssh->state->compression_in_stream,
677 Z_PARTIAL_FLUSH);
678 switch (status) {
679 case Z_OK:
680 if ((r = sshbuf_put(out, buf, sizeof(buf) -
681 ssh->state->compression_in_stream.avail_out)) != 0)
682 return r;
683 break;
684 case Z_BUF_ERROR:
685 /*
686 * Comments in zlib.h say that we should keep calling
687 * inflate() until we get an error. This appears to
688 * be the error that we get.
689 */
690 return 0;
691 case Z_DATA_ERROR:
692 return SSH_ERR_INVALID_FORMAT;
693 case Z_MEM_ERROR:
694 return SSH_ERR_ALLOC_FAIL;
695 case Z_STREAM_ERROR:
696 default:
697 ssh->state->compression_in_failures++;
698 return SSH_ERR_INTERNAL_ERROR;
699 }
700 }
701 /* NOTREACHED */
702}
703
704/* Serialise compression state into a blob for privsep */
705static int
706ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
707{
708 struct session_state *state = ssh->state;
709 struct sshbuf *b;
710 int r;
711
712 if ((b = sshbuf_new()) == NULL)
713 return SSH_ERR_ALLOC_FAIL;
714 if (state->compression_in_started) {
715 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
716 sizeof(state->compression_in_stream))) != 0)
717 goto out;
718 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
719 goto out;
720 if (state->compression_out_started) {
721 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
722 sizeof(state->compression_out_stream))) != 0)
723 goto out;
724 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
725 goto out;
726 r = sshbuf_put_stringb(m, b);
727 out:
728 sshbuf_free(b);
729 return r;
730}
731
732/* Deserialise compression state from a blob for privsep */
733static int
734ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
735{
736 struct session_state *state = ssh->state;
737 struct sshbuf *b = NULL;
738 int r;
739 const u_char *inblob, *outblob;
740 size_t inl, outl;
741
742 if ((r = sshbuf_froms(m, &b)) != 0)
743 goto out;
744 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
745 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
746 goto out;
747 if (inl == 0)
748 state->compression_in_started = 0;
749 else if (inl != sizeof(state->compression_in_stream)) {
750 r = SSH_ERR_INTERNAL_ERROR;
751 goto out;
752 } else {
753 state->compression_in_started = 1;
754 memcpy(&state->compression_in_stream, inblob, inl);
755 }
756 if (outl == 0)
757 state->compression_out_started = 0;
758 else if (outl != sizeof(state->compression_out_stream)) {
759 r = SSH_ERR_INTERNAL_ERROR;
760 goto out;
761 } else {
762 state->compression_out_started = 1;
763 memcpy(&state->compression_out_stream, outblob, outl);
764 }
765 r = 0;
766 out:
767 sshbuf_free(b);
768 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000769}
770
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000772ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
773 void *(*allocfunc)(void *, u_int, u_int),
774 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000776 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
777 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
778 ssh->state->compression_out_stream.opaque = ctx;
779 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
780 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
781 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782}
783
Damien Miller5428f641999-11-25 11:54:57 +1100784/*
Damien Miller5428f641999-11-25 11:54:57 +1100785 * Causes any further packets to be encrypted using the given key. The same
786 * key is used for both sending and reception. However, both directions are
787 * encrypted independently of each other.
788 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000789
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000791ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000793#ifndef WITH_SSH1
794 fatal("no SSH protocol 1 support");
795#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000796 struct session_state *state = ssh->state;
797 const struct sshcipher *cipher = cipher_by_number(number);
798 int r;
799 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100800
markus@openbsd.org091c3022015-01-19 19:52:16 +0000801 if (cipher == NULL)
802 fatal("%s: unknown cipher number %d", __func__, number);
803 if (keylen < 20)
804 fatal("%s: keylen too small: %d", __func__, keylen);
805 if (keylen > SSH_SESSION_KEY_LENGTH)
806 fatal("%s: keylen too big: %d", __func__, keylen);
807 memcpy(state->ssh1_key, key, keylen);
808 state->ssh1_keylen = keylen;
809 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
810 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
811 (r = cipher_init(&state->receive_context, cipher, key, keylen,
812 NULL, 0, CIPHER_DECRYPT) != 0))
813 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
814 if (!state->cipher_warning_done &&
815 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
816 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
817 error("Warning: %s", wmsg);
818 state->cipher_warning_done = 1;
819 }
Damien Miller773dda22015-01-30 23:10:17 +1100820#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000821}
822
Damien Miller5428f641999-11-25 11:54:57 +1100823/*
824 * Finalizes and sends the packet. If the encryption key has been set,
825 * encrypts the packet before sending.
826 */
Damien Miller95def091999-11-25 00:26:21 +1100827
markus@openbsd.org091c3022015-01-19 19:52:16 +0000828int
829ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000830{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000831 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000832 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000833 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000834 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835
Damien Miller5428f641999-11-25 11:54:57 +1100836 /*
837 * If using packet compression, compress the payload of the outgoing
838 * packet.
839 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000840 if (state->packet_compression) {
841 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100842 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000843 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
844 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100845 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000846 if ((r = sshbuf_put(state->compression_buffer,
847 "\0\0\0\0\0\0\0\0", 8)) != 0)
848 goto out;
849 if ((r = compress_buffer(ssh, state->outgoing_packet,
850 state->compression_buffer)) != 0)
851 goto out;
852 sshbuf_reset(state->outgoing_packet);
853 if ((r = sshbuf_putb(state->outgoing_packet,
854 state->compression_buffer)) != 0)
855 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100856 }
857 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000858 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859
Damien Millere247cc42000-05-07 12:03:14 +1000860 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100861 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000862 if (!state->send_context.plaintext) {
863 cp = sshbuf_mutable_ptr(state->outgoing_packet);
864 if (cp == NULL) {
865 r = SSH_ERR_INTERNAL_ERROR;
866 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100867 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000868 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100869 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000870 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
871 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100872
873 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000874 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
875 sshbuf_len(state->outgoing_packet));
876 POKE_U32(buf, checksum);
877 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
878 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
880#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100881 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000882 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883#endif
884
Damien Miller95def091999-11-25 00:26:21 +1100885 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000886 POKE_U32(buf, len);
887 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
888 goto out;
889 if ((r = sshbuf_reserve(state->output,
890 sshbuf_len(state->outgoing_packet), &cp)) != 0)
891 goto out;
892 if ((r = cipher_crypt(&state->send_context, 0, cp,
893 sshbuf_ptr(state->outgoing_packet),
894 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
895 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100896
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100898 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000899 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000901 state->p_send.packets++;
902 state->p_send.bytes += len +
903 sshbuf_len(state->outgoing_packet);
904 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000905
Damien Miller5428f641999-11-25 11:54:57 +1100906 /*
Damien Miller788f2122005-11-05 15:14:59 +1100907 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000908 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
909 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100910 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000911 r = 0;
912 out:
913 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914}
915
markus@openbsd.org091c3022015-01-19 19:52:16 +0000916int
917ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000918{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000919 struct session_state *state = ssh->state;
920 struct sshenc *enc;
921 struct sshmac *mac;
922 struct sshcomp *comp;
923 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000924 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000925 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000926 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000927
Ben Lindstrom064496f2002-12-23 02:04:22 +0000928 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000929
Damien Miller963f6b22002-02-19 15:21:23 +1100930 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000931 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000932 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000933 state->p_send.packets = state->p_send.blocks = 0;
934 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100935 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000936 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000937 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000938 state->p_read.packets = state->p_read.blocks = 0;
939 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100940 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000941 if (state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000942 debug("set_newkeys: rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000943 if ((r = cipher_cleanup(cc)) != 0)
944 return r;
945 enc = &state->newkeys[mode]->enc;
946 mac = &state->newkeys[mode]->mac;
947 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000948 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100949 explicit_bzero(enc->iv, enc->iv_len);
950 explicit_bzero(enc->key, enc->key_len);
951 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000952 free(enc->name);
953 free(enc->iv);
954 free(enc->key);
955 free(mac->name);
956 free(mac->key);
957 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000958 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000959 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000960 /* move newkeys from kex to state */
961 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
962 return SSH_ERR_INTERNAL_ERROR;
963 ssh->kex->newkeys[mode] = NULL;
964 enc = &state->newkeys[mode]->enc;
965 mac = &state->newkeys[mode]->mac;
966 comp = &state->newkeys[mode]->comp;
967 if (cipher_authlen(enc->cipher) == 0) {
968 if ((r = mac_init(mac)) != 0)
969 return r;
970 }
971 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000972 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000973 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
974 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000975 return r;
976 if (!state->cipher_warning_done &&
977 (wmsg = cipher_warning_message(cc)) != NULL) {
978 error("Warning: %s", wmsg);
979 state->cipher_warning_done = 1;
980 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000981 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100982 /* explicit_bzero(enc->iv, enc->block_size);
983 explicit_bzero(enc->key, enc->key_len);
984 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000985 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000986 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +0000987 state->after_authentication)) && comp->enabled == 0) {
988 if ((r = ssh_packet_init_compression(ssh)) < 0)
989 return r;
990 if (mode == MODE_OUT) {
991 if ((r = start_compression_out(ssh, 6)) != 0)
992 return r;
993 } else {
994 if ((r = start_compression_in(ssh)) != 0)
995 return r;
996 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000997 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000998 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000999 /*
1000 * The 2^(blocksize*2) limit is too expensive for 3DES,
1001 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1002 */
1003 if (enc->block_size >= 16)
1004 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1005 else
1006 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001007 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001008 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001009 state->rekey_limit / enc->block_size);
1010 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001011}
1012
Damien Miller5428f641999-11-25 11:54:57 +11001013/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001014 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001015 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001016 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1017 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001018static int
1019ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001020{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001021 struct session_state *state = ssh->state;
1022 struct sshcomp *comp = NULL;
1023 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001024
1025 /*
1026 * Remember that we are past the authentication step, so rekeying
1027 * with COMP_DELAYED will turn on compression immediately.
1028 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001029 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001030 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001031 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001032 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001033 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001034 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001035 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001036 if ((r = ssh_packet_init_compression(ssh)) != 0)
1037 return r;
1038 if (mode == MODE_OUT) {
1039 if ((r = start_compression_out(ssh, 6)) != 0)
1040 return r;
1041 } else {
1042 if ((r = start_compression_in(ssh)) != 0)
1043 return r;
1044 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001045 comp->enabled = 1;
1046 }
1047 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001048 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001049}
1050
1051/*
Damien Miller33b13562000-04-04 14:38:59 +10001052 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1053 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001054int
1055ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001056{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001057 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001058 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001059 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001060 u_int authlen = 0, aadlen = 0;
1061 u_int len;
1062 struct sshenc *enc = NULL;
1063 struct sshmac *mac = NULL;
1064 struct sshcomp *comp = NULL;
1065 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001066
markus@openbsd.org091c3022015-01-19 19:52:16 +00001067 if (state->newkeys[MODE_OUT] != NULL) {
1068 enc = &state->newkeys[MODE_OUT]->enc;
1069 mac = &state->newkeys[MODE_OUT]->mac;
1070 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001071 /* disable mac for authenticated encryption */
1072 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1073 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001074 }
Damien Miller963f6b22002-02-19 15:21:23 +11001075 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001076 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001077
markus@openbsd.org091c3022015-01-19 19:52:16 +00001078 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001079
1080#ifdef PACKET_DEBUG
1081 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001082 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001083#endif
1084
1085 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001086 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001087 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001088 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1089 goto out;
1090 sshbuf_reset(state->compression_buffer);
1091 if ((r = compress_buffer(ssh, state->outgoing_packet,
1092 state->compression_buffer)) != 0)
1093 goto out;
1094 sshbuf_reset(state->outgoing_packet);
1095 if ((r = sshbuf_put(state->outgoing_packet,
1096 "\0\0\0\0\0", 5)) != 0 ||
1097 (r = sshbuf_putb(state->outgoing_packet,
1098 state->compression_buffer)) != 0)
1099 goto out;
1100 DBG(debug("compression: raw %d compressed %zd", len,
1101 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001102 }
1103
1104 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001105 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001106
1107 /*
1108 * calc size of padding, alloc space, get random data,
1109 * minimum padding is 4 bytes
1110 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001111 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001112 padlen = block_size - (len % block_size);
1113 if (padlen < 4)
1114 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001115 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001116 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001117 state->extra_pad =
1118 roundup(state->extra_pad, block_size);
1119 pad = state->extra_pad -
1120 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001121 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001122 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001123 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001124 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001125 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001126 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1127 goto out;
1128 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001129 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001130 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001131 } else {
1132 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001133 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001134 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001135 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001136 len = sshbuf_len(state->outgoing_packet);
1137 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1138 if (cp == NULL) {
1139 r = SSH_ERR_INTERNAL_ERROR;
1140 goto out;
1141 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001142 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001143 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001144 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001145 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1146 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001147
1148 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001149 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001150 if ((r = mac_compute(mac, state->p_send.seqnr,
1151 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001152 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001153 goto out;
1154 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001155 }
1156 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001157 if ((r = sshbuf_reserve(state->output,
1158 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1159 goto out;
1160 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1161 sshbuf_ptr(state->outgoing_packet),
1162 len - aadlen, aadlen, authlen)) != 0)
1163 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001164 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001165 if (mac && mac->enabled) {
1166 if (mac->etm) {
1167 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001168 if ((r = mac_compute(mac, state->p_send.seqnr,
1169 cp, len, macbuf, sizeof(macbuf))) != 0)
1170 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001171 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001172 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001173 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001174 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1175 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001176 }
Damien Miller33b13562000-04-04 14:38:59 +10001177#ifdef PACKET_DEBUG
1178 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001179 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001180#endif
Damien Miller4af51302000-04-16 11:18:38 +10001181 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001182 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001183 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001184 if (++state->p_send.packets == 0)
1185 if (!(ssh->compat & SSH_BUG_NOREKEY))
1186 return SSH_ERR_NEED_REKEY;
1187 state->p_send.blocks += len / block_size;
1188 state->p_send.bytes += len;
1189 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001190
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001191 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001192 r = ssh_set_newkeys(ssh, MODE_OUT);
1193 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1194 r = ssh_packet_enable_delayed_compress(ssh);
1195 else
1196 r = 0;
1197 out:
1198 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001199}
1200
markus@openbsd.org091c3022015-01-19 19:52:16 +00001201int
1202ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001203{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001204 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001205 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001206 u_char type;
1207 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001208
markus@openbsd.org091c3022015-01-19 19:52:16 +00001209 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001210
1211 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001212 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001213 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1214 (type > SSH2_MSG_TRANSPORT_MAX) ||
1215 (type == SSH2_MSG_SERVICE_REQUEST) ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001216 (type == SSH2_MSG_SERVICE_ACCEPT) ||
1217 (type == SSH2_MSG_EXT_INFO)) {
Damien Millera5539d22003-04-09 20:50:06 +10001218 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001219 p = calloc(1, sizeof(*p));
1220 if (p == NULL)
1221 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001222 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001223 p->payload = state->outgoing_packet;
1224 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1225 state->outgoing_packet = sshbuf_new();
1226 if (state->outgoing_packet == NULL)
1227 return SSH_ERR_ALLOC_FAIL;
1228 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001229 }
1230 }
1231
1232 /* rekeying starts with sending KEXINIT */
1233 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001234 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001235
markus@openbsd.org091c3022015-01-19 19:52:16 +00001236 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1237 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001238
1239 /* after a NEWKEYS message we can send the complete queue */
1240 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001241 state->rekeying = 0;
1242 state->rekey_time = monotime();
1243 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001244 type = p->type;
1245 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001246 sshbuf_free(state->outgoing_packet);
1247 state->outgoing_packet = p->payload;
1248 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001249 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001250 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1251 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001252 }
1253 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001254 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001255}
1256
Damien Miller33b13562000-04-04 14:38:59 +10001257/*
Damien Miller5428f641999-11-25 11:54:57 +11001258 * Waits until a packet has been received, and returns its type. Note that
1259 * no other data is processed until this returns, so this function should not
1260 * be used during the interactive session.
1261 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262
1263int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001264ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001266 struct session_state *state = ssh->state;
1267 int len, r, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001268 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001269 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001270 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271
Darren Tucker99bb7612008-06-13 22:02:50 +10001272 DBG(debug("packet_read()"));
1273
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001274 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001275 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001276 if (setp == NULL)
1277 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001278
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001279 /*
1280 * Since we are blocking, ensure that all written packets have
1281 * been sent.
1282 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001283 if ((r = ssh_packet_write_wait(ssh)) != 0)
1284 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285
Damien Miller95def091999-11-25 00:26:21 +11001286 /* Stay in the loop until we have received a complete packet. */
1287 for (;;) {
1288 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001289 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1290 if (r != 0)
1291 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001292 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001293 *typep == SSH_SMSG_SUCCESS
1294 || *typep == SSH_SMSG_FAILURE
1295 || *typep == SSH_CMSG_EOF
1296 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1297 if ((r = sshpkt_get_end(ssh)) != 0)
1298 break;
Damien Miller95def091999-11-25 00:26:21 +11001299 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001300 if (*typep != SSH_MSG_NONE)
1301 break;
Damien Miller5428f641999-11-25 11:54:57 +11001302 /*
1303 * Otherwise, wait for some data to arrive, add it to the
1304 * buffer, and try again.
1305 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001306 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001307 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001308 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001309
markus@openbsd.org091c3022015-01-19 19:52:16 +00001310 if (state->packet_timeout_ms > 0) {
1311 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001312 timeoutp = &timeout;
1313 }
Damien Miller95def091999-11-25 00:26:21 +11001314 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001315 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001316 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001317 ms_to_timeval(&timeout, ms_remain);
1318 gettimeofday(&start, NULL);
1319 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001320 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001321 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001322 break;
Damien Millerea437422009-10-02 11:49:03 +10001323 if (errno != EAGAIN && errno != EINTR &&
1324 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001325 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001326 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001327 continue;
1328 ms_subtract_diff(&start, &ms_remain);
1329 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001330 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001331 break;
1332 }
1333 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001334 if (r == 0)
1335 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001336 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001337 do {
1338 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001339 len = roaming_read(state->connection_in, buf,
Darren Tuckerc5564e12009-06-21 18:53:53 +10001340 sizeof(buf), &cont);
1341 } while (len == 0 && cont);
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001342 if (len == 0) {
1343 r = SSH_ERR_CONN_CLOSED;
1344 goto out;
1345 }
1346 if (len < 0) {
1347 r = SSH_ERR_SYSTEM_ERROR;
1348 goto out;
1349 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001350
Damien Miller95def091999-11-25 00:26:21 +11001351 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001352 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001353 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001354 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001355 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001356 free(setp);
1357 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358}
1359
Damien Miller278f9072001-12-21 15:00:19 +11001360int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001361ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001362{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001363 u_char type;
1364 int r;
1365
1366 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1367 fatal("%s: %s", __func__, ssh_err(r));
1368 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001369}
1370
Damien Miller5428f641999-11-25 11:54:57 +11001371/*
1372 * Waits until a packet has been received, verifies that its type matches
1373 * that given, and gives a fatal error and exits if there is a mismatch.
1374 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001376int
1377ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001379 int r;
1380 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001382 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1383 return r;
1384 if (type != expected_type) {
1385 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001386 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001387 expected_type, type)) != 0)
1388 return r;
1389 return SSH_ERR_PROTOCOL_ERROR;
1390 }
1391 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392}
1393
1394/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001395 * packet_process_incoming. If so, reads the packet; otherwise returns
1396 * SSH_MSG_NONE. This does not wait for data from the connection.
1397 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001398 * SSH_MSG_DISCONNECT is handled specially here. Also,
1399 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1400 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001401 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402
markus@openbsd.org091c3022015-01-19 19:52:16 +00001403int
1404ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001406 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001407 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001408 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001409 const u_char *cp;
1410 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001411 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001412 int r;
1413
1414 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001415
Damien Miller95def091999-11-25 00:26:21 +11001416 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001417 if (sshbuf_len(state->input) < 4 + 8)
1418 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001419 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001420 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001421 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1422 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1423 len)) != 0)
1424 return r;
1425 return SSH_ERR_CONN_CORRUPT;
1426 }
Damien Miller95def091999-11-25 00:26:21 +11001427 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428
Damien Miller95def091999-11-25 00:26:21 +11001429 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001430 if (sshbuf_len(state->input) < 4 + padded_len)
1431 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432
Damien Miller95def091999-11-25 00:26:21 +11001433 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434
Damien Miller95def091999-11-25 00:26:21 +11001435 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001436 if ((r = sshbuf_consume(state->input, 4)) != 0)
1437 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001438
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001439 /*
1440 * Cryptographic attack detector for ssh
1441 * (C)1998 CORE-SDI, Buenos Aires Argentina
1442 * Ariel Futoransky(futo@core-sdi.com)
1443 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001444 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001445 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001446 switch (detect_attack(&state->deattack,
1447 sshbuf_ptr(state->input), padded_len)) {
1448 case DEATTACK_OK:
1449 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001450 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001451 emsg = "crc32 compensation attack detected";
1452 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001453 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001454 emsg = "deattack denial of service detected";
1455 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001456 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001457 emsg = "deattack error";
1458 break;
1459 }
1460 if (emsg != NULL) {
1461 error("%s", emsg);
1462 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1463 (r = ssh_packet_write_wait(ssh)) != 0)
1464 return r;
1465 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001466 }
1467 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001468
1469 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001470 sshbuf_reset(state->incoming_packet);
1471 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1472 goto out;
1473 if ((r = cipher_crypt(&state->receive_context, 0, p,
1474 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1475 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001476
markus@openbsd.org091c3022015-01-19 19:52:16 +00001477 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1478 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
1480#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001481 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001482 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001484
Damien Miller95def091999-11-25 00:26:21 +11001485 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001486 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1487 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488
Damien Miller95def091999-11-25 00:26:21 +11001489 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001490 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1491 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001492
Damien Miller95def091999-11-25 00:26:21 +11001493 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001494 if (len != sshbuf_len(state->incoming_packet)) {
1495 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001496 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001497 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1498 (r = ssh_packet_write_wait(ssh)) != 0)
1499 return r;
1500 return SSH_ERR_CONN_CORRUPT;
1501 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001502
markus@openbsd.org091c3022015-01-19 19:52:16 +00001503 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1504 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001505 if (checksum != stored_checksum) {
1506 error("Corrupted check bytes on input");
1507 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1508 (r = ssh_packet_write_wait(ssh)) != 0)
1509 return r;
1510 return SSH_ERR_CONN_CORRUPT;
1511 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001512 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1513 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001514
markus@openbsd.org091c3022015-01-19 19:52:16 +00001515 if (state->packet_compression) {
1516 sshbuf_reset(state->compression_buffer);
1517 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1518 state->compression_buffer)) != 0)
1519 goto out;
1520 sshbuf_reset(state->incoming_packet);
1521 if ((r = sshbuf_putb(state->incoming_packet,
1522 state->compression_buffer)) != 0)
1523 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001524 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001525 state->p_read.packets++;
1526 state->p_read.bytes += padded_len + 4;
1527 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1528 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001529 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1530 error("Invalid ssh1 packet type: %d", *typep);
1531 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1532 (r = ssh_packet_write_wait(ssh)) != 0)
1533 return r;
1534 return SSH_ERR_PROTOCOL_ERROR;
1535 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001536 r = 0;
1537 out:
1538 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539}
Damien Miller95def091999-11-25 00:26:21 +11001540
markus@openbsd.org091c3022015-01-19 19:52:16 +00001541int
1542ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001543{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001544 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001545 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001546 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1547 u_int maclen, aadlen = 0, authlen = 0, block_size;
1548 struct sshenc *enc = NULL;
1549 struct sshmac *mac = NULL;
1550 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001551 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001552
markus@openbsd.org091c3022015-01-19 19:52:16 +00001553 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001554
markus@openbsd.org091c3022015-01-19 19:52:16 +00001555 if (state->packet_discard)
1556 return 0;
1557
1558 if (state->newkeys[MODE_IN] != NULL) {
1559 enc = &state->newkeys[MODE_IN]->enc;
1560 mac = &state->newkeys[MODE_IN]->mac;
1561 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001562 /* disable mac for authenticated encryption */
1563 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1564 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001565 }
1566 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001567 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001568 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001569
markus@openbsd.org091c3022015-01-19 19:52:16 +00001570 if (aadlen && state->packlen == 0) {
1571 if (cipher_get_length(&state->receive_context,
1572 &state->packlen, state->p_read.seqnr,
1573 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1574 return 0;
1575 if (state->packlen < 1 + 4 ||
1576 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001577#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001578 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001579#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001580 logit("Bad packet length %u.", state->packlen);
1581 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1582 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001583 return SSH_ERR_CONN_CORRUPT;
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) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001785 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001786 return r;
1787 }
Damien Miller33b13562000-04-04 14:38:59 +10001788 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001789 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001790 break;
1791 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001792 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1793 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1794 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001795 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001796 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001797 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1798 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1799 "Received disconnect from %s: %u: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001800 ssh_remote_ipaddr(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001801 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001802 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001803 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001804 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1805 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001806 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1807 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001808 break;
Damien Miller33b13562000-04-04 14:38:59 +10001809 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001810 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001811 }
Damien Miller33b13562000-04-04 14:38:59 +10001812 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001813 r = ssh_packet_read_poll1(ssh, typep);
1814 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001815 case SSH_MSG_NONE:
1816 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001817 case SSH_MSG_IGNORE:
1818 break;
1819 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001820 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1821 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001822 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001823 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001824 break;
1825 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001826 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1827 return r;
Damien Miller894926e2013-02-12 11:03:58 +11001828 error("Received disconnect from %s: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001829 ssh_remote_ipaddr(ssh), msg);
1830 free(msg);
1831 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001832 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001833 DBG(debug("received packet type %d", *typep));
1834 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001835 }
Damien Miller33b13562000-04-04 14:38:59 +10001836 }
1837 }
1838}
1839
Damien Miller5428f641999-11-25 11:54:57 +11001840/*
1841 * Buffers the given amount of input characters. This is intended to be used
1842 * together with packet_read_poll.
1843 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001844
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001845int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001846ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001847{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001848 struct session_state *state = ssh->state;
1849 int r;
1850
1851 if (state->packet_discard) {
1852 state->keep_alive_timeouts = 0; /* ?? */
1853 if (len >= state->packet_discard) {
1854 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001855 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001856 }
1857 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001858 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001859 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001860 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001861 return r;
1862
1863 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001864}
1865
Damien Miller4af51302000-04-16 11:18:38 +10001866int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001867ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001868{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001869 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001870}
1871
Damien Miller5428f641999-11-25 11:54:57 +11001872/*
1873 * Sends a diagnostic message from the server to the client. This message
1874 * can be sent at any time (but not while constructing another message). The
1875 * message is printed immediately, but only if the client is being executed
1876 * in verbose mode. These messages are primarily intended to ease debugging
1877 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001878 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11001879 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001880void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001881ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001882{
Damien Miller95def091999-11-25 00:26:21 +11001883 char buf[1024];
1884 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001885 int r;
Damien Miller95def091999-11-25 00:26:21 +11001886
markus@openbsd.org091c3022015-01-19 19:52:16 +00001887 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001888 return;
1889
Damien Miller95def091999-11-25 00:26:21 +11001890 va_start(args, fmt);
1891 vsnprintf(buf, sizeof(buf), fmt, args);
1892 va_end(args);
1893
Damien Miller7c8af4f2000-05-01 08:24:07 +10001894 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001895 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1896 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1897 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1898 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1899 (r = sshpkt_send(ssh)) != 0)
1900 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001901 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001902 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1903 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1904 (r = sshpkt_send(ssh)) != 0)
1905 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001906 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001907 if ((r = ssh_packet_write_wait(ssh)) != 0)
1908 fatal("%s: %s", __func__, ssh_err(r));
1909}
1910
1911/*
1912 * Pretty-print connection-terminating errors and exit.
1913 */
1914void
1915sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
1916{
1917 switch (r) {
1918 case SSH_ERR_CONN_CLOSED:
1919 logit("Connection closed by %.200s", ssh_remote_ipaddr(ssh));
1920 cleanup_exit(255);
1921 case SSH_ERR_CONN_TIMEOUT:
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001922 logit("Connection to %.200s timed out", ssh_remote_ipaddr(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001923 cleanup_exit(255);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001924 case SSH_ERR_DISCONNECTED:
1925 logit("Disconnected from %.200s",
1926 ssh_remote_ipaddr(ssh));
1927 cleanup_exit(255);
1928 case SSH_ERR_SYSTEM_ERROR:
1929 if (errno == ECONNRESET) {
1930 logit("Connection reset by %.200s",
1931 ssh_remote_ipaddr(ssh));
1932 cleanup_exit(255);
1933 }
1934 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001935 case SSH_ERR_NO_CIPHER_ALG_MATCH:
1936 case SSH_ERR_NO_MAC_ALG_MATCH:
1937 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
1938 case SSH_ERR_NO_KEX_ALG_MATCH:
1939 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
1940 if (ssh && ssh->kex && ssh->kex->failed_choice) {
1941 fatal("Unable to negotiate with %.200s: %s. "
1942 "Their offer: %s", ssh_remote_ipaddr(ssh),
1943 ssh_err(r), ssh->kex->failed_choice);
1944 }
1945 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001946 default:
1947 fatal("%s%sConnection to %.200s: %s",
1948 tag != NULL ? tag : "", tag != NULL ? ": " : "",
1949 ssh_remote_ipaddr(ssh), ssh_err(r));
1950 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001951}
1952
Damien Miller5428f641999-11-25 11:54:57 +11001953/*
1954 * Logs the error plus constructs and sends a disconnect packet, closes the
1955 * connection, and exits. This function never returns. The error message
1956 * should not contain a newline. The length of the formatted message must
1957 * not exceed 1024 bytes.
1958 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001959void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001960ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001961{
Damien Miller95def091999-11-25 00:26:21 +11001962 char buf[1024];
1963 va_list args;
1964 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001965 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001966
Damien Miller95def091999-11-25 00:26:21 +11001967 if (disconnecting) /* Guard against recursive invocations. */
1968 fatal("packet_disconnect called recursively.");
1969 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001970
Damien Miller5428f641999-11-25 11:54:57 +11001971 /*
1972 * Format the message. Note that the caller must make sure the
1973 * message is of limited size.
1974 */
Damien Miller95def091999-11-25 00:26:21 +11001975 va_start(args, fmt);
1976 vsnprintf(buf, sizeof(buf), fmt, args);
1977 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001978
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001979 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001980 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001981
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001982 /*
1983 * Send the disconnect message to the other side, and wait
1984 * for it to get sent.
1985 */
1986 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
1987 sshpkt_fatal(ssh, __func__, r);
1988
1989 if ((r = ssh_packet_write_wait(ssh)) != 0)
1990 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001991
Damien Miller95def091999-11-25 00:26:21 +11001992 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001993 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001994 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001995}
1996
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001997/*
1998 * Checks if there is any buffered output, and tries to write some of
1999 * the output.
2000 */
2001int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002002ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002003{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002004 struct session_state *state = ssh->state;
2005 int len = sshbuf_len(state->output);
2006 int cont, r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002007
Damien Miller95def091999-11-25 00:26:21 +11002008 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10002009 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002010 len = roaming_write(state->connection_out,
2011 sshbuf_ptr(state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10002012 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002013 if (errno == EINTR || errno == EAGAIN ||
2014 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002015 return 0;
2016 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002017 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10002018 if (len == 0 && !cont)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002019 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002020 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002021 return r;
Damien Miller95def091999-11-25 00:26:21 +11002022 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002023 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002024}
2025
Damien Miller5428f641999-11-25 11:54:57 +11002026/*
2027 * Calls packet_write_poll repeatedly until all pending output data has been
2028 * written.
2029 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002030int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002031ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002032{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002033 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002034 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002035 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002036 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002037
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002038 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002039 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002040 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002041 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002042 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2043 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002044 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002045 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002046 while (ssh_packet_have_data_to_write(ssh)) {
2047 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002048 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002049 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002050
markus@openbsd.org091c3022015-01-19 19:52:16 +00002051 if (state->packet_timeout_ms > 0) {
2052 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002053 timeoutp = &timeout;
2054 }
2055 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002056 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002057 ms_to_timeval(&timeout, ms_remain);
2058 gettimeofday(&start, NULL);
2059 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002060 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002061 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002062 break;
Damien Millerea437422009-10-02 11:49:03 +10002063 if (errno != EAGAIN && errno != EINTR &&
2064 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002065 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002066 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002067 continue;
2068 ms_subtract_diff(&start, &ms_remain);
2069 if (ms_remain <= 0) {
2070 ret = 0;
2071 break;
2072 }
2073 }
2074 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002075 free(setp);
2076 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002077 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002078 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2079 free(setp);
2080 return r;
2081 }
Damien Miller95def091999-11-25 00:26:21 +11002082 }
Darren Tuckera627d422013-06-02 07:31:17 +10002083 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002084 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002085}
2086
2087/* Returns true if there is buffered data to write to the connection. */
2088
2089int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002090ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002091{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002092 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002093}
2094
2095/* Returns true if there is not too much data to write to the connection. */
2096
2097int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002098ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002099{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002100 if (ssh->state->interactive_mode)
2101 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002102 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002103 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002104}
2105
markus@openbsd.org091c3022015-01-19 19:52:16 +00002106void
2107ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002108{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002109#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002110 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002111 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002112 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002113# ifdef IP_TOS
2114 case AF_INET:
2115 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002116 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002117 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2118 error("setsockopt IP_TOS %d: %.100s:",
2119 tos, strerror(errno));
2120 break;
2121# endif /* IP_TOS */
2122# ifdef IPV6_TCLASS
2123 case AF_INET6:
2124 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002125 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002126 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2127 error("setsockopt IPV6_TCLASS %d: %.100s:",
2128 tos, strerror(errno));
2129 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002130# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002131 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002132#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002133}
Ben Lindstroma7433982002-12-23 02:41:41 +00002134
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002135/* Informs that the current session is interactive. Sets IP flags for that. */
2136
2137void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002138ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002139{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002140 struct session_state *state = ssh->state;
2141
2142 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002143 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002144 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002145
Damien Miller95def091999-11-25 00:26:21 +11002146 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002147 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002148
Damien Miller34132e52000-01-14 15:45:46 +11002149 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002150 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002151 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002152 set_nodelay(state->connection_in);
2153 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2154 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002155}
2156
2157/* Returns true if the current connection is interactive. */
2158
2159int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002160ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002161{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002162 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002163}
Damien Miller6162d121999-11-21 13:23:52 +11002164
Darren Tucker1f8311c2004-05-13 16:39:33 +10002165int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002166ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002167{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002168 struct session_state *state = ssh->state;
2169
2170 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002171 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002172 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002173 return -1;
2174 }
2175 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002176 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002177 return -1;
2178 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002179 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002180 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002181 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002182 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002183}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002184
Darren Tuckerf7288d72009-06-21 18:12:20 +10002185int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002186ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002187{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002188 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002189}
2190
2191void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002192ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002193{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002194 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002195}
2196
2197u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002198ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002199{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002200 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002201}
2202
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002203/*
2204 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002205 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002206 * byte SSH_MSG_IGNORE
2207 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002208 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002209 * All implementations MUST understand (and ignore) this message at any
2210 * time (after receiving the protocol version). No implementation is
2211 * required to send them. This message can be used as an additional
2212 * protection measure against advanced traffic analysis techniques.
2213 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002214void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002215ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002216{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002217 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002218 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002219
markus@openbsd.org091c3022015-01-19 19:52:16 +00002220 if ((r = sshpkt_start(ssh, compat20 ?
2221 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2222 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2223 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002224 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002225 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002226 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002227 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2228 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002229 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002230 }
2231}
Damien Millera5539d22003-04-09 20:50:06 +10002232
Darren Tucker1f8311c2004-05-13 16:39:33 +10002233#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002234int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002235ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002236{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002237 struct session_state *state = ssh->state;
2238
2239 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002240 return 0;
2241 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002242 (state->p_send.packets > MAX_PACKETS) ||
2243 (state->p_read.packets > MAX_PACKETS) ||
2244 (state->max_blocks_out &&
2245 (state->p_send.blocks > state->max_blocks_out)) ||
2246 (state->max_blocks_in &&
2247 (state->p_read.blocks > state->max_blocks_in)) ||
2248 (state->rekey_interval != 0 && state->rekey_time +
2249 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002250}
2251
2252void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002253ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002254{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002255 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
2256 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002257 ssh->state->rekey_limit = bytes;
2258 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002259}
2260
2261time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002262ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002263{
2264 time_t seconds;
2265
markus@openbsd.org091c3022015-01-19 19:52:16 +00002266 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002267 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002268 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002269}
Damien Miller9786e6e2005-07-26 21:54:56 +10002270
2271void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002272ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002273{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002274 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002275}
2276
2277void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002278ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002279{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002280 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002281}
2282
2283void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002284ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002285{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002286 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002287}
2288
2289void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002290ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002291{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002292 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002293}
2294
markus@openbsd.org091c3022015-01-19 19:52:16 +00002295/* XXX TODO update roaming to new API (does not work anyway) */
Darren Tuckere841eb02009-07-06 07:11:13 +10002296/*
2297 * Save the state for the real connection, and use a separate state when
2298 * resuming a suspended connection.
2299 */
2300void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002301ssh_packet_backup_state(struct ssh *ssh,
2302 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002303{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002304 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002305
markus@openbsd.org091c3022015-01-19 19:52:16 +00002306 close(ssh->state->connection_in);
2307 ssh->state->connection_in = -1;
2308 close(ssh->state->connection_out);
2309 ssh->state->connection_out = -1;
Darren Tuckere841eb02009-07-06 07:11:13 +10002310 if (backup_state)
2311 tmp = backup_state;
2312 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002313 tmp = ssh_alloc_session_state();
2314 backup_state = ssh;
2315 ssh = tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002316}
2317
markus@openbsd.org091c3022015-01-19 19:52:16 +00002318/* XXX FIXME FIXME FIXME */
Darren Tuckere841eb02009-07-06 07:11:13 +10002319/*
2320 * Swap in the old state when resuming a connecion.
2321 */
2322void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002323ssh_packet_restore_state(struct ssh *ssh,
2324 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002325{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002326 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002327 u_int len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002328 int r;
Darren Tuckere841eb02009-07-06 07:11:13 +10002329
2330 tmp = backup_state;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002331 backup_state = ssh;
2332 ssh = tmp;
2333 ssh->state->connection_in = backup_state->state->connection_in;
2334 backup_state->state->connection_in = -1;
2335 ssh->state->connection_out = backup_state->state->connection_out;
2336 backup_state->state->connection_out = -1;
2337 len = sshbuf_len(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002338 if (len > 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002339 if ((r = sshbuf_putb(ssh->state->input,
2340 backup_state->state->input)) != 0)
2341 fatal("%s: %s", __func__, ssh_err(r));
2342 sshbuf_reset(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002343 add_recv_bytes(len);
2344 }
2345}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002346
2347/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002348static int
2349ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002350{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002351 struct sshcomp *comp;
2352 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002353
2354 debug("%s: called", __func__);
2355 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002356 ssh->state->after_authentication = 1;
2357 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002358 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002359 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002360 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002361 comp = &ssh->state->newkeys[mode]->comp;
2362 if (comp && comp->enabled &&
2363 (r = ssh_packet_init_compression(ssh)) != 0)
2364 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002365 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002366 return 0;
2367}
2368
2369/* Packet state (de-)serialization for privsep */
2370
2371/* turn kex into a blob for packet state serialization */
2372static int
2373kex_to_blob(struct sshbuf *m, struct kex *kex)
2374{
2375 int r;
2376
2377 if ((r = sshbuf_put_string(m, kex->session_id,
2378 kex->session_id_len)) != 0 ||
2379 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2380 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2381 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2382 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2383 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2384 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2385 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2386 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2387 return r;
2388 return 0;
2389}
2390
2391/* turn key exchange results into a blob for packet state serialization */
2392static int
2393newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2394{
2395 struct sshbuf *b;
2396 struct sshcipher_ctx *cc;
2397 struct sshcomp *comp;
2398 struct sshenc *enc;
2399 struct sshmac *mac;
2400 struct newkeys *newkey;
2401 int r;
2402
2403 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2404 return SSH_ERR_INTERNAL_ERROR;
2405 enc = &newkey->enc;
2406 mac = &newkey->mac;
2407 comp = &newkey->comp;
2408 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2409 &ssh->state->receive_context;
2410 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2411 return r;
2412 if ((b = sshbuf_new()) == NULL)
2413 return SSH_ERR_ALLOC_FAIL;
2414 /* The cipher struct is constant and shared, you export pointer */
2415 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2416 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2417 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2418 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2419 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2420 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2421 goto out;
2422 if (cipher_authlen(enc->cipher) == 0) {
2423 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2424 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2425 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2426 goto out;
2427 }
2428 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2429 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2430 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2431 goto out;
2432 r = sshbuf_put_stringb(m, b);
2433 out:
2434 if (b != NULL)
2435 sshbuf_free(b);
2436 return r;
2437}
2438
2439/* serialize packet state into a blob */
2440int
2441ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2442{
2443 struct session_state *state = ssh->state;
2444 u_char *p;
2445 size_t slen, rlen;
2446 int r, ssh1cipher;
2447
2448 if (!compat20) {
2449 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2450 slen = cipher_get_keyiv_len(&state->send_context);
2451 rlen = cipher_get_keyiv_len(&state->receive_context);
2452 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2453 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2454 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2455 (r = sshbuf_put_u32(m, slen)) != 0 ||
2456 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2457 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2458 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2459 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2460 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2461 return r;
2462 } else {
2463 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2464 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2465 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002466 (r = sshbuf_put_u32(m, state->rekey_limit)) != 0 ||
2467 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002468 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2469 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2470 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2471 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2472 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2473 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2474 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2475 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2476 return r;
2477 }
2478
2479 slen = cipher_get_keycontext(&state->send_context, NULL);
2480 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2481 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2482 (r = sshbuf_reserve(m, slen, &p)) != 0)
2483 return r;
2484 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2485 return SSH_ERR_INTERNAL_ERROR;
2486 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2487 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2488 return r;
2489 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2490 return SSH_ERR_INTERNAL_ERROR;
2491
2492 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2493 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2494 (r = sshbuf_put_stringb(m, state->output)) != 0)
2495 return r;
2496
2497 if (compat20) {
2498 if ((r = sshbuf_put_u64(m, get_sent_bytes())) != 0 ||
2499 (r = sshbuf_put_u64(m, get_recv_bytes())) != 0)
2500 return r;
2501 }
2502 return 0;
2503}
2504
2505/* restore key exchange results from blob for packet state de-serialization */
2506static int
2507newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2508{
2509 struct sshbuf *b = NULL;
2510 struct sshcomp *comp;
2511 struct sshenc *enc;
2512 struct sshmac *mac;
2513 struct newkeys *newkey = NULL;
2514 size_t keylen, ivlen, maclen;
2515 int r;
2516
2517 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2518 r = SSH_ERR_ALLOC_FAIL;
2519 goto out;
2520 }
2521 if ((r = sshbuf_froms(m, &b)) != 0)
2522 goto out;
2523#ifdef DEBUG_PK
2524 sshbuf_dump(b, stderr);
2525#endif
2526 enc = &newkey->enc;
2527 mac = &newkey->mac;
2528 comp = &newkey->comp;
2529
2530 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2531 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2532 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2533 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2534 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2535 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2536 goto out;
2537 if (cipher_authlen(enc->cipher) == 0) {
2538 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2539 goto out;
2540 if ((r = mac_setup(mac, mac->name)) != 0)
2541 goto out;
2542 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2543 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2544 goto out;
2545 if (maclen > mac->key_len) {
2546 r = SSH_ERR_INVALID_FORMAT;
2547 goto out;
2548 }
2549 mac->key_len = maclen;
2550 }
2551 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2552 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2553 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2554 goto out;
2555 if (enc->name == NULL ||
2556 cipher_by_name(enc->name) != enc->cipher) {
2557 r = SSH_ERR_INVALID_FORMAT;
2558 goto out;
2559 }
2560 if (sshbuf_len(b) != 0) {
2561 r = SSH_ERR_INVALID_FORMAT;
2562 goto out;
2563 }
2564 enc->key_len = keylen;
2565 enc->iv_len = ivlen;
2566 ssh->kex->newkeys[mode] = newkey;
2567 newkey = NULL;
2568 r = 0;
2569 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002570 free(newkey);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002571 if (b != NULL)
2572 sshbuf_free(b);
2573 return r;
2574}
2575
2576/* restore kex from blob for packet state de-serialization */
2577static int
2578kex_from_blob(struct sshbuf *m, struct kex **kexp)
2579{
2580 struct kex *kex;
2581 int r;
2582
2583 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2584 (kex->my = sshbuf_new()) == NULL ||
2585 (kex->peer = sshbuf_new()) == NULL) {
2586 r = SSH_ERR_ALLOC_FAIL;
2587 goto out;
2588 }
2589 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2590 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2591 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2592 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2593 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2594 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2595 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2596 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2597 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2598 goto out;
2599 kex->server = 1;
2600 kex->done = 1;
2601 r = 0;
2602 out:
2603 if (r != 0 || kexp == NULL) {
2604 if (kex != NULL) {
2605 if (kex->my != NULL)
2606 sshbuf_free(kex->my);
2607 if (kex->peer != NULL)
2608 sshbuf_free(kex->peer);
2609 free(kex);
2610 }
2611 if (kexp != NULL)
2612 *kexp = NULL;
2613 } else {
2614 *kexp = kex;
2615 }
2616 return r;
2617}
2618
2619/*
2620 * Restore packet state from content of blob 'm' (de-serialization).
2621 * Note that 'm' will be partially consumed on parsing or any other errors.
2622 */
2623int
2624ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2625{
2626 struct session_state *state = ssh->state;
2627 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2628 size_t ssh1keylen, rlen, slen, ilen, olen;
2629 int r;
2630 u_int ssh1cipher = 0;
2631 u_int64_t sent_bytes = 0, recv_bytes = 0;
2632
2633 if (!compat20) {
2634 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2635 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2636 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2637 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2638 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2639 return r;
2640 if (ssh1cipher > INT_MAX)
2641 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2642 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2643 (int)ssh1cipher);
2644 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2645 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2646 return SSH_ERR_INVALID_FORMAT;
2647 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2648 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2649 return r;
2650 } else {
2651 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2652 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2653 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002654 (r = sshbuf_get_u32(m, &state->rekey_limit)) != 0 ||
2655 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002656 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2657 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2658 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2659 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2660 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2661 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2662 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2663 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2664 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002665 /*
2666 * We set the time here so that in post-auth privsep slave we
2667 * count from the completion of the authentication.
2668 */
2669 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002670 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2671 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2672 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2673 return r;
2674 }
2675 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2676 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2677 return r;
2678 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2679 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2680 return SSH_ERR_INVALID_FORMAT;
2681 cipher_set_keycontext(&state->send_context, keyout);
2682 cipher_set_keycontext(&state->receive_context, keyin);
2683
2684 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2685 (r = ssh_packet_set_postauth(ssh)) != 0)
2686 return r;
2687
2688 sshbuf_reset(state->input);
2689 sshbuf_reset(state->output);
2690 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2691 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2692 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2693 (r = sshbuf_put(state->output, output, olen)) != 0)
2694 return r;
2695
2696 if (compat20) {
2697 if ((r = sshbuf_get_u64(m, &sent_bytes)) != 0 ||
2698 (r = sshbuf_get_u64(m, &recv_bytes)) != 0)
2699 return r;
2700 roam_set_bytes(sent_bytes, recv_bytes);
2701 }
2702 if (sshbuf_len(m))
2703 return SSH_ERR_INVALID_FORMAT;
2704 debug3("%s: done", __func__);
2705 return 0;
2706}
2707
2708/* NEW API */
2709
2710/* put data to the outgoing packet */
2711
2712int
2713sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2714{
2715 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2716}
2717
2718int
2719sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2720{
2721 return sshbuf_putb(ssh->state->outgoing_packet, b);
2722}
2723
2724int
2725sshpkt_put_u8(struct ssh *ssh, u_char val)
2726{
2727 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2728}
2729
2730int
2731sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2732{
2733 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2734}
2735
2736int
2737sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2738{
2739 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2740}
2741
2742int
2743sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2744{
2745 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2746}
2747
2748int
2749sshpkt_put_cstring(struct ssh *ssh, const void *v)
2750{
2751 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2752}
2753
2754int
2755sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2756{
2757 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2758}
2759
djm@openbsd.org734226b2015-04-27 01:52:30 +00002760#ifdef WITH_OPENSSL
2761#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002762int
2763sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2764{
2765 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2766}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002767#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002768
Damien Miller773dda22015-01-30 23:10:17 +11002769#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002770int
2771sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2772{
2773 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2774}
Damien Miller773dda22015-01-30 23:10:17 +11002775#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002776
2777int
2778sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2779{
2780 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2781}
Damien Miller773dda22015-01-30 23:10:17 +11002782#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002783
2784/* fetch data from the incoming packet */
2785
2786int
2787sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2788{
2789 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2790}
2791
2792int
2793sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2794{
2795 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2796}
2797
2798int
2799sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2800{
2801 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2802}
2803
2804int
2805sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2806{
2807 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2808}
2809
2810int
2811sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2812{
2813 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2814}
2815
2816int
2817sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2818{
2819 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2820}
2821
2822int
2823sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2824{
2825 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2826}
2827
djm@openbsd.org734226b2015-04-27 01:52:30 +00002828#ifdef WITH_OPENSSL
2829#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002830int
2831sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2832{
2833 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2834}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002835#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002836
Damien Miller773dda22015-01-30 23:10:17 +11002837#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002838int
2839sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2840{
2841 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2842}
Damien Miller773dda22015-01-30 23:10:17 +11002843#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002844
2845int
2846sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2847{
2848 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2849}
Damien Miller773dda22015-01-30 23:10:17 +11002850#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002851
2852int
2853sshpkt_get_end(struct ssh *ssh)
2854{
2855 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2856 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2857 return 0;
2858}
2859
2860const u_char *
2861sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2862{
2863 if (lenp != NULL)
2864 *lenp = sshbuf_len(ssh->state->incoming_packet);
2865 return sshbuf_ptr(ssh->state->incoming_packet);
2866}
2867
2868/* start a new packet */
2869
2870int
2871sshpkt_start(struct ssh *ssh, u_char type)
2872{
2873 u_char buf[9];
2874 int len;
2875
2876 DBG(debug("packet_start[%d]", type));
2877 len = compat20 ? 6 : 9;
2878 memset(buf, 0, len - 1);
2879 buf[len - 1] = type;
2880 sshbuf_reset(ssh->state->outgoing_packet);
2881 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2882}
2883
2884/* send it */
2885
2886int
2887sshpkt_send(struct ssh *ssh)
2888{
2889 if (compat20)
2890 return ssh_packet_send2(ssh);
2891 else
2892 return ssh_packet_send1(ssh);
2893}
2894
2895int
2896sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2897{
2898 char buf[1024];
2899 va_list args;
2900 int r;
2901
2902 va_start(args, fmt);
2903 vsnprintf(buf, sizeof(buf), fmt, args);
2904 va_end(args);
2905
2906 if (compat20) {
2907 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2908 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2909 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2910 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2911 (r = sshpkt_send(ssh)) != 0)
2912 return r;
2913 } else {
2914 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2915 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2916 (r = sshpkt_send(ssh)) != 0)
2917 return r;
2918 }
2919 return 0;
2920}
2921
2922/* roundup current message to pad bytes */
2923int
2924sshpkt_add_padding(struct ssh *ssh, u_char pad)
2925{
2926 ssh->state->extra_pad = pad;
2927 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002928}