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