blob: f9ce084122fd2e493af0059fe25536aebd548d23 [file] [log] [blame]
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001/* $OpenBSD: packet.c,v 1.205 2015/01/30 01:13:33 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000042#include <sys/param.h> /* MIN roundup */
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100044#include "openbsd-compat/sys-queue.h"
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100046#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049
Damien Miller8ec8c3e2006-07-10 20:35:38 +100050#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110051#include <netinet/ip.h>
Darren Tuckerdace2332006-09-22 19:22:17 +100052#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100055#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100056#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100057#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100058#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100059#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000060#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100061#include <signal.h>
Darren Tuckerc53c2af2013-05-16 20:28:16 +100062#include <time.h>
Darren Tucker5d196262006-07-12 22:15:16 +100063
markus@openbsd.org091c3022015-01-19 19:52:16 +000064#include <zlib.h>
65
66#include "buffer.h" /* typedefs XXX */
67#include "key.h" /* typedefs XXX */
68
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "deattack.h"
Damien Miller33b13562000-04-04 14:38:59 +100072#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100074#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110075#include "cipher.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000076#include "sshkey.h"
Damien Miller33b13562000-04-04 14:38:59 +100077#include "kex.h"
markus@openbsd.org128343b2015-01-13 19:31:40 +000078#include "digest.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000079#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "log.h"
81#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110082#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100083#include "channels.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000084#include "ssh.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000085#include "packet.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100086#include "roaming.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000087#include "ssherr.h"
88#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100089
90#ifdef PACKET_DEBUG
91#define DBG(x) x
92#else
93#define DBG(x)
94#endif
95
Damien Miller13ae44c2009-01-28 16:38:41 +110096#define PACKET_MAX_SIZE (256 * 1024)
97
Darren Tuckerf7288d72009-06-21 18:12:20 +100098struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100099 u_int32_t seqnr;
100 u_int32_t packets;
101 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000102 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000103};
Damien Miller13ae44c2009-01-28 16:38:41 +1100104
Damien Millera5539d22003-04-09 20:50:06 +1000105struct packet {
106 TAILQ_ENTRY(packet) next;
107 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000108 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000109};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000110
111struct session_state {
112 /*
113 * This variable contains the file descriptors used for
114 * communicating with the other side. connection_in is used for
115 * reading; connection_out for writing. These can be the same
116 * descriptor, in which case it is assumed to be a socket.
117 */
118 int connection_in;
119 int connection_out;
120
121 /* Protocol flags for the remote side. */
122 u_int remote_protocol_flags;
123
124 /* Encryption context for receiving data. Only used for decryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000125 struct sshcipher_ctx receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000126
127 /* Encryption context for sending data. Only used for encryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000128 struct sshcipher_ctx send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000129
130 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000131 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000132
133 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000134 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000135
136 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000137 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000138
139 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000140 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000141
142 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000143 struct sshbuf *compression_buffer;
144
145 /* Incoming/outgoing compression dictionaries */
146 z_stream compression_in_stream;
147 z_stream compression_out_stream;
148 int compression_in_started;
149 int compression_out_started;
150 int compression_in_failures;
151 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000152
153 /*
154 * Flag indicating whether packet compression/decompression is
155 * enabled.
156 */
157 int packet_compression;
158
159 /* default maximum packet size */
160 u_int max_packet_size;
161
162 /* Flag indicating whether this module has been initialized. */
163 int initialized;
164
165 /* Set to true if the connection is interactive. */
166 int interactive_mode;
167
168 /* Set to true if we are the server side. */
169 int server_side;
170
171 /* Set to true if we are authenticated. */
172 int after_authentication;
173
174 int keep_alive_timeouts;
175
176 /* The maximum time that we will wait to send or receive a packet */
177 int packet_timeout_ms;
178
179 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000180 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000181 struct packet_state p_read, p_send;
182
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000183 /* Volume-based rekeying */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000184 u_int64_t max_blocks_in, max_blocks_out;
185 u_int32_t rekey_limit;
186
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000187 /* Time-based rekeying */
188 time_t rekey_interval; /* how often in seconds */
189 time_t rekey_time; /* time of last rekeying */
190
Darren Tuckerf7288d72009-06-21 18:12:20 +1000191 /* Session key for protocol v1 */
192 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
193 u_int ssh1_keylen;
194
195 /* roundup current message to extra_pad bytes */
196 u_char extra_pad;
197
198 /* XXX discard incoming data after MAC error */
199 u_int packet_discard;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000200 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000201
202 /* Used in packet_read_poll2() */
203 u_int packlen;
204
Darren Tucker7b935c72009-06-21 18:59:36 +1000205 /* Used in packet_send2 */
206 int rekeying;
207
208 /* Used in packet_set_interactive */
209 int set_interactive_called;
210
211 /* Used in packet_set_maxsize */
212 int set_maxsize_called;
213
markus@openbsd.org091c3022015-01-19 19:52:16 +0000214 /* One-off warning about weak ciphers */
215 int cipher_warning_done;
216
217 /* SSH1 CRC compensation attack detector */
218 struct deattack_ctx deattack;
219
Darren Tuckerf7288d72009-06-21 18:12:20 +1000220 TAILQ_HEAD(, packet) outgoing;
221};
222
markus@openbsd.org091c3022015-01-19 19:52:16 +0000223struct ssh *
224ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000225{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000226 struct ssh *ssh = NULL;
227 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000228
markus@openbsd.org091c3022015-01-19 19:52:16 +0000229 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
230 (state = calloc(1, sizeof(*state))) == NULL ||
231 (state->input = sshbuf_new()) == NULL ||
232 (state->output = sshbuf_new()) == NULL ||
233 (state->outgoing_packet = sshbuf_new()) == NULL ||
234 (state->incoming_packet = sshbuf_new()) == NULL)
235 goto fail;
236 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000237 TAILQ_INIT(&ssh->private_keys);
238 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000239 state->connection_in = -1;
240 state->connection_out = -1;
241 state->max_packet_size = 32768;
242 state->packet_timeout_ms = -1;
243 state->p_send.packets = state->p_read.packets = 0;
244 state->initialized = 1;
245 /*
246 * ssh_packet_send2() needs to queue packets until
247 * we've done the initial key exchange.
248 */
249 state->rekeying = 1;
250 ssh->state = state;
251 return ssh;
252 fail:
253 if (state) {
254 sshbuf_free(state->input);
255 sshbuf_free(state->output);
256 sshbuf_free(state->incoming_packet);
257 sshbuf_free(state->outgoing_packet);
258 free(state);
259 }
260 free(ssh);
261 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000262}
Damien Millera5539d22003-04-09 20:50:06 +1000263
Damien Miller5428f641999-11-25 11:54:57 +1100264/*
265 * Sets the descriptors used for communication. Disables encryption until
266 * packet_set_encryption_key is called.
267 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000268struct ssh *
269ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000271 struct session_state *state;
272 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000273 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000274
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000275 if (none == NULL) {
276 error("%s: cannot load cipher 'none'", __func__);
277 return NULL;
278 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000279 if (ssh == NULL)
280 ssh = ssh_alloc_session_state();
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000281 if (ssh == NULL) {
282 error("%s: cound not allocate state", __func__);
283 return NULL;
284 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000285 state = ssh->state;
286 state->connection_in = fd_in;
287 state->connection_out = fd_out;
288 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000289 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000290 (r = cipher_init(&state->receive_context, none,
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000291 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
292 error("%s: cipher_init failed: %s", __func__, ssh_err(r));
293 return NULL;
294 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000295 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
296 deattack_init(&state->deattack);
297 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298}
299
Darren Tucker3fc464e2008-06-13 06:42:45 +1000300void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000301ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000302{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000303 struct session_state *state = ssh->state;
304
Damien Miller8ed4de82011-12-19 10:52:50 +1100305 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000306 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000307 return;
308 }
309 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000310 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000311 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000312 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000313}
314
markus@openbsd.org091c3022015-01-19 19:52:16 +0000315int
316ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100317{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000318 struct session_state *state = ssh->state;
319 int r;
320
321 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100322 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000323
Damien Miller13ae44c2009-01-28 16:38:41 +1100324 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000325 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000326 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000327 if ((r = sshbuf_put(state->incoming_packet, buf,
328 sizeof(buf))) != 0)
329 return r;
330 (void) mac_compute(state->packet_discard_mac,
331 state->p_read.seqnr,
332 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
333 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100334 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000335 logit("Finished discarding for %.200s", ssh_remote_ipaddr(ssh));
336 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100337}
338
markus@openbsd.org091c3022015-01-19 19:52:16 +0000339static int
340ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
341 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100342{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000343 struct session_state *state = ssh->state;
344 int r;
345
346 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
347 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
348 return r;
349 return SSH_ERR_MAC_INVALID;
350 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100351 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000352 state->packet_discard_mac = mac;
353 if (sshbuf_len(state->input) >= discard &&
354 (r = ssh_packet_stop_discard(ssh)) != 0)
355 return r;
356 state->packet_discard = discard - sshbuf_len(state->input);
357 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100358}
359
Damien Miller34132e52000-01-14 15:45:46 +1100360/* Returns 1 if remote host is connected via socket, 0 if not. */
361
362int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000363ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100364{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000365 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100366 struct sockaddr_storage from, to;
367 socklen_t fromlen, tolen;
368
369 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000370 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100371 return 1;
372 fromlen = sizeof(from);
373 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000374 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000375 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100376 return 0;
377 tolen = sizeof(to);
378 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000379 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000380 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100381 return 0;
382 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
383 return 0;
384 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
385 return 0;
386 return 1;
387}
388
Ben Lindstromf6027d32002-03-22 01:42:04 +0000389void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000390ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000391{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000392 if (ibytes)
393 *ibytes = ssh->state->p_read.bytes;
394 if (obytes)
395 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000396}
397
398int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000399ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100400{
401 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100402 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100403
404 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000405 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000406 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100407 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000408#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100409 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000410 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000411 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000412#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000413 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100414}
415
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416/* Sets the connection into non-blocking mode. */
417
418void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000419ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420{
Damien Miller95def091999-11-25 00:26:21 +1100421 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000422 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
markus@openbsd.org091c3022015-01-19 19:52:16 +0000424 if (ssh->state->connection_out != ssh->state->connection_in)
425 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426}
427
428/* Returns the socket used for reading. */
429
430int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000431ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000433 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434}
435
436/* Returns the descriptor used for writing. */
437
438int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000439ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000441 return ssh->state->connection_out;
442}
443
444/*
445 * Returns the IP-address of the remote host as a string. The returned
446 * string must not be freed.
447 */
448
449const char *
450ssh_remote_ipaddr(struct ssh *ssh)
451{
452 /* Check whether we have cached the ipaddr. */
453 if (ssh->remote_ipaddr == NULL)
454 ssh->remote_ipaddr = ssh_packet_connection_is_on_socket(ssh) ?
455 get_peer_ipaddr(ssh->state->connection_in) :
456 strdup("UNKNOWN");
457 if (ssh->remote_ipaddr == NULL)
458 return "UNKNOWN";
459 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460}
461
462/* Closes the connection and clears and frees internal data structures. */
463
464void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000465ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000467 struct session_state *state = ssh->state;
468 int r;
469 u_int mode;
470
471 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100472 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000473 state->initialized = 0;
474 if (state->connection_in == state->connection_out) {
475 shutdown(state->connection_out, SHUT_RDWR);
476 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100477 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000478 close(state->connection_in);
479 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100480 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000481 sshbuf_free(state->input);
482 sshbuf_free(state->output);
483 sshbuf_free(state->outgoing_packet);
484 sshbuf_free(state->incoming_packet);
485 for (mode = 0; mode < MODE_MAX; mode++)
486 kex_free_newkeys(state->newkeys[mode]);
487 if (state->compression_buffer) {
488 sshbuf_free(state->compression_buffer);
489 if (state->compression_out_started) {
490 z_streamp stream = &state->compression_out_stream;
491 debug("compress outgoing: "
492 "raw data %llu, compressed %llu, factor %.2f",
493 (unsigned long long)stream->total_in,
494 (unsigned long long)stream->total_out,
495 stream->total_in == 0 ? 0.0 :
496 (double) stream->total_out / stream->total_in);
497 if (state->compression_out_failures == 0)
498 deflateEnd(stream);
499 }
500 if (state->compression_in_started) {
501 z_streamp stream = &state->compression_out_stream;
502 debug("compress incoming: "
503 "raw data %llu, compressed %llu, factor %.2f",
504 (unsigned long long)stream->total_out,
505 (unsigned long long)stream->total_in,
506 stream->total_out == 0 ? 0.0 :
507 (double) stream->total_in / stream->total_out);
508 if (state->compression_in_failures == 0)
509 inflateEnd(stream);
510 }
Damien Miller95def091999-11-25 00:26:21 +1100511 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000512 if ((r = cipher_cleanup(&state->send_context)) != 0)
513 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
514 if ((r = cipher_cleanup(&state->receive_context)) != 0)
515 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
516 if (ssh->remote_ipaddr) {
517 free(ssh->remote_ipaddr);
518 ssh->remote_ipaddr = NULL;
519 }
520 free(ssh->state);
521 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522}
523
524/* Sets remote side protocol flags. */
525
526void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000527ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000529 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530}
531
532/* Returns the remote protocol flags set earlier by the above function. */
533
Ben Lindstrom46c16222000-12-22 01:43:59 +0000534u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000535ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000537 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538}
539
Damien Miller5428f641999-11-25 11:54:57 +1100540/*
541 * Starts packet compression from the next packet on in both directions.
542 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
543 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544
markus@openbsd.org091c3022015-01-19 19:52:16 +0000545static int
546ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000547{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000548 if (!ssh->state->compression_buffer &&
549 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
550 return SSH_ERR_ALLOC_FAIL;
551 return 0;
552}
553
554static int
555start_compression_out(struct ssh *ssh, int level)
556{
557 if (level < 1 || level > 9)
558 return SSH_ERR_INVALID_ARGUMENT;
559 debug("Enabling compression at level %d.", level);
560 if (ssh->state->compression_out_started == 1)
561 deflateEnd(&ssh->state->compression_out_stream);
562 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
563 case Z_OK:
564 ssh->state->compression_out_started = 1;
565 break;
566 case Z_MEM_ERROR:
567 return SSH_ERR_ALLOC_FAIL;
568 default:
569 return SSH_ERR_INTERNAL_ERROR;
570 }
571 return 0;
572}
573
574static int
575start_compression_in(struct ssh *ssh)
576{
577 if (ssh->state->compression_in_started == 1)
578 inflateEnd(&ssh->state->compression_in_stream);
579 switch (inflateInit(&ssh->state->compression_in_stream)) {
580 case Z_OK:
581 ssh->state->compression_in_started = 1;
582 break;
583 case Z_MEM_ERROR:
584 return SSH_ERR_ALLOC_FAIL;
585 default:
586 return SSH_ERR_INTERNAL_ERROR;
587 }
588 return 0;
589}
590
591int
592ssh_packet_start_compression(struct ssh *ssh, int level)
593{
594 int r;
595
596 if (ssh->state->packet_compression && !compat20)
597 return SSH_ERR_INTERNAL_ERROR;
598 ssh->state->packet_compression = 1;
599 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
600 (r = start_compression_in(ssh)) != 0 ||
601 (r = start_compression_out(ssh, level)) != 0)
602 return r;
603 return 0;
604}
605
606/* XXX remove need for separate compression buffer */
607static int
608compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
609{
610 u_char buf[4096];
611 int r, status;
612
613 if (ssh->state->compression_out_started != 1)
614 return SSH_ERR_INTERNAL_ERROR;
615
616 /* This case is not handled below. */
617 if (sshbuf_len(in) == 0)
618 return 0;
619
620 /* Input is the contents of the input buffer. */
621 if ((ssh->state->compression_out_stream.next_in =
622 sshbuf_mutable_ptr(in)) == NULL)
623 return SSH_ERR_INTERNAL_ERROR;
624 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
625
626 /* Loop compressing until deflate() returns with avail_out != 0. */
627 do {
628 /* Set up fixed-size output buffer. */
629 ssh->state->compression_out_stream.next_out = buf;
630 ssh->state->compression_out_stream.avail_out = sizeof(buf);
631
632 /* Compress as much data into the buffer as possible. */
633 status = deflate(&ssh->state->compression_out_stream,
634 Z_PARTIAL_FLUSH);
635 switch (status) {
636 case Z_MEM_ERROR:
637 return SSH_ERR_ALLOC_FAIL;
638 case Z_OK:
639 /* Append compressed data to output_buffer. */
640 if ((r = sshbuf_put(out, buf, sizeof(buf) -
641 ssh->state->compression_out_stream.avail_out)) != 0)
642 return r;
643 break;
644 case Z_STREAM_ERROR:
645 default:
646 ssh->state->compression_out_failures++;
647 return SSH_ERR_INVALID_FORMAT;
648 }
649 } while (ssh->state->compression_out_stream.avail_out == 0);
650 return 0;
651}
652
653static int
654uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
655{
656 u_char buf[4096];
657 int r, status;
658
659 if (ssh->state->compression_in_started != 1)
660 return SSH_ERR_INTERNAL_ERROR;
661
662 if ((ssh->state->compression_in_stream.next_in =
663 sshbuf_mutable_ptr(in)) == NULL)
664 return SSH_ERR_INTERNAL_ERROR;
665 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
666
667 for (;;) {
668 /* Set up fixed-size output buffer. */
669 ssh->state->compression_in_stream.next_out = buf;
670 ssh->state->compression_in_stream.avail_out = sizeof(buf);
671
672 status = inflate(&ssh->state->compression_in_stream,
673 Z_PARTIAL_FLUSH);
674 switch (status) {
675 case Z_OK:
676 if ((r = sshbuf_put(out, buf, sizeof(buf) -
677 ssh->state->compression_in_stream.avail_out)) != 0)
678 return r;
679 break;
680 case Z_BUF_ERROR:
681 /*
682 * Comments in zlib.h say that we should keep calling
683 * inflate() until we get an error. This appears to
684 * be the error that we get.
685 */
686 return 0;
687 case Z_DATA_ERROR:
688 return SSH_ERR_INVALID_FORMAT;
689 case Z_MEM_ERROR:
690 return SSH_ERR_ALLOC_FAIL;
691 case Z_STREAM_ERROR:
692 default:
693 ssh->state->compression_in_failures++;
694 return SSH_ERR_INTERNAL_ERROR;
695 }
696 }
697 /* NOTREACHED */
698}
699
700/* Serialise compression state into a blob for privsep */
701static int
702ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
703{
704 struct session_state *state = ssh->state;
705 struct sshbuf *b;
706 int r;
707
708 if ((b = sshbuf_new()) == NULL)
709 return SSH_ERR_ALLOC_FAIL;
710 if (state->compression_in_started) {
711 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
712 sizeof(state->compression_in_stream))) != 0)
713 goto out;
714 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
715 goto out;
716 if (state->compression_out_started) {
717 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
718 sizeof(state->compression_out_stream))) != 0)
719 goto out;
720 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
721 goto out;
722 r = sshbuf_put_stringb(m, b);
723 out:
724 sshbuf_free(b);
725 return r;
726}
727
728/* Deserialise compression state from a blob for privsep */
729static int
730ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
731{
732 struct session_state *state = ssh->state;
733 struct sshbuf *b = NULL;
734 int r;
735 const u_char *inblob, *outblob;
736 size_t inl, outl;
737
738 if ((r = sshbuf_froms(m, &b)) != 0)
739 goto out;
740 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
741 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
742 goto out;
743 if (inl == 0)
744 state->compression_in_started = 0;
745 else if (inl != sizeof(state->compression_in_stream)) {
746 r = SSH_ERR_INTERNAL_ERROR;
747 goto out;
748 } else {
749 state->compression_in_started = 1;
750 memcpy(&state->compression_in_stream, inblob, inl);
751 }
752 if (outl == 0)
753 state->compression_out_started = 0;
754 else if (outl != sizeof(state->compression_out_stream)) {
755 r = SSH_ERR_INTERNAL_ERROR;
756 goto out;
757 } else {
758 state->compression_out_started = 1;
759 memcpy(&state->compression_out_stream, outblob, outl);
760 }
761 r = 0;
762 out:
763 sshbuf_free(b);
764 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000765}
766
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000768ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
769 void *(*allocfunc)(void *, u_int, u_int),
770 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000772 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
773 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
774 ssh->state->compression_out_stream.opaque = ctx;
775 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
776 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
777 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778}
779
Damien Miller5428f641999-11-25 11:54:57 +1100780/*
Damien Miller5428f641999-11-25 11:54:57 +1100781 * Causes any further packets to be encrypted using the given key. The same
782 * key is used for both sending and reception. However, both directions are
783 * encrypted independently of each other.
784 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000785
Damien Miller1f0311c2014-05-15 14:24:09 +1000786#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000788ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000790 struct session_state *state = ssh->state;
791 const struct sshcipher *cipher = cipher_by_number(number);
792 int r;
793 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100794
markus@openbsd.org091c3022015-01-19 19:52:16 +0000795 if (cipher == NULL)
796 fatal("%s: unknown cipher number %d", __func__, number);
797 if (keylen < 20)
798 fatal("%s: keylen too small: %d", __func__, keylen);
799 if (keylen > SSH_SESSION_KEY_LENGTH)
800 fatal("%s: keylen too big: %d", __func__, keylen);
801 memcpy(state->ssh1_key, key, keylen);
802 state->ssh1_keylen = keylen;
803 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
804 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
805 (r = cipher_init(&state->receive_context, cipher, key, keylen,
806 NULL, 0, CIPHER_DECRYPT) != 0))
807 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
808 if (!state->cipher_warning_done &&
809 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
810 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
811 error("Warning: %s", wmsg);
812 state->cipher_warning_done = 1;
813 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000814}
Damien Miller6af914a2010-09-10 11:39:26 +1000815#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000816
Damien Miller5428f641999-11-25 11:54:57 +1100817/*
818 * Finalizes and sends the packet. If the encryption key has been set,
819 * encrypts the packet before sending.
820 */
Damien Miller95def091999-11-25 00:26:21 +1100821
markus@openbsd.org091c3022015-01-19 19:52:16 +0000822int
823ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000825 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000826 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000827 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000828 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829
Damien Miller5428f641999-11-25 11:54:57 +1100830 /*
831 * If using packet compression, compress the payload of the outgoing
832 * packet.
833 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000834 if (state->packet_compression) {
835 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100836 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000837 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
838 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100839 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000840 if ((r = sshbuf_put(state->compression_buffer,
841 "\0\0\0\0\0\0\0\0", 8)) != 0)
842 goto out;
843 if ((r = compress_buffer(ssh, state->outgoing_packet,
844 state->compression_buffer)) != 0)
845 goto out;
846 sshbuf_reset(state->outgoing_packet);
847 if ((r = sshbuf_putb(state->outgoing_packet,
848 state->compression_buffer)) != 0)
849 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100850 }
851 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000852 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853
Damien Millere247cc42000-05-07 12:03:14 +1000854 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100855 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000856 if (!state->send_context.plaintext) {
857 cp = sshbuf_mutable_ptr(state->outgoing_packet);
858 if (cp == NULL) {
859 r = SSH_ERR_INTERNAL_ERROR;
860 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100861 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000862 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100863 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000864 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
865 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100866
867 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000868 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
869 sshbuf_len(state->outgoing_packet));
870 POKE_U32(buf, checksum);
871 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
872 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873
874#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100875 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000876 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877#endif
878
Damien Miller95def091999-11-25 00:26:21 +1100879 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000880 POKE_U32(buf, len);
881 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
882 goto out;
883 if ((r = sshbuf_reserve(state->output,
884 sshbuf_len(state->outgoing_packet), &cp)) != 0)
885 goto out;
886 if ((r = cipher_crypt(&state->send_context, 0, cp,
887 sshbuf_ptr(state->outgoing_packet),
888 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
889 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100890
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100892 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000893 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000895 state->p_send.packets++;
896 state->p_send.bytes += len +
897 sshbuf_len(state->outgoing_packet);
898 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899
Damien Miller5428f641999-11-25 11:54:57 +1100900 /*
Damien Miller788f2122005-11-05 15:14:59 +1100901 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000902 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
903 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100904 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000905 r = 0;
906 out:
907 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908}
909
markus@openbsd.org091c3022015-01-19 19:52:16 +0000910int
911ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000912{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000913 struct session_state *state = ssh->state;
914 struct sshenc *enc;
915 struct sshmac *mac;
916 struct sshcomp *comp;
917 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000918 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000919 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000920 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000921
Ben Lindstrom064496f2002-12-23 02:04:22 +0000922 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000923
Damien Miller963f6b22002-02-19 15:21:23 +1100924 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000925 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000926 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000927 state->p_send.packets = state->p_send.blocks = 0;
928 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100929 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000930 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000931 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000932 state->p_read.packets = state->p_read.blocks = 0;
933 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100934 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000935 if (state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000936 debug("set_newkeys: rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000937 if ((r = cipher_cleanup(cc)) != 0)
938 return r;
939 enc = &state->newkeys[mode]->enc;
940 mac = &state->newkeys[mode]->mac;
941 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000942 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100943 explicit_bzero(enc->iv, enc->iv_len);
944 explicit_bzero(enc->key, enc->key_len);
945 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000946 free(enc->name);
947 free(enc->iv);
948 free(enc->key);
949 free(mac->name);
950 free(mac->key);
951 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000952 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000953 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000954 /* move newkeys from kex to state */
955 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
956 return SSH_ERR_INTERNAL_ERROR;
957 ssh->kex->newkeys[mode] = NULL;
958 enc = &state->newkeys[mode]->enc;
959 mac = &state->newkeys[mode]->mac;
960 comp = &state->newkeys[mode]->comp;
961 if (cipher_authlen(enc->cipher) == 0) {
962 if ((r = mac_init(mac)) != 0)
963 return r;
964 }
965 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000966 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000967 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
968 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000969 return r;
970 if (!state->cipher_warning_done &&
971 (wmsg = cipher_warning_message(cc)) != NULL) {
972 error("Warning: %s", wmsg);
973 state->cipher_warning_done = 1;
974 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000975 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100976 /* explicit_bzero(enc->iv, enc->block_size);
977 explicit_bzero(enc->key, enc->key_len);
978 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000979 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000980 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +0000981 state->after_authentication)) && comp->enabled == 0) {
982 if ((r = ssh_packet_init_compression(ssh)) < 0)
983 return r;
984 if (mode == MODE_OUT) {
985 if ((r = start_compression_out(ssh, 6)) != 0)
986 return r;
987 } else {
988 if ((r = start_compression_in(ssh)) != 0)
989 return r;
990 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000991 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000992 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000993 /*
994 * The 2^(blocksize*2) limit is too expensive for 3DES,
995 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
996 */
997 if (enc->block_size >= 16)
998 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
999 else
1000 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001001 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001002 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001003 state->rekey_limit / enc->block_size);
1004 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001005}
1006
Damien Miller5428f641999-11-25 11:54:57 +11001007/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001008 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001009 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001010 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1011 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001012static int
1013ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001014{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001015 struct session_state *state = ssh->state;
1016 struct sshcomp *comp = NULL;
1017 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001018
1019 /*
1020 * Remember that we are past the authentication step, so rekeying
1021 * with COMP_DELAYED will turn on compression immediately.
1022 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001023 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001024 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001025 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001026 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001027 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001028 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001029 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001030 if ((r = ssh_packet_init_compression(ssh)) != 0)
1031 return r;
1032 if (mode == MODE_OUT) {
1033 if ((r = start_compression_out(ssh, 6)) != 0)
1034 return r;
1035 } else {
1036 if ((r = start_compression_in(ssh)) != 0)
1037 return r;
1038 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001039 comp->enabled = 1;
1040 }
1041 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001042 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001043}
1044
1045/*
Damien Miller33b13562000-04-04 14:38:59 +10001046 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1047 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001048int
1049ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001050{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001051 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001052 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001053 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001054 u_int authlen = 0, aadlen = 0;
1055 u_int len;
1056 struct sshenc *enc = NULL;
1057 struct sshmac *mac = NULL;
1058 struct sshcomp *comp = NULL;
1059 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001060
markus@openbsd.org091c3022015-01-19 19:52:16 +00001061 if (state->newkeys[MODE_OUT] != NULL) {
1062 enc = &state->newkeys[MODE_OUT]->enc;
1063 mac = &state->newkeys[MODE_OUT]->mac;
1064 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001065 /* disable mac for authenticated encryption */
1066 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1067 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001068 }
Damien Miller963f6b22002-02-19 15:21:23 +11001069 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001070 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001071
markus@openbsd.org091c3022015-01-19 19:52:16 +00001072 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001073
1074#ifdef PACKET_DEBUG
1075 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001076 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001077#endif
1078
1079 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001080 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001081 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001082 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1083 goto out;
1084 sshbuf_reset(state->compression_buffer);
1085 if ((r = compress_buffer(ssh, state->outgoing_packet,
1086 state->compression_buffer)) != 0)
1087 goto out;
1088 sshbuf_reset(state->outgoing_packet);
1089 if ((r = sshbuf_put(state->outgoing_packet,
1090 "\0\0\0\0\0", 5)) != 0 ||
1091 (r = sshbuf_putb(state->outgoing_packet,
1092 state->compression_buffer)) != 0)
1093 goto out;
1094 DBG(debug("compression: raw %d compressed %zd", len,
1095 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001096 }
1097
1098 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001099 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001100
1101 /*
1102 * calc size of padding, alloc space, get random data,
1103 * minimum padding is 4 bytes
1104 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001105 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001106 padlen = block_size - (len % block_size);
1107 if (padlen < 4)
1108 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001109 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001110 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001111 state->extra_pad =
1112 roundup(state->extra_pad, block_size);
1113 pad = state->extra_pad -
1114 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001115 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001116 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001117 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001118 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001119 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001120 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1121 goto out;
1122 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001123 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001124 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001125 } else {
1126 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001127 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001128 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001129 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001130 len = sshbuf_len(state->outgoing_packet);
1131 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1132 if (cp == NULL) {
1133 r = SSH_ERR_INTERNAL_ERROR;
1134 goto out;
1135 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001136 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001137 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001138 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001139 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1140 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001141
1142 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001143 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001144 if ((r = mac_compute(mac, state->p_send.seqnr,
1145 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001146 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001147 goto out;
1148 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001149 }
1150 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001151 if ((r = sshbuf_reserve(state->output,
1152 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1153 goto out;
1154 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1155 sshbuf_ptr(state->outgoing_packet),
1156 len - aadlen, aadlen, authlen)) != 0)
1157 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001158 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001159 if (mac && mac->enabled) {
1160 if (mac->etm) {
1161 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001162 if ((r = mac_compute(mac, state->p_send.seqnr,
1163 cp, len, macbuf, sizeof(macbuf))) != 0)
1164 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001165 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001166 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001167 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001168 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1169 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001170 }
Damien Miller33b13562000-04-04 14:38:59 +10001171#ifdef PACKET_DEBUG
1172 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001173 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001174#endif
Damien Miller4af51302000-04-16 11:18:38 +10001175 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001176 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001177 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001178 if (++state->p_send.packets == 0)
1179 if (!(ssh->compat & SSH_BUG_NOREKEY))
1180 return SSH_ERR_NEED_REKEY;
1181 state->p_send.blocks += len / block_size;
1182 state->p_send.bytes += len;
1183 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001184
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001185 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001186 r = ssh_set_newkeys(ssh, MODE_OUT);
1187 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1188 r = ssh_packet_enable_delayed_compress(ssh);
1189 else
1190 r = 0;
1191 out:
1192 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001193}
1194
markus@openbsd.org091c3022015-01-19 19:52:16 +00001195int
1196ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001197{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001198 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001199 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001200 u_char type;
1201 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001202
markus@openbsd.org091c3022015-01-19 19:52:16 +00001203 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001204
1205 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001206 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001207 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1208 (type > SSH2_MSG_TRANSPORT_MAX) ||
1209 (type == SSH2_MSG_SERVICE_REQUEST) ||
1210 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +10001211 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001212 p = calloc(1, sizeof(*p));
1213 if (p == NULL)
1214 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001215 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001216 p->payload = state->outgoing_packet;
1217 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1218 state->outgoing_packet = sshbuf_new();
1219 if (state->outgoing_packet == NULL)
1220 return SSH_ERR_ALLOC_FAIL;
1221 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001222 }
1223 }
1224
1225 /* rekeying starts with sending KEXINIT */
1226 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001227 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001228
markus@openbsd.org091c3022015-01-19 19:52:16 +00001229 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1230 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001231
1232 /* after a NEWKEYS message we can send the complete queue */
1233 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001234 state->rekeying = 0;
1235 state->rekey_time = monotime();
1236 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001237 type = p->type;
1238 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001239 sshbuf_free(state->outgoing_packet);
1240 state->outgoing_packet = p->payload;
1241 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001242 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001243 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1244 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001245 }
1246 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001247 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001248}
1249
Damien Miller33b13562000-04-04 14:38:59 +10001250/*
Damien Miller5428f641999-11-25 11:54:57 +11001251 * Waits until a packet has been received, and returns its type. Note that
1252 * no other data is processed until this returns, so this function should not
1253 * be used during the interactive session.
1254 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255
1256int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001257ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001259 struct session_state *state = ssh->state;
1260 int len, r, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001261 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001262 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001263 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264
Darren Tucker99bb7612008-06-13 22:02:50 +10001265 DBG(debug("packet_read()"));
1266
markus@openbsd.org091c3022015-01-19 19:52:16 +00001267 setp = (fd_set *)calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001268 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001269 if (setp == NULL)
1270 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001271
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001272 /*
1273 * Since we are blocking, ensure that all written packets have
1274 * been sent.
1275 */
1276 if ((r = ssh_packet_write_wait(ssh)) != 0)
1277 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001278
Damien Miller95def091999-11-25 00:26:21 +11001279 /* Stay in the loop until we have received a complete packet. */
1280 for (;;) {
1281 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001282 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1283 if (r != 0)
1284 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001285 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001286 *typep == SSH_SMSG_SUCCESS
1287 || *typep == SSH_SMSG_FAILURE
1288 || *typep == SSH_CMSG_EOF
1289 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1290 if ((r = sshpkt_get_end(ssh)) != 0)
1291 break;
Damien Miller95def091999-11-25 00:26:21 +11001292 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001293 if (*typep != SSH_MSG_NONE)
1294 break;
Damien Miller5428f641999-11-25 11:54:57 +11001295 /*
1296 * Otherwise, wait for some data to arrive, add it to the
1297 * buffer, and try again.
1298 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001299 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001300 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001301 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001302
markus@openbsd.org091c3022015-01-19 19:52:16 +00001303 if (state->packet_timeout_ms > 0) {
1304 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001305 timeoutp = &timeout;
1306 }
Damien Miller95def091999-11-25 00:26:21 +11001307 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001308 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001309 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001310 ms_to_timeval(&timeout, ms_remain);
1311 gettimeofday(&start, NULL);
1312 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001313 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001314 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001315 break;
Damien Millerea437422009-10-02 11:49:03 +10001316 if (errno != EAGAIN && errno != EINTR &&
1317 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001318 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001319 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001320 continue;
1321 ms_subtract_diff(&start, &ms_remain);
1322 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001323 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001324 break;
1325 }
1326 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001327 if (r == 0)
1328 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001329 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001330 do {
1331 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001332 len = roaming_read(state->connection_in, buf,
Darren Tuckerc5564e12009-06-21 18:53:53 +10001333 sizeof(buf), &cont);
1334 } while (len == 0 && cont);
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001335 if (len == 0)
1336 return SSH_ERR_CONN_CLOSED;
Damien Miller95def091999-11-25 00:26:21 +11001337 if (len < 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001338 return SSH_ERR_SYSTEM_ERROR;
1339
Damien Miller95def091999-11-25 00:26:21 +11001340 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001341 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
1342 return r;
Damien Miller95def091999-11-25 00:26:21 +11001343 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001344 free(setp);
1345 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346}
1347
Damien Miller278f9072001-12-21 15:00:19 +11001348int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001349ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001350{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001351 u_char type;
1352 int r;
1353
1354 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1355 fatal("%s: %s", __func__, ssh_err(r));
1356 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001357}
1358
Damien Miller5428f641999-11-25 11:54:57 +11001359/*
1360 * Waits until a packet has been received, verifies that its type matches
1361 * that given, and gives a fatal error and exits if there is a mismatch.
1362 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001364int
1365ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001367 int r;
1368 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001369
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001370 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1371 return r;
1372 if (type != expected_type) {
1373 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001374 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001375 expected_type, type)) != 0)
1376 return r;
1377 return SSH_ERR_PROTOCOL_ERROR;
1378 }
1379 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001380}
1381
1382/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001383 * packet_process_incoming. If so, reads the packet; otherwise returns
1384 * SSH_MSG_NONE. This does not wait for data from the connection.
1385 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001386 * SSH_MSG_DISCONNECT is handled specially here. Also,
1387 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1388 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001389 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390
markus@openbsd.org091c3022015-01-19 19:52:16 +00001391int
1392ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001394 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001395 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001396 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001397 const u_char *cp;
1398 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001399 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001400 int r;
1401
1402 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
Damien Miller95def091999-11-25 00:26:21 +11001404 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001405 if (sshbuf_len(state->input) < 4 + 8)
1406 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001407 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001408 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001409 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1410 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1411 len)) != 0)
1412 return r;
1413 return SSH_ERR_CONN_CORRUPT;
1414 }
Damien Miller95def091999-11-25 00:26:21 +11001415 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416
Damien Miller95def091999-11-25 00:26:21 +11001417 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001418 if (sshbuf_len(state->input) < 4 + padded_len)
1419 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420
Damien Miller95def091999-11-25 00:26:21 +11001421 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001422
Damien Miller95def091999-11-25 00:26:21 +11001423 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001424 if ((r = sshbuf_consume(state->input, 4)) != 0)
1425 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001426
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001427 /*
1428 * Cryptographic attack detector for ssh
1429 * (C)1998 CORE-SDI, Buenos Aires Argentina
1430 * Ariel Futoransky(futo@core-sdi.com)
1431 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001432 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001433 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001434 switch (detect_attack(&state->deattack,
1435 sshbuf_ptr(state->input), padded_len)) {
1436 case DEATTACK_OK:
1437 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001438 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001439 emsg = "crc32 compensation attack detected";
1440 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001441 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001442 emsg = "deattack denial of service detected";
1443 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001444 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001445 emsg = "deattack error";
1446 break;
1447 }
1448 if (emsg != NULL) {
1449 error("%s", emsg);
1450 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1451 (r = ssh_packet_write_wait(ssh)) != 0)
1452 return r;
1453 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001454 }
1455 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001456
1457 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001458 sshbuf_reset(state->incoming_packet);
1459 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1460 goto out;
1461 if ((r = cipher_crypt(&state->receive_context, 0, p,
1462 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1463 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001464
markus@openbsd.org091c3022015-01-19 19:52:16 +00001465 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1466 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467
1468#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001469 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001470 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001471#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472
Damien Miller95def091999-11-25 00:26:21 +11001473 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001474 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1475 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476
Damien Miller95def091999-11-25 00:26:21 +11001477 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001478 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1479 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001480
Damien Miller95def091999-11-25 00:26:21 +11001481 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001482 if (len != sshbuf_len(state->incoming_packet)) {
1483 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001484 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001485 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1486 (r = ssh_packet_write_wait(ssh)) != 0)
1487 return r;
1488 return SSH_ERR_CONN_CORRUPT;
1489 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490
markus@openbsd.org091c3022015-01-19 19:52:16 +00001491 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1492 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001493 if (checksum != stored_checksum) {
1494 error("Corrupted check bytes on input");
1495 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1496 (r = ssh_packet_write_wait(ssh)) != 0)
1497 return r;
1498 return SSH_ERR_CONN_CORRUPT;
1499 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001500 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1501 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001502
markus@openbsd.org091c3022015-01-19 19:52:16 +00001503 if (state->packet_compression) {
1504 sshbuf_reset(state->compression_buffer);
1505 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1506 state->compression_buffer)) != 0)
1507 goto out;
1508 sshbuf_reset(state->incoming_packet);
1509 if ((r = sshbuf_putb(state->incoming_packet,
1510 state->compression_buffer)) != 0)
1511 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001512 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001513 state->p_read.packets++;
1514 state->p_read.bytes += padded_len + 4;
1515 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1516 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001517 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1518 error("Invalid ssh1 packet type: %d", *typep);
1519 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1520 (r = ssh_packet_write_wait(ssh)) != 0)
1521 return r;
1522 return SSH_ERR_PROTOCOL_ERROR;
1523 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001524 r = 0;
1525 out:
1526 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001527}
Damien Miller95def091999-11-25 00:26:21 +11001528
markus@openbsd.org091c3022015-01-19 19:52:16 +00001529int
1530ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001531{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001532 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001533 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001534 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1535 u_int maclen, aadlen = 0, authlen = 0, block_size;
1536 struct sshenc *enc = NULL;
1537 struct sshmac *mac = NULL;
1538 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001539 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001540
markus@openbsd.org091c3022015-01-19 19:52:16 +00001541 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001542
markus@openbsd.org091c3022015-01-19 19:52:16 +00001543 if (state->packet_discard)
1544 return 0;
1545
1546 if (state->newkeys[MODE_IN] != NULL) {
1547 enc = &state->newkeys[MODE_IN]->enc;
1548 mac = &state->newkeys[MODE_IN]->mac;
1549 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001550 /* disable mac for authenticated encryption */
1551 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1552 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001553 }
1554 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001555 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001556 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001557
markus@openbsd.org091c3022015-01-19 19:52:16 +00001558 if (aadlen && state->packlen == 0) {
1559 if (cipher_get_length(&state->receive_context,
1560 &state->packlen, state->p_read.seqnr,
1561 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1562 return 0;
1563 if (state->packlen < 1 + 4 ||
1564 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001565#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001566 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001567#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001568 logit("Bad packet length %u.", state->packlen);
1569 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1570 return r;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001571 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001572 sshbuf_reset(state->incoming_packet);
1573 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001574 /*
1575 * check if input size is less than the cipher block size,
1576 * decrypt first block and extract length of incoming packet
1577 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001578 if (sshbuf_len(state->input) < block_size)
1579 return 0;
1580 sshbuf_reset(state->incoming_packet);
1581 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1582 &cp)) != 0)
1583 goto out;
1584 if ((r = cipher_crypt(&state->receive_context,
1585 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1586 block_size, 0, 0)) != 0)
1587 goto out;
1588 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1589 if (state->packlen < 1 + 4 ||
1590 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001591#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001592 fprintf(stderr, "input: \n");
1593 sshbuf_dump(state->input, stderr);
1594 fprintf(stderr, "incoming_packet: \n");
1595 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001596#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001597 logit("Bad packet length %u.", state->packlen);
1598 return ssh_packet_start_discard(ssh, enc, mac,
1599 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001600 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001601 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1602 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001603 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001604 DBG(debug("input: packet len %u", state->packlen+4));
1605
Damien Milleraf43a7a2012-12-12 10:46:31 +11001606 if (aadlen) {
1607 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001608 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001609 } else {
1610 /*
1611 * the payload size and the payload are encrypted, but we
1612 * have a partial packet of block_size bytes
1613 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001614 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001615 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001616 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1617 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001618 if (need % block_size != 0) {
1619 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001620 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001621 return ssh_packet_start_discard(ssh, enc, mac,
1622 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001623 }
Damien Miller33b13562000-04-04 14:38:59 +10001624 /*
1625 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001626 * decrypt into incoming_packet:
1627 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001628 * 'need' bytes are encrypted, followed by either
1629 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001630 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001631 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001632 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1633 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001634#ifdef PACKET_DEBUG
1635 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001636 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001637#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001638 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001639 if (mac && mac->enabled && mac->etm) {
1640 if ((r = mac_compute(mac, state->p_read.seqnr,
1641 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001642 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001643 goto out;
1644 }
1645 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1646 &cp)) != 0)
1647 goto out;
1648 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1649 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1650 goto out;
1651 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1652 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001653 /*
1654 * compute MAC over seqnr and packet,
1655 * increment sequence number for incoming packet
1656 */
Damien Miller4af51302000-04-16 11:18:38 +10001657 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001658 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001659 if ((r = mac_compute(mac, state->p_read.seqnr,
1660 sshbuf_ptr(state->incoming_packet),
1661 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001662 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001663 goto out;
1664 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001665 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001666 logit("Corrupted MAC on input.");
1667 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001668 return SSH_ERR_INTERNAL_ERROR;
1669 return ssh_packet_start_discard(ssh, enc, mac,
1670 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001671 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001672
1673 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1674 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1675 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001676 }
Damien Miller278f9072001-12-21 15:00:19 +11001677 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001678 *seqnr_p = state->p_read.seqnr;
1679 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001680 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001681 if (++state->p_read.packets == 0)
1682 if (!(ssh->compat & SSH_BUG_NOREKEY))
1683 return SSH_ERR_NEED_REKEY;
1684 state->p_read.blocks += (state->packlen + 4) / block_size;
1685 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001686
1687 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001688 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001689 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001690 if (padlen < 4) {
1691 if ((r = sshpkt_disconnect(ssh,
1692 "Corrupted padlen %d on input.", padlen)) != 0 ||
1693 (r = ssh_packet_write_wait(ssh)) != 0)
1694 return r;
1695 return SSH_ERR_CONN_CORRUPT;
1696 }
Damien Miller33b13562000-04-04 14:38:59 +10001697
1698 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001699 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1700 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1701 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001702
markus@openbsd.org091c3022015-01-19 19:52:16 +00001703 DBG(debug("input: len before de-compress %zd",
1704 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001705 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001706 sshbuf_reset(state->compression_buffer);
1707 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1708 state->compression_buffer)) != 0)
1709 goto out;
1710 sshbuf_reset(state->incoming_packet);
1711 if ((r = sshbuf_putb(state->incoming_packet,
1712 state->compression_buffer)) != 0)
1713 goto out;
1714 DBG(debug("input: len after de-compress %zd",
1715 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001716 }
1717 /*
1718 * get packet type, implies consume.
1719 * return length of payload (without type field)
1720 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001721 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1722 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001723 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1724 if ((r = sshpkt_disconnect(ssh,
1725 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1726 (r = ssh_packet_write_wait(ssh)) != 0)
1727 return r;
1728 return SSH_ERR_PROTOCOL_ERROR;
1729 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001730 if (*typep == SSH2_MSG_NEWKEYS)
1731 r = ssh_set_newkeys(ssh, MODE_IN);
1732 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1733 r = ssh_packet_enable_delayed_compress(ssh);
1734 else
1735 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001736#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001737 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1738 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001739#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001740 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001741 state->packlen = 0;
1742 out:
1743 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001744}
1745
1746int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001747ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001748{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001749 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001750 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001751 int r;
1752 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001753
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001754 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001755 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001756 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001757 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1758 if (r != 0)
1759 return r;
1760 if (*typep) {
1761 state->keep_alive_timeouts = 0;
1762 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001763 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001764 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001765 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001766 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001767 break;
Damien Miller33b13562000-04-04 14:38:59 +10001768 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001769 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1770 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1771 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1772 if (msg)
1773 free(msg);
1774 return r;
1775 }
Damien Miller33b13562000-04-04 14:38:59 +10001776 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001777 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001778 break;
1779 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001780 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1781 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1782 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001783 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001784 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001785 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1786 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1787 "Received disconnect from %s: %u: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001788 ssh_remote_ipaddr(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001789 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001790 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001791 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001792 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1793 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001794 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1795 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001796 break;
Damien Miller33b13562000-04-04 14:38:59 +10001797 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001798 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001799 }
Damien Miller33b13562000-04-04 14:38:59 +10001800 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001801 r = ssh_packet_read_poll1(ssh, typep);
1802 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001803 case SSH_MSG_NONE:
1804 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001805 case SSH_MSG_IGNORE:
1806 break;
1807 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001808 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1809 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001810 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001811 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001812 break;
1813 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001814 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1815 return r;
Damien Miller894926e2013-02-12 11:03:58 +11001816 error("Received disconnect from %s: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001817 ssh_remote_ipaddr(ssh), msg);
1818 free(msg);
1819 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001820 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001821 DBG(debug("received packet type %d", *typep));
1822 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001823 }
Damien Miller33b13562000-04-04 14:38:59 +10001824 }
1825 }
1826}
1827
Damien Miller5428f641999-11-25 11:54:57 +11001828/*
1829 * Buffers the given amount of input characters. This is intended to be used
1830 * together with packet_read_poll.
1831 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001832
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001833int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001834ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001835{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001836 struct session_state *state = ssh->state;
1837 int r;
1838
1839 if (state->packet_discard) {
1840 state->keep_alive_timeouts = 0; /* ?? */
1841 if (len >= state->packet_discard) {
1842 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001843 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001844 }
1845 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001846 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001847 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001848 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001849 return r;
1850
1851 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001852}
1853
Damien Miller4af51302000-04-16 11:18:38 +10001854int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001855ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001856{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001857 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001858}
1859
Damien Miller5428f641999-11-25 11:54:57 +11001860/*
1861 * Sends a diagnostic message from the server to the client. This message
1862 * can be sent at any time (but not while constructing another message). The
1863 * message is printed immediately, but only if the client is being executed
1864 * in verbose mode. These messages are primarily intended to ease debugging
1865 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001866 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11001867 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001868void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001869ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001870{
Damien Miller95def091999-11-25 00:26:21 +11001871 char buf[1024];
1872 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001873 int r;
Damien Miller95def091999-11-25 00:26:21 +11001874
markus@openbsd.org091c3022015-01-19 19:52:16 +00001875 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001876 return;
1877
Damien Miller95def091999-11-25 00:26:21 +11001878 va_start(args, fmt);
1879 vsnprintf(buf, sizeof(buf), fmt, args);
1880 va_end(args);
1881
Damien Miller7c8af4f2000-05-01 08:24:07 +10001882 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001883 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1884 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1885 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1886 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1887 (r = sshpkt_send(ssh)) != 0)
1888 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001889 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001890 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1891 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1892 (r = sshpkt_send(ssh)) != 0)
1893 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001894 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001895 if ((r = ssh_packet_write_wait(ssh)) != 0)
1896 fatal("%s: %s", __func__, ssh_err(r));
1897}
1898
1899/*
1900 * Pretty-print connection-terminating errors and exit.
1901 */
1902void
1903sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
1904{
1905 switch (r) {
1906 case SSH_ERR_CONN_CLOSED:
1907 logit("Connection closed by %.200s", ssh_remote_ipaddr(ssh));
1908 cleanup_exit(255);
1909 case SSH_ERR_CONN_TIMEOUT:
1910 logit("Connection to %.200s timed out while "
1911 "waiting to write", ssh_remote_ipaddr(ssh));
1912 cleanup_exit(255);
1913 default:
1914 fatal("%s%sConnection to %.200s: %s",
1915 tag != NULL ? tag : "", tag != NULL ? ": " : "",
1916 ssh_remote_ipaddr(ssh), ssh_err(r));
1917 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001918}
1919
Damien Miller5428f641999-11-25 11:54:57 +11001920/*
1921 * Logs the error plus constructs and sends a disconnect packet, closes the
1922 * connection, and exits. This function never returns. The error message
1923 * should not contain a newline. The length of the formatted message must
1924 * not exceed 1024 bytes.
1925 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001926void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001927ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001928{
Damien Miller95def091999-11-25 00:26:21 +11001929 char buf[1024];
1930 va_list args;
1931 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001932 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001933
Damien Miller95def091999-11-25 00:26:21 +11001934 if (disconnecting) /* Guard against recursive invocations. */
1935 fatal("packet_disconnect called recursively.");
1936 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001937
Damien Miller5428f641999-11-25 11:54:57 +11001938 /*
1939 * Format the message. Note that the caller must make sure the
1940 * message is of limited size.
1941 */
Damien Miller95def091999-11-25 00:26:21 +11001942 va_start(args, fmt);
1943 vsnprintf(buf, sizeof(buf), fmt, args);
1944 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001945
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001946 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001947 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001948
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001949 /*
1950 * Send the disconnect message to the other side, and wait
1951 * for it to get sent.
1952 */
1953 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
1954 sshpkt_fatal(ssh, __func__, r);
1955
1956 if ((r = ssh_packet_write_wait(ssh)) != 0)
1957 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001958
Damien Miller95def091999-11-25 00:26:21 +11001959 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001960 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001961 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001962}
1963
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001964/*
1965 * Checks if there is any buffered output, and tries to write some of
1966 * the output.
1967 */
1968int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001969ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001970{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001971 struct session_state *state = ssh->state;
1972 int len = sshbuf_len(state->output);
1973 int cont, r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001974
Damien Miller95def091999-11-25 00:26:21 +11001975 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001976 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001977 len = roaming_write(state->connection_out,
1978 sshbuf_ptr(state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001979 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001980 if (errno == EINTR || errno == EAGAIN ||
1981 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001982 return 0;
1983 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11001984 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001985 if (len == 0 && !cont)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001986 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001987 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001988 return r;
Damien Miller95def091999-11-25 00:26:21 +11001989 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001990 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001991}
1992
Damien Miller5428f641999-11-25 11:54:57 +11001993/*
1994 * Calls packet_write_poll repeatedly until all pending output data has been
1995 * written.
1996 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001997int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001998ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001999{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002000 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002001 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002002 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002003 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002004
markus@openbsd.org091c3022015-01-19 19:52:16 +00002005 setp = (fd_set *)calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002006 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002007 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002008 return SSH_ERR_ALLOC_FAIL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002009 ssh_packet_write_poll(ssh);
2010 while (ssh_packet_have_data_to_write(ssh)) {
2011 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002012 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002013 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002014
markus@openbsd.org091c3022015-01-19 19:52:16 +00002015 if (state->packet_timeout_ms > 0) {
2016 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002017 timeoutp = &timeout;
2018 }
2019 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002020 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002021 ms_to_timeval(&timeout, ms_remain);
2022 gettimeofday(&start, NULL);
2023 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002024 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002025 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002026 break;
Damien Millerea437422009-10-02 11:49:03 +10002027 if (errno != EAGAIN && errno != EINTR &&
2028 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002029 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002030 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002031 continue;
2032 ms_subtract_diff(&start, &ms_remain);
2033 if (ms_remain <= 0) {
2034 ret = 0;
2035 break;
2036 }
2037 }
2038 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002039 free(setp);
2040 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002041 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002042 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2043 free(setp);
2044 return r;
2045 }
Damien Miller95def091999-11-25 00:26:21 +11002046 }
Darren Tuckera627d422013-06-02 07:31:17 +10002047 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002048 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002049}
2050
2051/* Returns true if there is buffered data to write to the connection. */
2052
2053int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002054ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002055{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002056 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002057}
2058
2059/* Returns true if there is not too much data to write to the connection. */
2060
2061int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002062ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002063{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002064 if (ssh->state->interactive_mode)
2065 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002066 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002067 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002068}
2069
markus@openbsd.org091c3022015-01-19 19:52:16 +00002070void
2071ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002072{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002073#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002074 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002075 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002076 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002077# ifdef IP_TOS
2078 case AF_INET:
2079 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002080 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002081 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2082 error("setsockopt IP_TOS %d: %.100s:",
2083 tos, strerror(errno));
2084 break;
2085# endif /* IP_TOS */
2086# ifdef IPV6_TCLASS
2087 case AF_INET6:
2088 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002089 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002090 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2091 error("setsockopt IPV6_TCLASS %d: %.100s:",
2092 tos, strerror(errno));
2093 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002094# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002095 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002096#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002097}
Ben Lindstroma7433982002-12-23 02:41:41 +00002098
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002099/* Informs that the current session is interactive. Sets IP flags for that. */
2100
2101void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002102ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002103{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002104 struct session_state *state = ssh->state;
2105
2106 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002107 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002108 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002109
Damien Miller95def091999-11-25 00:26:21 +11002110 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002111 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002112
Damien Miller34132e52000-01-14 15:45:46 +11002113 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002114 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002115 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002116 set_nodelay(state->connection_in);
2117 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2118 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002119}
2120
2121/* Returns true if the current connection is interactive. */
2122
2123int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002124ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002125{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002126 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002127}
Damien Miller6162d121999-11-21 13:23:52 +11002128
Darren Tucker1f8311c2004-05-13 16:39:33 +10002129int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002130ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002131{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002132 struct session_state *state = ssh->state;
2133
2134 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002135 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002136 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002137 return -1;
2138 }
2139 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002140 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002141 return -1;
2142 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002143 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002144 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002145 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002146 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002147}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002148
Darren Tuckerf7288d72009-06-21 18:12:20 +10002149int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002150ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002151{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002152 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002153}
2154
2155void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002156ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002157{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002158 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002159}
2160
2161u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002162ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002163{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002164 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002165}
2166
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002167/*
2168 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002169 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002170 * byte SSH_MSG_IGNORE
2171 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002172 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002173 * All implementations MUST understand (and ignore) this message at any
2174 * time (after receiving the protocol version). No implementation is
2175 * required to send them. This message can be used as an additional
2176 * protection measure against advanced traffic analysis techniques.
2177 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002178void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002179ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002180{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002181 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002182 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002183
markus@openbsd.org091c3022015-01-19 19:52:16 +00002184 if ((r = sshpkt_start(ssh, compat20 ?
2185 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2186 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2187 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002188 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002189 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002190 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002191 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2192 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002193 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002194 }
2195}
Damien Millera5539d22003-04-09 20:50:06 +10002196
Darren Tucker1f8311c2004-05-13 16:39:33 +10002197#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002198int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002199ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002200{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002201 struct session_state *state = ssh->state;
2202
2203 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002204 return 0;
2205 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002206 (state->p_send.packets > MAX_PACKETS) ||
2207 (state->p_read.packets > MAX_PACKETS) ||
2208 (state->max_blocks_out &&
2209 (state->p_send.blocks > state->max_blocks_out)) ||
2210 (state->max_blocks_in &&
2211 (state->p_read.blocks > state->max_blocks_in)) ||
2212 (state->rekey_interval != 0 && state->rekey_time +
2213 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002214}
2215
2216void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002217ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002218{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002219 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
2220 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002221 ssh->state->rekey_limit = bytes;
2222 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002223 /*
2224 * We set the time here so that in post-auth privsep slave we count
2225 * from the completion of the authentication.
2226 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002227 ssh->state->rekey_time = monotime();
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002228}
2229
2230time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002231ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002232{
2233 time_t seconds;
2234
markus@openbsd.org091c3022015-01-19 19:52:16 +00002235 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002236 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002237 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002238}
Damien Miller9786e6e2005-07-26 21:54:56 +10002239
2240void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002241ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002242{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002243 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002244}
2245
2246void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002247ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002248{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002249 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002250}
2251
2252void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002253ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002254{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002255 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002256}
2257
2258void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002259ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002260{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002261 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002262}
2263
markus@openbsd.org091c3022015-01-19 19:52:16 +00002264/* XXX TODO update roaming to new API (does not work anyway) */
Darren Tuckere841eb02009-07-06 07:11:13 +10002265/*
2266 * Save the state for the real connection, and use a separate state when
2267 * resuming a suspended connection.
2268 */
2269void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002270ssh_packet_backup_state(struct ssh *ssh,
2271 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002272{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002273 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002274
markus@openbsd.org091c3022015-01-19 19:52:16 +00002275 close(ssh->state->connection_in);
2276 ssh->state->connection_in = -1;
2277 close(ssh->state->connection_out);
2278 ssh->state->connection_out = -1;
Darren Tuckere841eb02009-07-06 07:11:13 +10002279 if (backup_state)
2280 tmp = backup_state;
2281 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002282 tmp = ssh_alloc_session_state();
2283 backup_state = ssh;
2284 ssh = tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002285}
2286
markus@openbsd.org091c3022015-01-19 19:52:16 +00002287/* XXX FIXME FIXME FIXME */
Darren Tuckere841eb02009-07-06 07:11:13 +10002288/*
2289 * Swap in the old state when resuming a connecion.
2290 */
2291void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002292ssh_packet_restore_state(struct ssh *ssh,
2293 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002294{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002295 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002296 u_int len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002297 int r;
Darren Tuckere841eb02009-07-06 07:11:13 +10002298
2299 tmp = backup_state;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002300 backup_state = ssh;
2301 ssh = tmp;
2302 ssh->state->connection_in = backup_state->state->connection_in;
2303 backup_state->state->connection_in = -1;
2304 ssh->state->connection_out = backup_state->state->connection_out;
2305 backup_state->state->connection_out = -1;
2306 len = sshbuf_len(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002307 if (len > 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002308 if ((r = sshbuf_putb(ssh->state->input,
2309 backup_state->state->input)) != 0)
2310 fatal("%s: %s", __func__, ssh_err(r));
2311 sshbuf_reset(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002312 add_recv_bytes(len);
2313 }
2314}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002315
2316/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002317static int
2318ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002319{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002320 struct sshcomp *comp;
2321 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002322
2323 debug("%s: called", __func__);
2324 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002325 ssh->state->after_authentication = 1;
2326 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002327 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002328 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002329 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002330 comp = &ssh->state->newkeys[mode]->comp;
2331 if (comp && comp->enabled &&
2332 (r = ssh_packet_init_compression(ssh)) != 0)
2333 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002334 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002335 return 0;
2336}
2337
2338/* Packet state (de-)serialization for privsep */
2339
2340/* turn kex into a blob for packet state serialization */
2341static int
2342kex_to_blob(struct sshbuf *m, struct kex *kex)
2343{
2344 int r;
2345
2346 if ((r = sshbuf_put_string(m, kex->session_id,
2347 kex->session_id_len)) != 0 ||
2348 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2349 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2350 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2351 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2352 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2353 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2354 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2355 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2356 return r;
2357 return 0;
2358}
2359
2360/* turn key exchange results into a blob for packet state serialization */
2361static int
2362newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2363{
2364 struct sshbuf *b;
2365 struct sshcipher_ctx *cc;
2366 struct sshcomp *comp;
2367 struct sshenc *enc;
2368 struct sshmac *mac;
2369 struct newkeys *newkey;
2370 int r;
2371
2372 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2373 return SSH_ERR_INTERNAL_ERROR;
2374 enc = &newkey->enc;
2375 mac = &newkey->mac;
2376 comp = &newkey->comp;
2377 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2378 &ssh->state->receive_context;
2379 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2380 return r;
2381 if ((b = sshbuf_new()) == NULL)
2382 return SSH_ERR_ALLOC_FAIL;
2383 /* The cipher struct is constant and shared, you export pointer */
2384 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2385 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2386 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2387 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2388 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2389 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2390 goto out;
2391 if (cipher_authlen(enc->cipher) == 0) {
2392 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2393 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2394 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2395 goto out;
2396 }
2397 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2398 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2399 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2400 goto out;
2401 r = sshbuf_put_stringb(m, b);
2402 out:
2403 if (b != NULL)
2404 sshbuf_free(b);
2405 return r;
2406}
2407
2408/* serialize packet state into a blob */
2409int
2410ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2411{
2412 struct session_state *state = ssh->state;
2413 u_char *p;
2414 size_t slen, rlen;
2415 int r, ssh1cipher;
2416
2417 if (!compat20) {
2418 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2419 slen = cipher_get_keyiv_len(&state->send_context);
2420 rlen = cipher_get_keyiv_len(&state->receive_context);
2421 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2422 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2423 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2424 (r = sshbuf_put_u32(m, slen)) != 0 ||
2425 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2426 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2427 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2428 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2429 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2430 return r;
2431 } else {
2432 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2433 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2434 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
2435 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2436 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2437 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2438 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2439 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2440 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2441 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2442 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2443 return r;
2444 }
2445
2446 slen = cipher_get_keycontext(&state->send_context, NULL);
2447 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2448 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2449 (r = sshbuf_reserve(m, slen, &p)) != 0)
2450 return r;
2451 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2452 return SSH_ERR_INTERNAL_ERROR;
2453 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2454 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2455 return r;
2456 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2457 return SSH_ERR_INTERNAL_ERROR;
2458
2459 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2460 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2461 (r = sshbuf_put_stringb(m, state->output)) != 0)
2462 return r;
2463
2464 if (compat20) {
2465 if ((r = sshbuf_put_u64(m, get_sent_bytes())) != 0 ||
2466 (r = sshbuf_put_u64(m, get_recv_bytes())) != 0)
2467 return r;
2468 }
2469 return 0;
2470}
2471
2472/* restore key exchange results from blob for packet state de-serialization */
2473static int
2474newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2475{
2476 struct sshbuf *b = NULL;
2477 struct sshcomp *comp;
2478 struct sshenc *enc;
2479 struct sshmac *mac;
2480 struct newkeys *newkey = NULL;
2481 size_t keylen, ivlen, maclen;
2482 int r;
2483
2484 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2485 r = SSH_ERR_ALLOC_FAIL;
2486 goto out;
2487 }
2488 if ((r = sshbuf_froms(m, &b)) != 0)
2489 goto out;
2490#ifdef DEBUG_PK
2491 sshbuf_dump(b, stderr);
2492#endif
2493 enc = &newkey->enc;
2494 mac = &newkey->mac;
2495 comp = &newkey->comp;
2496
2497 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2498 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2499 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2500 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2501 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2502 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2503 goto out;
2504 if (cipher_authlen(enc->cipher) == 0) {
2505 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2506 goto out;
2507 if ((r = mac_setup(mac, mac->name)) != 0)
2508 goto out;
2509 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2510 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2511 goto out;
2512 if (maclen > mac->key_len) {
2513 r = SSH_ERR_INVALID_FORMAT;
2514 goto out;
2515 }
2516 mac->key_len = maclen;
2517 }
2518 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2519 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2520 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2521 goto out;
2522 if (enc->name == NULL ||
2523 cipher_by_name(enc->name) != enc->cipher) {
2524 r = SSH_ERR_INVALID_FORMAT;
2525 goto out;
2526 }
2527 if (sshbuf_len(b) != 0) {
2528 r = SSH_ERR_INVALID_FORMAT;
2529 goto out;
2530 }
2531 enc->key_len = keylen;
2532 enc->iv_len = ivlen;
2533 ssh->kex->newkeys[mode] = newkey;
2534 newkey = NULL;
2535 r = 0;
2536 out:
2537 if (newkey != NULL)
2538 free(newkey);
2539 if (b != NULL)
2540 sshbuf_free(b);
2541 return r;
2542}
2543
2544/* restore kex from blob for packet state de-serialization */
2545static int
2546kex_from_blob(struct sshbuf *m, struct kex **kexp)
2547{
2548 struct kex *kex;
2549 int r;
2550
2551 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2552 (kex->my = sshbuf_new()) == NULL ||
2553 (kex->peer = sshbuf_new()) == NULL) {
2554 r = SSH_ERR_ALLOC_FAIL;
2555 goto out;
2556 }
2557 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2558 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2559 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2560 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2561 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2562 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2563 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2564 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2565 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2566 goto out;
2567 kex->server = 1;
2568 kex->done = 1;
2569 r = 0;
2570 out:
2571 if (r != 0 || kexp == NULL) {
2572 if (kex != NULL) {
2573 if (kex->my != NULL)
2574 sshbuf_free(kex->my);
2575 if (kex->peer != NULL)
2576 sshbuf_free(kex->peer);
2577 free(kex);
2578 }
2579 if (kexp != NULL)
2580 *kexp = NULL;
2581 } else {
2582 *kexp = kex;
2583 }
2584 return r;
2585}
2586
2587/*
2588 * Restore packet state from content of blob 'm' (de-serialization).
2589 * Note that 'm' will be partially consumed on parsing or any other errors.
2590 */
2591int
2592ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2593{
2594 struct session_state *state = ssh->state;
2595 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2596 size_t ssh1keylen, rlen, slen, ilen, olen;
2597 int r;
2598 u_int ssh1cipher = 0;
2599 u_int64_t sent_bytes = 0, recv_bytes = 0;
2600
2601 if (!compat20) {
2602 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2603 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2604 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2605 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2606 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2607 return r;
2608 if (ssh1cipher > INT_MAX)
2609 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2610 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2611 (int)ssh1cipher);
2612 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2613 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2614 return SSH_ERR_INVALID_FORMAT;
2615 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2616 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2617 return r;
2618 } else {
2619 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2620 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2621 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
2622 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2623 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2624 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2625 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2626 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2627 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2628 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2629 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2630 return r;
2631 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2632 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2633 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2634 return r;
2635 }
2636 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2637 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2638 return r;
2639 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2640 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2641 return SSH_ERR_INVALID_FORMAT;
2642 cipher_set_keycontext(&state->send_context, keyout);
2643 cipher_set_keycontext(&state->receive_context, keyin);
2644
2645 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2646 (r = ssh_packet_set_postauth(ssh)) != 0)
2647 return r;
2648
2649 sshbuf_reset(state->input);
2650 sshbuf_reset(state->output);
2651 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2652 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2653 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2654 (r = sshbuf_put(state->output, output, olen)) != 0)
2655 return r;
2656
2657 if (compat20) {
2658 if ((r = sshbuf_get_u64(m, &sent_bytes)) != 0 ||
2659 (r = sshbuf_get_u64(m, &recv_bytes)) != 0)
2660 return r;
2661 roam_set_bytes(sent_bytes, recv_bytes);
2662 }
2663 if (sshbuf_len(m))
2664 return SSH_ERR_INVALID_FORMAT;
2665 debug3("%s: done", __func__);
2666 return 0;
2667}
2668
2669/* NEW API */
2670
2671/* put data to the outgoing packet */
2672
2673int
2674sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2675{
2676 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2677}
2678
2679int
2680sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2681{
2682 return sshbuf_putb(ssh->state->outgoing_packet, b);
2683}
2684
2685int
2686sshpkt_put_u8(struct ssh *ssh, u_char val)
2687{
2688 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2689}
2690
2691int
2692sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2693{
2694 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2695}
2696
2697int
2698sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2699{
2700 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2701}
2702
2703int
2704sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2705{
2706 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2707}
2708
2709int
2710sshpkt_put_cstring(struct ssh *ssh, const void *v)
2711{
2712 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2713}
2714
2715int
2716sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2717{
2718 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2719}
2720
2721int
2722sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2723{
2724 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2725}
2726
2727int
2728sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2729{
2730 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2731}
2732
2733int
2734sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2735{
2736 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2737}
2738
2739/* fetch data from the incoming packet */
2740
2741int
2742sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2743{
2744 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2745}
2746
2747int
2748sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2749{
2750 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2751}
2752
2753int
2754sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2755{
2756 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2757}
2758
2759int
2760sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2761{
2762 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2763}
2764
2765int
2766sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2767{
2768 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2769}
2770
2771int
2772sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2773{
2774 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2775}
2776
2777int
2778sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2779{
2780 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2781}
2782
2783int
2784sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2785{
2786 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2787}
2788
2789int
2790sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2791{
2792 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2793}
2794
2795int
2796sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2797{
2798 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2799}
2800
2801int
2802sshpkt_get_end(struct ssh *ssh)
2803{
2804 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2805 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2806 return 0;
2807}
2808
2809const u_char *
2810sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2811{
2812 if (lenp != NULL)
2813 *lenp = sshbuf_len(ssh->state->incoming_packet);
2814 return sshbuf_ptr(ssh->state->incoming_packet);
2815}
2816
2817/* start a new packet */
2818
2819int
2820sshpkt_start(struct ssh *ssh, u_char type)
2821{
2822 u_char buf[9];
2823 int len;
2824
2825 DBG(debug("packet_start[%d]", type));
2826 len = compat20 ? 6 : 9;
2827 memset(buf, 0, len - 1);
2828 buf[len - 1] = type;
2829 sshbuf_reset(ssh->state->outgoing_packet);
2830 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2831}
2832
2833/* send it */
2834
2835int
2836sshpkt_send(struct ssh *ssh)
2837{
2838 if (compat20)
2839 return ssh_packet_send2(ssh);
2840 else
2841 return ssh_packet_send1(ssh);
2842}
2843
2844int
2845sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2846{
2847 char buf[1024];
2848 va_list args;
2849 int r;
2850
2851 va_start(args, fmt);
2852 vsnprintf(buf, sizeof(buf), fmt, args);
2853 va_end(args);
2854
2855 if (compat20) {
2856 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2857 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2858 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2859 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2860 (r = sshpkt_send(ssh)) != 0)
2861 return r;
2862 } else {
2863 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2864 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2865 (r = sshpkt_send(ssh)) != 0)
2866 return r;
2867 }
2868 return 0;
2869}
2870
2871/* roundup current message to pad bytes */
2872int
2873sshpkt_add_padding(struct ssh *ssh, u_char pad)
2874{
2875 ssh->state->extra_pad = pad;
2876 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002877}