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