blob: 9cf200cc3e1c219caffd89c5b8c02999e96f4eba [file] [log] [blame]
markus@openbsd.orga3068632016-01-14 16:17:39 +00001/* $OpenBSD: packet.c,v 1.222 2016/01/14 16:17:40 markus 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 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000183 u_int64_t max_blocks_in, max_blocks_out;
184 u_int32_t rekey_limit;
185
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000186 /* Time-based rekeying */
markus@openbsd.org02db4682015-02-13 18:57:00 +0000187 u_int32_t rekey_interval; /* how often in seconds */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000188 time_t rekey_time; /* time of last rekeying */
189
Darren Tuckerf7288d72009-06-21 18:12:20 +1000190 /* Session key for protocol v1 */
191 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
192 u_int ssh1_keylen;
193
194 /* roundup current message to extra_pad bytes */
195 u_char extra_pad;
196
197 /* XXX discard incoming data after MAC error */
198 u_int packet_discard;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000199 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000200
201 /* Used in packet_read_poll2() */
202 u_int packlen;
203
Darren Tucker7b935c72009-06-21 18:59:36 +1000204 /* Used in packet_send2 */
205 int rekeying;
206
207 /* Used in packet_set_interactive */
208 int set_interactive_called;
209
210 /* Used in packet_set_maxsize */
211 int set_maxsize_called;
212
markus@openbsd.org091c3022015-01-19 19:52:16 +0000213 /* One-off warning about weak ciphers */
214 int cipher_warning_done;
215
216 /* SSH1 CRC compensation attack detector */
217 struct deattack_ctx deattack;
218
Darren Tuckerf7288d72009-06-21 18:12:20 +1000219 TAILQ_HEAD(, packet) outgoing;
220};
221
markus@openbsd.org091c3022015-01-19 19:52:16 +0000222struct ssh *
223ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000224{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000225 struct ssh *ssh = NULL;
226 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000227
markus@openbsd.org091c3022015-01-19 19:52:16 +0000228 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
229 (state = calloc(1, sizeof(*state))) == NULL ||
230 (state->input = sshbuf_new()) == NULL ||
231 (state->output = sshbuf_new()) == NULL ||
232 (state->outgoing_packet = sshbuf_new()) == NULL ||
233 (state->incoming_packet = sshbuf_new()) == NULL)
234 goto fail;
235 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000236 TAILQ_INIT(&ssh->private_keys);
237 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000238 state->connection_in = -1;
239 state->connection_out = -1;
240 state->max_packet_size = 32768;
241 state->packet_timeout_ms = -1;
242 state->p_send.packets = state->p_read.packets = 0;
243 state->initialized = 1;
244 /*
245 * ssh_packet_send2() needs to queue packets until
246 * we've done the initial key exchange.
247 */
248 state->rekeying = 1;
249 ssh->state = state;
250 return ssh;
251 fail:
252 if (state) {
253 sshbuf_free(state->input);
254 sshbuf_free(state->output);
255 sshbuf_free(state->incoming_packet);
256 sshbuf_free(state->outgoing_packet);
257 free(state);
258 }
259 free(ssh);
260 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000261}
Damien Millera5539d22003-04-09 20:50:06 +1000262
Damien Miller5428f641999-11-25 11:54:57 +1100263/*
264 * Sets the descriptors used for communication. Disables encryption until
265 * packet_set_encryption_key is called.
266 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000267struct ssh *
268ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000270 struct session_state *state;
271 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000272 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000273
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000274 if (none == NULL) {
275 error("%s: cannot load cipher 'none'", __func__);
276 return NULL;
277 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000278 if (ssh == NULL)
279 ssh = ssh_alloc_session_state();
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000280 if (ssh == NULL) {
281 error("%s: cound not allocate state", __func__);
282 return NULL;
283 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000284 state = ssh->state;
285 state->connection_in = fd_in;
286 state->connection_out = fd_out;
287 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000288 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000289 (r = cipher_init(&state->receive_context, none,
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000290 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
291 error("%s: cipher_init failed: %s", __func__, ssh_err(r));
jsg@openbsd.org1cb30162015-03-11 00:48:39 +0000292 free(ssh);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000293 return NULL;
294 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000295 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
296 deattack_init(&state->deattack);
djm@openbsd.orgd4c02952015-02-11 01:20:38 +0000297 /*
298 * Cache the IP address of the remote connection for use in error
299 * messages that might be generated after the connection has closed.
300 */
301 (void)ssh_remote_ipaddr(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000302 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303}
304
Darren Tucker3fc464e2008-06-13 06:42:45 +1000305void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000306ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000307{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000308 struct session_state *state = ssh->state;
309
Damien Miller8ed4de82011-12-19 10:52:50 +1100310 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000311 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000312 return;
313 }
314 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000315 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000316 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000317 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000318}
319
markus@openbsd.org091c3022015-01-19 19:52:16 +0000320int
321ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100322{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000323 struct session_state *state = ssh->state;
324 int r;
325
326 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100327 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000328
Damien Miller13ae44c2009-01-28 16:38:41 +1100329 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000330 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000331 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000332 if ((r = sshbuf_put(state->incoming_packet, buf,
333 sizeof(buf))) != 0)
334 return r;
335 (void) mac_compute(state->packet_discard_mac,
336 state->p_read.seqnr,
337 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
338 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100339 }
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000340 logit("Finished discarding for %.200s port %d",
341 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000342 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100343}
344
markus@openbsd.org091c3022015-01-19 19:52:16 +0000345static int
346ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
347 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100348{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000349 struct session_state *state = ssh->state;
350 int r;
351
352 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
353 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
354 return r;
355 return SSH_ERR_MAC_INVALID;
356 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100357 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000358 state->packet_discard_mac = mac;
359 if (sshbuf_len(state->input) >= discard &&
360 (r = ssh_packet_stop_discard(ssh)) != 0)
361 return r;
362 state->packet_discard = discard - sshbuf_len(state->input);
363 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100364}
365
Damien Miller34132e52000-01-14 15:45:46 +1100366/* Returns 1 if remote host is connected via socket, 0 if not. */
367
368int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000369ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100370{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000371 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100372 struct sockaddr_storage from, to;
373 socklen_t fromlen, tolen;
374
375 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000376 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100377 return 1;
378 fromlen = sizeof(from);
379 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000380 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000381 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100382 return 0;
383 tolen = sizeof(to);
384 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000385 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000386 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100387 return 0;
388 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
389 return 0;
390 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
391 return 0;
392 return 1;
393}
394
Ben Lindstromf6027d32002-03-22 01:42:04 +0000395void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000396ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000397{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000398 if (ibytes)
399 *ibytes = ssh->state->p_read.bytes;
400 if (obytes)
401 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000402}
403
404int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000405ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100406{
407 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100408 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100409
410 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000411 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000412 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100413 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000414#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100415 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000416 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000417 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000418#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000419 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100420}
421
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422/* Sets the connection into non-blocking mode. */
423
424void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000425ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426{
Damien Miller95def091999-11-25 00:26:21 +1100427 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000428 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429
markus@openbsd.org091c3022015-01-19 19:52:16 +0000430 if (ssh->state->connection_out != ssh->state->connection_in)
431 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
434/* Returns the socket used for reading. */
435
436int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000437ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000439 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440}
441
442/* Returns the descriptor used for writing. */
443
444int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000445ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000447 return ssh->state->connection_out;
448}
449
450/*
451 * Returns the IP-address of the remote host as a string. The returned
452 * string must not be freed.
453 */
454
455const char *
456ssh_remote_ipaddr(struct ssh *ssh)
457{
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000458 const int sock = ssh->state->connection_in;
459
markus@openbsd.org091c3022015-01-19 19:52:16 +0000460 /* Check whether we have cached the ipaddr. */
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000461 if (ssh->remote_ipaddr == NULL) {
462 if (ssh_packet_connection_is_on_socket(ssh)) {
463 ssh->remote_ipaddr = get_peer_ipaddr(sock);
464 ssh->remote_port = get_sock_port(sock, 0);
465 } else {
466 ssh->remote_ipaddr = strdup("UNKNOWN");
467 ssh->remote_port = 0;
468 }
469 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000470 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471}
472
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000473/* Returns the port number of the remote host. */
474
475int
476ssh_remote_port(struct ssh *ssh)
477{
478 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
479 return ssh->remote_port;
480}
481
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482/* Closes the connection and clears and frees internal data structures. */
483
484void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000485ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000487 struct session_state *state = ssh->state;
488 int r;
489 u_int mode;
490
491 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100492 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000493 state->initialized = 0;
494 if (state->connection_in == state->connection_out) {
495 shutdown(state->connection_out, SHUT_RDWR);
496 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100497 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000498 close(state->connection_in);
499 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100500 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000501 sshbuf_free(state->input);
502 sshbuf_free(state->output);
503 sshbuf_free(state->outgoing_packet);
504 sshbuf_free(state->incoming_packet);
505 for (mode = 0; mode < MODE_MAX; mode++)
506 kex_free_newkeys(state->newkeys[mode]);
507 if (state->compression_buffer) {
508 sshbuf_free(state->compression_buffer);
509 if (state->compression_out_started) {
510 z_streamp stream = &state->compression_out_stream;
511 debug("compress outgoing: "
512 "raw data %llu, compressed %llu, factor %.2f",
513 (unsigned long long)stream->total_in,
514 (unsigned long long)stream->total_out,
515 stream->total_in == 0 ? 0.0 :
516 (double) stream->total_out / stream->total_in);
517 if (state->compression_out_failures == 0)
518 deflateEnd(stream);
519 }
520 if (state->compression_in_started) {
521 z_streamp stream = &state->compression_out_stream;
522 debug("compress incoming: "
523 "raw data %llu, compressed %llu, factor %.2f",
524 (unsigned long long)stream->total_out,
525 (unsigned long long)stream->total_in,
526 stream->total_out == 0 ? 0.0 :
527 (double) stream->total_in / stream->total_out);
528 if (state->compression_in_failures == 0)
529 inflateEnd(stream);
530 }
Damien Miller95def091999-11-25 00:26:21 +1100531 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000532 if ((r = cipher_cleanup(&state->send_context)) != 0)
533 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
534 if ((r = cipher_cleanup(&state->receive_context)) != 0)
535 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000536 free(ssh->remote_ipaddr);
537 ssh->remote_ipaddr = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000538 free(ssh->state);
539 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540}
541
542/* Sets remote side protocol flags. */
543
544void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000545ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000547 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000548}
549
550/* Returns the remote protocol flags set earlier by the above function. */
551
Ben Lindstrom46c16222000-12-22 01:43:59 +0000552u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000553ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000554{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000555 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556}
557
Damien Miller5428f641999-11-25 11:54:57 +1100558/*
559 * Starts packet compression from the next packet on in both directions.
560 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
561 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562
markus@openbsd.org091c3022015-01-19 19:52:16 +0000563static int
564ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000565{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000566 if (!ssh->state->compression_buffer &&
567 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
568 return SSH_ERR_ALLOC_FAIL;
569 return 0;
570}
571
572static int
573start_compression_out(struct ssh *ssh, int level)
574{
575 if (level < 1 || level > 9)
576 return SSH_ERR_INVALID_ARGUMENT;
577 debug("Enabling compression at level %d.", level);
578 if (ssh->state->compression_out_started == 1)
579 deflateEnd(&ssh->state->compression_out_stream);
580 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
581 case Z_OK:
582 ssh->state->compression_out_started = 1;
583 break;
584 case Z_MEM_ERROR:
585 return SSH_ERR_ALLOC_FAIL;
586 default:
587 return SSH_ERR_INTERNAL_ERROR;
588 }
589 return 0;
590}
591
592static int
593start_compression_in(struct ssh *ssh)
594{
595 if (ssh->state->compression_in_started == 1)
596 inflateEnd(&ssh->state->compression_in_stream);
597 switch (inflateInit(&ssh->state->compression_in_stream)) {
598 case Z_OK:
599 ssh->state->compression_in_started = 1;
600 break;
601 case Z_MEM_ERROR:
602 return SSH_ERR_ALLOC_FAIL;
603 default:
604 return SSH_ERR_INTERNAL_ERROR;
605 }
606 return 0;
607}
608
609int
610ssh_packet_start_compression(struct ssh *ssh, int level)
611{
612 int r;
613
614 if (ssh->state->packet_compression && !compat20)
615 return SSH_ERR_INTERNAL_ERROR;
616 ssh->state->packet_compression = 1;
617 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
618 (r = start_compression_in(ssh)) != 0 ||
619 (r = start_compression_out(ssh, level)) != 0)
620 return r;
621 return 0;
622}
623
624/* XXX remove need for separate compression buffer */
625static int
626compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
627{
628 u_char buf[4096];
629 int r, status;
630
631 if (ssh->state->compression_out_started != 1)
632 return SSH_ERR_INTERNAL_ERROR;
633
634 /* This case is not handled below. */
635 if (sshbuf_len(in) == 0)
636 return 0;
637
638 /* Input is the contents of the input buffer. */
639 if ((ssh->state->compression_out_stream.next_in =
640 sshbuf_mutable_ptr(in)) == NULL)
641 return SSH_ERR_INTERNAL_ERROR;
642 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
643
644 /* Loop compressing until deflate() returns with avail_out != 0. */
645 do {
646 /* Set up fixed-size output buffer. */
647 ssh->state->compression_out_stream.next_out = buf;
648 ssh->state->compression_out_stream.avail_out = sizeof(buf);
649
650 /* Compress as much data into the buffer as possible. */
651 status = deflate(&ssh->state->compression_out_stream,
652 Z_PARTIAL_FLUSH);
653 switch (status) {
654 case Z_MEM_ERROR:
655 return SSH_ERR_ALLOC_FAIL;
656 case Z_OK:
657 /* Append compressed data to output_buffer. */
658 if ((r = sshbuf_put(out, buf, sizeof(buf) -
659 ssh->state->compression_out_stream.avail_out)) != 0)
660 return r;
661 break;
662 case Z_STREAM_ERROR:
663 default:
664 ssh->state->compression_out_failures++;
665 return SSH_ERR_INVALID_FORMAT;
666 }
667 } while (ssh->state->compression_out_stream.avail_out == 0);
668 return 0;
669}
670
671static int
672uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
673{
674 u_char buf[4096];
675 int r, status;
676
677 if (ssh->state->compression_in_started != 1)
678 return SSH_ERR_INTERNAL_ERROR;
679
680 if ((ssh->state->compression_in_stream.next_in =
681 sshbuf_mutable_ptr(in)) == NULL)
682 return SSH_ERR_INTERNAL_ERROR;
683 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
684
685 for (;;) {
686 /* Set up fixed-size output buffer. */
687 ssh->state->compression_in_stream.next_out = buf;
688 ssh->state->compression_in_stream.avail_out = sizeof(buf);
689
690 status = inflate(&ssh->state->compression_in_stream,
691 Z_PARTIAL_FLUSH);
692 switch (status) {
693 case Z_OK:
694 if ((r = sshbuf_put(out, buf, sizeof(buf) -
695 ssh->state->compression_in_stream.avail_out)) != 0)
696 return r;
697 break;
698 case Z_BUF_ERROR:
699 /*
700 * Comments in zlib.h say that we should keep calling
701 * inflate() until we get an error. This appears to
702 * be the error that we get.
703 */
704 return 0;
705 case Z_DATA_ERROR:
706 return SSH_ERR_INVALID_FORMAT;
707 case Z_MEM_ERROR:
708 return SSH_ERR_ALLOC_FAIL;
709 case Z_STREAM_ERROR:
710 default:
711 ssh->state->compression_in_failures++;
712 return SSH_ERR_INTERNAL_ERROR;
713 }
714 }
715 /* NOTREACHED */
716}
717
718/* Serialise compression state into a blob for privsep */
719static int
720ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
721{
722 struct session_state *state = ssh->state;
723 struct sshbuf *b;
724 int r;
725
726 if ((b = sshbuf_new()) == NULL)
727 return SSH_ERR_ALLOC_FAIL;
728 if (state->compression_in_started) {
729 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
730 sizeof(state->compression_in_stream))) != 0)
731 goto out;
732 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
733 goto out;
734 if (state->compression_out_started) {
735 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
736 sizeof(state->compression_out_stream))) != 0)
737 goto out;
738 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
739 goto out;
740 r = sshbuf_put_stringb(m, b);
741 out:
742 sshbuf_free(b);
743 return r;
744}
745
746/* Deserialise compression state from a blob for privsep */
747static int
748ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
749{
750 struct session_state *state = ssh->state;
751 struct sshbuf *b = NULL;
752 int r;
753 const u_char *inblob, *outblob;
754 size_t inl, outl;
755
756 if ((r = sshbuf_froms(m, &b)) != 0)
757 goto out;
758 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
759 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
760 goto out;
761 if (inl == 0)
762 state->compression_in_started = 0;
763 else if (inl != sizeof(state->compression_in_stream)) {
764 r = SSH_ERR_INTERNAL_ERROR;
765 goto out;
766 } else {
767 state->compression_in_started = 1;
768 memcpy(&state->compression_in_stream, inblob, inl);
769 }
770 if (outl == 0)
771 state->compression_out_started = 0;
772 else if (outl != sizeof(state->compression_out_stream)) {
773 r = SSH_ERR_INTERNAL_ERROR;
774 goto out;
775 } else {
776 state->compression_out_started = 1;
777 memcpy(&state->compression_out_stream, outblob, outl);
778 }
779 r = 0;
780 out:
781 sshbuf_free(b);
782 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000783}
784
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000786ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
787 void *(*allocfunc)(void *, u_int, u_int),
788 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000790 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
791 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
792 ssh->state->compression_out_stream.opaque = ctx;
793 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
794 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
795 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796}
797
Damien Miller5428f641999-11-25 11:54:57 +1100798/*
Damien Miller5428f641999-11-25 11:54:57 +1100799 * Causes any further packets to be encrypted using the given key. The same
800 * key is used for both sending and reception. However, both directions are
801 * encrypted independently of each other.
802 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000803
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000805ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000807#ifndef WITH_SSH1
808 fatal("no SSH protocol 1 support");
809#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000810 struct session_state *state = ssh->state;
811 const struct sshcipher *cipher = cipher_by_number(number);
812 int r;
813 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100814
markus@openbsd.org091c3022015-01-19 19:52:16 +0000815 if (cipher == NULL)
816 fatal("%s: unknown cipher number %d", __func__, number);
817 if (keylen < 20)
818 fatal("%s: keylen too small: %d", __func__, keylen);
819 if (keylen > SSH_SESSION_KEY_LENGTH)
820 fatal("%s: keylen too big: %d", __func__, keylen);
821 memcpy(state->ssh1_key, key, keylen);
822 state->ssh1_keylen = keylen;
823 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
824 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
825 (r = cipher_init(&state->receive_context, cipher, key, keylen,
826 NULL, 0, CIPHER_DECRYPT) != 0))
827 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
828 if (!state->cipher_warning_done &&
829 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
830 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
831 error("Warning: %s", wmsg);
832 state->cipher_warning_done = 1;
833 }
Damien Miller773dda22015-01-30 23:10:17 +1100834#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000835}
836
Damien Miller5428f641999-11-25 11:54:57 +1100837/*
838 * Finalizes and sends the packet. If the encryption key has been set,
839 * encrypts the packet before sending.
840 */
Damien Miller95def091999-11-25 00:26:21 +1100841
markus@openbsd.org091c3022015-01-19 19:52:16 +0000842int
843ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000845 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000846 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000847 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000848 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849
Damien Miller5428f641999-11-25 11:54:57 +1100850 /*
851 * If using packet compression, compress the payload of the outgoing
852 * packet.
853 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000854 if (state->packet_compression) {
855 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100856 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000857 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
858 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100859 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000860 if ((r = sshbuf_put(state->compression_buffer,
861 "\0\0\0\0\0\0\0\0", 8)) != 0)
862 goto out;
863 if ((r = compress_buffer(ssh, state->outgoing_packet,
864 state->compression_buffer)) != 0)
865 goto out;
866 sshbuf_reset(state->outgoing_packet);
867 if ((r = sshbuf_putb(state->outgoing_packet,
868 state->compression_buffer)) != 0)
869 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100870 }
871 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000872 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873
Damien Millere247cc42000-05-07 12:03:14 +1000874 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100875 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000876 if (!state->send_context.plaintext) {
877 cp = sshbuf_mutable_ptr(state->outgoing_packet);
878 if (cp == NULL) {
879 r = SSH_ERR_INTERNAL_ERROR;
880 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100881 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000882 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100883 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000884 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
885 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100886
887 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000888 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
889 sshbuf_len(state->outgoing_packet));
890 POKE_U32(buf, checksum);
891 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
892 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
894#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100895 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000896 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897#endif
898
Damien Miller95def091999-11-25 00:26:21 +1100899 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000900 POKE_U32(buf, len);
901 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
902 goto out;
903 if ((r = sshbuf_reserve(state->output,
904 sshbuf_len(state->outgoing_packet), &cp)) != 0)
905 goto out;
906 if ((r = cipher_crypt(&state->send_context, 0, cp,
907 sshbuf_ptr(state->outgoing_packet),
908 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
909 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100910
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100912 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000913 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000915 state->p_send.packets++;
916 state->p_send.bytes += len +
917 sshbuf_len(state->outgoing_packet);
918 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919
Damien Miller5428f641999-11-25 11:54:57 +1100920 /*
Damien Miller788f2122005-11-05 15:14:59 +1100921 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000922 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
923 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100924 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000925 r = 0;
926 out:
927 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928}
929
markus@openbsd.org091c3022015-01-19 19:52:16 +0000930int
931ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000932{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000933 struct session_state *state = ssh->state;
934 struct sshenc *enc;
935 struct sshmac *mac;
936 struct sshcomp *comp;
937 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000938 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000939 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000940 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000941
Ben Lindstrom064496f2002-12-23 02:04:22 +0000942 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000943
Damien Miller963f6b22002-02-19 15:21:23 +1100944 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000945 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000946 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000947 state->p_send.packets = state->p_send.blocks = 0;
948 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100949 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000950 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000951 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000952 state->p_read.packets = state->p_read.blocks = 0;
953 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100954 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000955 if (state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000956 debug("set_newkeys: rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000957 if ((r = cipher_cleanup(cc)) != 0)
958 return r;
959 enc = &state->newkeys[mode]->enc;
960 mac = &state->newkeys[mode]->mac;
961 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000962 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100963 explicit_bzero(enc->iv, enc->iv_len);
964 explicit_bzero(enc->key, enc->key_len);
965 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000966 free(enc->name);
967 free(enc->iv);
968 free(enc->key);
969 free(mac->name);
970 free(mac->key);
971 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000972 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000973 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000974 /* move newkeys from kex to state */
975 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
976 return SSH_ERR_INTERNAL_ERROR;
977 ssh->kex->newkeys[mode] = NULL;
978 enc = &state->newkeys[mode]->enc;
979 mac = &state->newkeys[mode]->mac;
980 comp = &state->newkeys[mode]->comp;
981 if (cipher_authlen(enc->cipher) == 0) {
982 if ((r = mac_init(mac)) != 0)
983 return r;
984 }
985 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000986 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000987 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
988 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000989 return r;
990 if (!state->cipher_warning_done &&
991 (wmsg = cipher_warning_message(cc)) != NULL) {
992 error("Warning: %s", wmsg);
993 state->cipher_warning_done = 1;
994 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000995 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100996 /* explicit_bzero(enc->iv, enc->block_size);
997 explicit_bzero(enc->key, enc->key_len);
998 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000999 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +10001000 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +00001001 state->after_authentication)) && comp->enabled == 0) {
1002 if ((r = ssh_packet_init_compression(ssh)) < 0)
1003 return r;
1004 if (mode == MODE_OUT) {
1005 if ((r = start_compression_out(ssh, 6)) != 0)
1006 return r;
1007 } else {
1008 if ((r = start_compression_in(ssh)) != 0)
1009 return r;
1010 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001011 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001012 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001013 /*
1014 * The 2^(blocksize*2) limit is too expensive for 3DES,
1015 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1016 */
1017 if (enc->block_size >= 16)
1018 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1019 else
1020 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001021 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001022 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001023 state->rekey_limit / enc->block_size);
1024 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001025}
1026
Damien Miller5428f641999-11-25 11:54:57 +11001027/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001028 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001029 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001030 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1031 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001032static int
1033ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001034{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001035 struct session_state *state = ssh->state;
1036 struct sshcomp *comp = NULL;
1037 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001038
1039 /*
1040 * Remember that we are past the authentication step, so rekeying
1041 * with COMP_DELAYED will turn on compression immediately.
1042 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001043 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001044 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001045 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001046 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001047 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001048 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001049 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001050 if ((r = ssh_packet_init_compression(ssh)) != 0)
1051 return r;
1052 if (mode == MODE_OUT) {
1053 if ((r = start_compression_out(ssh, 6)) != 0)
1054 return r;
1055 } else {
1056 if ((r = start_compression_in(ssh)) != 0)
1057 return r;
1058 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001059 comp->enabled = 1;
1060 }
1061 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001062 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001063}
1064
1065/*
Damien Miller33b13562000-04-04 14:38:59 +10001066 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1067 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001068int
1069ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001070{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001071 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001072 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001073 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001074 u_int authlen = 0, aadlen = 0;
1075 u_int len;
1076 struct sshenc *enc = NULL;
1077 struct sshmac *mac = NULL;
1078 struct sshcomp *comp = NULL;
1079 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001080
markus@openbsd.org091c3022015-01-19 19:52:16 +00001081 if (state->newkeys[MODE_OUT] != NULL) {
1082 enc = &state->newkeys[MODE_OUT]->enc;
1083 mac = &state->newkeys[MODE_OUT]->mac;
1084 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001085 /* disable mac for authenticated encryption */
1086 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1087 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001088 }
Damien Miller963f6b22002-02-19 15:21:23 +11001089 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001090 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001091
markus@openbsd.org091c3022015-01-19 19:52:16 +00001092 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001093
1094#ifdef PACKET_DEBUG
1095 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001096 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001097#endif
1098
1099 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001100 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001101 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001102 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1103 goto out;
1104 sshbuf_reset(state->compression_buffer);
1105 if ((r = compress_buffer(ssh, state->outgoing_packet,
1106 state->compression_buffer)) != 0)
1107 goto out;
1108 sshbuf_reset(state->outgoing_packet);
1109 if ((r = sshbuf_put(state->outgoing_packet,
1110 "\0\0\0\0\0", 5)) != 0 ||
1111 (r = sshbuf_putb(state->outgoing_packet,
1112 state->compression_buffer)) != 0)
1113 goto out;
1114 DBG(debug("compression: raw %d compressed %zd", len,
1115 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001116 }
1117
1118 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001119 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001120
1121 /*
1122 * calc size of padding, alloc space, get random data,
1123 * minimum padding is 4 bytes
1124 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001125 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001126 padlen = block_size - (len % block_size);
1127 if (padlen < 4)
1128 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001129 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001130 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001131 state->extra_pad =
1132 roundup(state->extra_pad, block_size);
1133 pad = state->extra_pad -
1134 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001135 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001136 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001137 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001138 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001139 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001140 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1141 goto out;
1142 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001143 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001144 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001145 } else {
1146 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001147 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001148 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001149 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001150 len = sshbuf_len(state->outgoing_packet);
1151 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1152 if (cp == NULL) {
1153 r = SSH_ERR_INTERNAL_ERROR;
1154 goto out;
1155 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001156 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001157 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001158 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001159 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1160 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001161
1162 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001163 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001164 if ((r = mac_compute(mac, state->p_send.seqnr,
1165 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001166 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001167 goto out;
1168 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001169 }
1170 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001171 if ((r = sshbuf_reserve(state->output,
1172 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1173 goto out;
1174 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1175 sshbuf_ptr(state->outgoing_packet),
1176 len - aadlen, aadlen, authlen)) != 0)
1177 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001178 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001179 if (mac && mac->enabled) {
1180 if (mac->etm) {
1181 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001182 if ((r = mac_compute(mac, state->p_send.seqnr,
1183 cp, len, macbuf, sizeof(macbuf))) != 0)
1184 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001185 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001186 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001187 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001188 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1189 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001190 }
Damien Miller33b13562000-04-04 14:38:59 +10001191#ifdef PACKET_DEBUG
1192 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001193 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001194#endif
Damien Miller4af51302000-04-16 11:18:38 +10001195 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001196 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001197 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001198 if (++state->p_send.packets == 0)
1199 if (!(ssh->compat & SSH_BUG_NOREKEY))
1200 return SSH_ERR_NEED_REKEY;
1201 state->p_send.blocks += len / block_size;
1202 state->p_send.bytes += len;
1203 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001204
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001205 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001206 r = ssh_set_newkeys(ssh, MODE_OUT);
1207 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1208 r = ssh_packet_enable_delayed_compress(ssh);
1209 else
1210 r = 0;
1211 out:
1212 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001213}
1214
markus@openbsd.org091c3022015-01-19 19:52:16 +00001215int
1216ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001217{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001218 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001219 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001220 u_char type;
1221 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001222
markus@openbsd.org091c3022015-01-19 19:52:16 +00001223 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001224
1225 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001226 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001227 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1228 (type > SSH2_MSG_TRANSPORT_MAX) ||
1229 (type == SSH2_MSG_SERVICE_REQUEST) ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001230 (type == SSH2_MSG_SERVICE_ACCEPT) ||
1231 (type == SSH2_MSG_EXT_INFO)) {
Damien Millera5539d22003-04-09 20:50:06 +10001232 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001233 p = calloc(1, sizeof(*p));
1234 if (p == NULL)
1235 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001236 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001237 p->payload = state->outgoing_packet;
1238 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1239 state->outgoing_packet = sshbuf_new();
1240 if (state->outgoing_packet == NULL)
1241 return SSH_ERR_ALLOC_FAIL;
1242 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001243 }
1244 }
1245
1246 /* rekeying starts with sending KEXINIT */
1247 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001248 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001249
markus@openbsd.org091c3022015-01-19 19:52:16 +00001250 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1251 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001252
1253 /* after a NEWKEYS message we can send the complete queue */
1254 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001255 state->rekeying = 0;
1256 state->rekey_time = monotime();
1257 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001258 type = p->type;
1259 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001260 sshbuf_free(state->outgoing_packet);
1261 state->outgoing_packet = p->payload;
1262 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001263 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001264 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1265 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001266 }
1267 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001268 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001269}
1270
Damien Miller33b13562000-04-04 14:38:59 +10001271/*
Damien Miller5428f641999-11-25 11:54:57 +11001272 * Waits until a packet has been received, and returns its type. Note that
1273 * no other data is processed until this returns, so this function should not
1274 * be used during the interactive session.
1275 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001276
1277int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001278ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001279{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001280 struct session_state *state = ssh->state;
markus@openbsd.orga3068632016-01-14 16:17:39 +00001281 int len, r, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001282 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001283 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001284 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285
Darren Tucker99bb7612008-06-13 22:02:50 +10001286 DBG(debug("packet_read()"));
1287
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001288 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001289 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001290 if (setp == NULL)
1291 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001292
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001293 /*
1294 * Since we are blocking, ensure that all written packets have
1295 * been sent.
1296 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001297 if ((r = ssh_packet_write_wait(ssh)) != 0)
1298 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299
Damien Miller95def091999-11-25 00:26:21 +11001300 /* Stay in the loop until we have received a complete packet. */
1301 for (;;) {
1302 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001303 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1304 if (r != 0)
1305 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001306 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001307 *typep == SSH_SMSG_SUCCESS
1308 || *typep == SSH_SMSG_FAILURE
1309 || *typep == SSH_CMSG_EOF
1310 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1311 if ((r = sshpkt_get_end(ssh)) != 0)
1312 break;
Damien Miller95def091999-11-25 00:26:21 +11001313 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001314 if (*typep != SSH_MSG_NONE)
1315 break;
Damien Miller5428f641999-11-25 11:54:57 +11001316 /*
1317 * Otherwise, wait for some data to arrive, add it to the
1318 * buffer, and try again.
1319 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001320 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001321 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001322 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001323
markus@openbsd.org091c3022015-01-19 19:52:16 +00001324 if (state->packet_timeout_ms > 0) {
1325 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001326 timeoutp = &timeout;
1327 }
Damien Miller95def091999-11-25 00:26:21 +11001328 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001329 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001330 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001331 ms_to_timeval(&timeout, ms_remain);
1332 gettimeofday(&start, NULL);
1333 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001334 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001335 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001336 break;
Damien Millerea437422009-10-02 11:49:03 +10001337 if (errno != EAGAIN && errno != EINTR &&
1338 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001339 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001340 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001341 continue;
1342 ms_subtract_diff(&start, &ms_remain);
1343 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001344 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001345 break;
1346 }
1347 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001348 if (r == 0)
1349 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001350 /* Read data from the socket. */
markus@openbsd.orga3068632016-01-14 16:17:39 +00001351 len = read(state->connection_in, buf, sizeof(buf));
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001352 if (len == 0) {
1353 r = SSH_ERR_CONN_CLOSED;
1354 goto out;
1355 }
1356 if (len < 0) {
1357 r = SSH_ERR_SYSTEM_ERROR;
1358 goto out;
1359 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001360
Damien Miller95def091999-11-25 00:26:21 +11001361 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001362 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001363 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001364 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001365 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001366 free(setp);
1367 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368}
1369
Damien Miller278f9072001-12-21 15:00:19 +11001370int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001371ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001372{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001373 u_char type;
1374 int r;
1375
1376 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1377 fatal("%s: %s", __func__, ssh_err(r));
1378 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001379}
1380
Damien Miller5428f641999-11-25 11:54:57 +11001381/*
1382 * Waits until a packet has been received, verifies that its type matches
1383 * that given, and gives a fatal error and exits if there is a mismatch.
1384 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001385
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001386int
1387ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001388{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001389 int r;
1390 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001392 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1393 return r;
1394 if (type != expected_type) {
1395 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001396 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001397 expected_type, type)) != 0)
1398 return r;
1399 return SSH_ERR_PROTOCOL_ERROR;
1400 }
1401 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402}
1403
1404/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001405 * packet_process_incoming. If so, reads the packet; otherwise returns
1406 * SSH_MSG_NONE. This does not wait for data from the connection.
1407 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001408 * SSH_MSG_DISCONNECT is handled specially here. Also,
1409 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1410 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001411 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001412
markus@openbsd.org091c3022015-01-19 19:52:16 +00001413int
1414ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001415{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001416 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001417 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001418 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001419 const u_char *cp;
1420 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001421 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001422 int r;
1423
1424 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425
Damien Miller95def091999-11-25 00:26:21 +11001426 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001427 if (sshbuf_len(state->input) < 4 + 8)
1428 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001429 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001430 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001431 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1432 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1433 len)) != 0)
1434 return r;
1435 return SSH_ERR_CONN_CORRUPT;
1436 }
Damien Miller95def091999-11-25 00:26:21 +11001437 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438
Damien Miller95def091999-11-25 00:26:21 +11001439 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001440 if (sshbuf_len(state->input) < 4 + padded_len)
1441 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001442
Damien Miller95def091999-11-25 00:26:21 +11001443 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444
Damien Miller95def091999-11-25 00:26:21 +11001445 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001446 if ((r = sshbuf_consume(state->input, 4)) != 0)
1447 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001448
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001449 /*
1450 * Cryptographic attack detector for ssh
1451 * (C)1998 CORE-SDI, Buenos Aires Argentina
1452 * Ariel Futoransky(futo@core-sdi.com)
1453 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001454 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001455 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001456 switch (detect_attack(&state->deattack,
1457 sshbuf_ptr(state->input), padded_len)) {
1458 case DEATTACK_OK:
1459 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001460 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001461 emsg = "crc32 compensation attack detected";
1462 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001463 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001464 emsg = "deattack denial of service detected";
1465 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001466 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001467 emsg = "deattack error";
1468 break;
1469 }
1470 if (emsg != NULL) {
1471 error("%s", emsg);
1472 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1473 (r = ssh_packet_write_wait(ssh)) != 0)
1474 return r;
1475 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001476 }
1477 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001478
1479 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001480 sshbuf_reset(state->incoming_packet);
1481 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1482 goto out;
1483 if ((r = cipher_crypt(&state->receive_context, 0, p,
1484 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1485 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001486
markus@openbsd.org091c3022015-01-19 19:52:16 +00001487 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1488 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001489
1490#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001491 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001492 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494
Damien Miller95def091999-11-25 00:26:21 +11001495 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001496 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1497 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498
Damien Miller95def091999-11-25 00:26:21 +11001499 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001500 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1501 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001502
Damien Miller95def091999-11-25 00:26:21 +11001503 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001504 if (len != sshbuf_len(state->incoming_packet)) {
1505 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001506 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001507 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1508 (r = ssh_packet_write_wait(ssh)) != 0)
1509 return r;
1510 return SSH_ERR_CONN_CORRUPT;
1511 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001512
markus@openbsd.org091c3022015-01-19 19:52:16 +00001513 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1514 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001515 if (checksum != stored_checksum) {
1516 error("Corrupted check bytes on input");
1517 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1518 (r = ssh_packet_write_wait(ssh)) != 0)
1519 return r;
1520 return SSH_ERR_CONN_CORRUPT;
1521 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001522 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1523 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001524
markus@openbsd.org091c3022015-01-19 19:52:16 +00001525 if (state->packet_compression) {
1526 sshbuf_reset(state->compression_buffer);
1527 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1528 state->compression_buffer)) != 0)
1529 goto out;
1530 sshbuf_reset(state->incoming_packet);
1531 if ((r = sshbuf_putb(state->incoming_packet,
1532 state->compression_buffer)) != 0)
1533 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001534 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001535 state->p_read.packets++;
1536 state->p_read.bytes += padded_len + 4;
1537 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1538 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001539 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1540 error("Invalid ssh1 packet type: %d", *typep);
1541 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1542 (r = ssh_packet_write_wait(ssh)) != 0)
1543 return r;
1544 return SSH_ERR_PROTOCOL_ERROR;
1545 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001546 r = 0;
1547 out:
1548 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549}
Damien Miller95def091999-11-25 00:26:21 +11001550
markus@openbsd.org091c3022015-01-19 19:52:16 +00001551int
1552ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001553{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001554 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001555 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001556 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1557 u_int maclen, aadlen = 0, authlen = 0, block_size;
1558 struct sshenc *enc = NULL;
1559 struct sshmac *mac = NULL;
1560 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001561 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001562
markus@openbsd.org091c3022015-01-19 19:52:16 +00001563 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001564
markus@openbsd.org091c3022015-01-19 19:52:16 +00001565 if (state->packet_discard)
1566 return 0;
1567
1568 if (state->newkeys[MODE_IN] != NULL) {
1569 enc = &state->newkeys[MODE_IN]->enc;
1570 mac = &state->newkeys[MODE_IN]->mac;
1571 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001572 /* disable mac for authenticated encryption */
1573 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1574 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001575 }
1576 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001577 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001578 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001579
markus@openbsd.org091c3022015-01-19 19:52:16 +00001580 if (aadlen && state->packlen == 0) {
1581 if (cipher_get_length(&state->receive_context,
1582 &state->packlen, state->p_read.seqnr,
1583 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1584 return 0;
1585 if (state->packlen < 1 + 4 ||
1586 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001587#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001588 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001589#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001590 logit("Bad packet length %u.", state->packlen);
1591 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1592 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001593 return SSH_ERR_CONN_CORRUPT;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001594 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001595 sshbuf_reset(state->incoming_packet);
1596 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001597 /*
1598 * check if input size is less than the cipher block size,
1599 * decrypt first block and extract length of incoming packet
1600 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001601 if (sshbuf_len(state->input) < block_size)
1602 return 0;
1603 sshbuf_reset(state->incoming_packet);
1604 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1605 &cp)) != 0)
1606 goto out;
1607 if ((r = cipher_crypt(&state->receive_context,
1608 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1609 block_size, 0, 0)) != 0)
1610 goto out;
1611 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1612 if (state->packlen < 1 + 4 ||
1613 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001614#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001615 fprintf(stderr, "input: \n");
1616 sshbuf_dump(state->input, stderr);
1617 fprintf(stderr, "incoming_packet: \n");
1618 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001619#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001620 logit("Bad packet length %u.", state->packlen);
1621 return ssh_packet_start_discard(ssh, enc, mac,
1622 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001623 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001624 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1625 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001626 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001627 DBG(debug("input: packet len %u", state->packlen+4));
1628
Damien Milleraf43a7a2012-12-12 10:46:31 +11001629 if (aadlen) {
1630 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001631 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001632 } else {
1633 /*
1634 * the payload size and the payload are encrypted, but we
1635 * have a partial packet of block_size bytes
1636 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001637 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001638 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001639 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1640 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001641 if (need % block_size != 0) {
1642 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001643 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001644 return ssh_packet_start_discard(ssh, enc, mac,
1645 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001646 }
Damien Miller33b13562000-04-04 14:38:59 +10001647 /*
1648 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001649 * decrypt into incoming_packet:
1650 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001651 * 'need' bytes are encrypted, followed by either
1652 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001653 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001654 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001655 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1656 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001657#ifdef PACKET_DEBUG
1658 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001659 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001660#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001661 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001662 if (mac && mac->enabled && mac->etm) {
1663 if ((r = mac_compute(mac, state->p_read.seqnr,
1664 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001665 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001666 goto out;
1667 }
1668 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1669 &cp)) != 0)
1670 goto out;
1671 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1672 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1673 goto out;
1674 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1675 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001676 /*
1677 * compute MAC over seqnr and packet,
1678 * increment sequence number for incoming packet
1679 */
Damien Miller4af51302000-04-16 11:18:38 +10001680 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001681 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001682 if ((r = mac_compute(mac, state->p_read.seqnr,
1683 sshbuf_ptr(state->incoming_packet),
1684 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001685 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001686 goto out;
1687 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001688 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001689 logit("Corrupted MAC on input.");
1690 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001691 return SSH_ERR_INTERNAL_ERROR;
1692 return ssh_packet_start_discard(ssh, enc, mac,
1693 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001694 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001695
1696 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1697 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1698 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001699 }
Damien Miller278f9072001-12-21 15:00:19 +11001700 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001701 *seqnr_p = state->p_read.seqnr;
1702 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001703 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001704 if (++state->p_read.packets == 0)
1705 if (!(ssh->compat & SSH_BUG_NOREKEY))
1706 return SSH_ERR_NEED_REKEY;
1707 state->p_read.blocks += (state->packlen + 4) / block_size;
1708 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001709
1710 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001711 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001712 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001713 if (padlen < 4) {
1714 if ((r = sshpkt_disconnect(ssh,
1715 "Corrupted padlen %d on input.", padlen)) != 0 ||
1716 (r = ssh_packet_write_wait(ssh)) != 0)
1717 return r;
1718 return SSH_ERR_CONN_CORRUPT;
1719 }
Damien Miller33b13562000-04-04 14:38:59 +10001720
1721 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001722 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1723 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1724 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001725
markus@openbsd.org091c3022015-01-19 19:52:16 +00001726 DBG(debug("input: len before de-compress %zd",
1727 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001728 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001729 sshbuf_reset(state->compression_buffer);
1730 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1731 state->compression_buffer)) != 0)
1732 goto out;
1733 sshbuf_reset(state->incoming_packet);
1734 if ((r = sshbuf_putb(state->incoming_packet,
1735 state->compression_buffer)) != 0)
1736 goto out;
1737 DBG(debug("input: len after de-compress %zd",
1738 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001739 }
1740 /*
1741 * get packet type, implies consume.
1742 * return length of payload (without type field)
1743 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001744 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1745 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001746 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1747 if ((r = sshpkt_disconnect(ssh,
1748 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1749 (r = ssh_packet_write_wait(ssh)) != 0)
1750 return r;
1751 return SSH_ERR_PROTOCOL_ERROR;
1752 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001753 if (*typep == SSH2_MSG_NEWKEYS)
1754 r = ssh_set_newkeys(ssh, MODE_IN);
1755 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1756 r = ssh_packet_enable_delayed_compress(ssh);
1757 else
1758 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001759#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001760 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1761 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001762#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001763 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001764 state->packlen = 0;
1765 out:
1766 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001767}
1768
1769int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001770ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001771{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001772 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001773 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001774 int r;
1775 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001776
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001777 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001778 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001779 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001780 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1781 if (r != 0)
1782 return r;
1783 if (*typep) {
1784 state->keep_alive_timeouts = 0;
1785 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001786 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001787 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001788 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001789 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001790 break;
Damien Miller33b13562000-04-04 14:38:59 +10001791 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001792 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1793 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1794 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001795 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001796 return r;
1797 }
Damien Miller33b13562000-04-04 14:38:59 +10001798 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001799 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001800 break;
1801 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001802 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1803 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1804 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001805 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001806 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001807 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1808 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001809 "Received disconnect from %s port %d:"
1810 "%u: %.400s", ssh_remote_ipaddr(ssh),
1811 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001812 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001813 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001814 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001815 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1816 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001817 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1818 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001819 break;
Damien Miller33b13562000-04-04 14:38:59 +10001820 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001821 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001822 }
Damien Miller33b13562000-04-04 14:38:59 +10001823 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001824 r = ssh_packet_read_poll1(ssh, typep);
1825 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001826 case SSH_MSG_NONE:
1827 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001828 case SSH_MSG_IGNORE:
1829 break;
1830 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001831 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1832 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001833 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001834 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001835 break;
1836 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001837 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1838 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001839 error("Received disconnect from %s port %d: "
1840 "%.400s", ssh_remote_ipaddr(ssh),
1841 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001842 free(msg);
1843 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001844 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001845 DBG(debug("received packet type %d", *typep));
1846 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001847 }
Damien Miller33b13562000-04-04 14:38:59 +10001848 }
1849 }
1850}
1851
Damien Miller5428f641999-11-25 11:54:57 +11001852/*
1853 * Buffers the given amount of input characters. This is intended to be used
1854 * together with packet_read_poll.
1855 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001856
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001857int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001858ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001859{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001860 struct session_state *state = ssh->state;
1861 int r;
1862
1863 if (state->packet_discard) {
1864 state->keep_alive_timeouts = 0; /* ?? */
1865 if (len >= state->packet_discard) {
1866 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001867 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001868 }
1869 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001870 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001871 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001872 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001873 return r;
1874
1875 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001876}
1877
Damien Miller4af51302000-04-16 11:18:38 +10001878int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001879ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001880{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001881 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001882}
1883
Damien Miller5428f641999-11-25 11:54:57 +11001884/*
1885 * Sends a diagnostic message from the server to the client. This message
1886 * can be sent at any time (but not while constructing another message). The
1887 * message is printed immediately, but only if the client is being executed
1888 * in verbose mode. These messages are primarily intended to ease debugging
1889 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001890 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11001891 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001892void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001893ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001894{
Damien Miller95def091999-11-25 00:26:21 +11001895 char buf[1024];
1896 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001897 int r;
Damien Miller95def091999-11-25 00:26:21 +11001898
markus@openbsd.org091c3022015-01-19 19:52:16 +00001899 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001900 return;
1901
Damien Miller95def091999-11-25 00:26:21 +11001902 va_start(args, fmt);
1903 vsnprintf(buf, sizeof(buf), fmt, args);
1904 va_end(args);
1905
Damien Miller7c8af4f2000-05-01 08:24:07 +10001906 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001907 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1908 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1909 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1910 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1911 (r = sshpkt_send(ssh)) != 0)
1912 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001913 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001914 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1915 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1916 (r = sshpkt_send(ssh)) != 0)
1917 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001918 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001919 if ((r = ssh_packet_write_wait(ssh)) != 0)
1920 fatal("%s: %s", __func__, ssh_err(r));
1921}
1922
1923/*
1924 * Pretty-print connection-terminating errors and exit.
1925 */
1926void
1927sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
1928{
1929 switch (r) {
1930 case SSH_ERR_CONN_CLOSED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001931 logit("Connection closed by %.200s port %d",
1932 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001933 cleanup_exit(255);
1934 case SSH_ERR_CONN_TIMEOUT:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001935 logit("Connection %s %.200s port %d timed out",
1936 ssh->state->server_side ? "from" : "to",
1937 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001938 cleanup_exit(255);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001939 case SSH_ERR_DISCONNECTED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001940 logit("Disconnected from %.200s port %d",
1941 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001942 cleanup_exit(255);
1943 case SSH_ERR_SYSTEM_ERROR:
1944 if (errno == ECONNRESET) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001945 logit("Connection reset by %.200s port %d",
1946 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001947 cleanup_exit(255);
1948 }
1949 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001950 case SSH_ERR_NO_CIPHER_ALG_MATCH:
1951 case SSH_ERR_NO_MAC_ALG_MATCH:
1952 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
1953 case SSH_ERR_NO_KEX_ALG_MATCH:
1954 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
1955 if (ssh && ssh->kex && ssh->kex->failed_choice) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001956 fatal("Unable to negotiate with %.200s port %d: %s. "
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001957 "Their offer: %s", ssh_remote_ipaddr(ssh),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001958 ssh_remote_port(ssh), ssh_err(r),
1959 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001960 }
1961 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001962 default:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001963 fatal("%s%sConnection %s %.200s port %d: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001964 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001965 ssh->state->server_side ? "from" : "to",
1966 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001967 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001968}
1969
Damien Miller5428f641999-11-25 11:54:57 +11001970/*
1971 * Logs the error plus constructs and sends a disconnect packet, closes the
1972 * connection, and exits. This function never returns. The error message
1973 * should not contain a newline. The length of the formatted message must
1974 * not exceed 1024 bytes.
1975 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001976void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001977ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001978{
Damien Miller95def091999-11-25 00:26:21 +11001979 char buf[1024];
1980 va_list args;
1981 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001982 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001983
Damien Miller95def091999-11-25 00:26:21 +11001984 if (disconnecting) /* Guard against recursive invocations. */
1985 fatal("packet_disconnect called recursively.");
1986 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001987
Damien Miller5428f641999-11-25 11:54:57 +11001988 /*
1989 * Format the message. Note that the caller must make sure the
1990 * message is of limited size.
1991 */
Damien Miller95def091999-11-25 00:26:21 +11001992 va_start(args, fmt);
1993 vsnprintf(buf, sizeof(buf), fmt, args);
1994 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001995
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001996 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001997 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001998
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001999 /*
2000 * Send the disconnect message to the other side, and wait
2001 * for it to get sent.
2002 */
2003 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2004 sshpkt_fatal(ssh, __func__, r);
2005
2006 if ((r = ssh_packet_write_wait(ssh)) != 0)
2007 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002008
Damien Miller95def091999-11-25 00:26:21 +11002009 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002010 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002011 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002012}
2013
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002014/*
2015 * Checks if there is any buffered output, and tries to write some of
2016 * the output.
2017 */
2018int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002019ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002020{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002021 struct session_state *state = ssh->state;
2022 int len = sshbuf_len(state->output);
markus@openbsd.orga3068632016-01-14 16:17:39 +00002023 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002024
Damien Miller95def091999-11-25 00:26:21 +11002025 if (len > 0) {
markus@openbsd.orga3068632016-01-14 16:17:39 +00002026 len = write(state->connection_out,
2027 sshbuf_ptr(state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10002028 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002029 if (errno == EINTR || errno == EAGAIN ||
2030 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002031 return 0;
2032 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002033 }
markus@openbsd.orga3068632016-01-14 16:17:39 +00002034 if (len == 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002035 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002036 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002037 return r;
Damien Miller95def091999-11-25 00:26:21 +11002038 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002039 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002040}
2041
Damien Miller5428f641999-11-25 11:54:57 +11002042/*
2043 * Calls packet_write_poll repeatedly until all pending output data has been
2044 * written.
2045 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002046int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002047ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002048{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002049 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002050 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002051 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002052 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002053
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002054 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002055 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002056 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002057 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002058 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2059 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002060 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002061 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002062 while (ssh_packet_have_data_to_write(ssh)) {
2063 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002064 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002065 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002066
markus@openbsd.org091c3022015-01-19 19:52:16 +00002067 if (state->packet_timeout_ms > 0) {
2068 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002069 timeoutp = &timeout;
2070 }
2071 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002072 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002073 ms_to_timeval(&timeout, ms_remain);
2074 gettimeofday(&start, NULL);
2075 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002076 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002077 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002078 break;
Damien Millerea437422009-10-02 11:49:03 +10002079 if (errno != EAGAIN && errno != EINTR &&
2080 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002081 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002082 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002083 continue;
2084 ms_subtract_diff(&start, &ms_remain);
2085 if (ms_remain <= 0) {
2086 ret = 0;
2087 break;
2088 }
2089 }
2090 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002091 free(setp);
2092 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002093 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002094 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2095 free(setp);
2096 return r;
2097 }
Damien Miller95def091999-11-25 00:26:21 +11002098 }
Darren Tuckera627d422013-06-02 07:31:17 +10002099 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002100 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002101}
2102
2103/* Returns true if there is buffered data to write to the connection. */
2104
2105int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002106ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002107{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002108 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002109}
2110
2111/* Returns true if there is not too much data to write to the connection. */
2112
2113int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002114ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002115{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002116 if (ssh->state->interactive_mode)
2117 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002118 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002119 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002120}
2121
markus@openbsd.org091c3022015-01-19 19:52:16 +00002122void
2123ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002124{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002125#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002126 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002127 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002128 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002129# ifdef IP_TOS
2130 case AF_INET:
2131 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002132 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002133 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2134 error("setsockopt IP_TOS %d: %.100s:",
2135 tos, strerror(errno));
2136 break;
2137# endif /* IP_TOS */
2138# ifdef IPV6_TCLASS
2139 case AF_INET6:
2140 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002141 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002142 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2143 error("setsockopt IPV6_TCLASS %d: %.100s:",
2144 tos, strerror(errno));
2145 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002146# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002147 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002148#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002149}
Ben Lindstroma7433982002-12-23 02:41:41 +00002150
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002151/* Informs that the current session is interactive. Sets IP flags for that. */
2152
2153void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002154ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002155{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002156 struct session_state *state = ssh->state;
2157
2158 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002159 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002160 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002161
Damien Miller95def091999-11-25 00:26:21 +11002162 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002163 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002164
Damien Miller34132e52000-01-14 15:45:46 +11002165 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002166 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002167 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002168 set_nodelay(state->connection_in);
2169 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2170 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002171}
2172
2173/* Returns true if the current connection is interactive. */
2174
2175int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002176ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002177{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002178 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002179}
Damien Miller6162d121999-11-21 13:23:52 +11002180
Darren Tucker1f8311c2004-05-13 16:39:33 +10002181int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002182ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002183{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002184 struct session_state *state = ssh->state;
2185
2186 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002187 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002188 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002189 return -1;
2190 }
2191 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002192 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002193 return -1;
2194 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002195 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002196 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002197 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002198 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002199}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002200
Darren Tuckerf7288d72009-06-21 18:12:20 +10002201int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002202ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002203{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002204 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002205}
2206
2207void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002208ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002209{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002210 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002211}
2212
2213u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002214ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002215{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002216 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002217}
2218
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002219/*
2220 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002221 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002222 * byte SSH_MSG_IGNORE
2223 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002224 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002225 * All implementations MUST understand (and ignore) this message at any
2226 * time (after receiving the protocol version). No implementation is
2227 * required to send them. This message can be used as an additional
2228 * protection measure against advanced traffic analysis techniques.
2229 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002230void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002231ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002232{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002233 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002234 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002235
markus@openbsd.org091c3022015-01-19 19:52:16 +00002236 if ((r = sshpkt_start(ssh, compat20 ?
2237 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2238 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2239 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002240 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002241 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002242 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002243 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2244 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002245 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002246 }
2247}
Damien Millera5539d22003-04-09 20:50:06 +10002248
Darren Tucker1f8311c2004-05-13 16:39:33 +10002249#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002250int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002251ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002252{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002253 struct session_state *state = ssh->state;
2254
2255 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002256 return 0;
2257 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002258 (state->p_send.packets > MAX_PACKETS) ||
2259 (state->p_read.packets > MAX_PACKETS) ||
2260 (state->max_blocks_out &&
2261 (state->p_send.blocks > state->max_blocks_out)) ||
2262 (state->max_blocks_in &&
2263 (state->p_read.blocks > state->max_blocks_in)) ||
2264 (state->rekey_interval != 0 && state->rekey_time +
2265 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002266}
2267
2268void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002269ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002270{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002271 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
2272 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002273 ssh->state->rekey_limit = bytes;
2274 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002275}
2276
2277time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002278ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002279{
2280 time_t seconds;
2281
markus@openbsd.org091c3022015-01-19 19:52:16 +00002282 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002283 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002284 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002285}
Damien Miller9786e6e2005-07-26 21:54:56 +10002286
2287void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002288ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002289{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002290 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002291}
2292
2293void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002294ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002295{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002296 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002297}
2298
2299void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002300ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002301{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002302 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002303}
2304
2305void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002306ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002307{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002308 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002309}
2310
Damien Millerc31a0cd2014-05-15 14:37:39 +10002311/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002312static int
2313ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002314{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002315 struct sshcomp *comp;
2316 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002317
2318 debug("%s: called", __func__);
2319 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002320 ssh->state->after_authentication = 1;
2321 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002322 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002323 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002324 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002325 comp = &ssh->state->newkeys[mode]->comp;
2326 if (comp && comp->enabled &&
2327 (r = ssh_packet_init_compression(ssh)) != 0)
2328 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002329 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002330 return 0;
2331}
2332
2333/* Packet state (de-)serialization for privsep */
2334
2335/* turn kex into a blob for packet state serialization */
2336static int
2337kex_to_blob(struct sshbuf *m, struct kex *kex)
2338{
2339 int r;
2340
2341 if ((r = sshbuf_put_string(m, kex->session_id,
2342 kex->session_id_len)) != 0 ||
2343 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2344 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2345 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2346 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2347 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2348 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2349 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2350 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2351 return r;
2352 return 0;
2353}
2354
2355/* turn key exchange results into a blob for packet state serialization */
2356static int
2357newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2358{
2359 struct sshbuf *b;
2360 struct sshcipher_ctx *cc;
2361 struct sshcomp *comp;
2362 struct sshenc *enc;
2363 struct sshmac *mac;
2364 struct newkeys *newkey;
2365 int r;
2366
2367 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2368 return SSH_ERR_INTERNAL_ERROR;
2369 enc = &newkey->enc;
2370 mac = &newkey->mac;
2371 comp = &newkey->comp;
2372 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2373 &ssh->state->receive_context;
2374 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2375 return r;
2376 if ((b = sshbuf_new()) == NULL)
2377 return SSH_ERR_ALLOC_FAIL;
2378 /* The cipher struct is constant and shared, you export pointer */
2379 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2380 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2381 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2382 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2383 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2384 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2385 goto out;
2386 if (cipher_authlen(enc->cipher) == 0) {
2387 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2388 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2389 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2390 goto out;
2391 }
2392 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2393 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2394 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2395 goto out;
2396 r = sshbuf_put_stringb(m, b);
2397 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002398 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002399 return r;
2400}
2401
2402/* serialize packet state into a blob */
2403int
2404ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2405{
2406 struct session_state *state = ssh->state;
2407 u_char *p;
2408 size_t slen, rlen;
2409 int r, ssh1cipher;
2410
2411 if (!compat20) {
2412 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2413 slen = cipher_get_keyiv_len(&state->send_context);
2414 rlen = cipher_get_keyiv_len(&state->receive_context);
2415 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2416 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2417 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2418 (r = sshbuf_put_u32(m, slen)) != 0 ||
2419 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2420 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2421 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2422 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2423 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2424 return r;
2425 } else {
2426 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2427 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2428 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002429 (r = sshbuf_put_u32(m, state->rekey_limit)) != 0 ||
2430 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002431 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2432 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2433 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2434 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2435 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2436 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2437 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2438 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2439 return r;
2440 }
2441
2442 slen = cipher_get_keycontext(&state->send_context, NULL);
2443 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2444 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2445 (r = sshbuf_reserve(m, slen, &p)) != 0)
2446 return r;
2447 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2448 return SSH_ERR_INTERNAL_ERROR;
2449 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2450 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2451 return r;
2452 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2453 return SSH_ERR_INTERNAL_ERROR;
2454
2455 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2456 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2457 (r = sshbuf_put_stringb(m, state->output)) != 0)
2458 return r;
2459
markus@openbsd.org091c3022015-01-19 19:52:16 +00002460 return 0;
2461}
2462
2463/* restore key exchange results from blob for packet state de-serialization */
2464static int
2465newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2466{
2467 struct sshbuf *b = NULL;
2468 struct sshcomp *comp;
2469 struct sshenc *enc;
2470 struct sshmac *mac;
2471 struct newkeys *newkey = NULL;
2472 size_t keylen, ivlen, maclen;
2473 int r;
2474
2475 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2476 r = SSH_ERR_ALLOC_FAIL;
2477 goto out;
2478 }
2479 if ((r = sshbuf_froms(m, &b)) != 0)
2480 goto out;
2481#ifdef DEBUG_PK
2482 sshbuf_dump(b, stderr);
2483#endif
2484 enc = &newkey->enc;
2485 mac = &newkey->mac;
2486 comp = &newkey->comp;
2487
2488 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2489 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2490 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2491 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2492 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2493 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2494 goto out;
2495 if (cipher_authlen(enc->cipher) == 0) {
2496 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2497 goto out;
2498 if ((r = mac_setup(mac, mac->name)) != 0)
2499 goto out;
2500 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2501 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2502 goto out;
2503 if (maclen > mac->key_len) {
2504 r = SSH_ERR_INVALID_FORMAT;
2505 goto out;
2506 }
2507 mac->key_len = maclen;
2508 }
2509 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2510 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2511 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2512 goto out;
2513 if (enc->name == NULL ||
2514 cipher_by_name(enc->name) != enc->cipher) {
2515 r = SSH_ERR_INVALID_FORMAT;
2516 goto out;
2517 }
2518 if (sshbuf_len(b) != 0) {
2519 r = SSH_ERR_INVALID_FORMAT;
2520 goto out;
2521 }
2522 enc->key_len = keylen;
2523 enc->iv_len = ivlen;
2524 ssh->kex->newkeys[mode] = newkey;
2525 newkey = NULL;
2526 r = 0;
2527 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002528 free(newkey);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002529 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002530 return r;
2531}
2532
2533/* restore kex from blob for packet state de-serialization */
2534static int
2535kex_from_blob(struct sshbuf *m, struct kex **kexp)
2536{
2537 struct kex *kex;
2538 int r;
2539
2540 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2541 (kex->my = sshbuf_new()) == NULL ||
2542 (kex->peer = sshbuf_new()) == NULL) {
2543 r = SSH_ERR_ALLOC_FAIL;
2544 goto out;
2545 }
2546 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2547 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2548 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2549 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2550 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2551 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2552 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2553 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2554 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2555 goto out;
2556 kex->server = 1;
2557 kex->done = 1;
2558 r = 0;
2559 out:
2560 if (r != 0 || kexp == NULL) {
2561 if (kex != NULL) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002562 sshbuf_free(kex->my);
2563 sshbuf_free(kex->peer);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002564 free(kex);
2565 }
2566 if (kexp != NULL)
2567 *kexp = NULL;
2568 } else {
2569 *kexp = kex;
2570 }
2571 return r;
2572}
2573
2574/*
2575 * Restore packet state from content of blob 'm' (de-serialization).
2576 * Note that 'm' will be partially consumed on parsing or any other errors.
2577 */
2578int
2579ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2580{
2581 struct session_state *state = ssh->state;
2582 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2583 size_t ssh1keylen, rlen, slen, ilen, olen;
2584 int r;
2585 u_int ssh1cipher = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002586
2587 if (!compat20) {
2588 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2589 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2590 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2591 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2592 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2593 return r;
2594 if (ssh1cipher > INT_MAX)
2595 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2596 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2597 (int)ssh1cipher);
2598 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2599 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2600 return SSH_ERR_INVALID_FORMAT;
2601 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2602 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2603 return r;
2604 } else {
2605 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2606 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2607 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002608 (r = sshbuf_get_u32(m, &state->rekey_limit)) != 0 ||
2609 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002610 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2611 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2612 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2613 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2614 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2615 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2616 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2617 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2618 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002619 /*
2620 * We set the time here so that in post-auth privsep slave we
2621 * count from the completion of the authentication.
2622 */
2623 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002624 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2625 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2626 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2627 return r;
2628 }
2629 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2630 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2631 return r;
2632 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2633 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2634 return SSH_ERR_INVALID_FORMAT;
2635 cipher_set_keycontext(&state->send_context, keyout);
2636 cipher_set_keycontext(&state->receive_context, keyin);
2637
2638 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2639 (r = ssh_packet_set_postauth(ssh)) != 0)
2640 return r;
2641
2642 sshbuf_reset(state->input);
2643 sshbuf_reset(state->output);
2644 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2645 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2646 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2647 (r = sshbuf_put(state->output, output, olen)) != 0)
2648 return r;
2649
markus@openbsd.org091c3022015-01-19 19:52:16 +00002650 if (sshbuf_len(m))
2651 return SSH_ERR_INVALID_FORMAT;
2652 debug3("%s: done", __func__);
2653 return 0;
2654}
2655
2656/* NEW API */
2657
2658/* put data to the outgoing packet */
2659
2660int
2661sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2662{
2663 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2664}
2665
2666int
2667sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2668{
2669 return sshbuf_putb(ssh->state->outgoing_packet, b);
2670}
2671
2672int
2673sshpkt_put_u8(struct ssh *ssh, u_char val)
2674{
2675 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2676}
2677
2678int
2679sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2680{
2681 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2682}
2683
2684int
2685sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2686{
2687 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2688}
2689
2690int
2691sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2692{
2693 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2694}
2695
2696int
2697sshpkt_put_cstring(struct ssh *ssh, const void *v)
2698{
2699 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2700}
2701
2702int
2703sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2704{
2705 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2706}
2707
djm@openbsd.org734226b2015-04-27 01:52:30 +00002708#ifdef WITH_OPENSSL
2709#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002710int
2711sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2712{
2713 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2714}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002715#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002716
Damien Miller773dda22015-01-30 23:10:17 +11002717#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002718int
2719sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2720{
2721 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2722}
Damien Miller773dda22015-01-30 23:10:17 +11002723#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002724
2725int
2726sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2727{
2728 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2729}
Damien Miller773dda22015-01-30 23:10:17 +11002730#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002731
2732/* fetch data from the incoming packet */
2733
2734int
2735sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2736{
2737 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2738}
2739
2740int
2741sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2742{
2743 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2744}
2745
2746int
2747sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2748{
2749 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2750}
2751
2752int
2753sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2754{
2755 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2756}
2757
2758int
2759sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2760{
2761 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2762}
2763
2764int
2765sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2766{
2767 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2768}
2769
2770int
2771sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2772{
2773 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2774}
2775
djm@openbsd.org734226b2015-04-27 01:52:30 +00002776#ifdef WITH_OPENSSL
2777#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002778int
2779sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2780{
2781 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2782}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002783#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002784
Damien Miller773dda22015-01-30 23:10:17 +11002785#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002786int
2787sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2788{
2789 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2790}
Damien Miller773dda22015-01-30 23:10:17 +11002791#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002792
2793int
2794sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2795{
2796 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2797}
Damien Miller773dda22015-01-30 23:10:17 +11002798#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002799
2800int
2801sshpkt_get_end(struct ssh *ssh)
2802{
2803 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2804 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2805 return 0;
2806}
2807
2808const u_char *
2809sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2810{
2811 if (lenp != NULL)
2812 *lenp = sshbuf_len(ssh->state->incoming_packet);
2813 return sshbuf_ptr(ssh->state->incoming_packet);
2814}
2815
2816/* start a new packet */
2817
2818int
2819sshpkt_start(struct ssh *ssh, u_char type)
2820{
2821 u_char buf[9];
2822 int len;
2823
2824 DBG(debug("packet_start[%d]", type));
2825 len = compat20 ? 6 : 9;
2826 memset(buf, 0, len - 1);
2827 buf[len - 1] = type;
2828 sshbuf_reset(ssh->state->outgoing_packet);
2829 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2830}
2831
2832/* send it */
2833
2834int
2835sshpkt_send(struct ssh *ssh)
2836{
2837 if (compat20)
2838 return ssh_packet_send2(ssh);
2839 else
2840 return ssh_packet_send1(ssh);
2841}
2842
2843int
2844sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2845{
2846 char buf[1024];
2847 va_list args;
2848 int r;
2849
2850 va_start(args, fmt);
2851 vsnprintf(buf, sizeof(buf), fmt, args);
2852 va_end(args);
2853
2854 if (compat20) {
2855 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2856 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2857 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2858 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2859 (r = sshpkt_send(ssh)) != 0)
2860 return r;
2861 } else {
2862 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2863 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2864 (r = sshpkt_send(ssh)) != 0)
2865 return r;
2866 }
2867 return 0;
2868}
2869
2870/* roundup current message to pad bytes */
2871int
2872sshpkt_add_padding(struct ssh *ssh, u_char pad)
2873{
2874 ssh->state->extra_pad = pad;
2875 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002876}