]>
Commit | Line | Data |
---|---|---|
dc1f1948 | 1 | /* $OpenBSD: packet.c,v 1.161 2009/05/25 06:48:01 andreas Exp $ */ |
8efc0c15 | 2 | /* |
5260325f | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
5 | * All rights reserved | |
5260325f | 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. | |
7e7327a1 | 8 | * |
bcbf86ec | 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". | |
14 | * | |
15 | * | |
7e7327a1 | 16 | * SSH2 packet format added by Markus Friedl. |
a96070d4 | 17 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
bcbf86ec | 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. | |
7e7327a1 | 27 | * |
bcbf86ec | 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. | |
5260325f | 38 | */ |
8efc0c15 | 39 | |
40 | #include "includes.h" | |
a3dcf543 | 41 | |
5b04a8bf | 42 | #include <sys/types.h> |
2f5b2528 | 43 | #include "openbsd-compat/sys-queue.h" |
536c14e8 | 44 | #include <sys/param.h> |
9794d008 | 45 | #include <sys/socket.h> |
e264ac72 | 46 | #ifdef HAVE_SYS_TIME_H |
47 | # include <sys/time.h> | |
48 | #endif | |
9794d008 | 49 | |
9794d008 | 50 | #include <netinet/in.h> |
a3dcf543 | 51 | #include <netinet/ip.h> |
c5bca5d4 | 52 | #include <arpa/inet.h> |
8efc0c15 | 53 | |
028094f4 | 54 | #include <errno.h> |
4c72fcfd | 55 | #include <stdarg.h> |
cf851879 | 56 | #include <stdio.h> |
ffa517a8 | 57 | #include <stdlib.h> |
00146caa | 58 | #include <string.h> |
5188ba17 | 59 | #include <unistd.h> |
31652869 | 60 | #include <signal.h> |
4c72fcfd | 61 | |
8efc0c15 | 62 | #include "xmalloc.h" |
63 | #include "buffer.h" | |
64 | #include "packet.h" | |
8efc0c15 | 65 | #include "crc32.h" |
8efc0c15 | 66 | #include "compress.h" |
67 | #include "deattack.h" | |
a5efa1bb | 68 | #include "channels.h" |
7e7327a1 | 69 | #include "compat.h" |
42f11eb2 | 70 | #include "ssh1.h" |
7e7327a1 | 71 | #include "ssh2.h" |
94ec8c6b | 72 | #include "cipher.h" |
31652869 | 73 | #include "key.h" |
7e7327a1 | 74 | #include "kex.h" |
b2552997 | 75 | #include "mac.h" |
42f11eb2 | 76 | #include "log.h" |
77 | #include "canohost.h" | |
2ac91be1 | 78 | #include "misc.h" |
9459414c | 79 | #include "ssh.h" |
7e7327a1 | 80 | |
81 | #ifdef PACKET_DEBUG | |
82 | #define DBG(x) x | |
83 | #else | |
84 | #define DBG(x) | |
85 | #endif | |
86 | ||
608bcf58 | 87 | #define PACKET_MAX_SIZE (256 * 1024) |
88 | ||
dc1f1948 | 89 | struct packet_state { |
90 | u_int32_t seqnr; | |
91 | u_int32_t packets; | |
92 | u_int64_t blocks; | |
93 | u_int64_t bytes; | |
94 | }; | |
8efc0c15 | 95 | |
dc1f1948 | 96 | struct packet { |
97 | TAILQ_ENTRY(packet) next; | |
98 | u_char type; | |
99 | Buffer payload; | |
100 | }; | |
8efc0c15 | 101 | |
dc1f1948 | 102 | struct session_state { |
103 | /* | |
104 | * This variable contains the file descriptors used for | |
105 | * communicating with the other side. connection_in is used for | |
106 | * reading; connection_out for writing. These can be the same | |
107 | * descriptor, in which case it is assumed to be a socket. | |
108 | */ | |
109 | int connection_in; | |
110 | int connection_out; | |
5260325f | 111 | |
dc1f1948 | 112 | /* Protocol flags for the remote side. */ |
113 | u_int remote_protocol_flags; | |
8efc0c15 | 114 | |
dc1f1948 | 115 | /* Encryption context for receiving data. Only used for decryption. */ |
116 | CipherContext receive_context; | |
8efc0c15 | 117 | |
dc1f1948 | 118 | /* Encryption context for sending data. Only used for encryption. */ |
119 | CipherContext send_context; | |
8efc0c15 | 120 | |
dc1f1948 | 121 | /* Buffer for raw input data from the socket. */ |
122 | Buffer input; | |
8efc0c15 | 123 | |
dc1f1948 | 124 | /* Buffer for raw output data going to the socket. */ |
125 | Buffer output; | |
8efc0c15 | 126 | |
dc1f1948 | 127 | /* Buffer for the partial outgoing packet being constructed. */ |
128 | Buffer outgoing_packet; | |
8efc0c15 | 129 | |
dc1f1948 | 130 | /* Buffer for the incoming packet currently being processed. */ |
131 | Buffer incoming_packet; | |
8efc0c15 | 132 | |
dc1f1948 | 133 | /* Scratch buffer for packet compression/decompression. */ |
134 | Buffer compression_buffer; | |
135 | int compression_buffer_ready; | |
9d6b7add | 136 | |
dc1f1948 | 137 | /* |
138 | * Flag indicating whether packet compression/decompression is | |
139 | * enabled. | |
140 | */ | |
141 | int packet_compression; | |
8efc0c15 | 142 | |
dc1f1948 | 143 | /* default maximum packet size */ |
144 | u_int max_packet_size; | |
8efc0c15 | 145 | |
dc1f1948 | 146 | /* Flag indicating whether this module has been initialized. */ |
147 | int initialized; | |
07200973 | 148 | |
dc1f1948 | 149 | /* Set to true if the connection is interactive. */ |
150 | int interactive_mode; | |
07200973 | 151 | |
dc1f1948 | 152 | /* Set to true if we are the server side. */ |
153 | int server_side; | |
ac47b626 | 154 | |
dc1f1948 | 155 | /* Set to true if we are authenticated. */ |
156 | int after_authentication; | |
bc2d97c8 | 157 | |
dc1f1948 | 158 | int keep_alive_timeouts; |
ffd7b36b | 159 | |
dc1f1948 | 160 | /* The maximum time that we will wait to send or receive a packet */ |
161 | int packet_timeout_ms; | |
7e7327a1 | 162 | |
dc1f1948 | 163 | /* Session key information for Encryption and MAC */ |
164 | Newkeys *newkeys[MODE_MAX]; | |
165 | struct packet_state p_read, p_send; | |
9459414c | 166 | |
dc1f1948 | 167 | u_int64_t max_blocks_in, max_blocks_out; |
168 | u_int32_t rekey_limit; | |
b4b701be | 169 | |
dc1f1948 | 170 | /* Session key for protocol v1 */ |
171 | u_char ssh1_key[SSH_SESSION_KEY_LENGTH]; | |
172 | u_int ssh1_keylen; | |
608bcf58 | 173 | |
dc1f1948 | 174 | /* roundup current message to extra_pad bytes */ |
175 | u_char extra_pad; | |
176 | ||
177 | /* XXX discard incoming data after MAC error */ | |
178 | u_int packet_discard; | |
179 | Mac *packet_discard_mac; | |
180 | ||
181 | /* Used in packet_read_poll2() */ | |
182 | u_int packlen; | |
183 | ||
184 | TAILQ_HEAD(, packet) outgoing; | |
ffd7b36b | 185 | }; |
dc1f1948 | 186 | |
187 | static struct session_state *active_state; | |
188 | ||
189 | static struct session_state * | |
190 | alloc_session_state() | |
191 | { | |
192 | struct session_state *s = xcalloc(1, sizeof(*s)); | |
193 | ||
194 | s->connection_in = -1; | |
195 | s->connection_out = -1; | |
196 | s->max_packet_size = 32768; | |
197 | s->packet_timeout_ms = -1; | |
198 | return s; | |
199 | } | |
ffd7b36b | 200 | |
aa3378df | 201 | /* |
202 | * Sets the descriptors used for communication. Disables encryption until | |
203 | * packet_set_encryption_key is called. | |
204 | */ | |
8efc0c15 | 205 | void |
206 | packet_set_connection(int fd_in, int fd_out) | |
207 | { | |
94ec8c6b | 208 | Cipher *none = cipher_by_name("none"); |
e1feb9bf | 209 | |
94ec8c6b | 210 | if (none == NULL) |
211 | fatal("packet_set_connection: cannot load cipher 'none'"); | |
dc1f1948 | 212 | if (active_state == NULL) |
213 | active_state = alloc_session_state(); | |
214 | active_state->connection_in = fd_in; | |
215 | active_state->connection_out = fd_out; | |
216 | cipher_init(&active_state->send_context, none, (const u_char *)"", | |
f6be21a0 | 217 | 0, NULL, 0, CIPHER_ENCRYPT); |
dc1f1948 | 218 | cipher_init(&active_state->receive_context, none, (const u_char *)"", |
f6be21a0 | 219 | 0, NULL, 0, CIPHER_DECRYPT); |
dc1f1948 | 220 | active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL; |
221 | if (!active_state->initialized) { | |
222 | active_state->initialized = 1; | |
223 | buffer_init(&active_state->input); | |
224 | buffer_init(&active_state->output); | |
225 | buffer_init(&active_state->outgoing_packet); | |
226 | buffer_init(&active_state->incoming_packet); | |
227 | TAILQ_INIT(&active_state->outgoing); | |
228 | active_state->p_send.packets = active_state->p_read.packets = 0; | |
5260325f | 229 | } |
8efc0c15 | 230 | } |
231 | ||
bc2d97c8 | 232 | void |
233 | packet_set_timeout(int timeout, int count) | |
234 | { | |
235 | if (timeout == 0 || count == 0) { | |
dc1f1948 | 236 | active_state->packet_timeout_ms = -1; |
bc2d97c8 | 237 | return; |
238 | } | |
239 | if ((INT_MAX / 1000) / count < timeout) | |
dc1f1948 | 240 | active_state->packet_timeout_ms = INT_MAX; |
bc2d97c8 | 241 | else |
dc1f1948 | 242 | active_state->packet_timeout_ms = timeout * count * 1000; |
bc2d97c8 | 243 | } |
244 | ||
608bcf58 | 245 | static void |
246 | packet_stop_discard(void) | |
247 | { | |
dc1f1948 | 248 | if (active_state->packet_discard_mac) { |
608bcf58 | 249 | char buf[1024]; |
250 | ||
251 | memset(buf, 'a', sizeof(buf)); | |
dc1f1948 | 252 | while (buffer_len(&active_state->incoming_packet) < |
253 | PACKET_MAX_SIZE) | |
254 | buffer_append(&active_state->incoming_packet, buf, | |
255 | sizeof(buf)); | |
256 | (void) mac_compute(active_state->packet_discard_mac, | |
257 | active_state->p_read.seqnr, | |
258 | buffer_ptr(&active_state->incoming_packet), | |
608bcf58 | 259 | PACKET_MAX_SIZE); |
260 | } | |
261 | logit("Finished discarding for %.200s", get_remote_ipaddr()); | |
262 | cleanup_exit(255); | |
263 | } | |
264 | ||
265 | static void | |
266 | packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard) | |
267 | { | |
8b773163 | 268 | if (enc == NULL || !cipher_is_cbc(enc->cipher)) |
608bcf58 | 269 | packet_disconnect("Packet corrupt"); |
270 | if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled) | |
dc1f1948 | 271 | active_state->packet_discard_mac = mac; |
272 | if (buffer_len(&active_state->input) >= discard) | |
608bcf58 | 273 | packet_stop_discard(); |
dc1f1948 | 274 | active_state->packet_discard = discard - |
275 | buffer_len(&active_state->input); | |
608bcf58 | 276 | } |
277 | ||
48e671d5 | 278 | /* Returns 1 if remote host is connected via socket, 0 if not. */ |
279 | ||
280 | int | |
d5bb9418 | 281 | packet_connection_is_on_socket(void) |
48e671d5 | 282 | { |
283 | struct sockaddr_storage from, to; | |
284 | socklen_t fromlen, tolen; | |
285 | ||
286 | /* filedescriptors in and out are the same, so it's a socket */ | |
dc1f1948 | 287 | if (active_state->connection_in == active_state->connection_out) |
48e671d5 | 288 | return 1; |
289 | fromlen = sizeof(from); | |
290 | memset(&from, 0, sizeof(from)); | |
dc1f1948 | 291 | if (getpeername(active_state->connection_in, (struct sockaddr *)&from, |
292 | &fromlen) < 0) | |
48e671d5 | 293 | return 0; |
294 | tolen = sizeof(to); | |
295 | memset(&to, 0, sizeof(to)); | |
dc1f1948 | 296 | if (getpeername(active_state->connection_out, (struct sockaddr *)&to, |
297 | &tolen) < 0) | |
48e671d5 | 298 | return 0; |
299 | if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0) | |
300 | return 0; | |
301 | if (from.ss_family != AF_INET && from.ss_family != AF_INET6) | |
302 | return 0; | |
303 | return 1; | |
304 | } | |
305 | ||
762715ce | 306 | /* |
e050d348 | 307 | * Exports an IV from the CipherContext required to export the key |
308 | * state back from the unprivileged child to the privileged parent | |
309 | * process. | |
310 | */ | |
311 | ||
312 | void | |
313 | packet_get_keyiv(int mode, u_char *iv, u_int len) | |
314 | { | |
315 | CipherContext *cc; | |
316 | ||
317 | if (mode == MODE_OUT) | |
dc1f1948 | 318 | cc = &active_state->send_context; |
e050d348 | 319 | else |
dc1f1948 | 320 | cc = &active_state->receive_context; |
e050d348 | 321 | |
322 | cipher_get_keyiv(cc, iv, len); | |
323 | } | |
324 | ||
325 | int | |
326 | packet_get_keycontext(int mode, u_char *dat) | |
327 | { | |
328 | CipherContext *cc; | |
762715ce | 329 | |
e050d348 | 330 | if (mode == MODE_OUT) |
dc1f1948 | 331 | cc = &active_state->send_context; |
e050d348 | 332 | else |
dc1f1948 | 333 | cc = &active_state->receive_context; |
e050d348 | 334 | |
335 | return (cipher_get_keycontext(cc, dat)); | |
336 | } | |
337 | ||
338 | void | |
339 | packet_set_keycontext(int mode, u_char *dat) | |
340 | { | |
341 | CipherContext *cc; | |
762715ce | 342 | |
e050d348 | 343 | if (mode == MODE_OUT) |
dc1f1948 | 344 | cc = &active_state->send_context; |
e050d348 | 345 | else |
dc1f1948 | 346 | cc = &active_state->receive_context; |
e050d348 | 347 | |
348 | cipher_set_keycontext(cc, dat); | |
349 | } | |
350 | ||
351 | int | |
352 | packet_get_keyiv_len(int mode) | |
353 | { | |
354 | CipherContext *cc; | |
355 | ||
356 | if (mode == MODE_OUT) | |
dc1f1948 | 357 | cc = &active_state->send_context; |
e050d348 | 358 | else |
dc1f1948 | 359 | cc = &active_state->receive_context; |
e050d348 | 360 | |
361 | return (cipher_get_keyiv_len(cc)); | |
362 | } | |
a7137f6b | 363 | |
e050d348 | 364 | void |
365 | packet_set_iv(int mode, u_char *dat) | |
366 | { | |
367 | CipherContext *cc; | |
368 | ||
369 | if (mode == MODE_OUT) | |
dc1f1948 | 370 | cc = &active_state->send_context; |
e050d348 | 371 | else |
dc1f1948 | 372 | cc = &active_state->receive_context; |
e050d348 | 373 | |
374 | cipher_set_keyiv(cc, dat); | |
375 | } | |
a7137f6b | 376 | |
e050d348 | 377 | int |
0daa6547 | 378 | packet_get_ssh1_cipher(void) |
e050d348 | 379 | { |
dc1f1948 | 380 | return (cipher_get_number(active_state->receive_context.cipher)); |
e050d348 | 381 | } |
382 | ||
ffd7b36b | 383 | void |
e8e08a80 | 384 | packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets, |
385 | u_int64_t *bytes) | |
e050d348 | 386 | { |
ffd7b36b | 387 | struct packet_state *state; |
388 | ||
dc1f1948 | 389 | state = (mode == MODE_IN) ? |
390 | &active_state->p_read : &active_state->p_send; | |
e8e08a80 | 391 | if (seqnr) |
392 | *seqnr = state->seqnr; | |
393 | if (blocks) | |
394 | *blocks = state->blocks; | |
395 | if (packets) | |
396 | *packets = state->packets; | |
397 | if (bytes) | |
398 | *bytes = state->bytes; | |
e050d348 | 399 | } |
400 | ||
401 | void | |
e8e08a80 | 402 | packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets, |
403 | u_int64_t bytes) | |
e050d348 | 404 | { |
ffd7b36b | 405 | struct packet_state *state; |
406 | ||
dc1f1948 | 407 | state = (mode == MODE_IN) ? |
408 | &active_state->p_read : &active_state->p_send; | |
ffd7b36b | 409 | state->seqnr = seqnr; |
410 | state->blocks = blocks; | |
411 | state->packets = packets; | |
e8e08a80 | 412 | state->bytes = bytes; |
e050d348 | 413 | } |
414 | ||
48e671d5 | 415 | /* returns 1 if connection is via ipv4 */ |
416 | ||
417 | int | |
d5bb9418 | 418 | packet_connection_is_ipv4(void) |
48e671d5 | 419 | { |
420 | struct sockaddr_storage to; | |
d45317d8 | 421 | socklen_t tolen = sizeof(to); |
48e671d5 | 422 | |
423 | memset(&to, 0, sizeof(to)); | |
dc1f1948 | 424 | if (getsockname(active_state->connection_out, (struct sockaddr *)&to, |
425 | &tolen) < 0) | |
48e671d5 | 426 | return 0; |
782e2103 | 427 | if (to.ss_family == AF_INET) |
428 | return 1; | |
429 | #ifdef IPV4_IN_IPV6 | |
aff51935 | 430 | if (to.ss_family == AF_INET6 && |
782e2103 | 431 | IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr)) |
432 | return 1; | |
433 | #endif | |
434 | return 0; | |
48e671d5 | 435 | } |
436 | ||
8efc0c15 | 437 | /* Sets the connection into non-blocking mode. */ |
438 | ||
439 | void | |
d5bb9418 | 440 | packet_set_nonblocking(void) |
8efc0c15 | 441 | { |
5260325f | 442 | /* Set the socket into non-blocking mode. */ |
dc1f1948 | 443 | set_nonblock(active_state->connection_in); |
8efc0c15 | 444 | |
dc1f1948 | 445 | if (active_state->connection_out != active_state->connection_in) |
446 | set_nonblock(active_state->connection_out); | |
8efc0c15 | 447 | } |
448 | ||
449 | /* Returns the socket used for reading. */ | |
450 | ||
451 | int | |
d5bb9418 | 452 | packet_get_connection_in(void) |
8efc0c15 | 453 | { |
dc1f1948 | 454 | return active_state->connection_in; |
8efc0c15 | 455 | } |
456 | ||
457 | /* Returns the descriptor used for writing. */ | |
458 | ||
459 | int | |
d5bb9418 | 460 | packet_get_connection_out(void) |
8efc0c15 | 461 | { |
dc1f1948 | 462 | return active_state->connection_out; |
8efc0c15 | 463 | } |
464 | ||
465 | /* Closes the connection and clears and frees internal data structures. */ | |
466 | ||
467 | void | |
d5bb9418 | 468 | packet_close(void) |
8efc0c15 | 469 | { |
dc1f1948 | 470 | if (!active_state->initialized) |
5260325f | 471 | return; |
dc1f1948 | 472 | active_state->initialized = 0; |
473 | if (active_state->connection_in == active_state->connection_out) { | |
474 | shutdown(active_state->connection_out, SHUT_RDWR); | |
475 | close(active_state->connection_out); | |
5260325f | 476 | } else { |
dc1f1948 | 477 | close(active_state->connection_in); |
478 | close(active_state->connection_out); | |
5260325f | 479 | } |
dc1f1948 | 480 | buffer_free(&active_state->input); |
481 | buffer_free(&active_state->output); | |
482 | buffer_free(&active_state->outgoing_packet); | |
483 | buffer_free(&active_state->incoming_packet); | |
484 | if (active_state->compression_buffer_ready) { | |
485 | buffer_free(&active_state->compression_buffer); | |
5260325f | 486 | buffer_compress_uninit(); |
487 | } | |
dc1f1948 | 488 | cipher_cleanup(&active_state->send_context); |
489 | cipher_cleanup(&active_state->receive_context); | |
8efc0c15 | 490 | } |
491 | ||
492 | /* Sets remote side protocol flags. */ | |
493 | ||
494 | void | |
1e3b8b07 | 495 | packet_set_protocol_flags(u_int protocol_flags) |
8efc0c15 | 496 | { |
dc1f1948 | 497 | active_state->remote_protocol_flags = protocol_flags; |
8efc0c15 | 498 | } |
499 | ||
500 | /* Returns the remote protocol flags set earlier by the above function. */ | |
501 | ||
1e3b8b07 | 502 | u_int |
d5bb9418 | 503 | packet_get_protocol_flags(void) |
8efc0c15 | 504 | { |
dc1f1948 | 505 | return active_state->remote_protocol_flags; |
8efc0c15 | 506 | } |
507 | ||
aa3378df | 508 | /* |
509 | * Starts packet compression from the next packet on in both directions. | |
510 | * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip. | |
511 | */ | |
8efc0c15 | 512 | |
396c147e | 513 | static void |
514 | packet_init_compression(void) | |
6ba22c93 | 515 | { |
dc1f1948 | 516 | if (active_state->compression_buffer_ready == 1) |
6ba22c93 | 517 | return; |
dc1f1948 | 518 | active_state->compression_buffer_ready = 1; |
519 | buffer_init(&active_state->compression_buffer); | |
6ba22c93 | 520 | } |
521 | ||
8efc0c15 | 522 | void |
523 | packet_start_compression(int level) | |
524 | { | |
dc1f1948 | 525 | if (active_state->packet_compression && !compat20) |
5260325f | 526 | fatal("Compression already enabled."); |
dc1f1948 | 527 | active_state->packet_compression = 1; |
6ba22c93 | 528 | packet_init_compression(); |
529 | buffer_compress_init_send(level); | |
530 | buffer_compress_init_recv(); | |
8efc0c15 | 531 | } |
532 | ||
aa3378df | 533 | /* |
534 | * Causes any further packets to be encrypted using the given key. The same | |
535 | * key is used for both sending and reception. However, both directions are | |
536 | * encrypted independently of each other. | |
537 | */ | |
9459414c | 538 | |
8efc0c15 | 539 | void |
1e3b8b07 | 540 | packet_set_encryption_key(const u_char *key, u_int keylen, |
94ec8c6b | 541 | int number) |
8efc0c15 | 542 | { |
94ec8c6b | 543 | Cipher *cipher = cipher_by_number(number); |
e1feb9bf | 544 | |
94ec8c6b | 545 | if (cipher == NULL) |
546 | fatal("packet_set_encryption_key: unknown cipher number %d", number); | |
7e7327a1 | 547 | if (keylen < 20) |
94ec8c6b | 548 | fatal("packet_set_encryption_key: keylen too small: %d", keylen); |
9459414c | 549 | if (keylen > SSH_SESSION_KEY_LENGTH) |
550 | fatal("packet_set_encryption_key: keylen too big: %d", keylen); | |
dc1f1948 | 551 | memcpy(active_state->ssh1_key, key, keylen); |
552 | active_state->ssh1_keylen = keylen; | |
553 | cipher_init(&active_state->send_context, cipher, key, keylen, NULL, | |
554 | 0, CIPHER_ENCRYPT); | |
555 | cipher_init(&active_state->receive_context, cipher, key, keylen, NULL, | |
556 | 0, CIPHER_DECRYPT); | |
8efc0c15 | 557 | } |
558 | ||
9459414c | 559 | u_int |
560 | packet_get_encryption_key(u_char *key) | |
561 | { | |
562 | if (key == NULL) | |
dc1f1948 | 563 | return (active_state->ssh1_keylen); |
564 | memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen); | |
565 | return (active_state->ssh1_keylen); | |
9459414c | 566 | } |
567 | ||
08dcb5d7 | 568 | /* Start constructing a packet to send. */ |
8efc0c15 | 569 | void |
08dcb5d7 | 570 | packet_start(u_char type) |
8efc0c15 | 571 | { |
08dcb5d7 | 572 | u_char buf[9]; |
573 | int len; | |
7e7327a1 | 574 | |
12bf85ed | 575 | DBG(debug("packet_start[%d]", type)); |
08dcb5d7 | 576 | len = compat20 ? 6 : 9; |
577 | memset(buf, 0, len - 1); | |
578 | buf[len - 1] = type; | |
dc1f1948 | 579 | buffer_clear(&active_state->outgoing_packet); |
580 | buffer_append(&active_state->outgoing_packet, buf, len); | |
7e7327a1 | 581 | } |
582 | ||
08dcb5d7 | 583 | /* Append payload. */ |
8efc0c15 | 584 | void |
585 | packet_put_char(int value) | |
586 | { | |
5260325f | 587 | char ch = value; |
e1feb9bf | 588 | |
dc1f1948 | 589 | buffer_append(&active_state->outgoing_packet, &ch, 1); |
8efc0c15 | 590 | } |
a7137f6b | 591 | |
8efc0c15 | 592 | void |
1e3b8b07 | 593 | packet_put_int(u_int value) |
8efc0c15 | 594 | { |
dc1f1948 | 595 | buffer_put_int(&active_state->outgoing_packet, value); |
8efc0c15 | 596 | } |
a7137f6b | 597 | |
8efc0c15 | 598 | void |
6c0fa2b1 | 599 | packet_put_string(const void *buf, u_int len) |
8efc0c15 | 600 | { |
dc1f1948 | 601 | buffer_put_string(&active_state->outgoing_packet, buf, len); |
8efc0c15 | 602 | } |
a7137f6b | 603 | |
7e7327a1 | 604 | void |
605 | packet_put_cstring(const char *str) | |
606 | { | |
dc1f1948 | 607 | buffer_put_cstring(&active_state->outgoing_packet, str); |
7e7327a1 | 608 | } |
a7137f6b | 609 | |
7e7327a1 | 610 | void |
6c0fa2b1 | 611 | packet_put_raw(const void *buf, u_int len) |
7e7327a1 | 612 | { |
dc1f1948 | 613 | buffer_append(&active_state->outgoing_packet, buf, len); |
7e7327a1 | 614 | } |
a7137f6b | 615 | |
8efc0c15 | 616 | void |
5260325f | 617 | packet_put_bignum(BIGNUM * value) |
8efc0c15 | 618 | { |
dc1f1948 | 619 | buffer_put_bignum(&active_state->outgoing_packet, value); |
8efc0c15 | 620 | } |
a7137f6b | 621 | |
7e7327a1 | 622 | void |
623 | packet_put_bignum2(BIGNUM * value) | |
624 | { | |
dc1f1948 | 625 | buffer_put_bignum2(&active_state->outgoing_packet, value); |
7e7327a1 | 626 | } |
8efc0c15 | 627 | |
aa3378df | 628 | /* |
629 | * Finalizes and sends the packet. If the encryption key has been set, | |
630 | * encrypts the packet before sending. | |
631 | */ | |
5260325f | 632 | |
396c147e | 633 | static void |
5ca51e19 | 634 | packet_send1(void) |
8efc0c15 | 635 | { |
a318bbf4 | 636 | u_char buf[8], *cp; |
5260325f | 637 | int i, padding, len; |
1e3b8b07 | 638 | u_int checksum; |
ca75d7de | 639 | u_int32_t rnd = 0; |
5260325f | 640 | |
aa3378df | 641 | /* |
642 | * If using packet compression, compress the payload of the outgoing | |
643 | * packet. | |
644 | */ | |
dc1f1948 | 645 | if (active_state->packet_compression) { |
646 | buffer_clear(&active_state->compression_buffer); | |
5260325f | 647 | /* Skip padding. */ |
dc1f1948 | 648 | buffer_consume(&active_state->outgoing_packet, 8); |
5260325f | 649 | /* padding */ |
dc1f1948 | 650 | buffer_append(&active_state->compression_buffer, |
651 | "\0\0\0\0\0\0\0\0", 8); | |
652 | buffer_compress(&active_state->outgoing_packet, | |
653 | &active_state->compression_buffer); | |
654 | buffer_clear(&active_state->outgoing_packet); | |
655 | buffer_append(&active_state->outgoing_packet, | |
656 | buffer_ptr(&active_state->compression_buffer), | |
657 | buffer_len(&active_state->compression_buffer)); | |
5260325f | 658 | } |
659 | /* Compute packet length without padding (add checksum, remove padding). */ | |
dc1f1948 | 660 | len = buffer_len(&active_state->outgoing_packet) + 4 - 8; |
5260325f | 661 | |
1d1ffb87 | 662 | /* Insert padding. Initialized to zero in packet_start1() */ |
5260325f | 663 | padding = 8 - len % 8; |
dc1f1948 | 664 | if (!active_state->send_context.plaintext) { |
665 | cp = buffer_ptr(&active_state->outgoing_packet); | |
5260325f | 666 | for (i = 0; i < padding; i++) { |
667 | if (i % 4 == 0) | |
ca75d7de | 668 | rnd = arc4random(); |
669 | cp[7 - i] = rnd & 0xff; | |
670 | rnd >>= 8; | |
5260325f | 671 | } |
672 | } | |
dc1f1948 | 673 | buffer_consume(&active_state->outgoing_packet, 8 - padding); |
5260325f | 674 | |
675 | /* Add check bytes. */ | |
dc1f1948 | 676 | checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet), |
677 | buffer_len(&active_state->outgoing_packet)); | |
51e7a012 | 678 | put_u32(buf, checksum); |
dc1f1948 | 679 | buffer_append(&active_state->outgoing_packet, buf, 4); |
8efc0c15 | 680 | |
681 | #ifdef PACKET_DEBUG | |
5260325f | 682 | fprintf(stderr, "packet_send plain: "); |
dc1f1948 | 683 | buffer_dump(&active_state->outgoing_packet); |
8efc0c15 | 684 | #endif |
685 | ||
5260325f | 686 | /* Append to output. */ |
51e7a012 | 687 | put_u32(buf, len); |
dc1f1948 | 688 | buffer_append(&active_state->output, buf, 4); |
689 | cp = buffer_append_space(&active_state->output, | |
690 | buffer_len(&active_state->outgoing_packet)); | |
691 | cipher_crypt(&active_state->send_context, cp, | |
692 | buffer_ptr(&active_state->outgoing_packet), | |
693 | buffer_len(&active_state->outgoing_packet)); | |
5260325f | 694 | |
8efc0c15 | 695 | #ifdef PACKET_DEBUG |
5260325f | 696 | fprintf(stderr, "encrypted: "); |
dc1f1948 | 697 | buffer_dump(&active_state->output); |
8efc0c15 | 698 | #endif |
dc1f1948 | 699 | active_state->p_send.packets++; |
700 | active_state->p_send.bytes += len + | |
701 | buffer_len(&active_state->outgoing_packet); | |
702 | buffer_clear(&active_state->outgoing_packet); | |
8efc0c15 | 703 | |
aa3378df | 704 | /* |
7b9b0103 | 705 | * Note that the packet is now only buffered in output. It won't be |
aa3378df | 706 | * actually sent until packet_write_wait or packet_write_poll is |
707 | * called. | |
708 | */ | |
8efc0c15 | 709 | } |
710 | ||
e050d348 | 711 | void |
d1ac6175 | 712 | set_newkeys(int mode) |
713 | { | |
714 | Enc *enc; | |
715 | Mac *mac; | |
716 | Comp *comp; | |
717 | CipherContext *cc; | |
ffd7b36b | 718 | u_int64_t *max_blocks; |
ca75d7de | 719 | int crypt_type; |
d1ac6175 | 720 | |
a77673cc | 721 | debug2("set_newkeys: mode %d", mode); |
d1ac6175 | 722 | |
3ee832e5 | 723 | if (mode == MODE_OUT) { |
dc1f1948 | 724 | cc = &active_state->send_context; |
ca75d7de | 725 | crypt_type = CIPHER_ENCRYPT; |
dc1f1948 | 726 | active_state->p_send.packets = active_state->p_send.blocks = 0; |
727 | max_blocks = &active_state->max_blocks_out; | |
3ee832e5 | 728 | } else { |
dc1f1948 | 729 | cc = &active_state->receive_context; |
ca75d7de | 730 | crypt_type = CIPHER_DECRYPT; |
dc1f1948 | 731 | active_state->p_read.packets = active_state->p_read.blocks = 0; |
732 | max_blocks = &active_state->max_blocks_in; | |
3ee832e5 | 733 | } |
dc1f1948 | 734 | if (active_state->newkeys[mode] != NULL) { |
a77673cc | 735 | debug("set_newkeys: rekeying"); |
3ee832e5 | 736 | cipher_cleanup(cc); |
dc1f1948 | 737 | enc = &active_state->newkeys[mode]->enc; |
738 | mac = &active_state->newkeys[mode]->mac; | |
739 | comp = &active_state->newkeys[mode]->comp; | |
f444d0f8 | 740 | mac_clear(mac); |
a7ca6275 | 741 | xfree(enc->name); |
742 | xfree(enc->iv); | |
743 | xfree(enc->key); | |
744 | xfree(mac->name); | |
745 | xfree(mac->key); | |
746 | xfree(comp->name); | |
dc1f1948 | 747 | xfree(active_state->newkeys[mode]); |
d1ac6175 | 748 | } |
dc1f1948 | 749 | active_state->newkeys[mode] = kex_get_newkeys(mode); |
750 | if (active_state->newkeys[mode] == NULL) | |
d1ac6175 | 751 | fatal("newkeys: no keys for mode %d", mode); |
dc1f1948 | 752 | enc = &active_state->newkeys[mode]->enc; |
753 | mac = &active_state->newkeys[mode]->mac; | |
754 | comp = &active_state->newkeys[mode]->comp; | |
f444d0f8 | 755 | if (mac_init(mac) == 0) |
d1ac6175 | 756 | mac->enabled = 1; |
757 | DBG(debug("cipher_init_context: %d", mode)); | |
3ee832e5 | 758 | cipher_init(cc, enc->cipher, enc->key, enc->key_len, |
ca75d7de | 759 | enc->iv, enc->block_size, crypt_type); |
e050d348 | 760 | /* Deleting the keys does not gain extra security */ |
761 | /* memset(enc->iv, 0, enc->block_size); | |
a3de8da1 | 762 | memset(enc->key, 0, enc->key_len); |
763 | memset(mac->key, 0, mac->key_len); */ | |
07200973 | 764 | if ((comp->type == COMP_ZLIB || |
dc1f1948 | 765 | (comp->type == COMP_DELAYED && |
766 | active_state->after_authentication)) && comp->enabled == 0) { | |
6ba22c93 | 767 | packet_init_compression(); |
768 | if (mode == MODE_OUT) | |
769 | buffer_compress_init_send(6); | |
770 | else | |
771 | buffer_compress_init_recv(); | |
d1ac6175 | 772 | comp->enabled = 1; |
d1ac6175 | 773 | } |
40729edd | 774 | /* |
775 | * The 2^(blocksize*2) limit is too expensive for 3DES, | |
776 | * blowfish, etc, so enforce a 1GB limit for small blocksizes. | |
777 | */ | |
778 | if (enc->block_size >= 16) | |
779 | *max_blocks = (u_int64_t)1 << (enc->block_size*2); | |
780 | else | |
781 | *max_blocks = ((u_int64_t)1 << 30) / enc->block_size; | |
dc1f1948 | 782 | if (active_state->rekey_limit) |
783 | *max_blocks = MIN(*max_blocks, | |
784 | active_state->rekey_limit / enc->block_size); | |
d1ac6175 | 785 | } |
786 | ||
07200973 | 787 | /* |
788 | * Delayed compression for SSH2 is enabled after authentication: | |
0e13ec0f | 789 | * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent, |
07200973 | 790 | * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received. |
791 | */ | |
792 | static void | |
793 | packet_enable_delayed_compress(void) | |
794 | { | |
795 | Comp *comp = NULL; | |
796 | int mode; | |
797 | ||
798 | /* | |
799 | * Remember that we are past the authentication step, so rekeying | |
800 | * with COMP_DELAYED will turn on compression immediately. | |
801 | */ | |
dc1f1948 | 802 | active_state->after_authentication = 1; |
07200973 | 803 | for (mode = 0; mode < MODE_MAX; mode++) { |
a29c9898 | 804 | /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */ |
dc1f1948 | 805 | if (active_state->newkeys[mode] == NULL) |
a29c9898 | 806 | continue; |
dc1f1948 | 807 | comp = &active_state->newkeys[mode]->comp; |
07200973 | 808 | if (comp && !comp->enabled && comp->type == COMP_DELAYED) { |
4145cbfa | 809 | packet_init_compression(); |
07200973 | 810 | if (mode == MODE_OUT) |
811 | buffer_compress_init_send(6); | |
812 | else | |
813 | buffer_compress_init_recv(); | |
814 | comp->enabled = 1; | |
815 | } | |
816 | } | |
817 | } | |
818 | ||
7e7327a1 | 819 | /* |
820 | * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue) | |
821 | */ | |
396c147e | 822 | static void |
ffd7b36b | 823 | packet_send2_wrapped(void) |
7e7327a1 | 824 | { |
a318bbf4 | 825 | u_char type, *cp, *macbuf = NULL; |
b4b701be | 826 | u_char padlen, pad; |
1e3b8b07 | 827 | u_int packet_length = 0; |
b4b701be | 828 | u_int i, len; |
ca75d7de | 829 | u_int32_t rnd = 0; |
7e7327a1 | 830 | Enc *enc = NULL; |
831 | Mac *mac = NULL; | |
832 | Comp *comp = NULL; | |
833 | int block_size; | |
834 | ||
dc1f1948 | 835 | if (active_state->newkeys[MODE_OUT] != NULL) { |
836 | enc = &active_state->newkeys[MODE_OUT]->enc; | |
837 | mac = &active_state->newkeys[MODE_OUT]->mac; | |
838 | comp = &active_state->newkeys[MODE_OUT]->comp; | |
7e7327a1 | 839 | } |
3ee832e5 | 840 | block_size = enc ? enc->block_size : 8; |
7e7327a1 | 841 | |
dc1f1948 | 842 | cp = buffer_ptr(&active_state->outgoing_packet); |
a318bbf4 | 843 | type = cp[5]; |
7e7327a1 | 844 | |
845 | #ifdef PACKET_DEBUG | |
846 | fprintf(stderr, "plain: "); | |
dc1f1948 | 847 | buffer_dump(&active_state->outgoing_packet); |
7e7327a1 | 848 | #endif |
849 | ||
850 | if (comp && comp->enabled) { | |
dc1f1948 | 851 | len = buffer_len(&active_state->outgoing_packet); |
7e7327a1 | 852 | /* skip header, compress only payload */ |
dc1f1948 | 853 | buffer_consume(&active_state->outgoing_packet, 5); |
854 | buffer_clear(&active_state->compression_buffer); | |
855 | buffer_compress(&active_state->outgoing_packet, | |
856 | &active_state->compression_buffer); | |
857 | buffer_clear(&active_state->outgoing_packet); | |
858 | buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5); | |
859 | buffer_append(&active_state->outgoing_packet, | |
860 | buffer_ptr(&active_state->compression_buffer), | |
861 | buffer_len(&active_state->compression_buffer)); | |
7e7327a1 | 862 | DBG(debug("compression: raw %d compressed %d", len, |
dc1f1948 | 863 | buffer_len(&active_state->outgoing_packet))); |
7e7327a1 | 864 | } |
865 | ||
866 | /* sizeof (packet_len + pad_len + payload) */ | |
dc1f1948 | 867 | len = buffer_len(&active_state->outgoing_packet); |
7e7327a1 | 868 | |
869 | /* | |
870 | * calc size of padding, alloc space, get random data, | |
871 | * minimum padding is 4 bytes | |
872 | */ | |
873 | padlen = block_size - (len % block_size); | |
874 | if (padlen < 4) | |
875 | padlen += block_size; | |
dc1f1948 | 876 | if (active_state->extra_pad) { |
b4b701be | 877 | /* will wrap if extra_pad+padlen > 255 */ |
dc1f1948 | 878 | active_state->extra_pad = |
879 | roundup(active_state->extra_pad, block_size); | |
880 | pad = active_state->extra_pad - | |
881 | ((len + padlen) % active_state->extra_pad); | |
a49dfdec | 882 | debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)", |
dc1f1948 | 883 | pad, len, padlen, active_state->extra_pad); |
b4b701be | 884 | padlen += pad; |
dc1f1948 | 885 | active_state->extra_pad = 0; |
b4b701be | 886 | } |
dc1f1948 | 887 | cp = buffer_append_space(&active_state->outgoing_packet, padlen); |
888 | if (enc && !active_state->send_context.plaintext) { | |
1d1ffb87 | 889 | /* random padding */ |
7e7327a1 | 890 | for (i = 0; i < padlen; i++) { |
891 | if (i % 4 == 0) | |
ca75d7de | 892 | rnd = arc4random(); |
893 | cp[i] = rnd & 0xff; | |
894 | rnd >>= 8; | |
7e7327a1 | 895 | } |
1d1ffb87 | 896 | } else { |
897 | /* clear padding */ | |
898 | memset(cp, 0, padlen); | |
7e7327a1 | 899 | } |
900 | /* packet_length includes payload, padding and padding length field */ | |
dc1f1948 | 901 | packet_length = buffer_len(&active_state->outgoing_packet) - 4; |
902 | cp = buffer_ptr(&active_state->outgoing_packet); | |
51e7a012 | 903 | put_u32(cp, packet_length); |
a318bbf4 | 904 | cp[4] = padlen; |
7e7327a1 | 905 | DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen)); |
906 | ||
907 | /* compute MAC over seqnr and packet(length fields, payload, padding) */ | |
908 | if (mac && mac->enabled) { | |
dc1f1948 | 909 | macbuf = mac_compute(mac, active_state->p_send.seqnr, |
910 | buffer_ptr(&active_state->outgoing_packet), | |
911 | buffer_len(&active_state->outgoing_packet)); | |
912 | DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr)); | |
7e7327a1 | 913 | } |
914 | /* encrypt packet and append to output buffer. */ | |
dc1f1948 | 915 | cp = buffer_append_space(&active_state->output, |
916 | buffer_len(&active_state->outgoing_packet)); | |
917 | cipher_crypt(&active_state->send_context, cp, | |
918 | buffer_ptr(&active_state->outgoing_packet), | |
919 | buffer_len(&active_state->outgoing_packet)); | |
7e7327a1 | 920 | /* append unencrypted MAC */ |
921 | if (mac && mac->enabled) | |
dc1f1948 | 922 | buffer_append(&active_state->output, macbuf, mac->mac_len); |
7e7327a1 | 923 | #ifdef PACKET_DEBUG |
924 | fprintf(stderr, "encrypted: "); | |
dc1f1948 | 925 | buffer_dump(&active_state->output); |
7e7327a1 | 926 | #endif |
6ae2364d | 927 | /* increment sequence number for outgoing packets */ |
dc1f1948 | 928 | if (++active_state->p_send.seqnr == 0) |
bbe88b6d | 929 | logit("outgoing seqnr wraps around"); |
dc1f1948 | 930 | if (++active_state->p_send.packets == 0) |
ffd7b36b | 931 | if (!(datafellows & SSH_BUG_NOREKEY)) |
932 | fatal("XXX too many packets with same key"); | |
dc1f1948 | 933 | active_state->p_send.blocks += (packet_length + 4) / block_size; |
934 | active_state->p_send.bytes += packet_length + 4; | |
935 | buffer_clear(&active_state->outgoing_packet); | |
7e7327a1 | 936 | |
d1ac6175 | 937 | if (type == SSH2_MSG_NEWKEYS) |
938 | set_newkeys(MODE_OUT); | |
dc1f1948 | 939 | else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side) |
07200973 | 940 | packet_enable_delayed_compress(); |
7e7327a1 | 941 | } |
942 | ||
ffd7b36b | 943 | static void |
944 | packet_send2(void) | |
945 | { | |
946 | static int rekeying = 0; | |
947 | struct packet *p; | |
948 | u_char type, *cp; | |
949 | ||
dc1f1948 | 950 | cp = buffer_ptr(&active_state->outgoing_packet); |
ffd7b36b | 951 | type = cp[5]; |
952 | ||
953 | /* during rekeying we can only send key exchange messages */ | |
954 | if (rekeying) { | |
955 | if (!((type >= SSH2_MSG_TRANSPORT_MIN) && | |
956 | (type <= SSH2_MSG_TRANSPORT_MAX))) { | |
957 | debug("enqueue packet: %u", type); | |
958 | p = xmalloc(sizeof(*p)); | |
959 | p->type = type; | |
dc1f1948 | 960 | memcpy(&p->payload, &active_state->outgoing_packet, |
961 | sizeof(Buffer)); | |
962 | buffer_init(&active_state->outgoing_packet); | |
963 | TAILQ_INSERT_TAIL(&active_state->outgoing, p, next); | |
ffd7b36b | 964 | return; |
965 | } | |
966 | } | |
967 | ||
968 | /* rekeying starts with sending KEXINIT */ | |
969 | if (type == SSH2_MSG_KEXINIT) | |
970 | rekeying = 1; | |
971 | ||
972 | packet_send2_wrapped(); | |
973 | ||
974 | /* after a NEWKEYS message we can send the complete queue */ | |
975 | if (type == SSH2_MSG_NEWKEYS) { | |
976 | rekeying = 0; | |
dc1f1948 | 977 | while ((p = TAILQ_FIRST(&active_state->outgoing))) { |
ffd7b36b | 978 | type = p->type; |
979 | debug("dequeue packet: %u", type); | |
dc1f1948 | 980 | buffer_free(&active_state->outgoing_packet); |
981 | memcpy(&active_state->outgoing_packet, &p->payload, | |
ffd7b36b | 982 | sizeof(Buffer)); |
dc1f1948 | 983 | TAILQ_REMOVE(&active_state->outgoing, p, next); |
ffd7b36b | 984 | xfree(p); |
985 | packet_send2_wrapped(); | |
986 | } | |
987 | } | |
988 | } | |
989 | ||
7e7327a1 | 990 | void |
d5bb9418 | 991 | packet_send(void) |
7e7327a1 | 992 | { |
08dcb5d7 | 993 | if (compat20) |
7e7327a1 | 994 | packet_send2(); |
995 | else | |
996 | packet_send1(); | |
997 | DBG(debug("packet_send done")); | |
998 | } | |
999 | ||
aa3378df | 1000 | /* |
1001 | * Waits until a packet has been received, and returns its type. Note that | |
1002 | * no other data is processed until this returns, so this function should not | |
1003 | * be used during the interactive session. | |
1004 | */ | |
8efc0c15 | 1005 | |
1006 | int | |
54a5250f | 1007 | packet_read_seqnr(u_int32_t *seqnr_p) |
8efc0c15 | 1008 | { |
bc2d97c8 | 1009 | int type, len, ret, ms_remain; |
20e04e90 | 1010 | fd_set *setp; |
5260325f | 1011 | char buf[8192]; |
bc2d97c8 | 1012 | struct timeval timeout, start, *timeoutp = NULL; |
5260325f | 1013 | |
849d3ceb | 1014 | DBG(debug("packet_read()")); |
1015 | ||
dc1f1948 | 1016 | setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1, |
1017 | NFDBITS), sizeof(fd_mask)); | |
20e04e90 | 1018 | |
5260325f | 1019 | /* Since we are blocking, ensure that all written packets have been sent. */ |
1020 | packet_write_wait(); | |
1021 | ||
1022 | /* Stay in the loop until we have received a complete packet. */ | |
1023 | for (;;) { | |
1024 | /* Try to read a packet from the buffer. */ | |
54a5250f | 1025 | type = packet_read_poll_seqnr(seqnr_p); |
08dcb5d7 | 1026 | if (!compat20 && ( |
1d1ffb87 | 1027 | type == SSH_SMSG_SUCCESS |
5260325f | 1028 | || type == SSH_SMSG_FAILURE |
1029 | || type == SSH_CMSG_EOF | |
1d1ffb87 | 1030 | || type == SSH_CMSG_EXIT_CONFIRMATION)) |
95500969 | 1031 | packet_check_eom(); |
5260325f | 1032 | /* If we got a packet, return it. */ |
20e04e90 | 1033 | if (type != SSH_MSG_NONE) { |
1034 | xfree(setp); | |
5260325f | 1035 | return type; |
20e04e90 | 1036 | } |
aa3378df | 1037 | /* |
1038 | * Otherwise, wait for some data to arrive, add it to the | |
1039 | * buffer, and try again. | |
1040 | */ | |
dc1f1948 | 1041 | memset(setp, 0, howmany(active_state->connection_in + 1, |
1042 | NFDBITS) * sizeof(fd_mask)); | |
1043 | FD_SET(active_state->connection_in, setp); | |
aa3378df | 1044 | |
dc1f1948 | 1045 | if (active_state->packet_timeout_ms > 0) { |
1046 | ms_remain = active_state->packet_timeout_ms; | |
bc2d97c8 | 1047 | timeoutp = &timeout; |
1048 | } | |
5260325f | 1049 | /* Wait for some data to arrive. */ |
bc2d97c8 | 1050 | for (;;) { |
dc1f1948 | 1051 | if (active_state->packet_timeout_ms != -1) { |
bc2d97c8 | 1052 | ms_to_timeval(&timeout, ms_remain); |
1053 | gettimeofday(&start, NULL); | |
1054 | } | |
dc1f1948 | 1055 | if ((ret = select(active_state->connection_in + 1, setp, |
1056 | NULL, NULL, timeoutp)) >= 0) | |
bc2d97c8 | 1057 | break; |
dc1f1948 | 1058 | if (errno != EAGAIN && errno != EINTR && |
5a0c8771 | 1059 | errno != EWOULDBLOCK) |
bc2d97c8 | 1060 | break; |
dc1f1948 | 1061 | if (active_state->packet_timeout_ms == -1) |
bc2d97c8 | 1062 | continue; |
1063 | ms_subtract_diff(&start, &ms_remain); | |
1064 | if (ms_remain <= 0) { | |
1065 | ret = 0; | |
1066 | break; | |
1067 | } | |
1068 | } | |
1069 | if (ret == 0) { | |
1070 | logit("Connection to %.200s timed out while " | |
1071 | "waiting to read", get_remote_ipaddr()); | |
1072 | cleanup_exit(255); | |
1073 | } | |
5260325f | 1074 | /* Read data from the socket. */ |
dc1f1948 | 1075 | len = read(active_state->connection_in, buf, sizeof(buf)); |
89cafde6 | 1076 | if (len == 0) { |
bbe88b6d | 1077 | logit("Connection closed by %.200s", get_remote_ipaddr()); |
2362db19 | 1078 | cleanup_exit(255); |
89cafde6 | 1079 | } |
5260325f | 1080 | if (len < 0) |
1081 | fatal("Read from socket failed: %.100s", strerror(errno)); | |
1082 | /* Append it to the buffer. */ | |
1083 | packet_process_incoming(buf, len); | |
1084 | } | |
1085 | /* NOTREACHED */ | |
8efc0c15 | 1086 | } |
1087 | ||
24ca6821 | 1088 | int |
54a5250f | 1089 | packet_read(void) |
24ca6821 | 1090 | { |
54a5250f | 1091 | return packet_read_seqnr(NULL); |
24ca6821 | 1092 | } |
1093 | ||
aa3378df | 1094 | /* |
1095 | * Waits until a packet has been received, verifies that its type matches | |
1096 | * that given, and gives a fatal error and exits if there is a mismatch. | |
1097 | */ | |
8efc0c15 | 1098 | |
1099 | void | |
54a5250f | 1100 | packet_read_expect(int expected_type) |
8efc0c15 | 1101 | { |
5260325f | 1102 | int type; |
8efc0c15 | 1103 | |
54a5250f | 1104 | type = packet_read(); |
5260325f | 1105 | if (type != expected_type) |
1106 | packet_disconnect("Protocol error: expected packet type %d, got %d", | |
7e7327a1 | 1107 | expected_type, type); |
8efc0c15 | 1108 | } |
1109 | ||
1110 | /* Checks if a full packet is available in the data received so far via | |
5260325f | 1111 | * packet_process_incoming. If so, reads the packet; otherwise returns |
1112 | * SSH_MSG_NONE. This does not wait for data from the connection. | |
1113 | * | |
aacfb17b | 1114 | * SSH_MSG_DISCONNECT is handled specially here. Also, |
1115 | * SSH_MSG_IGNORE messages are skipped by this function and are never returned | |
1116 | * to higher levels. | |
5260325f | 1117 | */ |
8efc0c15 | 1118 | |
396c147e | 1119 | static int |
54a5250f | 1120 | packet_read_poll1(void) |
8efc0c15 | 1121 | { |
1e3b8b07 | 1122 | u_int len, padded_len; |
a318bbf4 | 1123 | u_char *cp, type; |
1e3b8b07 | 1124 | u_int checksum, stored_checksum; |
5260325f | 1125 | |
5260325f | 1126 | /* Check if input size is less than minimum packet size. */ |
dc1f1948 | 1127 | if (buffer_len(&active_state->input) < 4 + 8) |
5260325f | 1128 | return SSH_MSG_NONE; |
1129 | /* Get length of incoming packet. */ | |
dc1f1948 | 1130 | cp = buffer_ptr(&active_state->input); |
51e7a012 | 1131 | len = get_u32(cp); |
5260325f | 1132 | if (len < 1 + 2 + 2 || len > 256 * 1024) |
2fe3c2db | 1133 | packet_disconnect("Bad packet length %u.", len); |
5260325f | 1134 | padded_len = (len + 8) & ~7; |
1135 | ||
1136 | /* Check if the packet has been entirely received. */ | |
dc1f1948 | 1137 | if (buffer_len(&active_state->input) < 4 + padded_len) |
5260325f | 1138 | return SSH_MSG_NONE; |
1139 | ||
1140 | /* The entire packet is in buffer. */ | |
1141 | ||
1142 | /* Consume packet length. */ | |
dc1f1948 | 1143 | buffer_consume(&active_state->input, 4); |
5260325f | 1144 | |
08dcb5d7 | 1145 | /* |
1146 | * Cryptographic attack detector for ssh | |
1147 | * (C)1998 CORE-SDI, Buenos Aires Argentina | |
1148 | * Ariel Futoransky(futo@core-sdi.com) | |
1149 | */ | |
dc1f1948 | 1150 | if (!active_state->receive_context.plaintext) { |
1151 | switch (detect_attack(buffer_ptr(&active_state->input), | |
1152 | padded_len)) { | |
f0d0e025 | 1153 | case DEATTACK_DETECTED: |
1154 | packet_disconnect("crc32 compensation attack: " | |
1155 | "network attack detected"); | |
1156 | case DEATTACK_DOS_DETECTED: | |
1157 | packet_disconnect("deattack denial of " | |
1158 | "service detected"); | |
1159 | } | |
1160 | } | |
08dcb5d7 | 1161 | |
1162 | /* Decrypt data to incoming_packet. */ | |
dc1f1948 | 1163 | buffer_clear(&active_state->incoming_packet); |
1164 | cp = buffer_append_space(&active_state->incoming_packet, padded_len); | |
1165 | cipher_crypt(&active_state->receive_context, cp, | |
1166 | buffer_ptr(&active_state->input), padded_len); | |
08dcb5d7 | 1167 | |
dc1f1948 | 1168 | buffer_consume(&active_state->input, padded_len); |
8efc0c15 | 1169 | |
1170 | #ifdef PACKET_DEBUG | |
5260325f | 1171 | fprintf(stderr, "read_poll plain: "); |
dc1f1948 | 1172 | buffer_dump(&active_state->incoming_packet); |
8efc0c15 | 1173 | #endif |
5260325f | 1174 | |
1175 | /* Compute packet checksum. */ | |
dc1f1948 | 1176 | checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet), |
1177 | buffer_len(&active_state->incoming_packet) - 4); | |
5260325f | 1178 | |
1179 | /* Skip padding. */ | |
dc1f1948 | 1180 | buffer_consume(&active_state->incoming_packet, 8 - len % 8); |
5260325f | 1181 | |
1182 | /* Test check bytes. */ | |
dc1f1948 | 1183 | if (len != buffer_len(&active_state->incoming_packet)) |
24ca6821 | 1184 | packet_disconnect("packet_read_poll1: len %d != buffer_len %d.", |
dc1f1948 | 1185 | len, buffer_len(&active_state->incoming_packet)); |
5260325f | 1186 | |
dc1f1948 | 1187 | cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4; |
51e7a012 | 1188 | stored_checksum = get_u32(cp); |
5260325f | 1189 | if (checksum != stored_checksum) |
1190 | packet_disconnect("Corrupted check bytes on input."); | |
dc1f1948 | 1191 | buffer_consume_end(&active_state->incoming_packet, 4); |
1192 | ||
1193 | if (active_state->packet_compression) { | |
1194 | buffer_clear(&active_state->compression_buffer); | |
1195 | buffer_uncompress(&active_state->incoming_packet, | |
1196 | &active_state->compression_buffer); | |
1197 | buffer_clear(&active_state->incoming_packet); | |
1198 | buffer_append(&active_state->incoming_packet, | |
1199 | buffer_ptr(&active_state->compression_buffer), | |
1200 | buffer_len(&active_state->compression_buffer)); | |
5260325f | 1201 | } |
dc1f1948 | 1202 | active_state->p_read.packets++; |
1203 | active_state->p_read.bytes += padded_len + 4; | |
1204 | type = buffer_get_char(&active_state->incoming_packet); | |
750bbb35 | 1205 | if (type < SSH_MSG_MIN || type > SSH_MSG_MAX) |
1206 | packet_disconnect("Invalid ssh1 packet type: %d", type); | |
08dcb5d7 | 1207 | return type; |
5260325f | 1208 | } |
1209 | ||
396c147e | 1210 | static int |
54a5250f | 1211 | packet_read_poll2(u_int32_t *seqnr_p) |
7e7327a1 | 1212 | { |
1e3b8b07 | 1213 | u_int padlen, need; |
a318bbf4 | 1214 | u_char *macbuf, *cp, type; |
2ceb8101 | 1215 | u_int maclen, block_size; |
7e7327a1 | 1216 | Enc *enc = NULL; |
1217 | Mac *mac = NULL; | |
1218 | Comp *comp = NULL; | |
1219 | ||
dc1f1948 | 1220 | if (active_state->packet_discard) |
608bcf58 | 1221 | return SSH_MSG_NONE; |
1222 | ||
dc1f1948 | 1223 | if (active_state->newkeys[MODE_IN] != NULL) { |
1224 | enc = &active_state->newkeys[MODE_IN]->enc; | |
1225 | mac = &active_state->newkeys[MODE_IN]->mac; | |
1226 | comp = &active_state->newkeys[MODE_IN]->comp; | |
7e7327a1 | 1227 | } |
1228 | maclen = mac && mac->enabled ? mac->mac_len : 0; | |
3ee832e5 | 1229 | block_size = enc ? enc->block_size : 8; |
7e7327a1 | 1230 | |
dc1f1948 | 1231 | if (active_state->packlen == 0) { |
7e7327a1 | 1232 | /* |
1233 | * check if input size is less than the cipher block size, | |
1234 | * decrypt first block and extract length of incoming packet | |
1235 | */ | |
dc1f1948 | 1236 | if (buffer_len(&active_state->input) < block_size) |
7e7327a1 | 1237 | return SSH_MSG_NONE; |
dc1f1948 | 1238 | buffer_clear(&active_state->incoming_packet); |
1239 | cp = buffer_append_space(&active_state->incoming_packet, | |
7e7327a1 | 1240 | block_size); |
dc1f1948 | 1241 | cipher_crypt(&active_state->receive_context, cp, |
1242 | buffer_ptr(&active_state->input), block_size); | |
1243 | cp = buffer_ptr(&active_state->incoming_packet); | |
1244 | active_state->packlen = get_u32(cp); | |
1245 | if (active_state->packlen < 1 + 4 || | |
1246 | active_state->packlen > PACKET_MAX_SIZE) { | |
9adbb4a4 | 1247 | #ifdef PACKET_DEBUG |
dc1f1948 | 1248 | buffer_dump(&active_state->incoming_packet); |
9adbb4a4 | 1249 | #endif |
dc1f1948 | 1250 | logit("Bad packet length %u.", active_state->packlen); |
1251 | packet_start_discard(enc, mac, active_state->packlen, | |
608bcf58 | 1252 | PACKET_MAX_SIZE); |
1253 | return SSH_MSG_NONE; | |
7e7327a1 | 1254 | } |
dc1f1948 | 1255 | DBG(debug("input: packet len %u", active_state->packlen+4)); |
1256 | buffer_consume(&active_state->input, block_size); | |
7e7327a1 | 1257 | } |
1258 | /* we have a partial packet of block_size bytes */ | |
dc1f1948 | 1259 | need = 4 + active_state->packlen - block_size; |
7e7327a1 | 1260 | DBG(debug("partial packet %d, need %d, maclen %d", block_size, |
1261 | need, maclen)); | |
66af1c21 | 1262 | if (need % block_size != 0) { |
1263 | logit("padding error: need %d block %d mod %d", | |
7e7327a1 | 1264 | need, block_size, need % block_size); |
dc1f1948 | 1265 | packet_start_discard(enc, mac, active_state->packlen, |
608bcf58 | 1266 | PACKET_MAX_SIZE - block_size); |
1267 | return SSH_MSG_NONE; | |
66af1c21 | 1268 | } |
7e7327a1 | 1269 | /* |
1270 | * check if the entire packet has been received and | |
1271 | * decrypt into incoming_packet | |
1272 | */ | |
dc1f1948 | 1273 | if (buffer_len(&active_state->input) < need + maclen) |
7e7327a1 | 1274 | return SSH_MSG_NONE; |
1275 | #ifdef PACKET_DEBUG | |
1276 | fprintf(stderr, "read_poll enc/full: "); | |
dc1f1948 | 1277 | buffer_dump(&active_state->input); |
7e7327a1 | 1278 | #endif |
dc1f1948 | 1279 | cp = buffer_append_space(&active_state->incoming_packet, need); |
1280 | cipher_crypt(&active_state->receive_context, cp, | |
1281 | buffer_ptr(&active_state->input), need); | |
1282 | buffer_consume(&active_state->input, need); | |
7e7327a1 | 1283 | /* |
1284 | * compute MAC over seqnr and packet, | |
1285 | * increment sequence number for incoming packet | |
1286 | */ | |
6ae2364d | 1287 | if (mac && mac->enabled) { |
dc1f1948 | 1288 | macbuf = mac_compute(mac, active_state->p_read.seqnr, |
1289 | buffer_ptr(&active_state->incoming_packet), | |
1290 | buffer_len(&active_state->incoming_packet)); | |
1291 | if (memcmp(macbuf, buffer_ptr(&active_state->input), | |
1292 | mac->mac_len) != 0) { | |
608bcf58 | 1293 | logit("Corrupted MAC on input."); |
1294 | if (need > PACKET_MAX_SIZE) | |
1295 | fatal("internal error need %d", need); | |
dc1f1948 | 1296 | packet_start_discard(enc, mac, active_state->packlen, |
608bcf58 | 1297 | PACKET_MAX_SIZE - need); |
1298 | return SSH_MSG_NONE; | |
1299 | } | |
1300 | ||
dc1f1948 | 1301 | DBG(debug("MAC #%d ok", active_state->p_read.seqnr)); |
1302 | buffer_consume(&active_state->input, mac->mac_len); | |
7e7327a1 | 1303 | } |
608bcf58 | 1304 | /* XXX now it's safe to use fatal/packet_disconnect */ |
24ca6821 | 1305 | if (seqnr_p != NULL) |
dc1f1948 | 1306 | *seqnr_p = active_state->p_read.seqnr; |
1307 | if (++active_state->p_read.seqnr == 0) | |
bbe88b6d | 1308 | logit("incoming seqnr wraps around"); |
dc1f1948 | 1309 | if (++active_state->p_read.packets == 0) |
ffd7b36b | 1310 | if (!(datafellows & SSH_BUG_NOREKEY)) |
1311 | fatal("XXX too many packets with same key"); | |
dc1f1948 | 1312 | active_state->p_read.blocks += (active_state->packlen + 4) / block_size; |
1313 | active_state->p_read.bytes += active_state->packlen + 4; | |
7e7327a1 | 1314 | |
1315 | /* get padlen */ | |
dc1f1948 | 1316 | cp = buffer_ptr(&active_state->incoming_packet); |
a318bbf4 | 1317 | padlen = cp[4]; |
7e7327a1 | 1318 | DBG(debug("input: padlen %d", padlen)); |
1319 | if (padlen < 4) | |
1320 | packet_disconnect("Corrupted padlen %d on input.", padlen); | |
1321 | ||
1322 | /* skip packet size + padlen, discard padding */ | |
dc1f1948 | 1323 | buffer_consume(&active_state->incoming_packet, 4 + 1); |
1324 | buffer_consume_end(&active_state->incoming_packet, padlen); | |
7e7327a1 | 1325 | |
dc1f1948 | 1326 | DBG(debug("input: len before de-compress %d", |
1327 | buffer_len(&active_state->incoming_packet))); | |
7e7327a1 | 1328 | if (comp && comp->enabled) { |
dc1f1948 | 1329 | buffer_clear(&active_state->compression_buffer); |
1330 | buffer_uncompress(&active_state->incoming_packet, | |
1331 | &active_state->compression_buffer); | |
1332 | buffer_clear(&active_state->incoming_packet); | |
1333 | buffer_append(&active_state->incoming_packet, | |
1334 | buffer_ptr(&active_state->compression_buffer), | |
1335 | buffer_len(&active_state->compression_buffer)); | |
e1feb9bf | 1336 | DBG(debug("input: len after de-compress %d", |
dc1f1948 | 1337 | buffer_len(&active_state->incoming_packet))); |
7e7327a1 | 1338 | } |
1339 | /* | |
1340 | * get packet type, implies consume. | |
1341 | * return length of payload (without type field) | |
1342 | */ | |
dc1f1948 | 1343 | type = buffer_get_char(&active_state->incoming_packet); |
750bbb35 | 1344 | if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN) |
1345 | packet_disconnect("Invalid ssh2 packet type: %d", type); | |
d1ac6175 | 1346 | if (type == SSH2_MSG_NEWKEYS) |
1347 | set_newkeys(MODE_IN); | |
dc1f1948 | 1348 | else if (type == SSH2_MSG_USERAUTH_SUCCESS && |
1349 | !active_state->server_side) | |
07200973 | 1350 | packet_enable_delayed_compress(); |
7e7327a1 | 1351 | #ifdef PACKET_DEBUG |
12bf85ed | 1352 | fprintf(stderr, "read/plain[%d]:\r\n", type); |
dc1f1948 | 1353 | buffer_dump(&active_state->incoming_packet); |
7e7327a1 | 1354 | #endif |
08dcb5d7 | 1355 | /* reset for next packet */ |
dc1f1948 | 1356 | active_state->packlen = 0; |
08dcb5d7 | 1357 | return type; |
7e7327a1 | 1358 | } |
1359 | ||
1360 | int | |
54a5250f | 1361 | packet_read_poll_seqnr(u_int32_t *seqnr_p) |
7e7327a1 | 1362 | { |
0bc50167 | 1363 | u_int reason, seqnr; |
08dcb5d7 | 1364 | u_char type; |
7e7327a1 | 1365 | char *msg; |
7e7327a1 | 1366 | |
08dcb5d7 | 1367 | for (;;) { |
1368 | if (compat20) { | |
54a5250f | 1369 | type = packet_read_poll2(seqnr_p); |
2abb1ef5 | 1370 | if (type) { |
dc1f1948 | 1371 | active_state->keep_alive_timeouts = 0; |
7e7327a1 | 1372 | DBG(debug("received packet type %d", type)); |
2abb1ef5 | 1373 | } |
6aacefa7 | 1374 | switch (type) { |
aacfb17b | 1375 | case SSH2_MSG_IGNORE: |
ac47b626 | 1376 | debug3("Received SSH2_MSG_IGNORE"); |
aacfb17b | 1377 | break; |
7e7327a1 | 1378 | case SSH2_MSG_DEBUG: |
1379 | packet_get_char(); | |
1380 | msg = packet_get_string(NULL); | |
1381 | debug("Remote: %.900s", msg); | |
1382 | xfree(msg); | |
1383 | msg = packet_get_string(NULL); | |
1384 | xfree(msg); | |
1385 | break; | |
1386 | case SSH2_MSG_DISCONNECT: | |
1387 | reason = packet_get_int(); | |
1388 | msg = packet_get_string(NULL); | |
bbe88b6d | 1389 | logit("Received disconnect from %s: %u: %.400s", |
0bc50167 | 1390 | get_remote_ipaddr(), reason, msg); |
7e7327a1 | 1391 | xfree(msg); |
2362db19 | 1392 | cleanup_exit(255); |
7e7327a1 | 1393 | break; |
5a5f4c37 | 1394 | case SSH2_MSG_UNIMPLEMENTED: |
1395 | seqnr = packet_get_int(); | |
0bc50167 | 1396 | debug("Received SSH2_MSG_UNIMPLEMENTED for %u", |
1397 | seqnr); | |
aacfb17b | 1398 | break; |
7e7327a1 | 1399 | default: |
1400 | return type; | |
2b87da3b | 1401 | } |
7e7327a1 | 1402 | } else { |
54a5250f | 1403 | type = packet_read_poll1(); |
6aacefa7 | 1404 | switch (type) { |
7e7327a1 | 1405 | case SSH_MSG_IGNORE: |
1406 | break; | |
1407 | case SSH_MSG_DEBUG: | |
1408 | msg = packet_get_string(NULL); | |
1409 | debug("Remote: %.900s", msg); | |
1410 | xfree(msg); | |
1411 | break; | |
1412 | case SSH_MSG_DISCONNECT: | |
1413 | msg = packet_get_string(NULL); | |
bbe88b6d | 1414 | logit("Received disconnect from %s: %.400s", |
0bc50167 | 1415 | get_remote_ipaddr(), msg); |
2362db19 | 1416 | cleanup_exit(255); |
7e7327a1 | 1417 | break; |
1418 | default: | |
08dcb5d7 | 1419 | if (type) |
7e7327a1 | 1420 | DBG(debug("received packet type %d", type)); |
1421 | return type; | |
2b87da3b | 1422 | } |
7e7327a1 | 1423 | } |
1424 | } | |
1425 | } | |
1426 | ||
24ca6821 | 1427 | int |
54a5250f | 1428 | packet_read_poll(void) |
24ca6821 | 1429 | { |
54a5250f | 1430 | return packet_read_poll_seqnr(NULL); |
24ca6821 | 1431 | } |
1432 | ||
aa3378df | 1433 | /* |
1434 | * Buffers the given amount of input characters. This is intended to be used | |
1435 | * together with packet_read_poll. | |
1436 | */ | |
8efc0c15 | 1437 | |
1438 | void | |
1e3b8b07 | 1439 | packet_process_incoming(const char *buf, u_int len) |
8efc0c15 | 1440 | { |
dc1f1948 | 1441 | if (active_state->packet_discard) { |
1442 | active_state->keep_alive_timeouts = 0; /* ?? */ | |
1443 | if (len >= active_state->packet_discard) | |
608bcf58 | 1444 | packet_stop_discard(); |
dc1f1948 | 1445 | active_state->packet_discard -= len; |
608bcf58 | 1446 | return; |
1447 | } | |
dc1f1948 | 1448 | buffer_append(&active_state->input, buf, len); |
8efc0c15 | 1449 | } |
1450 | ||
1451 | /* Returns a character from the packet. */ | |
1452 | ||
1e3b8b07 | 1453 | u_int |
d5bb9418 | 1454 | packet_get_char(void) |
8efc0c15 | 1455 | { |
5260325f | 1456 | char ch; |
e1feb9bf | 1457 | |
dc1f1948 | 1458 | buffer_get(&active_state->incoming_packet, &ch, 1); |
1e3b8b07 | 1459 | return (u_char) ch; |
8efc0c15 | 1460 | } |
1461 | ||
1462 | /* Returns an integer from the packet data. */ | |
1463 | ||
1e3b8b07 | 1464 | u_int |
d5bb9418 | 1465 | packet_get_int(void) |
8efc0c15 | 1466 | { |
dc1f1948 | 1467 | return buffer_get_int(&active_state->incoming_packet); |
8efc0c15 | 1468 | } |
1469 | ||
aa3378df | 1470 | /* |
1471 | * Returns an arbitrary precision integer from the packet data. The integer | |
1472 | * must have been initialized before this call. | |
1473 | */ | |
8efc0c15 | 1474 | |
1475 | void | |
20b279e6 | 1476 | packet_get_bignum(BIGNUM * value) |
8efc0c15 | 1477 | { |
dc1f1948 | 1478 | buffer_get_bignum(&active_state->incoming_packet, value); |
8efc0c15 | 1479 | } |
1480 | ||
7e7327a1 | 1481 | void |
20b279e6 | 1482 | packet_get_bignum2(BIGNUM * value) |
7e7327a1 | 1483 | { |
dc1f1948 | 1484 | buffer_get_bignum2(&active_state->incoming_packet, value); |
7e7327a1 | 1485 | } |
1486 | ||
6c0fa2b1 | 1487 | void * |
2ceb8101 | 1488 | packet_get_raw(u_int *length_ptr) |
7e7327a1 | 1489 | { |
dc1f1948 | 1490 | u_int bytes = buffer_len(&active_state->incoming_packet); |
e1feb9bf | 1491 | |
7e7327a1 | 1492 | if (length_ptr != NULL) |
1493 | *length_ptr = bytes; | |
dc1f1948 | 1494 | return buffer_ptr(&active_state->incoming_packet); |
7e7327a1 | 1495 | } |
1496 | ||
6ae2364d | 1497 | int |
1498 | packet_remaining(void) | |
1499 | { | |
dc1f1948 | 1500 | return buffer_len(&active_state->incoming_packet); |
6ae2364d | 1501 | } |
1502 | ||
aa3378df | 1503 | /* |
1504 | * Returns a string from the packet data. The string is allocated using | |
1505 | * xmalloc; it is the responsibility of the calling program to free it when | |
1506 | * no longer needed. The length_ptr argument may be NULL, or point to an | |
1507 | * integer into which the length of the string is stored. | |
1508 | */ | |
8efc0c15 | 1509 | |
6c0fa2b1 | 1510 | void * |
1e3b8b07 | 1511 | packet_get_string(u_int *length_ptr) |
8efc0c15 | 1512 | { |
dc1f1948 | 1513 | return buffer_get_string(&active_state->incoming_packet, length_ptr); |
17f02f0a | 1514 | } |
1515 | ||
1516 | void * | |
1517 | packet_get_string_ptr(u_int *length_ptr) | |
1518 | { | |
dc1f1948 | 1519 | return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr); |
8efc0c15 | 1520 | } |
1521 | ||
aa3378df | 1522 | /* |
1523 | * Sends a diagnostic message from the server to the client. This message | |
1524 | * can be sent at any time (but not while constructing another message). The | |
1525 | * message is printed immediately, but only if the client is being executed | |
1526 | * in verbose mode. These messages are primarily intended to ease debugging | |
1527 | * authentication problems. The length of the formatted message must not | |
1528 | * exceed 1024 bytes. This will automatically call packet_write_wait. | |
1529 | */ | |
8efc0c15 | 1530 | |
1531 | void | |
5260325f | 1532 | packet_send_debug(const char *fmt,...) |
8efc0c15 | 1533 | { |
5260325f | 1534 | char buf[1024]; |
1535 | va_list args; | |
1536 | ||
f72fc97f | 1537 | if (compat20 && (datafellows & SSH_BUG_DEBUG)) |
1538 | return; | |
1539 | ||
5260325f | 1540 | va_start(args, fmt); |
1541 | vsnprintf(buf, sizeof(buf), fmt, args); | |
1542 | va_end(args); | |
1543 | ||
c4bc58eb | 1544 | if (compat20) { |
1545 | packet_start(SSH2_MSG_DEBUG); | |
1546 | packet_put_char(0); /* bool: always display */ | |
1547 | packet_put_cstring(buf); | |
1548 | packet_put_cstring(""); | |
1549 | } else { | |
1550 | packet_start(SSH_MSG_DEBUG); | |
1551 | packet_put_cstring(buf); | |
1552 | } | |
5260325f | 1553 | packet_send(); |
1554 | packet_write_wait(); | |
8efc0c15 | 1555 | } |
1556 | ||
aa3378df | 1557 | /* |
1558 | * Logs the error plus constructs and sends a disconnect packet, closes the | |
1559 | * connection, and exits. This function never returns. The error message | |
1560 | * should not contain a newline. The length of the formatted message must | |
1561 | * not exceed 1024 bytes. | |
1562 | */ | |
8efc0c15 | 1563 | |
1564 | void | |
5260325f | 1565 | packet_disconnect(const char *fmt,...) |
1566 | { | |
1567 | char buf[1024]; | |
1568 | va_list args; | |
1569 | static int disconnecting = 0; | |
e1feb9bf | 1570 | |
5260325f | 1571 | if (disconnecting) /* Guard against recursive invocations. */ |
1572 | fatal("packet_disconnect called recursively."); | |
1573 | disconnecting = 1; | |
1574 | ||
aa3378df | 1575 | /* |
1576 | * Format the message. Note that the caller must make sure the | |
1577 | * message is of limited size. | |
1578 | */ | |
5260325f | 1579 | va_start(args, fmt); |
1580 | vsnprintf(buf, sizeof(buf), fmt, args); | |
1581 | va_end(args); | |
1582 | ||
2ccb7bde | 1583 | /* Display the error locally */ |
bbe88b6d | 1584 | logit("Disconnecting: %.100s", buf); |
2ccb7bde | 1585 | |
5260325f | 1586 | /* Send the disconnect message to the other side, and wait for it to get sent. */ |
7e7327a1 | 1587 | if (compat20) { |
1588 | packet_start(SSH2_MSG_DISCONNECT); | |
1589 | packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR); | |
1590 | packet_put_cstring(buf); | |
1591 | packet_put_cstring(""); | |
1592 | } else { | |
1593 | packet_start(SSH_MSG_DISCONNECT); | |
449c5ba5 | 1594 | packet_put_cstring(buf); |
7e7327a1 | 1595 | } |
5260325f | 1596 | packet_send(); |
1597 | packet_write_wait(); | |
1598 | ||
1599 | /* Stop listening for connections. */ | |
d6746a0b | 1600 | channel_close_all(); |
5260325f | 1601 | |
1602 | /* Close the connection. */ | |
1603 | packet_close(); | |
2362db19 | 1604 | cleanup_exit(255); |
8efc0c15 | 1605 | } |
1606 | ||
aa3378df | 1607 | /* Checks if there is any buffered output, and tries to write some of the output. */ |
8efc0c15 | 1608 | |
1609 | void | |
d5bb9418 | 1610 | packet_write_poll(void) |
8efc0c15 | 1611 | { |
dc1f1948 | 1612 | int len = buffer_len(&active_state->output); |
e1feb9bf | 1613 | |
5260325f | 1614 | if (len > 0) { |
dc1f1948 | 1615 | len = write(active_state->connection_out, |
1616 | buffer_ptr(&active_state->output), len); | |
d38d9a80 | 1617 | if (len == -1) { |
1618 | if (errno == EINTR || errno == EAGAIN || | |
1619 | errno == EWOULDBLOCK) | |
5260325f | 1620 | return; |
d38d9a80 | 1621 | fatal("Write failed: %.100s", strerror(errno)); |
5260325f | 1622 | } |
d38d9a80 | 1623 | if (len == 0) |
1624 | fatal("Write connection closed"); | |
dc1f1948 | 1625 | buffer_consume(&active_state->output, len); |
5260325f | 1626 | } |
8efc0c15 | 1627 | } |
1628 | ||
d38d9a80 | 1629 | |
aa3378df | 1630 | /* |
1631 | * Calls packet_write_poll repeatedly until all pending output data has been | |
1632 | * written. | |
1633 | */ | |
8efc0c15 | 1634 | |
1635 | void | |
d5bb9418 | 1636 | packet_write_wait(void) |
8efc0c15 | 1637 | { |
20e04e90 | 1638 | fd_set *setp; |
bc2d97c8 | 1639 | int ret, ms_remain; |
1640 | struct timeval start, timeout, *timeoutp = NULL; | |
20e04e90 | 1641 | |
dc1f1948 | 1642 | setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1, |
1643 | NFDBITS), sizeof(fd_mask)); | |
5260325f | 1644 | packet_write_poll(); |
1645 | while (packet_have_data_to_write()) { | |
dc1f1948 | 1646 | memset(setp, 0, howmany(active_state->connection_out + 1, |
1647 | NFDBITS) * sizeof(fd_mask)); | |
1648 | FD_SET(active_state->connection_out, setp); | |
bc2d97c8 | 1649 | |
dc1f1948 | 1650 | if (active_state->packet_timeout_ms > 0) { |
1651 | ms_remain = active_state->packet_timeout_ms; | |
bc2d97c8 | 1652 | timeoutp = &timeout; |
1653 | } | |
1654 | for (;;) { | |
dc1f1948 | 1655 | if (active_state->packet_timeout_ms != -1) { |
bc2d97c8 | 1656 | ms_to_timeval(&timeout, ms_remain); |
1657 | gettimeofday(&start, NULL); | |
1658 | } | |
dc1f1948 | 1659 | if ((ret = select(active_state->connection_out + 1, |
1660 | NULL, setp, NULL, timeoutp)) >= 0) | |
bc2d97c8 | 1661 | break; |
dc1f1948 | 1662 | if (errno != EAGAIN && errno != EINTR && |
5a0c8771 | 1663 | errno != EWOULDBLOCK) |
bc2d97c8 | 1664 | break; |
dc1f1948 | 1665 | if (active_state->packet_timeout_ms == -1) |
bc2d97c8 | 1666 | continue; |
1667 | ms_subtract_diff(&start, &ms_remain); | |
1668 | if (ms_remain <= 0) { | |
1669 | ret = 0; | |
1670 | break; | |
1671 | } | |
1672 | } | |
1673 | if (ret == 0) { | |
1674 | logit("Connection to %.200s timed out while " | |
1675 | "waiting to write", get_remote_ipaddr()); | |
1676 | cleanup_exit(255); | |
1677 | } | |
5260325f | 1678 | packet_write_poll(); |
1679 | } | |
20e04e90 | 1680 | xfree(setp); |
8efc0c15 | 1681 | } |
1682 | ||
1683 | /* Returns true if there is buffered data to write to the connection. */ | |
1684 | ||
1685 | int | |
d5bb9418 | 1686 | packet_have_data_to_write(void) |
8efc0c15 | 1687 | { |
dc1f1948 | 1688 | return buffer_len(&active_state->output) != 0; |
8efc0c15 | 1689 | } |
1690 | ||
1691 | /* Returns true if there is not too much data to write to the connection. */ | |
1692 | ||
1693 | int | |
d5bb9418 | 1694 | packet_not_very_much_data_to_write(void) |
8efc0c15 | 1695 | { |
dc1f1948 | 1696 | if (active_state->interactive_mode) |
1697 | return buffer_len(&active_state->output) < 16384; | |
5260325f | 1698 | else |
dc1f1948 | 1699 | return buffer_len(&active_state->output) < 128 * 1024; |
8efc0c15 | 1700 | } |
1701 | ||
4d0cb2e5 | 1702 | |
9e637910 | 1703 | static void |
48f636b2 | 1704 | packet_set_tos(int interactive) |
1705 | { | |
00df6acd | 1706 | #if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN) |
48f636b2 | 1707 | int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT; |
1708 | ||
1709 | if (!packet_connection_is_on_socket() || | |
1710 | !packet_connection_is_ipv4()) | |
1711 | return; | |
dc1f1948 | 1712 | if (setsockopt(active_state->connection_in, IPPROTO_IP, IP_TOS, &tos, |
48f636b2 | 1713 | sizeof(tos)) < 0) |
1714 | error("setsockopt IP_TOS %d: %.100s:", | |
1715 | tos, strerror(errno)); | |
4d0cb2e5 | 1716 | #endif |
00df6acd | 1717 | } |
48f636b2 | 1718 | |
8efc0c15 | 1719 | /* Informs that the current session is interactive. Sets IP flags for that. */ |
1720 | ||
1721 | void | |
b5c334cc | 1722 | packet_set_interactive(int interactive) |
8efc0c15 | 1723 | { |
b5c334cc | 1724 | static int called = 0; |
5260325f | 1725 | |
b5c334cc | 1726 | if (called) |
1727 | return; | |
1728 | called = 1; | |
1729 | ||
5260325f | 1730 | /* Record that we are in interactive mode. */ |
dc1f1948 | 1731 | active_state->interactive_mode = interactive; |
5260325f | 1732 | |
48e671d5 | 1733 | /* Only set socket options if using a socket. */ |
1734 | if (!packet_connection_is_on_socket()) | |
aceb0423 | 1735 | return; |
dc1f1948 | 1736 | set_nodelay(active_state->connection_in); |
48f636b2 | 1737 | packet_set_tos(interactive); |
8efc0c15 | 1738 | } |
1739 | ||
1740 | /* Returns true if the current connection is interactive. */ | |
1741 | ||
1742 | int | |
d5bb9418 | 1743 | packet_is_interactive(void) |
8efc0c15 | 1744 | { |
dc1f1948 | 1745 | return active_state->interactive_mode; |
8efc0c15 | 1746 | } |
9d6b7add | 1747 | |
f6be21a0 | 1748 | int |
a27002e5 | 1749 | packet_set_maxsize(u_int s) |
9d6b7add | 1750 | { |
5260325f | 1751 | static int called = 0; |
e1feb9bf | 1752 | |
5260325f | 1753 | if (called) { |
bbe88b6d | 1754 | logit("packet_set_maxsize: called twice: old %d new %d", |
dc1f1948 | 1755 | active_state->max_packet_size, s); |
5260325f | 1756 | return -1; |
1757 | } | |
1758 | if (s < 4 * 1024 || s > 1024 * 1024) { | |
bbe88b6d | 1759 | logit("packet_set_maxsize: bad size %d", s); |
5260325f | 1760 | return -1; |
1761 | } | |
b6350327 | 1762 | called = 1; |
76735fe3 | 1763 | debug("packet_set_maxsize: setting to %d", s); |
dc1f1948 | 1764 | active_state->max_packet_size = s; |
5260325f | 1765 | return s; |
9d6b7add | 1766 | } |
a6215e53 | 1767 | |
dc1f1948 | 1768 | int |
1769 | packet_inc_alive_timeouts(void) | |
1770 | { | |
1771 | return ++active_state->keep_alive_timeouts; | |
1772 | } | |
1773 | ||
1774 | void | |
1775 | packet_set_alive_timeouts(int ka) | |
1776 | { | |
1777 | active_state->keep_alive_timeouts = ka; | |
1778 | } | |
1779 | ||
1780 | u_int | |
1781 | packet_get_maxsize(void) | |
1782 | { | |
1783 | return active_state->max_packet_size; | |
1784 | } | |
1785 | ||
b4b701be | 1786 | /* roundup current message to pad bytes */ |
1787 | void | |
1788 | packet_add_padding(u_char pad) | |
1789 | { | |
dc1f1948 | 1790 | active_state->extra_pad = pad; |
b4b701be | 1791 | } |
1792 | ||
a6215e53 | 1793 | /* |
1794 | * 9.2. Ignored Data Message | |
cd332296 | 1795 | * |
a6215e53 | 1796 | * byte SSH_MSG_IGNORE |
1797 | * string data | |
cd332296 | 1798 | * |
a6215e53 | 1799 | * All implementations MUST understand (and ignore) this message at any |
1800 | * time (after receiving the protocol version). No implementation is | |
1801 | * required to send them. This message can be used as an additional | |
1802 | * protection measure against advanced traffic analysis techniques. | |
1803 | */ | |
95ce5599 | 1804 | void |
1805 | packet_send_ignore(int nbytes) | |
1806 | { | |
ca75d7de | 1807 | u_int32_t rnd = 0; |
95ce5599 | 1808 | int i; |
1809 | ||
1810 | packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE); | |
a6215e53 | 1811 | packet_put_int(nbytes); |
184eed6a | 1812 | for (i = 0; i < nbytes; i++) { |
a6215e53 | 1813 | if (i % 4 == 0) |
ca75d7de | 1814 | rnd = arc4random(); |
febd6f21 | 1815 | packet_put_char((u_char)rnd & 0xff); |
ca75d7de | 1816 | rnd >>= 8; |
a6215e53 | 1817 | } |
1818 | } | |
ffd7b36b | 1819 | |
f6be21a0 | 1820 | #define MAX_PACKETS (1U<<31) |
ffd7b36b | 1821 | int |
1822 | packet_need_rekeying(void) | |
1823 | { | |
1824 | if (datafellows & SSH_BUG_NOREKEY) | |
1825 | return 0; | |
1826 | return | |
dc1f1948 | 1827 | (active_state->p_send.packets > MAX_PACKETS) || |
1828 | (active_state->p_read.packets > MAX_PACKETS) || | |
1829 | (active_state->max_blocks_out && | |
1830 | (active_state->p_send.blocks > active_state->max_blocks_out)) || | |
1831 | (active_state->max_blocks_in && | |
1832 | (active_state->p_read.blocks > active_state->max_blocks_in)); | |
ffd7b36b | 1833 | } |
1834 | ||
1835 | void | |
1836 | packet_set_rekey_limit(u_int32_t bytes) | |
1837 | { | |
dc1f1948 | 1838 | active_state->rekey_limit = bytes; |
ffd7b36b | 1839 | } |
07200973 | 1840 | |
1841 | void | |
1842 | packet_set_server(void) | |
1843 | { | |
dc1f1948 | 1844 | active_state->server_side = 1; |
07200973 | 1845 | } |
1846 | ||
1847 | void | |
1848 | packet_set_authenticated(void) | |
1849 | { | |
dc1f1948 | 1850 | active_state->after_authentication = 1; |
1851 | } | |
1852 | ||
1853 | void * | |
1854 | packet_get_input(void) | |
1855 | { | |
1856 | return (void *)&active_state->input; | |
1857 | } | |
1858 | ||
1859 | void * | |
1860 | packet_get_output(void) | |
1861 | { | |
1862 | return (void *)&active_state->output; | |
1863 | } | |
1864 | ||
1865 | void * | |
1866 | packet_get_newkeys(int mode) | |
1867 | { | |
1868 | return (void *)active_state->newkeys[mode]; | |
07200973 | 1869 | } |