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