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