blob: e05bc0ad7c5410c3519061cc749123cc2b9a5d30 [file] [log] [blame]
djm@openbsd.org28136472016-01-29 05:46:01 +00001/* $OpenBSD: packet.c,v 1.226 2016/01/29 05:46:01 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",
957 state->p_read.bytes, state->p_read.blocks,
958 state->p_send.bytes, state->p_send.blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000959 if ((r = cipher_cleanup(cc)) != 0)
960 return r;
961 enc = &state->newkeys[mode]->enc;
962 mac = &state->newkeys[mode]->mac;
963 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000964 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100965 explicit_bzero(enc->iv, enc->iv_len);
966 explicit_bzero(enc->key, enc->key_len);
967 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000968 free(enc->name);
969 free(enc->iv);
970 free(enc->key);
971 free(mac->name);
972 free(mac->key);
973 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000974 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000975 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000976 /* move newkeys from kex to state */
977 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
978 return SSH_ERR_INTERNAL_ERROR;
979 ssh->kex->newkeys[mode] = NULL;
980 enc = &state->newkeys[mode]->enc;
981 mac = &state->newkeys[mode]->mac;
982 comp = &state->newkeys[mode]->comp;
983 if (cipher_authlen(enc->cipher) == 0) {
984 if ((r = mac_init(mac)) != 0)
985 return r;
986 }
987 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000988 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000989 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
990 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000991 return r;
992 if (!state->cipher_warning_done &&
993 (wmsg = cipher_warning_message(cc)) != NULL) {
994 error("Warning: %s", wmsg);
995 state->cipher_warning_done = 1;
996 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000997 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100998 /* explicit_bzero(enc->iv, enc->block_size);
999 explicit_bzero(enc->key, enc->key_len);
1000 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +10001001 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +10001002 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +00001003 state->after_authentication)) && comp->enabled == 0) {
1004 if ((r = ssh_packet_init_compression(ssh)) < 0)
1005 return r;
1006 if (mode == MODE_OUT) {
1007 if ((r = start_compression_out(ssh, 6)) != 0)
1008 return r;
1009 } else {
1010 if ((r = start_compression_in(ssh)) != 0)
1011 return r;
1012 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001013 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001014 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001015 /*
1016 * The 2^(blocksize*2) limit is too expensive for 3DES,
1017 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1018 */
1019 if (enc->block_size >= 16)
1020 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1021 else
1022 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001023 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001024 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001025 state->rekey_limit / enc->block_size);
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001026 debug("rekey after %llu blocks", *max_blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001027 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001028}
1029
Damien Miller5428f641999-11-25 11:54:57 +11001030/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001031 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001032 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001033 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1034 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001035static int
1036ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001037{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001038 struct session_state *state = ssh->state;
1039 struct sshcomp *comp = NULL;
1040 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001041
1042 /*
1043 * Remember that we are past the authentication step, so rekeying
1044 * with COMP_DELAYED will turn on compression immediately.
1045 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001046 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001047 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001048 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001049 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001050 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001051 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001052 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001053 if ((r = ssh_packet_init_compression(ssh)) != 0)
1054 return r;
1055 if (mode == MODE_OUT) {
1056 if ((r = start_compression_out(ssh, 6)) != 0)
1057 return r;
1058 } else {
1059 if ((r = start_compression_in(ssh)) != 0)
1060 return r;
1061 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001062 comp->enabled = 1;
1063 }
1064 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001065 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001066}
1067
djm@openbsd.org28136472016-01-29 05:46:01 +00001068/* Used to mute debug logging for noisy packet types */
1069static int
1070ssh_packet_log_type(u_char type)
1071{
1072 switch (type) {
1073 case SSH2_MSG_CHANNEL_DATA:
1074 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
1075 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
1076 return 0;
1077 default:
1078 return 1;
1079 }
1080}
1081
Damien Miller9786e6e2005-07-26 21:54:56 +10001082/*
Damien Miller33b13562000-04-04 14:38:59 +10001083 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1084 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001085int
1086ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001087{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001088 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001089 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001090 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001091 u_int authlen = 0, aadlen = 0;
1092 u_int len;
1093 struct sshenc *enc = NULL;
1094 struct sshmac *mac = NULL;
1095 struct sshcomp *comp = NULL;
1096 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001097
markus@openbsd.org091c3022015-01-19 19:52:16 +00001098 if (state->newkeys[MODE_OUT] != NULL) {
1099 enc = &state->newkeys[MODE_OUT]->enc;
1100 mac = &state->newkeys[MODE_OUT]->mac;
1101 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001102 /* disable mac for authenticated encryption */
1103 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1104 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001105 }
Damien Miller963f6b22002-02-19 15:21:23 +11001106 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001107 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001108
markus@openbsd.org091c3022015-01-19 19:52:16 +00001109 type = (sshbuf_ptr(state->outgoing_packet))[5];
djm@openbsd.org28136472016-01-29 05:46:01 +00001110 if (ssh_packet_log_type(type))
1111 debug3("send packet: type %u", type);
Damien Miller33b13562000-04-04 14:38:59 +10001112#ifdef PACKET_DEBUG
1113 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001114 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001115#endif
1116
1117 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001118 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001119 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001120 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1121 goto out;
1122 sshbuf_reset(state->compression_buffer);
1123 if ((r = compress_buffer(ssh, state->outgoing_packet,
1124 state->compression_buffer)) != 0)
1125 goto out;
1126 sshbuf_reset(state->outgoing_packet);
1127 if ((r = sshbuf_put(state->outgoing_packet,
1128 "\0\0\0\0\0", 5)) != 0 ||
1129 (r = sshbuf_putb(state->outgoing_packet,
1130 state->compression_buffer)) != 0)
1131 goto out;
1132 DBG(debug("compression: raw %d compressed %zd", len,
1133 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001134 }
1135
1136 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001137 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001138
1139 /*
1140 * calc size of padding, alloc space, get random data,
1141 * minimum padding is 4 bytes
1142 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001143 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001144 padlen = block_size - (len % block_size);
1145 if (padlen < 4)
1146 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001147 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001148 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001149 state->extra_pad =
1150 roundup(state->extra_pad, block_size);
1151 pad = state->extra_pad -
1152 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001153 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001154 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001155 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001156 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001157 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001158 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1159 goto out;
1160 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001161 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001162 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001163 } else {
1164 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001165 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001166 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001167 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001168 len = sshbuf_len(state->outgoing_packet);
1169 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1170 if (cp == NULL) {
1171 r = SSH_ERR_INTERNAL_ERROR;
1172 goto out;
1173 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001174 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001175 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001176 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001177 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1178 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001179
1180 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001181 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001182 if ((r = mac_compute(mac, state->p_send.seqnr,
1183 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001184 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001185 goto out;
1186 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001187 }
1188 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001189 if ((r = sshbuf_reserve(state->output,
1190 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1191 goto out;
1192 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1193 sshbuf_ptr(state->outgoing_packet),
1194 len - aadlen, aadlen, authlen)) != 0)
1195 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001196 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001197 if (mac && mac->enabled) {
1198 if (mac->etm) {
1199 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001200 if ((r = mac_compute(mac, state->p_send.seqnr,
1201 cp, len, macbuf, sizeof(macbuf))) != 0)
1202 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001203 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001204 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001205 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001206 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1207 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001208 }
Damien Miller33b13562000-04-04 14:38:59 +10001209#ifdef PACKET_DEBUG
1210 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001211 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001212#endif
Damien Miller4af51302000-04-16 11:18:38 +10001213 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001214 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001215 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001216 if (++state->p_send.packets == 0)
1217 if (!(ssh->compat & SSH_BUG_NOREKEY))
1218 return SSH_ERR_NEED_REKEY;
1219 state->p_send.blocks += len / block_size;
1220 state->p_send.bytes += len;
1221 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001222
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001223 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001224 r = ssh_set_newkeys(ssh, MODE_OUT);
1225 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1226 r = ssh_packet_enable_delayed_compress(ssh);
1227 else
1228 r = 0;
1229 out:
1230 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001231}
1232
markus@openbsd.org091c3022015-01-19 19:52:16 +00001233int
1234ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001235{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001236 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001237 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001238 u_char type;
1239 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001240
markus@openbsd.org091c3022015-01-19 19:52:16 +00001241 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001242
1243 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001244 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001245 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1246 (type > SSH2_MSG_TRANSPORT_MAX) ||
1247 (type == SSH2_MSG_SERVICE_REQUEST) ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001248 (type == SSH2_MSG_SERVICE_ACCEPT) ||
1249 (type == SSH2_MSG_EXT_INFO)) {
Damien Millera5539d22003-04-09 20:50:06 +10001250 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001251 p = calloc(1, sizeof(*p));
1252 if (p == NULL)
1253 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001254 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001255 p->payload = state->outgoing_packet;
1256 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1257 state->outgoing_packet = sshbuf_new();
1258 if (state->outgoing_packet == NULL)
1259 return SSH_ERR_ALLOC_FAIL;
1260 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001261 }
1262 }
1263
1264 /* rekeying starts with sending KEXINIT */
1265 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001266 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001267
markus@openbsd.org091c3022015-01-19 19:52:16 +00001268 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1269 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001270
1271 /* after a NEWKEYS message we can send the complete queue */
1272 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001273 state->rekeying = 0;
1274 state->rekey_time = monotime();
1275 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001276 type = p->type;
1277 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001278 sshbuf_free(state->outgoing_packet);
1279 state->outgoing_packet = p->payload;
1280 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001281 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001282 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1283 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001284 }
1285 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001286 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001287}
1288
Damien Miller33b13562000-04-04 14:38:59 +10001289/*
Damien Miller5428f641999-11-25 11:54:57 +11001290 * Waits until a packet has been received, and returns its type. Note that
1291 * no other data is processed until this returns, so this function should not
1292 * be used during the interactive session.
1293 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001294
1295int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001296ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001297{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001298 struct session_state *state = ssh->state;
markus@openbsd.orga3068632016-01-14 16:17:39 +00001299 int len, r, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001300 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001301 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001302 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001303
Darren Tucker99bb7612008-06-13 22:02:50 +10001304 DBG(debug("packet_read()"));
1305
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001306 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001307 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001308 if (setp == NULL)
1309 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001310
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001311 /*
1312 * Since we are blocking, ensure that all written packets have
1313 * been sent.
1314 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001315 if ((r = ssh_packet_write_wait(ssh)) != 0)
1316 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001317
Damien Miller95def091999-11-25 00:26:21 +11001318 /* Stay in the loop until we have received a complete packet. */
1319 for (;;) {
1320 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001321 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1322 if (r != 0)
1323 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001324 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001325 *typep == SSH_SMSG_SUCCESS
1326 || *typep == SSH_SMSG_FAILURE
1327 || *typep == SSH_CMSG_EOF
1328 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1329 if ((r = sshpkt_get_end(ssh)) != 0)
1330 break;
Damien Miller95def091999-11-25 00:26:21 +11001331 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001332 if (*typep != SSH_MSG_NONE)
1333 break;
Damien Miller5428f641999-11-25 11:54:57 +11001334 /*
1335 * Otherwise, wait for some data to arrive, add it to the
1336 * buffer, and try again.
1337 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001338 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001339 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001340 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001341
markus@openbsd.org091c3022015-01-19 19:52:16 +00001342 if (state->packet_timeout_ms > 0) {
1343 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001344 timeoutp = &timeout;
1345 }
Damien Miller95def091999-11-25 00:26:21 +11001346 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001347 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001348 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001349 ms_to_timeval(&timeout, ms_remain);
1350 gettimeofday(&start, NULL);
1351 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001352 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001353 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001354 break;
Damien Millerea437422009-10-02 11:49:03 +10001355 if (errno != EAGAIN && errno != EINTR &&
1356 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001357 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001358 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001359 continue;
1360 ms_subtract_diff(&start, &ms_remain);
1361 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001362 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001363 break;
1364 }
1365 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001366 if (r == 0)
1367 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001368 /* Read data from the socket. */
markus@openbsd.orga3068632016-01-14 16:17:39 +00001369 len = read(state->connection_in, buf, sizeof(buf));
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001370 if (len == 0) {
1371 r = SSH_ERR_CONN_CLOSED;
1372 goto out;
1373 }
1374 if (len < 0) {
1375 r = SSH_ERR_SYSTEM_ERROR;
1376 goto out;
1377 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001378
Damien Miller95def091999-11-25 00:26:21 +11001379 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001380 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001381 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001382 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001383 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001384 free(setp);
1385 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001386}
1387
Damien Miller278f9072001-12-21 15:00:19 +11001388int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001389ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001390{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001391 u_char type;
1392 int r;
1393
1394 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1395 fatal("%s: %s", __func__, ssh_err(r));
1396 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001397}
1398
Damien Miller5428f641999-11-25 11:54:57 +11001399/*
1400 * Waits until a packet has been received, verifies that its type matches
1401 * that given, and gives a fatal error and exits if there is a mismatch.
1402 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001404int
1405ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001407 int r;
1408 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001409
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001410 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1411 return r;
1412 if (type != expected_type) {
1413 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001414 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001415 expected_type, type)) != 0)
1416 return r;
1417 return SSH_ERR_PROTOCOL_ERROR;
1418 }
1419 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420}
1421
1422/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001423 * packet_process_incoming. If so, reads the packet; otherwise returns
1424 * SSH_MSG_NONE. This does not wait for data from the connection.
1425 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001426 * SSH_MSG_DISCONNECT is handled specially here. Also,
1427 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1428 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001429 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430
markus@openbsd.org091c3022015-01-19 19:52:16 +00001431int
1432ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001434 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001435 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001436 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001437 const u_char *cp;
1438 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001439 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001440 int r;
1441
1442 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443
Damien Miller95def091999-11-25 00:26:21 +11001444 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001445 if (sshbuf_len(state->input) < 4 + 8)
1446 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001447 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001448 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001449 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1450 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1451 len)) != 0)
1452 return r;
1453 return SSH_ERR_CONN_CORRUPT;
1454 }
Damien Miller95def091999-11-25 00:26:21 +11001455 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456
Damien Miller95def091999-11-25 00:26:21 +11001457 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001458 if (sshbuf_len(state->input) < 4 + padded_len)
1459 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001460
Damien Miller95def091999-11-25 00:26:21 +11001461 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462
Damien Miller95def091999-11-25 00:26:21 +11001463 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001464 if ((r = sshbuf_consume(state->input, 4)) != 0)
1465 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001466
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001467 /*
1468 * Cryptographic attack detector for ssh
1469 * (C)1998 CORE-SDI, Buenos Aires Argentina
1470 * Ariel Futoransky(futo@core-sdi.com)
1471 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001472 if (!state->receive_context.plaintext) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001473 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001474 switch (detect_attack(&state->deattack,
1475 sshbuf_ptr(state->input), padded_len)) {
1476 case DEATTACK_OK:
1477 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001478 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001479 emsg = "crc32 compensation attack detected";
1480 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001481 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001482 emsg = "deattack denial of service detected";
1483 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001484 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001485 emsg = "deattack error";
1486 break;
1487 }
1488 if (emsg != NULL) {
1489 error("%s", emsg);
1490 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1491 (r = ssh_packet_write_wait(ssh)) != 0)
1492 return r;
1493 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001494 }
1495 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001496
1497 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001498 sshbuf_reset(state->incoming_packet);
1499 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1500 goto out;
1501 if ((r = cipher_crypt(&state->receive_context, 0, p,
1502 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1503 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001504
markus@openbsd.org091c3022015-01-19 19:52:16 +00001505 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1506 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001507
1508#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001509 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001510 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001511#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001512
Damien Miller95def091999-11-25 00:26:21 +11001513 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001514 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1515 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516
Damien Miller95def091999-11-25 00:26:21 +11001517 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001518 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1519 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001520
Damien Miller95def091999-11-25 00:26:21 +11001521 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001522 if (len != sshbuf_len(state->incoming_packet)) {
1523 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001524 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001525 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1526 (r = ssh_packet_write_wait(ssh)) != 0)
1527 return r;
1528 return SSH_ERR_CONN_CORRUPT;
1529 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530
markus@openbsd.org091c3022015-01-19 19:52:16 +00001531 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1532 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001533 if (checksum != stored_checksum) {
1534 error("Corrupted check bytes on input");
1535 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1536 (r = ssh_packet_write_wait(ssh)) != 0)
1537 return r;
1538 return SSH_ERR_CONN_CORRUPT;
1539 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001540 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1541 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001542
markus@openbsd.org091c3022015-01-19 19:52:16 +00001543 if (state->packet_compression) {
1544 sshbuf_reset(state->compression_buffer);
1545 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1546 state->compression_buffer)) != 0)
1547 goto out;
1548 sshbuf_reset(state->incoming_packet);
1549 if ((r = sshbuf_putb(state->incoming_packet,
1550 state->compression_buffer)) != 0)
1551 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001552 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001553 state->p_read.packets++;
1554 state->p_read.bytes += padded_len + 4;
1555 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1556 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001557 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1558 error("Invalid ssh1 packet type: %d", *typep);
1559 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1560 (r = ssh_packet_write_wait(ssh)) != 0)
1561 return r;
1562 return SSH_ERR_PROTOCOL_ERROR;
1563 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001564 r = 0;
1565 out:
1566 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567}
Damien Miller95def091999-11-25 00:26:21 +11001568
markus@openbsd.org091c3022015-01-19 19:52:16 +00001569int
1570ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001571{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001572 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001573 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001574 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1575 u_int maclen, aadlen = 0, authlen = 0, block_size;
1576 struct sshenc *enc = NULL;
1577 struct sshmac *mac = NULL;
1578 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001579 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001580
markus@openbsd.org091c3022015-01-19 19:52:16 +00001581 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001582
markus@openbsd.org091c3022015-01-19 19:52:16 +00001583 if (state->packet_discard)
1584 return 0;
1585
1586 if (state->newkeys[MODE_IN] != NULL) {
1587 enc = &state->newkeys[MODE_IN]->enc;
1588 mac = &state->newkeys[MODE_IN]->mac;
1589 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001590 /* disable mac for authenticated encryption */
1591 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1592 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001593 }
1594 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001595 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001596 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001597
markus@openbsd.org091c3022015-01-19 19:52:16 +00001598 if (aadlen && state->packlen == 0) {
1599 if (cipher_get_length(&state->receive_context,
1600 &state->packlen, state->p_read.seqnr,
1601 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1602 return 0;
1603 if (state->packlen < 1 + 4 ||
1604 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001605#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001606 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001607#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001608 logit("Bad packet length %u.", state->packlen);
1609 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1610 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001611 return SSH_ERR_CONN_CORRUPT;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001612 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001613 sshbuf_reset(state->incoming_packet);
1614 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001615 /*
1616 * check if input size is less than the cipher block size,
1617 * decrypt first block and extract length of incoming packet
1618 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001619 if (sshbuf_len(state->input) < block_size)
1620 return 0;
1621 sshbuf_reset(state->incoming_packet);
1622 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1623 &cp)) != 0)
1624 goto out;
1625 if ((r = cipher_crypt(&state->receive_context,
1626 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1627 block_size, 0, 0)) != 0)
1628 goto out;
1629 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1630 if (state->packlen < 1 + 4 ||
1631 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001632#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001633 fprintf(stderr, "input: \n");
1634 sshbuf_dump(state->input, stderr);
1635 fprintf(stderr, "incoming_packet: \n");
1636 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001637#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001638 logit("Bad packet length %u.", state->packlen);
1639 return ssh_packet_start_discard(ssh, enc, mac,
1640 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001641 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001642 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1643 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001644 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001645 DBG(debug("input: packet len %u", state->packlen+4));
1646
Damien Milleraf43a7a2012-12-12 10:46:31 +11001647 if (aadlen) {
1648 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001649 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001650 } else {
1651 /*
1652 * the payload size and the payload are encrypted, but we
1653 * have a partial packet of block_size bytes
1654 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001655 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001656 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001657 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1658 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001659 if (need % block_size != 0) {
1660 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001661 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001662 return ssh_packet_start_discard(ssh, enc, mac,
1663 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001664 }
Damien Miller33b13562000-04-04 14:38:59 +10001665 /*
1666 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001667 * decrypt into incoming_packet:
1668 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001669 * 'need' bytes are encrypted, followed by either
1670 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001671 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001672 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001673 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1674 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001675#ifdef PACKET_DEBUG
1676 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001677 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001678#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001679 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001680 if (mac && mac->enabled && mac->etm) {
1681 if ((r = mac_compute(mac, state->p_read.seqnr,
1682 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001683 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001684 goto out;
1685 }
1686 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1687 &cp)) != 0)
1688 goto out;
1689 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1690 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1691 goto out;
1692 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1693 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001694 /*
1695 * compute MAC over seqnr and packet,
1696 * increment sequence number for incoming packet
1697 */
Damien Miller4af51302000-04-16 11:18:38 +10001698 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001699 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001700 if ((r = mac_compute(mac, state->p_read.seqnr,
1701 sshbuf_ptr(state->incoming_packet),
1702 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001703 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001704 goto out;
1705 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001706 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001707 logit("Corrupted MAC on input.");
1708 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001709 return SSH_ERR_INTERNAL_ERROR;
1710 return ssh_packet_start_discard(ssh, enc, mac,
1711 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001712 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001713
1714 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1715 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1716 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001717 }
Damien Miller278f9072001-12-21 15:00:19 +11001718 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001719 *seqnr_p = state->p_read.seqnr;
1720 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001721 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001722 if (++state->p_read.packets == 0)
1723 if (!(ssh->compat & SSH_BUG_NOREKEY))
1724 return SSH_ERR_NEED_REKEY;
1725 state->p_read.blocks += (state->packlen + 4) / block_size;
1726 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001727
1728 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001729 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001730 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001731 if (padlen < 4) {
1732 if ((r = sshpkt_disconnect(ssh,
1733 "Corrupted padlen %d on input.", padlen)) != 0 ||
1734 (r = ssh_packet_write_wait(ssh)) != 0)
1735 return r;
1736 return SSH_ERR_CONN_CORRUPT;
1737 }
Damien Miller33b13562000-04-04 14:38:59 +10001738
1739 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001740 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1741 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1742 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001743
markus@openbsd.org091c3022015-01-19 19:52:16 +00001744 DBG(debug("input: len before de-compress %zd",
1745 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001746 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001747 sshbuf_reset(state->compression_buffer);
1748 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1749 state->compression_buffer)) != 0)
1750 goto out;
1751 sshbuf_reset(state->incoming_packet);
1752 if ((r = sshbuf_putb(state->incoming_packet,
1753 state->compression_buffer)) != 0)
1754 goto out;
1755 DBG(debug("input: len after de-compress %zd",
1756 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001757 }
1758 /*
1759 * get packet type, implies consume.
1760 * return length of payload (without type field)
1761 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001762 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1763 goto out;
djm@openbsd.org28136472016-01-29 05:46:01 +00001764 if (ssh_packet_log_type(*typep))
1765 debug3("receive packet: type %u", *typep);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001766 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1767 if ((r = sshpkt_disconnect(ssh,
1768 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1769 (r = ssh_packet_write_wait(ssh)) != 0)
1770 return r;
1771 return SSH_ERR_PROTOCOL_ERROR;
1772 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001773 if (*typep == SSH2_MSG_NEWKEYS)
1774 r = ssh_set_newkeys(ssh, MODE_IN);
1775 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1776 r = ssh_packet_enable_delayed_compress(ssh);
1777 else
1778 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001779#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001780 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1781 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001782#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001783 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001784 state->packlen = 0;
1785 out:
1786 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001787}
1788
1789int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001790ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001791{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001792 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001793 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001794 int r;
1795 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001796
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001797 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001798 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001799 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001800 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1801 if (r != 0)
1802 return r;
1803 if (*typep) {
1804 state->keep_alive_timeouts = 0;
1805 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001806 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001807 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001808 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001809 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001810 break;
Damien Miller33b13562000-04-04 14:38:59 +10001811 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001812 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1813 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1814 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001815 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001816 return r;
1817 }
Damien Miller33b13562000-04-04 14:38:59 +10001818 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001819 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001820 break;
1821 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001822 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1823 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1824 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001825 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001826 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001827 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1828 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001829 "Received disconnect from %s port %d:"
1830 "%u: %.400s", ssh_remote_ipaddr(ssh),
1831 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001832 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001833 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001834 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001835 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1836 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001837 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1838 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001839 break;
Damien Miller33b13562000-04-04 14:38:59 +10001840 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001841 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001842 }
Damien Miller33b13562000-04-04 14:38:59 +10001843 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001844 r = ssh_packet_read_poll1(ssh, typep);
1845 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001846 case SSH_MSG_NONE:
1847 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001848 case SSH_MSG_IGNORE:
1849 break;
1850 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001851 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1852 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001853 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001854 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001855 break;
1856 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001857 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1858 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001859 error("Received disconnect from %s port %d: "
1860 "%.400s", ssh_remote_ipaddr(ssh),
1861 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001862 free(msg);
1863 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001864 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001865 DBG(debug("received packet type %d", *typep));
1866 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001867 }
Damien Miller33b13562000-04-04 14:38:59 +10001868 }
1869 }
1870}
1871
Damien Miller5428f641999-11-25 11:54:57 +11001872/*
1873 * Buffers the given amount of input characters. This is intended to be used
1874 * together with packet_read_poll.
1875 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001876
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001877int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001878ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001879{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001880 struct session_state *state = ssh->state;
1881 int r;
1882
1883 if (state->packet_discard) {
1884 state->keep_alive_timeouts = 0; /* ?? */
1885 if (len >= state->packet_discard) {
1886 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001887 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001888 }
1889 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001890 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001891 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001892 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001893 return r;
1894
1895 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001896}
1897
Damien Miller4af51302000-04-16 11:18:38 +10001898int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001899ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001900{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001901 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001902}
1903
Damien Miller5428f641999-11-25 11:54:57 +11001904/*
1905 * Sends a diagnostic message from the server to the client. This message
1906 * can be sent at any time (but not while constructing another message). The
1907 * message is printed immediately, but only if the client is being executed
1908 * in verbose mode. These messages are primarily intended to ease debugging
1909 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001910 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11001911 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001912void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001913ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001914{
Damien Miller95def091999-11-25 00:26:21 +11001915 char buf[1024];
1916 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001917 int r;
Damien Miller95def091999-11-25 00:26:21 +11001918
markus@openbsd.org091c3022015-01-19 19:52:16 +00001919 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001920 return;
1921
Damien Miller95def091999-11-25 00:26:21 +11001922 va_start(args, fmt);
1923 vsnprintf(buf, sizeof(buf), fmt, args);
1924 va_end(args);
1925
Damien Miller7c8af4f2000-05-01 08:24:07 +10001926 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001927 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1928 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1929 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1930 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1931 (r = sshpkt_send(ssh)) != 0)
1932 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001933 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001934 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1935 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1936 (r = sshpkt_send(ssh)) != 0)
1937 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001938 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001939 if ((r = ssh_packet_write_wait(ssh)) != 0)
1940 fatal("%s: %s", __func__, ssh_err(r));
1941}
1942
1943/*
1944 * Pretty-print connection-terminating errors and exit.
1945 */
1946void
1947sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
1948{
1949 switch (r) {
1950 case SSH_ERR_CONN_CLOSED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001951 logit("Connection closed by %.200s port %d",
1952 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001953 cleanup_exit(255);
1954 case SSH_ERR_CONN_TIMEOUT:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001955 logit("Connection %s %.200s port %d timed out",
1956 ssh->state->server_side ? "from" : "to",
1957 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001958 cleanup_exit(255);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001959 case SSH_ERR_DISCONNECTED:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001960 logit("Disconnected from %.200s port %d",
1961 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001962 cleanup_exit(255);
1963 case SSH_ERR_SYSTEM_ERROR:
1964 if (errno == ECONNRESET) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001965 logit("Connection reset by %.200s port %d",
1966 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00001967 cleanup_exit(255);
1968 }
1969 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001970 case SSH_ERR_NO_CIPHER_ALG_MATCH:
1971 case SSH_ERR_NO_MAC_ALG_MATCH:
1972 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
1973 case SSH_ERR_NO_KEX_ALG_MATCH:
1974 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
1975 if (ssh && ssh->kex && ssh->kex->failed_choice) {
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001976 fatal("Unable to negotiate with %.200s port %d: %s. "
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001977 "Their offer: %s", ssh_remote_ipaddr(ssh),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001978 ssh_remote_port(ssh), ssh_err(r),
1979 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00001980 }
1981 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001982 default:
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001983 fatal("%s%sConnection %s %.200s port %d: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001984 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001985 ssh->state->server_side ? "from" : "to",
1986 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001987 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001988}
1989
Damien Miller5428f641999-11-25 11:54:57 +11001990/*
1991 * Logs the error plus constructs and sends a disconnect packet, closes the
1992 * connection, and exits. This function never returns. The error message
1993 * should not contain a newline. The length of the formatted message must
1994 * not exceed 1024 bytes.
1995 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001996void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001997ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001998{
Damien Miller95def091999-11-25 00:26:21 +11001999 char buf[1024];
2000 va_list args;
2001 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002002 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002003
Damien Miller95def091999-11-25 00:26:21 +11002004 if (disconnecting) /* Guard against recursive invocations. */
2005 fatal("packet_disconnect called recursively.");
2006 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002007
Damien Miller5428f641999-11-25 11:54:57 +11002008 /*
2009 * Format the message. Note that the caller must make sure the
2010 * message is of limited size.
2011 */
Damien Miller95def091999-11-25 00:26:21 +11002012 va_start(args, fmt);
2013 vsnprintf(buf, sizeof(buf), fmt, args);
2014 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002015
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002016 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10002017 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002018
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002019 /*
2020 * Send the disconnect message to the other side, and wait
2021 * for it to get sent.
2022 */
2023 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2024 sshpkt_fatal(ssh, __func__, r);
2025
2026 if ((r = ssh_packet_write_wait(ssh)) != 0)
2027 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002028
Damien Miller95def091999-11-25 00:26:21 +11002029 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002030 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002031 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002032}
2033
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002034/*
2035 * Checks if there is any buffered output, and tries to write some of
2036 * the output.
2037 */
2038int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002039ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002040{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002041 struct session_state *state = ssh->state;
2042 int len = sshbuf_len(state->output);
markus@openbsd.orga3068632016-01-14 16:17:39 +00002043 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002044
Damien Miller95def091999-11-25 00:26:21 +11002045 if (len > 0) {
markus@openbsd.orga3068632016-01-14 16:17:39 +00002046 len = write(state->connection_out,
2047 sshbuf_ptr(state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10002048 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002049 if (errno == EINTR || errno == EAGAIN ||
2050 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002051 return 0;
2052 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002053 }
markus@openbsd.orga3068632016-01-14 16:17:39 +00002054 if (len == 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002055 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002056 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002057 return r;
Damien Miller95def091999-11-25 00:26:21 +11002058 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002059 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002060}
2061
Damien Miller5428f641999-11-25 11:54:57 +11002062/*
2063 * Calls packet_write_poll repeatedly until all pending output data has been
2064 * written.
2065 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002066int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002067ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002068{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002069 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002070 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002071 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002072 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002073
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002074 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002075 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002076 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002077 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002078 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2079 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002080 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002081 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002082 while (ssh_packet_have_data_to_write(ssh)) {
2083 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002084 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002085 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002086
markus@openbsd.org091c3022015-01-19 19:52:16 +00002087 if (state->packet_timeout_ms > 0) {
2088 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002089 timeoutp = &timeout;
2090 }
2091 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002092 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002093 ms_to_timeval(&timeout, ms_remain);
2094 gettimeofday(&start, NULL);
2095 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002096 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002097 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002098 break;
Damien Millerea437422009-10-02 11:49:03 +10002099 if (errno != EAGAIN && errno != EINTR &&
2100 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002101 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002102 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002103 continue;
2104 ms_subtract_diff(&start, &ms_remain);
2105 if (ms_remain <= 0) {
2106 ret = 0;
2107 break;
2108 }
2109 }
2110 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002111 free(setp);
2112 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002113 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002114 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2115 free(setp);
2116 return r;
2117 }
Damien Miller95def091999-11-25 00:26:21 +11002118 }
Darren Tuckera627d422013-06-02 07:31:17 +10002119 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002120 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002121}
2122
2123/* Returns true if there is buffered data to write to the connection. */
2124
2125int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002126ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002127{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002128 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002129}
2130
2131/* Returns true if there is not too much data to write to the connection. */
2132
2133int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002134ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002135{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002136 if (ssh->state->interactive_mode)
2137 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002138 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002139 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002140}
2141
markus@openbsd.org091c3022015-01-19 19:52:16 +00002142void
2143ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002144{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002145#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002146 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002147 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002148 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002149# ifdef IP_TOS
2150 case AF_INET:
2151 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002152 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002153 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2154 error("setsockopt IP_TOS %d: %.100s:",
2155 tos, strerror(errno));
2156 break;
2157# endif /* IP_TOS */
2158# ifdef IPV6_TCLASS
2159 case AF_INET6:
2160 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002161 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002162 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2163 error("setsockopt IPV6_TCLASS %d: %.100s:",
2164 tos, strerror(errno));
2165 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002166# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002167 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002168#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002169}
Ben Lindstroma7433982002-12-23 02:41:41 +00002170
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002171/* Informs that the current session is interactive. Sets IP flags for that. */
2172
2173void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002174ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002175{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002176 struct session_state *state = ssh->state;
2177
2178 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002179 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002180 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002181
Damien Miller95def091999-11-25 00:26:21 +11002182 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002183 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002184
Damien Miller34132e52000-01-14 15:45:46 +11002185 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002186 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002187 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002188 set_nodelay(state->connection_in);
2189 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2190 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002191}
2192
2193/* Returns true if the current connection is interactive. */
2194
2195int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002196ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002197{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002198 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002199}
Damien Miller6162d121999-11-21 13:23:52 +11002200
Darren Tucker1f8311c2004-05-13 16:39:33 +10002201int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002202ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002203{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002204 struct session_state *state = ssh->state;
2205
2206 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002207 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002208 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002209 return -1;
2210 }
2211 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002212 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002213 return -1;
2214 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002215 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002216 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002217 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002218 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002219}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002220
Darren Tuckerf7288d72009-06-21 18:12:20 +10002221int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002222ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002223{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002224 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002225}
2226
2227void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002228ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002229{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002230 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002231}
2232
2233u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002234ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002235{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002236 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002237}
2238
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002239/*
2240 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002241 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002242 * byte SSH_MSG_IGNORE
2243 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002244 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002245 * All implementations MUST understand (and ignore) this message at any
2246 * time (after receiving the protocol version). No implementation is
2247 * required to send them. This message can be used as an additional
2248 * protection measure against advanced traffic analysis techniques.
2249 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002250void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002251ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002252{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002253 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002254 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002255
markus@openbsd.org091c3022015-01-19 19:52:16 +00002256 if ((r = sshpkt_start(ssh, compat20 ?
2257 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2258 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2259 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002260 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002261 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002262 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002263 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2264 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002265 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002266 }
2267}
Damien Millera5539d22003-04-09 20:50:06 +10002268
Darren Tucker1f8311c2004-05-13 16:39:33 +10002269#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002270int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002271ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002272{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002273 struct session_state *state = ssh->state;
2274
2275 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002276 return 0;
2277 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002278 (state->p_send.packets > MAX_PACKETS) ||
2279 (state->p_read.packets > MAX_PACKETS) ||
2280 (state->max_blocks_out &&
dtucker@openbsd.org6fd6e282016-01-29 03:31:03 +00002281 (state->p_send.blocks > state->max_blocks_out)) ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002282 (state->max_blocks_in &&
dtucker@openbsd.org6fd6e282016-01-29 03:31:03 +00002283 (state->p_read.blocks > state->max_blocks_in)) ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002284 (state->rekey_interval != 0 && state->rekey_time +
2285 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002286}
2287
2288void
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002289ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002290{
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002291 debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes,
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002292 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002293 ssh->state->rekey_limit = bytes;
2294 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002295}
2296
2297time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002298ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002299{
2300 time_t seconds;
2301
markus@openbsd.org091c3022015-01-19 19:52:16 +00002302 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002303 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002304 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002305}
Damien Miller9786e6e2005-07-26 21:54:56 +10002306
2307void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002308ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002309{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002310 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002311}
2312
2313void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002314ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002315{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002316 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002317}
2318
2319void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002320ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002321{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002322 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002323}
2324
2325void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002326ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002327{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002328 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002329}
2330
Damien Millerc31a0cd2014-05-15 14:37:39 +10002331/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002332static int
2333ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002334{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002335 struct sshcomp *comp;
2336 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002337
2338 debug("%s: called", __func__);
2339 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002340 ssh->state->after_authentication = 1;
2341 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002342 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002343 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002344 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002345 comp = &ssh->state->newkeys[mode]->comp;
2346 if (comp && comp->enabled &&
2347 (r = ssh_packet_init_compression(ssh)) != 0)
2348 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002349 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002350 return 0;
2351}
2352
2353/* Packet state (de-)serialization for privsep */
2354
2355/* turn kex into a blob for packet state serialization */
2356static int
2357kex_to_blob(struct sshbuf *m, struct kex *kex)
2358{
2359 int r;
2360
2361 if ((r = sshbuf_put_string(m, kex->session_id,
2362 kex->session_id_len)) != 0 ||
2363 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2364 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2365 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2366 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2367 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2368 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2369 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2370 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2371 return r;
2372 return 0;
2373}
2374
2375/* turn key exchange results into a blob for packet state serialization */
2376static int
2377newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2378{
2379 struct sshbuf *b;
2380 struct sshcipher_ctx *cc;
2381 struct sshcomp *comp;
2382 struct sshenc *enc;
2383 struct sshmac *mac;
2384 struct newkeys *newkey;
2385 int r;
2386
2387 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2388 return SSH_ERR_INTERNAL_ERROR;
2389 enc = &newkey->enc;
2390 mac = &newkey->mac;
2391 comp = &newkey->comp;
2392 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2393 &ssh->state->receive_context;
2394 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2395 return r;
2396 if ((b = sshbuf_new()) == NULL)
2397 return SSH_ERR_ALLOC_FAIL;
2398 /* The cipher struct is constant and shared, you export pointer */
2399 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2400 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2401 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2402 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2403 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2404 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2405 goto out;
2406 if (cipher_authlen(enc->cipher) == 0) {
2407 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2408 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2409 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2410 goto out;
2411 }
2412 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2413 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2414 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2415 goto out;
2416 r = sshbuf_put_stringb(m, b);
2417 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002418 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002419 return r;
2420}
2421
2422/* serialize packet state into a blob */
2423int
2424ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2425{
2426 struct session_state *state = ssh->state;
2427 u_char *p;
2428 size_t slen, rlen;
2429 int r, ssh1cipher;
2430
2431 if (!compat20) {
2432 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2433 slen = cipher_get_keyiv_len(&state->send_context);
2434 rlen = cipher_get_keyiv_len(&state->receive_context);
2435 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2436 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2437 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2438 (r = sshbuf_put_u32(m, slen)) != 0 ||
2439 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2440 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2441 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2442 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2443 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2444 return r;
2445 } else {
2446 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2447 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2448 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002449 (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002450 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002451 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2452 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2453 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2454 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2455 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2456 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2457 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2458 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2459 return r;
2460 }
2461
2462 slen = cipher_get_keycontext(&state->send_context, NULL);
2463 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2464 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2465 (r = sshbuf_reserve(m, slen, &p)) != 0)
2466 return r;
2467 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2468 return SSH_ERR_INTERNAL_ERROR;
2469 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2470 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2471 return r;
2472 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2473 return SSH_ERR_INTERNAL_ERROR;
2474
2475 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2476 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2477 (r = sshbuf_put_stringb(m, state->output)) != 0)
2478 return r;
2479
markus@openbsd.org091c3022015-01-19 19:52:16 +00002480 return 0;
2481}
2482
2483/* restore key exchange results from blob for packet state de-serialization */
2484static int
2485newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2486{
2487 struct sshbuf *b = NULL;
2488 struct sshcomp *comp;
2489 struct sshenc *enc;
2490 struct sshmac *mac;
2491 struct newkeys *newkey = NULL;
2492 size_t keylen, ivlen, maclen;
2493 int r;
2494
2495 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2496 r = SSH_ERR_ALLOC_FAIL;
2497 goto out;
2498 }
2499 if ((r = sshbuf_froms(m, &b)) != 0)
2500 goto out;
2501#ifdef DEBUG_PK
2502 sshbuf_dump(b, stderr);
2503#endif
2504 enc = &newkey->enc;
2505 mac = &newkey->mac;
2506 comp = &newkey->comp;
2507
2508 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2509 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2510 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2511 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2512 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2513 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2514 goto out;
2515 if (cipher_authlen(enc->cipher) == 0) {
2516 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2517 goto out;
2518 if ((r = mac_setup(mac, mac->name)) != 0)
2519 goto out;
2520 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2521 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2522 goto out;
2523 if (maclen > mac->key_len) {
2524 r = SSH_ERR_INVALID_FORMAT;
2525 goto out;
2526 }
2527 mac->key_len = maclen;
2528 }
2529 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2530 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2531 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2532 goto out;
2533 if (enc->name == NULL ||
2534 cipher_by_name(enc->name) != enc->cipher) {
2535 r = SSH_ERR_INVALID_FORMAT;
2536 goto out;
2537 }
2538 if (sshbuf_len(b) != 0) {
2539 r = SSH_ERR_INVALID_FORMAT;
2540 goto out;
2541 }
2542 enc->key_len = keylen;
2543 enc->iv_len = ivlen;
2544 ssh->kex->newkeys[mode] = newkey;
2545 newkey = NULL;
2546 r = 0;
2547 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002548 free(newkey);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002549 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002550 return r;
2551}
2552
2553/* restore kex from blob for packet state de-serialization */
2554static int
2555kex_from_blob(struct sshbuf *m, struct kex **kexp)
2556{
2557 struct kex *kex;
2558 int r;
2559
2560 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2561 (kex->my = sshbuf_new()) == NULL ||
2562 (kex->peer = sshbuf_new()) == NULL) {
2563 r = SSH_ERR_ALLOC_FAIL;
2564 goto out;
2565 }
2566 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2567 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2568 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2569 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2570 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2571 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2572 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2573 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2574 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2575 goto out;
2576 kex->server = 1;
2577 kex->done = 1;
2578 r = 0;
2579 out:
2580 if (r != 0 || kexp == NULL) {
2581 if (kex != NULL) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002582 sshbuf_free(kex->my);
2583 sshbuf_free(kex->peer);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002584 free(kex);
2585 }
2586 if (kexp != NULL)
2587 *kexp = NULL;
2588 } else {
2589 *kexp = kex;
2590 }
2591 return r;
2592}
2593
2594/*
2595 * Restore packet state from content of blob 'm' (de-serialization).
2596 * Note that 'm' will be partially consumed on parsing or any other errors.
2597 */
2598int
2599ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2600{
2601 struct session_state *state = ssh->state;
2602 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2603 size_t ssh1keylen, rlen, slen, ilen, olen;
2604 int r;
2605 u_int ssh1cipher = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002606
2607 if (!compat20) {
2608 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2609 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2610 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2611 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2612 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2613 return r;
2614 if (ssh1cipher > INT_MAX)
2615 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2616 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2617 (int)ssh1cipher);
2618 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2619 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2620 return SSH_ERR_INVALID_FORMAT;
2621 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2622 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2623 return r;
2624 } else {
2625 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2626 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2627 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002628 (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002629 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002630 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2631 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2632 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2633 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2634 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2635 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2636 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2637 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2638 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002639 /*
2640 * We set the time here so that in post-auth privsep slave we
2641 * count from the completion of the authentication.
2642 */
2643 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002644 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2645 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2646 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2647 return r;
2648 }
2649 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2650 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2651 return r;
2652 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2653 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2654 return SSH_ERR_INVALID_FORMAT;
2655 cipher_set_keycontext(&state->send_context, keyout);
2656 cipher_set_keycontext(&state->receive_context, keyin);
2657
2658 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2659 (r = ssh_packet_set_postauth(ssh)) != 0)
2660 return r;
2661
2662 sshbuf_reset(state->input);
2663 sshbuf_reset(state->output);
2664 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2665 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2666 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2667 (r = sshbuf_put(state->output, output, olen)) != 0)
2668 return r;
2669
markus@openbsd.org091c3022015-01-19 19:52:16 +00002670 if (sshbuf_len(m))
2671 return SSH_ERR_INVALID_FORMAT;
2672 debug3("%s: done", __func__);
2673 return 0;
2674}
2675
2676/* NEW API */
2677
2678/* put data to the outgoing packet */
2679
2680int
2681sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2682{
2683 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2684}
2685
2686int
2687sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2688{
2689 return sshbuf_putb(ssh->state->outgoing_packet, b);
2690}
2691
2692int
2693sshpkt_put_u8(struct ssh *ssh, u_char val)
2694{
2695 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2696}
2697
2698int
2699sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2700{
2701 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2702}
2703
2704int
2705sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2706{
2707 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2708}
2709
2710int
2711sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2712{
2713 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2714}
2715
2716int
2717sshpkt_put_cstring(struct ssh *ssh, const void *v)
2718{
2719 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2720}
2721
2722int
2723sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2724{
2725 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2726}
2727
djm@openbsd.org734226b2015-04-27 01:52:30 +00002728#ifdef WITH_OPENSSL
2729#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002730int
2731sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2732{
2733 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2734}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002735#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002736
Damien Miller773dda22015-01-30 23:10:17 +11002737#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002738int
2739sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2740{
2741 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2742}
Damien Miller773dda22015-01-30 23:10:17 +11002743#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002744
2745int
2746sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2747{
2748 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2749}
Damien Miller773dda22015-01-30 23:10:17 +11002750#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002751
2752/* fetch data from the incoming packet */
2753
2754int
2755sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2756{
2757 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2758}
2759
2760int
2761sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2762{
2763 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2764}
2765
2766int
2767sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2768{
2769 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2770}
2771
2772int
2773sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2774{
2775 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2776}
2777
2778int
2779sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2780{
2781 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2782}
2783
2784int
2785sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2786{
2787 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2788}
2789
2790int
2791sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2792{
2793 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2794}
2795
djm@openbsd.org734226b2015-04-27 01:52:30 +00002796#ifdef WITH_OPENSSL
2797#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002798int
2799sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2800{
2801 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2802}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002803#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002804
Damien Miller773dda22015-01-30 23:10:17 +11002805#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002806int
2807sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2808{
2809 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2810}
Damien Miller773dda22015-01-30 23:10:17 +11002811#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002812
2813int
2814sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2815{
2816 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2817}
Damien Miller773dda22015-01-30 23:10:17 +11002818#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002819
2820int
2821sshpkt_get_end(struct ssh *ssh)
2822{
2823 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2824 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2825 return 0;
2826}
2827
2828const u_char *
2829sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2830{
2831 if (lenp != NULL)
2832 *lenp = sshbuf_len(ssh->state->incoming_packet);
2833 return sshbuf_ptr(ssh->state->incoming_packet);
2834}
2835
2836/* start a new packet */
2837
2838int
2839sshpkt_start(struct ssh *ssh, u_char type)
2840{
2841 u_char buf[9];
2842 int len;
2843
2844 DBG(debug("packet_start[%d]", type));
2845 len = compat20 ? 6 : 9;
2846 memset(buf, 0, len - 1);
2847 buf[len - 1] = type;
2848 sshbuf_reset(ssh->state->outgoing_packet);
2849 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2850}
2851
2852/* send it */
2853
2854int
2855sshpkt_send(struct ssh *ssh)
2856{
2857 if (compat20)
2858 return ssh_packet_send2(ssh);
2859 else
2860 return ssh_packet_send1(ssh);
2861}
2862
2863int
2864sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2865{
2866 char buf[1024];
2867 va_list args;
2868 int r;
2869
2870 va_start(args, fmt);
2871 vsnprintf(buf, sizeof(buf), fmt, args);
2872 va_end(args);
2873
2874 if (compat20) {
2875 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2876 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2877 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2878 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2879 (r = sshpkt_send(ssh)) != 0)
2880 return r;
2881 } else {
2882 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2883 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2884 (r = sshpkt_send(ssh)) != 0)
2885 return r;
2886 }
2887 return 0;
2888}
2889
2890/* roundup current message to pad bytes */
2891int
2892sshpkt_add_padding(struct ssh *ssh, u_char pad)
2893{
2894 ssh->state->extra_pad = pad;
2895 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002896}