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