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