blob: 48111bb151d4c4ee58db6aa110312be4cb9131e5 [file] [log] [blame]
djm@openbsd.org95767262016-03-07 19:02:43 +00001/* $OpenBSD: packet.c,v 1.230 2016/03/07 19:02:43 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000042#include <sys/param.h> /* MIN roundup */
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100044#include "openbsd-compat/sys-queue.h"
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100046#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049
Damien Miller8ec8c3e2006-07-10 20:35:38 +100050#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110051#include <netinet/ip.h>
Darren Tuckerdace2332006-09-22 19:22:17 +100052#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.h>
djm@openbsd.org95767262016-03-07 19:02:43 +000055#include <netdb.h>
Darren Tucker5d196262006-07-12 22:15:16 +100056#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100057#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100058#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100059#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100060#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000061#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100062#include <signal.h>
Darren Tuckerc53c2af2013-05-16 20:28:16 +100063#include <time.h>
Darren Tucker5d196262006-07-12 22:15:16 +100064
markus@openbsd.org091c3022015-01-19 19:52:16 +000065#include <zlib.h>
66
67#include "buffer.h" /* typedefs XXX */
68#include "key.h" /* typedefs XXX */
69
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#include "deattack.h"
Damien Miller33b13562000-04-04 14:38:59 +100073#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100075#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110076#include "cipher.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000077#include "sshkey.h"
Damien Miller33b13562000-04-04 14:38:59 +100078#include "kex.h"
markus@openbsd.org128343b2015-01-13 19:31:40 +000079#include "digest.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000080#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "log.h"
82#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110083#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100084#include "channels.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000085#include "ssh.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000086#include "packet.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000087#include "ssherr.h"
88#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100089
90#ifdef PACKET_DEBUG
91#define DBG(x) x
92#else
93#define DBG(x)
94#endif
95
Damien Miller13ae44c2009-01-28 16:38:41 +110096#define PACKET_MAX_SIZE (256 * 1024)
97
Darren Tuckerf7288d72009-06-21 18:12:20 +100098struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100099 u_int32_t seqnr;
100 u_int32_t packets;
101 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000102 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000103};
Damien Miller13ae44c2009-01-28 16:38:41 +1100104
Damien Millera5539d22003-04-09 20:50:06 +1000105struct packet {
106 TAILQ_ENTRY(packet) next;
107 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000108 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000109};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000110
111struct session_state {
112 /*
113 * This variable contains the file descriptors used for
114 * communicating with the other side. connection_in is used for
115 * reading; connection_out for writing. These can be the same
116 * descriptor, in which case it is assumed to be a socket.
117 */
118 int connection_in;
119 int connection_out;
120
121 /* Protocol flags for the remote side. */
122 u_int remote_protocol_flags;
123
124 /* Encryption context for receiving data. Only used for decryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000125 struct sshcipher_ctx receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000126
127 /* Encryption context for sending data. Only used for encryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000128 struct sshcipher_ctx send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000129
130 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000131 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000132
133 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000134 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000135
136 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000137 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000138
139 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000140 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000141
142 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000143 struct sshbuf *compression_buffer;
144
145 /* Incoming/outgoing compression dictionaries */
146 z_stream compression_in_stream;
147 z_stream compression_out_stream;
148 int compression_in_started;
149 int compression_out_started;
150 int compression_in_failures;
151 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000152
153 /*
154 * Flag indicating whether packet compression/decompression is
155 * enabled.
156 */
157 int packet_compression;
158
159 /* default maximum packet size */
160 u_int max_packet_size;
161
162 /* Flag indicating whether this module has been initialized. */
163 int initialized;
164
165 /* Set to true if the connection is interactive. */
166 int interactive_mode;
167
168 /* Set to true if we are the server side. */
169 int server_side;
170
171 /* Set to true if we are authenticated. */
172 int after_authentication;
173
174 int keep_alive_timeouts;
175
176 /* The maximum time that we will wait to send or receive a packet */
177 int packet_timeout_ms;
178
179 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000180 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000181 struct packet_state p_read, p_send;
182
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000183 /* Volume-based rekeying */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000184 u_int64_t max_blocks_in, max_blocks_out, rekey_limit;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000185
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
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000263/* Returns nonzero if rekeying is in progress */
264int
265ssh_packet_is_rekeying(struct ssh *ssh)
266{
djm@openbsd.org292a8de2016-02-17 22:20:14 +0000267 return compat20 &&
268 (ssh->state->rekeying || (ssh->kex != NULL && ssh->kex->done == 0));
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000269}
270
Damien Miller5428f641999-11-25 11:54:57 +1100271/*
272 * Sets the descriptors used for communication. Disables encryption until
273 * packet_set_encryption_key is called.
274 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000275struct ssh *
276ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000278 struct session_state *state;
279 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000280 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000281
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000282 if (none == NULL) {
283 error("%s: cannot load cipher 'none'", __func__);
284 return NULL;
285 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000286 if (ssh == NULL)
287 ssh = ssh_alloc_session_state();
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000288 if (ssh == NULL) {
289 error("%s: cound not allocate state", __func__);
290 return NULL;
291 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000292 state = ssh->state;
293 state->connection_in = fd_in;
294 state->connection_out = fd_out;
295 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000296 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000297 (r = cipher_init(&state->receive_context, none,
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000298 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
299 error("%s: cipher_init failed: %s", __func__, ssh_err(r));
djm@openbsd.org95767262016-03-07 19:02:43 +0000300 free(ssh); /* XXX need ssh_free_session_state? */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000301 return NULL;
302 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000303 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
304 deattack_init(&state->deattack);
djm@openbsd.orgd4c02952015-02-11 01:20:38 +0000305 /*
306 * Cache the IP address of the remote connection for use in error
307 * messages that might be generated after the connection has closed.
308 */
309 (void)ssh_remote_ipaddr(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000310 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311}
312
Darren Tucker3fc464e2008-06-13 06:42:45 +1000313void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000314ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000315{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000316 struct session_state *state = ssh->state;
317
Damien Miller8ed4de82011-12-19 10:52:50 +1100318 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000319 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000320 return;
321 }
322 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000323 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000324 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000325 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000326}
327
markus@openbsd.org091c3022015-01-19 19:52:16 +0000328int
329ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100330{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000331 struct session_state *state = ssh->state;
332 int r;
333
334 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100335 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000336
Damien Miller13ae44c2009-01-28 16:38:41 +1100337 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000338 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000339 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000340 if ((r = sshbuf_put(state->incoming_packet, buf,
341 sizeof(buf))) != 0)
342 return r;
343 (void) mac_compute(state->packet_discard_mac,
344 state->p_read.seqnr,
345 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
346 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100347 }
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000348 logit("Finished discarding for %.200s port %d",
349 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000350 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100351}
352
markus@openbsd.org091c3022015-01-19 19:52:16 +0000353static int
354ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
355 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100356{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000357 struct session_state *state = ssh->state;
358 int r;
359
360 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
361 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
362 return r;
363 return SSH_ERR_MAC_INVALID;
364 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100365 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000366 state->packet_discard_mac = mac;
367 if (sshbuf_len(state->input) >= discard &&
368 (r = ssh_packet_stop_discard(ssh)) != 0)
369 return r;
370 state->packet_discard = discard - sshbuf_len(state->input);
371 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100372}
373
Damien Miller34132e52000-01-14 15:45:46 +1100374/* Returns 1 if remote host is connected via socket, 0 if not. */
375
376int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000377ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100378{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000379 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100380 struct sockaddr_storage from, to;
381 socklen_t fromlen, tolen;
382
djm@openbsd.org95767262016-03-07 19:02:43 +0000383 if (state->connection_in == -1 || state->connection_out == -1)
384 return 0;
385
Damien Miller34132e52000-01-14 15:45:46 +1100386 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000387 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100388 return 1;
389 fromlen = sizeof(from);
390 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000391 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000392 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100393 return 0;
394 tolen = sizeof(to);
395 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000396 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000397 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100398 return 0;
399 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
400 return 0;
401 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
402 return 0;
403 return 1;
404}
405
Ben Lindstromf6027d32002-03-22 01:42:04 +0000406void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000407ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000408{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000409 if (ibytes)
410 *ibytes = ssh->state->p_read.bytes;
411 if (obytes)
412 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000413}
414
415int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000416ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100417{
418 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100419 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100420
421 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000422 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000423 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100424 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000425#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100426 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000427 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000428 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000429#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000430 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100431}
432
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433/* Sets the connection into non-blocking mode. */
434
435void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000436ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437{
Damien Miller95def091999-11-25 00:26:21 +1100438 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000439 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
markus@openbsd.org091c3022015-01-19 19:52:16 +0000441 if (ssh->state->connection_out != ssh->state->connection_in)
442 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443}
444
445/* Returns the socket used for reading. */
446
447int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000448ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000450 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451}
452
453/* Returns the descriptor used for writing. */
454
455int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000456ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000458 return ssh->state->connection_out;
459}
460
461/*
462 * Returns the IP-address of the remote host as a string. The returned
463 * string must not be freed.
464 */
465
466const char *
467ssh_remote_ipaddr(struct ssh *ssh)
468{
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000469 const int sock = ssh->state->connection_in;
470
markus@openbsd.org091c3022015-01-19 19:52:16 +0000471 /* Check whether we have cached the ipaddr. */
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000472 if (ssh->remote_ipaddr == NULL) {
473 if (ssh_packet_connection_is_on_socket(ssh)) {
474 ssh->remote_ipaddr = get_peer_ipaddr(sock);
djm@openbsd.org95767262016-03-07 19:02:43 +0000475 ssh->remote_port = get_peer_port(sock);
476 ssh->local_ipaddr = get_local_ipaddr(sock);
477 ssh->local_port = get_local_port(sock);
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000478 } else {
479 ssh->remote_ipaddr = strdup("UNKNOWN");
djm@openbsd.org95767262016-03-07 19:02:43 +0000480 ssh->remote_port = 65535;
481 ssh->local_ipaddr = strdup("UNKNOWN");
482 ssh->local_port = 65535;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000483 }
484 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000485 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486}
487
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000488/* Returns the port number of the remote host. */
489
490int
491ssh_remote_port(struct ssh *ssh)
492{
493 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
494 return ssh->remote_port;
495}
496
djm@openbsd.org95767262016-03-07 19:02:43 +0000497/*
498 * Returns the IP-address of the local host as a string. The returned
499 * string must not be freed.
500 */
501
502const char *
503ssh_local_ipaddr(struct ssh *ssh)
504{
505 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
506 return ssh->local_ipaddr;
507}
508
509/* Returns the port number of the local host. */
510
511int
512ssh_local_port(struct ssh *ssh)
513{
514 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
515 return ssh->local_port;
516}
517
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518/* Closes the connection and clears and frees internal data structures. */
519
520void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000521ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000523 struct session_state *state = ssh->state;
524 int r;
525 u_int mode;
526
527 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100528 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000529 state->initialized = 0;
530 if (state->connection_in == state->connection_out) {
531 shutdown(state->connection_out, SHUT_RDWR);
532 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100533 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000534 close(state->connection_in);
535 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100536 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000537 sshbuf_free(state->input);
538 sshbuf_free(state->output);
539 sshbuf_free(state->outgoing_packet);
540 sshbuf_free(state->incoming_packet);
541 for (mode = 0; mode < MODE_MAX; mode++)
542 kex_free_newkeys(state->newkeys[mode]);
543 if (state->compression_buffer) {
544 sshbuf_free(state->compression_buffer);
545 if (state->compression_out_started) {
546 z_streamp stream = &state->compression_out_stream;
547 debug("compress outgoing: "
548 "raw data %llu, compressed %llu, factor %.2f",
549 (unsigned long long)stream->total_in,
550 (unsigned long long)stream->total_out,
551 stream->total_in == 0 ? 0.0 :
552 (double) stream->total_out / stream->total_in);
553 if (state->compression_out_failures == 0)
554 deflateEnd(stream);
555 }
556 if (state->compression_in_started) {
557 z_streamp stream = &state->compression_out_stream;
558 debug("compress incoming: "
559 "raw data %llu, compressed %llu, factor %.2f",
560 (unsigned long long)stream->total_out,
561 (unsigned long long)stream->total_in,
562 stream->total_out == 0 ? 0.0 :
563 (double) stream->total_in / stream->total_out);
564 if (state->compression_in_failures == 0)
565 inflateEnd(stream);
566 }
Damien Miller95def091999-11-25 00:26:21 +1100567 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000568 if ((r = cipher_cleanup(&state->send_context)) != 0)
569 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
570 if ((r = cipher_cleanup(&state->receive_context)) != 0)
571 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000572 free(ssh->remote_ipaddr);
573 ssh->remote_ipaddr = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000574 free(ssh->state);
575 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576}
577
578/* Sets remote side protocol flags. */
579
580void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000581ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000583 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584}
585
586/* Returns the remote protocol flags set earlier by the above function. */
587
Ben Lindstrom46c16222000-12-22 01:43:59 +0000588u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000589ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000591 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592}
593
Damien Miller5428f641999-11-25 11:54:57 +1100594/*
595 * Starts packet compression from the next packet on in both directions.
596 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
597 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598
markus@openbsd.org091c3022015-01-19 19:52:16 +0000599static int
600ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000601{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000602 if (!ssh->state->compression_buffer &&
603 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
604 return SSH_ERR_ALLOC_FAIL;
605 return 0;
606}
607
608static int
609start_compression_out(struct ssh *ssh, int level)
610{
611 if (level < 1 || level > 9)
612 return SSH_ERR_INVALID_ARGUMENT;
613 debug("Enabling compression at level %d.", level);
614 if (ssh->state->compression_out_started == 1)
615 deflateEnd(&ssh->state->compression_out_stream);
616 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
617 case Z_OK:
618 ssh->state->compression_out_started = 1;
619 break;
620 case Z_MEM_ERROR:
621 return SSH_ERR_ALLOC_FAIL;
622 default:
623 return SSH_ERR_INTERNAL_ERROR;
624 }
625 return 0;
626}
627
628static int
629start_compression_in(struct ssh *ssh)
630{
631 if (ssh->state->compression_in_started == 1)
632 inflateEnd(&ssh->state->compression_in_stream);
633 switch (inflateInit(&ssh->state->compression_in_stream)) {
634 case Z_OK:
635 ssh->state->compression_in_started = 1;
636 break;
637 case Z_MEM_ERROR:
638 return SSH_ERR_ALLOC_FAIL;
639 default:
640 return SSH_ERR_INTERNAL_ERROR;
641 }
642 return 0;
643}
644
645int
646ssh_packet_start_compression(struct ssh *ssh, int level)
647{
648 int r;
649
650 if (ssh->state->packet_compression && !compat20)
651 return SSH_ERR_INTERNAL_ERROR;
652 ssh->state->packet_compression = 1;
653 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
654 (r = start_compression_in(ssh)) != 0 ||
655 (r = start_compression_out(ssh, level)) != 0)
656 return r;
657 return 0;
658}
659
660/* XXX remove need for separate compression buffer */
661static int
662compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
663{
664 u_char buf[4096];
665 int r, status;
666
667 if (ssh->state->compression_out_started != 1)
668 return SSH_ERR_INTERNAL_ERROR;
669
670 /* This case is not handled below. */
671 if (sshbuf_len(in) == 0)
672 return 0;
673
674 /* Input is the contents of the input buffer. */
675 if ((ssh->state->compression_out_stream.next_in =
676 sshbuf_mutable_ptr(in)) == NULL)
677 return SSH_ERR_INTERNAL_ERROR;
678 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
679
680 /* Loop compressing until deflate() returns with avail_out != 0. */
681 do {
682 /* Set up fixed-size output buffer. */
683 ssh->state->compression_out_stream.next_out = buf;
684 ssh->state->compression_out_stream.avail_out = sizeof(buf);
685
686 /* Compress as much data into the buffer as possible. */
687 status = deflate(&ssh->state->compression_out_stream,
688 Z_PARTIAL_FLUSH);
689 switch (status) {
690 case Z_MEM_ERROR:
691 return SSH_ERR_ALLOC_FAIL;
692 case Z_OK:
693 /* Append compressed data to output_buffer. */
694 if ((r = sshbuf_put(out, buf, sizeof(buf) -
695 ssh->state->compression_out_stream.avail_out)) != 0)
696 return r;
697 break;
698 case Z_STREAM_ERROR:
699 default:
700 ssh->state->compression_out_failures++;
701 return SSH_ERR_INVALID_FORMAT;
702 }
703 } while (ssh->state->compression_out_stream.avail_out == 0);
704 return 0;
705}
706
707static int
708uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
709{
710 u_char buf[4096];
711 int r, status;
712
713 if (ssh->state->compression_in_started != 1)
714 return SSH_ERR_INTERNAL_ERROR;
715
716 if ((ssh->state->compression_in_stream.next_in =
717 sshbuf_mutable_ptr(in)) == NULL)
718 return SSH_ERR_INTERNAL_ERROR;
719 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
720
721 for (;;) {
722 /* Set up fixed-size output buffer. */
723 ssh->state->compression_in_stream.next_out = buf;
724 ssh->state->compression_in_stream.avail_out = sizeof(buf);
725
726 status = inflate(&ssh->state->compression_in_stream,
727 Z_PARTIAL_FLUSH);
728 switch (status) {
729 case Z_OK:
730 if ((r = sshbuf_put(out, buf, sizeof(buf) -
731 ssh->state->compression_in_stream.avail_out)) != 0)
732 return r;
733 break;
734 case Z_BUF_ERROR:
735 /*
736 * Comments in zlib.h say that we should keep calling
737 * inflate() until we get an error. This appears to
738 * be the error that we get.
739 */
740 return 0;
741 case Z_DATA_ERROR:
742 return SSH_ERR_INVALID_FORMAT;
743 case Z_MEM_ERROR:
744 return SSH_ERR_ALLOC_FAIL;
745 case Z_STREAM_ERROR:
746 default:
747 ssh->state->compression_in_failures++;
748 return SSH_ERR_INTERNAL_ERROR;
749 }
750 }
751 /* NOTREACHED */
752}
753
754/* Serialise compression state into a blob for privsep */
755static int
756ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
757{
758 struct session_state *state = ssh->state;
759 struct sshbuf *b;
760 int r;
761
762 if ((b = sshbuf_new()) == NULL)
763 return SSH_ERR_ALLOC_FAIL;
764 if (state->compression_in_started) {
765 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
766 sizeof(state->compression_in_stream))) != 0)
767 goto out;
768 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
769 goto out;
770 if (state->compression_out_started) {
771 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
772 sizeof(state->compression_out_stream))) != 0)
773 goto out;
774 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
775 goto out;
776 r = sshbuf_put_stringb(m, b);
777 out:
778 sshbuf_free(b);
779 return r;
780}
781
782/* Deserialise compression state from a blob for privsep */
783static int
784ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
785{
786 struct session_state *state = ssh->state;
787 struct sshbuf *b = NULL;
788 int r;
789 const u_char *inblob, *outblob;
790 size_t inl, outl;
791
792 if ((r = sshbuf_froms(m, &b)) != 0)
793 goto out;
794 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
795 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
796 goto out;
797 if (inl == 0)
798 state->compression_in_started = 0;
799 else if (inl != sizeof(state->compression_in_stream)) {
800 r = SSH_ERR_INTERNAL_ERROR;
801 goto out;
802 } else {
803 state->compression_in_started = 1;
804 memcpy(&state->compression_in_stream, inblob, inl);
805 }
806 if (outl == 0)
807 state->compression_out_started = 0;
808 else if (outl != sizeof(state->compression_out_stream)) {
809 r = SSH_ERR_INTERNAL_ERROR;
810 goto out;
811 } else {
812 state->compression_out_started = 1;
813 memcpy(&state->compression_out_stream, outblob, outl);
814 }
815 r = 0;
816 out:
817 sshbuf_free(b);
818 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000819}
820
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000822ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
823 void *(*allocfunc)(void *, u_int, u_int),
824 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000826 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
827 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
828 ssh->state->compression_out_stream.opaque = ctx;
829 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
830 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
831 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832}
833
Damien Miller5428f641999-11-25 11:54:57 +1100834/*
Damien Miller5428f641999-11-25 11:54:57 +1100835 * Causes any further packets to be encrypted using the given key. The same
836 * key is used for both sending and reception. However, both directions are
837 * encrypted independently of each other.
838 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000839
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000840void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000841ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000843#ifndef WITH_SSH1
844 fatal("no SSH protocol 1 support");
845#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000846 struct session_state *state = ssh->state;
847 const struct sshcipher *cipher = cipher_by_number(number);
848 int r;
849 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100850
markus@openbsd.org091c3022015-01-19 19:52:16 +0000851 if (cipher == NULL)
852 fatal("%s: unknown cipher number %d", __func__, number);
853 if (keylen < 20)
854 fatal("%s: keylen too small: %d", __func__, keylen);
855 if (keylen > SSH_SESSION_KEY_LENGTH)
856 fatal("%s: keylen too big: %d", __func__, keylen);
857 memcpy(state->ssh1_key, key, keylen);
858 state->ssh1_keylen = keylen;
859 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
860 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
861 (r = cipher_init(&state->receive_context, cipher, key, keylen,
862 NULL, 0, CIPHER_DECRYPT) != 0))
863 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
864 if (!state->cipher_warning_done &&
865 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
866 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
867 error("Warning: %s", wmsg);
868 state->cipher_warning_done = 1;
869 }
Damien Miller773dda22015-01-30 23:10:17 +1100870#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000871}
872
Damien Miller5428f641999-11-25 11:54:57 +1100873/*
874 * Finalizes and sends the packet. If the encryption key has been set,
875 * encrypts the packet before sending.
876 */
Damien Miller95def091999-11-25 00:26:21 +1100877
markus@openbsd.org091c3022015-01-19 19:52:16 +0000878int
879ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000881 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000882 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000883 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000884 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885
Damien Miller5428f641999-11-25 11:54:57 +1100886 /*
887 * If using packet compression, compress the payload of the outgoing
888 * packet.
889 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000890 if (state->packet_compression) {
891 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100892 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000893 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
894 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100895 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000896 if ((r = sshbuf_put(state->compression_buffer,
897 "\0\0\0\0\0\0\0\0", 8)) != 0)
898 goto out;
899 if ((r = compress_buffer(ssh, state->outgoing_packet,
900 state->compression_buffer)) != 0)
901 goto out;
902 sshbuf_reset(state->outgoing_packet);
903 if ((r = sshbuf_putb(state->outgoing_packet,
904 state->compression_buffer)) != 0)
905 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100906 }
907 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000908 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000909
Damien Millere247cc42000-05-07 12:03:14 +1000910 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100911 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000912 if (!state->send_context.plaintext) {
913 cp = sshbuf_mutable_ptr(state->outgoing_packet);
914 if (cp == NULL) {
915 r = SSH_ERR_INTERNAL_ERROR;
916 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100917 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000918 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100919 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000920 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
921 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100922
923 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000924 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
925 sshbuf_len(state->outgoing_packet));
926 POKE_U32(buf, checksum);
927 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
928 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929
930#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100931 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000932 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933#endif
934
Damien Miller95def091999-11-25 00:26:21 +1100935 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000936 POKE_U32(buf, len);
937 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
938 goto out;
939 if ((r = sshbuf_reserve(state->output,
940 sshbuf_len(state->outgoing_packet), &cp)) != 0)
941 goto out;
942 if ((r = cipher_crypt(&state->send_context, 0, cp,
943 sshbuf_ptr(state->outgoing_packet),
944 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
945 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100946
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100948 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000949 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000951 state->p_send.packets++;
952 state->p_send.bytes += len +
953 sshbuf_len(state->outgoing_packet);
954 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955
Damien Miller5428f641999-11-25 11:54:57 +1100956 /*
Damien Miller788f2122005-11-05 15:14:59 +1100957 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000958 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
959 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100960 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000961 r = 0;
962 out:
963 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964}
965
markus@openbsd.org091c3022015-01-19 19:52:16 +0000966int
967ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000968{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000969 struct session_state *state = ssh->state;
970 struct sshenc *enc;
971 struct sshmac *mac;
972 struct sshcomp *comp;
973 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000974 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000975 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000976 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000977
Ben Lindstrom064496f2002-12-23 02:04:22 +0000978 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000979
Damien Miller963f6b22002-02-19 15:21:23 +1100980 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000981 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000982 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000983 state->p_send.packets = state->p_send.blocks = 0;
984 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100985 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000986 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000987 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000988 state->p_read.packets = state->p_read.blocks = 0;
989 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100990 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000991 if (state->newkeys[mode] != NULL) {
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000992 debug("set_newkeys: rekeying, input %llu bytes %llu blocks, "
993 "output %llu bytes %llu blocks",
djm@openbsd.org696d1262016-02-04 23:43:48 +0000994 (unsigned long long)state->p_read.bytes,
995 (unsigned long long)state->p_read.blocks,
996 (unsigned long long)state->p_send.bytes,
997 (unsigned long long)state->p_send.blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000998 if ((r = cipher_cleanup(cc)) != 0)
999 return r;
1000 enc = &state->newkeys[mode]->enc;
1001 mac = &state->newkeys[mode]->mac;
1002 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +10001003 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +11001004 explicit_bzero(enc->iv, enc->iv_len);
1005 explicit_bzero(enc->key, enc->key_len);
1006 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001007 free(enc->name);
1008 free(enc->iv);
1009 free(enc->key);
1010 free(mac->name);
1011 free(mac->key);
1012 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001013 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001014 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001015 /* move newkeys from kex to state */
1016 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
1017 return SSH_ERR_INTERNAL_ERROR;
1018 ssh->kex->newkeys[mode] = NULL;
1019 enc = &state->newkeys[mode]->enc;
1020 mac = &state->newkeys[mode]->mac;
1021 comp = &state->newkeys[mode]->comp;
1022 if (cipher_authlen(enc->cipher) == 0) {
1023 if ((r = mac_init(mac)) != 0)
1024 return r;
1025 }
1026 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001027 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +10001028 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
1029 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001030 return r;
1031 if (!state->cipher_warning_done &&
1032 (wmsg = cipher_warning_message(cc)) != NULL) {
1033 error("Warning: %s", wmsg);
1034 state->cipher_warning_done = 1;
1035 }
Ben Lindstromf6027d32002-03-22 01:42:04 +00001036 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +11001037 /* explicit_bzero(enc->iv, enc->block_size);
1038 explicit_bzero(enc->key, enc->key_len);
1039 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +10001040 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +10001041 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +00001042 state->after_authentication)) && comp->enabled == 0) {
1043 if ((r = ssh_packet_init_compression(ssh)) < 0)
1044 return r;
1045 if (mode == MODE_OUT) {
1046 if ((r = start_compression_out(ssh, 6)) != 0)
1047 return r;
1048 } else {
1049 if ((r = start_compression_in(ssh)) != 0)
1050 return r;
1051 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001052 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001053 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001054 /*
1055 * The 2^(blocksize*2) limit is too expensive for 3DES,
1056 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1057 */
1058 if (enc->block_size >= 16)
1059 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1060 else
1061 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001062 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001063 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001064 state->rekey_limit / enc->block_size);
djm@openbsd.org696d1262016-02-04 23:43:48 +00001065 debug("rekey after %llu blocks", (unsigned long long)*max_blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001066 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001067}
1068
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001069#define MAX_PACKETS (1U<<31)
1070static int
1071ssh_packet_need_rekeying(struct ssh *ssh, u_int outbound_packet_len)
1072{
1073 struct session_state *state = ssh->state;
1074 u_int32_t out_blocks;
1075
1076 /* XXX client can't cope with rekeying pre-auth */
1077 if (!state->after_authentication)
1078 return 0;
1079
1080 /* Haven't keyed yet or KEX in progress. */
1081 if (ssh->kex == NULL || ssh_packet_is_rekeying(ssh))
1082 return 0;
1083
1084 /* Peer can't rekey */
1085 if (ssh->compat & SSH_BUG_NOREKEY)
1086 return 0;
1087
1088 /*
1089 * Permit one packet in or out per rekey - this allows us to
1090 * make progress when rekey limits are very small.
1091 */
1092 if (state->p_send.packets == 0 && state->p_read.packets == 0)
1093 return 0;
1094
1095 /* Time-based rekeying */
1096 if (state->rekey_interval != 0 &&
1097 state->rekey_time + state->rekey_interval <= monotime())
1098 return 1;
1099
1100 /* Always rekey when MAX_PACKETS sent in either direction */
1101 if (state->p_send.packets > MAX_PACKETS ||
1102 state->p_read.packets > MAX_PACKETS)
1103 return 1;
1104
1105 /* Rekey after (cipher-specific) maxiumum blocks */
1106 out_blocks = roundup(outbound_packet_len,
1107 state->newkeys[MODE_OUT]->enc.block_size);
1108 return (state->max_blocks_out &&
1109 (state->p_send.blocks + out_blocks > state->max_blocks_out)) ||
1110 (state->max_blocks_in &&
1111 (state->p_read.blocks > state->max_blocks_in));
1112}
1113
Damien Miller5428f641999-11-25 11:54:57 +11001114/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001115 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001116 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001117 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1118 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001119static int
1120ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001121{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001122 struct session_state *state = ssh->state;
1123 struct sshcomp *comp = NULL;
1124 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001125
1126 /*
1127 * Remember that we are past the authentication step, so rekeying
1128 * with COMP_DELAYED will turn on compression immediately.
1129 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001130 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001131 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001132 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001133 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001134 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001135 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001136 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001137 if ((r = ssh_packet_init_compression(ssh)) != 0)
1138 return r;
1139 if (mode == MODE_OUT) {
1140 if ((r = start_compression_out(ssh, 6)) != 0)
1141 return r;
1142 } else {
1143 if ((r = start_compression_in(ssh)) != 0)
1144 return r;
1145 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001146 comp->enabled = 1;
1147 }
1148 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001149 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001150}
1151
djm@openbsd.org28136472016-01-29 05:46:01 +00001152/* Used to mute debug logging for noisy packet types */
1153static int
1154ssh_packet_log_type(u_char type)
1155{
1156 switch (type) {
1157 case SSH2_MSG_CHANNEL_DATA:
1158 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
1159 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
1160 return 0;
1161 default:
1162 return 1;
1163 }
1164}
1165
Damien Miller9786e6e2005-07-26 21:54:56 +10001166/*
Damien Miller33b13562000-04-04 14:38:59 +10001167 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1168 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001169int
1170ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001171{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001172 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001173 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001174 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001175 u_int authlen = 0, aadlen = 0;
1176 u_int len;
1177 struct sshenc *enc = NULL;
1178 struct sshmac *mac = NULL;
1179 struct sshcomp *comp = NULL;
1180 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001181
markus@openbsd.org091c3022015-01-19 19:52:16 +00001182 if (state->newkeys[MODE_OUT] != NULL) {
1183 enc = &state->newkeys[MODE_OUT]->enc;
1184 mac = &state->newkeys[MODE_OUT]->mac;
1185 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001186 /* disable mac for authenticated encryption */
1187 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1188 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001189 }
Damien Miller963f6b22002-02-19 15:21:23 +11001190 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001191 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001192
markus@openbsd.org091c3022015-01-19 19:52:16 +00001193 type = (sshbuf_ptr(state->outgoing_packet))[5];
djm@openbsd.org28136472016-01-29 05:46:01 +00001194 if (ssh_packet_log_type(type))
1195 debug3("send packet: type %u", type);
Damien Miller33b13562000-04-04 14:38:59 +10001196#ifdef PACKET_DEBUG
1197 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001198 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001199#endif
1200
1201 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001202 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001203 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001204 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1205 goto out;
1206 sshbuf_reset(state->compression_buffer);
1207 if ((r = compress_buffer(ssh, state->outgoing_packet,
1208 state->compression_buffer)) != 0)
1209 goto out;
1210 sshbuf_reset(state->outgoing_packet);
1211 if ((r = sshbuf_put(state->outgoing_packet,
1212 "\0\0\0\0\0", 5)) != 0 ||
1213 (r = sshbuf_putb(state->outgoing_packet,
1214 state->compression_buffer)) != 0)
1215 goto out;
1216 DBG(debug("compression: raw %d compressed %zd", len,
1217 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001218 }
1219
1220 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001221 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001222
1223 /*
1224 * calc size of padding, alloc space, get random data,
1225 * minimum padding is 4 bytes
1226 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001227 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001228 padlen = block_size - (len % block_size);
1229 if (padlen < 4)
1230 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001231 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001232 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001233 state->extra_pad =
1234 roundup(state->extra_pad, block_size);
1235 pad = state->extra_pad -
1236 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001237 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001238 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001239 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001240 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001241 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001242 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1243 goto out;
1244 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001245 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001246 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001247 } else {
1248 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001249 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001250 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001251 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001252 len = sshbuf_len(state->outgoing_packet);
1253 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1254 if (cp == NULL) {
1255 r = SSH_ERR_INTERNAL_ERROR;
1256 goto out;
1257 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001258 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001259 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001260 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001261 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1262 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001263
1264 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001265 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001266 if ((r = mac_compute(mac, state->p_send.seqnr,
1267 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001268 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001269 goto out;
1270 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001271 }
1272 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001273 if ((r = sshbuf_reserve(state->output,
1274 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1275 goto out;
1276 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1277 sshbuf_ptr(state->outgoing_packet),
1278 len - aadlen, aadlen, authlen)) != 0)
1279 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001280 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001281 if (mac && mac->enabled) {
1282 if (mac->etm) {
1283 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001284 if ((r = mac_compute(mac, state->p_send.seqnr,
1285 cp, len, macbuf, sizeof(macbuf))) != 0)
1286 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001287 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001288 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001289 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001290 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1291 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001292 }
Damien Miller33b13562000-04-04 14:38:59 +10001293#ifdef PACKET_DEBUG
1294 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001295 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001296#endif
Damien Miller4af51302000-04-16 11:18:38 +10001297 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001298 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001299 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001300 if (++state->p_send.packets == 0)
1301 if (!(ssh->compat & SSH_BUG_NOREKEY))
1302 return SSH_ERR_NEED_REKEY;
1303 state->p_send.blocks += len / block_size;
1304 state->p_send.bytes += len;
1305 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001306
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001307 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001308 r = ssh_set_newkeys(ssh, MODE_OUT);
1309 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1310 r = ssh_packet_enable_delayed_compress(ssh);
1311 else
1312 r = 0;
1313 out:
1314 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001315}
1316
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001317/* returns non-zero if the specified packet type is usec by KEX */
1318static int
1319ssh_packet_type_is_kex(u_char type)
1320{
1321 return
1322 type >= SSH2_MSG_TRANSPORT_MIN &&
1323 type <= SSH2_MSG_TRANSPORT_MAX &&
1324 type != SSH2_MSG_SERVICE_REQUEST &&
1325 type != SSH2_MSG_SERVICE_ACCEPT &&
1326 type != SSH2_MSG_EXT_INFO;
1327}
1328
markus@openbsd.org091c3022015-01-19 19:52:16 +00001329int
1330ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001331{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001332 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001333 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001334 u_char type;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001335 int r, need_rekey;
Damien Millera5539d22003-04-09 20:50:06 +10001336
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001337 if (sshbuf_len(state->outgoing_packet) < 6)
1338 return SSH_ERR_INTERNAL_ERROR;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001339 type = sshbuf_ptr(state->outgoing_packet)[5];
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001340 need_rekey = !ssh_packet_type_is_kex(type) &&
1341 ssh_packet_need_rekeying(ssh, sshbuf_len(state->outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +10001342
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001343 /*
1344 * During rekeying we can only send key exchange messages.
1345 * Queue everything else.
1346 */
1347 if ((need_rekey || state->rekeying) && !ssh_packet_type_is_kex(type)) {
1348 if (need_rekey)
1349 debug3("%s: rekex triggered", __func__);
1350 debug("enqueue packet: %u", type);
1351 p = calloc(1, sizeof(*p));
1352 if (p == NULL)
1353 return SSH_ERR_ALLOC_FAIL;
1354 p->type = type;
1355 p->payload = state->outgoing_packet;
1356 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1357 state->outgoing_packet = sshbuf_new();
1358 if (state->outgoing_packet == NULL)
1359 return SSH_ERR_ALLOC_FAIL;
1360 if (need_rekey) {
1361 /*
1362 * This packet triggered a rekey, so send the
1363 * KEXINIT now.
1364 * NB. reenters this function via kex_start_rekex().
1365 */
1366 return kex_start_rekex(ssh);
Damien Millera5539d22003-04-09 20:50:06 +10001367 }
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001368 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001369 }
1370
1371 /* rekeying starts with sending KEXINIT */
1372 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001373 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001374
markus@openbsd.org091c3022015-01-19 19:52:16 +00001375 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1376 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001377
1378 /* after a NEWKEYS message we can send the complete queue */
1379 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001380 state->rekeying = 0;
1381 state->rekey_time = monotime();
1382 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001383 type = p->type;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001384 /*
1385 * If this packet triggers a rekex, then skip the
1386 * remaining packets in the queue for now.
1387 * NB. re-enters this function via kex_start_rekex.
1388 */
1389 if (ssh_packet_need_rekeying(ssh,
1390 sshbuf_len(p->payload))) {
1391 debug3("%s: queued packet triggered rekex",
1392 __func__);
1393 return kex_start_rekex(ssh);
1394 }
Damien Millera5539d22003-04-09 20:50:06 +10001395 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001396 sshbuf_free(state->outgoing_packet);
1397 state->outgoing_packet = p->payload;
1398 TAILQ_REMOVE(&state->outgoing, p, next);
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001399 memset(p, 0, sizeof(*p));
Darren Tuckera627d422013-06-02 07:31:17 +10001400 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001401 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1402 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001403 }
1404 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001405 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001406}
1407
Damien Miller33b13562000-04-04 14:38:59 +10001408/*
Damien Miller5428f641999-11-25 11:54:57 +11001409 * Waits until a packet has been received, and returns its type. Note that
1410 * no other data is processed until this returns, so this function should not
1411 * be used during the interactive session.
1412 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413
1414int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001415ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001417 struct session_state *state = ssh->state;
markus@openbsd.orga3068632016-01-14 16:17:39 +00001418 int len, r, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001419 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001420 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001421 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001422
Darren Tucker99bb7612008-06-13 22:02:50 +10001423 DBG(debug("packet_read()"));
1424
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001425 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001426 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001427 if (setp == NULL)
1428 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001429
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001430 /*
1431 * Since we are blocking, ensure that all written packets have
1432 * been sent.
1433 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001434 if ((r = ssh_packet_write_wait(ssh)) != 0)
1435 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436
Damien Miller95def091999-11-25 00:26:21 +11001437 /* Stay in the loop until we have received a complete packet. */
1438 for (;;) {
1439 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001440 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1441 if (r != 0)
1442 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001443 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001444 *typep == SSH_SMSG_SUCCESS
1445 || *typep == SSH_SMSG_FAILURE
1446 || *typep == SSH_CMSG_EOF
1447 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1448 if ((r = sshpkt_get_end(ssh)) != 0)
1449 break;
Damien Miller95def091999-11-25 00:26:21 +11001450 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001451 if (*typep != SSH_MSG_NONE)
1452 break;
Damien Miller5428f641999-11-25 11:54:57 +11001453 /*
1454 * Otherwise, wait for some data to arrive, add it to the
1455 * buffer, and try again.
1456 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001457 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001458 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001459 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001460
markus@openbsd.org091c3022015-01-19 19:52:16 +00001461 if (state->packet_timeout_ms > 0) {
1462 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001463 timeoutp = &timeout;
1464 }
Damien Miller95def091999-11-25 00:26:21 +11001465 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001466 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001467 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001468 ms_to_timeval(&timeout, ms_remain);
1469 gettimeofday(&start, NULL);
1470 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001471 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001472 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001473 break;
Damien Millerea437422009-10-02 11:49:03 +10001474 if (errno != EAGAIN && errno != EINTR &&
1475 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001476 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001477 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001478 continue;
1479 ms_subtract_diff(&start, &ms_remain);
1480 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001481 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001482 break;
1483 }
1484 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001485 if (r == 0)
1486 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001487 /* Read data from the socket. */
markus@openbsd.orga3068632016-01-14 16:17:39 +00001488 len = read(state->connection_in, buf, sizeof(buf));
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001489 if (len == 0) {
1490 r = SSH_ERR_CONN_CLOSED;
1491 goto out;
1492 }
1493 if (len < 0) {
1494 r = SSH_ERR_SYSTEM_ERROR;
1495 goto out;
1496 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001497
Damien Miller95def091999-11-25 00:26:21 +11001498 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001499 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001500 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001501 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001502 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001503 free(setp);
1504 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001505}
1506
Damien Miller278f9072001-12-21 15:00:19 +11001507int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001508ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001509{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001510 u_char type;
1511 int r;
1512
1513 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1514 fatal("%s: %s", __func__, ssh_err(r));
1515 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001516}
1517
Damien Miller5428f641999-11-25 11:54:57 +11001518/*
1519 * Waits until a packet has been received, verifies that its type matches
1520 * that given, and gives a fatal error and exits if there is a mismatch.
1521 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001522
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001523int
1524ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001526 int r;
1527 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001528
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001529 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1530 return r;
1531 if (type != expected_type) {
1532 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001533 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001534 expected_type, type)) != 0)
1535 return r;
1536 return SSH_ERR_PROTOCOL_ERROR;
1537 }
1538 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539}
1540
1541/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001542 * packet_process_incoming. If so, reads the packet; otherwise returns
1543 * SSH_MSG_NONE. This does not wait for data from the connection.
1544 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001545 * SSH_MSG_DISCONNECT is handled specially here. Also,
1546 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1547 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001548 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549
markus@openbsd.org091c3022015-01-19 19:52:16 +00001550int
1551ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001553 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001554 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001555 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001556 const u_char *cp;
1557 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001558 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001559 int r;
1560
1561 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001562
Damien Miller95def091999-11-25 00:26:21 +11001563 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001564 if (sshbuf_len(state->input) < 4 + 8)
1565 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001566 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001567 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001568 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1569 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1570 len)) != 0)
1571 return r;
1572 return SSH_ERR_CONN_CORRUPT;
1573 }
Damien Miller95def091999-11-25 00:26:21 +11001574 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001575
Damien Miller95def091999-11-25 00:26:21 +11001576 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001577 if (sshbuf_len(state->input) < 4 + padded_len)
1578 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001579
Damien Miller95def091999-11-25 00:26:21 +11001580 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001581
Damien Miller95def091999-11-25 00:26:21 +11001582 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001583 if ((r = sshbuf_consume(state->input, 4)) != 0)
1584 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001585
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001586 /*
1587 * Cryptographic attack detector for ssh
1588 * (C)1998 CORE-SDI, Buenos Aires Argentina
1589 * Ariel Futoransky(futo@core-sdi.com)
1590 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001591 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001592 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001593 switch (detect_attack(&state->deattack,
1594 sshbuf_ptr(state->input), padded_len)) {
1595 case DEATTACK_OK:
1596 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001597 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001598 emsg = "crc32 compensation attack detected";
1599 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001600 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001601 emsg = "deattack denial of service detected";
1602 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001603 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001604 emsg = "deattack error";
1605 break;
1606 }
1607 if (emsg != NULL) {
1608 error("%s", emsg);
1609 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1610 (r = ssh_packet_write_wait(ssh)) != 0)
1611 return r;
1612 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001613 }
1614 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001615
1616 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001617 sshbuf_reset(state->incoming_packet);
1618 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1619 goto out;
1620 if ((r = cipher_crypt(&state->receive_context, 0, p,
1621 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1622 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001623
markus@openbsd.org091c3022015-01-19 19:52:16 +00001624 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1625 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001626
1627#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001628 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001629 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001630#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001631
Damien Miller95def091999-11-25 00:26:21 +11001632 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001633 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1634 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001635
Damien Miller95def091999-11-25 00:26:21 +11001636 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001637 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1638 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001639
Damien Miller95def091999-11-25 00:26:21 +11001640 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001641 if (len != sshbuf_len(state->incoming_packet)) {
1642 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001643 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001644 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1645 (r = ssh_packet_write_wait(ssh)) != 0)
1646 return r;
1647 return SSH_ERR_CONN_CORRUPT;
1648 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001649
markus@openbsd.org091c3022015-01-19 19:52:16 +00001650 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1651 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001652 if (checksum != stored_checksum) {
1653 error("Corrupted check bytes on input");
1654 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1655 (r = ssh_packet_write_wait(ssh)) != 0)
1656 return r;
1657 return SSH_ERR_CONN_CORRUPT;
1658 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001659 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1660 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001661
markus@openbsd.org091c3022015-01-19 19:52:16 +00001662 if (state->packet_compression) {
1663 sshbuf_reset(state->compression_buffer);
1664 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1665 state->compression_buffer)) != 0)
1666 goto out;
1667 sshbuf_reset(state->incoming_packet);
1668 if ((r = sshbuf_putb(state->incoming_packet,
1669 state->compression_buffer)) != 0)
1670 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001671 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001672 state->p_read.packets++;
1673 state->p_read.bytes += padded_len + 4;
1674 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1675 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001676 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1677 error("Invalid ssh1 packet type: %d", *typep);
1678 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1679 (r = ssh_packet_write_wait(ssh)) != 0)
1680 return r;
1681 return SSH_ERR_PROTOCOL_ERROR;
1682 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001683 r = 0;
1684 out:
1685 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001686}
Damien Miller95def091999-11-25 00:26:21 +11001687
markus@openbsd.org091c3022015-01-19 19:52:16 +00001688int
1689ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001690{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001691 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001692 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001693 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1694 u_int maclen, aadlen = 0, authlen = 0, block_size;
1695 struct sshenc *enc = NULL;
1696 struct sshmac *mac = NULL;
1697 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001698 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001699
markus@openbsd.org091c3022015-01-19 19:52:16 +00001700 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001701
markus@openbsd.org091c3022015-01-19 19:52:16 +00001702 if (state->packet_discard)
1703 return 0;
1704
1705 if (state->newkeys[MODE_IN] != NULL) {
1706 enc = &state->newkeys[MODE_IN]->enc;
1707 mac = &state->newkeys[MODE_IN]->mac;
1708 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001709 /* disable mac for authenticated encryption */
1710 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1711 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001712 }
1713 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001714 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001715 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001716
markus@openbsd.org091c3022015-01-19 19:52:16 +00001717 if (aadlen && state->packlen == 0) {
1718 if (cipher_get_length(&state->receive_context,
1719 &state->packlen, state->p_read.seqnr,
1720 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1721 return 0;
1722 if (state->packlen < 1 + 4 ||
1723 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001724#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001725 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001726#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001727 logit("Bad packet length %u.", state->packlen);
1728 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1729 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001730 return SSH_ERR_CONN_CORRUPT;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001731 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001732 sshbuf_reset(state->incoming_packet);
1733 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001734 /*
1735 * check if input size is less than the cipher block size,
1736 * decrypt first block and extract length of incoming packet
1737 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001738 if (sshbuf_len(state->input) < block_size)
1739 return 0;
1740 sshbuf_reset(state->incoming_packet);
1741 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1742 &cp)) != 0)
1743 goto out;
1744 if ((r = cipher_crypt(&state->receive_context,
1745 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1746 block_size, 0, 0)) != 0)
1747 goto out;
1748 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1749 if (state->packlen < 1 + 4 ||
1750 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001751#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001752 fprintf(stderr, "input: \n");
1753 sshbuf_dump(state->input, stderr);
1754 fprintf(stderr, "incoming_packet: \n");
1755 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001756#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001757 logit("Bad packet length %u.", state->packlen);
1758 return ssh_packet_start_discard(ssh, enc, mac,
1759 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001760 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001761 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1762 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001763 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001764 DBG(debug("input: packet len %u", state->packlen+4));
1765
Damien Milleraf43a7a2012-12-12 10:46:31 +11001766 if (aadlen) {
1767 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001768 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001769 } else {
1770 /*
1771 * the payload size and the payload are encrypted, but we
1772 * have a partial packet of block_size bytes
1773 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001774 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001775 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001776 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1777 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001778 if (need % block_size != 0) {
1779 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001780 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001781 return ssh_packet_start_discard(ssh, enc, mac,
1782 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001783 }
Damien Miller33b13562000-04-04 14:38:59 +10001784 /*
1785 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001786 * decrypt into incoming_packet:
1787 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001788 * 'need' bytes are encrypted, followed by either
1789 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001790 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001791 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001792 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1793 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001794#ifdef PACKET_DEBUG
1795 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001796 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001797#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001798 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001799 if (mac && mac->enabled && mac->etm) {
1800 if ((r = mac_compute(mac, state->p_read.seqnr,
1801 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001802 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001803 goto out;
1804 }
1805 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1806 &cp)) != 0)
1807 goto out;
1808 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1809 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1810 goto out;
1811 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1812 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001813 /*
1814 * compute MAC over seqnr and packet,
1815 * increment sequence number for incoming packet
1816 */
Damien Miller4af51302000-04-16 11:18:38 +10001817 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001818 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001819 if ((r = mac_compute(mac, state->p_read.seqnr,
1820 sshbuf_ptr(state->incoming_packet),
1821 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001822 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001823 goto out;
1824 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001825 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001826 logit("Corrupted MAC on input.");
1827 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001828 return SSH_ERR_INTERNAL_ERROR;
1829 return ssh_packet_start_discard(ssh, enc, mac,
1830 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001831 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001832
1833 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1834 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1835 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001836 }
Damien Miller278f9072001-12-21 15:00:19 +11001837 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001838 *seqnr_p = state->p_read.seqnr;
1839 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001840 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001841 if (++state->p_read.packets == 0)
1842 if (!(ssh->compat & SSH_BUG_NOREKEY))
1843 return SSH_ERR_NEED_REKEY;
1844 state->p_read.blocks += (state->packlen + 4) / block_size;
1845 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001846
1847 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001848 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001849 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001850 if (padlen < 4) {
1851 if ((r = sshpkt_disconnect(ssh,
1852 "Corrupted padlen %d on input.", padlen)) != 0 ||
1853 (r = ssh_packet_write_wait(ssh)) != 0)
1854 return r;
1855 return SSH_ERR_CONN_CORRUPT;
1856 }
Damien Miller33b13562000-04-04 14:38:59 +10001857
1858 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001859 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1860 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1861 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001862
markus@openbsd.org091c3022015-01-19 19:52:16 +00001863 DBG(debug("input: len before de-compress %zd",
1864 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001865 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001866 sshbuf_reset(state->compression_buffer);
1867 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1868 state->compression_buffer)) != 0)
1869 goto out;
1870 sshbuf_reset(state->incoming_packet);
1871 if ((r = sshbuf_putb(state->incoming_packet,
1872 state->compression_buffer)) != 0)
1873 goto out;
1874 DBG(debug("input: len after de-compress %zd",
1875 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001876 }
1877 /*
1878 * get packet type, implies consume.
1879 * return length of payload (without type field)
1880 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001881 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1882 goto out;
djm@openbsd.org28136472016-01-29 05:46:01 +00001883 if (ssh_packet_log_type(*typep))
1884 debug3("receive packet: type %u", *typep);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001885 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1886 if ((r = sshpkt_disconnect(ssh,
1887 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1888 (r = ssh_packet_write_wait(ssh)) != 0)
1889 return r;
1890 return SSH_ERR_PROTOCOL_ERROR;
1891 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001892 if (*typep == SSH2_MSG_NEWKEYS)
1893 r = ssh_set_newkeys(ssh, MODE_IN);
1894 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1895 r = ssh_packet_enable_delayed_compress(ssh);
1896 else
1897 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001898#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001899 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1900 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001901#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001902 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001903 state->packlen = 0;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001904
1905 /* do we need to rekey? */
1906 if (ssh_packet_need_rekeying(ssh, 0)) {
1907 debug3("%s: rekex triggered", __func__);
1908 if ((r = kex_start_rekex(ssh)) != 0)
1909 return r;
1910 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001911 out:
1912 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001913}
1914
1915int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001916ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001917{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001918 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001919 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001920 int r;
1921 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001922
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001923 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001924 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001925 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001926 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1927 if (r != 0)
1928 return r;
1929 if (*typep) {
1930 state->keep_alive_timeouts = 0;
1931 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001932 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001933 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001934 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001935 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001936 break;
Damien Miller33b13562000-04-04 14:38:59 +10001937 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001938 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1939 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1940 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001941 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001942 return r;
1943 }
Damien Miller33b13562000-04-04 14:38:59 +10001944 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001945 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001946 break;
1947 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001948 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1949 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1950 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001951 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001952 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001953 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1954 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001955 "Received disconnect from %s port %d:"
1956 "%u: %.400s", ssh_remote_ipaddr(ssh),
1957 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001958 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001959 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001960 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001961 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1962 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001963 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1964 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001965 break;
Damien Miller33b13562000-04-04 14:38:59 +10001966 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001967 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001968 }
Damien Miller33b13562000-04-04 14:38:59 +10001969 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001970 r = ssh_packet_read_poll1(ssh, typep);
1971 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001972 case SSH_MSG_NONE:
1973 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001974 case SSH_MSG_IGNORE:
1975 break;
1976 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001977 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1978 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001979 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001980 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001981 break;
1982 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001983 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1984 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001985 error("Received disconnect from %s port %d: "
1986 "%.400s", ssh_remote_ipaddr(ssh),
1987 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001988 free(msg);
1989 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001990 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001991 DBG(debug("received packet type %d", *typep));
1992 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001993 }
Damien Miller33b13562000-04-04 14:38:59 +10001994 }
1995 }
1996}
1997
Damien Miller5428f641999-11-25 11:54:57 +11001998/*
1999 * Buffers the given amount of input characters. This is intended to be used
2000 * together with packet_read_poll.
2001 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002002
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002003int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002004ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002005{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002006 struct session_state *state = ssh->state;
2007 int r;
2008
2009 if (state->packet_discard) {
2010 state->keep_alive_timeouts = 0; /* ?? */
2011 if (len >= state->packet_discard) {
2012 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002013 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002014 }
2015 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002016 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11002017 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002018 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002019 return r;
2020
2021 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002022}
2023
Damien Miller4af51302000-04-16 11:18:38 +10002024int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002025ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10002026{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002027 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10002028}
2029
Damien Miller5428f641999-11-25 11:54:57 +11002030/*
2031 * Sends a diagnostic message from the server to the client. This message
2032 * can be sent at any time (but not while constructing another message). The
2033 * message is printed immediately, but only if the client is being executed
2034 * in verbose mode. These messages are primarily intended to ease debugging
2035 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002036 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11002037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002038void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002039ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002040{
Damien Miller95def091999-11-25 00:26:21 +11002041 char buf[1024];
2042 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002043 int r;
Damien Miller95def091999-11-25 00:26:21 +11002044
markus@openbsd.org091c3022015-01-19 19:52:16 +00002045 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00002046 return;
2047
Damien Miller95def091999-11-25 00:26:21 +11002048 va_start(args, fmt);
2049 vsnprintf(buf, sizeof(buf), fmt, args);
2050 va_end(args);
2051
Damien Miller7c8af4f2000-05-01 08:24:07 +10002052 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002053 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
2054 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
2055 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2056 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2057 (r = sshpkt_send(ssh)) != 0)
2058 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002059 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002060 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
2061 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2062 (r = sshpkt_send(ssh)) != 0)
2063 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002064 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002065 if ((r = ssh_packet_write_wait(ssh)) != 0)
2066 fatal("%s: %s", __func__, ssh_err(r));
2067}
2068
2069/*
2070 * Pretty-print connection-terminating errors and exit.
2071 */
2072void
2073sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
2074{
2075 switch (r) {
2076 case SSH_ERR_CONN_CLOSED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002077 logit("Connection closed by %.200s port %d",
2078 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002079 cleanup_exit(255);
2080 case SSH_ERR_CONN_TIMEOUT:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002081 logit("Connection %s %.200s port %d timed out",
2082 ssh->state->server_side ? "from" : "to",
2083 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002084 cleanup_exit(255);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002085 case SSH_ERR_DISCONNECTED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002086 logit("Disconnected from %.200s port %d",
2087 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002088 cleanup_exit(255);
2089 case SSH_ERR_SYSTEM_ERROR:
2090 if (errno == ECONNRESET) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002091 logit("Connection reset by %.200s port %d",
2092 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002093 cleanup_exit(255);
2094 }
2095 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002096 case SSH_ERR_NO_CIPHER_ALG_MATCH:
2097 case SSH_ERR_NO_MAC_ALG_MATCH:
2098 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
2099 case SSH_ERR_NO_KEX_ALG_MATCH:
2100 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
2101 if (ssh && ssh->kex && ssh->kex->failed_choice) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002102 fatal("Unable to negotiate with %.200s port %d: %s. "
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002103 "Their offer: %s", ssh_remote_ipaddr(ssh),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002104 ssh_remote_port(ssh), ssh_err(r),
2105 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002106 }
2107 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002108 default:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002109 fatal("%s%sConnection %s %.200s port %d: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002110 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002111 ssh->state->server_side ? "from" : "to",
2112 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002113 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002114}
2115
Damien Miller5428f641999-11-25 11:54:57 +11002116/*
2117 * Logs the error plus constructs and sends a disconnect packet, closes the
2118 * connection, and exits. This function never returns. The error message
2119 * should not contain a newline. The length of the formatted message must
2120 * not exceed 1024 bytes.
2121 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002122void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002123ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002124{
Damien Miller95def091999-11-25 00:26:21 +11002125 char buf[1024];
2126 va_list args;
2127 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002128 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002129
Damien Miller95def091999-11-25 00:26:21 +11002130 if (disconnecting) /* Guard against recursive invocations. */
2131 fatal("packet_disconnect called recursively.");
2132 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002133
Damien Miller5428f641999-11-25 11:54:57 +11002134 /*
2135 * Format the message. Note that the caller must make sure the
2136 * message is of limited size.
2137 */
Damien Miller95def091999-11-25 00:26:21 +11002138 va_start(args, fmt);
2139 vsnprintf(buf, sizeof(buf), fmt, args);
2140 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002141
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002142 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10002143 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002144
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002145 /*
2146 * Send the disconnect message to the other side, and wait
2147 * for it to get sent.
2148 */
2149 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2150 sshpkt_fatal(ssh, __func__, r);
2151
2152 if ((r = ssh_packet_write_wait(ssh)) != 0)
2153 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002154
Damien Miller95def091999-11-25 00:26:21 +11002155 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002156 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002157 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002158}
2159
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002160/*
2161 * Checks if there is any buffered output, and tries to write some of
2162 * the output.
2163 */
2164int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002165ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002166{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002167 struct session_state *state = ssh->state;
2168 int len = sshbuf_len(state->output);
markus@openbsd.orga3068632016-01-14 16:17:39 +00002169 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002170
Damien Miller95def091999-11-25 00:26:21 +11002171 if (len > 0) {
markus@openbsd.orga3068632016-01-14 16:17:39 +00002172 len = write(state->connection_out,
2173 sshbuf_ptr(state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10002174 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002175 if (errno == EINTR || errno == EAGAIN ||
2176 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002177 return 0;
2178 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002179 }
markus@openbsd.orga3068632016-01-14 16:17:39 +00002180 if (len == 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002181 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002182 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002183 return r;
Damien Miller95def091999-11-25 00:26:21 +11002184 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002185 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002186}
2187
Damien Miller5428f641999-11-25 11:54:57 +11002188/*
2189 * Calls packet_write_poll repeatedly until all pending output data has been
2190 * written.
2191 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002192int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002193ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002194{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002195 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002196 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002197 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002198 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002199
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002200 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002201 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002202 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002203 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002204 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2205 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002206 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002207 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002208 while (ssh_packet_have_data_to_write(ssh)) {
2209 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002210 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002211 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002212
markus@openbsd.org091c3022015-01-19 19:52:16 +00002213 if (state->packet_timeout_ms > 0) {
2214 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002215 timeoutp = &timeout;
2216 }
2217 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002218 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002219 ms_to_timeval(&timeout, ms_remain);
2220 gettimeofday(&start, NULL);
2221 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002222 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002223 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002224 break;
Damien Millerea437422009-10-02 11:49:03 +10002225 if (errno != EAGAIN && errno != EINTR &&
2226 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002227 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002228 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002229 continue;
2230 ms_subtract_diff(&start, &ms_remain);
2231 if (ms_remain <= 0) {
2232 ret = 0;
2233 break;
2234 }
2235 }
2236 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002237 free(setp);
2238 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002239 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002240 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2241 free(setp);
2242 return r;
2243 }
Damien Miller95def091999-11-25 00:26:21 +11002244 }
Darren Tuckera627d422013-06-02 07:31:17 +10002245 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002246 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002247}
2248
2249/* Returns true if there is buffered data to write to the connection. */
2250
2251int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002252ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002253{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002254 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002255}
2256
2257/* Returns true if there is not too much data to write to the connection. */
2258
2259int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002260ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002261{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002262 if (ssh->state->interactive_mode)
2263 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002264 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002265 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002266}
2267
markus@openbsd.org091c3022015-01-19 19:52:16 +00002268void
2269ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002270{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002271#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002272 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002273 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002274 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002275# ifdef IP_TOS
2276 case AF_INET:
2277 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002278 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002279 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2280 error("setsockopt IP_TOS %d: %.100s:",
2281 tos, strerror(errno));
2282 break;
2283# endif /* IP_TOS */
2284# ifdef IPV6_TCLASS
2285 case AF_INET6:
2286 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002287 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002288 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2289 error("setsockopt IPV6_TCLASS %d: %.100s:",
2290 tos, strerror(errno));
2291 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002292# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002293 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002294#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002295}
Ben Lindstroma7433982002-12-23 02:41:41 +00002296
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002297/* Informs that the current session is interactive. Sets IP flags for that. */
2298
2299void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002300ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002301{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002302 struct session_state *state = ssh->state;
2303
2304 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002305 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002306 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002307
Damien Miller95def091999-11-25 00:26:21 +11002308 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002309 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002310
Damien Miller34132e52000-01-14 15:45:46 +11002311 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002312 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002313 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002314 set_nodelay(state->connection_in);
2315 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2316 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002317}
2318
2319/* Returns true if the current connection is interactive. */
2320
2321int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002322ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002323{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002324 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002325}
Damien Miller6162d121999-11-21 13:23:52 +11002326
Darren Tucker1f8311c2004-05-13 16:39:33 +10002327int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002328ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002329{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002330 struct session_state *state = ssh->state;
2331
2332 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002333 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002334 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002335 return -1;
2336 }
2337 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002338 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002339 return -1;
2340 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002341 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002342 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002343 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002344 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002345}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002346
Darren Tuckerf7288d72009-06-21 18:12:20 +10002347int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002348ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002349{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002350 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002351}
2352
2353void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002354ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002355{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002356 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002357}
2358
2359u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002360ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002361{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002362 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002363}
2364
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002365/*
2366 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002367 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002368 * byte SSH_MSG_IGNORE
2369 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002370 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002371 * All implementations MUST understand (and ignore) this message at any
2372 * time (after receiving the protocol version). No implementation is
2373 * required to send them. This message can be used as an additional
2374 * protection measure against advanced traffic analysis techniques.
2375 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002376void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002377ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002378{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002379 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002380 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002381
markus@openbsd.org091c3022015-01-19 19:52:16 +00002382 if ((r = sshpkt_start(ssh, compat20 ?
2383 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2384 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2385 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002386 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002387 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002388 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002389 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2390 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002391 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002392 }
2393}
Damien Millera5539d22003-04-09 20:50:06 +10002394
Damien Millera5539d22003-04-09 20:50:06 +10002395void
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002396ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002397{
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002398 debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes,
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002399 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002400 ssh->state->rekey_limit = bytes;
2401 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002402}
2403
2404time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002405ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002406{
2407 time_t seconds;
2408
markus@openbsd.org091c3022015-01-19 19:52:16 +00002409 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002410 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002411 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002412}
Damien Miller9786e6e2005-07-26 21:54:56 +10002413
2414void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002415ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002416{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002417 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002418}
2419
2420void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002421ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002422{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002423 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002424}
2425
2426void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002427ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002428{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002429 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002430}
2431
2432void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002433ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002434{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002435 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002436}
2437
Damien Millerc31a0cd2014-05-15 14:37:39 +10002438/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002439static int
2440ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002441{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002442 struct sshcomp *comp;
2443 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002444
2445 debug("%s: called", __func__);
2446 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002447 ssh->state->after_authentication = 1;
2448 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002449 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002450 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002451 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002452 comp = &ssh->state->newkeys[mode]->comp;
2453 if (comp && comp->enabled &&
2454 (r = ssh_packet_init_compression(ssh)) != 0)
2455 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002456 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002457 return 0;
2458}
2459
2460/* Packet state (de-)serialization for privsep */
2461
2462/* turn kex into a blob for packet state serialization */
2463static int
2464kex_to_blob(struct sshbuf *m, struct kex *kex)
2465{
2466 int r;
2467
2468 if ((r = sshbuf_put_string(m, kex->session_id,
2469 kex->session_id_len)) != 0 ||
2470 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2471 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2472 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2473 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2474 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2475 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2476 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2477 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2478 return r;
2479 return 0;
2480}
2481
2482/* turn key exchange results into a blob for packet state serialization */
2483static int
2484newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2485{
2486 struct sshbuf *b;
2487 struct sshcipher_ctx *cc;
2488 struct sshcomp *comp;
2489 struct sshenc *enc;
2490 struct sshmac *mac;
2491 struct newkeys *newkey;
2492 int r;
2493
2494 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2495 return SSH_ERR_INTERNAL_ERROR;
2496 enc = &newkey->enc;
2497 mac = &newkey->mac;
2498 comp = &newkey->comp;
2499 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2500 &ssh->state->receive_context;
2501 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2502 return r;
2503 if ((b = sshbuf_new()) == NULL)
2504 return SSH_ERR_ALLOC_FAIL;
2505 /* The cipher struct is constant and shared, you export pointer */
2506 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2507 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2508 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2509 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2510 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2511 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2512 goto out;
2513 if (cipher_authlen(enc->cipher) == 0) {
2514 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2515 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2516 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2517 goto out;
2518 }
2519 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2520 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2521 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2522 goto out;
2523 r = sshbuf_put_stringb(m, b);
2524 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002525 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002526 return r;
2527}
2528
2529/* serialize packet state into a blob */
2530int
2531ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2532{
2533 struct session_state *state = ssh->state;
2534 u_char *p;
2535 size_t slen, rlen;
2536 int r, ssh1cipher;
2537
2538 if (!compat20) {
2539 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2540 slen = cipher_get_keyiv_len(&state->send_context);
2541 rlen = cipher_get_keyiv_len(&state->receive_context);
2542 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2543 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2544 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2545 (r = sshbuf_put_u32(m, slen)) != 0 ||
2546 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2547 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2548 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2549 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2550 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2551 return r;
2552 } else {
2553 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2554 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2555 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002556 (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002557 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002558 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2559 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2560 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2561 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2562 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2563 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2564 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2565 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2566 return r;
2567 }
2568
2569 slen = cipher_get_keycontext(&state->send_context, NULL);
2570 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2571 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2572 (r = sshbuf_reserve(m, slen, &p)) != 0)
2573 return r;
2574 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2575 return SSH_ERR_INTERNAL_ERROR;
2576 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2577 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2578 return r;
2579 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2580 return SSH_ERR_INTERNAL_ERROR;
2581
2582 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2583 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2584 (r = sshbuf_put_stringb(m, state->output)) != 0)
2585 return r;
2586
markus@openbsd.org091c3022015-01-19 19:52:16 +00002587 return 0;
2588}
2589
2590/* restore key exchange results from blob for packet state de-serialization */
2591static int
2592newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2593{
2594 struct sshbuf *b = NULL;
2595 struct sshcomp *comp;
2596 struct sshenc *enc;
2597 struct sshmac *mac;
2598 struct newkeys *newkey = NULL;
2599 size_t keylen, ivlen, maclen;
2600 int r;
2601
2602 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2603 r = SSH_ERR_ALLOC_FAIL;
2604 goto out;
2605 }
2606 if ((r = sshbuf_froms(m, &b)) != 0)
2607 goto out;
2608#ifdef DEBUG_PK
2609 sshbuf_dump(b, stderr);
2610#endif
2611 enc = &newkey->enc;
2612 mac = &newkey->mac;
2613 comp = &newkey->comp;
2614
2615 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2616 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2617 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2618 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2619 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2620 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2621 goto out;
2622 if (cipher_authlen(enc->cipher) == 0) {
2623 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2624 goto out;
2625 if ((r = mac_setup(mac, mac->name)) != 0)
2626 goto out;
2627 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2628 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2629 goto out;
2630 if (maclen > mac->key_len) {
2631 r = SSH_ERR_INVALID_FORMAT;
2632 goto out;
2633 }
2634 mac->key_len = maclen;
2635 }
2636 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2637 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2638 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2639 goto out;
2640 if (enc->name == NULL ||
2641 cipher_by_name(enc->name) != enc->cipher) {
2642 r = SSH_ERR_INVALID_FORMAT;
2643 goto out;
2644 }
2645 if (sshbuf_len(b) != 0) {
2646 r = SSH_ERR_INVALID_FORMAT;
2647 goto out;
2648 }
2649 enc->key_len = keylen;
2650 enc->iv_len = ivlen;
2651 ssh->kex->newkeys[mode] = newkey;
2652 newkey = NULL;
2653 r = 0;
2654 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002655 free(newkey);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002656 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002657 return r;
2658}
2659
2660/* restore kex from blob for packet state de-serialization */
2661static int
2662kex_from_blob(struct sshbuf *m, struct kex **kexp)
2663{
2664 struct kex *kex;
2665 int r;
2666
2667 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2668 (kex->my = sshbuf_new()) == NULL ||
2669 (kex->peer = sshbuf_new()) == NULL) {
2670 r = SSH_ERR_ALLOC_FAIL;
2671 goto out;
2672 }
2673 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2674 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2675 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2676 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2677 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2678 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2679 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2680 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2681 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2682 goto out;
2683 kex->server = 1;
2684 kex->done = 1;
2685 r = 0;
2686 out:
2687 if (r != 0 || kexp == NULL) {
2688 if (kex != NULL) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002689 sshbuf_free(kex->my);
2690 sshbuf_free(kex->peer);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002691 free(kex);
2692 }
2693 if (kexp != NULL)
2694 *kexp = NULL;
2695 } else {
2696 *kexp = kex;
2697 }
2698 return r;
2699}
2700
2701/*
2702 * Restore packet state from content of blob 'm' (de-serialization).
2703 * Note that 'm' will be partially consumed on parsing or any other errors.
2704 */
2705int
2706ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2707{
2708 struct session_state *state = ssh->state;
2709 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2710 size_t ssh1keylen, rlen, slen, ilen, olen;
2711 int r;
2712 u_int ssh1cipher = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002713
2714 if (!compat20) {
2715 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2716 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2717 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2718 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2719 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2720 return r;
2721 if (ssh1cipher > INT_MAX)
2722 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2723 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2724 (int)ssh1cipher);
2725 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2726 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2727 return SSH_ERR_INVALID_FORMAT;
2728 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2729 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2730 return r;
2731 } else {
2732 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2733 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2734 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002735 (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002736 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002737 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2738 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2739 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2740 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2741 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2742 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2743 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2744 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2745 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002746 /*
2747 * We set the time here so that in post-auth privsep slave we
2748 * count from the completion of the authentication.
2749 */
2750 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002751 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2752 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2753 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2754 return r;
2755 }
2756 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2757 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2758 return r;
2759 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2760 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2761 return SSH_ERR_INVALID_FORMAT;
2762 cipher_set_keycontext(&state->send_context, keyout);
2763 cipher_set_keycontext(&state->receive_context, keyin);
2764
2765 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2766 (r = ssh_packet_set_postauth(ssh)) != 0)
2767 return r;
2768
2769 sshbuf_reset(state->input);
2770 sshbuf_reset(state->output);
2771 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2772 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2773 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2774 (r = sshbuf_put(state->output, output, olen)) != 0)
2775 return r;
2776
markus@openbsd.org091c3022015-01-19 19:52:16 +00002777 if (sshbuf_len(m))
2778 return SSH_ERR_INVALID_FORMAT;
2779 debug3("%s: done", __func__);
2780 return 0;
2781}
2782
2783/* NEW API */
2784
2785/* put data to the outgoing packet */
2786
2787int
2788sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2789{
2790 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2791}
2792
2793int
2794sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2795{
2796 return sshbuf_putb(ssh->state->outgoing_packet, b);
2797}
2798
2799int
2800sshpkt_put_u8(struct ssh *ssh, u_char val)
2801{
2802 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2803}
2804
2805int
2806sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2807{
2808 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2809}
2810
2811int
2812sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2813{
2814 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2815}
2816
2817int
2818sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2819{
2820 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2821}
2822
2823int
2824sshpkt_put_cstring(struct ssh *ssh, const void *v)
2825{
2826 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2827}
2828
2829int
2830sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2831{
2832 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2833}
2834
djm@openbsd.org734226b2015-04-27 01:52:30 +00002835#ifdef WITH_OPENSSL
2836#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002837int
2838sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2839{
2840 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2841}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002842#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002843
Damien Miller773dda22015-01-30 23:10:17 +11002844#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002845int
2846sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2847{
2848 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2849}
Damien Miller773dda22015-01-30 23:10:17 +11002850#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002851
2852int
2853sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2854{
2855 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2856}
Damien Miller773dda22015-01-30 23:10:17 +11002857#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002858
2859/* fetch data from the incoming packet */
2860
2861int
2862sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2863{
2864 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2865}
2866
2867int
2868sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2869{
2870 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2871}
2872
2873int
2874sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2875{
2876 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2877}
2878
2879int
2880sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2881{
2882 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2883}
2884
2885int
2886sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2887{
2888 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2889}
2890
2891int
2892sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2893{
2894 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2895}
2896
2897int
2898sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2899{
2900 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2901}
2902
djm@openbsd.org734226b2015-04-27 01:52:30 +00002903#ifdef WITH_OPENSSL
2904#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002905int
2906sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2907{
2908 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2909}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002910#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002911
Damien Miller773dda22015-01-30 23:10:17 +11002912#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002913int
2914sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2915{
2916 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2917}
Damien Miller773dda22015-01-30 23:10:17 +11002918#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002919
2920int
2921sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2922{
2923 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2924}
Damien Miller773dda22015-01-30 23:10:17 +11002925#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002926
2927int
2928sshpkt_get_end(struct ssh *ssh)
2929{
2930 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2931 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2932 return 0;
2933}
2934
2935const u_char *
2936sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2937{
2938 if (lenp != NULL)
2939 *lenp = sshbuf_len(ssh->state->incoming_packet);
2940 return sshbuf_ptr(ssh->state->incoming_packet);
2941}
2942
2943/* start a new packet */
2944
2945int
2946sshpkt_start(struct ssh *ssh, u_char type)
2947{
2948 u_char buf[9];
2949 int len;
2950
2951 DBG(debug("packet_start[%d]", type));
2952 len = compat20 ? 6 : 9;
2953 memset(buf, 0, len - 1);
2954 buf[len - 1] = type;
2955 sshbuf_reset(ssh->state->outgoing_packet);
2956 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2957}
2958
2959/* send it */
2960
2961int
2962sshpkt_send(struct ssh *ssh)
2963{
2964 if (compat20)
2965 return ssh_packet_send2(ssh);
2966 else
2967 return ssh_packet_send1(ssh);
2968}
2969
2970int
2971sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2972{
2973 char buf[1024];
2974 va_list args;
2975 int r;
2976
2977 va_start(args, fmt);
2978 vsnprintf(buf, sizeof(buf), fmt, args);
2979 va_end(args);
2980
2981 if (compat20) {
2982 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2983 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2984 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2985 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2986 (r = sshpkt_send(ssh)) != 0)
2987 return r;
2988 } else {
2989 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2990 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2991 (r = sshpkt_send(ssh)) != 0)
2992 return r;
2993 }
2994 return 0;
2995}
2996
2997/* roundup current message to pad bytes */
2998int
2999sshpkt_add_padding(struct ssh *ssh, u_char pad)
3000{
3001 ssh->state->extra_pad = pad;
3002 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10003003}