blob: 3810f1128907fa793fe18f536db0ddc8e3feb9e1 [file] [log] [blame]
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00001/* $OpenBSD: packet.c,v 1.232 2016/07/15 05:01:58 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000042#include <sys/param.h> /* MIN roundup */
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100044#include "openbsd-compat/sys-queue.h"
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100046#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049
Damien Miller8ec8c3e2006-07-10 20:35:38 +100050#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110051#include <netinet/ip.h>
Darren Tuckerdace2332006-09-22 19:22:17 +100052#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.h>
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;
djm@openbsd.org6d311932016-07-08 03:44:42 +00001693 u_char *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001694 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)
djm@openbsd.org6d311932016-07-08 03:44:42 +00001793 return 0; /* packet is incomplete */
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
djm@openbsd.org6d311932016-07-08 03:44:42 +00001798 /* EtM: check mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001799 if (mac && mac->enabled && mac->etm) {
djm@openbsd.org6d311932016-07-08 03:44:42 +00001800 if ((r = mac_check(mac, state->p_read.seqnr,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001801 sshbuf_ptr(state->input), aadlen + need,
djm@openbsd.org6d311932016-07-08 03:44:42 +00001802 sshbuf_ptr(state->input) + aadlen + need + authlen,
1803 maclen)) != 0) {
1804 if (r == SSH_ERR_MAC_INVALID)
1805 logit("Corrupted MAC on input.");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001806 goto out;
djm@openbsd.org6d311932016-07-08 03:44:42 +00001807 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001808 }
1809 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1810 &cp)) != 0)
1811 goto out;
1812 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1813 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1814 goto out;
1815 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1816 goto out;
Damien Miller4af51302000-04-16 11:18:38 +10001817 if (mac && mac->enabled) {
djm@openbsd.org6d311932016-07-08 03:44:42 +00001818 /* Not EtM: check MAC over cleartext */
1819 if (!mac->etm && (r = mac_check(mac, state->p_read.seqnr,
1820 sshbuf_ptr(state->incoming_packet),
1821 sshbuf_len(state->incoming_packet),
1822 sshbuf_ptr(state->input), maclen)) != 0) {
1823 if (r != SSH_ERR_MAC_INVALID)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001824 goto out;
Damien Miller13ae44c2009-01-28 16:38:41 +11001825 logit("Corrupted MAC on input.");
1826 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001827 return SSH_ERR_INTERNAL_ERROR;
1828 return ssh_packet_start_discard(ssh, enc, mac,
1829 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001830 }
djm@openbsd.org6d311932016-07-08 03:44:42 +00001831 /* Remove MAC from input buffer */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001832 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1833 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1834 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001835 }
Damien Miller278f9072001-12-21 15:00:19 +11001836 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001837 *seqnr_p = state->p_read.seqnr;
1838 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001839 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001840 if (++state->p_read.packets == 0)
1841 if (!(ssh->compat & SSH_BUG_NOREKEY))
1842 return SSH_ERR_NEED_REKEY;
1843 state->p_read.blocks += (state->packlen + 4) / block_size;
1844 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001845
1846 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001847 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001848 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001849 if (padlen < 4) {
1850 if ((r = sshpkt_disconnect(ssh,
1851 "Corrupted padlen %d on input.", padlen)) != 0 ||
1852 (r = ssh_packet_write_wait(ssh)) != 0)
1853 return r;
1854 return SSH_ERR_CONN_CORRUPT;
1855 }
Damien Miller33b13562000-04-04 14:38:59 +10001856
1857 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001858 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1859 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1860 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001861
markus@openbsd.org091c3022015-01-19 19:52:16 +00001862 DBG(debug("input: len before de-compress %zd",
1863 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001864 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001865 sshbuf_reset(state->compression_buffer);
1866 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1867 state->compression_buffer)) != 0)
1868 goto out;
1869 sshbuf_reset(state->incoming_packet);
1870 if ((r = sshbuf_putb(state->incoming_packet,
1871 state->compression_buffer)) != 0)
1872 goto out;
1873 DBG(debug("input: len after de-compress %zd",
1874 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001875 }
1876 /*
1877 * get packet type, implies consume.
1878 * return length of payload (without type field)
1879 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001880 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1881 goto out;
djm@openbsd.org28136472016-01-29 05:46:01 +00001882 if (ssh_packet_log_type(*typep))
1883 debug3("receive packet: type %u", *typep);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001884 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1885 if ((r = sshpkt_disconnect(ssh,
1886 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1887 (r = ssh_packet_write_wait(ssh)) != 0)
1888 return r;
1889 return SSH_ERR_PROTOCOL_ERROR;
1890 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001891 if (*typep == SSH2_MSG_NEWKEYS)
1892 r = ssh_set_newkeys(ssh, MODE_IN);
1893 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1894 r = ssh_packet_enable_delayed_compress(ssh);
1895 else
1896 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001897#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001898 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1899 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001900#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001901 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001902 state->packlen = 0;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001903
1904 /* do we need to rekey? */
1905 if (ssh_packet_need_rekeying(ssh, 0)) {
1906 debug3("%s: rekex triggered", __func__);
1907 if ((r = kex_start_rekex(ssh)) != 0)
1908 return r;
1909 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001910 out:
1911 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001912}
1913
1914int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001915ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001916{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001917 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001918 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001919 int r;
1920 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001921
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001922 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001923 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001924 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001925 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1926 if (r != 0)
1927 return r;
1928 if (*typep) {
1929 state->keep_alive_timeouts = 0;
1930 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001931 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001932 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001933 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001934 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001935 break;
Damien Miller33b13562000-04-04 14:38:59 +10001936 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001937 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1938 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1939 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001940 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001941 return r;
1942 }
Damien Miller33b13562000-04-04 14:38:59 +10001943 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001944 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001945 break;
1946 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001947 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1948 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1949 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001950 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001951 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001952 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1953 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001954 "Received disconnect from %s port %d:"
1955 "%u: %.400s", ssh_remote_ipaddr(ssh),
1956 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001957 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001958 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001959 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001960 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1961 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001962 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1963 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001964 break;
Damien Miller33b13562000-04-04 14:38:59 +10001965 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001966 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001967 }
Damien Miller33b13562000-04-04 14:38:59 +10001968 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001969 r = ssh_packet_read_poll1(ssh, typep);
1970 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001971 case SSH_MSG_NONE:
1972 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001973 case SSH_MSG_IGNORE:
1974 break;
1975 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001976 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1977 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001978 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001979 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001980 break;
1981 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001982 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1983 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001984 error("Received disconnect from %s port %d: "
1985 "%.400s", ssh_remote_ipaddr(ssh),
1986 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001987 free(msg);
1988 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001989 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001990 DBG(debug("received packet type %d", *typep));
1991 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001992 }
Damien Miller33b13562000-04-04 14:38:59 +10001993 }
1994 }
1995}
1996
Damien Miller5428f641999-11-25 11:54:57 +11001997/*
1998 * Buffers the given amount of input characters. This is intended to be used
1999 * together with packet_read_poll.
2000 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002001
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002002int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002003ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002004{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002005 struct session_state *state = ssh->state;
2006 int r;
2007
2008 if (state->packet_discard) {
2009 state->keep_alive_timeouts = 0; /* ?? */
2010 if (len >= state->packet_discard) {
2011 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002012 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002013 }
2014 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002015 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11002016 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002017 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002018 return r;
2019
2020 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002021}
2022
Damien Miller4af51302000-04-16 11:18:38 +10002023int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002024ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10002025{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002026 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10002027}
2028
Damien Miller5428f641999-11-25 11:54:57 +11002029/*
2030 * Sends a diagnostic message from the server to the client. This message
2031 * can be sent at any time (but not while constructing another message). The
2032 * message is printed immediately, but only if the client is being executed
2033 * in verbose mode. These messages are primarily intended to ease debugging
2034 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002035 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11002036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002037void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002038ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002039{
Damien Miller95def091999-11-25 00:26:21 +11002040 char buf[1024];
2041 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002042 int r;
Damien Miller95def091999-11-25 00:26:21 +11002043
markus@openbsd.org091c3022015-01-19 19:52:16 +00002044 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00002045 return;
2046
Damien Miller95def091999-11-25 00:26:21 +11002047 va_start(args, fmt);
2048 vsnprintf(buf, sizeof(buf), fmt, args);
2049 va_end(args);
2050
Damien Miller7c8af4f2000-05-01 08:24:07 +10002051 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002052 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
2053 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
2054 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2055 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2056 (r = sshpkt_send(ssh)) != 0)
2057 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002058 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002059 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
2060 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2061 (r = sshpkt_send(ssh)) != 0)
2062 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002063 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002064 if ((r = ssh_packet_write_wait(ssh)) != 0)
2065 fatal("%s: %s", __func__, ssh_err(r));
2066}
2067
2068/*
2069 * Pretty-print connection-terminating errors and exit.
2070 */
2071void
2072sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
2073{
2074 switch (r) {
2075 case SSH_ERR_CONN_CLOSED:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002076 logdie("Connection closed by %.200s port %d",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002077 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002078 case SSH_ERR_CONN_TIMEOUT:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002079 logdie("Connection %s %.200s port %d timed out",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002080 ssh->state->server_side ? "from" : "to",
2081 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002082 case SSH_ERR_DISCONNECTED:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002083 logdie("Disconnected from %.200s port %d",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002084 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002085 case SSH_ERR_SYSTEM_ERROR:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002086 if (errno == ECONNRESET)
2087 logdie("Connection reset by %.200s port %d",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002088 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002089 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002090 case SSH_ERR_NO_CIPHER_ALG_MATCH:
2091 case SSH_ERR_NO_MAC_ALG_MATCH:
2092 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
2093 case SSH_ERR_NO_KEX_ALG_MATCH:
2094 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
2095 if (ssh && ssh->kex && ssh->kex->failed_choice) {
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002096 logdie("Unable to negotiate with %.200s port %d: %s. "
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002097 "Their offer: %s", ssh_remote_ipaddr(ssh),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002098 ssh_remote_port(ssh), ssh_err(r),
2099 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002100 }
2101 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002102 default:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002103 logdie("%s%sConnection %s %.200s port %d: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002104 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002105 ssh->state->server_side ? "from" : "to",
2106 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002107 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002108}
2109
Damien Miller5428f641999-11-25 11:54:57 +11002110/*
2111 * Logs the error plus constructs and sends a disconnect packet, closes the
2112 * connection, and exits. This function never returns. The error message
2113 * should not contain a newline. The length of the formatted message must
2114 * not exceed 1024 bytes.
2115 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002116void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002117ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002118{
Damien Miller95def091999-11-25 00:26:21 +11002119 char buf[1024];
2120 va_list args;
2121 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002122 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002123
Damien Miller95def091999-11-25 00:26:21 +11002124 if (disconnecting) /* Guard against recursive invocations. */
2125 fatal("packet_disconnect called recursively.");
2126 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002127
Damien Miller5428f641999-11-25 11:54:57 +11002128 /*
2129 * Format the message. Note that the caller must make sure the
2130 * message is of limited size.
2131 */
Damien Miller95def091999-11-25 00:26:21 +11002132 va_start(args, fmt);
2133 vsnprintf(buf, sizeof(buf), fmt, args);
2134 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002135
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002136 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10002137 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002138
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002139 /*
2140 * Send the disconnect message to the other side, and wait
2141 * for it to get sent.
2142 */
2143 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2144 sshpkt_fatal(ssh, __func__, r);
2145
2146 if ((r = ssh_packet_write_wait(ssh)) != 0)
2147 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002148
Damien Miller95def091999-11-25 00:26:21 +11002149 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002150 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002151 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002152}
2153
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002154/*
2155 * Checks if there is any buffered output, and tries to write some of
2156 * the output.
2157 */
2158int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002159ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002160{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002161 struct session_state *state = ssh->state;
2162 int len = sshbuf_len(state->output);
markus@openbsd.orga3068632016-01-14 16:17:39 +00002163 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002164
Damien Miller95def091999-11-25 00:26:21 +11002165 if (len > 0) {
markus@openbsd.orga3068632016-01-14 16:17:39 +00002166 len = write(state->connection_out,
2167 sshbuf_ptr(state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10002168 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002169 if (errno == EINTR || errno == EAGAIN ||
2170 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002171 return 0;
2172 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002173 }
markus@openbsd.orga3068632016-01-14 16:17:39 +00002174 if (len == 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002175 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002176 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002177 return r;
Damien Miller95def091999-11-25 00:26:21 +11002178 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002179 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002180}
2181
Damien Miller5428f641999-11-25 11:54:57 +11002182/*
2183 * Calls packet_write_poll repeatedly until all pending output data has been
2184 * written.
2185 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002186int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002187ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002188{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002189 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002190 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002191 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002192 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002193
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002194 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002195 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002196 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002197 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002198 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2199 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002200 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002201 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002202 while (ssh_packet_have_data_to_write(ssh)) {
2203 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002204 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002205 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002206
markus@openbsd.org091c3022015-01-19 19:52:16 +00002207 if (state->packet_timeout_ms > 0) {
2208 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002209 timeoutp = &timeout;
2210 }
2211 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002212 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002213 ms_to_timeval(&timeout, ms_remain);
2214 gettimeofday(&start, NULL);
2215 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002216 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002217 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002218 break;
Damien Millerea437422009-10-02 11:49:03 +10002219 if (errno != EAGAIN && errno != EINTR &&
2220 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002221 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002222 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002223 continue;
2224 ms_subtract_diff(&start, &ms_remain);
2225 if (ms_remain <= 0) {
2226 ret = 0;
2227 break;
2228 }
2229 }
2230 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002231 free(setp);
2232 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002233 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002234 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2235 free(setp);
2236 return r;
2237 }
Damien Miller95def091999-11-25 00:26:21 +11002238 }
Darren Tuckera627d422013-06-02 07:31:17 +10002239 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002240 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002241}
2242
2243/* Returns true if there is buffered data to write to the connection. */
2244
2245int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002246ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002247{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002248 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002249}
2250
2251/* Returns true if there is not too much data to write to the connection. */
2252
2253int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002254ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002255{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002256 if (ssh->state->interactive_mode)
2257 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002258 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002259 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002260}
2261
markus@openbsd.org091c3022015-01-19 19:52:16 +00002262void
2263ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002264{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002265#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002266 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002267 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002268 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002269# ifdef IP_TOS
2270 case AF_INET:
2271 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002272 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002273 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2274 error("setsockopt IP_TOS %d: %.100s:",
2275 tos, strerror(errno));
2276 break;
2277# endif /* IP_TOS */
2278# ifdef IPV6_TCLASS
2279 case AF_INET6:
2280 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002281 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002282 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2283 error("setsockopt IPV6_TCLASS %d: %.100s:",
2284 tos, strerror(errno));
2285 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002286# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002287 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002288#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002289}
Ben Lindstroma7433982002-12-23 02:41:41 +00002290
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002291/* Informs that the current session is interactive. Sets IP flags for that. */
2292
2293void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002294ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002295{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002296 struct session_state *state = ssh->state;
2297
2298 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002299 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002300 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002301
Damien Miller95def091999-11-25 00:26:21 +11002302 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002303 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002304
Damien Miller34132e52000-01-14 15:45:46 +11002305 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002306 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002307 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002308 set_nodelay(state->connection_in);
2309 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2310 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002311}
2312
2313/* Returns true if the current connection is interactive. */
2314
2315int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002316ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002317{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002318 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002319}
Damien Miller6162d121999-11-21 13:23:52 +11002320
Darren Tucker1f8311c2004-05-13 16:39:33 +10002321int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002322ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002323{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002324 struct session_state *state = ssh->state;
2325
2326 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002327 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002328 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002329 return -1;
2330 }
2331 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002332 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002333 return -1;
2334 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002335 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002336 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002337 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002338 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002339}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002340
Darren Tuckerf7288d72009-06-21 18:12:20 +10002341int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002342ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002343{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002344 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002345}
2346
2347void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002348ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002349{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002350 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002351}
2352
2353u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002354ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002355{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002356 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002357}
2358
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002359/*
2360 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002361 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002362 * byte SSH_MSG_IGNORE
2363 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002364 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002365 * All implementations MUST understand (and ignore) this message at any
2366 * time (after receiving the protocol version). No implementation is
2367 * required to send them. This message can be used as an additional
2368 * protection measure against advanced traffic analysis techniques.
2369 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002370void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002371ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002372{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002373 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002374 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002375
markus@openbsd.org091c3022015-01-19 19:52:16 +00002376 if ((r = sshpkt_start(ssh, compat20 ?
2377 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2378 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2379 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002380 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002381 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002382 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002383 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2384 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002385 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002386 }
2387}
Damien Millera5539d22003-04-09 20:50:06 +10002388
Damien Millera5539d22003-04-09 20:50:06 +10002389void
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002390ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002391{
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002392 debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes,
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002393 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002394 ssh->state->rekey_limit = bytes;
2395 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002396}
2397
2398time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002399ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002400{
2401 time_t seconds;
2402
markus@openbsd.org091c3022015-01-19 19:52:16 +00002403 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002404 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002405 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002406}
Damien Miller9786e6e2005-07-26 21:54:56 +10002407
2408void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002409ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002410{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002411 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002412}
2413
2414void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002415ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002416{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002417 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002418}
2419
2420void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002421ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002422{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002423 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002424}
2425
2426void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002427ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002428{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002429 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002430}
2431
Damien Millerc31a0cd2014-05-15 14:37:39 +10002432/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002433static int
2434ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002435{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002436 struct sshcomp *comp;
2437 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002438
2439 debug("%s: called", __func__);
2440 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002441 ssh->state->after_authentication = 1;
2442 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002443 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002444 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002445 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002446 comp = &ssh->state->newkeys[mode]->comp;
2447 if (comp && comp->enabled &&
2448 (r = ssh_packet_init_compression(ssh)) != 0)
2449 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002450 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002451 return 0;
2452}
2453
2454/* Packet state (de-)serialization for privsep */
2455
2456/* turn kex into a blob for packet state serialization */
2457static int
2458kex_to_blob(struct sshbuf *m, struct kex *kex)
2459{
2460 int r;
2461
2462 if ((r = sshbuf_put_string(m, kex->session_id,
2463 kex->session_id_len)) != 0 ||
2464 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2465 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2466 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2467 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2468 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2469 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2470 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2471 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2472 return r;
2473 return 0;
2474}
2475
2476/* turn key exchange results into a blob for packet state serialization */
2477static int
2478newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2479{
2480 struct sshbuf *b;
2481 struct sshcipher_ctx *cc;
2482 struct sshcomp *comp;
2483 struct sshenc *enc;
2484 struct sshmac *mac;
2485 struct newkeys *newkey;
2486 int r;
2487
2488 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2489 return SSH_ERR_INTERNAL_ERROR;
2490 enc = &newkey->enc;
2491 mac = &newkey->mac;
2492 comp = &newkey->comp;
2493 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2494 &ssh->state->receive_context;
2495 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2496 return r;
2497 if ((b = sshbuf_new()) == NULL)
2498 return SSH_ERR_ALLOC_FAIL;
2499 /* The cipher struct is constant and shared, you export pointer */
2500 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2501 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2502 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2503 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2504 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2505 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2506 goto out;
2507 if (cipher_authlen(enc->cipher) == 0) {
2508 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2509 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2510 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2511 goto out;
2512 }
2513 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2514 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2515 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2516 goto out;
2517 r = sshbuf_put_stringb(m, b);
2518 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002519 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002520 return r;
2521}
2522
2523/* serialize packet state into a blob */
2524int
2525ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2526{
2527 struct session_state *state = ssh->state;
2528 u_char *p;
2529 size_t slen, rlen;
2530 int r, ssh1cipher;
2531
2532 if (!compat20) {
2533 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2534 slen = cipher_get_keyiv_len(&state->send_context);
2535 rlen = cipher_get_keyiv_len(&state->receive_context);
2536 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2537 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2538 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2539 (r = sshbuf_put_u32(m, slen)) != 0 ||
2540 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2541 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2542 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2543 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2544 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2545 return r;
2546 } else {
2547 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2548 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2549 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002550 (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002551 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002552 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2553 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2554 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2555 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2556 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2557 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2558 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2559 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2560 return r;
2561 }
2562
2563 slen = cipher_get_keycontext(&state->send_context, NULL);
2564 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2565 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2566 (r = sshbuf_reserve(m, slen, &p)) != 0)
2567 return r;
2568 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2569 return SSH_ERR_INTERNAL_ERROR;
2570 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2571 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2572 return r;
2573 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2574 return SSH_ERR_INTERNAL_ERROR;
2575
2576 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2577 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2578 (r = sshbuf_put_stringb(m, state->output)) != 0)
2579 return r;
2580
markus@openbsd.org091c3022015-01-19 19:52:16 +00002581 return 0;
2582}
2583
2584/* restore key exchange results from blob for packet state de-serialization */
2585static int
2586newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2587{
2588 struct sshbuf *b = NULL;
2589 struct sshcomp *comp;
2590 struct sshenc *enc;
2591 struct sshmac *mac;
2592 struct newkeys *newkey = NULL;
2593 size_t keylen, ivlen, maclen;
2594 int r;
2595
2596 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2597 r = SSH_ERR_ALLOC_FAIL;
2598 goto out;
2599 }
2600 if ((r = sshbuf_froms(m, &b)) != 0)
2601 goto out;
2602#ifdef DEBUG_PK
2603 sshbuf_dump(b, stderr);
2604#endif
2605 enc = &newkey->enc;
2606 mac = &newkey->mac;
2607 comp = &newkey->comp;
2608
2609 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2610 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2611 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2612 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2613 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2614 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2615 goto out;
2616 if (cipher_authlen(enc->cipher) == 0) {
2617 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2618 goto out;
2619 if ((r = mac_setup(mac, mac->name)) != 0)
2620 goto out;
2621 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2622 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2623 goto out;
2624 if (maclen > mac->key_len) {
2625 r = SSH_ERR_INVALID_FORMAT;
2626 goto out;
2627 }
2628 mac->key_len = maclen;
2629 }
2630 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2631 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2632 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2633 goto out;
2634 if (enc->name == NULL ||
2635 cipher_by_name(enc->name) != enc->cipher) {
2636 r = SSH_ERR_INVALID_FORMAT;
2637 goto out;
2638 }
2639 if (sshbuf_len(b) != 0) {
2640 r = SSH_ERR_INVALID_FORMAT;
2641 goto out;
2642 }
2643 enc->key_len = keylen;
2644 enc->iv_len = ivlen;
2645 ssh->kex->newkeys[mode] = newkey;
2646 newkey = NULL;
2647 r = 0;
2648 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002649 free(newkey);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002650 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002651 return r;
2652}
2653
2654/* restore kex from blob for packet state de-serialization */
2655static int
2656kex_from_blob(struct sshbuf *m, struct kex **kexp)
2657{
2658 struct kex *kex;
2659 int r;
2660
2661 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2662 (kex->my = sshbuf_new()) == NULL ||
2663 (kex->peer = sshbuf_new()) == NULL) {
2664 r = SSH_ERR_ALLOC_FAIL;
2665 goto out;
2666 }
2667 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2668 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2669 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2670 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2671 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2672 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2673 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2674 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2675 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2676 goto out;
2677 kex->server = 1;
2678 kex->done = 1;
2679 r = 0;
2680 out:
2681 if (r != 0 || kexp == NULL) {
2682 if (kex != NULL) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002683 sshbuf_free(kex->my);
2684 sshbuf_free(kex->peer);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002685 free(kex);
2686 }
2687 if (kexp != NULL)
2688 *kexp = NULL;
2689 } else {
2690 *kexp = kex;
2691 }
2692 return r;
2693}
2694
2695/*
2696 * Restore packet state from content of blob 'm' (de-serialization).
2697 * Note that 'm' will be partially consumed on parsing or any other errors.
2698 */
2699int
2700ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2701{
2702 struct session_state *state = ssh->state;
2703 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2704 size_t ssh1keylen, rlen, slen, ilen, olen;
2705 int r;
2706 u_int ssh1cipher = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002707
2708 if (!compat20) {
2709 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2710 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2711 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2712 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2713 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2714 return r;
2715 if (ssh1cipher > INT_MAX)
2716 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2717 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2718 (int)ssh1cipher);
2719 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2720 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2721 return SSH_ERR_INVALID_FORMAT;
2722 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2723 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2724 return r;
2725 } else {
2726 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2727 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2728 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002729 (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002730 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002731 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2732 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2733 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2734 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2735 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2736 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2737 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2738 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2739 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002740 /*
2741 * We set the time here so that in post-auth privsep slave we
2742 * count from the completion of the authentication.
2743 */
2744 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002745 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2746 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2747 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2748 return r;
2749 }
2750 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2751 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2752 return r;
2753 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2754 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2755 return SSH_ERR_INVALID_FORMAT;
2756 cipher_set_keycontext(&state->send_context, keyout);
2757 cipher_set_keycontext(&state->receive_context, keyin);
2758
2759 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2760 (r = ssh_packet_set_postauth(ssh)) != 0)
2761 return r;
2762
2763 sshbuf_reset(state->input);
2764 sshbuf_reset(state->output);
2765 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2766 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2767 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2768 (r = sshbuf_put(state->output, output, olen)) != 0)
2769 return r;
2770
markus@openbsd.org091c3022015-01-19 19:52:16 +00002771 if (sshbuf_len(m))
2772 return SSH_ERR_INVALID_FORMAT;
2773 debug3("%s: done", __func__);
2774 return 0;
2775}
2776
2777/* NEW API */
2778
2779/* put data to the outgoing packet */
2780
2781int
2782sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2783{
2784 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2785}
2786
2787int
2788sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2789{
2790 return sshbuf_putb(ssh->state->outgoing_packet, b);
2791}
2792
2793int
2794sshpkt_put_u8(struct ssh *ssh, u_char val)
2795{
2796 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2797}
2798
2799int
2800sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2801{
2802 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2803}
2804
2805int
2806sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2807{
2808 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2809}
2810
2811int
2812sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2813{
2814 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2815}
2816
2817int
2818sshpkt_put_cstring(struct ssh *ssh, const void *v)
2819{
2820 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2821}
2822
2823int
2824sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2825{
2826 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2827}
2828
djm@openbsd.org734226b2015-04-27 01:52:30 +00002829#ifdef WITH_OPENSSL
2830#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002831int
2832sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2833{
2834 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2835}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002836#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002837
Damien Miller773dda22015-01-30 23:10:17 +11002838#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002839int
2840sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2841{
2842 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2843}
Damien Miller773dda22015-01-30 23:10:17 +11002844#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002845
2846int
2847sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2848{
2849 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2850}
Damien Miller773dda22015-01-30 23:10:17 +11002851#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002852
2853/* fetch data from the incoming packet */
2854
2855int
2856sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2857{
2858 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2859}
2860
2861int
2862sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2863{
2864 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2865}
2866
2867int
2868sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2869{
2870 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2871}
2872
2873int
2874sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2875{
2876 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2877}
2878
2879int
2880sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2881{
2882 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2883}
2884
2885int
2886sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2887{
2888 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2889}
2890
2891int
2892sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2893{
2894 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2895}
2896
djm@openbsd.org734226b2015-04-27 01:52:30 +00002897#ifdef WITH_OPENSSL
2898#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002899int
2900sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2901{
2902 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2903}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002904#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002905
Damien Miller773dda22015-01-30 23:10:17 +11002906#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002907int
2908sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2909{
2910 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2911}
Damien Miller773dda22015-01-30 23:10:17 +11002912#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002913
2914int
2915sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2916{
2917 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2918}
Damien Miller773dda22015-01-30 23:10:17 +11002919#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002920
2921int
2922sshpkt_get_end(struct ssh *ssh)
2923{
2924 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2925 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2926 return 0;
2927}
2928
2929const u_char *
2930sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2931{
2932 if (lenp != NULL)
2933 *lenp = sshbuf_len(ssh->state->incoming_packet);
2934 return sshbuf_ptr(ssh->state->incoming_packet);
2935}
2936
2937/* start a new packet */
2938
2939int
2940sshpkt_start(struct ssh *ssh, u_char type)
2941{
2942 u_char buf[9];
2943 int len;
2944
2945 DBG(debug("packet_start[%d]", type));
2946 len = compat20 ? 6 : 9;
2947 memset(buf, 0, len - 1);
2948 buf[len - 1] = type;
2949 sshbuf_reset(ssh->state->outgoing_packet);
2950 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2951}
2952
2953/* send it */
2954
2955int
2956sshpkt_send(struct ssh *ssh)
2957{
2958 if (compat20)
2959 return ssh_packet_send2(ssh);
2960 else
2961 return ssh_packet_send1(ssh);
2962}
2963
2964int
2965sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2966{
2967 char buf[1024];
2968 va_list args;
2969 int r;
2970
2971 va_start(args, fmt);
2972 vsnprintf(buf, sizeof(buf), fmt, args);
2973 va_end(args);
2974
2975 if (compat20) {
2976 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2977 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2978 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2979 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2980 (r = sshpkt_send(ssh)) != 0)
2981 return r;
2982 } else {
2983 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2984 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2985 (r = sshpkt_send(ssh)) != 0)
2986 return r;
2987 }
2988 return 0;
2989}
2990
2991/* roundup current message to pad bytes */
2992int
2993sshpkt_add_padding(struct ssh *ssh, u_char pad)
2994{
2995 ssh->state->extra_pad = pad;
2996 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002997}