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