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