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