blob: f61b32b8045f825476126ae1a9560389a55de757 [file] [log] [blame]
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001/* $OpenBSD: packet.c,v 1.224 2016/01/29 02:54:45 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000042#include <sys/param.h> /* MIN roundup */
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100044#include "openbsd-compat/sys-queue.h"
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100046#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049
Damien Miller8ec8c3e2006-07-10 20:35:38 +100050#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110051#include <netinet/ip.h>
Darren Tuckerdace2332006-09-22 19:22:17 +100052#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100055#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100056#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100057#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100058#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100059#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000060#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100061#include <signal.h>
Darren Tuckerc53c2af2013-05-16 20:28:16 +100062#include <time.h>
Darren Tucker5d196262006-07-12 22:15:16 +100063
markus@openbsd.org091c3022015-01-19 19:52:16 +000064#include <zlib.h>
65
66#include "buffer.h" /* typedefs XXX */
67#include "key.h" /* typedefs XXX */
68
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "deattack.h"
Damien Miller33b13562000-04-04 14:38:59 +100072#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100074#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110075#include "cipher.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000076#include "sshkey.h"
Damien Miller33b13562000-04-04 14:38:59 +100077#include "kex.h"
markus@openbsd.org128343b2015-01-13 19:31:40 +000078#include "digest.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000079#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "log.h"
81#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110082#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100083#include "channels.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000084#include "ssh.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000085#include "packet.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000086#include "ssherr.h"
87#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100088
89#ifdef PACKET_DEBUG
90#define DBG(x) x
91#else
92#define DBG(x)
93#endif
94
Damien Miller13ae44c2009-01-28 16:38:41 +110095#define PACKET_MAX_SIZE (256 * 1024)
96
Darren Tuckerf7288d72009-06-21 18:12:20 +100097struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100098 u_int32_t seqnr;
99 u_int32_t packets;
100 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000101 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000102};
Damien Miller13ae44c2009-01-28 16:38:41 +1100103
Damien Millera5539d22003-04-09 20:50:06 +1000104struct packet {
105 TAILQ_ENTRY(packet) next;
106 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000107 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000108};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000109
110struct session_state {
111 /*
112 * This variable contains the file descriptors used for
113 * communicating with the other side. connection_in is used for
114 * reading; connection_out for writing. These can be the same
115 * descriptor, in which case it is assumed to be a socket.
116 */
117 int connection_in;
118 int connection_out;
119
120 /* Protocol flags for the remote side. */
121 u_int remote_protocol_flags;
122
123 /* Encryption context for receiving data. Only used for decryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000124 struct sshcipher_ctx receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000125
126 /* Encryption context for sending data. Only used for encryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000127 struct sshcipher_ctx send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000128
129 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000130 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000131
132 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000133 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000134
135 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000136 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000137
138 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000139 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000140
141 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000142 struct sshbuf *compression_buffer;
143
144 /* Incoming/outgoing compression dictionaries */
145 z_stream compression_in_stream;
146 z_stream compression_out_stream;
147 int compression_in_started;
148 int compression_out_started;
149 int compression_in_failures;
150 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000151
152 /*
153 * Flag indicating whether packet compression/decompression is
154 * enabled.
155 */
156 int packet_compression;
157
158 /* default maximum packet size */
159 u_int max_packet_size;
160
161 /* Flag indicating whether this module has been initialized. */
162 int initialized;
163
164 /* Set to true if the connection is interactive. */
165 int interactive_mode;
166
167 /* Set to true if we are the server side. */
168 int server_side;
169
170 /* Set to true if we are authenticated. */
171 int after_authentication;
172
173 int keep_alive_timeouts;
174
175 /* The maximum time that we will wait to send or receive a packet */
176 int packet_timeout_ms;
177
178 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000179 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000180 struct packet_state p_read, p_send;
181
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000182 /* Volume-based rekeying */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000183 u_int64_t max_blocks_in, max_blocks_out, rekey_limit;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000184
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000185 /* Time-based rekeying */
markus@openbsd.org02db4682015-02-13 18:57:00 +0000186 u_int32_t rekey_interval; /* how often in seconds */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000187 time_t rekey_time; /* time of last rekeying */
188
Darren Tuckerf7288d72009-06-21 18:12:20 +1000189 /* Session key for protocol v1 */
190 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
191 u_int ssh1_keylen;
192
193 /* roundup current message to extra_pad bytes */
194 u_char extra_pad;
195
196 /* XXX discard incoming data after MAC error */
197 u_int packet_discard;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000198 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000199
200 /* Used in packet_read_poll2() */
201 u_int packlen;
202
Darren Tucker7b935c72009-06-21 18:59:36 +1000203 /* Used in packet_send2 */
204 int rekeying;
205
206 /* Used in packet_set_interactive */
207 int set_interactive_called;
208
209 /* Used in packet_set_maxsize */
210 int set_maxsize_called;
211
markus@openbsd.org091c3022015-01-19 19:52:16 +0000212 /* One-off warning about weak ciphers */
213 int cipher_warning_done;
214
215 /* SSH1 CRC compensation attack detector */
216 struct deattack_ctx deattack;
217
Darren Tuckerf7288d72009-06-21 18:12:20 +1000218 TAILQ_HEAD(, packet) outgoing;
219};
220
markus@openbsd.org091c3022015-01-19 19:52:16 +0000221struct ssh *
222ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000223{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000224 struct ssh *ssh = NULL;
225 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000226
markus@openbsd.org091c3022015-01-19 19:52:16 +0000227 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
228 (state = calloc(1, sizeof(*state))) == NULL ||
229 (state->input = sshbuf_new()) == NULL ||
230 (state->output = sshbuf_new()) == NULL ||
231 (state->outgoing_packet = sshbuf_new()) == NULL ||
232 (state->incoming_packet = sshbuf_new()) == NULL)
233 goto fail;
234 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000235 TAILQ_INIT(&ssh->private_keys);
236 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000237 state->connection_in = -1;
238 state->connection_out = -1;
239 state->max_packet_size = 32768;
240 state->packet_timeout_ms = -1;
241 state->p_send.packets = state->p_read.packets = 0;
242 state->initialized = 1;
243 /*
244 * ssh_packet_send2() needs to queue packets until
245 * we've done the initial key exchange.
246 */
247 state->rekeying = 1;
248 ssh->state = state;
249 return ssh;
250 fail:
251 if (state) {
252 sshbuf_free(state->input);
253 sshbuf_free(state->output);
254 sshbuf_free(state->incoming_packet);
255 sshbuf_free(state->outgoing_packet);
256 free(state);
257 }
258 free(ssh);
259 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000260}
Damien Millera5539d22003-04-09 20:50:06 +1000261
Damien Miller5428f641999-11-25 11:54:57 +1100262/*
263 * Sets the descriptors used for communication. Disables encryption until
264 * packet_set_encryption_key is called.
265 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000266struct ssh *
267ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000269 struct session_state *state;
270 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000271 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000272
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000273 if (none == NULL) {
274 error("%s: cannot load cipher 'none'", __func__);
275 return NULL;
276 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000277 if (ssh == NULL)
278 ssh = ssh_alloc_session_state();
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000279 if (ssh == NULL) {
280 error("%s: cound not allocate state", __func__);
281 return NULL;
282 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000283 state = ssh->state;
284 state->connection_in = fd_in;
285 state->connection_out = fd_out;
286 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000287 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000288 (r = cipher_init(&state->receive_context, none,
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000289 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
290 error("%s: cipher_init failed: %s", __func__, ssh_err(r));
jsg@openbsd.org1cb30162015-03-11 00:48:39 +0000291 free(ssh);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000292 return NULL;
293 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000294 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
295 deattack_init(&state->deattack);
djm@openbsd.orgd4c02952015-02-11 01:20:38 +0000296 /*
297 * Cache the IP address of the remote connection for use in error
298 * messages that might be generated after the connection has closed.
299 */
300 (void)ssh_remote_ipaddr(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000301 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302}
303
Darren Tucker3fc464e2008-06-13 06:42:45 +1000304void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000305ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000306{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000307 struct session_state *state = ssh->state;
308
Damien Miller8ed4de82011-12-19 10:52:50 +1100309 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000310 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000311 return;
312 }
313 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000314 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000315 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000316 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000317}
318
markus@openbsd.org091c3022015-01-19 19:52:16 +0000319int
320ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100321{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000322 struct session_state *state = ssh->state;
323 int r;
324
325 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100326 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000327
Damien Miller13ae44c2009-01-28 16:38:41 +1100328 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000329 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000330 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000331 if ((r = sshbuf_put(state->incoming_packet, buf,
332 sizeof(buf))) != 0)
333 return r;
334 (void) mac_compute(state->packet_discard_mac,
335 state->p_read.seqnr,
336 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
337 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100338 }
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000339 logit("Finished discarding for %.200s port %d",
340 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000341 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100342}
343
markus@openbsd.org091c3022015-01-19 19:52:16 +0000344static int
345ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
346 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100347{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000348 struct session_state *state = ssh->state;
349 int r;
350
351 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
352 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
353 return r;
354 return SSH_ERR_MAC_INVALID;
355 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100356 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000357 state->packet_discard_mac = mac;
358 if (sshbuf_len(state->input) >= discard &&
359 (r = ssh_packet_stop_discard(ssh)) != 0)
360 return r;
361 state->packet_discard = discard - sshbuf_len(state->input);
362 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100363}
364
Damien Miller34132e52000-01-14 15:45:46 +1100365/* Returns 1 if remote host is connected via socket, 0 if not. */
366
367int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000368ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100369{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000370 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100371 struct sockaddr_storage from, to;
372 socklen_t fromlen, tolen;
373
374 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000375 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100376 return 1;
377 fromlen = sizeof(from);
378 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000379 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000380 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100381 return 0;
382 tolen = sizeof(to);
383 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000384 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000385 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100386 return 0;
387 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
388 return 0;
389 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
390 return 0;
391 return 1;
392}
393
Ben Lindstromf6027d32002-03-22 01:42:04 +0000394void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000395ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000396{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000397 if (ibytes)
398 *ibytes = ssh->state->p_read.bytes;
399 if (obytes)
400 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000401}
402
403int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000404ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100405{
406 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100407 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100408
409 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000410 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000411 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100412 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000413#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100414 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000415 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000416 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000417#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000418 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100419}
420
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421/* Sets the connection into non-blocking mode. */
422
423void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000424ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425{
Damien Miller95def091999-11-25 00:26:21 +1100426 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000427 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
markus@openbsd.org091c3022015-01-19 19:52:16 +0000429 if (ssh->state->connection_out != ssh->state->connection_in)
430 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431}
432
433/* Returns the socket used for reading. */
434
435int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000436ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000438 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439}
440
441/* Returns the descriptor used for writing. */
442
443int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000444ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000446 return ssh->state->connection_out;
447}
448
449/*
450 * Returns the IP-address of the remote host as a string. The returned
451 * string must not be freed.
452 */
453
454const char *
455ssh_remote_ipaddr(struct ssh *ssh)
456{
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000457 const int sock = ssh->state->connection_in;
458
markus@openbsd.org091c3022015-01-19 19:52:16 +0000459 /* Check whether we have cached the ipaddr. */
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000460 if (ssh->remote_ipaddr == NULL) {
461 if (ssh_packet_connection_is_on_socket(ssh)) {
462 ssh->remote_ipaddr = get_peer_ipaddr(sock);
463 ssh->remote_port = get_sock_port(sock, 0);
464 } else {
465 ssh->remote_ipaddr = strdup("UNKNOWN");
466 ssh->remote_port = 0;
467 }
468 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000469 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470}
471
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000472/* Returns the port number of the remote host. */
473
474int
475ssh_remote_port(struct ssh *ssh)
476{
477 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
478 return ssh->remote_port;
479}
480
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481/* Closes the connection and clears and frees internal data structures. */
482
483void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000484ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000486 struct session_state *state = ssh->state;
487 int r;
488 u_int mode;
489
490 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100491 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000492 state->initialized = 0;
493 if (state->connection_in == state->connection_out) {
494 shutdown(state->connection_out, SHUT_RDWR);
495 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100496 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000497 close(state->connection_in);
498 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100499 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000500 sshbuf_free(state->input);
501 sshbuf_free(state->output);
502 sshbuf_free(state->outgoing_packet);
503 sshbuf_free(state->incoming_packet);
504 for (mode = 0; mode < MODE_MAX; mode++)
505 kex_free_newkeys(state->newkeys[mode]);
506 if (state->compression_buffer) {
507 sshbuf_free(state->compression_buffer);
508 if (state->compression_out_started) {
509 z_streamp stream = &state->compression_out_stream;
510 debug("compress outgoing: "
511 "raw data %llu, compressed %llu, factor %.2f",
512 (unsigned long long)stream->total_in,
513 (unsigned long long)stream->total_out,
514 stream->total_in == 0 ? 0.0 :
515 (double) stream->total_out / stream->total_in);
516 if (state->compression_out_failures == 0)
517 deflateEnd(stream);
518 }
519 if (state->compression_in_started) {
520 z_streamp stream = &state->compression_out_stream;
521 debug("compress incoming: "
522 "raw data %llu, compressed %llu, factor %.2f",
523 (unsigned long long)stream->total_out,
524 (unsigned long long)stream->total_in,
525 stream->total_out == 0 ? 0.0 :
526 (double) stream->total_in / stream->total_out);
527 if (state->compression_in_failures == 0)
528 inflateEnd(stream);
529 }
Damien Miller95def091999-11-25 00:26:21 +1100530 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000531 if ((r = cipher_cleanup(&state->send_context)) != 0)
532 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
533 if ((r = cipher_cleanup(&state->receive_context)) != 0)
534 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000535 free(ssh->remote_ipaddr);
536 ssh->remote_ipaddr = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000537 free(ssh->state);
538 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539}
540
541/* Sets remote side protocol flags. */
542
543void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000544ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000546 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547}
548
549/* Returns the remote protocol flags set earlier by the above function. */
550
Ben Lindstrom46c16222000-12-22 01:43:59 +0000551u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000552ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000554 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555}
556
Damien Miller5428f641999-11-25 11:54:57 +1100557/*
558 * Starts packet compression from the next packet on in both directions.
559 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
560 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561
markus@openbsd.org091c3022015-01-19 19:52:16 +0000562static int
563ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000564{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000565 if (!ssh->state->compression_buffer &&
566 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
567 return SSH_ERR_ALLOC_FAIL;
568 return 0;
569}
570
571static int
572start_compression_out(struct ssh *ssh, int level)
573{
574 if (level < 1 || level > 9)
575 return SSH_ERR_INVALID_ARGUMENT;
576 debug("Enabling compression at level %d.", level);
577 if (ssh->state->compression_out_started == 1)
578 deflateEnd(&ssh->state->compression_out_stream);
579 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
580 case Z_OK:
581 ssh->state->compression_out_started = 1;
582 break;
583 case Z_MEM_ERROR:
584 return SSH_ERR_ALLOC_FAIL;
585 default:
586 return SSH_ERR_INTERNAL_ERROR;
587 }
588 return 0;
589}
590
591static int
592start_compression_in(struct ssh *ssh)
593{
594 if (ssh->state->compression_in_started == 1)
595 inflateEnd(&ssh->state->compression_in_stream);
596 switch (inflateInit(&ssh->state->compression_in_stream)) {
597 case Z_OK:
598 ssh->state->compression_in_started = 1;
599 break;
600 case Z_MEM_ERROR:
601 return SSH_ERR_ALLOC_FAIL;
602 default:
603 return SSH_ERR_INTERNAL_ERROR;
604 }
605 return 0;
606}
607
608int
609ssh_packet_start_compression(struct ssh *ssh, int level)
610{
611 int r;
612
613 if (ssh->state->packet_compression && !compat20)
614 return SSH_ERR_INTERNAL_ERROR;
615 ssh->state->packet_compression = 1;
616 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
617 (r = start_compression_in(ssh)) != 0 ||
618 (r = start_compression_out(ssh, level)) != 0)
619 return r;
620 return 0;
621}
622
623/* XXX remove need for separate compression buffer */
624static int
625compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
626{
627 u_char buf[4096];
628 int r, status;
629
630 if (ssh->state->compression_out_started != 1)
631 return SSH_ERR_INTERNAL_ERROR;
632
633 /* This case is not handled below. */
634 if (sshbuf_len(in) == 0)
635 return 0;
636
637 /* Input is the contents of the input buffer. */
638 if ((ssh->state->compression_out_stream.next_in =
639 sshbuf_mutable_ptr(in)) == NULL)
640 return SSH_ERR_INTERNAL_ERROR;
641 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
642
643 /* Loop compressing until deflate() returns with avail_out != 0. */
644 do {
645 /* Set up fixed-size output buffer. */
646 ssh->state->compression_out_stream.next_out = buf;
647 ssh->state->compression_out_stream.avail_out = sizeof(buf);
648
649 /* Compress as much data into the buffer as possible. */
650 status = deflate(&ssh->state->compression_out_stream,
651 Z_PARTIAL_FLUSH);
652 switch (status) {
653 case Z_MEM_ERROR:
654 return SSH_ERR_ALLOC_FAIL;
655 case Z_OK:
656 /* Append compressed data to output_buffer. */
657 if ((r = sshbuf_put(out, buf, sizeof(buf) -
658 ssh->state->compression_out_stream.avail_out)) != 0)
659 return r;
660 break;
661 case Z_STREAM_ERROR:
662 default:
663 ssh->state->compression_out_failures++;
664 return SSH_ERR_INVALID_FORMAT;
665 }
666 } while (ssh->state->compression_out_stream.avail_out == 0);
667 return 0;
668}
669
670static int
671uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
672{
673 u_char buf[4096];
674 int r, status;
675
676 if (ssh->state->compression_in_started != 1)
677 return SSH_ERR_INTERNAL_ERROR;
678
679 if ((ssh->state->compression_in_stream.next_in =
680 sshbuf_mutable_ptr(in)) == NULL)
681 return SSH_ERR_INTERNAL_ERROR;
682 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
683
684 for (;;) {
685 /* Set up fixed-size output buffer. */
686 ssh->state->compression_in_stream.next_out = buf;
687 ssh->state->compression_in_stream.avail_out = sizeof(buf);
688
689 status = inflate(&ssh->state->compression_in_stream,
690 Z_PARTIAL_FLUSH);
691 switch (status) {
692 case Z_OK:
693 if ((r = sshbuf_put(out, buf, sizeof(buf) -
694 ssh->state->compression_in_stream.avail_out)) != 0)
695 return r;
696 break;
697 case Z_BUF_ERROR:
698 /*
699 * Comments in zlib.h say that we should keep calling
700 * inflate() until we get an error. This appears to
701 * be the error that we get.
702 */
703 return 0;
704 case Z_DATA_ERROR:
705 return SSH_ERR_INVALID_FORMAT;
706 case Z_MEM_ERROR:
707 return SSH_ERR_ALLOC_FAIL;
708 case Z_STREAM_ERROR:
709 default:
710 ssh->state->compression_in_failures++;
711 return SSH_ERR_INTERNAL_ERROR;
712 }
713 }
714 /* NOTREACHED */
715}
716
717/* Serialise compression state into a blob for privsep */
718static int
719ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
720{
721 struct session_state *state = ssh->state;
722 struct sshbuf *b;
723 int r;
724
725 if ((b = sshbuf_new()) == NULL)
726 return SSH_ERR_ALLOC_FAIL;
727 if (state->compression_in_started) {
728 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
729 sizeof(state->compression_in_stream))) != 0)
730 goto out;
731 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
732 goto out;
733 if (state->compression_out_started) {
734 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
735 sizeof(state->compression_out_stream))) != 0)
736 goto out;
737 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
738 goto out;
739 r = sshbuf_put_stringb(m, b);
740 out:
741 sshbuf_free(b);
742 return r;
743}
744
745/* Deserialise compression state from a blob for privsep */
746static int
747ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
748{
749 struct session_state *state = ssh->state;
750 struct sshbuf *b = NULL;
751 int r;
752 const u_char *inblob, *outblob;
753 size_t inl, outl;
754
755 if ((r = sshbuf_froms(m, &b)) != 0)
756 goto out;
757 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
758 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
759 goto out;
760 if (inl == 0)
761 state->compression_in_started = 0;
762 else if (inl != sizeof(state->compression_in_stream)) {
763 r = SSH_ERR_INTERNAL_ERROR;
764 goto out;
765 } else {
766 state->compression_in_started = 1;
767 memcpy(&state->compression_in_stream, inblob, inl);
768 }
769 if (outl == 0)
770 state->compression_out_started = 0;
771 else if (outl != sizeof(state->compression_out_stream)) {
772 r = SSH_ERR_INTERNAL_ERROR;
773 goto out;
774 } else {
775 state->compression_out_started = 1;
776 memcpy(&state->compression_out_stream, outblob, outl);
777 }
778 r = 0;
779 out:
780 sshbuf_free(b);
781 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000782}
783
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000785ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
786 void *(*allocfunc)(void *, u_int, u_int),
787 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000789 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
790 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
791 ssh->state->compression_out_stream.opaque = ctx;
792 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
793 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
794 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795}
796
Damien Miller5428f641999-11-25 11:54:57 +1100797/*
Damien Miller5428f641999-11-25 11:54:57 +1100798 * Causes any further packets to be encrypted using the given key. The same
799 * key is used for both sending and reception. However, both directions are
800 * encrypted independently of each other.
801 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000802
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000804ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000806#ifndef WITH_SSH1
807 fatal("no SSH protocol 1 support");
808#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000809 struct session_state *state = ssh->state;
810 const struct sshcipher *cipher = cipher_by_number(number);
811 int r;
812 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100813
markus@openbsd.org091c3022015-01-19 19:52:16 +0000814 if (cipher == NULL)
815 fatal("%s: unknown cipher number %d", __func__, number);
816 if (keylen < 20)
817 fatal("%s: keylen too small: %d", __func__, keylen);
818 if (keylen > SSH_SESSION_KEY_LENGTH)
819 fatal("%s: keylen too big: %d", __func__, keylen);
820 memcpy(state->ssh1_key, key, keylen);
821 state->ssh1_keylen = keylen;
822 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
823 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
824 (r = cipher_init(&state->receive_context, cipher, key, keylen,
825 NULL, 0, CIPHER_DECRYPT) != 0))
826 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
827 if (!state->cipher_warning_done &&
828 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
829 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
830 error("Warning: %s", wmsg);
831 state->cipher_warning_done = 1;
832 }
Damien Miller773dda22015-01-30 23:10:17 +1100833#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000834}
835
Damien Miller5428f641999-11-25 11:54:57 +1100836/*
837 * Finalizes and sends the packet. If the encryption key has been set,
838 * encrypts the packet before sending.
839 */
Damien Miller95def091999-11-25 00:26:21 +1100840
markus@openbsd.org091c3022015-01-19 19:52:16 +0000841int
842ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000844 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000845 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000846 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000847 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
Damien Miller5428f641999-11-25 11:54:57 +1100849 /*
850 * If using packet compression, compress the payload of the outgoing
851 * packet.
852 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000853 if (state->packet_compression) {
854 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100855 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000856 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
857 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100858 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000859 if ((r = sshbuf_put(state->compression_buffer,
860 "\0\0\0\0\0\0\0\0", 8)) != 0)
861 goto out;
862 if ((r = compress_buffer(ssh, state->outgoing_packet,
863 state->compression_buffer)) != 0)
864 goto out;
865 sshbuf_reset(state->outgoing_packet);
866 if ((r = sshbuf_putb(state->outgoing_packet,
867 state->compression_buffer)) != 0)
868 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100869 }
870 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000871 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872
Damien Millere247cc42000-05-07 12:03:14 +1000873 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100874 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000875 if (!state->send_context.plaintext) {
876 cp = sshbuf_mutable_ptr(state->outgoing_packet);
877 if (cp == NULL) {
878 r = SSH_ERR_INTERNAL_ERROR;
879 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100880 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000881 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100882 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000883 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
884 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100885
886 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000887 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
888 sshbuf_len(state->outgoing_packet));
889 POKE_U32(buf, checksum);
890 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
891 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
893#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100894 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000895 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896#endif
897
Damien Miller95def091999-11-25 00:26:21 +1100898 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000899 POKE_U32(buf, len);
900 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
901 goto out;
902 if ((r = sshbuf_reserve(state->output,
903 sshbuf_len(state->outgoing_packet), &cp)) != 0)
904 goto out;
905 if ((r = cipher_crypt(&state->send_context, 0, cp,
906 sshbuf_ptr(state->outgoing_packet),
907 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
908 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100909
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100911 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000912 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000914 state->p_send.packets++;
915 state->p_send.bytes += len +
916 sshbuf_len(state->outgoing_packet);
917 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918
Damien Miller5428f641999-11-25 11:54:57 +1100919 /*
Damien Miller788f2122005-11-05 15:14:59 +1100920 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000921 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
922 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100923 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000924 r = 0;
925 out:
926 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927}
928
markus@openbsd.org091c3022015-01-19 19:52:16 +0000929int
930ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000931{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000932 struct session_state *state = ssh->state;
933 struct sshenc *enc;
934 struct sshmac *mac;
935 struct sshcomp *comp;
936 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000937 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000938 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000939 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000940
Ben Lindstrom064496f2002-12-23 02:04:22 +0000941 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000942
Damien Miller963f6b22002-02-19 15:21:23 +1100943 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000944 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000945 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000946 state->p_send.packets = state->p_send.blocks = 0;
947 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100948 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000949 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000950 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000951 state->p_read.packets = state->p_read.blocks = 0;
952 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100953 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000954 if (state->newkeys[mode] != NULL) {
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000955 debug("set_newkeys: rekeying, input %llu bytes %llu blocks, "
956 "output %llu bytes %llu blocks",
957 state->p_read.bytes, state->p_read.blocks,
958 state->p_send.bytes, state->p_send.blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000959 if ((r = cipher_cleanup(cc)) != 0)
960 return r;
961 enc = &state->newkeys[mode]->enc;
962 mac = &state->newkeys[mode]->mac;
963 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000964 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100965 explicit_bzero(enc->iv, enc->iv_len);
966 explicit_bzero(enc->key, enc->key_len);
967 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000968 free(enc->name);
969 free(enc->iv);
970 free(enc->key);
971 free(mac->name);
972 free(mac->key);
973 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000974 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000975 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000976 /* move newkeys from kex to state */
977 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
978 return SSH_ERR_INTERNAL_ERROR;
979 ssh->kex->newkeys[mode] = NULL;
980 enc = &state->newkeys[mode]->enc;
981 mac = &state->newkeys[mode]->mac;
982 comp = &state->newkeys[mode]->comp;
983 if (cipher_authlen(enc->cipher) == 0) {
984 if ((r = mac_init(mac)) != 0)
985 return r;
986 }
987 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000988 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000989 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
990 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000991 return r;
992 if (!state->cipher_warning_done &&
993 (wmsg = cipher_warning_message(cc)) != NULL) {
994 error("Warning: %s", wmsg);
995 state->cipher_warning_done = 1;
996 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000997 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100998 /* explicit_bzero(enc->iv, enc->block_size);
999 explicit_bzero(enc->key, enc->key_len);
1000 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +10001001 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +10001002 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +00001003 state->after_authentication)) && comp->enabled == 0) {
1004 if ((r = ssh_packet_init_compression(ssh)) < 0)
1005 return r;
1006 if (mode == MODE_OUT) {
1007 if ((r = start_compression_out(ssh, 6)) != 0)
1008 return r;
1009 } else {
1010 if ((r = start_compression_in(ssh)) != 0)
1011 return r;
1012 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001013 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001014 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001015 /*
1016 * The 2^(blocksize*2) limit is too expensive for 3DES,
1017 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1018 */
1019 if (enc->block_size >= 16)
1020 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1021 else
1022 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001023 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001024 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001025 state->rekey_limit / enc->block_size);
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001026 debug("rekey after %llu blocks", *max_blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001027 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001028}
1029
Damien Miller5428f641999-11-25 11:54:57 +11001030/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001031 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001032 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001033 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1034 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001035static int
1036ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001037{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001038 struct session_state *state = ssh->state;
1039 struct sshcomp *comp = NULL;
1040 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001041
1042 /*
1043 * Remember that we are past the authentication step, so rekeying
1044 * with COMP_DELAYED will turn on compression immediately.
1045 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001046 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001047 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001048 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001049 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001050 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001051 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001052 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001053 if ((r = ssh_packet_init_compression(ssh)) != 0)
1054 return r;
1055 if (mode == MODE_OUT) {
1056 if ((r = start_compression_out(ssh, 6)) != 0)
1057 return r;
1058 } else {
1059 if ((r = start_compression_in(ssh)) != 0)
1060 return r;
1061 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001062 comp->enabled = 1;
1063 }
1064 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001065 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001066}
1067
1068/*
Damien Miller33b13562000-04-04 14:38:59 +10001069 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1070 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001071int
1072ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001073{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001074 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001075 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001076 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001077 u_int authlen = 0, aadlen = 0;
1078 u_int len;
1079 struct sshenc *enc = NULL;
1080 struct sshmac *mac = NULL;
1081 struct sshcomp *comp = NULL;
1082 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001083
markus@openbsd.org091c3022015-01-19 19:52:16 +00001084 if (state->newkeys[MODE_OUT] != NULL) {
1085 enc = &state->newkeys[MODE_OUT]->enc;
1086 mac = &state->newkeys[MODE_OUT]->mac;
1087 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001088 /* disable mac for authenticated encryption */
1089 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1090 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001091 }
Damien Miller963f6b22002-02-19 15:21:23 +11001092 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001093 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001094
markus@openbsd.org091c3022015-01-19 19:52:16 +00001095 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001096
1097#ifdef PACKET_DEBUG
1098 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001099 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001100#endif
1101
1102 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001103 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001104 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001105 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1106 goto out;
1107 sshbuf_reset(state->compression_buffer);
1108 if ((r = compress_buffer(ssh, state->outgoing_packet,
1109 state->compression_buffer)) != 0)
1110 goto out;
1111 sshbuf_reset(state->outgoing_packet);
1112 if ((r = sshbuf_put(state->outgoing_packet,
1113 "\0\0\0\0\0", 5)) != 0 ||
1114 (r = sshbuf_putb(state->outgoing_packet,
1115 state->compression_buffer)) != 0)
1116 goto out;
1117 DBG(debug("compression: raw %d compressed %zd", len,
1118 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001119 }
1120
1121 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001122 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001123
1124 /*
1125 * calc size of padding, alloc space, get random data,
1126 * minimum padding is 4 bytes
1127 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001128 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001129 padlen = block_size - (len % block_size);
1130 if (padlen < 4)
1131 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001132 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001133 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001134 state->extra_pad =
1135 roundup(state->extra_pad, block_size);
1136 pad = state->extra_pad -
1137 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001138 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001139 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001140 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001141 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001142 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001143 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1144 goto out;
1145 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001146 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001147 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001148 } else {
1149 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001150 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001151 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001152 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001153 len = sshbuf_len(state->outgoing_packet);
1154 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1155 if (cp == NULL) {
1156 r = SSH_ERR_INTERNAL_ERROR;
1157 goto out;
1158 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001159 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001160 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001161 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001162 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1163 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001164
1165 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001166 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001167 if ((r = mac_compute(mac, state->p_send.seqnr,
1168 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001169 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001170 goto out;
1171 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001172 }
1173 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001174 if ((r = sshbuf_reserve(state->output,
1175 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1176 goto out;
1177 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1178 sshbuf_ptr(state->outgoing_packet),
1179 len - aadlen, aadlen, authlen)) != 0)
1180 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001181 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001182 if (mac && mac->enabled) {
1183 if (mac->etm) {
1184 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001185 if ((r = mac_compute(mac, state->p_send.seqnr,
1186 cp, len, macbuf, sizeof(macbuf))) != 0)
1187 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001188 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001189 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001190 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001191 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1192 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001193 }
Damien Miller33b13562000-04-04 14:38:59 +10001194#ifdef PACKET_DEBUG
1195 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001196 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001197#endif
Damien Miller4af51302000-04-16 11:18:38 +10001198 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001199 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001200 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001201 if (++state->p_send.packets == 0)
1202 if (!(ssh->compat & SSH_BUG_NOREKEY))
1203 return SSH_ERR_NEED_REKEY;
1204 state->p_send.blocks += len / block_size;
1205 state->p_send.bytes += len;
1206 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001207
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001208 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001209 r = ssh_set_newkeys(ssh, MODE_OUT);
1210 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1211 r = ssh_packet_enable_delayed_compress(ssh);
1212 else
1213 r = 0;
1214 out:
1215 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001216}
1217
markus@openbsd.org091c3022015-01-19 19:52:16 +00001218int
1219ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001220{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001221 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001222 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001223 u_char type;
1224 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001225
markus@openbsd.org091c3022015-01-19 19:52:16 +00001226 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001227
1228 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001229 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001230 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1231 (type > SSH2_MSG_TRANSPORT_MAX) ||
1232 (type == SSH2_MSG_SERVICE_REQUEST) ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001233 (type == SSH2_MSG_SERVICE_ACCEPT) ||
1234 (type == SSH2_MSG_EXT_INFO)) {
Damien Millera5539d22003-04-09 20:50:06 +10001235 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001236 p = calloc(1, sizeof(*p));
1237 if (p == NULL)
1238 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001239 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001240 p->payload = state->outgoing_packet;
1241 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1242 state->outgoing_packet = sshbuf_new();
1243 if (state->outgoing_packet == NULL)
1244 return SSH_ERR_ALLOC_FAIL;
1245 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001246 }
1247 }
1248
1249 /* rekeying starts with sending KEXINIT */
1250 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001251 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001252
markus@openbsd.org091c3022015-01-19 19:52:16 +00001253 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1254 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001255
1256 /* after a NEWKEYS message we can send the complete queue */
1257 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001258 state->rekeying = 0;
1259 state->rekey_time = monotime();
1260 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001261 type = p->type;
1262 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001263 sshbuf_free(state->outgoing_packet);
1264 state->outgoing_packet = p->payload;
1265 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001266 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001267 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1268 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001269 }
1270 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001271 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001272}
1273
Damien Miller33b13562000-04-04 14:38:59 +10001274/*
Damien Miller5428f641999-11-25 11:54:57 +11001275 * Waits until a packet has been received, and returns its type. Note that
1276 * no other data is processed until this returns, so this function should not
1277 * be used during the interactive session.
1278 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001279
1280int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001281ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001282{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001283 struct session_state *state = ssh->state;
markus@openbsd.orga3068632016-01-14 16:17:39 +00001284 int len, r, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001285 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001286 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001287 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001288
Darren Tucker99bb7612008-06-13 22:02:50 +10001289 DBG(debug("packet_read()"));
1290
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001291 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001292 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001293 if (setp == NULL)
1294 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001295
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001296 /*
1297 * Since we are blocking, ensure that all written packets have
1298 * been sent.
1299 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001300 if ((r = ssh_packet_write_wait(ssh)) != 0)
1301 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302
Damien Miller95def091999-11-25 00:26:21 +11001303 /* Stay in the loop until we have received a complete packet. */
1304 for (;;) {
1305 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001306 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1307 if (r != 0)
1308 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001309 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001310 *typep == SSH_SMSG_SUCCESS
1311 || *typep == SSH_SMSG_FAILURE
1312 || *typep == SSH_CMSG_EOF
1313 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1314 if ((r = sshpkt_get_end(ssh)) != 0)
1315 break;
Damien Miller95def091999-11-25 00:26:21 +11001316 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001317 if (*typep != SSH_MSG_NONE)
1318 break;
Damien Miller5428f641999-11-25 11:54:57 +11001319 /*
1320 * Otherwise, wait for some data to arrive, add it to the
1321 * buffer, and try again.
1322 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001323 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001324 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001325 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001326
markus@openbsd.org091c3022015-01-19 19:52:16 +00001327 if (state->packet_timeout_ms > 0) {
1328 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001329 timeoutp = &timeout;
1330 }
Damien Miller95def091999-11-25 00:26:21 +11001331 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001332 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001333 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001334 ms_to_timeval(&timeout, ms_remain);
1335 gettimeofday(&start, NULL);
1336 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001337 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001338 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001339 break;
Damien Millerea437422009-10-02 11:49:03 +10001340 if (errno != EAGAIN && errno != EINTR &&
1341 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001342 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001343 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001344 continue;
1345 ms_subtract_diff(&start, &ms_remain);
1346 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001347 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001348 break;
1349 }
1350 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001351 if (r == 0)
1352 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001353 /* Read data from the socket. */
markus@openbsd.orga3068632016-01-14 16:17:39 +00001354 len = read(state->connection_in, buf, sizeof(buf));
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001355 if (len == 0) {
1356 r = SSH_ERR_CONN_CLOSED;
1357 goto out;
1358 }
1359 if (len < 0) {
1360 r = SSH_ERR_SYSTEM_ERROR;
1361 goto out;
1362 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001363
Damien Miller95def091999-11-25 00:26:21 +11001364 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001365 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001366 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001367 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001368 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001369 free(setp);
1370 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001371}
1372
Damien Miller278f9072001-12-21 15:00:19 +11001373int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001374ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001375{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001376 u_char type;
1377 int r;
1378
1379 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1380 fatal("%s: %s", __func__, ssh_err(r));
1381 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001382}
1383
Damien Miller5428f641999-11-25 11:54:57 +11001384/*
1385 * Waits until a packet has been received, verifies that its type matches
1386 * that given, and gives a fatal error and exits if there is a mismatch.
1387 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001388
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001389int
1390ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001392 int r;
1393 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001395 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1396 return r;
1397 if (type != expected_type) {
1398 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001399 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001400 expected_type, type)) != 0)
1401 return r;
1402 return SSH_ERR_PROTOCOL_ERROR;
1403 }
1404 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405}
1406
1407/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001408 * packet_process_incoming. If so, reads the packet; otherwise returns
1409 * SSH_MSG_NONE. This does not wait for data from the connection.
1410 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001411 * SSH_MSG_DISCONNECT is handled specially here. Also,
1412 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1413 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001414 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001415
markus@openbsd.org091c3022015-01-19 19:52:16 +00001416int
1417ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001419 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001420 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001421 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001422 const u_char *cp;
1423 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001424 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001425 int r;
1426
1427 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428
Damien Miller95def091999-11-25 00:26:21 +11001429 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001430 if (sshbuf_len(state->input) < 4 + 8)
1431 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001432 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001433 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001434 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1435 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1436 len)) != 0)
1437 return r;
1438 return SSH_ERR_CONN_CORRUPT;
1439 }
Damien Miller95def091999-11-25 00:26:21 +11001440 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441
Damien Miller95def091999-11-25 00:26:21 +11001442 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001443 if (sshbuf_len(state->input) < 4 + padded_len)
1444 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445
Damien Miller95def091999-11-25 00:26:21 +11001446 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
Damien Miller95def091999-11-25 00:26:21 +11001448 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001449 if ((r = sshbuf_consume(state->input, 4)) != 0)
1450 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001451
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001452 /*
1453 * Cryptographic attack detector for ssh
1454 * (C)1998 CORE-SDI, Buenos Aires Argentina
1455 * Ariel Futoransky(futo@core-sdi.com)
1456 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001457 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001458 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001459 switch (detect_attack(&state->deattack,
1460 sshbuf_ptr(state->input), padded_len)) {
1461 case DEATTACK_OK:
1462 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001463 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001464 emsg = "crc32 compensation attack detected";
1465 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001466 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001467 emsg = "deattack denial of service detected";
1468 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001469 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001470 emsg = "deattack error";
1471 break;
1472 }
1473 if (emsg != NULL) {
1474 error("%s", emsg);
1475 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1476 (r = ssh_packet_write_wait(ssh)) != 0)
1477 return r;
1478 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001479 }
1480 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001481
1482 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001483 sshbuf_reset(state->incoming_packet);
1484 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1485 goto out;
1486 if ((r = cipher_crypt(&state->receive_context, 0, p,
1487 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1488 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001489
markus@openbsd.org091c3022015-01-19 19:52:16 +00001490 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1491 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492
1493#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001494 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001495 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001496#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497
Damien Miller95def091999-11-25 00:26:21 +11001498 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001499 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1500 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001501
Damien Miller95def091999-11-25 00:26:21 +11001502 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001503 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1504 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001505
Damien Miller95def091999-11-25 00:26:21 +11001506 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001507 if (len != sshbuf_len(state->incoming_packet)) {
1508 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001509 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001510 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1511 (r = ssh_packet_write_wait(ssh)) != 0)
1512 return r;
1513 return SSH_ERR_CONN_CORRUPT;
1514 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515
markus@openbsd.org091c3022015-01-19 19:52:16 +00001516 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1517 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001518 if (checksum != stored_checksum) {
1519 error("Corrupted check bytes on input");
1520 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1521 (r = ssh_packet_write_wait(ssh)) != 0)
1522 return r;
1523 return SSH_ERR_CONN_CORRUPT;
1524 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001525 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1526 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001527
markus@openbsd.org091c3022015-01-19 19:52:16 +00001528 if (state->packet_compression) {
1529 sshbuf_reset(state->compression_buffer);
1530 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1531 state->compression_buffer)) != 0)
1532 goto out;
1533 sshbuf_reset(state->incoming_packet);
1534 if ((r = sshbuf_putb(state->incoming_packet,
1535 state->compression_buffer)) != 0)
1536 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001537 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001538 state->p_read.packets++;
1539 state->p_read.bytes += padded_len + 4;
1540 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1541 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001542 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1543 error("Invalid ssh1 packet type: %d", *typep);
1544 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1545 (r = ssh_packet_write_wait(ssh)) != 0)
1546 return r;
1547 return SSH_ERR_PROTOCOL_ERROR;
1548 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001549 r = 0;
1550 out:
1551 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552}
Damien Miller95def091999-11-25 00:26:21 +11001553
markus@openbsd.org091c3022015-01-19 19:52:16 +00001554int
1555ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001556{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001557 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001558 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001559 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1560 u_int maclen, aadlen = 0, authlen = 0, block_size;
1561 struct sshenc *enc = NULL;
1562 struct sshmac *mac = NULL;
1563 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001564 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001565
markus@openbsd.org091c3022015-01-19 19:52:16 +00001566 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001567
markus@openbsd.org091c3022015-01-19 19:52:16 +00001568 if (state->packet_discard)
1569 return 0;
1570
1571 if (state->newkeys[MODE_IN] != NULL) {
1572 enc = &state->newkeys[MODE_IN]->enc;
1573 mac = &state->newkeys[MODE_IN]->mac;
1574 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001575 /* disable mac for authenticated encryption */
1576 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1577 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001578 }
1579 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001580 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001581 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001582
markus@openbsd.org091c3022015-01-19 19:52:16 +00001583 if (aadlen && state->packlen == 0) {
1584 if (cipher_get_length(&state->receive_context,
1585 &state->packlen, state->p_read.seqnr,
1586 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1587 return 0;
1588 if (state->packlen < 1 + 4 ||
1589 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001590#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001591 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001592#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001593 logit("Bad packet length %u.", state->packlen);
1594 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1595 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001596 return SSH_ERR_CONN_CORRUPT;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001597 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001598 sshbuf_reset(state->incoming_packet);
1599 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001600 /*
1601 * check if input size is less than the cipher block size,
1602 * decrypt first block and extract length of incoming packet
1603 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001604 if (sshbuf_len(state->input) < block_size)
1605 return 0;
1606 sshbuf_reset(state->incoming_packet);
1607 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1608 &cp)) != 0)
1609 goto out;
1610 if ((r = cipher_crypt(&state->receive_context,
1611 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1612 block_size, 0, 0)) != 0)
1613 goto out;
1614 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1615 if (state->packlen < 1 + 4 ||
1616 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001617#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001618 fprintf(stderr, "input: \n");
1619 sshbuf_dump(state->input, stderr);
1620 fprintf(stderr, "incoming_packet: \n");
1621 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001622#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001623 logit("Bad packet length %u.", state->packlen);
1624 return ssh_packet_start_discard(ssh, enc, mac,
1625 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001626 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001627 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1628 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001629 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001630 DBG(debug("input: packet len %u", state->packlen+4));
1631
Damien Milleraf43a7a2012-12-12 10:46:31 +11001632 if (aadlen) {
1633 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001634 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001635 } else {
1636 /*
1637 * the payload size and the payload are encrypted, but we
1638 * have a partial packet of block_size bytes
1639 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001640 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001641 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001642 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1643 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001644 if (need % block_size != 0) {
1645 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001646 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001647 return ssh_packet_start_discard(ssh, enc, mac,
1648 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001649 }
Damien Miller33b13562000-04-04 14:38:59 +10001650 /*
1651 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001652 * decrypt into incoming_packet:
1653 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001654 * 'need' bytes are encrypted, followed by either
1655 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001656 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001657 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001658 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1659 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001660#ifdef PACKET_DEBUG
1661 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001662 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001663#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001664 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001665 if (mac && mac->enabled && mac->etm) {
1666 if ((r = mac_compute(mac, state->p_read.seqnr,
1667 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001668 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001669 goto out;
1670 }
1671 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1672 &cp)) != 0)
1673 goto out;
1674 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1675 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1676 goto out;
1677 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1678 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001679 /*
1680 * compute MAC over seqnr and packet,
1681 * increment sequence number for incoming packet
1682 */
Damien Miller4af51302000-04-16 11:18:38 +10001683 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001684 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001685 if ((r = mac_compute(mac, state->p_read.seqnr,
1686 sshbuf_ptr(state->incoming_packet),
1687 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001688 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001689 goto out;
1690 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001691 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001692 logit("Corrupted MAC on input.");
1693 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001694 return SSH_ERR_INTERNAL_ERROR;
1695 return ssh_packet_start_discard(ssh, enc, mac,
1696 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001697 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001698
1699 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1700 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1701 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001702 }
Damien Miller278f9072001-12-21 15:00:19 +11001703 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001704 *seqnr_p = state->p_read.seqnr;
1705 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001706 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001707 if (++state->p_read.packets == 0)
1708 if (!(ssh->compat & SSH_BUG_NOREKEY))
1709 return SSH_ERR_NEED_REKEY;
1710 state->p_read.blocks += (state->packlen + 4) / block_size;
1711 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001712
1713 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001714 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001715 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001716 if (padlen < 4) {
1717 if ((r = sshpkt_disconnect(ssh,
1718 "Corrupted padlen %d on input.", padlen)) != 0 ||
1719 (r = ssh_packet_write_wait(ssh)) != 0)
1720 return r;
1721 return SSH_ERR_CONN_CORRUPT;
1722 }
Damien Miller33b13562000-04-04 14:38:59 +10001723
1724 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001725 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1726 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1727 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001728
markus@openbsd.org091c3022015-01-19 19:52:16 +00001729 DBG(debug("input: len before de-compress %zd",
1730 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001731 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001732 sshbuf_reset(state->compression_buffer);
1733 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1734 state->compression_buffer)) != 0)
1735 goto out;
1736 sshbuf_reset(state->incoming_packet);
1737 if ((r = sshbuf_putb(state->incoming_packet,
1738 state->compression_buffer)) != 0)
1739 goto out;
1740 DBG(debug("input: len after de-compress %zd",
1741 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001742 }
1743 /*
1744 * get packet type, implies consume.
1745 * return length of payload (without type field)
1746 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001747 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1748 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001749 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1750 if ((r = sshpkt_disconnect(ssh,
1751 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1752 (r = ssh_packet_write_wait(ssh)) != 0)
1753 return r;
1754 return SSH_ERR_PROTOCOL_ERROR;
1755 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001756 if (*typep == SSH2_MSG_NEWKEYS)
1757 r = ssh_set_newkeys(ssh, MODE_IN);
1758 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1759 r = ssh_packet_enable_delayed_compress(ssh);
1760 else
1761 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001762#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001763 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1764 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001765#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001766 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001767 state->packlen = 0;
1768 out:
1769 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001770}
1771
1772int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001773ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001774{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001775 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001776 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001777 int r;
1778 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001779
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001780 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001781 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001782 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001783 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1784 if (r != 0)
1785 return r;
1786 if (*typep) {
1787 state->keep_alive_timeouts = 0;
1788 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001789 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001790 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001791 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001792 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001793 break;
Damien Miller33b13562000-04-04 14:38:59 +10001794 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001795 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1796 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1797 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001798 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001799 return r;
1800 }
Damien Miller33b13562000-04-04 14:38:59 +10001801 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001802 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001803 break;
1804 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001805 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1806 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1807 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001808 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001809 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001810 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1811 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001812 "Received disconnect from %s port %d:"
1813 "%u: %.400s", ssh_remote_ipaddr(ssh),
1814 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001815 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001816 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001817 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001818 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1819 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001820 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1821 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001822 break;
Damien Miller33b13562000-04-04 14:38:59 +10001823 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001824 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001825 }
Damien Miller33b13562000-04-04 14:38:59 +10001826 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001827 r = ssh_packet_read_poll1(ssh, typep);
1828 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001829 case SSH_MSG_NONE:
1830 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001831 case SSH_MSG_IGNORE:
1832 break;
1833 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001834 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1835 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001836 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001837 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001838 break;
1839 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001840 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1841 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001842 error("Received disconnect from %s port %d: "
1843 "%.400s", ssh_remote_ipaddr(ssh),
1844 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001845 free(msg);
1846 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001847 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001848 DBG(debug("received packet type %d", *typep));
1849 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001850 }
Damien Miller33b13562000-04-04 14:38:59 +10001851 }
1852 }
1853}
1854
Damien Miller5428f641999-11-25 11:54:57 +11001855/*
1856 * Buffers the given amount of input characters. This is intended to be used
1857 * together with packet_read_poll.
1858 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001859
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001860int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001861ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001862{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001863 struct session_state *state = ssh->state;
1864 int r;
1865
1866 if (state->packet_discard) {
1867 state->keep_alive_timeouts = 0; /* ?? */
1868 if (len >= state->packet_discard) {
1869 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001870 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001871 }
1872 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001873 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001874 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001875 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001876 return r;
1877
1878 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001879}
1880
Damien Miller4af51302000-04-16 11:18:38 +10001881int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001882ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001883{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001884 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001885}
1886
Damien Miller5428f641999-11-25 11:54:57 +11001887/*
1888 * Sends a diagnostic message from the server to the client. This message
1889 * can be sent at any time (but not while constructing another message). The
1890 * message is printed immediately, but only if the client is being executed
1891 * in verbose mode. These messages are primarily intended to ease debugging
1892 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001893 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11001894 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001895void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001896ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001897{
Damien Miller95def091999-11-25 00:26:21 +11001898 char buf[1024];
1899 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001900 int r;
Damien Miller95def091999-11-25 00:26:21 +11001901
markus@openbsd.org091c3022015-01-19 19:52:16 +00001902 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001903 return;
1904
Damien Miller95def091999-11-25 00:26:21 +11001905 va_start(args, fmt);
1906 vsnprintf(buf, sizeof(buf), fmt, args);
1907 va_end(args);
1908
Damien Miller7c8af4f2000-05-01 08:24:07 +10001909 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001910 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1911 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1912 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1913 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1914 (r = sshpkt_send(ssh)) != 0)
1915 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001916 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001917 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1918 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1919 (r = sshpkt_send(ssh)) != 0)
1920 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001921 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001922 if ((r = ssh_packet_write_wait(ssh)) != 0)
1923 fatal("%s: %s", __func__, ssh_err(r));
1924}
1925
1926/*
1927 * Pretty-print connection-terminating errors and exit.
1928 */
1929void
1930sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
1931{
1932 switch (r) {
1933 case SSH_ERR_CONN_CLOSED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001934 logit("Connection closed by %.200s port %d",
1935 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001936 cleanup_exit(255);
1937 case SSH_ERR_CONN_TIMEOUT:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001938 logit("Connection %s %.200s port %d timed out",
1939 ssh->state->server_side ? "from" : "to",
1940 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001941 cleanup_exit(255);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001942 case SSH_ERR_DISCONNECTED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001943 logit("Disconnected from %.200s port %d",
1944 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001945 cleanup_exit(255);
1946 case SSH_ERR_SYSTEM_ERROR:
1947 if (errno == ECONNRESET) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001948 logit("Connection reset by %.200s port %d",
1949 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001950 cleanup_exit(255);
1951 }
1952 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001953 case SSH_ERR_NO_CIPHER_ALG_MATCH:
1954 case SSH_ERR_NO_MAC_ALG_MATCH:
1955 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
1956 case SSH_ERR_NO_KEX_ALG_MATCH:
1957 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
1958 if (ssh && ssh->kex && ssh->kex->failed_choice) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001959 fatal("Unable to negotiate with %.200s port %d: %s. "
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001960 "Their offer: %s", ssh_remote_ipaddr(ssh),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001961 ssh_remote_port(ssh), ssh_err(r),
1962 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001963 }
1964 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001965 default:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001966 fatal("%s%sConnection %s %.200s port %d: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001967 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001968 ssh->state->server_side ? "from" : "to",
1969 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001970 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001971}
1972
Damien Miller5428f641999-11-25 11:54:57 +11001973/*
1974 * Logs the error plus constructs and sends a disconnect packet, closes the
1975 * connection, and exits. This function never returns. The error message
1976 * should not contain a newline. The length of the formatted message must
1977 * not exceed 1024 bytes.
1978 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001979void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001980ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001981{
Damien Miller95def091999-11-25 00:26:21 +11001982 char buf[1024];
1983 va_list args;
1984 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001985 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001986
Damien Miller95def091999-11-25 00:26:21 +11001987 if (disconnecting) /* Guard against recursive invocations. */
1988 fatal("packet_disconnect called recursively.");
1989 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001990
Damien Miller5428f641999-11-25 11:54:57 +11001991 /*
1992 * Format the message. Note that the caller must make sure the
1993 * message is of limited size.
1994 */
Damien Miller95def091999-11-25 00:26:21 +11001995 va_start(args, fmt);
1996 vsnprintf(buf, sizeof(buf), fmt, args);
1997 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001998
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001999 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10002000 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002001
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002002 /*
2003 * Send the disconnect message to the other side, and wait
2004 * for it to get sent.
2005 */
2006 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2007 sshpkt_fatal(ssh, __func__, r);
2008
2009 if ((r = ssh_packet_write_wait(ssh)) != 0)
2010 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002011
Damien Miller95def091999-11-25 00:26:21 +11002012 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002013 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002014 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002015}
2016
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002017/*
2018 * Checks if there is any buffered output, and tries to write some of
2019 * the output.
2020 */
2021int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002022ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002023{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002024 struct session_state *state = ssh->state;
2025 int len = sshbuf_len(state->output);
markus@openbsd.orga3068632016-01-14 16:17:39 +00002026 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002027
Damien Miller95def091999-11-25 00:26:21 +11002028 if (len > 0) {
markus@openbsd.orga3068632016-01-14 16:17:39 +00002029 len = write(state->connection_out,
2030 sshbuf_ptr(state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10002031 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002032 if (errno == EINTR || errno == EAGAIN ||
2033 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002034 return 0;
2035 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002036 }
markus@openbsd.orga3068632016-01-14 16:17:39 +00002037 if (len == 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002038 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002039 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002040 return r;
Damien Miller95def091999-11-25 00:26:21 +11002041 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002042 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002043}
2044
Damien Miller5428f641999-11-25 11:54:57 +11002045/*
2046 * Calls packet_write_poll repeatedly until all pending output data has been
2047 * written.
2048 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002049int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002050ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002051{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002052 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002053 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002054 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002055 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002056
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002057 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002058 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002059 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002060 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002061 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2062 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002063 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002064 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002065 while (ssh_packet_have_data_to_write(ssh)) {
2066 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002067 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002068 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002069
markus@openbsd.org091c3022015-01-19 19:52:16 +00002070 if (state->packet_timeout_ms > 0) {
2071 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002072 timeoutp = &timeout;
2073 }
2074 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002075 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002076 ms_to_timeval(&timeout, ms_remain);
2077 gettimeofday(&start, NULL);
2078 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002079 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002080 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002081 break;
Damien Millerea437422009-10-02 11:49:03 +10002082 if (errno != EAGAIN && errno != EINTR &&
2083 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002084 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002085 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002086 continue;
2087 ms_subtract_diff(&start, &ms_remain);
2088 if (ms_remain <= 0) {
2089 ret = 0;
2090 break;
2091 }
2092 }
2093 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002094 free(setp);
2095 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002096 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002097 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2098 free(setp);
2099 return r;
2100 }
Damien Miller95def091999-11-25 00:26:21 +11002101 }
Darren Tuckera627d422013-06-02 07:31:17 +10002102 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002103 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002104}
2105
2106/* Returns true if there is buffered data to write to the connection. */
2107
2108int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002109ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002110{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002111 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002112}
2113
2114/* Returns true if there is not too much data to write to the connection. */
2115
2116int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002117ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002118{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002119 if (ssh->state->interactive_mode)
2120 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002121 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002122 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002123}
2124
markus@openbsd.org091c3022015-01-19 19:52:16 +00002125void
2126ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002127{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002128#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002129 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002130 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002131 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002132# ifdef IP_TOS
2133 case AF_INET:
2134 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002135 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002136 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2137 error("setsockopt IP_TOS %d: %.100s:",
2138 tos, strerror(errno));
2139 break;
2140# endif /* IP_TOS */
2141# ifdef IPV6_TCLASS
2142 case AF_INET6:
2143 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002144 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002145 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2146 error("setsockopt IPV6_TCLASS %d: %.100s:",
2147 tos, strerror(errno));
2148 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002149# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002150 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002151#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002152}
Ben Lindstroma7433982002-12-23 02:41:41 +00002153
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002154/* Informs that the current session is interactive. Sets IP flags for that. */
2155
2156void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002157ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002158{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002159 struct session_state *state = ssh->state;
2160
2161 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002162 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002163 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002164
Damien Miller95def091999-11-25 00:26:21 +11002165 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002166 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002167
Damien Miller34132e52000-01-14 15:45:46 +11002168 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002169 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002170 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002171 set_nodelay(state->connection_in);
2172 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2173 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002174}
2175
2176/* Returns true if the current connection is interactive. */
2177
2178int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002179ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002180{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002181 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002182}
Damien Miller6162d121999-11-21 13:23:52 +11002183
Darren Tucker1f8311c2004-05-13 16:39:33 +10002184int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002185ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002186{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002187 struct session_state *state = ssh->state;
2188
2189 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002190 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002191 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002192 return -1;
2193 }
2194 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002195 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002196 return -1;
2197 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002198 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002199 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002200 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002201 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002202}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002203
Darren Tuckerf7288d72009-06-21 18:12:20 +10002204int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002205ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002206{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002207 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002208}
2209
2210void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002211ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002212{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002213 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002214}
2215
2216u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002217ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002218{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002219 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002220}
2221
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002222/*
2223 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002224 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002225 * byte SSH_MSG_IGNORE
2226 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002227 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002228 * All implementations MUST understand (and ignore) this message at any
2229 * time (after receiving the protocol version). No implementation is
2230 * required to send them. This message can be used as an additional
2231 * protection measure against advanced traffic analysis techniques.
2232 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002233void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002234ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002235{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002236 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002237 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002238
markus@openbsd.org091c3022015-01-19 19:52:16 +00002239 if ((r = sshpkt_start(ssh, compat20 ?
2240 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2241 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2242 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002243 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002244 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002245 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002246 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2247 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002248 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002249 }
2250}
Damien Millera5539d22003-04-09 20:50:06 +10002251
Darren Tucker1f8311c2004-05-13 16:39:33 +10002252#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002253int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002254ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002255{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002256 struct session_state *state = ssh->state;
dtucker@openbsd.orgc0060a62016-01-29 02:42:46 +00002257 u_int32_t buf_in, buf_out;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002258
2259 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002260 return 0;
dtucker@openbsd.orgc0060a62016-01-29 02:42:46 +00002261 buf_in = roundup(sshbuf_len(state->input),
2262 state->newkeys[MODE_IN]->enc.block_size);
2263 buf_out = roundup(sshbuf_len(state->output),
2264 state->newkeys[MODE_OUT]->enc.block_size);
Damien Millera5539d22003-04-09 20:50:06 +10002265 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002266 (state->p_send.packets > MAX_PACKETS) ||
2267 (state->p_read.packets > MAX_PACKETS) ||
2268 (state->max_blocks_out &&
dtucker@openbsd.orgc0060a62016-01-29 02:42:46 +00002269 (state->p_send.blocks + buf_out > state->max_blocks_out)) ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002270 (state->max_blocks_in &&
dtucker@openbsd.orgc0060a62016-01-29 02:42:46 +00002271 (state->p_read.blocks + buf_in > state->max_blocks_in)) ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002272 (state->rekey_interval != 0 && state->rekey_time +
2273 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002274}
2275
2276void
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002277ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002278{
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002279 debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes,
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002280 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002281 ssh->state->rekey_limit = bytes;
2282 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002283}
2284
2285time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002286ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002287{
2288 time_t seconds;
2289
markus@openbsd.org091c3022015-01-19 19:52:16 +00002290 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002291 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002292 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002293}
Damien Miller9786e6e2005-07-26 21:54:56 +10002294
2295void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002296ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002297{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002298 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002299}
2300
2301void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002302ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002303{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002304 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002305}
2306
2307void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002308ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002309{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002310 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002311}
2312
2313void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002314ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002315{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002316 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002317}
2318
Damien Millerc31a0cd2014-05-15 14:37:39 +10002319/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002320static int
2321ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002322{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002323 struct sshcomp *comp;
2324 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002325
2326 debug("%s: called", __func__);
2327 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002328 ssh->state->after_authentication = 1;
2329 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002330 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002331 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002332 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002333 comp = &ssh->state->newkeys[mode]->comp;
2334 if (comp && comp->enabled &&
2335 (r = ssh_packet_init_compression(ssh)) != 0)
2336 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002337 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002338 return 0;
2339}
2340
2341/* Packet state (de-)serialization for privsep */
2342
2343/* turn kex into a blob for packet state serialization */
2344static int
2345kex_to_blob(struct sshbuf *m, struct kex *kex)
2346{
2347 int r;
2348
2349 if ((r = sshbuf_put_string(m, kex->session_id,
2350 kex->session_id_len)) != 0 ||
2351 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2352 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2353 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2354 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2355 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2356 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2357 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2358 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2359 return r;
2360 return 0;
2361}
2362
2363/* turn key exchange results into a blob for packet state serialization */
2364static int
2365newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2366{
2367 struct sshbuf *b;
2368 struct sshcipher_ctx *cc;
2369 struct sshcomp *comp;
2370 struct sshenc *enc;
2371 struct sshmac *mac;
2372 struct newkeys *newkey;
2373 int r;
2374
2375 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2376 return SSH_ERR_INTERNAL_ERROR;
2377 enc = &newkey->enc;
2378 mac = &newkey->mac;
2379 comp = &newkey->comp;
2380 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2381 &ssh->state->receive_context;
2382 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2383 return r;
2384 if ((b = sshbuf_new()) == NULL)
2385 return SSH_ERR_ALLOC_FAIL;
2386 /* The cipher struct is constant and shared, you export pointer */
2387 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2388 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2389 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2390 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2391 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2392 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2393 goto out;
2394 if (cipher_authlen(enc->cipher) == 0) {
2395 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2396 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2397 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2398 goto out;
2399 }
2400 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2401 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2402 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2403 goto out;
2404 r = sshbuf_put_stringb(m, b);
2405 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002406 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002407 return r;
2408}
2409
2410/* serialize packet state into a blob */
2411int
2412ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2413{
2414 struct session_state *state = ssh->state;
2415 u_char *p;
2416 size_t slen, rlen;
2417 int r, ssh1cipher;
2418
2419 if (!compat20) {
2420 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2421 slen = cipher_get_keyiv_len(&state->send_context);
2422 rlen = cipher_get_keyiv_len(&state->receive_context);
2423 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2424 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2425 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2426 (r = sshbuf_put_u32(m, slen)) != 0 ||
2427 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2428 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2429 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2430 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2431 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2432 return r;
2433 } else {
2434 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2435 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2436 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002437 (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002438 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002439 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2440 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2441 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2442 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2443 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2444 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2445 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2446 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2447 return r;
2448 }
2449
2450 slen = cipher_get_keycontext(&state->send_context, NULL);
2451 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2452 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2453 (r = sshbuf_reserve(m, slen, &p)) != 0)
2454 return r;
2455 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2456 return SSH_ERR_INTERNAL_ERROR;
2457 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2458 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2459 return r;
2460 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2461 return SSH_ERR_INTERNAL_ERROR;
2462
2463 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2464 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2465 (r = sshbuf_put_stringb(m, state->output)) != 0)
2466 return r;
2467
markus@openbsd.org091c3022015-01-19 19:52:16 +00002468 return 0;
2469}
2470
2471/* restore key exchange results from blob for packet state de-serialization */
2472static int
2473newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2474{
2475 struct sshbuf *b = NULL;
2476 struct sshcomp *comp;
2477 struct sshenc *enc;
2478 struct sshmac *mac;
2479 struct newkeys *newkey = NULL;
2480 size_t keylen, ivlen, maclen;
2481 int r;
2482
2483 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2484 r = SSH_ERR_ALLOC_FAIL;
2485 goto out;
2486 }
2487 if ((r = sshbuf_froms(m, &b)) != 0)
2488 goto out;
2489#ifdef DEBUG_PK
2490 sshbuf_dump(b, stderr);
2491#endif
2492 enc = &newkey->enc;
2493 mac = &newkey->mac;
2494 comp = &newkey->comp;
2495
2496 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2497 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2498 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2499 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2500 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2501 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2502 goto out;
2503 if (cipher_authlen(enc->cipher) == 0) {
2504 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2505 goto out;
2506 if ((r = mac_setup(mac, mac->name)) != 0)
2507 goto out;
2508 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2509 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2510 goto out;
2511 if (maclen > mac->key_len) {
2512 r = SSH_ERR_INVALID_FORMAT;
2513 goto out;
2514 }
2515 mac->key_len = maclen;
2516 }
2517 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2518 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2519 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2520 goto out;
2521 if (enc->name == NULL ||
2522 cipher_by_name(enc->name) != enc->cipher) {
2523 r = SSH_ERR_INVALID_FORMAT;
2524 goto out;
2525 }
2526 if (sshbuf_len(b) != 0) {
2527 r = SSH_ERR_INVALID_FORMAT;
2528 goto out;
2529 }
2530 enc->key_len = keylen;
2531 enc->iv_len = ivlen;
2532 ssh->kex->newkeys[mode] = newkey;
2533 newkey = NULL;
2534 r = 0;
2535 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002536 free(newkey);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002537 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002538 return r;
2539}
2540
2541/* restore kex from blob for packet state de-serialization */
2542static int
2543kex_from_blob(struct sshbuf *m, struct kex **kexp)
2544{
2545 struct kex *kex;
2546 int r;
2547
2548 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2549 (kex->my = sshbuf_new()) == NULL ||
2550 (kex->peer = sshbuf_new()) == NULL) {
2551 r = SSH_ERR_ALLOC_FAIL;
2552 goto out;
2553 }
2554 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2555 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2556 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2557 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2558 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2559 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2560 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2561 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2562 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2563 goto out;
2564 kex->server = 1;
2565 kex->done = 1;
2566 r = 0;
2567 out:
2568 if (r != 0 || kexp == NULL) {
2569 if (kex != NULL) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002570 sshbuf_free(kex->my);
2571 sshbuf_free(kex->peer);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002572 free(kex);
2573 }
2574 if (kexp != NULL)
2575 *kexp = NULL;
2576 } else {
2577 *kexp = kex;
2578 }
2579 return r;
2580}
2581
2582/*
2583 * Restore packet state from content of blob 'm' (de-serialization).
2584 * Note that 'm' will be partially consumed on parsing or any other errors.
2585 */
2586int
2587ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2588{
2589 struct session_state *state = ssh->state;
2590 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2591 size_t ssh1keylen, rlen, slen, ilen, olen;
2592 int r;
2593 u_int ssh1cipher = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002594
2595 if (!compat20) {
2596 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2597 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2598 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2599 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2600 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2601 return r;
2602 if (ssh1cipher > INT_MAX)
2603 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2604 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2605 (int)ssh1cipher);
2606 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2607 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2608 return SSH_ERR_INVALID_FORMAT;
2609 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2610 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2611 return r;
2612 } else {
2613 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2614 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2615 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002616 (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002617 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002618 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2619 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2620 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2621 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2622 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2623 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2624 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2625 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2626 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002627 /*
2628 * We set the time here so that in post-auth privsep slave we
2629 * count from the completion of the authentication.
2630 */
2631 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002632 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2633 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2634 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2635 return r;
2636 }
2637 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2638 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2639 return r;
2640 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2641 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2642 return SSH_ERR_INVALID_FORMAT;
2643 cipher_set_keycontext(&state->send_context, keyout);
2644 cipher_set_keycontext(&state->receive_context, keyin);
2645
2646 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2647 (r = ssh_packet_set_postauth(ssh)) != 0)
2648 return r;
2649
2650 sshbuf_reset(state->input);
2651 sshbuf_reset(state->output);
2652 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2653 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2654 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2655 (r = sshbuf_put(state->output, output, olen)) != 0)
2656 return r;
2657
markus@openbsd.org091c3022015-01-19 19:52:16 +00002658 if (sshbuf_len(m))
2659 return SSH_ERR_INVALID_FORMAT;
2660 debug3("%s: done", __func__);
2661 return 0;
2662}
2663
2664/* NEW API */
2665
2666/* put data to the outgoing packet */
2667
2668int
2669sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2670{
2671 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2672}
2673
2674int
2675sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2676{
2677 return sshbuf_putb(ssh->state->outgoing_packet, b);
2678}
2679
2680int
2681sshpkt_put_u8(struct ssh *ssh, u_char val)
2682{
2683 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2684}
2685
2686int
2687sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2688{
2689 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2690}
2691
2692int
2693sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2694{
2695 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2696}
2697
2698int
2699sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2700{
2701 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2702}
2703
2704int
2705sshpkt_put_cstring(struct ssh *ssh, const void *v)
2706{
2707 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2708}
2709
2710int
2711sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2712{
2713 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2714}
2715
djm@openbsd.org734226b2015-04-27 01:52:30 +00002716#ifdef WITH_OPENSSL
2717#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002718int
2719sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2720{
2721 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2722}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002723#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002724
Damien Miller773dda22015-01-30 23:10:17 +11002725#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002726int
2727sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2728{
2729 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2730}
Damien Miller773dda22015-01-30 23:10:17 +11002731#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002732
2733int
2734sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2735{
2736 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2737}
Damien Miller773dda22015-01-30 23:10:17 +11002738#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002739
2740/* fetch data from the incoming packet */
2741
2742int
2743sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2744{
2745 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2746}
2747
2748int
2749sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2750{
2751 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2752}
2753
2754int
2755sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2756{
2757 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2758}
2759
2760int
2761sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2762{
2763 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2764}
2765
2766int
2767sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2768{
2769 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2770}
2771
2772int
2773sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2774{
2775 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2776}
2777
2778int
2779sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2780{
2781 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2782}
2783
djm@openbsd.org734226b2015-04-27 01:52:30 +00002784#ifdef WITH_OPENSSL
2785#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002786int
2787sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2788{
2789 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2790}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002791#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002792
Damien Miller773dda22015-01-30 23:10:17 +11002793#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002794int
2795sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2796{
2797 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2798}
Damien Miller773dda22015-01-30 23:10:17 +11002799#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002800
2801int
2802sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2803{
2804 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2805}
Damien Miller773dda22015-01-30 23:10:17 +11002806#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002807
2808int
2809sshpkt_get_end(struct ssh *ssh)
2810{
2811 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2812 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2813 return 0;
2814}
2815
2816const u_char *
2817sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2818{
2819 if (lenp != NULL)
2820 *lenp = sshbuf_len(ssh->state->incoming_packet);
2821 return sshbuf_ptr(ssh->state->incoming_packet);
2822}
2823
2824/* start a new packet */
2825
2826int
2827sshpkt_start(struct ssh *ssh, u_char type)
2828{
2829 u_char buf[9];
2830 int len;
2831
2832 DBG(debug("packet_start[%d]", type));
2833 len = compat20 ? 6 : 9;
2834 memset(buf, 0, len - 1);
2835 buf[len - 1] = type;
2836 sshbuf_reset(ssh->state->outgoing_packet);
2837 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2838}
2839
2840/* send it */
2841
2842int
2843sshpkt_send(struct ssh *ssh)
2844{
2845 if (compat20)
2846 return ssh_packet_send2(ssh);
2847 else
2848 return ssh_packet_send1(ssh);
2849}
2850
2851int
2852sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2853{
2854 char buf[1024];
2855 va_list args;
2856 int r;
2857
2858 va_start(args, fmt);
2859 vsnprintf(buf, sizeof(buf), fmt, args);
2860 va_end(args);
2861
2862 if (compat20) {
2863 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2864 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2865 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2866 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2867 (r = sshpkt_send(ssh)) != 0)
2868 return r;
2869 } else {
2870 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2871 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2872 (r = sshpkt_send(ssh)) != 0)
2873 return r;
2874 }
2875 return 0;
2876}
2877
2878/* roundup current message to pad bytes */
2879int
2880sshpkt_add_padding(struct ssh *ssh, u_char pad)
2881{
2882 ssh->state->extra_pad = pad;
2883 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002884}