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