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