jsg@openbsd.org | 1cb3016 | 2015-03-11 00:48:39 +0000 | [diff] [blame] | 1 | /* $OpenBSD: packet.c,v 1.209 2015/03/11 00:48:39 jsg Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * 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 Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * 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 Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 14 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | * |
| 16 | * SSH2 packet format added by Markus Friedl. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 17 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 18 | * |
| 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 38 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
| 40 | #include "includes.h" |
Damien Miller | 68f8e99 | 2006-03-15 11:24:12 +1100 | [diff] [blame] | 41 | |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 42 | #include <sys/param.h> /* MIN roundup */ |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 43 | #include <sys/types.h> |
Damien Miller | a0898b8 | 2003-04-09 21:05:52 +1000 | [diff] [blame] | 44 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 45 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 46 | #ifdef HAVE_SYS_TIME_H |
| 47 | # include <sys/time.h> |
| 48 | #endif |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 49 | |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 50 | #include <netinet/in.h> |
Damien Miller | 68f8e99 | 2006-03-15 11:24:12 +1100 | [diff] [blame] | 51 | #include <netinet/ip.h> |
Darren Tucker | dace233 | 2006-09-22 19:22:17 +1000 | [diff] [blame] | 52 | #include <arpa/inet.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 54 | #include <errno.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 55 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 56 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 57 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 58 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 59 | #include <unistd.h> |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 60 | #include <limits.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 61 | #include <signal.h> |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 62 | #include <time.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 63 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 64 | #include <zlib.h> |
| 65 | |
| 66 | #include "buffer.h" /* typedefs XXX */ |
| 67 | #include "key.h" /* typedefs XXX */ |
| 68 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | #include "crc32.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | #include "deattack.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 72 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 73 | #include "ssh1.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 74 | #include "ssh2.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 75 | #include "cipher.h" |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 76 | #include "sshkey.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 77 | #include "kex.h" |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 78 | #include "digest.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 79 | #include "mac.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 80 | #include "log.h" |
| 81 | #include "canohost.h" |
Damien Miller | 4d00776 | 2002-02-05 11:52:54 +1100 | [diff] [blame] | 82 | #include "misc.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 83 | #include "channels.h" |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 84 | #include "ssh.h" |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 85 | #include "packet.h" |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 86 | #include "roaming.h" |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 87 | #include "ssherr.h" |
| 88 | #include "sshbuf.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 89 | |
| 90 | #ifdef PACKET_DEBUG |
| 91 | #define DBG(x) x |
| 92 | #else |
| 93 | #define DBG(x) |
| 94 | #endif |
| 95 | |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 96 | #define PACKET_MAX_SIZE (256 * 1024) |
| 97 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 98 | struct packet_state { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 99 | u_int32_t seqnr; |
| 100 | u_int32_t packets; |
| 101 | u_int64_t blocks; |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 102 | u_int64_t bytes; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 103 | }; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 104 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 105 | struct packet { |
| 106 | TAILQ_ENTRY(packet) next; |
| 107 | u_char type; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 108 | struct sshbuf *payload; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 109 | }; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 110 | |
| 111 | struct session_state { |
| 112 | /* |
| 113 | * This variable contains the file descriptors used for |
| 114 | * communicating with the other side. connection_in is used for |
| 115 | * reading; connection_out for writing. These can be the same |
| 116 | * descriptor, in which case it is assumed to be a socket. |
| 117 | */ |
| 118 | int connection_in; |
| 119 | int connection_out; |
| 120 | |
| 121 | /* Protocol flags for the remote side. */ |
| 122 | u_int remote_protocol_flags; |
| 123 | |
| 124 | /* Encryption context for receiving data. Only used for decryption. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 125 | struct sshcipher_ctx receive_context; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 126 | |
| 127 | /* Encryption context for sending data. Only used for encryption. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 128 | struct sshcipher_ctx send_context; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 129 | |
| 130 | /* Buffer for raw input data from the socket. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 131 | struct sshbuf *input; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 132 | |
| 133 | /* Buffer for raw output data going to the socket. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 134 | struct sshbuf *output; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 135 | |
| 136 | /* Buffer for the partial outgoing packet being constructed. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 137 | struct sshbuf *outgoing_packet; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 138 | |
| 139 | /* Buffer for the incoming packet currently being processed. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 140 | struct sshbuf *incoming_packet; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 141 | |
| 142 | /* Scratch buffer for packet compression/decompression. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 143 | struct sshbuf *compression_buffer; |
| 144 | |
| 145 | /* Incoming/outgoing compression dictionaries */ |
| 146 | z_stream compression_in_stream; |
| 147 | z_stream compression_out_stream; |
| 148 | int compression_in_started; |
| 149 | int compression_out_started; |
| 150 | int compression_in_failures; |
| 151 | int compression_out_failures; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * Flag indicating whether packet compression/decompression is |
| 155 | * enabled. |
| 156 | */ |
| 157 | int packet_compression; |
| 158 | |
| 159 | /* default maximum packet size */ |
| 160 | u_int max_packet_size; |
| 161 | |
| 162 | /* Flag indicating whether this module has been initialized. */ |
| 163 | int initialized; |
| 164 | |
| 165 | /* Set to true if the connection is interactive. */ |
| 166 | int interactive_mode; |
| 167 | |
| 168 | /* Set to true if we are the server side. */ |
| 169 | int server_side; |
| 170 | |
| 171 | /* Set to true if we are authenticated. */ |
| 172 | int after_authentication; |
| 173 | |
| 174 | int keep_alive_timeouts; |
| 175 | |
| 176 | /* The maximum time that we will wait to send or receive a packet */ |
| 177 | int packet_timeout_ms; |
| 178 | |
| 179 | /* Session key information for Encryption and MAC */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 180 | struct newkeys *newkeys[MODE_MAX]; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 181 | struct packet_state p_read, p_send; |
| 182 | |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 183 | /* Volume-based rekeying */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 184 | u_int64_t max_blocks_in, max_blocks_out; |
| 185 | u_int32_t rekey_limit; |
| 186 | |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 187 | /* Time-based rekeying */ |
markus@openbsd.org | 02db468 | 2015-02-13 18:57:00 +0000 | [diff] [blame] | 188 | u_int32_t rekey_interval; /* how often in seconds */ |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 189 | time_t rekey_time; /* time of last rekeying */ |
| 190 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 191 | /* Session key for protocol v1 */ |
| 192 | u_char ssh1_key[SSH_SESSION_KEY_LENGTH]; |
| 193 | u_int ssh1_keylen; |
| 194 | |
| 195 | /* roundup current message to extra_pad bytes */ |
| 196 | u_char extra_pad; |
| 197 | |
| 198 | /* XXX discard incoming data after MAC error */ |
| 199 | u_int packet_discard; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 200 | struct sshmac *packet_discard_mac; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 201 | |
| 202 | /* Used in packet_read_poll2() */ |
| 203 | u_int packlen; |
| 204 | |
Darren Tucker | 7b935c7 | 2009-06-21 18:59:36 +1000 | [diff] [blame] | 205 | /* Used in packet_send2 */ |
| 206 | int rekeying; |
| 207 | |
| 208 | /* Used in packet_set_interactive */ |
| 209 | int set_interactive_called; |
| 210 | |
| 211 | /* Used in packet_set_maxsize */ |
| 212 | int set_maxsize_called; |
| 213 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 214 | /* One-off warning about weak ciphers */ |
| 215 | int cipher_warning_done; |
| 216 | |
| 217 | /* SSH1 CRC compensation attack detector */ |
| 218 | struct deattack_ctx deattack; |
| 219 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 220 | TAILQ_HEAD(, packet) outgoing; |
| 221 | }; |
| 222 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 223 | struct ssh * |
| 224 | ssh_alloc_session_state(void) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 225 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 226 | struct ssh *ssh = NULL; |
| 227 | struct session_state *state = NULL; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 228 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 229 | if ((ssh = calloc(1, sizeof(*ssh))) == NULL || |
| 230 | (state = calloc(1, sizeof(*state))) == NULL || |
| 231 | (state->input = sshbuf_new()) == NULL || |
| 232 | (state->output = sshbuf_new()) == NULL || |
| 233 | (state->outgoing_packet = sshbuf_new()) == NULL || |
| 234 | (state->incoming_packet = sshbuf_new()) == NULL) |
| 235 | goto fail; |
| 236 | TAILQ_INIT(&state->outgoing); |
markus@openbsd.org | f582f0e | 2015-01-19 20:30:23 +0000 | [diff] [blame] | 237 | TAILQ_INIT(&ssh->private_keys); |
| 238 | TAILQ_INIT(&ssh->public_keys); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 239 | state->connection_in = -1; |
| 240 | state->connection_out = -1; |
| 241 | state->max_packet_size = 32768; |
| 242 | state->packet_timeout_ms = -1; |
| 243 | state->p_send.packets = state->p_read.packets = 0; |
| 244 | state->initialized = 1; |
| 245 | /* |
| 246 | * ssh_packet_send2() needs to queue packets until |
| 247 | * we've done the initial key exchange. |
| 248 | */ |
| 249 | state->rekeying = 1; |
| 250 | ssh->state = state; |
| 251 | return ssh; |
| 252 | fail: |
| 253 | if (state) { |
| 254 | sshbuf_free(state->input); |
| 255 | sshbuf_free(state->output); |
| 256 | sshbuf_free(state->incoming_packet); |
| 257 | sshbuf_free(state->outgoing_packet); |
| 258 | free(state); |
| 259 | } |
| 260 | free(ssh); |
| 261 | return NULL; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 262 | } |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 263 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 264 | /* |
| 265 | * Sets the descriptors used for communication. Disables encryption until |
| 266 | * packet_set_encryption_key is called. |
| 267 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 268 | struct ssh * |
| 269 | ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 270 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 271 | struct session_state *state; |
| 272 | const struct sshcipher *none = cipher_by_name("none"); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 273 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 274 | |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 275 | if (none == NULL) { |
| 276 | error("%s: cannot load cipher 'none'", __func__); |
| 277 | return NULL; |
| 278 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 279 | if (ssh == NULL) |
| 280 | ssh = ssh_alloc_session_state(); |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 281 | if (ssh == NULL) { |
| 282 | error("%s: cound not allocate state", __func__); |
| 283 | return NULL; |
| 284 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 285 | state = ssh->state; |
| 286 | state->connection_in = fd_in; |
| 287 | state->connection_out = fd_out; |
| 288 | if ((r = cipher_init(&state->send_context, none, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 289 | (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 || |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 290 | (r = cipher_init(&state->receive_context, none, |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 291 | (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) { |
| 292 | error("%s: cipher_init failed: %s", __func__, ssh_err(r)); |
jsg@openbsd.org | 1cb3016 | 2015-03-11 00:48:39 +0000 | [diff] [blame] | 293 | free(ssh); |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 294 | return NULL; |
| 295 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 296 | state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL; |
| 297 | deattack_init(&state->deattack); |
djm@openbsd.org | d4c0295 | 2015-02-11 01:20:38 +0000 | [diff] [blame] | 298 | /* |
| 299 | * Cache the IP address of the remote connection for use in error |
| 300 | * messages that might be generated after the connection has closed. |
| 301 | */ |
| 302 | (void)ssh_remote_ipaddr(ssh); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 303 | return ssh; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 304 | } |
| 305 | |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 306 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 307 | ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 308 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 309 | struct session_state *state = ssh->state; |
| 310 | |
Damien Miller | 8ed4de8 | 2011-12-19 10:52:50 +1100 | [diff] [blame] | 311 | if (timeout <= 0 || count <= 0) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 312 | state->packet_timeout_ms = -1; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | if ((INT_MAX / 1000) / count < timeout) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 316 | state->packet_timeout_ms = INT_MAX; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 317 | else |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 318 | state->packet_timeout_ms = timeout * count * 1000; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 319 | } |
| 320 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 321 | int |
| 322 | ssh_packet_stop_discard(struct ssh *ssh) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 323 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 324 | struct session_state *state = ssh->state; |
| 325 | int r; |
| 326 | |
| 327 | if (state->packet_discard_mac) { |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 328 | char buf[1024]; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 329 | |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 330 | memset(buf, 'a', sizeof(buf)); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 331 | while (sshbuf_len(state->incoming_packet) < |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 332 | PACKET_MAX_SIZE) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 333 | if ((r = sshbuf_put(state->incoming_packet, buf, |
| 334 | sizeof(buf))) != 0) |
| 335 | return r; |
| 336 | (void) mac_compute(state->packet_discard_mac, |
| 337 | state->p_read.seqnr, |
| 338 | sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE, |
| 339 | NULL, 0); |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 340 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 341 | logit("Finished discarding for %.200s", ssh_remote_ipaddr(ssh)); |
| 342 | return SSH_ERR_MAC_INVALID; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 343 | } |
| 344 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 345 | static int |
| 346 | ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc, |
| 347 | struct sshmac *mac, u_int packet_length, u_int discard) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 348 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 349 | struct session_state *state = ssh->state; |
| 350 | int r; |
| 351 | |
| 352 | if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) { |
| 353 | if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0) |
| 354 | return r; |
| 355 | return SSH_ERR_MAC_INVALID; |
| 356 | } |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 357 | if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 358 | state->packet_discard_mac = mac; |
| 359 | if (sshbuf_len(state->input) >= discard && |
| 360 | (r = ssh_packet_stop_discard(ssh)) != 0) |
| 361 | return r; |
| 362 | state->packet_discard = discard - sshbuf_len(state->input); |
| 363 | return 0; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 364 | } |
| 365 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 366 | /* Returns 1 if remote host is connected via socket, 0 if not. */ |
| 367 | |
| 368 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 369 | ssh_packet_connection_is_on_socket(struct ssh *ssh) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 370 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 371 | struct session_state *state = ssh->state; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 372 | struct sockaddr_storage from, to; |
| 373 | socklen_t fromlen, tolen; |
| 374 | |
| 375 | /* filedescriptors in and out are the same, so it's a socket */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 376 | if (state->connection_in == state->connection_out) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 377 | return 1; |
| 378 | fromlen = sizeof(from); |
| 379 | memset(&from, 0, sizeof(from)); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 380 | if (getpeername(state->connection_in, (struct sockaddr *)&from, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 381 | &fromlen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 382 | return 0; |
| 383 | tolen = sizeof(to); |
| 384 | memset(&to, 0, sizeof(to)); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 385 | if (getpeername(state->connection_out, (struct sockaddr *)&to, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 386 | &tolen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 387 | return 0; |
| 388 | if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0) |
| 389 | return 0; |
| 390 | if (from.ss_family != AF_INET && from.ss_family != AF_INET6) |
| 391 | return 0; |
| 392 | return 1; |
| 393 | } |
| 394 | |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 395 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 396 | ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes) |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 397 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 398 | if (ibytes) |
| 399 | *ibytes = ssh->state->p_read.bytes; |
| 400 | if (obytes) |
| 401 | *obytes = ssh->state->p_send.bytes; |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 405 | ssh_packet_connection_af(struct ssh *ssh) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 406 | { |
| 407 | struct sockaddr_storage to; |
Damien Miller | 6fe375d | 2000-01-23 09:38:00 +1100 | [diff] [blame] | 408 | socklen_t tolen = sizeof(to); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 409 | |
| 410 | memset(&to, 0, sizeof(to)); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 411 | if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 412 | &tolen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 413 | return 0; |
Damien Miller | aa100c5 | 2002-04-26 16:54:34 +1000 | [diff] [blame] | 414 | #ifdef IPV4_IN_IPV6 |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 415 | if (to.ss_family == AF_INET6 && |
Damien Miller | aa100c5 | 2002-04-26 16:54:34 +1000 | [diff] [blame] | 416 | IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr)) |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 417 | return AF_INET; |
Damien Miller | aa100c5 | 2002-04-26 16:54:34 +1000 | [diff] [blame] | 418 | #endif |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 419 | return to.ss_family; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 420 | } |
| 421 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 422 | /* Sets the connection into non-blocking mode. */ |
| 423 | |
| 424 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 425 | ssh_packet_set_nonblocking(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 426 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 427 | /* Set the socket into non-blocking mode. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 428 | set_nonblock(ssh->state->connection_in); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 429 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 430 | if (ssh->state->connection_out != ssh->state->connection_in) |
| 431 | set_nonblock(ssh->state->connection_out); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* Returns the socket used for reading. */ |
| 435 | |
| 436 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 437 | ssh_packet_get_connection_in(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 438 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 439 | return ssh->state->connection_in; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* Returns the descriptor used for writing. */ |
| 443 | |
| 444 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 445 | ssh_packet_get_connection_out(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 446 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 447 | return ssh->state->connection_out; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Returns the IP-address of the remote host as a string. The returned |
| 452 | * string must not be freed. |
| 453 | */ |
| 454 | |
| 455 | const char * |
| 456 | ssh_remote_ipaddr(struct ssh *ssh) |
| 457 | { |
| 458 | /* Check whether we have cached the ipaddr. */ |
| 459 | if (ssh->remote_ipaddr == NULL) |
| 460 | ssh->remote_ipaddr = ssh_packet_connection_is_on_socket(ssh) ? |
| 461 | get_peer_ipaddr(ssh->state->connection_in) : |
| 462 | strdup("UNKNOWN"); |
| 463 | if (ssh->remote_ipaddr == NULL) |
| 464 | return "UNKNOWN"; |
| 465 | return ssh->remote_ipaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* Closes the connection and clears and frees internal data structures. */ |
| 469 | |
| 470 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 471 | ssh_packet_close(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 472 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 473 | struct session_state *state = ssh->state; |
| 474 | int r; |
| 475 | u_int mode; |
| 476 | |
| 477 | if (!state->initialized) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 478 | return; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 479 | state->initialized = 0; |
| 480 | if (state->connection_in == state->connection_out) { |
| 481 | shutdown(state->connection_out, SHUT_RDWR); |
| 482 | close(state->connection_out); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 483 | } else { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 484 | close(state->connection_in); |
| 485 | close(state->connection_out); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 486 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 487 | sshbuf_free(state->input); |
| 488 | sshbuf_free(state->output); |
| 489 | sshbuf_free(state->outgoing_packet); |
| 490 | sshbuf_free(state->incoming_packet); |
| 491 | for (mode = 0; mode < MODE_MAX; mode++) |
| 492 | kex_free_newkeys(state->newkeys[mode]); |
| 493 | if (state->compression_buffer) { |
| 494 | sshbuf_free(state->compression_buffer); |
| 495 | if (state->compression_out_started) { |
| 496 | z_streamp stream = &state->compression_out_stream; |
| 497 | debug("compress outgoing: " |
| 498 | "raw data %llu, compressed %llu, factor %.2f", |
| 499 | (unsigned long long)stream->total_in, |
| 500 | (unsigned long long)stream->total_out, |
| 501 | stream->total_in == 0 ? 0.0 : |
| 502 | (double) stream->total_out / stream->total_in); |
| 503 | if (state->compression_out_failures == 0) |
| 504 | deflateEnd(stream); |
| 505 | } |
| 506 | if (state->compression_in_started) { |
| 507 | z_streamp stream = &state->compression_out_stream; |
| 508 | debug("compress incoming: " |
| 509 | "raw data %llu, compressed %llu, factor %.2f", |
| 510 | (unsigned long long)stream->total_out, |
| 511 | (unsigned long long)stream->total_in, |
| 512 | stream->total_out == 0 ? 0.0 : |
| 513 | (double) stream->total_in / stream->total_out); |
| 514 | if (state->compression_in_failures == 0) |
| 515 | inflateEnd(stream); |
| 516 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 517 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 518 | if ((r = cipher_cleanup(&state->send_context)) != 0) |
| 519 | error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r)); |
| 520 | if ((r = cipher_cleanup(&state->receive_context)) != 0) |
| 521 | error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r)); |
| 522 | if (ssh->remote_ipaddr) { |
| 523 | free(ssh->remote_ipaddr); |
| 524 | ssh->remote_ipaddr = NULL; |
| 525 | } |
| 526 | free(ssh->state); |
| 527 | ssh->state = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* Sets remote side protocol flags. */ |
| 531 | |
| 532 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 533 | ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 534 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 535 | ssh->state->remote_protocol_flags = protocol_flags; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* Returns the remote protocol flags set earlier by the above function. */ |
| 539 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 540 | u_int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 541 | ssh_packet_get_protocol_flags(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 542 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 543 | return ssh->state->remote_protocol_flags; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 544 | } |
| 545 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 546 | /* |
| 547 | * Starts packet compression from the next packet on in both directions. |
| 548 | * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip. |
| 549 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 550 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 551 | static int |
| 552 | ssh_packet_init_compression(struct ssh *ssh) |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 553 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 554 | if (!ssh->state->compression_buffer && |
| 555 | ((ssh->state->compression_buffer = sshbuf_new()) == NULL)) |
| 556 | return SSH_ERR_ALLOC_FAIL; |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | static int |
| 561 | start_compression_out(struct ssh *ssh, int level) |
| 562 | { |
| 563 | if (level < 1 || level > 9) |
| 564 | return SSH_ERR_INVALID_ARGUMENT; |
| 565 | debug("Enabling compression at level %d.", level); |
| 566 | if (ssh->state->compression_out_started == 1) |
| 567 | deflateEnd(&ssh->state->compression_out_stream); |
| 568 | switch (deflateInit(&ssh->state->compression_out_stream, level)) { |
| 569 | case Z_OK: |
| 570 | ssh->state->compression_out_started = 1; |
| 571 | break; |
| 572 | case Z_MEM_ERROR: |
| 573 | return SSH_ERR_ALLOC_FAIL; |
| 574 | default: |
| 575 | return SSH_ERR_INTERNAL_ERROR; |
| 576 | } |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static int |
| 581 | start_compression_in(struct ssh *ssh) |
| 582 | { |
| 583 | if (ssh->state->compression_in_started == 1) |
| 584 | inflateEnd(&ssh->state->compression_in_stream); |
| 585 | switch (inflateInit(&ssh->state->compression_in_stream)) { |
| 586 | case Z_OK: |
| 587 | ssh->state->compression_in_started = 1; |
| 588 | break; |
| 589 | case Z_MEM_ERROR: |
| 590 | return SSH_ERR_ALLOC_FAIL; |
| 591 | default: |
| 592 | return SSH_ERR_INTERNAL_ERROR; |
| 593 | } |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | int |
| 598 | ssh_packet_start_compression(struct ssh *ssh, int level) |
| 599 | { |
| 600 | int r; |
| 601 | |
| 602 | if (ssh->state->packet_compression && !compat20) |
| 603 | return SSH_ERR_INTERNAL_ERROR; |
| 604 | ssh->state->packet_compression = 1; |
| 605 | if ((r = ssh_packet_init_compression(ssh)) != 0 || |
| 606 | (r = start_compression_in(ssh)) != 0 || |
| 607 | (r = start_compression_out(ssh, level)) != 0) |
| 608 | return r; |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | /* XXX remove need for separate compression buffer */ |
| 613 | static int |
| 614 | compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out) |
| 615 | { |
| 616 | u_char buf[4096]; |
| 617 | int r, status; |
| 618 | |
| 619 | if (ssh->state->compression_out_started != 1) |
| 620 | return SSH_ERR_INTERNAL_ERROR; |
| 621 | |
| 622 | /* This case is not handled below. */ |
| 623 | if (sshbuf_len(in) == 0) |
| 624 | return 0; |
| 625 | |
| 626 | /* Input is the contents of the input buffer. */ |
| 627 | if ((ssh->state->compression_out_stream.next_in = |
| 628 | sshbuf_mutable_ptr(in)) == NULL) |
| 629 | return SSH_ERR_INTERNAL_ERROR; |
| 630 | ssh->state->compression_out_stream.avail_in = sshbuf_len(in); |
| 631 | |
| 632 | /* Loop compressing until deflate() returns with avail_out != 0. */ |
| 633 | do { |
| 634 | /* Set up fixed-size output buffer. */ |
| 635 | ssh->state->compression_out_stream.next_out = buf; |
| 636 | ssh->state->compression_out_stream.avail_out = sizeof(buf); |
| 637 | |
| 638 | /* Compress as much data into the buffer as possible. */ |
| 639 | status = deflate(&ssh->state->compression_out_stream, |
| 640 | Z_PARTIAL_FLUSH); |
| 641 | switch (status) { |
| 642 | case Z_MEM_ERROR: |
| 643 | return SSH_ERR_ALLOC_FAIL; |
| 644 | case Z_OK: |
| 645 | /* Append compressed data to output_buffer. */ |
| 646 | if ((r = sshbuf_put(out, buf, sizeof(buf) - |
| 647 | ssh->state->compression_out_stream.avail_out)) != 0) |
| 648 | return r; |
| 649 | break; |
| 650 | case Z_STREAM_ERROR: |
| 651 | default: |
| 652 | ssh->state->compression_out_failures++; |
| 653 | return SSH_ERR_INVALID_FORMAT; |
| 654 | } |
| 655 | } while (ssh->state->compression_out_stream.avail_out == 0); |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static int |
| 660 | uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out) |
| 661 | { |
| 662 | u_char buf[4096]; |
| 663 | int r, status; |
| 664 | |
| 665 | if (ssh->state->compression_in_started != 1) |
| 666 | return SSH_ERR_INTERNAL_ERROR; |
| 667 | |
| 668 | if ((ssh->state->compression_in_stream.next_in = |
| 669 | sshbuf_mutable_ptr(in)) == NULL) |
| 670 | return SSH_ERR_INTERNAL_ERROR; |
| 671 | ssh->state->compression_in_stream.avail_in = sshbuf_len(in); |
| 672 | |
| 673 | for (;;) { |
| 674 | /* Set up fixed-size output buffer. */ |
| 675 | ssh->state->compression_in_stream.next_out = buf; |
| 676 | ssh->state->compression_in_stream.avail_out = sizeof(buf); |
| 677 | |
| 678 | status = inflate(&ssh->state->compression_in_stream, |
| 679 | Z_PARTIAL_FLUSH); |
| 680 | switch (status) { |
| 681 | case Z_OK: |
| 682 | if ((r = sshbuf_put(out, buf, sizeof(buf) - |
| 683 | ssh->state->compression_in_stream.avail_out)) != 0) |
| 684 | return r; |
| 685 | break; |
| 686 | case Z_BUF_ERROR: |
| 687 | /* |
| 688 | * Comments in zlib.h say that we should keep calling |
| 689 | * inflate() until we get an error. This appears to |
| 690 | * be the error that we get. |
| 691 | */ |
| 692 | return 0; |
| 693 | case Z_DATA_ERROR: |
| 694 | return SSH_ERR_INVALID_FORMAT; |
| 695 | case Z_MEM_ERROR: |
| 696 | return SSH_ERR_ALLOC_FAIL; |
| 697 | case Z_STREAM_ERROR: |
| 698 | default: |
| 699 | ssh->state->compression_in_failures++; |
| 700 | return SSH_ERR_INTERNAL_ERROR; |
| 701 | } |
| 702 | } |
| 703 | /* NOTREACHED */ |
| 704 | } |
| 705 | |
| 706 | /* Serialise compression state into a blob for privsep */ |
| 707 | static int |
| 708 | ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh) |
| 709 | { |
| 710 | struct session_state *state = ssh->state; |
| 711 | struct sshbuf *b; |
| 712 | int r; |
| 713 | |
| 714 | if ((b = sshbuf_new()) == NULL) |
| 715 | return SSH_ERR_ALLOC_FAIL; |
| 716 | if (state->compression_in_started) { |
| 717 | if ((r = sshbuf_put_string(b, &state->compression_in_stream, |
| 718 | sizeof(state->compression_in_stream))) != 0) |
| 719 | goto out; |
| 720 | } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0) |
| 721 | goto out; |
| 722 | if (state->compression_out_started) { |
| 723 | if ((r = sshbuf_put_string(b, &state->compression_out_stream, |
| 724 | sizeof(state->compression_out_stream))) != 0) |
| 725 | goto out; |
| 726 | } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0) |
| 727 | goto out; |
| 728 | r = sshbuf_put_stringb(m, b); |
| 729 | out: |
| 730 | sshbuf_free(b); |
| 731 | return r; |
| 732 | } |
| 733 | |
| 734 | /* Deserialise compression state from a blob for privsep */ |
| 735 | static int |
| 736 | ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m) |
| 737 | { |
| 738 | struct session_state *state = ssh->state; |
| 739 | struct sshbuf *b = NULL; |
| 740 | int r; |
| 741 | const u_char *inblob, *outblob; |
| 742 | size_t inl, outl; |
| 743 | |
| 744 | if ((r = sshbuf_froms(m, &b)) != 0) |
| 745 | goto out; |
| 746 | if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 || |
| 747 | (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0) |
| 748 | goto out; |
| 749 | if (inl == 0) |
| 750 | state->compression_in_started = 0; |
| 751 | else if (inl != sizeof(state->compression_in_stream)) { |
| 752 | r = SSH_ERR_INTERNAL_ERROR; |
| 753 | goto out; |
| 754 | } else { |
| 755 | state->compression_in_started = 1; |
| 756 | memcpy(&state->compression_in_stream, inblob, inl); |
| 757 | } |
| 758 | if (outl == 0) |
| 759 | state->compression_out_started = 0; |
| 760 | else if (outl != sizeof(state->compression_out_stream)) { |
| 761 | r = SSH_ERR_INTERNAL_ERROR; |
| 762 | goto out; |
| 763 | } else { |
| 764 | state->compression_out_started = 1; |
| 765 | memcpy(&state->compression_out_stream, outblob, outl); |
| 766 | } |
| 767 | r = 0; |
| 768 | out: |
| 769 | sshbuf_free(b); |
| 770 | return r; |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 773 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 774 | ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx, |
| 775 | void *(*allocfunc)(void *, u_int, u_int), |
| 776 | void (*freefunc)(void *, void *)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 777 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 778 | ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc; |
| 779 | ssh->state->compression_out_stream.zfree = (free_func)freefunc; |
| 780 | ssh->state->compression_out_stream.opaque = ctx; |
| 781 | ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc; |
| 782 | ssh->state->compression_in_stream.zfree = (free_func)freefunc; |
| 783 | ssh->state->compression_in_stream.opaque = ctx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 784 | } |
| 785 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 786 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 787 | * Causes any further packets to be encrypted using the given key. The same |
| 788 | * key is used for both sending and reception. However, both directions are |
| 789 | * encrypted independently of each other. |
| 790 | */ |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 791 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 792 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 793 | ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 794 | { |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 795 | #ifdef WITH_SSH1 |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 796 | struct session_state *state = ssh->state; |
| 797 | const struct sshcipher *cipher = cipher_by_number(number); |
| 798 | int r; |
| 799 | const char *wmsg; |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 800 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 801 | if (cipher == NULL) |
| 802 | fatal("%s: unknown cipher number %d", __func__, number); |
| 803 | if (keylen < 20) |
| 804 | fatal("%s: keylen too small: %d", __func__, keylen); |
| 805 | if (keylen > SSH_SESSION_KEY_LENGTH) |
| 806 | fatal("%s: keylen too big: %d", __func__, keylen); |
| 807 | memcpy(state->ssh1_key, key, keylen); |
| 808 | state->ssh1_keylen = keylen; |
| 809 | if ((r = cipher_init(&state->send_context, cipher, key, keylen, |
| 810 | NULL, 0, CIPHER_ENCRYPT)) != 0 || |
| 811 | (r = cipher_init(&state->receive_context, cipher, key, keylen, |
| 812 | NULL, 0, CIPHER_DECRYPT) != 0)) |
| 813 | fatal("%s: cipher_init failed: %s", __func__, ssh_err(r)); |
| 814 | if (!state->cipher_warning_done && |
| 815 | ((wmsg = cipher_warning_message(&state->send_context)) != NULL || |
| 816 | (wmsg = cipher_warning_message(&state->send_context)) != NULL)) { |
| 817 | error("Warning: %s", wmsg); |
| 818 | state->cipher_warning_done = 1; |
| 819 | } |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 820 | #endif /* WITH_SSH1 */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 821 | } |
| 822 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 823 | /* |
| 824 | * Finalizes and sends the packet. If the encryption key has been set, |
| 825 | * encrypts the packet before sending. |
| 826 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 827 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 828 | int |
| 829 | ssh_packet_send1(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 830 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 831 | struct session_state *state = ssh->state; |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 832 | u_char buf[8], *cp; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 833 | int r, padding, len; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 834 | u_int checksum; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 835 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 836 | /* |
| 837 | * If using packet compression, compress the payload of the outgoing |
| 838 | * packet. |
| 839 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 840 | if (state->packet_compression) { |
| 841 | sshbuf_reset(state->compression_buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 842 | /* Skip padding. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 843 | if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0) |
| 844 | goto out; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 845 | /* padding */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 846 | if ((r = sshbuf_put(state->compression_buffer, |
| 847 | "\0\0\0\0\0\0\0\0", 8)) != 0) |
| 848 | goto out; |
| 849 | if ((r = compress_buffer(ssh, state->outgoing_packet, |
| 850 | state->compression_buffer)) != 0) |
| 851 | goto out; |
| 852 | sshbuf_reset(state->outgoing_packet); |
| 853 | if ((r = sshbuf_putb(state->outgoing_packet, |
| 854 | state->compression_buffer)) != 0) |
| 855 | goto out; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 856 | } |
| 857 | /* Compute packet length without padding (add checksum, remove padding). */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 858 | len = sshbuf_len(state->outgoing_packet) + 4 - 8; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 859 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 860 | /* Insert padding. Initialized to zero in packet_start1() */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 861 | padding = 8 - len % 8; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 862 | if (!state->send_context.plaintext) { |
| 863 | cp = sshbuf_mutable_ptr(state->outgoing_packet); |
| 864 | if (cp == NULL) { |
| 865 | r = SSH_ERR_INTERNAL_ERROR; |
| 866 | goto out; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 867 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 868 | arc4random_buf(cp + 8 - padding, padding); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 869 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 870 | if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0) |
| 871 | goto out; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 872 | |
| 873 | /* Add check bytes. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 874 | checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet), |
| 875 | sshbuf_len(state->outgoing_packet)); |
| 876 | POKE_U32(buf, checksum); |
| 877 | if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0) |
| 878 | goto out; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 879 | |
| 880 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 881 | fprintf(stderr, "packet_send plain: "); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 882 | sshbuf_dump(state->outgoing_packet, stderr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 883 | #endif |
| 884 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 885 | /* Append to output. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 886 | POKE_U32(buf, len); |
| 887 | if ((r = sshbuf_put(state->output, buf, 4)) != 0) |
| 888 | goto out; |
| 889 | if ((r = sshbuf_reserve(state->output, |
| 890 | sshbuf_len(state->outgoing_packet), &cp)) != 0) |
| 891 | goto out; |
| 892 | if ((r = cipher_crypt(&state->send_context, 0, cp, |
| 893 | sshbuf_ptr(state->outgoing_packet), |
| 894 | sshbuf_len(state->outgoing_packet), 0, 0)) != 0) |
| 895 | goto out; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 896 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 897 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 898 | fprintf(stderr, "encrypted: "); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 899 | sshbuf_dump(state->output, stderr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 900 | #endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 901 | state->p_send.packets++; |
| 902 | state->p_send.bytes += len + |
| 903 | sshbuf_len(state->outgoing_packet); |
| 904 | sshbuf_reset(state->outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 905 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 906 | /* |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 907 | * Note that the packet is now only buffered in output. It won't be |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 908 | * actually sent until ssh_packet_write_wait or ssh_packet_write_poll |
| 909 | * is called. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 910 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 911 | r = 0; |
| 912 | out: |
| 913 | return r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 914 | } |
| 915 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 916 | int |
| 917 | ssh_set_newkeys(struct ssh *ssh, int mode) |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 918 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 919 | struct session_state *state = ssh->state; |
| 920 | struct sshenc *enc; |
| 921 | struct sshmac *mac; |
| 922 | struct sshcomp *comp; |
| 923 | struct sshcipher_ctx *cc; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 924 | u_int64_t *max_blocks; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 925 | const char *wmsg; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 926 | int r, crypt_type; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 927 | |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 928 | debug2("set_newkeys: mode %d", mode); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 929 | |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 930 | if (mode == MODE_OUT) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 931 | cc = &state->send_context; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 932 | crypt_type = CIPHER_ENCRYPT; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 933 | state->p_send.packets = state->p_send.blocks = 0; |
| 934 | max_blocks = &state->max_blocks_out; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 935 | } else { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 936 | cc = &state->receive_context; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 937 | crypt_type = CIPHER_DECRYPT; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 938 | state->p_read.packets = state->p_read.blocks = 0; |
| 939 | max_blocks = &state->max_blocks_in; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 940 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 941 | if (state->newkeys[mode] != NULL) { |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 942 | debug("set_newkeys: rekeying"); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 943 | if ((r = cipher_cleanup(cc)) != 0) |
| 944 | return r; |
| 945 | enc = &state->newkeys[mode]->enc; |
| 946 | mac = &state->newkeys[mode]->mac; |
| 947 | comp = &state->newkeys[mode]->comp; |
Damien Miller | e45796f | 2007-06-11 14:01:42 +1000 | [diff] [blame] | 948 | mac_clear(mac); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 949 | explicit_bzero(enc->iv, enc->iv_len); |
| 950 | explicit_bzero(enc->key, enc->key_len); |
| 951 | explicit_bzero(mac->key, mac->key_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 952 | free(enc->name); |
| 953 | free(enc->iv); |
| 954 | free(enc->key); |
| 955 | free(mac->name); |
| 956 | free(mac->key); |
| 957 | free(comp->name); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 958 | free(state->newkeys[mode]); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 959 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 960 | /* move newkeys from kex to state */ |
| 961 | if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL) |
| 962 | return SSH_ERR_INTERNAL_ERROR; |
| 963 | ssh->kex->newkeys[mode] = NULL; |
| 964 | enc = &state->newkeys[mode]->enc; |
| 965 | mac = &state->newkeys[mode]->mac; |
| 966 | comp = &state->newkeys[mode]->comp; |
| 967 | if (cipher_authlen(enc->cipher) == 0) { |
| 968 | if ((r = mac_init(mac)) != 0) |
| 969 | return r; |
| 970 | } |
| 971 | mac->enabled = 1; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 972 | DBG(debug("cipher_init_context: %d", mode)); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 973 | if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len, |
| 974 | enc->iv, enc->iv_len, crypt_type)) != 0) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 975 | return r; |
| 976 | if (!state->cipher_warning_done && |
| 977 | (wmsg = cipher_warning_message(cc)) != NULL) { |
| 978 | error("Warning: %s", wmsg); |
| 979 | state->cipher_warning_done = 1; |
| 980 | } |
Ben Lindstrom | f6027d3 | 2002-03-22 01:42:04 +0000 | [diff] [blame] | 981 | /* Deleting the keys does not gain extra security */ |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 982 | /* explicit_bzero(enc->iv, enc->block_size); |
| 983 | explicit_bzero(enc->key, enc->key_len); |
| 984 | explicit_bzero(mac->key, mac->key_len); */ |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 985 | if ((comp->type == COMP_ZLIB || |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 986 | (comp->type == COMP_DELAYED && |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 987 | state->after_authentication)) && comp->enabled == 0) { |
| 988 | if ((r = ssh_packet_init_compression(ssh)) < 0) |
| 989 | return r; |
| 990 | if (mode == MODE_OUT) { |
| 991 | if ((r = start_compression_out(ssh, 6)) != 0) |
| 992 | return r; |
| 993 | } else { |
| 994 | if ((r = start_compression_in(ssh)) != 0) |
| 995 | return r; |
| 996 | } |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 997 | comp->enabled = 1; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 998 | } |
Darren Tucker | 81a0b37 | 2003-07-14 17:31:06 +1000 | [diff] [blame] | 999 | /* |
| 1000 | * The 2^(blocksize*2) limit is too expensive for 3DES, |
| 1001 | * blowfish, etc, so enforce a 1GB limit for small blocksizes. |
| 1002 | */ |
| 1003 | if (enc->block_size >= 16) |
| 1004 | *max_blocks = (u_int64_t)1 << (enc->block_size*2); |
| 1005 | else |
| 1006 | *max_blocks = ((u_int64_t)1 << 30) / enc->block_size; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1007 | if (state->rekey_limit) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1008 | *max_blocks = MIN(*max_blocks, |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1009 | state->rekey_limit / enc->block_size); |
| 1010 | return 0; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1013 | /* |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1014 | * Delayed compression for SSH2 is enabled after authentication: |
Darren Tucker | f676c57 | 2006-08-05 18:51:08 +1000 | [diff] [blame] | 1015 | * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent, |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1016 | * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received. |
| 1017 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1018 | static int |
| 1019 | ssh_packet_enable_delayed_compress(struct ssh *ssh) |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1020 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1021 | struct session_state *state = ssh->state; |
| 1022 | struct sshcomp *comp = NULL; |
| 1023 | int r, mode; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1024 | |
| 1025 | /* |
| 1026 | * Remember that we are past the authentication step, so rekeying |
| 1027 | * with COMP_DELAYED will turn on compression immediately. |
| 1028 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1029 | state->after_authentication = 1; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1030 | for (mode = 0; mode < MODE_MAX; mode++) { |
Darren Tucker | 4aa665b | 2006-09-21 13:00:25 +1000 | [diff] [blame] | 1031 | /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1032 | if (state->newkeys[mode] == NULL) |
Darren Tucker | 4aa665b | 2006-09-21 13:00:25 +1000 | [diff] [blame] | 1033 | continue; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1034 | comp = &state->newkeys[mode]->comp; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1035 | if (comp && !comp->enabled && comp->type == COMP_DELAYED) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1036 | if ((r = ssh_packet_init_compression(ssh)) != 0) |
| 1037 | return r; |
| 1038 | if (mode == MODE_OUT) { |
| 1039 | if ((r = start_compression_out(ssh, 6)) != 0) |
| 1040 | return r; |
| 1041 | } else { |
| 1042 | if ((r = start_compression_in(ssh)) != 0) |
| 1043 | return r; |
| 1044 | } |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1045 | comp->enabled = 1; |
| 1046 | } |
| 1047 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1048 | return 0; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1052 | * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue) |
| 1053 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1054 | int |
| 1055 | ssh_packet_send2_wrapped(struct ssh *ssh) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1056 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1057 | struct session_state *state = ssh->state; |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1058 | u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH]; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1059 | u_char padlen, pad = 0; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1060 | u_int authlen = 0, aadlen = 0; |
| 1061 | u_int len; |
| 1062 | struct sshenc *enc = NULL; |
| 1063 | struct sshmac *mac = NULL; |
| 1064 | struct sshcomp *comp = NULL; |
| 1065 | int r, block_size; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1066 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1067 | if (state->newkeys[MODE_OUT] != NULL) { |
| 1068 | enc = &state->newkeys[MODE_OUT]->enc; |
| 1069 | mac = &state->newkeys[MODE_OUT]->mac; |
| 1070 | comp = &state->newkeys[MODE_OUT]->comp; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1071 | /* disable mac for authenticated encryption */ |
| 1072 | if ((authlen = cipher_authlen(enc->cipher)) != 0) |
| 1073 | mac = NULL; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1074 | } |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 1075 | block_size = enc ? enc->block_size : 8; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1076 | aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1077 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1078 | type = (sshbuf_ptr(state->outgoing_packet))[5]; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1079 | |
| 1080 | #ifdef PACKET_DEBUG |
| 1081 | fprintf(stderr, "plain: "); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1082 | sshbuf_dump(state->outgoing_packet, stderr); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1083 | #endif |
| 1084 | |
| 1085 | if (comp && comp->enabled) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1086 | len = sshbuf_len(state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1087 | /* skip header, compress only payload */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1088 | if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0) |
| 1089 | goto out; |
| 1090 | sshbuf_reset(state->compression_buffer); |
| 1091 | if ((r = compress_buffer(ssh, state->outgoing_packet, |
| 1092 | state->compression_buffer)) != 0) |
| 1093 | goto out; |
| 1094 | sshbuf_reset(state->outgoing_packet); |
| 1095 | if ((r = sshbuf_put(state->outgoing_packet, |
| 1096 | "\0\0\0\0\0", 5)) != 0 || |
| 1097 | (r = sshbuf_putb(state->outgoing_packet, |
| 1098 | state->compression_buffer)) != 0) |
| 1099 | goto out; |
| 1100 | DBG(debug("compression: raw %d compressed %zd", len, |
| 1101 | sshbuf_len(state->outgoing_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* sizeof (packet_len + pad_len + payload) */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1105 | len = sshbuf_len(state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1106 | |
| 1107 | /* |
| 1108 | * calc size of padding, alloc space, get random data, |
| 1109 | * minimum padding is 4 bytes |
| 1110 | */ |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1111 | len -= aadlen; /* packet length is not encrypted for EtM modes */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1112 | padlen = block_size - (len % block_size); |
| 1113 | if (padlen < 4) |
| 1114 | padlen += block_size; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1115 | if (state->extra_pad) { |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1116 | /* will wrap if extra_pad+padlen > 255 */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1117 | state->extra_pad = |
| 1118 | roundup(state->extra_pad, block_size); |
| 1119 | pad = state->extra_pad - |
| 1120 | ((len + padlen) % state->extra_pad); |
Damien Miller | 2a32843 | 2014-04-20 13:24:01 +1000 | [diff] [blame] | 1121 | DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)", |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1122 | __func__, pad, len, padlen, state->extra_pad)); |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1123 | padlen += pad; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1124 | state->extra_pad = 0; |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1125 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1126 | if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0) |
| 1127 | goto out; |
| 1128 | if (enc && !state->send_context.plaintext) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1129 | /* random padding */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1130 | arc4random_buf(cp, padlen); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1131 | } else { |
| 1132 | /* clear padding */ |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1133 | explicit_bzero(cp, padlen); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1134 | } |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1135 | /* sizeof (packet_len + pad_len + payload + padding) */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1136 | len = sshbuf_len(state->outgoing_packet); |
| 1137 | cp = sshbuf_mutable_ptr(state->outgoing_packet); |
| 1138 | if (cp == NULL) { |
| 1139 | r = SSH_ERR_INTERNAL_ERROR; |
| 1140 | goto out; |
| 1141 | } |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1142 | /* packet_length includes payload, padding and padding length field */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1143 | POKE_U32(cp, len - 4); |
Ben Lindstrom | 4a7714a | 2002-02-26 18:04:38 +0000 | [diff] [blame] | 1144 | cp[4] = padlen; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1145 | DBG(debug("send: len %d (includes padlen %d, aadlen %d)", |
| 1146 | len, padlen, aadlen)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1147 | |
| 1148 | /* compute MAC over seqnr and packet(length fields, payload, padding) */ |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1149 | if (mac && mac->enabled && !mac->etm) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1150 | if ((r = mac_compute(mac, state->p_send.seqnr, |
| 1151 | sshbuf_ptr(state->outgoing_packet), len, |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1152 | macbuf, sizeof(macbuf))) != 0) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1153 | goto out; |
| 1154 | DBG(debug("done calc MAC out #%d", state->p_send.seqnr)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1155 | } |
| 1156 | /* encrypt packet and append to output buffer. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1157 | if ((r = sshbuf_reserve(state->output, |
| 1158 | sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0) |
| 1159 | goto out; |
| 1160 | if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp, |
| 1161 | sshbuf_ptr(state->outgoing_packet), |
| 1162 | len - aadlen, aadlen, authlen)) != 0) |
| 1163 | goto out; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1164 | /* append unencrypted MAC */ |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1165 | if (mac && mac->enabled) { |
| 1166 | if (mac->etm) { |
| 1167 | /* EtM: compute mac over aadlen + cipher text */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1168 | if ((r = mac_compute(mac, state->p_send.seqnr, |
| 1169 | cp, len, macbuf, sizeof(macbuf))) != 0) |
| 1170 | goto out; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1171 | DBG(debug("done calc MAC(EtM) out #%d", |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1172 | state->p_send.seqnr)); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1173 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1174 | if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0) |
| 1175 | goto out; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1176 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1177 | #ifdef PACKET_DEBUG |
| 1178 | fprintf(stderr, "encrypted: "); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1179 | sshbuf_dump(state->output, stderr); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1180 | #endif |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1181 | /* increment sequence number for outgoing packets */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1182 | if (++state->p_send.seqnr == 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1183 | logit("outgoing seqnr wraps around"); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1184 | if (++state->p_send.packets == 0) |
| 1185 | if (!(ssh->compat & SSH_BUG_NOREKEY)) |
| 1186 | return SSH_ERR_NEED_REKEY; |
| 1187 | state->p_send.blocks += len / block_size; |
| 1188 | state->p_send.bytes += len; |
| 1189 | sshbuf_reset(state->outgoing_packet); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1190 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 1191 | if (type == SSH2_MSG_NEWKEYS) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1192 | r = ssh_set_newkeys(ssh, MODE_OUT); |
| 1193 | else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side) |
| 1194 | r = ssh_packet_enable_delayed_compress(ssh); |
| 1195 | else |
| 1196 | r = 0; |
| 1197 | out: |
| 1198 | return r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1199 | } |
| 1200 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1201 | int |
| 1202 | ssh_packet_send2(struct ssh *ssh) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1203 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1204 | struct session_state *state = ssh->state; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1205 | struct packet *p; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1206 | u_char type; |
| 1207 | int r; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1208 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1209 | type = sshbuf_ptr(state->outgoing_packet)[5]; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1210 | |
| 1211 | /* during rekeying we can only send key exchange messages */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1212 | if (state->rekeying) { |
Damien Miller | 1de2cfe | 2012-02-11 08:18:43 +1100 | [diff] [blame] | 1213 | if ((type < SSH2_MSG_TRANSPORT_MIN) || |
| 1214 | (type > SSH2_MSG_TRANSPORT_MAX) || |
| 1215 | (type == SSH2_MSG_SERVICE_REQUEST) || |
| 1216 | (type == SSH2_MSG_SERVICE_ACCEPT)) { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1217 | debug("enqueue packet: %u", type); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1218 | p = calloc(1, sizeof(*p)); |
| 1219 | if (p == NULL) |
| 1220 | return SSH_ERR_ALLOC_FAIL; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1221 | p->type = type; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1222 | p->payload = state->outgoing_packet; |
| 1223 | TAILQ_INSERT_TAIL(&state->outgoing, p, next); |
| 1224 | state->outgoing_packet = sshbuf_new(); |
| 1225 | if (state->outgoing_packet == NULL) |
| 1226 | return SSH_ERR_ALLOC_FAIL; |
| 1227 | return 0; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | /* rekeying starts with sending KEXINIT */ |
| 1232 | if (type == SSH2_MSG_KEXINIT) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1233 | state->rekeying = 1; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1234 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1235 | if ((r = ssh_packet_send2_wrapped(ssh)) != 0) |
| 1236 | return r; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1237 | |
| 1238 | /* after a NEWKEYS message we can send the complete queue */ |
| 1239 | if (type == SSH2_MSG_NEWKEYS) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1240 | state->rekeying = 0; |
| 1241 | state->rekey_time = monotime(); |
| 1242 | while ((p = TAILQ_FIRST(&state->outgoing))) { |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1243 | type = p->type; |
| 1244 | debug("dequeue packet: %u", type); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1245 | sshbuf_free(state->outgoing_packet); |
| 1246 | state->outgoing_packet = p->payload; |
| 1247 | TAILQ_REMOVE(&state->outgoing, p, next); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1248 | free(p); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1249 | if ((r = ssh_packet_send2_wrapped(ssh)) != 0) |
| 1250 | return r; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1251 | } |
| 1252 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1253 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1254 | } |
| 1255 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1256 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1257 | * Waits until a packet has been received, and returns its type. Note that |
| 1258 | * no other data is processed until this returns, so this function should not |
| 1259 | * be used during the interactive session. |
| 1260 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1261 | |
| 1262 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1263 | ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1264 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1265 | struct session_state *state = ssh->state; |
| 1266 | int len, r, ms_remain, cont; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1267 | fd_set *setp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1268 | char buf[8192]; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1269 | struct timeval timeout, start, *timeoutp = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1270 | |
Darren Tucker | 99bb761 | 2008-06-13 22:02:50 +1000 | [diff] [blame] | 1271 | DBG(debug("packet_read()")); |
| 1272 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1273 | setp = (fd_set *)calloc(howmany(state->connection_in + 1, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1274 | NFDBITS), sizeof(fd_mask)); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1275 | if (setp == NULL) |
| 1276 | return SSH_ERR_ALLOC_FAIL; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 1277 | |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1278 | /* |
| 1279 | * Since we are blocking, ensure that all written packets have |
| 1280 | * been sent. |
| 1281 | */ |
jsg@openbsd.org | 1cb3016 | 2015-03-11 00:48:39 +0000 | [diff] [blame] | 1282 | if ((r = ssh_packet_write_wait(ssh)) != 0) { |
| 1283 | free(setp); |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1284 | return r; |
jsg@openbsd.org | 1cb3016 | 2015-03-11 00:48:39 +0000 | [diff] [blame] | 1285 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1286 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1287 | /* Stay in the loop until we have received a complete packet. */ |
| 1288 | for (;;) { |
| 1289 | /* Try to read a packet from the buffer. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1290 | r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p); |
| 1291 | if (r != 0) |
| 1292 | break; |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1293 | if (!compat20 && ( |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1294 | *typep == SSH_SMSG_SUCCESS |
| 1295 | || *typep == SSH_SMSG_FAILURE |
| 1296 | || *typep == SSH_CMSG_EOF |
| 1297 | || *typep == SSH_CMSG_EXIT_CONFIRMATION)) |
| 1298 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 1299 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1300 | /* If we got a packet, return it. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1301 | if (*typep != SSH_MSG_NONE) |
| 1302 | break; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1303 | /* |
| 1304 | * Otherwise, wait for some data to arrive, add it to the |
| 1305 | * buffer, and try again. |
| 1306 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1307 | memset(setp, 0, howmany(state->connection_in + 1, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1308 | NFDBITS) * sizeof(fd_mask)); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1309 | FD_SET(state->connection_in, setp); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1310 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1311 | if (state->packet_timeout_ms > 0) { |
| 1312 | ms_remain = state->packet_timeout_ms; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1313 | timeoutp = &timeout; |
| 1314 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1315 | /* Wait for some data to arrive. */ |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1316 | for (;;) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1317 | if (state->packet_timeout_ms != -1) { |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1318 | ms_to_timeval(&timeout, ms_remain); |
| 1319 | gettimeofday(&start, NULL); |
| 1320 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1321 | if ((r = select(state->connection_in + 1, setp, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1322 | NULL, NULL, timeoutp)) >= 0) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1323 | break; |
Damien Miller | ea43742 | 2009-10-02 11:49:03 +1000 | [diff] [blame] | 1324 | if (errno != EAGAIN && errno != EINTR && |
| 1325 | errno != EWOULDBLOCK) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1326 | break; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1327 | if (state->packet_timeout_ms == -1) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1328 | continue; |
| 1329 | ms_subtract_diff(&start, &ms_remain); |
| 1330 | if (ms_remain <= 0) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1331 | r = 0; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1332 | break; |
| 1333 | } |
| 1334 | } |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 1335 | if (r == 0) |
| 1336 | return SSH_ERR_CONN_TIMEOUT; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1337 | /* Read data from the socket. */ |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1338 | do { |
| 1339 | cont = 0; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1340 | len = roaming_read(state->connection_in, buf, |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1341 | sizeof(buf), &cont); |
| 1342 | } while (len == 0 && cont); |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 1343 | if (len == 0) |
| 1344 | return SSH_ERR_CONN_CLOSED; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1345 | if (len < 0) |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 1346 | return SSH_ERR_SYSTEM_ERROR; |
| 1347 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1348 | /* Append it to the buffer. */ |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 1349 | if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0) |
| 1350 | return r; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1351 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1352 | free(setp); |
| 1353 | return r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1354 | } |
| 1355 | |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1356 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1357 | ssh_packet_read(struct ssh *ssh) |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1358 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1359 | u_char type; |
| 1360 | int r; |
| 1361 | |
| 1362 | if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0) |
| 1363 | fatal("%s: %s", __func__, ssh_err(r)); |
| 1364 | return type; |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1365 | } |
| 1366 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1367 | /* |
| 1368 | * Waits until a packet has been received, verifies that its type matches |
| 1369 | * that given, and gives a fatal error and exits if there is a mismatch. |
| 1370 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1371 | |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1372 | int |
| 1373 | ssh_packet_read_expect(struct ssh *ssh, u_int expected_type) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1374 | { |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1375 | int r; |
| 1376 | u_char type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1377 | |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1378 | if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0) |
| 1379 | return r; |
| 1380 | if (type != expected_type) { |
| 1381 | if ((r = sshpkt_disconnect(ssh, |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1382 | "Protocol error: expected packet type %d, got %d", |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1383 | expected_type, type)) != 0) |
| 1384 | return r; |
| 1385 | return SSH_ERR_PROTOCOL_ERROR; |
| 1386 | } |
| 1387 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /* Checks if a full packet is available in the data received so far via |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1391 | * packet_process_incoming. If so, reads the packet; otherwise returns |
| 1392 | * SSH_MSG_NONE. This does not wait for data from the connection. |
| 1393 | * |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1394 | * SSH_MSG_DISCONNECT is handled specially here. Also, |
| 1395 | * SSH_MSG_IGNORE messages are skipped by this function and are never returned |
| 1396 | * to higher levels. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1397 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1398 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1399 | int |
| 1400 | ssh_packet_read_poll1(struct ssh *ssh, u_char *typep) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1401 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1402 | struct session_state *state = ssh->state; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1403 | u_int len, padded_len; |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1404 | const char *emsg; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1405 | const u_char *cp; |
| 1406 | u_char *p; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1407 | u_int checksum, stored_checksum; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1408 | int r; |
| 1409 | |
| 1410 | *typep = SSH_MSG_NONE; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1411 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1412 | /* Check if input size is less than minimum packet size. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1413 | if (sshbuf_len(state->input) < 4 + 8) |
| 1414 | return 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1415 | /* Get length of incoming packet. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1416 | len = PEEK_U32(sshbuf_ptr(state->input)); |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1417 | if (len < 1 + 2 + 2 || len > 256 * 1024) { |
| 1418 | if ((r = sshpkt_disconnect(ssh, "Bad packet length %u", |
| 1419 | len)) != 0) |
| 1420 | return r; |
| 1421 | return SSH_ERR_CONN_CORRUPT; |
| 1422 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1423 | padded_len = (len + 8) & ~7; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1424 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1425 | /* Check if the packet has been entirely received. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1426 | if (sshbuf_len(state->input) < 4 + padded_len) |
| 1427 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1428 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1429 | /* The entire packet is in buffer. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1430 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1431 | /* Consume packet length. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1432 | if ((r = sshbuf_consume(state->input, 4)) != 0) |
| 1433 | goto out; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1434 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1435 | /* |
| 1436 | * Cryptographic attack detector for ssh |
| 1437 | * (C)1998 CORE-SDI, Buenos Aires Argentina |
| 1438 | * Ariel Futoransky(futo@core-sdi.com) |
| 1439 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1440 | if (!state->receive_context.plaintext) { |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1441 | emsg = NULL; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1442 | switch (detect_attack(&state->deattack, |
| 1443 | sshbuf_ptr(state->input), padded_len)) { |
| 1444 | case DEATTACK_OK: |
| 1445 | break; |
Damien Miller | 3c9c1fb | 2006-09-17 06:08:53 +1000 | [diff] [blame] | 1446 | case DEATTACK_DETECTED: |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1447 | emsg = "crc32 compensation attack detected"; |
| 1448 | break; |
Damien Miller | 3c9c1fb | 2006-09-17 06:08:53 +1000 | [diff] [blame] | 1449 | case DEATTACK_DOS_DETECTED: |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1450 | emsg = "deattack denial of service detected"; |
| 1451 | break; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1452 | default: |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1453 | emsg = "deattack error"; |
| 1454 | break; |
| 1455 | } |
| 1456 | if (emsg != NULL) { |
| 1457 | error("%s", emsg); |
| 1458 | if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 || |
| 1459 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 1460 | return r; |
| 1461 | return SSH_ERR_CONN_CORRUPT; |
Damien Miller | 3c9c1fb | 2006-09-17 06:08:53 +1000 | [diff] [blame] | 1462 | } |
| 1463 | } |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1464 | |
| 1465 | /* Decrypt data to incoming_packet. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1466 | sshbuf_reset(state->incoming_packet); |
| 1467 | if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0) |
| 1468 | goto out; |
| 1469 | if ((r = cipher_crypt(&state->receive_context, 0, p, |
| 1470 | sshbuf_ptr(state->input), padded_len, 0, 0)) != 0) |
| 1471 | goto out; |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1472 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1473 | if ((r = sshbuf_consume(state->input, padded_len)) != 0) |
| 1474 | goto out; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1475 | |
| 1476 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1477 | fprintf(stderr, "read_poll plain: "); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1478 | sshbuf_dump(state->incoming_packet, stderr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1479 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1480 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1481 | /* Compute packet checksum. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1482 | checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet), |
| 1483 | sshbuf_len(state->incoming_packet) - 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1484 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1485 | /* Skip padding. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1486 | if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0) |
| 1487 | goto out; |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 1488 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1489 | /* Test check bytes. */ |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1490 | if (len != sshbuf_len(state->incoming_packet)) { |
| 1491 | error("%s: len %d != sshbuf_len %zd", __func__, |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1492 | len, sshbuf_len(state->incoming_packet)); |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1493 | if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 || |
| 1494 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 1495 | return r; |
| 1496 | return SSH_ERR_CONN_CORRUPT; |
| 1497 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1498 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1499 | cp = sshbuf_ptr(state->incoming_packet) + len - 4; |
| 1500 | stored_checksum = PEEK_U32(cp); |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1501 | if (checksum != stored_checksum) { |
| 1502 | error("Corrupted check bytes on input"); |
| 1503 | if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 || |
| 1504 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 1505 | return r; |
| 1506 | return SSH_ERR_CONN_CORRUPT; |
| 1507 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1508 | if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0) |
| 1509 | goto out; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1510 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1511 | if (state->packet_compression) { |
| 1512 | sshbuf_reset(state->compression_buffer); |
| 1513 | if ((r = uncompress_buffer(ssh, state->incoming_packet, |
| 1514 | state->compression_buffer)) != 0) |
| 1515 | goto out; |
| 1516 | sshbuf_reset(state->incoming_packet); |
| 1517 | if ((r = sshbuf_putb(state->incoming_packet, |
| 1518 | state->compression_buffer)) != 0) |
| 1519 | goto out; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1520 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1521 | state->p_read.packets++; |
| 1522 | state->p_read.bytes += padded_len + 4; |
| 1523 | if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0) |
| 1524 | goto out; |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1525 | if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) { |
| 1526 | error("Invalid ssh1 packet type: %d", *typep); |
| 1527 | if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 || |
| 1528 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 1529 | return r; |
| 1530 | return SSH_ERR_PROTOCOL_ERROR; |
| 1531 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1532 | r = 0; |
| 1533 | out: |
| 1534 | return r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1535 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1536 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1537 | int |
| 1538 | ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1539 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1540 | struct session_state *state = ssh->state; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1541 | u_int padlen, need; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1542 | u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH]; |
| 1543 | u_int maclen, aadlen = 0, authlen = 0, block_size; |
| 1544 | struct sshenc *enc = NULL; |
| 1545 | struct sshmac *mac = NULL; |
| 1546 | struct sshcomp *comp = NULL; |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1547 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1548 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1549 | *typep = SSH_MSG_NONE; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1550 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1551 | if (state->packet_discard) |
| 1552 | return 0; |
| 1553 | |
| 1554 | if (state->newkeys[MODE_IN] != NULL) { |
| 1555 | enc = &state->newkeys[MODE_IN]->enc; |
| 1556 | mac = &state->newkeys[MODE_IN]->mac; |
| 1557 | comp = &state->newkeys[MODE_IN]->comp; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1558 | /* disable mac for authenticated encryption */ |
| 1559 | if ((authlen = cipher_authlen(enc->cipher)) != 0) |
| 1560 | mac = NULL; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1561 | } |
| 1562 | maclen = mac && mac->enabled ? mac->mac_len : 0; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 1563 | block_size = enc ? enc->block_size : 8; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1564 | aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1565 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1566 | if (aadlen && state->packlen == 0) { |
| 1567 | if (cipher_get_length(&state->receive_context, |
| 1568 | &state->packlen, state->p_read.seqnr, |
| 1569 | sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0) |
| 1570 | return 0; |
| 1571 | if (state->packlen < 1 + 4 || |
| 1572 | state->packlen > PACKET_MAX_SIZE) { |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1573 | #ifdef PACKET_DEBUG |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1574 | sshbuf_dump(state->input, stderr); |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1575 | #endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1576 | logit("Bad packet length %u.", state->packlen); |
| 1577 | if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0) |
| 1578 | return r; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1579 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1580 | sshbuf_reset(state->incoming_packet); |
| 1581 | } else if (state->packlen == 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1582 | /* |
| 1583 | * check if input size is less than the cipher block size, |
| 1584 | * decrypt first block and extract length of incoming packet |
| 1585 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1586 | if (sshbuf_len(state->input) < block_size) |
| 1587 | return 0; |
| 1588 | sshbuf_reset(state->incoming_packet); |
| 1589 | if ((r = sshbuf_reserve(state->incoming_packet, block_size, |
| 1590 | &cp)) != 0) |
| 1591 | goto out; |
| 1592 | if ((r = cipher_crypt(&state->receive_context, |
| 1593 | state->p_send.seqnr, cp, sshbuf_ptr(state->input), |
| 1594 | block_size, 0, 0)) != 0) |
| 1595 | goto out; |
| 1596 | state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet)); |
| 1597 | if (state->packlen < 1 + 4 || |
| 1598 | state->packlen > PACKET_MAX_SIZE) { |
Darren Tucker | a8151da | 2003-09-22 21:06:46 +1000 | [diff] [blame] | 1599 | #ifdef PACKET_DEBUG |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1600 | fprintf(stderr, "input: \n"); |
| 1601 | sshbuf_dump(state->input, stderr); |
| 1602 | fprintf(stderr, "incoming_packet: \n"); |
| 1603 | sshbuf_dump(state->incoming_packet, stderr); |
Darren Tucker | a8151da | 2003-09-22 21:06:46 +1000 | [diff] [blame] | 1604 | #endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1605 | logit("Bad packet length %u.", state->packlen); |
| 1606 | return ssh_packet_start_discard(ssh, enc, mac, |
| 1607 | state->packlen, PACKET_MAX_SIZE); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1608 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1609 | if ((r = sshbuf_consume(state->input, block_size)) != 0) |
| 1610 | goto out; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1611 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1612 | DBG(debug("input: packet len %u", state->packlen+4)); |
| 1613 | |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1614 | if (aadlen) { |
| 1615 | /* only the payload is encrypted */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1616 | need = state->packlen; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1617 | } else { |
| 1618 | /* |
| 1619 | * the payload size and the payload are encrypted, but we |
| 1620 | * have a partial packet of block_size bytes |
| 1621 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1622 | need = 4 + state->packlen - block_size; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1623 | } |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1624 | DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d," |
| 1625 | " aadlen %d", block_size, need, maclen, authlen, aadlen)); |
Darren Tucker | 99d11a3 | 2008-12-01 21:40:48 +1100 | [diff] [blame] | 1626 | if (need % block_size != 0) { |
| 1627 | logit("padding error: need %d block %d mod %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1628 | need, block_size, need % block_size); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1629 | return ssh_packet_start_discard(ssh, enc, mac, |
| 1630 | state->packlen, PACKET_MAX_SIZE - block_size); |
Darren Tucker | 99d11a3 | 2008-12-01 21:40:48 +1100 | [diff] [blame] | 1631 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1632 | /* |
| 1633 | * check if the entire packet has been received and |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1634 | * decrypt into incoming_packet: |
| 1635 | * 'aadlen' bytes are unencrypted, but authenticated. |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 1636 | * 'need' bytes are encrypted, followed by either |
| 1637 | * 'authlen' bytes of authentication tag or |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1638 | * 'maclen' bytes of message authentication code. |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1639 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1640 | if (sshbuf_len(state->input) < aadlen + need + authlen + maclen) |
| 1641 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1642 | #ifdef PACKET_DEBUG |
| 1643 | fprintf(stderr, "read_poll enc/full: "); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1644 | sshbuf_dump(state->input, stderr); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1645 | #endif |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1646 | /* EtM: compute mac over encrypted input */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1647 | if (mac && mac->enabled && mac->etm) { |
| 1648 | if ((r = mac_compute(mac, state->p_read.seqnr, |
| 1649 | sshbuf_ptr(state->input), aadlen + need, |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1650 | macbuf, sizeof(macbuf))) != 0) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1651 | goto out; |
| 1652 | } |
| 1653 | if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need, |
| 1654 | &cp)) != 0) |
| 1655 | goto out; |
| 1656 | if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp, |
| 1657 | sshbuf_ptr(state->input), need, aadlen, authlen)) != 0) |
| 1658 | goto out; |
| 1659 | if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0) |
| 1660 | goto out; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1661 | /* |
| 1662 | * compute MAC over seqnr and packet, |
| 1663 | * increment sequence number for incoming packet |
| 1664 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1665 | if (mac && mac->enabled) { |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 1666 | if (!mac->etm) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1667 | if ((r = mac_compute(mac, state->p_read.seqnr, |
| 1668 | sshbuf_ptr(state->incoming_packet), |
| 1669 | sshbuf_len(state->incoming_packet), |
markus@openbsd.org | 128343b | 2015-01-13 19:31:40 +0000 | [diff] [blame] | 1670 | macbuf, sizeof(macbuf))) != 0) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1671 | goto out; |
| 1672 | if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input), |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1673 | mac->mac_len) != 0) { |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1674 | logit("Corrupted MAC on input."); |
| 1675 | if (need > PACKET_MAX_SIZE) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1676 | return SSH_ERR_INTERNAL_ERROR; |
| 1677 | return ssh_packet_start_discard(ssh, enc, mac, |
| 1678 | state->packlen, PACKET_MAX_SIZE - need); |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1679 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1680 | |
| 1681 | DBG(debug("MAC #%d ok", state->p_read.seqnr)); |
| 1682 | if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0) |
| 1683 | goto out; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1684 | } |
Damien Miller | 278f907 | 2001-12-21 15:00:19 +1100 | [diff] [blame] | 1685 | if (seqnr_p != NULL) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1686 | *seqnr_p = state->p_read.seqnr; |
| 1687 | if (++state->p_read.seqnr == 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1688 | logit("incoming seqnr wraps around"); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1689 | if (++state->p_read.packets == 0) |
| 1690 | if (!(ssh->compat & SSH_BUG_NOREKEY)) |
| 1691 | return SSH_ERR_NEED_REKEY; |
| 1692 | state->p_read.blocks += (state->packlen + 4) / block_size; |
| 1693 | state->p_read.bytes += state->packlen + 4; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1694 | |
| 1695 | /* get padlen */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1696 | padlen = sshbuf_ptr(state->incoming_packet)[4]; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1697 | DBG(debug("input: padlen %d", padlen)); |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1698 | if (padlen < 4) { |
| 1699 | if ((r = sshpkt_disconnect(ssh, |
| 1700 | "Corrupted padlen %d on input.", padlen)) != 0 || |
| 1701 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 1702 | return r; |
| 1703 | return SSH_ERR_CONN_CORRUPT; |
| 1704 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1705 | |
| 1706 | /* skip packet size + padlen, discard padding */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1707 | if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 || |
| 1708 | ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0)) |
| 1709 | goto out; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1710 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1711 | DBG(debug("input: len before de-compress %zd", |
| 1712 | sshbuf_len(state->incoming_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1713 | if (comp && comp->enabled) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1714 | sshbuf_reset(state->compression_buffer); |
| 1715 | if ((r = uncompress_buffer(ssh, state->incoming_packet, |
| 1716 | state->compression_buffer)) != 0) |
| 1717 | goto out; |
| 1718 | sshbuf_reset(state->incoming_packet); |
| 1719 | if ((r = sshbuf_putb(state->incoming_packet, |
| 1720 | state->compression_buffer)) != 0) |
| 1721 | goto out; |
| 1722 | DBG(debug("input: len after de-compress %zd", |
| 1723 | sshbuf_len(state->incoming_packet))); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1724 | } |
| 1725 | /* |
| 1726 | * get packet type, implies consume. |
| 1727 | * return length of payload (without type field) |
| 1728 | */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1729 | if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0) |
| 1730 | goto out; |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1731 | if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) { |
| 1732 | if ((r = sshpkt_disconnect(ssh, |
| 1733 | "Invalid ssh2 packet type: %d", *typep)) != 0 || |
| 1734 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 1735 | return r; |
| 1736 | return SSH_ERR_PROTOCOL_ERROR; |
| 1737 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1738 | if (*typep == SSH2_MSG_NEWKEYS) |
| 1739 | r = ssh_set_newkeys(ssh, MODE_IN); |
| 1740 | else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) |
| 1741 | r = ssh_packet_enable_delayed_compress(ssh); |
| 1742 | else |
| 1743 | r = 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1744 | #ifdef PACKET_DEBUG |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1745 | fprintf(stderr, "read/plain[%d]:\r\n", *typep); |
| 1746 | sshbuf_dump(state->incoming_packet, stderr); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1747 | #endif |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1748 | /* reset for next packet */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1749 | state->packlen = 0; |
| 1750 | out: |
| 1751 | return r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1755 | ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1756 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1757 | struct session_state *state = ssh->state; |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1758 | u_int reason, seqnr; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1759 | int r; |
| 1760 | u_char *msg; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1761 | |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1762 | for (;;) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1763 | msg = NULL; |
Ben Lindstrom | 80c6d77 | 2001-06-05 21:09:18 +0000 | [diff] [blame] | 1764 | if (compat20) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1765 | r = ssh_packet_read_poll2(ssh, typep, seqnr_p); |
| 1766 | if (r != 0) |
| 1767 | return r; |
| 1768 | if (*typep) { |
| 1769 | state->keep_alive_timeouts = 0; |
| 1770 | DBG(debug("received packet type %d", *typep)); |
Darren Tucker | 136e56f | 2008-06-08 12:49:30 +1000 | [diff] [blame] | 1771 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1772 | switch (*typep) { |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1773 | case SSH2_MSG_IGNORE: |
Damien Miller | 58226f6 | 2008-03-07 18:33:30 +1100 | [diff] [blame] | 1774 | debug3("Received SSH2_MSG_IGNORE"); |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1775 | break; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1776 | case SSH2_MSG_DEBUG: |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1777 | if ((r = sshpkt_get_u8(ssh, NULL)) != 0 || |
| 1778 | (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 || |
| 1779 | (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) { |
| 1780 | if (msg) |
| 1781 | free(msg); |
| 1782 | return r; |
| 1783 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1784 | debug("Remote: %.900s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1785 | free(msg); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1786 | break; |
| 1787 | case SSH2_MSG_DISCONNECT: |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1788 | if ((r = sshpkt_get_u32(ssh, &reason)) != 0 || |
| 1789 | (r = sshpkt_get_string(ssh, &msg, NULL)) != 0) |
| 1790 | return r; |
Damien Miller | d5edefd | 2013-04-23 15:21:39 +1000 | [diff] [blame] | 1791 | /* Ignore normal client exit notifications */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1792 | do_log2(ssh->state->server_side && |
Damien Miller | d5edefd | 2013-04-23 15:21:39 +1000 | [diff] [blame] | 1793 | reason == SSH2_DISCONNECT_BY_APPLICATION ? |
| 1794 | SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR, |
| 1795 | "Received disconnect from %s: %u: %.400s", |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1796 | ssh_remote_ipaddr(ssh), reason, msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1797 | free(msg); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1798 | return SSH_ERR_DISCONNECTED; |
Damien Miller | 659811f | 2002-01-22 23:23:11 +1100 | [diff] [blame] | 1799 | case SSH2_MSG_UNIMPLEMENTED: |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1800 | if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0) |
| 1801 | return r; |
Ben Lindstrom | 3f58474 | 2002-06-23 21:49:25 +0000 | [diff] [blame] | 1802 | debug("Received SSH2_MSG_UNIMPLEMENTED for %u", |
| 1803 | seqnr); |
Damien Miller | 5ed3d57 | 2008-02-10 22:27:47 +1100 | [diff] [blame] | 1804 | break; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1805 | default: |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1806 | return 0; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1807 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1808 | } else { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1809 | r = ssh_packet_read_poll1(ssh, typep); |
| 1810 | switch (*typep) { |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1811 | case SSH_MSG_NONE: |
| 1812 | return SSH_MSG_NONE; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1813 | case SSH_MSG_IGNORE: |
| 1814 | break; |
| 1815 | case SSH_MSG_DEBUG: |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1816 | if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0) |
| 1817 | return r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1818 | debug("Remote: %.900s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1819 | free(msg); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1820 | break; |
| 1821 | case SSH_MSG_DISCONNECT: |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1822 | if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0) |
| 1823 | return r; |
Damien Miller | 894926e | 2013-02-12 11:03:58 +1100 | [diff] [blame] | 1824 | error("Received disconnect from %s: %.400s", |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1825 | ssh_remote_ipaddr(ssh), msg); |
| 1826 | free(msg); |
| 1827 | return SSH_ERR_DISCONNECTED; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1828 | default: |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1829 | DBG(debug("received packet type %d", *typep)); |
| 1830 | return 0; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1831 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1836 | /* |
| 1837 | * Buffers the given amount of input characters. This is intended to be used |
| 1838 | * together with packet_read_poll. |
| 1839 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1840 | |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 1841 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1842 | ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1843 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1844 | struct session_state *state = ssh->state; |
| 1845 | int r; |
| 1846 | |
| 1847 | if (state->packet_discard) { |
| 1848 | state->keep_alive_timeouts = 0; /* ?? */ |
| 1849 | if (len >= state->packet_discard) { |
| 1850 | if ((r = ssh_packet_stop_discard(ssh)) != 0) |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 1851 | return r; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1852 | } |
| 1853 | state->packet_discard -= len; |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 1854 | return 0; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 1855 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1856 | if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0) |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 1857 | return r; |
| 1858 | |
| 1859 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1860 | } |
| 1861 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1862 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1863 | ssh_packet_remaining(struct ssh *ssh) |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1864 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1865 | return sshbuf_len(ssh->state->incoming_packet); |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 1866 | } |
| 1867 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1868 | /* |
| 1869 | * Sends a diagnostic message from the server to the client. This message |
| 1870 | * can be sent at any time (but not while constructing another message). The |
| 1871 | * message is printed immediately, but only if the client is being executed |
| 1872 | * in verbose mode. These messages are primarily intended to ease debugging |
| 1873 | * authentication problems. The length of the formatted message must not |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1874 | * exceed 1024 bytes. This will automatically call ssh_packet_write_wait. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1875 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1876 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1877 | ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1878 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1879 | char buf[1024]; |
| 1880 | va_list args; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1881 | int r; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1882 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1883 | if (compat20 && (ssh->compat & SSH_BUG_DEBUG)) |
Ben Lindstrom | a14ee47 | 2000-12-07 01:24:58 +0000 | [diff] [blame] | 1884 | return; |
| 1885 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1886 | va_start(args, fmt); |
| 1887 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 1888 | va_end(args); |
| 1889 | |
Damien Miller | 7c8af4f | 2000-05-01 08:24:07 +1000 | [diff] [blame] | 1890 | if (compat20) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1891 | if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 || |
| 1892 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */ |
| 1893 | (r = sshpkt_put_cstring(ssh, buf)) != 0 || |
| 1894 | (r = sshpkt_put_cstring(ssh, "")) != 0 || |
| 1895 | (r = sshpkt_send(ssh)) != 0) |
| 1896 | fatal("%s: %s", __func__, ssh_err(r)); |
Damien Miller | 7c8af4f | 2000-05-01 08:24:07 +1000 | [diff] [blame] | 1897 | } else { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1898 | if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 || |
| 1899 | (r = sshpkt_put_cstring(ssh, buf)) != 0 || |
| 1900 | (r = sshpkt_send(ssh)) != 0) |
| 1901 | fatal("%s: %s", __func__, ssh_err(r)); |
Damien Miller | 7c8af4f | 2000-05-01 08:24:07 +1000 | [diff] [blame] | 1902 | } |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1903 | if ((r = ssh_packet_write_wait(ssh)) != 0) |
| 1904 | fatal("%s: %s", __func__, ssh_err(r)); |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Pretty-print connection-terminating errors and exit. |
| 1909 | */ |
| 1910 | void |
| 1911 | sshpkt_fatal(struct ssh *ssh, const char *tag, int r) |
| 1912 | { |
| 1913 | switch (r) { |
| 1914 | case SSH_ERR_CONN_CLOSED: |
| 1915 | logit("Connection closed by %.200s", ssh_remote_ipaddr(ssh)); |
| 1916 | cleanup_exit(255); |
| 1917 | case SSH_ERR_CONN_TIMEOUT: |
| 1918 | logit("Connection to %.200s timed out while " |
| 1919 | "waiting to write", ssh_remote_ipaddr(ssh)); |
| 1920 | cleanup_exit(255); |
| 1921 | default: |
| 1922 | fatal("%s%sConnection to %.200s: %s", |
| 1923 | tag != NULL ? tag : "", tag != NULL ? ": " : "", |
| 1924 | ssh_remote_ipaddr(ssh), ssh_err(r)); |
| 1925 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1926 | } |
| 1927 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1928 | /* |
| 1929 | * Logs the error plus constructs and sends a disconnect packet, closes the |
| 1930 | * connection, and exits. This function never returns. The error message |
| 1931 | * should not contain a newline. The length of the formatted message must |
| 1932 | * not exceed 1024 bytes. |
| 1933 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1934 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1935 | ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1936 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1937 | char buf[1024]; |
| 1938 | va_list args; |
| 1939 | static int disconnecting = 0; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1940 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1941 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1942 | if (disconnecting) /* Guard against recursive invocations. */ |
| 1943 | fatal("packet_disconnect called recursively."); |
| 1944 | disconnecting = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1945 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1946 | /* |
| 1947 | * Format the message. Note that the caller must make sure the |
| 1948 | * message is of limited size. |
| 1949 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1950 | va_start(args, fmt); |
| 1951 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 1952 | va_end(args); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1953 | |
Ben Lindstrom | 9bda7ae | 2002-11-09 15:46:24 +0000 | [diff] [blame] | 1954 | /* Display the error locally */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1955 | logit("Disconnecting: %.100s", buf); |
Ben Lindstrom | 9bda7ae | 2002-11-09 15:46:24 +0000 | [diff] [blame] | 1956 | |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1957 | /* |
| 1958 | * Send the disconnect message to the other side, and wait |
| 1959 | * for it to get sent. |
| 1960 | */ |
| 1961 | if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0) |
| 1962 | sshpkt_fatal(ssh, __func__, r); |
| 1963 | |
| 1964 | if ((r = ssh_packet_write_wait(ssh)) != 0) |
| 1965 | sshpkt_fatal(ssh, __func__, r); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1966 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1967 | /* Close the connection. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1968 | ssh_packet_close(ssh); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1969 | cleanup_exit(255); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1970 | } |
| 1971 | |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1972 | /* |
| 1973 | * Checks if there is any buffered output, and tries to write some of |
| 1974 | * the output. |
| 1975 | */ |
| 1976 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1977 | ssh_packet_write_poll(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1978 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1979 | struct session_state *state = ssh->state; |
| 1980 | int len = sshbuf_len(state->output); |
| 1981 | int cont, r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1982 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1983 | if (len > 0) { |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1984 | cont = 0; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1985 | len = roaming_write(state->connection_out, |
| 1986 | sshbuf_ptr(state->output), len, &cont); |
Damien Miller | d874fa5 | 2008-07-05 09:40:56 +1000 | [diff] [blame] | 1987 | if (len == -1) { |
Damien Miller | ea43742 | 2009-10-02 11:49:03 +1000 | [diff] [blame] | 1988 | if (errno == EINTR || errno == EAGAIN || |
| 1989 | errno == EWOULDBLOCK) |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1990 | return 0; |
| 1991 | return SSH_ERR_SYSTEM_ERROR; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1992 | } |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1993 | if (len == 0 && !cont) |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1994 | return SSH_ERR_CONN_CLOSED; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1995 | if ((r = sshbuf_consume(state->output, len)) != 0) |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1996 | return r; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1997 | } |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 1998 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1999 | } |
| 2000 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2001 | /* |
| 2002 | * Calls packet_write_poll repeatedly until all pending output data has been |
| 2003 | * written. |
| 2004 | */ |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 2005 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2006 | ssh_packet_write_wait(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2007 | { |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 2008 | fd_set *setp; |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 2009 | int ret, r, ms_remain = 0; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 2010 | struct timeval start, timeout, *timeoutp = NULL; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2011 | struct session_state *state = ssh->state; |
Ben Lindstrom | cb978aa | 2001-03-05 07:07:49 +0000 | [diff] [blame] | 2012 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2013 | setp = (fd_set *)calloc(howmany(state->connection_out + 1, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2014 | NFDBITS), sizeof(fd_mask)); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2015 | if (setp == NULL) |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 2016 | return SSH_ERR_ALLOC_FAIL; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2017 | ssh_packet_write_poll(ssh); |
| 2018 | while (ssh_packet_have_data_to_write(ssh)) { |
| 2019 | memset(setp, 0, howmany(state->connection_out + 1, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2020 | NFDBITS) * sizeof(fd_mask)); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2021 | FD_SET(state->connection_out, setp); |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 2022 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2023 | if (state->packet_timeout_ms > 0) { |
| 2024 | ms_remain = state->packet_timeout_ms; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 2025 | timeoutp = &timeout; |
| 2026 | } |
| 2027 | for (;;) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2028 | if (state->packet_timeout_ms != -1) { |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 2029 | ms_to_timeval(&timeout, ms_remain); |
| 2030 | gettimeofday(&start, NULL); |
| 2031 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2032 | if ((ret = select(state->connection_out + 1, |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2033 | NULL, setp, NULL, timeoutp)) >= 0) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 2034 | break; |
Damien Miller | ea43742 | 2009-10-02 11:49:03 +1000 | [diff] [blame] | 2035 | if (errno != EAGAIN && errno != EINTR && |
| 2036 | errno != EWOULDBLOCK) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 2037 | break; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2038 | if (state->packet_timeout_ms == -1) |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 2039 | continue; |
| 2040 | ms_subtract_diff(&start, &ms_remain); |
| 2041 | if (ms_remain <= 0) { |
| 2042 | ret = 0; |
| 2043 | break; |
| 2044 | } |
| 2045 | } |
| 2046 | if (ret == 0) { |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 2047 | free(setp); |
| 2048 | return SSH_ERR_CONN_TIMEOUT; |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 2049 | } |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 2050 | if ((r = ssh_packet_write_poll(ssh)) != 0) { |
| 2051 | free(setp); |
| 2052 | return r; |
| 2053 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2054 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2055 | free(setp); |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 2056 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | /* Returns true if there is buffered data to write to the connection. */ |
| 2060 | |
| 2061 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2062 | ssh_packet_have_data_to_write(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2063 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2064 | return sshbuf_len(ssh->state->output) != 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /* Returns true if there is not too much data to write to the connection. */ |
| 2068 | |
| 2069 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2070 | ssh_packet_not_very_much_data_to_write(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2071 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2072 | if (ssh->state->interactive_mode) |
| 2073 | return sshbuf_len(ssh->state->output) < 16384; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2074 | else |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2075 | return sshbuf_len(ssh->state->output) < 128 * 1024; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2076 | } |
| 2077 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2078 | void |
| 2079 | ssh_packet_set_tos(struct ssh *ssh, int tos) |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 2080 | { |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 2081 | #ifndef IP_TOS_IS_BROKEN |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2082 | if (!ssh_packet_connection_is_on_socket(ssh)) |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 2083 | return; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2084 | switch (ssh_packet_connection_af(ssh)) { |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 2085 | # ifdef IP_TOS |
| 2086 | case AF_INET: |
| 2087 | debug3("%s: set IP_TOS 0x%02x", __func__, tos); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2088 | if (setsockopt(ssh->state->connection_in, |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 2089 | IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) |
| 2090 | error("setsockopt IP_TOS %d: %.100s:", |
| 2091 | tos, strerror(errno)); |
| 2092 | break; |
| 2093 | # endif /* IP_TOS */ |
| 2094 | # ifdef IPV6_TCLASS |
| 2095 | case AF_INET6: |
| 2096 | debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2097 | if (setsockopt(ssh->state->connection_in, |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 2098 | IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0) |
| 2099 | error("setsockopt IPV6_TCLASS %d: %.100s:", |
| 2100 | tos, strerror(errno)); |
| 2101 | break; |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 2102 | # endif /* IPV6_TCLASS */ |
Damien Miller | 23f425b | 2011-05-15 08:58:15 +1000 | [diff] [blame] | 2103 | } |
Damien Miller | d2ac5d7 | 2011-05-15 08:43:13 +1000 | [diff] [blame] | 2104 | #endif /* IP_TOS_IS_BROKEN */ |
Damien Miller | 5924ceb | 2003-11-22 15:02:42 +1100 | [diff] [blame] | 2105 | } |
Ben Lindstrom | a743398 | 2002-12-23 02:41:41 +0000 | [diff] [blame] | 2106 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2107 | /* Informs that the current session is interactive. Sets IP flags for that. */ |
| 2108 | |
| 2109 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2110 | ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2111 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2112 | struct session_state *state = ssh->state; |
| 2113 | |
| 2114 | if (state->set_interactive_called) |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 2115 | return; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2116 | state->set_interactive_called = 1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 2117 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2118 | /* Record that we are in interactive mode. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2119 | state->interactive_mode = interactive; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2120 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2121 | /* Only set socket options if using a socket. */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2122 | if (!ssh_packet_connection_is_on_socket(ssh)) |
Ben Lindstrom | 93b6b77 | 2003-04-27 17:55:33 +0000 | [diff] [blame] | 2123 | return; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2124 | set_nodelay(state->connection_in); |
| 2125 | ssh_packet_set_tos(ssh, interactive ? qos_interactive : |
| 2126 | qos_bulk); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | /* Returns true if the current connection is interactive. */ |
| 2130 | |
| 2131 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2132 | ssh_packet_is_interactive(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2133 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2134 | return ssh->state->interactive_mode; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2135 | } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 2136 | |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 2137 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2138 | ssh_packet_set_maxsize(struct ssh *ssh, u_int s) |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 2139 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2140 | struct session_state *state = ssh->state; |
| 2141 | |
| 2142 | if (state->set_maxsize_called) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2143 | logit("packet_set_maxsize: called twice: old %d new %d", |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2144 | state->max_packet_size, s); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2145 | return -1; |
| 2146 | } |
| 2147 | if (s < 4 * 1024 || s > 1024 * 1024) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2148 | logit("packet_set_maxsize: bad size %d", s); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2149 | return -1; |
| 2150 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2151 | state->set_maxsize_called = 1; |
Ben Lindstrom | 16d45b3 | 2001-06-13 04:39:18 +0000 | [diff] [blame] | 2152 | debug("packet_set_maxsize: setting to %d", s); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2153 | state->max_packet_size = s; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2154 | return s; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 2155 | } |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 2156 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2157 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2158 | ssh_packet_inc_alive_timeouts(struct ssh *ssh) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2159 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2160 | return ++ssh->state->keep_alive_timeouts; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2164 | ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2165 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2166 | ssh->state->keep_alive_timeouts = ka; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | u_int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2170 | ssh_packet_get_maxsize(struct ssh *ssh) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2171 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2172 | return ssh->state->max_packet_size; |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 2173 | } |
| 2174 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 2175 | /* |
| 2176 | * 9.2. Ignored Data Message |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 2177 | * |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 2178 | * byte SSH_MSG_IGNORE |
| 2179 | * string data |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 2180 | * |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 2181 | * All implementations MUST understand (and ignore) this message at any |
| 2182 | * time (after receiving the protocol version). No implementation is |
| 2183 | * required to send them. This message can be used as an additional |
| 2184 | * protection measure against advanced traffic analysis techniques. |
| 2185 | */ |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 2186 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2187 | ssh_packet_send_ignore(struct ssh *ssh, int nbytes) |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 2188 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2189 | u_int32_t rnd = 0; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2190 | int r, i; |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 2191 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2192 | if ((r = sshpkt_start(ssh, compat20 ? |
| 2193 | SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 || |
| 2194 | (r = sshpkt_put_u32(ssh, nbytes)) != 0) |
| 2195 | fatal("%s: %s", __func__, ssh_err(r)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 2196 | for (i = 0; i < nbytes; i++) { |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 2197 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2198 | rnd = arc4random(); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2199 | if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0) |
| 2200 | fatal("%s: %s", __func__, ssh_err(r)); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2201 | rnd >>= 8; |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 2202 | } |
| 2203 | } |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 2204 | |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 2205 | #define MAX_PACKETS (1U<<31) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 2206 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2207 | ssh_packet_need_rekeying(struct ssh *ssh) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 2208 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2209 | struct session_state *state = ssh->state; |
| 2210 | |
| 2211 | if (ssh->compat & SSH_BUG_NOREKEY) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 2212 | return 0; |
| 2213 | return |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2214 | (state->p_send.packets > MAX_PACKETS) || |
| 2215 | (state->p_read.packets > MAX_PACKETS) || |
| 2216 | (state->max_blocks_out && |
| 2217 | (state->p_send.blocks > state->max_blocks_out)) || |
| 2218 | (state->max_blocks_in && |
| 2219 | (state->p_read.blocks > state->max_blocks_in)) || |
| 2220 | (state->rekey_interval != 0 && state->rekey_time + |
| 2221 | state->rekey_interval <= monotime()); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2225 | ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds) |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 2226 | { |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 2227 | debug3("rekey after %lld bytes, %d seconds", (long long)bytes, |
| 2228 | (int)seconds); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2229 | ssh->state->rekey_limit = bytes; |
| 2230 | ssh->state->rekey_interval = seconds; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | time_t |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2234 | ssh_packet_get_rekey_timeout(struct ssh *ssh) |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 2235 | { |
| 2236 | time_t seconds; |
| 2237 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2238 | seconds = ssh->state->rekey_time + ssh->state->rekey_interval - |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2239 | monotime(); |
Darren Tucker | 5f96f3b | 2013-05-16 20:29:28 +1000 | [diff] [blame] | 2240 | return (seconds <= 0 ? 1 : seconds); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 2241 | } |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 2242 | |
| 2243 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2244 | ssh_packet_set_server(struct ssh *ssh) |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 2245 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2246 | ssh->state->server_side = 1; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2250 | ssh_packet_set_authenticated(struct ssh *ssh) |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 2251 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2252 | ssh->state->after_authentication = 1; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | void * |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2256 | ssh_packet_get_input(struct ssh *ssh) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2257 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2258 | return (void *)ssh->state->input; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | void * |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2262 | ssh_packet_get_output(struct ssh *ssh) |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2263 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2264 | return (void *)ssh->state->output; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2265 | } |
| 2266 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2267 | /* XXX TODO update roaming to new API (does not work anyway) */ |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2268 | /* |
| 2269 | * Save the state for the real connection, and use a separate state when |
| 2270 | * resuming a suspended connection. |
| 2271 | */ |
| 2272 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2273 | ssh_packet_backup_state(struct ssh *ssh, |
| 2274 | struct ssh *backup_state) |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2275 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2276 | struct ssh *tmp; |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2277 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2278 | close(ssh->state->connection_in); |
| 2279 | ssh->state->connection_in = -1; |
| 2280 | close(ssh->state->connection_out); |
| 2281 | ssh->state->connection_out = -1; |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2282 | if (backup_state) |
| 2283 | tmp = backup_state; |
| 2284 | else |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2285 | tmp = ssh_alloc_session_state(); |
| 2286 | backup_state = ssh; |
| 2287 | ssh = tmp; |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2288 | } |
| 2289 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2290 | /* XXX FIXME FIXME FIXME */ |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2291 | /* |
| 2292 | * Swap in the old state when resuming a connecion. |
| 2293 | */ |
| 2294 | void |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2295 | ssh_packet_restore_state(struct ssh *ssh, |
| 2296 | struct ssh *backup_state) |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2297 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2298 | struct ssh *tmp; |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2299 | u_int len; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2300 | int r; |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2301 | |
| 2302 | tmp = backup_state; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2303 | backup_state = ssh; |
| 2304 | ssh = tmp; |
| 2305 | ssh->state->connection_in = backup_state->state->connection_in; |
| 2306 | backup_state->state->connection_in = -1; |
| 2307 | ssh->state->connection_out = backup_state->state->connection_out; |
| 2308 | backup_state->state->connection_out = -1; |
| 2309 | len = sshbuf_len(backup_state->state->input); |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2310 | if (len > 0) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2311 | if ((r = sshbuf_putb(ssh->state->input, |
| 2312 | backup_state->state->input)) != 0) |
| 2313 | fatal("%s: %s", __func__, ssh_err(r)); |
| 2314 | sshbuf_reset(backup_state->state->input); |
Darren Tucker | e841eb0 | 2009-07-06 07:11:13 +1000 | [diff] [blame] | 2315 | add_recv_bytes(len); |
| 2316 | } |
| 2317 | } |
Damien Miller | c31a0cd | 2014-05-15 14:37:39 +1000 | [diff] [blame] | 2318 | |
| 2319 | /* Reset after_authentication and reset compression in post-auth privsep */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2320 | static int |
| 2321 | ssh_packet_set_postauth(struct ssh *ssh) |
Damien Miller | c31a0cd | 2014-05-15 14:37:39 +1000 | [diff] [blame] | 2322 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2323 | struct sshcomp *comp; |
| 2324 | int r, mode; |
Damien Miller | c31a0cd | 2014-05-15 14:37:39 +1000 | [diff] [blame] | 2325 | |
| 2326 | debug("%s: called", __func__); |
| 2327 | /* This was set in net child, but is not visible in user child */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2328 | ssh->state->after_authentication = 1; |
| 2329 | ssh->state->rekeying = 0; |
Damien Miller | c31a0cd | 2014-05-15 14:37:39 +1000 | [diff] [blame] | 2330 | for (mode = 0; mode < MODE_MAX; mode++) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2331 | if (ssh->state->newkeys[mode] == NULL) |
Damien Miller | c31a0cd | 2014-05-15 14:37:39 +1000 | [diff] [blame] | 2332 | continue; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2333 | comp = &ssh->state->newkeys[mode]->comp; |
| 2334 | if (comp && comp->enabled && |
| 2335 | (r = ssh_packet_init_compression(ssh)) != 0) |
| 2336 | return r; |
Damien Miller | c31a0cd | 2014-05-15 14:37:39 +1000 | [diff] [blame] | 2337 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2338 | return 0; |
| 2339 | } |
| 2340 | |
| 2341 | /* Packet state (de-)serialization for privsep */ |
| 2342 | |
| 2343 | /* turn kex into a blob for packet state serialization */ |
| 2344 | static int |
| 2345 | kex_to_blob(struct sshbuf *m, struct kex *kex) |
| 2346 | { |
| 2347 | int r; |
| 2348 | |
| 2349 | if ((r = sshbuf_put_string(m, kex->session_id, |
| 2350 | kex->session_id_len)) != 0 || |
| 2351 | (r = sshbuf_put_u32(m, kex->we_need)) != 0 || |
| 2352 | (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 || |
| 2353 | (r = sshbuf_put_u32(m, kex->kex_type)) != 0 || |
| 2354 | (r = sshbuf_put_stringb(m, kex->my)) != 0 || |
| 2355 | (r = sshbuf_put_stringb(m, kex->peer)) != 0 || |
| 2356 | (r = sshbuf_put_u32(m, kex->flags)) != 0 || |
| 2357 | (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 || |
| 2358 | (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0) |
| 2359 | return r; |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
| 2363 | /* turn key exchange results into a blob for packet state serialization */ |
| 2364 | static int |
| 2365 | newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode) |
| 2366 | { |
| 2367 | struct sshbuf *b; |
| 2368 | struct sshcipher_ctx *cc; |
| 2369 | struct sshcomp *comp; |
| 2370 | struct sshenc *enc; |
| 2371 | struct sshmac *mac; |
| 2372 | struct newkeys *newkey; |
| 2373 | int r; |
| 2374 | |
| 2375 | if ((newkey = ssh->state->newkeys[mode]) == NULL) |
| 2376 | return SSH_ERR_INTERNAL_ERROR; |
| 2377 | enc = &newkey->enc; |
| 2378 | mac = &newkey->mac; |
| 2379 | comp = &newkey->comp; |
| 2380 | cc = (mode == MODE_OUT) ? &ssh->state->send_context : |
| 2381 | &ssh->state->receive_context; |
| 2382 | if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0) |
| 2383 | return r; |
| 2384 | if ((b = sshbuf_new()) == NULL) |
| 2385 | return SSH_ERR_ALLOC_FAIL; |
| 2386 | /* The cipher struct is constant and shared, you export pointer */ |
| 2387 | if ((r = sshbuf_put_cstring(b, enc->name)) != 0 || |
| 2388 | (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 || |
| 2389 | (r = sshbuf_put_u32(b, enc->enabled)) != 0 || |
| 2390 | (r = sshbuf_put_u32(b, enc->block_size)) != 0 || |
| 2391 | (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 || |
| 2392 | (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0) |
| 2393 | goto out; |
| 2394 | if (cipher_authlen(enc->cipher) == 0) { |
| 2395 | if ((r = sshbuf_put_cstring(b, mac->name)) != 0 || |
| 2396 | (r = sshbuf_put_u32(b, mac->enabled)) != 0 || |
| 2397 | (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0) |
| 2398 | goto out; |
| 2399 | } |
| 2400 | if ((r = sshbuf_put_u32(b, comp->type)) != 0 || |
| 2401 | (r = sshbuf_put_u32(b, comp->enabled)) != 0 || |
| 2402 | (r = sshbuf_put_cstring(b, comp->name)) != 0) |
| 2403 | goto out; |
| 2404 | r = sshbuf_put_stringb(m, b); |
| 2405 | out: |
| 2406 | if (b != NULL) |
| 2407 | sshbuf_free(b); |
| 2408 | return r; |
| 2409 | } |
| 2410 | |
| 2411 | /* serialize packet state into a blob */ |
| 2412 | int |
| 2413 | ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m) |
| 2414 | { |
| 2415 | struct session_state *state = ssh->state; |
| 2416 | u_char *p; |
| 2417 | size_t slen, rlen; |
| 2418 | int r, ssh1cipher; |
| 2419 | |
| 2420 | if (!compat20) { |
| 2421 | ssh1cipher = cipher_get_number(state->receive_context.cipher); |
| 2422 | slen = cipher_get_keyiv_len(&state->send_context); |
| 2423 | rlen = cipher_get_keyiv_len(&state->receive_context); |
| 2424 | if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 || |
| 2425 | (r = sshbuf_put_u32(m, ssh1cipher)) != 0 || |
| 2426 | (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 || |
| 2427 | (r = sshbuf_put_u32(m, slen)) != 0 || |
| 2428 | (r = sshbuf_reserve(m, slen, &p)) != 0 || |
| 2429 | (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 || |
| 2430 | (r = sshbuf_put_u32(m, rlen)) != 0 || |
| 2431 | (r = sshbuf_reserve(m, rlen, &p)) != 0 || |
| 2432 | (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0) |
| 2433 | return r; |
| 2434 | } else { |
| 2435 | if ((r = kex_to_blob(m, ssh->kex)) != 0 || |
| 2436 | (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 || |
| 2437 | (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 || |
markus@openbsd.org | 02db468 | 2015-02-13 18:57:00 +0000 | [diff] [blame] | 2438 | (r = sshbuf_put_u32(m, state->rekey_limit)) != 0 || |
| 2439 | (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 || |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2440 | (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 || |
| 2441 | (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 || |
| 2442 | (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 || |
| 2443 | (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 || |
| 2444 | (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 || |
| 2445 | (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 || |
| 2446 | (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 || |
| 2447 | (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0) |
| 2448 | return r; |
| 2449 | } |
| 2450 | |
| 2451 | slen = cipher_get_keycontext(&state->send_context, NULL); |
| 2452 | rlen = cipher_get_keycontext(&state->receive_context, NULL); |
| 2453 | if ((r = sshbuf_put_u32(m, slen)) != 0 || |
| 2454 | (r = sshbuf_reserve(m, slen, &p)) != 0) |
| 2455 | return r; |
| 2456 | if (cipher_get_keycontext(&state->send_context, p) != (int)slen) |
| 2457 | return SSH_ERR_INTERNAL_ERROR; |
| 2458 | if ((r = sshbuf_put_u32(m, rlen)) != 0 || |
| 2459 | (r = sshbuf_reserve(m, rlen, &p)) != 0) |
| 2460 | return r; |
| 2461 | if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen) |
| 2462 | return SSH_ERR_INTERNAL_ERROR; |
| 2463 | |
| 2464 | if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 || |
| 2465 | (r = sshbuf_put_stringb(m, state->input)) != 0 || |
| 2466 | (r = sshbuf_put_stringb(m, state->output)) != 0) |
| 2467 | return r; |
| 2468 | |
| 2469 | if (compat20) { |
| 2470 | if ((r = sshbuf_put_u64(m, get_sent_bytes())) != 0 || |
| 2471 | (r = sshbuf_put_u64(m, get_recv_bytes())) != 0) |
| 2472 | return r; |
| 2473 | } |
| 2474 | return 0; |
| 2475 | } |
| 2476 | |
| 2477 | /* restore key exchange results from blob for packet state de-serialization */ |
| 2478 | static int |
| 2479 | newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode) |
| 2480 | { |
| 2481 | struct sshbuf *b = NULL; |
| 2482 | struct sshcomp *comp; |
| 2483 | struct sshenc *enc; |
| 2484 | struct sshmac *mac; |
| 2485 | struct newkeys *newkey = NULL; |
| 2486 | size_t keylen, ivlen, maclen; |
| 2487 | int r; |
| 2488 | |
| 2489 | if ((newkey = calloc(1, sizeof(*newkey))) == NULL) { |
| 2490 | r = SSH_ERR_ALLOC_FAIL; |
| 2491 | goto out; |
| 2492 | } |
| 2493 | if ((r = sshbuf_froms(m, &b)) != 0) |
| 2494 | goto out; |
| 2495 | #ifdef DEBUG_PK |
| 2496 | sshbuf_dump(b, stderr); |
| 2497 | #endif |
| 2498 | enc = &newkey->enc; |
| 2499 | mac = &newkey->mac; |
| 2500 | comp = &newkey->comp; |
| 2501 | |
| 2502 | if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 || |
| 2503 | (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 || |
| 2504 | (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 || |
| 2505 | (r = sshbuf_get_u32(b, &enc->block_size)) != 0 || |
| 2506 | (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 || |
| 2507 | (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0) |
| 2508 | goto out; |
| 2509 | if (cipher_authlen(enc->cipher) == 0) { |
| 2510 | if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0) |
| 2511 | goto out; |
| 2512 | if ((r = mac_setup(mac, mac->name)) != 0) |
| 2513 | goto out; |
| 2514 | if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 || |
| 2515 | (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0) |
| 2516 | goto out; |
| 2517 | if (maclen > mac->key_len) { |
| 2518 | r = SSH_ERR_INVALID_FORMAT; |
| 2519 | goto out; |
| 2520 | } |
| 2521 | mac->key_len = maclen; |
| 2522 | } |
| 2523 | if ((r = sshbuf_get_u32(b, &comp->type)) != 0 || |
| 2524 | (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 || |
| 2525 | (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0) |
| 2526 | goto out; |
| 2527 | if (enc->name == NULL || |
| 2528 | cipher_by_name(enc->name) != enc->cipher) { |
| 2529 | r = SSH_ERR_INVALID_FORMAT; |
| 2530 | goto out; |
| 2531 | } |
| 2532 | if (sshbuf_len(b) != 0) { |
| 2533 | r = SSH_ERR_INVALID_FORMAT; |
| 2534 | goto out; |
| 2535 | } |
| 2536 | enc->key_len = keylen; |
| 2537 | enc->iv_len = ivlen; |
| 2538 | ssh->kex->newkeys[mode] = newkey; |
| 2539 | newkey = NULL; |
| 2540 | r = 0; |
| 2541 | out: |
| 2542 | if (newkey != NULL) |
| 2543 | free(newkey); |
| 2544 | if (b != NULL) |
| 2545 | sshbuf_free(b); |
| 2546 | return r; |
| 2547 | } |
| 2548 | |
| 2549 | /* restore kex from blob for packet state de-serialization */ |
| 2550 | static int |
| 2551 | kex_from_blob(struct sshbuf *m, struct kex **kexp) |
| 2552 | { |
| 2553 | struct kex *kex; |
| 2554 | int r; |
| 2555 | |
| 2556 | if ((kex = calloc(1, sizeof(struct kex))) == NULL || |
| 2557 | (kex->my = sshbuf_new()) == NULL || |
| 2558 | (kex->peer = sshbuf_new()) == NULL) { |
| 2559 | r = SSH_ERR_ALLOC_FAIL; |
| 2560 | goto out; |
| 2561 | } |
| 2562 | if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 || |
| 2563 | (r = sshbuf_get_u32(m, &kex->we_need)) != 0 || |
| 2564 | (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 || |
| 2565 | (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 || |
| 2566 | (r = sshbuf_get_stringb(m, kex->my)) != 0 || |
| 2567 | (r = sshbuf_get_stringb(m, kex->peer)) != 0 || |
| 2568 | (r = sshbuf_get_u32(m, &kex->flags)) != 0 || |
| 2569 | (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 || |
| 2570 | (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0) |
| 2571 | goto out; |
| 2572 | kex->server = 1; |
| 2573 | kex->done = 1; |
| 2574 | r = 0; |
| 2575 | out: |
| 2576 | if (r != 0 || kexp == NULL) { |
| 2577 | if (kex != NULL) { |
| 2578 | if (kex->my != NULL) |
| 2579 | sshbuf_free(kex->my); |
| 2580 | if (kex->peer != NULL) |
| 2581 | sshbuf_free(kex->peer); |
| 2582 | free(kex); |
| 2583 | } |
| 2584 | if (kexp != NULL) |
| 2585 | *kexp = NULL; |
| 2586 | } else { |
| 2587 | *kexp = kex; |
| 2588 | } |
| 2589 | return r; |
| 2590 | } |
| 2591 | |
| 2592 | /* |
| 2593 | * Restore packet state from content of blob 'm' (de-serialization). |
| 2594 | * Note that 'm' will be partially consumed on parsing or any other errors. |
| 2595 | */ |
| 2596 | int |
| 2597 | ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m) |
| 2598 | { |
| 2599 | struct session_state *state = ssh->state; |
| 2600 | const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output; |
| 2601 | size_t ssh1keylen, rlen, slen, ilen, olen; |
| 2602 | int r; |
| 2603 | u_int ssh1cipher = 0; |
| 2604 | u_int64_t sent_bytes = 0, recv_bytes = 0; |
| 2605 | |
| 2606 | if (!compat20) { |
| 2607 | if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 || |
| 2608 | (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 || |
| 2609 | (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 || |
| 2610 | (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 || |
| 2611 | (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0) |
| 2612 | return r; |
| 2613 | if (ssh1cipher > INT_MAX) |
| 2614 | return SSH_ERR_KEY_UNKNOWN_CIPHER; |
| 2615 | ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen, |
| 2616 | (int)ssh1cipher); |
| 2617 | if (cipher_get_keyiv_len(&state->send_context) != (int)slen || |
| 2618 | cipher_get_keyiv_len(&state->receive_context) != (int)rlen) |
| 2619 | return SSH_ERR_INVALID_FORMAT; |
| 2620 | if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 || |
| 2621 | (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0) |
| 2622 | return r; |
| 2623 | } else { |
| 2624 | if ((r = kex_from_blob(m, &ssh->kex)) != 0 || |
| 2625 | (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 || |
| 2626 | (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 || |
markus@openbsd.org | 02db468 | 2015-02-13 18:57:00 +0000 | [diff] [blame] | 2627 | (r = sshbuf_get_u32(m, &state->rekey_limit)) != 0 || |
| 2628 | (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 || |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2629 | (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 || |
| 2630 | (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 || |
| 2631 | (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 || |
| 2632 | (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 || |
| 2633 | (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 || |
| 2634 | (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 || |
| 2635 | (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 || |
| 2636 | (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0) |
| 2637 | return r; |
markus@openbsd.org | 02db468 | 2015-02-13 18:57:00 +0000 | [diff] [blame] | 2638 | /* |
| 2639 | * We set the time here so that in post-auth privsep slave we |
| 2640 | * count from the completion of the authentication. |
| 2641 | */ |
| 2642 | state->rekey_time = monotime(); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2643 | /* XXX ssh_set_newkeys overrides p_read.packets? XXX */ |
| 2644 | if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 || |
| 2645 | (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0) |
| 2646 | return r; |
| 2647 | } |
| 2648 | if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 || |
| 2649 | (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0) |
| 2650 | return r; |
| 2651 | if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen || |
| 2652 | cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen) |
| 2653 | return SSH_ERR_INVALID_FORMAT; |
| 2654 | cipher_set_keycontext(&state->send_context, keyout); |
| 2655 | cipher_set_keycontext(&state->receive_context, keyin); |
| 2656 | |
| 2657 | if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 || |
| 2658 | (r = ssh_packet_set_postauth(ssh)) != 0) |
| 2659 | return r; |
| 2660 | |
| 2661 | sshbuf_reset(state->input); |
| 2662 | sshbuf_reset(state->output); |
| 2663 | if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 || |
| 2664 | (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 || |
| 2665 | (r = sshbuf_put(state->input, input, ilen)) != 0 || |
| 2666 | (r = sshbuf_put(state->output, output, olen)) != 0) |
| 2667 | return r; |
| 2668 | |
| 2669 | if (compat20) { |
| 2670 | if ((r = sshbuf_get_u64(m, &sent_bytes)) != 0 || |
| 2671 | (r = sshbuf_get_u64(m, &recv_bytes)) != 0) |
| 2672 | return r; |
| 2673 | roam_set_bytes(sent_bytes, recv_bytes); |
| 2674 | } |
| 2675 | if (sshbuf_len(m)) |
| 2676 | return SSH_ERR_INVALID_FORMAT; |
| 2677 | debug3("%s: done", __func__); |
| 2678 | return 0; |
| 2679 | } |
| 2680 | |
| 2681 | /* NEW API */ |
| 2682 | |
| 2683 | /* put data to the outgoing packet */ |
| 2684 | |
| 2685 | int |
| 2686 | sshpkt_put(struct ssh *ssh, const void *v, size_t len) |
| 2687 | { |
| 2688 | return sshbuf_put(ssh->state->outgoing_packet, v, len); |
| 2689 | } |
| 2690 | |
| 2691 | int |
| 2692 | sshpkt_putb(struct ssh *ssh, const struct sshbuf *b) |
| 2693 | { |
| 2694 | return sshbuf_putb(ssh->state->outgoing_packet, b); |
| 2695 | } |
| 2696 | |
| 2697 | int |
| 2698 | sshpkt_put_u8(struct ssh *ssh, u_char val) |
| 2699 | { |
| 2700 | return sshbuf_put_u8(ssh->state->outgoing_packet, val); |
| 2701 | } |
| 2702 | |
| 2703 | int |
| 2704 | sshpkt_put_u32(struct ssh *ssh, u_int32_t val) |
| 2705 | { |
| 2706 | return sshbuf_put_u32(ssh->state->outgoing_packet, val); |
| 2707 | } |
| 2708 | |
| 2709 | int |
| 2710 | sshpkt_put_u64(struct ssh *ssh, u_int64_t val) |
| 2711 | { |
| 2712 | return sshbuf_put_u64(ssh->state->outgoing_packet, val); |
| 2713 | } |
| 2714 | |
| 2715 | int |
| 2716 | sshpkt_put_string(struct ssh *ssh, const void *v, size_t len) |
| 2717 | { |
| 2718 | return sshbuf_put_string(ssh->state->outgoing_packet, v, len); |
| 2719 | } |
| 2720 | |
| 2721 | int |
| 2722 | sshpkt_put_cstring(struct ssh *ssh, const void *v) |
| 2723 | { |
| 2724 | return sshbuf_put_cstring(ssh->state->outgoing_packet, v); |
| 2725 | } |
| 2726 | |
| 2727 | int |
| 2728 | sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v) |
| 2729 | { |
| 2730 | return sshbuf_put_stringb(ssh->state->outgoing_packet, v); |
| 2731 | } |
| 2732 | |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2733 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2734 | int |
| 2735 | sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g) |
| 2736 | { |
| 2737 | return sshbuf_put_ec(ssh->state->outgoing_packet, v, g); |
| 2738 | } |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2739 | #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2740 | |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2741 | #ifdef WITH_SSH1 |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2742 | int |
| 2743 | sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v) |
| 2744 | { |
| 2745 | return sshbuf_put_bignum1(ssh->state->outgoing_packet, v); |
| 2746 | } |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2747 | #endif /* WITH_SSH1 */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2748 | |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2749 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2750 | int |
| 2751 | sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v) |
| 2752 | { |
| 2753 | return sshbuf_put_bignum2(ssh->state->outgoing_packet, v); |
| 2754 | } |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2755 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2756 | |
| 2757 | /* fetch data from the incoming packet */ |
| 2758 | |
| 2759 | int |
| 2760 | sshpkt_get(struct ssh *ssh, void *valp, size_t len) |
| 2761 | { |
| 2762 | return sshbuf_get(ssh->state->incoming_packet, valp, len); |
| 2763 | } |
| 2764 | |
| 2765 | int |
| 2766 | sshpkt_get_u8(struct ssh *ssh, u_char *valp) |
| 2767 | { |
| 2768 | return sshbuf_get_u8(ssh->state->incoming_packet, valp); |
| 2769 | } |
| 2770 | |
| 2771 | int |
| 2772 | sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp) |
| 2773 | { |
| 2774 | return sshbuf_get_u32(ssh->state->incoming_packet, valp); |
| 2775 | } |
| 2776 | |
| 2777 | int |
| 2778 | sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp) |
| 2779 | { |
| 2780 | return sshbuf_get_u64(ssh->state->incoming_packet, valp); |
| 2781 | } |
| 2782 | |
| 2783 | int |
| 2784 | sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp) |
| 2785 | { |
| 2786 | return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp); |
| 2787 | } |
| 2788 | |
| 2789 | int |
| 2790 | sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp) |
| 2791 | { |
| 2792 | return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp); |
| 2793 | } |
| 2794 | |
| 2795 | int |
| 2796 | sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp) |
| 2797 | { |
| 2798 | return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp); |
| 2799 | } |
| 2800 | |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2801 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2802 | int |
| 2803 | sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g) |
| 2804 | { |
| 2805 | return sshbuf_get_ec(ssh->state->incoming_packet, v, g); |
| 2806 | } |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2807 | #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2808 | |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2809 | #ifdef WITH_SSH1 |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2810 | int |
| 2811 | sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v) |
| 2812 | { |
| 2813 | return sshbuf_get_bignum1(ssh->state->incoming_packet, v); |
| 2814 | } |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2815 | #endif /* WITH_SSH1 */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2816 | |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2817 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2818 | int |
| 2819 | sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v) |
| 2820 | { |
| 2821 | return sshbuf_get_bignum2(ssh->state->incoming_packet, v); |
| 2822 | } |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 2823 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2824 | |
| 2825 | int |
| 2826 | sshpkt_get_end(struct ssh *ssh) |
| 2827 | { |
| 2828 | if (sshbuf_len(ssh->state->incoming_packet) > 0) |
| 2829 | return SSH_ERR_UNEXPECTED_TRAILING_DATA; |
| 2830 | return 0; |
| 2831 | } |
| 2832 | |
| 2833 | const u_char * |
| 2834 | sshpkt_ptr(struct ssh *ssh, size_t *lenp) |
| 2835 | { |
| 2836 | if (lenp != NULL) |
| 2837 | *lenp = sshbuf_len(ssh->state->incoming_packet); |
| 2838 | return sshbuf_ptr(ssh->state->incoming_packet); |
| 2839 | } |
| 2840 | |
| 2841 | /* start a new packet */ |
| 2842 | |
| 2843 | int |
| 2844 | sshpkt_start(struct ssh *ssh, u_char type) |
| 2845 | { |
| 2846 | u_char buf[9]; |
| 2847 | int len; |
| 2848 | |
| 2849 | DBG(debug("packet_start[%d]", type)); |
| 2850 | len = compat20 ? 6 : 9; |
| 2851 | memset(buf, 0, len - 1); |
| 2852 | buf[len - 1] = type; |
| 2853 | sshbuf_reset(ssh->state->outgoing_packet); |
| 2854 | return sshbuf_put(ssh->state->outgoing_packet, buf, len); |
| 2855 | } |
| 2856 | |
| 2857 | /* send it */ |
| 2858 | |
| 2859 | int |
| 2860 | sshpkt_send(struct ssh *ssh) |
| 2861 | { |
| 2862 | if (compat20) |
| 2863 | return ssh_packet_send2(ssh); |
| 2864 | else |
| 2865 | return ssh_packet_send1(ssh); |
| 2866 | } |
| 2867 | |
| 2868 | int |
| 2869 | sshpkt_disconnect(struct ssh *ssh, const char *fmt,...) |
| 2870 | { |
| 2871 | char buf[1024]; |
| 2872 | va_list args; |
| 2873 | int r; |
| 2874 | |
| 2875 | va_start(args, fmt); |
| 2876 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 2877 | va_end(args); |
| 2878 | |
| 2879 | if (compat20) { |
| 2880 | if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 || |
| 2881 | (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 || |
| 2882 | (r = sshpkt_put_cstring(ssh, buf)) != 0 || |
| 2883 | (r = sshpkt_put_cstring(ssh, "")) != 0 || |
| 2884 | (r = sshpkt_send(ssh)) != 0) |
| 2885 | return r; |
| 2886 | } else { |
| 2887 | if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 || |
| 2888 | (r = sshpkt_put_cstring(ssh, buf)) != 0 || |
| 2889 | (r = sshpkt_send(ssh)) != 0) |
| 2890 | return r; |
| 2891 | } |
| 2892 | return 0; |
| 2893 | } |
| 2894 | |
| 2895 | /* roundup current message to pad bytes */ |
| 2896 | int |
| 2897 | sshpkt_add_padding(struct ssh *ssh, u_char pad) |
| 2898 | { |
| 2899 | ssh->state->extra_pad = pad; |
| 2900 | return 0; |
Damien Miller | c31a0cd | 2014-05-15 14:37:39 +1000 | [diff] [blame] | 2901 | } |