blob: f7e5bc78f1324a17de0c865272f9d7a5d23e180c [file] [log] [blame]
djm@openbsd.org696d1262016-02-04 23:43:48 +00001/* $OpenBSD: packet.c,v 1.227 2016/02/04 23:43:48 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"
markus@openbsd.org091c3022015-01-19 19:52:16 +000086#include "ssherr.h"
87#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100088
89#ifdef PACKET_DEBUG
90#define DBG(x) x
91#else
92#define DBG(x)
93#endif
94
Damien Miller13ae44c2009-01-28 16:38:41 +110095#define PACKET_MAX_SIZE (256 * 1024)
96
Darren Tuckerf7288d72009-06-21 18:12:20 +100097struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100098 u_int32_t seqnr;
99 u_int32_t packets;
100 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000101 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000102};
Damien Miller13ae44c2009-01-28 16:38:41 +1100103
Damien Millera5539d22003-04-09 20:50:06 +1000104struct packet {
105 TAILQ_ENTRY(packet) next;
106 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000107 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000108};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000109
110struct session_state {
111 /*
112 * This variable contains the file descriptors used for
113 * communicating with the other side. connection_in is used for
114 * reading; connection_out for writing. These can be the same
115 * descriptor, in which case it is assumed to be a socket.
116 */
117 int connection_in;
118 int connection_out;
119
120 /* Protocol flags for the remote side. */
121 u_int remote_protocol_flags;
122
123 /* Encryption context for receiving data. Only used for decryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000124 struct sshcipher_ctx receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000125
126 /* Encryption context for sending data. Only used for encryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000127 struct sshcipher_ctx send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000128
129 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000130 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000131
132 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000133 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000134
135 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000136 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000137
138 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000139 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000140
141 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000142 struct sshbuf *compression_buffer;
143
144 /* Incoming/outgoing compression dictionaries */
145 z_stream compression_in_stream;
146 z_stream compression_out_stream;
147 int compression_in_started;
148 int compression_out_started;
149 int compression_in_failures;
150 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000151
152 /*
153 * Flag indicating whether packet compression/decompression is
154 * enabled.
155 */
156 int packet_compression;
157
158 /* default maximum packet size */
159 u_int max_packet_size;
160
161 /* Flag indicating whether this module has been initialized. */
162 int initialized;
163
164 /* Set to true if the connection is interactive. */
165 int interactive_mode;
166
167 /* Set to true if we are the server side. */
168 int server_side;
169
170 /* Set to true if we are authenticated. */
171 int after_authentication;
172
173 int keep_alive_timeouts;
174
175 /* The maximum time that we will wait to send or receive a packet */
176 int packet_timeout_ms;
177
178 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000179 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000180 struct packet_state p_read, p_send;
181
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000182 /* Volume-based rekeying */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000183 u_int64_t max_blocks_in, max_blocks_out, rekey_limit;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000184
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000185 /* Time-based rekeying */
markus@openbsd.org02db4682015-02-13 18:57:00 +0000186 u_int32_t rekey_interval; /* how often in seconds */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000187 time_t rekey_time; /* time of last rekeying */
188
Darren Tuckerf7288d72009-06-21 18:12:20 +1000189 /* Session key for protocol v1 */
190 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
191 u_int ssh1_keylen;
192
193 /* roundup current message to extra_pad bytes */
194 u_char extra_pad;
195
196 /* XXX discard incoming data after MAC error */
197 u_int packet_discard;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000198 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000199
200 /* Used in packet_read_poll2() */
201 u_int packlen;
202
Darren Tucker7b935c72009-06-21 18:59:36 +1000203 /* Used in packet_send2 */
204 int rekeying;
205
206 /* Used in packet_set_interactive */
207 int set_interactive_called;
208
209 /* Used in packet_set_maxsize */
210 int set_maxsize_called;
211
markus@openbsd.org091c3022015-01-19 19:52:16 +0000212 /* One-off warning about weak ciphers */
213 int cipher_warning_done;
214
215 /* SSH1 CRC compensation attack detector */
216 struct deattack_ctx deattack;
217
Darren Tuckerf7288d72009-06-21 18:12:20 +1000218 TAILQ_HEAD(, packet) outgoing;
219};
220
markus@openbsd.org091c3022015-01-19 19:52:16 +0000221struct ssh *
222ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000223{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000224 struct ssh *ssh = NULL;
225 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000226
markus@openbsd.org091c3022015-01-19 19:52:16 +0000227 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
228 (state = calloc(1, sizeof(*state))) == NULL ||
229 (state->input = sshbuf_new()) == NULL ||
230 (state->output = sshbuf_new()) == NULL ||
231 (state->outgoing_packet = sshbuf_new()) == NULL ||
232 (state->incoming_packet = sshbuf_new()) == NULL)
233 goto fail;
234 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000235 TAILQ_INIT(&ssh->private_keys);
236 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000237 state->connection_in = -1;
238 state->connection_out = -1;
239 state->max_packet_size = 32768;
240 state->packet_timeout_ms = -1;
241 state->p_send.packets = state->p_read.packets = 0;
242 state->initialized = 1;
243 /*
244 * ssh_packet_send2() needs to queue packets until
245 * we've done the initial key exchange.
246 */
247 state->rekeying = 1;
248 ssh->state = state;
249 return ssh;
250 fail:
251 if (state) {
252 sshbuf_free(state->input);
253 sshbuf_free(state->output);
254 sshbuf_free(state->incoming_packet);
255 sshbuf_free(state->outgoing_packet);
256 free(state);
257 }
258 free(ssh);
259 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000260}
Damien Millera5539d22003-04-09 20:50:06 +1000261
Damien Miller5428f641999-11-25 11:54:57 +1100262/*
263 * Sets the descriptors used for communication. Disables encryption until
264 * packet_set_encryption_key is called.
265 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000266struct ssh *
267ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000269 struct session_state *state;
270 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000271 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000272
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000273 if (none == NULL) {
274 error("%s: cannot load cipher 'none'", __func__);
275 return NULL;
276 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000277 if (ssh == NULL)
278 ssh = ssh_alloc_session_state();
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000279 if (ssh == NULL) {
280 error("%s: cound not allocate state", __func__);
281 return NULL;
282 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000283 state = ssh->state;
284 state->connection_in = fd_in;
285 state->connection_out = fd_out;
286 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000287 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000288 (r = cipher_init(&state->receive_context, none,
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000289 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
290 error("%s: cipher_init failed: %s", __func__, ssh_err(r));
jsg@openbsd.org1cb30162015-03-11 00:48:39 +0000291 free(ssh);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000292 return NULL;
293 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000294 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
295 deattack_init(&state->deattack);
djm@openbsd.orgd4c02952015-02-11 01:20:38 +0000296 /*
297 * Cache the IP address of the remote connection for use in error
298 * messages that might be generated after the connection has closed.
299 */
300 (void)ssh_remote_ipaddr(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000301 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302}
303
Darren Tucker3fc464e2008-06-13 06:42:45 +1000304void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000305ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000306{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000307 struct session_state *state = ssh->state;
308
Damien Miller8ed4de82011-12-19 10:52:50 +1100309 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000310 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000311 return;
312 }
313 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000314 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000315 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000316 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000317}
318
markus@openbsd.org091c3022015-01-19 19:52:16 +0000319int
320ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100321{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000322 struct session_state *state = ssh->state;
323 int r;
324
325 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100326 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000327
Damien Miller13ae44c2009-01-28 16:38:41 +1100328 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000329 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000330 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000331 if ((r = sshbuf_put(state->incoming_packet, buf,
332 sizeof(buf))) != 0)
333 return r;
334 (void) mac_compute(state->packet_discard_mac,
335 state->p_read.seqnr,
336 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
337 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100338 }
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000339 logit("Finished discarding for %.200s port %d",
340 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000341 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100342}
343
markus@openbsd.org091c3022015-01-19 19:52:16 +0000344static int
345ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
346 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100347{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000348 struct session_state *state = ssh->state;
349 int r;
350
351 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
352 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
353 return r;
354 return SSH_ERR_MAC_INVALID;
355 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100356 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000357 state->packet_discard_mac = mac;
358 if (sshbuf_len(state->input) >= discard &&
359 (r = ssh_packet_stop_discard(ssh)) != 0)
360 return r;
361 state->packet_discard = discard - sshbuf_len(state->input);
362 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100363}
364
Damien Miller34132e52000-01-14 15:45:46 +1100365/* Returns 1 if remote host is connected via socket, 0 if not. */
366
367int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000368ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100369{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000370 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100371 struct sockaddr_storage from, to;
372 socklen_t fromlen, tolen;
373
374 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000375 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100376 return 1;
377 fromlen = sizeof(from);
378 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000379 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000380 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100381 return 0;
382 tolen = sizeof(to);
383 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000384 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000385 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100386 return 0;
387 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
388 return 0;
389 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
390 return 0;
391 return 1;
392}
393
Ben Lindstromf6027d32002-03-22 01:42:04 +0000394void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000395ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000396{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000397 if (ibytes)
398 *ibytes = ssh->state->p_read.bytes;
399 if (obytes)
400 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000401}
402
403int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000404ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100405{
406 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100407 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100408
409 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000410 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000411 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100412 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000413#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100414 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000415 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000416 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000417#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000418 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100419}
420
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421/* Sets the connection into non-blocking mode. */
422
423void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000424ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425{
Damien Miller95def091999-11-25 00:26:21 +1100426 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000427 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
markus@openbsd.org091c3022015-01-19 19:52:16 +0000429 if (ssh->state->connection_out != ssh->state->connection_in)
430 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431}
432
433/* Returns the socket used for reading. */
434
435int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000436ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000438 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439}
440
441/* Returns the descriptor used for writing. */
442
443int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000444ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000446 return ssh->state->connection_out;
447}
448
449/*
450 * Returns the IP-address of the remote host as a string. The returned
451 * string must not be freed.
452 */
453
454const char *
455ssh_remote_ipaddr(struct ssh *ssh)
456{
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000457 const int sock = ssh->state->connection_in;
458
markus@openbsd.org091c3022015-01-19 19:52:16 +0000459 /* Check whether we have cached the ipaddr. */
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000460 if (ssh->remote_ipaddr == NULL) {
461 if (ssh_packet_connection_is_on_socket(ssh)) {
462 ssh->remote_ipaddr = get_peer_ipaddr(sock);
463 ssh->remote_port = get_sock_port(sock, 0);
464 } else {
465 ssh->remote_ipaddr = strdup("UNKNOWN");
466 ssh->remote_port = 0;
467 }
468 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000469 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470}
471
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000472/* Returns the port number of the remote host. */
473
474int
475ssh_remote_port(struct ssh *ssh)
476{
477 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
478 return ssh->remote_port;
479}
480
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481/* Closes the connection and clears and frees internal data structures. */
482
483void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000484ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000486 struct session_state *state = ssh->state;
487 int r;
488 u_int mode;
489
490 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100491 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000492 state->initialized = 0;
493 if (state->connection_in == state->connection_out) {
494 shutdown(state->connection_out, SHUT_RDWR);
495 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100496 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000497 close(state->connection_in);
498 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100499 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000500 sshbuf_free(state->input);
501 sshbuf_free(state->output);
502 sshbuf_free(state->outgoing_packet);
503 sshbuf_free(state->incoming_packet);
504 for (mode = 0; mode < MODE_MAX; mode++)
505 kex_free_newkeys(state->newkeys[mode]);
506 if (state->compression_buffer) {
507 sshbuf_free(state->compression_buffer);
508 if (state->compression_out_started) {
509 z_streamp stream = &state->compression_out_stream;
510 debug("compress outgoing: "
511 "raw data %llu, compressed %llu, factor %.2f",
512 (unsigned long long)stream->total_in,
513 (unsigned long long)stream->total_out,
514 stream->total_in == 0 ? 0.0 :
515 (double) stream->total_out / stream->total_in);
516 if (state->compression_out_failures == 0)
517 deflateEnd(stream);
518 }
519 if (state->compression_in_started) {
520 z_streamp stream = &state->compression_out_stream;
521 debug("compress incoming: "
522 "raw data %llu, compressed %llu, factor %.2f",
523 (unsigned long long)stream->total_out,
524 (unsigned long long)stream->total_in,
525 stream->total_out == 0 ? 0.0 :
526 (double) stream->total_in / stream->total_out);
527 if (state->compression_in_failures == 0)
528 inflateEnd(stream);
529 }
Damien Miller95def091999-11-25 00:26:21 +1100530 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000531 if ((r = cipher_cleanup(&state->send_context)) != 0)
532 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
533 if ((r = cipher_cleanup(&state->receive_context)) != 0)
534 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000535 free(ssh->remote_ipaddr);
536 ssh->remote_ipaddr = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000537 free(ssh->state);
538 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539}
540
541/* Sets remote side protocol flags. */
542
543void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000544ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000546 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547}
548
549/* Returns the remote protocol flags set earlier by the above function. */
550
Ben Lindstrom46c16222000-12-22 01:43:59 +0000551u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000552ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000554 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555}
556
Damien Miller5428f641999-11-25 11:54:57 +1100557/*
558 * Starts packet compression from the next packet on in both directions.
559 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
560 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561
markus@openbsd.org091c3022015-01-19 19:52:16 +0000562static int
563ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000564{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000565 if (!ssh->state->compression_buffer &&
566 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
567 return SSH_ERR_ALLOC_FAIL;
568 return 0;
569}
570
571static int
572start_compression_out(struct ssh *ssh, int level)
573{
574 if (level < 1 || level > 9)
575 return SSH_ERR_INVALID_ARGUMENT;
576 debug("Enabling compression at level %d.", level);
577 if (ssh->state->compression_out_started == 1)
578 deflateEnd(&ssh->state->compression_out_stream);
579 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
580 case Z_OK:
581 ssh->state->compression_out_started = 1;
582 break;
583 case Z_MEM_ERROR:
584 return SSH_ERR_ALLOC_FAIL;
585 default:
586 return SSH_ERR_INTERNAL_ERROR;
587 }
588 return 0;
589}
590
591static int
592start_compression_in(struct ssh *ssh)
593{
594 if (ssh->state->compression_in_started == 1)
595 inflateEnd(&ssh->state->compression_in_stream);
596 switch (inflateInit(&ssh->state->compression_in_stream)) {
597 case Z_OK:
598 ssh->state->compression_in_started = 1;
599 break;
600 case Z_MEM_ERROR:
601 return SSH_ERR_ALLOC_FAIL;
602 default:
603 return SSH_ERR_INTERNAL_ERROR;
604 }
605 return 0;
606}
607
608int
609ssh_packet_start_compression(struct ssh *ssh, int level)
610{
611 int r;
612
613 if (ssh->state->packet_compression && !compat20)
614 return SSH_ERR_INTERNAL_ERROR;
615 ssh->state->packet_compression = 1;
616 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
617 (r = start_compression_in(ssh)) != 0 ||
618 (r = start_compression_out(ssh, level)) != 0)
619 return r;
620 return 0;
621}
622
623/* XXX remove need for separate compression buffer */
624static int
625compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
626{
627 u_char buf[4096];
628 int r, status;
629
630 if (ssh->state->compression_out_started != 1)
631 return SSH_ERR_INTERNAL_ERROR;
632
633 /* This case is not handled below. */
634 if (sshbuf_len(in) == 0)
635 return 0;
636
637 /* Input is the contents of the input buffer. */
638 if ((ssh->state->compression_out_stream.next_in =
639 sshbuf_mutable_ptr(in)) == NULL)
640 return SSH_ERR_INTERNAL_ERROR;
641 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
642
643 /* Loop compressing until deflate() returns with avail_out != 0. */
644 do {
645 /* Set up fixed-size output buffer. */
646 ssh->state->compression_out_stream.next_out = buf;
647 ssh->state->compression_out_stream.avail_out = sizeof(buf);
648
649 /* Compress as much data into the buffer as possible. */
650 status = deflate(&ssh->state->compression_out_stream,
651 Z_PARTIAL_FLUSH);
652 switch (status) {
653 case Z_MEM_ERROR:
654 return SSH_ERR_ALLOC_FAIL;
655 case Z_OK:
656 /* Append compressed data to output_buffer. */
657 if ((r = sshbuf_put(out, buf, sizeof(buf) -
658 ssh->state->compression_out_stream.avail_out)) != 0)
659 return r;
660 break;
661 case Z_STREAM_ERROR:
662 default:
663 ssh->state->compression_out_failures++;
664 return SSH_ERR_INVALID_FORMAT;
665 }
666 } while (ssh->state->compression_out_stream.avail_out == 0);
667 return 0;
668}
669
670static int
671uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
672{
673 u_char buf[4096];
674 int r, status;
675
676 if (ssh->state->compression_in_started != 1)
677 return SSH_ERR_INTERNAL_ERROR;
678
679 if ((ssh->state->compression_in_stream.next_in =
680 sshbuf_mutable_ptr(in)) == NULL)
681 return SSH_ERR_INTERNAL_ERROR;
682 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
683
684 for (;;) {
685 /* Set up fixed-size output buffer. */
686 ssh->state->compression_in_stream.next_out = buf;
687 ssh->state->compression_in_stream.avail_out = sizeof(buf);
688
689 status = inflate(&ssh->state->compression_in_stream,
690 Z_PARTIAL_FLUSH);
691 switch (status) {
692 case Z_OK:
693 if ((r = sshbuf_put(out, buf, sizeof(buf) -
694 ssh->state->compression_in_stream.avail_out)) != 0)
695 return r;
696 break;
697 case Z_BUF_ERROR:
698 /*
699 * Comments in zlib.h say that we should keep calling
700 * inflate() until we get an error. This appears to
701 * be the error that we get.
702 */
703 return 0;
704 case Z_DATA_ERROR:
705 return SSH_ERR_INVALID_FORMAT;
706 case Z_MEM_ERROR:
707 return SSH_ERR_ALLOC_FAIL;
708 case Z_STREAM_ERROR:
709 default:
710 ssh->state->compression_in_failures++;
711 return SSH_ERR_INTERNAL_ERROR;
712 }
713 }
714 /* NOTREACHED */
715}
716
717/* Serialise compression state into a blob for privsep */
718static int
719ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
720{
721 struct session_state *state = ssh->state;
722 struct sshbuf *b;
723 int r;
724
725 if ((b = sshbuf_new()) == NULL)
726 return SSH_ERR_ALLOC_FAIL;
727 if (state->compression_in_started) {
728 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
729 sizeof(state->compression_in_stream))) != 0)
730 goto out;
731 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
732 goto out;
733 if (state->compression_out_started) {
734 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
735 sizeof(state->compression_out_stream))) != 0)
736 goto out;
737 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
738 goto out;
739 r = sshbuf_put_stringb(m, b);
740 out:
741 sshbuf_free(b);
742 return r;
743}
744
745/* Deserialise compression state from a blob for privsep */
746static int
747ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
748{
749 struct session_state *state = ssh->state;
750 struct sshbuf *b = NULL;
751 int r;
752 const u_char *inblob, *outblob;
753 size_t inl, outl;
754
755 if ((r = sshbuf_froms(m, &b)) != 0)
756 goto out;
757 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
758 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
759 goto out;
760 if (inl == 0)
761 state->compression_in_started = 0;
762 else if (inl != sizeof(state->compression_in_stream)) {
763 r = SSH_ERR_INTERNAL_ERROR;
764 goto out;
765 } else {
766 state->compression_in_started = 1;
767 memcpy(&state->compression_in_stream, inblob, inl);
768 }
769 if (outl == 0)
770 state->compression_out_started = 0;
771 else if (outl != sizeof(state->compression_out_stream)) {
772 r = SSH_ERR_INTERNAL_ERROR;
773 goto out;
774 } else {
775 state->compression_out_started = 1;
776 memcpy(&state->compression_out_stream, outblob, outl);
777 }
778 r = 0;
779 out:
780 sshbuf_free(b);
781 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000782}
783
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000785ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
786 void *(*allocfunc)(void *, u_int, u_int),
787 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000789 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
790 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
791 ssh->state->compression_out_stream.opaque = ctx;
792 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
793 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
794 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795}
796
Damien Miller5428f641999-11-25 11:54:57 +1100797/*
Damien Miller5428f641999-11-25 11:54:57 +1100798 * Causes any further packets to be encrypted using the given key. The same
799 * key is used for both sending and reception. However, both directions are
800 * encrypted independently of each other.
801 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000802
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000804ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000806#ifndef WITH_SSH1
807 fatal("no SSH protocol 1 support");
808#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000809 struct session_state *state = ssh->state;
810 const struct sshcipher *cipher = cipher_by_number(number);
811 int r;
812 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100813
markus@openbsd.org091c3022015-01-19 19:52:16 +0000814 if (cipher == NULL)
815 fatal("%s: unknown cipher number %d", __func__, number);
816 if (keylen < 20)
817 fatal("%s: keylen too small: %d", __func__, keylen);
818 if (keylen > SSH_SESSION_KEY_LENGTH)
819 fatal("%s: keylen too big: %d", __func__, keylen);
820 memcpy(state->ssh1_key, key, keylen);
821 state->ssh1_keylen = keylen;
822 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
823 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
824 (r = cipher_init(&state->receive_context, cipher, key, keylen,
825 NULL, 0, CIPHER_DECRYPT) != 0))
826 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
827 if (!state->cipher_warning_done &&
828 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
829 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
830 error("Warning: %s", wmsg);
831 state->cipher_warning_done = 1;
832 }
Damien Miller773dda22015-01-30 23:10:17 +1100833#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000834}
835
Damien Miller5428f641999-11-25 11:54:57 +1100836/*
837 * Finalizes and sends the packet. If the encryption key has been set,
838 * encrypts the packet before sending.
839 */
Damien Miller95def091999-11-25 00:26:21 +1100840
markus@openbsd.org091c3022015-01-19 19:52:16 +0000841int
842ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000844 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000845 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000846 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000847 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
Damien Miller5428f641999-11-25 11:54:57 +1100849 /*
850 * If using packet compression, compress the payload of the outgoing
851 * packet.
852 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000853 if (state->packet_compression) {
854 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100855 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000856 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
857 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100858 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000859 if ((r = sshbuf_put(state->compression_buffer,
860 "\0\0\0\0\0\0\0\0", 8)) != 0)
861 goto out;
862 if ((r = compress_buffer(ssh, state->outgoing_packet,
863 state->compression_buffer)) != 0)
864 goto out;
865 sshbuf_reset(state->outgoing_packet);
866 if ((r = sshbuf_putb(state->outgoing_packet,
867 state->compression_buffer)) != 0)
868 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100869 }
870 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000871 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872
Damien Millere247cc42000-05-07 12:03:14 +1000873 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100874 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000875 if (!state->send_context.plaintext) {
876 cp = sshbuf_mutable_ptr(state->outgoing_packet);
877 if (cp == NULL) {
878 r = SSH_ERR_INTERNAL_ERROR;
879 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100880 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000881 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100882 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000883 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
884 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100885
886 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000887 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
888 sshbuf_len(state->outgoing_packet));
889 POKE_U32(buf, checksum);
890 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
891 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
893#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100894 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000895 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896#endif
897
Damien Miller95def091999-11-25 00:26:21 +1100898 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000899 POKE_U32(buf, len);
900 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
901 goto out;
902 if ((r = sshbuf_reserve(state->output,
903 sshbuf_len(state->outgoing_packet), &cp)) != 0)
904 goto out;
905 if ((r = cipher_crypt(&state->send_context, 0, cp,
906 sshbuf_ptr(state->outgoing_packet),
907 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
908 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100909
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100911 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000912 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000914 state->p_send.packets++;
915 state->p_send.bytes += len +
916 sshbuf_len(state->outgoing_packet);
917 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918
Damien Miller5428f641999-11-25 11:54:57 +1100919 /*
Damien Miller788f2122005-11-05 15:14:59 +1100920 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000921 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
922 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100923 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000924 r = 0;
925 out:
926 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927}
928
markus@openbsd.org091c3022015-01-19 19:52:16 +0000929int
930ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000931{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000932 struct session_state *state = ssh->state;
933 struct sshenc *enc;
934 struct sshmac *mac;
935 struct sshcomp *comp;
936 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000937 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000938 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000939 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000940
Ben Lindstrom064496f2002-12-23 02:04:22 +0000941 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000942
Damien Miller963f6b22002-02-19 15:21:23 +1100943 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000944 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000945 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000946 state->p_send.packets = state->p_send.blocks = 0;
947 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100948 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000949 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000950 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000951 state->p_read.packets = state->p_read.blocks = 0;
952 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100953 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000954 if (state->newkeys[mode] != NULL) {
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000955 debug("set_newkeys: rekeying, input %llu bytes %llu blocks, "
956 "output %llu bytes %llu blocks",
djm@openbsd.org696d1262016-02-04 23:43:48 +0000957 (unsigned long long)state->p_read.bytes,
958 (unsigned long long)state->p_read.blocks,
959 (unsigned long long)state->p_send.bytes,
960 (unsigned long long)state->p_send.blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000961 if ((r = cipher_cleanup(cc)) != 0)
962 return r;
963 enc = &state->newkeys[mode]->enc;
964 mac = &state->newkeys[mode]->mac;
965 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000966 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100967 explicit_bzero(enc->iv, enc->iv_len);
968 explicit_bzero(enc->key, enc->key_len);
969 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000970 free(enc->name);
971 free(enc->iv);
972 free(enc->key);
973 free(mac->name);
974 free(mac->key);
975 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000976 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000977 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000978 /* move newkeys from kex to state */
979 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
980 return SSH_ERR_INTERNAL_ERROR;
981 ssh->kex->newkeys[mode] = NULL;
982 enc = &state->newkeys[mode]->enc;
983 mac = &state->newkeys[mode]->mac;
984 comp = &state->newkeys[mode]->comp;
985 if (cipher_authlen(enc->cipher) == 0) {
986 if ((r = mac_init(mac)) != 0)
987 return r;
988 }
989 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000990 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000991 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
992 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000993 return r;
994 if (!state->cipher_warning_done &&
995 (wmsg = cipher_warning_message(cc)) != NULL) {
996 error("Warning: %s", wmsg);
997 state->cipher_warning_done = 1;
998 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000999 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +11001000 /* explicit_bzero(enc->iv, enc->block_size);
1001 explicit_bzero(enc->key, enc->key_len);
1002 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +10001003 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +10001004 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +00001005 state->after_authentication)) && comp->enabled == 0) {
1006 if ((r = ssh_packet_init_compression(ssh)) < 0)
1007 return r;
1008 if (mode == MODE_OUT) {
1009 if ((r = start_compression_out(ssh, 6)) != 0)
1010 return r;
1011 } else {
1012 if ((r = start_compression_in(ssh)) != 0)
1013 return r;
1014 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001015 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001016 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001017 /*
1018 * The 2^(blocksize*2) limit is too expensive for 3DES,
1019 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1020 */
1021 if (enc->block_size >= 16)
1022 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1023 else
1024 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001025 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001026 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001027 state->rekey_limit / enc->block_size);
djm@openbsd.org696d1262016-02-04 23:43:48 +00001028 debug("rekey after %llu blocks", (unsigned long long)*max_blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001029 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001030}
1031
Damien Miller5428f641999-11-25 11:54:57 +11001032/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001033 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001034 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001035 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1036 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001037static int
1038ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001039{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001040 struct session_state *state = ssh->state;
1041 struct sshcomp *comp = NULL;
1042 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001043
1044 /*
1045 * Remember that we are past the authentication step, so rekeying
1046 * with COMP_DELAYED will turn on compression immediately.
1047 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001048 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001049 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001050 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001051 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001052 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001053 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001054 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001055 if ((r = ssh_packet_init_compression(ssh)) != 0)
1056 return r;
1057 if (mode == MODE_OUT) {
1058 if ((r = start_compression_out(ssh, 6)) != 0)
1059 return r;
1060 } else {
1061 if ((r = start_compression_in(ssh)) != 0)
1062 return r;
1063 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001064 comp->enabled = 1;
1065 }
1066 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001067 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001068}
1069
djm@openbsd.org28136472016-01-29 05:46:01 +00001070/* Used to mute debug logging for noisy packet types */
1071static int
1072ssh_packet_log_type(u_char type)
1073{
1074 switch (type) {
1075 case SSH2_MSG_CHANNEL_DATA:
1076 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
1077 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
1078 return 0;
1079 default:
1080 return 1;
1081 }
1082}
1083
Damien Miller9786e6e2005-07-26 21:54:56 +10001084/*
Damien Miller33b13562000-04-04 14:38:59 +10001085 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1086 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001087int
1088ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001089{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001090 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001091 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001092 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001093 u_int authlen = 0, aadlen = 0;
1094 u_int len;
1095 struct sshenc *enc = NULL;
1096 struct sshmac *mac = NULL;
1097 struct sshcomp *comp = NULL;
1098 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001099
markus@openbsd.org091c3022015-01-19 19:52:16 +00001100 if (state->newkeys[MODE_OUT] != NULL) {
1101 enc = &state->newkeys[MODE_OUT]->enc;
1102 mac = &state->newkeys[MODE_OUT]->mac;
1103 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001104 /* disable mac for authenticated encryption */
1105 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1106 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001107 }
Damien Miller963f6b22002-02-19 15:21:23 +11001108 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001109 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001110
markus@openbsd.org091c3022015-01-19 19:52:16 +00001111 type = (sshbuf_ptr(state->outgoing_packet))[5];
djm@openbsd.org28136472016-01-29 05:46:01 +00001112 if (ssh_packet_log_type(type))
1113 debug3("send packet: type %u", type);
Damien Miller33b13562000-04-04 14:38:59 +10001114#ifdef PACKET_DEBUG
1115 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001116 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001117#endif
1118
1119 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001120 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001121 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001122 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1123 goto out;
1124 sshbuf_reset(state->compression_buffer);
1125 if ((r = compress_buffer(ssh, state->outgoing_packet,
1126 state->compression_buffer)) != 0)
1127 goto out;
1128 sshbuf_reset(state->outgoing_packet);
1129 if ((r = sshbuf_put(state->outgoing_packet,
1130 "\0\0\0\0\0", 5)) != 0 ||
1131 (r = sshbuf_putb(state->outgoing_packet,
1132 state->compression_buffer)) != 0)
1133 goto out;
1134 DBG(debug("compression: raw %d compressed %zd", len,
1135 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001136 }
1137
1138 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001139 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001140
1141 /*
1142 * calc size of padding, alloc space, get random data,
1143 * minimum padding is 4 bytes
1144 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001145 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001146 padlen = block_size - (len % block_size);
1147 if (padlen < 4)
1148 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001149 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001150 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001151 state->extra_pad =
1152 roundup(state->extra_pad, block_size);
1153 pad = state->extra_pad -
1154 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001155 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001156 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001157 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001158 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001159 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001160 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1161 goto out;
1162 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001163 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001164 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001165 } else {
1166 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001167 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001168 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001169 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001170 len = sshbuf_len(state->outgoing_packet);
1171 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1172 if (cp == NULL) {
1173 r = SSH_ERR_INTERNAL_ERROR;
1174 goto out;
1175 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001176 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001177 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001178 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001179 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1180 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001181
1182 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001183 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001184 if ((r = mac_compute(mac, state->p_send.seqnr,
1185 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001186 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001187 goto out;
1188 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001189 }
1190 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001191 if ((r = sshbuf_reserve(state->output,
1192 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1193 goto out;
1194 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1195 sshbuf_ptr(state->outgoing_packet),
1196 len - aadlen, aadlen, authlen)) != 0)
1197 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001198 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001199 if (mac && mac->enabled) {
1200 if (mac->etm) {
1201 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001202 if ((r = mac_compute(mac, state->p_send.seqnr,
1203 cp, len, macbuf, sizeof(macbuf))) != 0)
1204 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001205 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001206 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001207 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001208 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1209 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001210 }
Damien Miller33b13562000-04-04 14:38:59 +10001211#ifdef PACKET_DEBUG
1212 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001213 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001214#endif
Damien Miller4af51302000-04-16 11:18:38 +10001215 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001216 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001217 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001218 if (++state->p_send.packets == 0)
1219 if (!(ssh->compat & SSH_BUG_NOREKEY))
1220 return SSH_ERR_NEED_REKEY;
1221 state->p_send.blocks += len / block_size;
1222 state->p_send.bytes += len;
1223 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001224
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001225 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001226 r = ssh_set_newkeys(ssh, MODE_OUT);
1227 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1228 r = ssh_packet_enable_delayed_compress(ssh);
1229 else
1230 r = 0;
1231 out:
1232 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001233}
1234
markus@openbsd.org091c3022015-01-19 19:52:16 +00001235int
1236ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001237{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001238 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001239 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001240 u_char type;
1241 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001242
markus@openbsd.org091c3022015-01-19 19:52:16 +00001243 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001244
1245 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001246 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001247 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1248 (type > SSH2_MSG_TRANSPORT_MAX) ||
1249 (type == SSH2_MSG_SERVICE_REQUEST) ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001250 (type == SSH2_MSG_SERVICE_ACCEPT) ||
1251 (type == SSH2_MSG_EXT_INFO)) {
Damien Millera5539d22003-04-09 20:50:06 +10001252 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001253 p = calloc(1, sizeof(*p));
1254 if (p == NULL)
1255 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001256 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001257 p->payload = state->outgoing_packet;
1258 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1259 state->outgoing_packet = sshbuf_new();
1260 if (state->outgoing_packet == NULL)
1261 return SSH_ERR_ALLOC_FAIL;
1262 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001263 }
1264 }
1265
1266 /* rekeying starts with sending KEXINIT */
1267 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001268 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001269
markus@openbsd.org091c3022015-01-19 19:52:16 +00001270 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1271 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001272
1273 /* after a NEWKEYS message we can send the complete queue */
1274 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001275 state->rekeying = 0;
1276 state->rekey_time = monotime();
1277 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001278 type = p->type;
1279 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001280 sshbuf_free(state->outgoing_packet);
1281 state->outgoing_packet = p->payload;
1282 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001283 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001284 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1285 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001286 }
1287 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001288 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001289}
1290
Damien Miller33b13562000-04-04 14:38:59 +10001291/*
Damien Miller5428f641999-11-25 11:54:57 +11001292 * Waits until a packet has been received, and returns its type. Note that
1293 * no other data is processed until this returns, so this function should not
1294 * be used during the interactive session.
1295 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296
1297int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001298ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001300 struct session_state *state = ssh->state;
markus@openbsd.orga3068632016-01-14 16:17:39 +00001301 int len, r, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001302 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001303 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001304 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001305
Darren Tucker99bb7612008-06-13 22:02:50 +10001306 DBG(debug("packet_read()"));
1307
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001308 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001309 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001310 if (setp == NULL)
1311 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001312
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001313 /*
1314 * Since we are blocking, ensure that all written packets have
1315 * been sent.
1316 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001317 if ((r = ssh_packet_write_wait(ssh)) != 0)
1318 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319
Damien Miller95def091999-11-25 00:26:21 +11001320 /* Stay in the loop until we have received a complete packet. */
1321 for (;;) {
1322 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001323 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1324 if (r != 0)
1325 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001326 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001327 *typep == SSH_SMSG_SUCCESS
1328 || *typep == SSH_SMSG_FAILURE
1329 || *typep == SSH_CMSG_EOF
1330 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1331 if ((r = sshpkt_get_end(ssh)) != 0)
1332 break;
Damien Miller95def091999-11-25 00:26:21 +11001333 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001334 if (*typep != SSH_MSG_NONE)
1335 break;
Damien Miller5428f641999-11-25 11:54:57 +11001336 /*
1337 * Otherwise, wait for some data to arrive, add it to the
1338 * buffer, and try again.
1339 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001340 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001341 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001342 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001343
markus@openbsd.org091c3022015-01-19 19:52:16 +00001344 if (state->packet_timeout_ms > 0) {
1345 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001346 timeoutp = &timeout;
1347 }
Damien Miller95def091999-11-25 00:26:21 +11001348 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001349 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001350 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001351 ms_to_timeval(&timeout, ms_remain);
1352 gettimeofday(&start, NULL);
1353 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001354 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001355 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001356 break;
Damien Millerea437422009-10-02 11:49:03 +10001357 if (errno != EAGAIN && errno != EINTR &&
1358 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001359 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001360 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001361 continue;
1362 ms_subtract_diff(&start, &ms_remain);
1363 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001364 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001365 break;
1366 }
1367 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001368 if (r == 0)
1369 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001370 /* Read data from the socket. */
markus@openbsd.orga3068632016-01-14 16:17:39 +00001371 len = read(state->connection_in, buf, sizeof(buf));
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001372 if (len == 0) {
1373 r = SSH_ERR_CONN_CLOSED;
1374 goto out;
1375 }
1376 if (len < 0) {
1377 r = SSH_ERR_SYSTEM_ERROR;
1378 goto out;
1379 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001380
Damien Miller95def091999-11-25 00:26:21 +11001381 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001382 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001383 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001384 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001385 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001386 free(setp);
1387 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001388}
1389
Damien Miller278f9072001-12-21 15:00:19 +11001390int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001391ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001392{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001393 u_char type;
1394 int r;
1395
1396 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1397 fatal("%s: %s", __func__, ssh_err(r));
1398 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001399}
1400
Damien Miller5428f641999-11-25 11:54:57 +11001401/*
1402 * Waits until a packet has been received, verifies that its type matches
1403 * that given, and gives a fatal error and exits if there is a mismatch.
1404 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001406int
1407ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001408{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001409 int r;
1410 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001412 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1413 return r;
1414 if (type != expected_type) {
1415 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001416 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001417 expected_type, type)) != 0)
1418 return r;
1419 return SSH_ERR_PROTOCOL_ERROR;
1420 }
1421 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001422}
1423
1424/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001425 * packet_process_incoming. If so, reads the packet; otherwise returns
1426 * SSH_MSG_NONE. This does not wait for data from the connection.
1427 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001428 * SSH_MSG_DISCONNECT is handled specially here. Also,
1429 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1430 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001431 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432
markus@openbsd.org091c3022015-01-19 19:52:16 +00001433int
1434ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001436 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001437 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001438 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001439 const u_char *cp;
1440 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001441 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001442 int r;
1443
1444 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445
Damien Miller95def091999-11-25 00:26:21 +11001446 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001447 if (sshbuf_len(state->input) < 4 + 8)
1448 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001449 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001450 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001451 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1452 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1453 len)) != 0)
1454 return r;
1455 return SSH_ERR_CONN_CORRUPT;
1456 }
Damien Miller95def091999-11-25 00:26:21 +11001457 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001458
Damien Miller95def091999-11-25 00:26:21 +11001459 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001460 if (sshbuf_len(state->input) < 4 + padded_len)
1461 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462
Damien Miller95def091999-11-25 00:26:21 +11001463 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001464
Damien Miller95def091999-11-25 00:26:21 +11001465 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001466 if ((r = sshbuf_consume(state->input, 4)) != 0)
1467 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001468
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001469 /*
1470 * Cryptographic attack detector for ssh
1471 * (C)1998 CORE-SDI, Buenos Aires Argentina
1472 * Ariel Futoransky(futo@core-sdi.com)
1473 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001474 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001475 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001476 switch (detect_attack(&state->deattack,
1477 sshbuf_ptr(state->input), padded_len)) {
1478 case DEATTACK_OK:
1479 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001480 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001481 emsg = "crc32 compensation attack detected";
1482 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001483 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001484 emsg = "deattack denial of service detected";
1485 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001486 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001487 emsg = "deattack error";
1488 break;
1489 }
1490 if (emsg != NULL) {
1491 error("%s", emsg);
1492 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1493 (r = ssh_packet_write_wait(ssh)) != 0)
1494 return r;
1495 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001496 }
1497 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001498
1499 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001500 sshbuf_reset(state->incoming_packet);
1501 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1502 goto out;
1503 if ((r = cipher_crypt(&state->receive_context, 0, p,
1504 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1505 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001506
markus@openbsd.org091c3022015-01-19 19:52:16 +00001507 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1508 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001509
1510#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001511 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001512 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001513#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001514
Damien Miller95def091999-11-25 00:26:21 +11001515 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001516 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1517 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001518
Damien Miller95def091999-11-25 00:26:21 +11001519 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001520 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1521 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001522
Damien Miller95def091999-11-25 00:26:21 +11001523 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001524 if (len != sshbuf_len(state->incoming_packet)) {
1525 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001526 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001527 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1528 (r = ssh_packet_write_wait(ssh)) != 0)
1529 return r;
1530 return SSH_ERR_CONN_CORRUPT;
1531 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001532
markus@openbsd.org091c3022015-01-19 19:52:16 +00001533 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1534 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001535 if (checksum != stored_checksum) {
1536 error("Corrupted check bytes on input");
1537 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1538 (r = ssh_packet_write_wait(ssh)) != 0)
1539 return r;
1540 return SSH_ERR_CONN_CORRUPT;
1541 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001542 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1543 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001544
markus@openbsd.org091c3022015-01-19 19:52:16 +00001545 if (state->packet_compression) {
1546 sshbuf_reset(state->compression_buffer);
1547 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1548 state->compression_buffer)) != 0)
1549 goto out;
1550 sshbuf_reset(state->incoming_packet);
1551 if ((r = sshbuf_putb(state->incoming_packet,
1552 state->compression_buffer)) != 0)
1553 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001554 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001555 state->p_read.packets++;
1556 state->p_read.bytes += padded_len + 4;
1557 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1558 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001559 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1560 error("Invalid ssh1 packet type: %d", *typep);
1561 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1562 (r = ssh_packet_write_wait(ssh)) != 0)
1563 return r;
1564 return SSH_ERR_PROTOCOL_ERROR;
1565 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001566 r = 0;
1567 out:
1568 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001569}
Damien Miller95def091999-11-25 00:26:21 +11001570
markus@openbsd.org091c3022015-01-19 19:52:16 +00001571int
1572ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001573{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001574 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001575 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001576 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1577 u_int maclen, aadlen = 0, authlen = 0, block_size;
1578 struct sshenc *enc = NULL;
1579 struct sshmac *mac = NULL;
1580 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001581 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001582
markus@openbsd.org091c3022015-01-19 19:52:16 +00001583 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001584
markus@openbsd.org091c3022015-01-19 19:52:16 +00001585 if (state->packet_discard)
1586 return 0;
1587
1588 if (state->newkeys[MODE_IN] != NULL) {
1589 enc = &state->newkeys[MODE_IN]->enc;
1590 mac = &state->newkeys[MODE_IN]->mac;
1591 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001592 /* disable mac for authenticated encryption */
1593 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1594 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001595 }
1596 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001597 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001598 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001599
markus@openbsd.org091c3022015-01-19 19:52:16 +00001600 if (aadlen && state->packlen == 0) {
1601 if (cipher_get_length(&state->receive_context,
1602 &state->packlen, state->p_read.seqnr,
1603 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1604 return 0;
1605 if (state->packlen < 1 + 4 ||
1606 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001607#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001608 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001609#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001610 logit("Bad packet length %u.", state->packlen);
1611 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1612 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001613 return SSH_ERR_CONN_CORRUPT;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001614 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001615 sshbuf_reset(state->incoming_packet);
1616 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001617 /*
1618 * check if input size is less than the cipher block size,
1619 * decrypt first block and extract length of incoming packet
1620 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001621 if (sshbuf_len(state->input) < block_size)
1622 return 0;
1623 sshbuf_reset(state->incoming_packet);
1624 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1625 &cp)) != 0)
1626 goto out;
1627 if ((r = cipher_crypt(&state->receive_context,
1628 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1629 block_size, 0, 0)) != 0)
1630 goto out;
1631 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1632 if (state->packlen < 1 + 4 ||
1633 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001634#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001635 fprintf(stderr, "input: \n");
1636 sshbuf_dump(state->input, stderr);
1637 fprintf(stderr, "incoming_packet: \n");
1638 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001639#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001640 logit("Bad packet length %u.", state->packlen);
1641 return ssh_packet_start_discard(ssh, enc, mac,
1642 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001643 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001644 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1645 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001646 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001647 DBG(debug("input: packet len %u", state->packlen+4));
1648
Damien Milleraf43a7a2012-12-12 10:46:31 +11001649 if (aadlen) {
1650 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001651 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001652 } else {
1653 /*
1654 * the payload size and the payload are encrypted, but we
1655 * have a partial packet of block_size bytes
1656 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001657 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001658 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001659 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1660 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001661 if (need % block_size != 0) {
1662 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001663 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001664 return ssh_packet_start_discard(ssh, enc, mac,
1665 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001666 }
Damien Miller33b13562000-04-04 14:38:59 +10001667 /*
1668 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001669 * decrypt into incoming_packet:
1670 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001671 * 'need' bytes are encrypted, followed by either
1672 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001673 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001674 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001675 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1676 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001677#ifdef PACKET_DEBUG
1678 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001679 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001680#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001681 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001682 if (mac && mac->enabled && mac->etm) {
1683 if ((r = mac_compute(mac, state->p_read.seqnr,
1684 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001685 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001686 goto out;
1687 }
1688 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1689 &cp)) != 0)
1690 goto out;
1691 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1692 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1693 goto out;
1694 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1695 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001696 /*
1697 * compute MAC over seqnr and packet,
1698 * increment sequence number for incoming packet
1699 */
Damien Miller4af51302000-04-16 11:18:38 +10001700 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001701 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001702 if ((r = mac_compute(mac, state->p_read.seqnr,
1703 sshbuf_ptr(state->incoming_packet),
1704 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001705 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001706 goto out;
1707 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001708 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001709 logit("Corrupted MAC on input.");
1710 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001711 return SSH_ERR_INTERNAL_ERROR;
1712 return ssh_packet_start_discard(ssh, enc, mac,
1713 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001714 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001715
1716 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1717 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1718 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001719 }
Damien Miller278f9072001-12-21 15:00:19 +11001720 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001721 *seqnr_p = state->p_read.seqnr;
1722 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001723 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001724 if (++state->p_read.packets == 0)
1725 if (!(ssh->compat & SSH_BUG_NOREKEY))
1726 return SSH_ERR_NEED_REKEY;
1727 state->p_read.blocks += (state->packlen + 4) / block_size;
1728 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001729
1730 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001731 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001732 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001733 if (padlen < 4) {
1734 if ((r = sshpkt_disconnect(ssh,
1735 "Corrupted padlen %d on input.", padlen)) != 0 ||
1736 (r = ssh_packet_write_wait(ssh)) != 0)
1737 return r;
1738 return SSH_ERR_CONN_CORRUPT;
1739 }
Damien Miller33b13562000-04-04 14:38:59 +10001740
1741 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001742 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1743 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1744 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001745
markus@openbsd.org091c3022015-01-19 19:52:16 +00001746 DBG(debug("input: len before de-compress %zd",
1747 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001748 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001749 sshbuf_reset(state->compression_buffer);
1750 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1751 state->compression_buffer)) != 0)
1752 goto out;
1753 sshbuf_reset(state->incoming_packet);
1754 if ((r = sshbuf_putb(state->incoming_packet,
1755 state->compression_buffer)) != 0)
1756 goto out;
1757 DBG(debug("input: len after de-compress %zd",
1758 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001759 }
1760 /*
1761 * get packet type, implies consume.
1762 * return length of payload (without type field)
1763 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001764 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1765 goto out;
djm@openbsd.org28136472016-01-29 05:46:01 +00001766 if (ssh_packet_log_type(*typep))
1767 debug3("receive packet: type %u", *typep);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001768 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1769 if ((r = sshpkt_disconnect(ssh,
1770 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1771 (r = ssh_packet_write_wait(ssh)) != 0)
1772 return r;
1773 return SSH_ERR_PROTOCOL_ERROR;
1774 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001775 if (*typep == SSH2_MSG_NEWKEYS)
1776 r = ssh_set_newkeys(ssh, MODE_IN);
1777 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1778 r = ssh_packet_enable_delayed_compress(ssh);
1779 else
1780 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001781#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001782 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1783 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001784#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001785 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001786 state->packlen = 0;
1787 out:
1788 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001789}
1790
1791int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001792ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001793{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001794 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001795 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001796 int r;
1797 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001798
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001799 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001800 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001801 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001802 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1803 if (r != 0)
1804 return r;
1805 if (*typep) {
1806 state->keep_alive_timeouts = 0;
1807 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001808 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001809 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001810 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001811 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001812 break;
Damien Miller33b13562000-04-04 14:38:59 +10001813 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001814 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1815 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1816 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001817 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001818 return r;
1819 }
Damien Miller33b13562000-04-04 14:38:59 +10001820 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001821 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001822 break;
1823 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001824 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1825 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1826 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001827 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001828 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001829 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1830 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001831 "Received disconnect from %s port %d:"
1832 "%u: %.400s", ssh_remote_ipaddr(ssh),
1833 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001834 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001835 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001836 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001837 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1838 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001839 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1840 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001841 break;
Damien Miller33b13562000-04-04 14:38:59 +10001842 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001843 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001844 }
Damien Miller33b13562000-04-04 14:38:59 +10001845 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001846 r = ssh_packet_read_poll1(ssh, typep);
1847 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001848 case SSH_MSG_NONE:
1849 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001850 case SSH_MSG_IGNORE:
1851 break;
1852 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001853 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1854 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001855 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001856 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001857 break;
1858 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001859 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1860 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001861 error("Received disconnect from %s port %d: "
1862 "%.400s", ssh_remote_ipaddr(ssh),
1863 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001864 free(msg);
1865 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001866 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001867 DBG(debug("received packet type %d", *typep));
1868 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001869 }
Damien Miller33b13562000-04-04 14:38:59 +10001870 }
1871 }
1872}
1873
Damien Miller5428f641999-11-25 11:54:57 +11001874/*
1875 * Buffers the given amount of input characters. This is intended to be used
1876 * together with packet_read_poll.
1877 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001879int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001880ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001881{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001882 struct session_state *state = ssh->state;
1883 int r;
1884
1885 if (state->packet_discard) {
1886 state->keep_alive_timeouts = 0; /* ?? */
1887 if (len >= state->packet_discard) {
1888 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001889 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001890 }
1891 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001892 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001893 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001894 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001895 return r;
1896
1897 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001898}
1899
Damien Miller4af51302000-04-16 11:18:38 +10001900int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001901ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001902{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001903 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001904}
1905
Damien Miller5428f641999-11-25 11:54:57 +11001906/*
1907 * Sends a diagnostic message from the server to the client. This message
1908 * can be sent at any time (but not while constructing another message). The
1909 * message is printed immediately, but only if the client is being executed
1910 * in verbose mode. These messages are primarily intended to ease debugging
1911 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001912 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11001913 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001914void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001915ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001916{
Damien Miller95def091999-11-25 00:26:21 +11001917 char buf[1024];
1918 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001919 int r;
Damien Miller95def091999-11-25 00:26:21 +11001920
markus@openbsd.org091c3022015-01-19 19:52:16 +00001921 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001922 return;
1923
Damien Miller95def091999-11-25 00:26:21 +11001924 va_start(args, fmt);
1925 vsnprintf(buf, sizeof(buf), fmt, args);
1926 va_end(args);
1927
Damien Miller7c8af4f2000-05-01 08:24:07 +10001928 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001929 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1930 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1931 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1932 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1933 (r = sshpkt_send(ssh)) != 0)
1934 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001935 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001936 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1937 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1938 (r = sshpkt_send(ssh)) != 0)
1939 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001940 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001941 if ((r = ssh_packet_write_wait(ssh)) != 0)
1942 fatal("%s: %s", __func__, ssh_err(r));
1943}
1944
1945/*
1946 * Pretty-print connection-terminating errors and exit.
1947 */
1948void
1949sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
1950{
1951 switch (r) {
1952 case SSH_ERR_CONN_CLOSED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001953 logit("Connection closed by %.200s port %d",
1954 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001955 cleanup_exit(255);
1956 case SSH_ERR_CONN_TIMEOUT:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001957 logit("Connection %s %.200s port %d timed out",
1958 ssh->state->server_side ? "from" : "to",
1959 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001960 cleanup_exit(255);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001961 case SSH_ERR_DISCONNECTED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001962 logit("Disconnected from %.200s port %d",
1963 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001964 cleanup_exit(255);
1965 case SSH_ERR_SYSTEM_ERROR:
1966 if (errno == ECONNRESET) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001967 logit("Connection reset by %.200s port %d",
1968 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001969 cleanup_exit(255);
1970 }
1971 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001972 case SSH_ERR_NO_CIPHER_ALG_MATCH:
1973 case SSH_ERR_NO_MAC_ALG_MATCH:
1974 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
1975 case SSH_ERR_NO_KEX_ALG_MATCH:
1976 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
1977 if (ssh && ssh->kex && ssh->kex->failed_choice) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001978 fatal("Unable to negotiate with %.200s port %d: %s. "
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001979 "Their offer: %s", ssh_remote_ipaddr(ssh),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001980 ssh_remote_port(ssh), ssh_err(r),
1981 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001982 }
1983 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001984 default:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001985 fatal("%s%sConnection %s %.200s port %d: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001986 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001987 ssh->state->server_side ? "from" : "to",
1988 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001989 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001990}
1991
Damien Miller5428f641999-11-25 11:54:57 +11001992/*
1993 * Logs the error plus constructs and sends a disconnect packet, closes the
1994 * connection, and exits. This function never returns. The error message
1995 * should not contain a newline. The length of the formatted message must
1996 * not exceed 1024 bytes.
1997 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001998void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001999ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002000{
Damien Miller95def091999-11-25 00:26:21 +11002001 char buf[1024];
2002 va_list args;
2003 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002004 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002005
Damien Miller95def091999-11-25 00:26:21 +11002006 if (disconnecting) /* Guard against recursive invocations. */
2007 fatal("packet_disconnect called recursively.");
2008 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002009
Damien Miller5428f641999-11-25 11:54:57 +11002010 /*
2011 * Format the message. Note that the caller must make sure the
2012 * message is of limited size.
2013 */
Damien Miller95def091999-11-25 00:26:21 +11002014 va_start(args, fmt);
2015 vsnprintf(buf, sizeof(buf), fmt, args);
2016 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002017
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002018 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10002019 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002020
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002021 /*
2022 * Send the disconnect message to the other side, and wait
2023 * for it to get sent.
2024 */
2025 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2026 sshpkt_fatal(ssh, __func__, r);
2027
2028 if ((r = ssh_packet_write_wait(ssh)) != 0)
2029 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002030
Damien Miller95def091999-11-25 00:26:21 +11002031 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002032 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002033 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002034}
2035
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002036/*
2037 * Checks if there is any buffered output, and tries to write some of
2038 * the output.
2039 */
2040int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002041ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002042{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002043 struct session_state *state = ssh->state;
2044 int len = sshbuf_len(state->output);
markus@openbsd.orga3068632016-01-14 16:17:39 +00002045 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002046
Damien Miller95def091999-11-25 00:26:21 +11002047 if (len > 0) {
markus@openbsd.orga3068632016-01-14 16:17:39 +00002048 len = write(state->connection_out,
2049 sshbuf_ptr(state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10002050 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002051 if (errno == EINTR || errno == EAGAIN ||
2052 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002053 return 0;
2054 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002055 }
markus@openbsd.orga3068632016-01-14 16:17:39 +00002056 if (len == 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002057 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002058 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002059 return r;
Damien Miller95def091999-11-25 00:26:21 +11002060 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002061 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002062}
2063
Damien Miller5428f641999-11-25 11:54:57 +11002064/*
2065 * Calls packet_write_poll repeatedly until all pending output data has been
2066 * written.
2067 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002068int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002069ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002070{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002071 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002072 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002073 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002074 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002075
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002076 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002077 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002078 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002079 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002080 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2081 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002082 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002083 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002084 while (ssh_packet_have_data_to_write(ssh)) {
2085 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002086 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002087 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002088
markus@openbsd.org091c3022015-01-19 19:52:16 +00002089 if (state->packet_timeout_ms > 0) {
2090 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002091 timeoutp = &timeout;
2092 }
2093 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002094 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002095 ms_to_timeval(&timeout, ms_remain);
2096 gettimeofday(&start, NULL);
2097 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002098 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002099 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002100 break;
Damien Millerea437422009-10-02 11:49:03 +10002101 if (errno != EAGAIN && errno != EINTR &&
2102 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002103 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002104 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002105 continue;
2106 ms_subtract_diff(&start, &ms_remain);
2107 if (ms_remain <= 0) {
2108 ret = 0;
2109 break;
2110 }
2111 }
2112 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002113 free(setp);
2114 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002115 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002116 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2117 free(setp);
2118 return r;
2119 }
Damien Miller95def091999-11-25 00:26:21 +11002120 }
Darren Tuckera627d422013-06-02 07:31:17 +10002121 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002122 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002123}
2124
2125/* Returns true if there is buffered data to write to the connection. */
2126
2127int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002128ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002129{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002130 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002131}
2132
2133/* Returns true if there is not too much data to write to the connection. */
2134
2135int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002136ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002137{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002138 if (ssh->state->interactive_mode)
2139 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002140 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002141 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002142}
2143
markus@openbsd.org091c3022015-01-19 19:52:16 +00002144void
2145ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002146{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002147#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002148 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002149 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002150 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002151# ifdef IP_TOS
2152 case AF_INET:
2153 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002154 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002155 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2156 error("setsockopt IP_TOS %d: %.100s:",
2157 tos, strerror(errno));
2158 break;
2159# endif /* IP_TOS */
2160# ifdef IPV6_TCLASS
2161 case AF_INET6:
2162 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002163 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002164 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2165 error("setsockopt IPV6_TCLASS %d: %.100s:",
2166 tos, strerror(errno));
2167 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002168# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002169 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002170#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002171}
Ben Lindstroma7433982002-12-23 02:41:41 +00002172
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002173/* Informs that the current session is interactive. Sets IP flags for that. */
2174
2175void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002176ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002177{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002178 struct session_state *state = ssh->state;
2179
2180 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002181 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002182 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002183
Damien Miller95def091999-11-25 00:26:21 +11002184 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002185 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002186
Damien Miller34132e52000-01-14 15:45:46 +11002187 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002188 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002189 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002190 set_nodelay(state->connection_in);
2191 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2192 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002193}
2194
2195/* Returns true if the current connection is interactive. */
2196
2197int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002198ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002199{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002200 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002201}
Damien Miller6162d121999-11-21 13:23:52 +11002202
Darren Tucker1f8311c2004-05-13 16:39:33 +10002203int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002204ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002205{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002206 struct session_state *state = ssh->state;
2207
2208 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002209 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002210 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002211 return -1;
2212 }
2213 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002214 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002215 return -1;
2216 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002217 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002218 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002219 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002220 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002221}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002222
Darren Tuckerf7288d72009-06-21 18:12:20 +10002223int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002224ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002225{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002226 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002227}
2228
2229void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002230ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002231{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002232 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002233}
2234
2235u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002236ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002237{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002238 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002239}
2240
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002241/*
2242 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002243 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002244 * byte SSH_MSG_IGNORE
2245 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002246 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002247 * All implementations MUST understand (and ignore) this message at any
2248 * time (after receiving the protocol version). No implementation is
2249 * required to send them. This message can be used as an additional
2250 * protection measure against advanced traffic analysis techniques.
2251 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002252void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002253ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002254{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002255 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002256 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002257
markus@openbsd.org091c3022015-01-19 19:52:16 +00002258 if ((r = sshpkt_start(ssh, compat20 ?
2259 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2260 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2261 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002262 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002263 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002264 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002265 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2266 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002267 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002268 }
2269}
Damien Millera5539d22003-04-09 20:50:06 +10002270
Darren Tucker1f8311c2004-05-13 16:39:33 +10002271#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002272int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002273ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002274{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002275 struct session_state *state = ssh->state;
2276
2277 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002278 return 0;
2279 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002280 (state->p_send.packets > MAX_PACKETS) ||
2281 (state->p_read.packets > MAX_PACKETS) ||
2282 (state->max_blocks_out &&
dtucker@openbsd.org6fd6e282016-01-29 03:31:03 +00002283 (state->p_send.blocks > state->max_blocks_out)) ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002284 (state->max_blocks_in &&
dtucker@openbsd.org6fd6e282016-01-29 03:31:03 +00002285 (state->p_read.blocks > state->max_blocks_in)) ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002286 (state->rekey_interval != 0 && state->rekey_time +
2287 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002288}
2289
2290void
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002291ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002292{
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002293 debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes,
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002294 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002295 ssh->state->rekey_limit = bytes;
2296 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002297}
2298
2299time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002300ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002301{
2302 time_t seconds;
2303
markus@openbsd.org091c3022015-01-19 19:52:16 +00002304 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002305 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002306 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002307}
Damien Miller9786e6e2005-07-26 21:54:56 +10002308
2309void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002310ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002311{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002312 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002313}
2314
2315void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002316ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002317{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002318 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002319}
2320
2321void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002322ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002323{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002324 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002325}
2326
2327void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002328ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002329{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002330 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002331}
2332
Damien Millerc31a0cd2014-05-15 14:37:39 +10002333/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002334static int
2335ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002336{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002337 struct sshcomp *comp;
2338 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002339
2340 debug("%s: called", __func__);
2341 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002342 ssh->state->after_authentication = 1;
2343 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002344 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002345 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002346 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002347 comp = &ssh->state->newkeys[mode]->comp;
2348 if (comp && comp->enabled &&
2349 (r = ssh_packet_init_compression(ssh)) != 0)
2350 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002351 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002352 return 0;
2353}
2354
2355/* Packet state (de-)serialization for privsep */
2356
2357/* turn kex into a blob for packet state serialization */
2358static int
2359kex_to_blob(struct sshbuf *m, struct kex *kex)
2360{
2361 int r;
2362
2363 if ((r = sshbuf_put_string(m, kex->session_id,
2364 kex->session_id_len)) != 0 ||
2365 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2366 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2367 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2368 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2369 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2370 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2371 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2372 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2373 return r;
2374 return 0;
2375}
2376
2377/* turn key exchange results into a blob for packet state serialization */
2378static int
2379newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2380{
2381 struct sshbuf *b;
2382 struct sshcipher_ctx *cc;
2383 struct sshcomp *comp;
2384 struct sshenc *enc;
2385 struct sshmac *mac;
2386 struct newkeys *newkey;
2387 int r;
2388
2389 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2390 return SSH_ERR_INTERNAL_ERROR;
2391 enc = &newkey->enc;
2392 mac = &newkey->mac;
2393 comp = &newkey->comp;
2394 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2395 &ssh->state->receive_context;
2396 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2397 return r;
2398 if ((b = sshbuf_new()) == NULL)
2399 return SSH_ERR_ALLOC_FAIL;
2400 /* The cipher struct is constant and shared, you export pointer */
2401 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2402 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2403 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2404 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2405 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2406 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2407 goto out;
2408 if (cipher_authlen(enc->cipher) == 0) {
2409 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2410 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2411 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2412 goto out;
2413 }
2414 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2415 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2416 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2417 goto out;
2418 r = sshbuf_put_stringb(m, b);
2419 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002420 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002421 return r;
2422}
2423
2424/* serialize packet state into a blob */
2425int
2426ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2427{
2428 struct session_state *state = ssh->state;
2429 u_char *p;
2430 size_t slen, rlen;
2431 int r, ssh1cipher;
2432
2433 if (!compat20) {
2434 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2435 slen = cipher_get_keyiv_len(&state->send_context);
2436 rlen = cipher_get_keyiv_len(&state->receive_context);
2437 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2438 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2439 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2440 (r = sshbuf_put_u32(m, slen)) != 0 ||
2441 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2442 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2443 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2444 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2445 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2446 return r;
2447 } else {
2448 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2449 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2450 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002451 (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002452 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002453 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2454 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2455 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2456 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2457 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2458 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2459 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2460 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2461 return r;
2462 }
2463
2464 slen = cipher_get_keycontext(&state->send_context, NULL);
2465 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2466 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2467 (r = sshbuf_reserve(m, slen, &p)) != 0)
2468 return r;
2469 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2470 return SSH_ERR_INTERNAL_ERROR;
2471 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2472 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2473 return r;
2474 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2475 return SSH_ERR_INTERNAL_ERROR;
2476
2477 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2478 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2479 (r = sshbuf_put_stringb(m, state->output)) != 0)
2480 return r;
2481
markus@openbsd.org091c3022015-01-19 19:52:16 +00002482 return 0;
2483}
2484
2485/* restore key exchange results from blob for packet state de-serialization */
2486static int
2487newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2488{
2489 struct sshbuf *b = NULL;
2490 struct sshcomp *comp;
2491 struct sshenc *enc;
2492 struct sshmac *mac;
2493 struct newkeys *newkey = NULL;
2494 size_t keylen, ivlen, maclen;
2495 int r;
2496
2497 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2498 r = SSH_ERR_ALLOC_FAIL;
2499 goto out;
2500 }
2501 if ((r = sshbuf_froms(m, &b)) != 0)
2502 goto out;
2503#ifdef DEBUG_PK
2504 sshbuf_dump(b, stderr);
2505#endif
2506 enc = &newkey->enc;
2507 mac = &newkey->mac;
2508 comp = &newkey->comp;
2509
2510 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2511 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2512 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2513 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2514 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2515 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2516 goto out;
2517 if (cipher_authlen(enc->cipher) == 0) {
2518 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2519 goto out;
2520 if ((r = mac_setup(mac, mac->name)) != 0)
2521 goto out;
2522 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2523 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2524 goto out;
2525 if (maclen > mac->key_len) {
2526 r = SSH_ERR_INVALID_FORMAT;
2527 goto out;
2528 }
2529 mac->key_len = maclen;
2530 }
2531 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2532 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2533 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2534 goto out;
2535 if (enc->name == NULL ||
2536 cipher_by_name(enc->name) != enc->cipher) {
2537 r = SSH_ERR_INVALID_FORMAT;
2538 goto out;
2539 }
2540 if (sshbuf_len(b) != 0) {
2541 r = SSH_ERR_INVALID_FORMAT;
2542 goto out;
2543 }
2544 enc->key_len = keylen;
2545 enc->iv_len = ivlen;
2546 ssh->kex->newkeys[mode] = newkey;
2547 newkey = NULL;
2548 r = 0;
2549 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002550 free(newkey);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002551 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002552 return r;
2553}
2554
2555/* restore kex from blob for packet state de-serialization */
2556static int
2557kex_from_blob(struct sshbuf *m, struct kex **kexp)
2558{
2559 struct kex *kex;
2560 int r;
2561
2562 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2563 (kex->my = sshbuf_new()) == NULL ||
2564 (kex->peer = sshbuf_new()) == NULL) {
2565 r = SSH_ERR_ALLOC_FAIL;
2566 goto out;
2567 }
2568 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2569 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2570 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2571 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2572 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2573 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2574 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2575 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2576 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2577 goto out;
2578 kex->server = 1;
2579 kex->done = 1;
2580 r = 0;
2581 out:
2582 if (r != 0 || kexp == NULL) {
2583 if (kex != NULL) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002584 sshbuf_free(kex->my);
2585 sshbuf_free(kex->peer);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002586 free(kex);
2587 }
2588 if (kexp != NULL)
2589 *kexp = NULL;
2590 } else {
2591 *kexp = kex;
2592 }
2593 return r;
2594}
2595
2596/*
2597 * Restore packet state from content of blob 'm' (de-serialization).
2598 * Note that 'm' will be partially consumed on parsing or any other errors.
2599 */
2600int
2601ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2602{
2603 struct session_state *state = ssh->state;
2604 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2605 size_t ssh1keylen, rlen, slen, ilen, olen;
2606 int r;
2607 u_int ssh1cipher = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002608
2609 if (!compat20) {
2610 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2611 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2612 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2613 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2614 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2615 return r;
2616 if (ssh1cipher > INT_MAX)
2617 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2618 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2619 (int)ssh1cipher);
2620 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2621 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2622 return SSH_ERR_INVALID_FORMAT;
2623 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2624 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2625 return r;
2626 } else {
2627 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2628 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2629 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002630 (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002631 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002632 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2633 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2634 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2635 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2636 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2637 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2638 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2639 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2640 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002641 /*
2642 * We set the time here so that in post-auth privsep slave we
2643 * count from the completion of the authentication.
2644 */
2645 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002646 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2647 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2648 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2649 return r;
2650 }
2651 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2652 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2653 return r;
2654 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2655 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2656 return SSH_ERR_INVALID_FORMAT;
2657 cipher_set_keycontext(&state->send_context, keyout);
2658 cipher_set_keycontext(&state->receive_context, keyin);
2659
2660 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2661 (r = ssh_packet_set_postauth(ssh)) != 0)
2662 return r;
2663
2664 sshbuf_reset(state->input);
2665 sshbuf_reset(state->output);
2666 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2667 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2668 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2669 (r = sshbuf_put(state->output, output, olen)) != 0)
2670 return r;
2671
markus@openbsd.org091c3022015-01-19 19:52:16 +00002672 if (sshbuf_len(m))
2673 return SSH_ERR_INVALID_FORMAT;
2674 debug3("%s: done", __func__);
2675 return 0;
2676}
2677
2678/* NEW API */
2679
2680/* put data to the outgoing packet */
2681
2682int
2683sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2684{
2685 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2686}
2687
2688int
2689sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2690{
2691 return sshbuf_putb(ssh->state->outgoing_packet, b);
2692}
2693
2694int
2695sshpkt_put_u8(struct ssh *ssh, u_char val)
2696{
2697 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2698}
2699
2700int
2701sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2702{
2703 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2704}
2705
2706int
2707sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2708{
2709 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2710}
2711
2712int
2713sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2714{
2715 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2716}
2717
2718int
2719sshpkt_put_cstring(struct ssh *ssh, const void *v)
2720{
2721 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2722}
2723
2724int
2725sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2726{
2727 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2728}
2729
djm@openbsd.org734226b2015-04-27 01:52:30 +00002730#ifdef WITH_OPENSSL
2731#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002732int
2733sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2734{
2735 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2736}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002737#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002738
Damien Miller773dda22015-01-30 23:10:17 +11002739#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002740int
2741sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2742{
2743 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2744}
Damien Miller773dda22015-01-30 23:10:17 +11002745#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002746
2747int
2748sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2749{
2750 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2751}
Damien Miller773dda22015-01-30 23:10:17 +11002752#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002753
2754/* fetch data from the incoming packet */
2755
2756int
2757sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2758{
2759 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2760}
2761
2762int
2763sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2764{
2765 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2766}
2767
2768int
2769sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2770{
2771 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2772}
2773
2774int
2775sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2776{
2777 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2778}
2779
2780int
2781sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2782{
2783 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2784}
2785
2786int
2787sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2788{
2789 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2790}
2791
2792int
2793sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2794{
2795 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2796}
2797
djm@openbsd.org734226b2015-04-27 01:52:30 +00002798#ifdef WITH_OPENSSL
2799#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002800int
2801sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2802{
2803 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2804}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002805#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002806
Damien Miller773dda22015-01-30 23:10:17 +11002807#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002808int
2809sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2810{
2811 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2812}
Damien Miller773dda22015-01-30 23:10:17 +11002813#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002814
2815int
2816sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2817{
2818 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2819}
Damien Miller773dda22015-01-30 23:10:17 +11002820#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002821
2822int
2823sshpkt_get_end(struct ssh *ssh)
2824{
2825 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2826 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2827 return 0;
2828}
2829
2830const u_char *
2831sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2832{
2833 if (lenp != NULL)
2834 *lenp = sshbuf_len(ssh->state->incoming_packet);
2835 return sshbuf_ptr(ssh->state->incoming_packet);
2836}
2837
2838/* start a new packet */
2839
2840int
2841sshpkt_start(struct ssh *ssh, u_char type)
2842{
2843 u_char buf[9];
2844 int len;
2845
2846 DBG(debug("packet_start[%d]", type));
2847 len = compat20 ? 6 : 9;
2848 memset(buf, 0, len - 1);
2849 buf[len - 1] = type;
2850 sshbuf_reset(ssh->state->outgoing_packet);
2851 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2852}
2853
2854/* send it */
2855
2856int
2857sshpkt_send(struct ssh *ssh)
2858{
2859 if (compat20)
2860 return ssh_packet_send2(ssh);
2861 else
2862 return ssh_packet_send1(ssh);
2863}
2864
2865int
2866sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2867{
2868 char buf[1024];
2869 va_list args;
2870 int r;
2871
2872 va_start(args, fmt);
2873 vsnprintf(buf, sizeof(buf), fmt, args);
2874 va_end(args);
2875
2876 if (compat20) {
2877 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2878 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2879 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2880 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2881 (r = sshpkt_send(ssh)) != 0)
2882 return r;
2883 } else {
2884 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2885 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2886 (r = sshpkt_send(ssh)) != 0)
2887 return r;
2888 }
2889 return 0;
2890}
2891
2892/* roundup current message to pad bytes */
2893int
2894sshpkt_add_padding(struct ssh *ssh, u_char pad)
2895{
2896 ssh->state->extra_pad = pad;
2897 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002898}