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