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