Line | Count | Source |
1 | | /* $OpenBSD: packet.c,v 1.327 2025/12/05 06:16:27 dtucker Exp $ */ |
2 | | /* |
3 | | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
4 | | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
5 | | * All rights reserved |
6 | | * This file contains code implementing the packet protocol and communication |
7 | | * with the other side. This same code is used both on client and server side. |
8 | | * |
9 | | * As far as I am concerned, the code I have written for this software |
10 | | * can be used freely for any purpose. Any derived versions of this |
11 | | * software must be clearly marked as such, and if the derived work is |
12 | | * incompatible with the protocol description in the RFC file, it must be |
13 | | * called by a name other than "ssh" or "Secure Shell". |
14 | | * |
15 | | * |
16 | | * SSH2 packet format added by Markus Friedl. |
17 | | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
18 | | * |
19 | | * Redistribution and use in source and binary forms, with or without |
20 | | * modification, are permitted provided that the following conditions |
21 | | * are met: |
22 | | * 1. Redistributions of source code must retain the above copyright |
23 | | * notice, this list of conditions and the following disclaimer. |
24 | | * 2. Redistributions in binary form must reproduce the above copyright |
25 | | * notice, this list of conditions and the following disclaimer in the |
26 | | * documentation and/or other materials provided with the distribution. |
27 | | * |
28 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
29 | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
30 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
31 | | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
32 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
33 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
37 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | */ |
39 | | |
40 | | #include "includes.h" |
41 | | |
42 | | #include <sys/types.h> |
43 | | #include "openbsd-compat/sys-queue.h" |
44 | | #include <sys/socket.h> |
45 | | #include <sys/time.h> |
46 | | |
47 | | #include <netinet/in.h> |
48 | | #include <netinet/ip.h> |
49 | | #include <arpa/inet.h> |
50 | | |
51 | | #include <errno.h> |
52 | | #include <netdb.h> |
53 | | #include <stdarg.h> |
54 | | #include <stdio.h> |
55 | | #include <stdlib.h> |
56 | | #include <string.h> |
57 | | #include <unistd.h> |
58 | | #include <limits.h> |
59 | | #include <poll.h> |
60 | | #include <signal.h> |
61 | | #include <time.h> |
62 | | #include <util.h> |
63 | | |
64 | | /* |
65 | | * Explicitly include OpenSSL before zlib as some versions of OpenSSL have |
66 | | * "free_func" in their headers, which zlib typedefs. |
67 | | */ |
68 | | #ifdef WITH_OPENSSL |
69 | | # include <openssl/bn.h> |
70 | | # include <openssl/evp.h> |
71 | | # ifdef OPENSSL_HAS_ECC |
72 | | # include <openssl/ec.h> |
73 | | # endif |
74 | | #endif |
75 | | |
76 | | #ifdef WITH_ZLIB |
77 | | #include <zlib.h> |
78 | | #endif |
79 | | |
80 | | #include "xmalloc.h" |
81 | | #include "compat.h" |
82 | | #include "ssh2.h" |
83 | | #include "cipher.h" |
84 | | #include "sshkey.h" |
85 | | #include "kex.h" |
86 | | #include "digest.h" |
87 | | #include "mac.h" |
88 | | #include "log.h" |
89 | | #include "canohost.h" |
90 | | #include "misc.h" |
91 | | #include "channels.h" |
92 | | #include "ssh.h" |
93 | | #include "packet.h" |
94 | | #include "ssherr.h" |
95 | | #include "sshbuf.h" |
96 | | |
97 | | #ifdef PACKET_DEBUG |
98 | | #define DBG(x) x |
99 | | #else |
100 | | #define DBG(x) |
101 | | #endif |
102 | | |
103 | 1.35M | #define PACKET_MAX_SIZE (256 * 1024) |
104 | | |
105 | | struct packet_state { |
106 | | u_int32_t seqnr; |
107 | | u_int32_t packets; |
108 | | u_int64_t blocks; |
109 | | u_int64_t bytes; |
110 | | }; |
111 | | |
112 | | struct packet { |
113 | | TAILQ_ENTRY(packet) next; |
114 | | u_char type; |
115 | | struct sshbuf *payload; |
116 | | }; |
117 | | |
118 | | struct session_state { |
119 | | /* |
120 | | * This variable contains the file descriptors used for |
121 | | * communicating with the other side. connection_in is used for |
122 | | * reading; connection_out for writing. These can be the same |
123 | | * descriptor, in which case it is assumed to be a socket. |
124 | | */ |
125 | | int connection_in; |
126 | | int connection_out; |
127 | | |
128 | | /* Protocol flags for the remote side. */ |
129 | | u_int remote_protocol_flags; |
130 | | |
131 | | /* Encryption context for receiving data. Only used for decryption. */ |
132 | | struct sshcipher_ctx *receive_context; |
133 | | |
134 | | /* Encryption context for sending data. Only used for encryption. */ |
135 | | struct sshcipher_ctx *send_context; |
136 | | |
137 | | /* Buffer for raw input data from the socket. */ |
138 | | struct sshbuf *input; |
139 | | |
140 | | /* Buffer for raw output data going to the socket. */ |
141 | | struct sshbuf *output; |
142 | | |
143 | | /* Buffer for the partial outgoing packet being constructed. */ |
144 | | struct sshbuf *outgoing_packet; |
145 | | |
146 | | /* Buffer for the incoming packet currently being processed. */ |
147 | | struct sshbuf *incoming_packet; |
148 | | |
149 | | /* Scratch buffer for packet compression/decompression. */ |
150 | | struct sshbuf *compression_buffer; |
151 | | |
152 | | #ifdef WITH_ZLIB |
153 | | /* Incoming/outgoing compression dictionaries */ |
154 | | z_stream compression_in_stream; |
155 | | z_stream compression_out_stream; |
156 | | #endif |
157 | | int compression_in_started; |
158 | | int compression_out_started; |
159 | | int compression_in_failures; |
160 | | int compression_out_failures; |
161 | | |
162 | | /* default maximum packet size */ |
163 | | u_int max_packet_size; |
164 | | |
165 | | /* Flag indicating whether this module has been initialized. */ |
166 | | int initialized; |
167 | | |
168 | | /* Set to true if the connection is interactive. */ |
169 | | int interactive_mode; |
170 | | |
171 | | /* Set to true if we are the server side. */ |
172 | | int server_side; |
173 | | |
174 | | /* Set to true if we are authenticated. */ |
175 | | int after_authentication; |
176 | | |
177 | | int keep_alive_timeouts; |
178 | | |
179 | | /* The maximum time that we will wait to send or receive a packet */ |
180 | | int packet_timeout_ms; |
181 | | |
182 | | /* Session key information for Encryption and MAC */ |
183 | | struct newkeys *newkeys[MODE_MAX]; |
184 | | struct packet_state p_read, p_send; |
185 | | |
186 | | /* Volume-based rekeying */ |
187 | | u_int64_t max_blocks_in, max_blocks_out, rekey_limit; |
188 | | |
189 | | /* Time-based rekeying */ |
190 | | u_int32_t rekey_interval; /* how often in seconds */ |
191 | | time_t rekey_time; /* time of last rekeying */ |
192 | | |
193 | | /* roundup current message to extra_pad bytes */ |
194 | | u_char extra_pad; |
195 | | |
196 | | /* XXX discard incoming data after MAC error */ |
197 | | u_int packet_discard; |
198 | | size_t packet_discard_mac_already; |
199 | | struct sshmac *packet_discard_mac; |
200 | | |
201 | | /* Used in packet_read_poll2() */ |
202 | | u_int packlen; |
203 | | |
204 | | /* Used in packet_send2 */ |
205 | | int rekeying; |
206 | | |
207 | | /* Used in ssh_packet_send_mux() */ |
208 | | int mux; |
209 | | |
210 | | /* QoS handling */ |
211 | | int qos_interactive, qos_other; |
212 | | |
213 | | /* Used in packet_set_maxsize */ |
214 | | int set_maxsize_called; |
215 | | |
216 | | /* One-off warning about weak ciphers */ |
217 | | int cipher_warning_done; |
218 | | |
219 | | /* |
220 | | * Disconnect in progress. Used to prevent reentry in |
221 | | * ssh_packet_disconnect() |
222 | | */ |
223 | | int disconnecting; |
224 | | |
225 | | /* Nagle disabled on socket */ |
226 | | int nodelay_set; |
227 | | |
228 | | /* Hook for fuzzing inbound packets */ |
229 | | ssh_packet_hook_fn *hook_in; |
230 | | void *hook_in_ctx; |
231 | | |
232 | | TAILQ_HEAD(, packet) outgoing; |
233 | | }; |
234 | | |
235 | | struct ssh * |
236 | | ssh_alloc_session_state(void) |
237 | 177k | { |
238 | 177k | struct ssh *ssh = NULL; |
239 | 177k | struct session_state *state = NULL; |
240 | | |
241 | 177k | if ((ssh = calloc(1, sizeof(*ssh))) == NULL || |
242 | 177k | (state = calloc(1, sizeof(*state))) == NULL || |
243 | 177k | (ssh->kex = kex_new()) == NULL || |
244 | 177k | (state->input = sshbuf_new()) == NULL || |
245 | 177k | (state->output = sshbuf_new()) == NULL || |
246 | 177k | (state->outgoing_packet = sshbuf_new()) == NULL || |
247 | 177k | (state->incoming_packet = sshbuf_new()) == NULL) |
248 | 0 | goto fail; |
249 | 177k | TAILQ_INIT(&state->outgoing); |
250 | 177k | TAILQ_INIT(&ssh->private_keys); |
251 | 177k | TAILQ_INIT(&ssh->public_keys); |
252 | 177k | state->connection_in = -1; |
253 | 177k | state->connection_out = -1; |
254 | 177k | state->max_packet_size = 32768; |
255 | 177k | state->packet_timeout_ms = -1; |
256 | 177k | state->interactive_mode = 1; |
257 | 177k | state->qos_interactive = state->qos_other = -1; |
258 | 177k | state->p_send.packets = state->p_read.packets = 0; |
259 | 177k | state->initialized = 1; |
260 | | /* |
261 | | * ssh_packet_send2() needs to queue packets until |
262 | | * we've done the initial key exchange. |
263 | | */ |
264 | 177k | state->rekeying = 1; |
265 | 177k | ssh->state = state; |
266 | 177k | return ssh; |
267 | 0 | fail: |
268 | 0 | if (ssh) { |
269 | 0 | kex_free(ssh->kex); |
270 | 0 | free(ssh); |
271 | 0 | } |
272 | 0 | if (state) { |
273 | 0 | sshbuf_free(state->input); |
274 | 0 | sshbuf_free(state->output); |
275 | 0 | sshbuf_free(state->incoming_packet); |
276 | 0 | sshbuf_free(state->outgoing_packet); |
277 | 0 | free(state); |
278 | 0 | } |
279 | 0 | return NULL; |
280 | 177k | } |
281 | | |
282 | | void |
283 | | ssh_packet_set_input_hook(struct ssh *ssh, ssh_packet_hook_fn *hook, void *ctx) |
284 | 0 | { |
285 | 0 | ssh->state->hook_in = hook; |
286 | 0 | ssh->state->hook_in_ctx = ctx; |
287 | 0 | } |
288 | | |
289 | | /* Returns nonzero if rekeying is in progress */ |
290 | | int |
291 | | ssh_packet_is_rekeying(struct ssh *ssh) |
292 | 4.13k | { |
293 | 4.13k | return ssh->state->rekeying || |
294 | 0 | (ssh->kex != NULL && ssh->kex->done == 0); |
295 | 4.13k | } |
296 | | |
297 | | /* |
298 | | * Sets the descriptors used for communication. |
299 | | */ |
300 | | struct ssh * |
301 | | ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out) |
302 | 177k | { |
303 | 177k | struct session_state *state; |
304 | 177k | const struct sshcipher *none = cipher_by_name("none"); |
305 | 177k | int r; |
306 | | |
307 | 177k | if (none == NULL) { |
308 | 0 | error_f("cannot load cipher 'none'"); |
309 | 0 | return NULL; |
310 | 0 | } |
311 | 177k | if (ssh == NULL) |
312 | 177k | ssh = ssh_alloc_session_state(); |
313 | 177k | if (ssh == NULL) { |
314 | 0 | error_f("could not allocate state"); |
315 | 0 | return NULL; |
316 | 0 | } |
317 | 177k | state = ssh->state; |
318 | 177k | state->connection_in = fd_in; |
319 | 177k | state->connection_out = fd_out; |
320 | 177k | if ((r = cipher_init(&state->send_context, none, |
321 | 177k | (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 || |
322 | 177k | (r = cipher_init(&state->receive_context, none, |
323 | 177k | (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) { |
324 | 0 | error_fr(r, "cipher_init failed"); |
325 | 0 | free(ssh); /* XXX need ssh_free_session_state? */ |
326 | 0 | return NULL; |
327 | 0 | } |
328 | 177k | state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL; |
329 | | /* |
330 | | * Cache the IP address of the remote connection for use in error |
331 | | * messages that might be generated after the connection has closed. |
332 | | */ |
333 | 177k | (void)ssh_remote_ipaddr(ssh); |
334 | 177k | return ssh; |
335 | 177k | } |
336 | | |
337 | | void |
338 | | ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count) |
339 | 0 | { |
340 | 0 | struct session_state *state = ssh->state; |
341 | |
|
342 | 0 | if (timeout <= 0 || count <= 0) { |
343 | 0 | state->packet_timeout_ms = -1; |
344 | 0 | return; |
345 | 0 | } |
346 | 0 | if ((INT_MAX / 1000) / count < timeout) |
347 | 0 | state->packet_timeout_ms = INT_MAX; |
348 | 0 | else |
349 | 0 | state->packet_timeout_ms = timeout * count * 1000; |
350 | 0 | } |
351 | | |
352 | | void |
353 | | ssh_packet_set_mux(struct ssh *ssh) |
354 | 0 | { |
355 | 0 | ssh->state->mux = 1; |
356 | 0 | ssh->state->rekeying = 0; |
357 | 0 | kex_free(ssh->kex); |
358 | 0 | ssh->kex = NULL; |
359 | 0 | } |
360 | | |
361 | | int |
362 | | ssh_packet_get_mux(struct ssh *ssh) |
363 | 0 | { |
364 | 0 | return ssh->state->mux; |
365 | 0 | } |
366 | | |
367 | | int |
368 | | ssh_packet_set_log_preamble(struct ssh *ssh, const char *fmt, ...) |
369 | 0 | { |
370 | 0 | va_list args; |
371 | 0 | int r; |
372 | |
|
373 | 0 | free(ssh->log_preamble); |
374 | 0 | if (fmt == NULL) |
375 | 0 | ssh->log_preamble = NULL; |
376 | 0 | else { |
377 | 0 | va_start(args, fmt); |
378 | 0 | r = vasprintf(&ssh->log_preamble, fmt, args); |
379 | 0 | va_end(args); |
380 | 0 | if (r < 0 || ssh->log_preamble == NULL) |
381 | 0 | return SSH_ERR_ALLOC_FAIL; |
382 | 0 | } |
383 | 0 | return 0; |
384 | 0 | } |
385 | | |
386 | | int |
387 | | ssh_packet_stop_discard(struct ssh *ssh) |
388 | 0 | { |
389 | 0 | struct session_state *state = ssh->state; |
390 | 0 | int r; |
391 | |
|
392 | 0 | if (state->packet_discard_mac) { |
393 | 0 | char buf[1024]; |
394 | 0 | size_t dlen = PACKET_MAX_SIZE; |
395 | |
|
396 | 0 | if (dlen > state->packet_discard_mac_already) |
397 | 0 | dlen -= state->packet_discard_mac_already; |
398 | 0 | memset(buf, 'a', sizeof(buf)); |
399 | 0 | while (sshbuf_len(state->incoming_packet) < dlen) |
400 | 0 | if ((r = sshbuf_put(state->incoming_packet, buf, |
401 | 0 | sizeof(buf))) != 0) |
402 | 0 | return r; |
403 | 0 | (void) mac_compute(state->packet_discard_mac, |
404 | 0 | state->p_read.seqnr, |
405 | 0 | sshbuf_ptr(state->incoming_packet), dlen, |
406 | 0 | NULL, 0); |
407 | 0 | } |
408 | 0 | logit("Finished discarding for %.200s port %d", |
409 | 0 | ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); |
410 | 0 | return SSH_ERR_MAC_INVALID; |
411 | 0 | } |
412 | | |
413 | | static int |
414 | | ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc, |
415 | | struct sshmac *mac, size_t mac_already, u_int discard) |
416 | 8.81k | { |
417 | 8.81k | struct session_state *state = ssh->state; |
418 | 8.81k | int r; |
419 | | |
420 | 8.81k | if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) { |
421 | 8.81k | if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0) |
422 | 0 | return r; |
423 | 8.81k | return SSH_ERR_MAC_INVALID; |
424 | 8.81k | } |
425 | | /* |
426 | | * Record number of bytes over which the mac has already |
427 | | * been computed in order to minimize timing attacks. |
428 | | */ |
429 | 0 | if (mac && mac->enabled) { |
430 | 0 | state->packet_discard_mac = mac; |
431 | 0 | state->packet_discard_mac_already = mac_already; |
432 | 0 | } |
433 | 0 | if (sshbuf_len(state->input) >= discard) |
434 | 0 | return ssh_packet_stop_discard(ssh); |
435 | 0 | state->packet_discard = discard - sshbuf_len(state->input); |
436 | 0 | return 0; |
437 | 0 | } |
438 | | |
439 | | /* Returns 1 if remote host is connected via socket, 0 if not. */ |
440 | | |
441 | | int |
442 | | ssh_packet_connection_is_on_socket(struct ssh *ssh) |
443 | 177k | { |
444 | 177k | struct session_state *state; |
445 | 177k | struct sockaddr_storage from, to; |
446 | 177k | socklen_t fromlen, tolen; |
447 | | |
448 | 177k | if (ssh == NULL || ssh->state == NULL) |
449 | 0 | return 0; |
450 | | |
451 | 177k | state = ssh->state; |
452 | 177k | if (state->connection_in == -1 || state->connection_out == -1) |
453 | 177k | return 0; |
454 | | /* filedescriptors in and out are the same, so it's a socket */ |
455 | 0 | if (state->connection_in == state->connection_out) |
456 | 0 | return 1; |
457 | 0 | fromlen = sizeof(from); |
458 | 0 | memset(&from, 0, sizeof(from)); |
459 | 0 | if (getpeername(state->connection_in, (struct sockaddr *)&from, |
460 | 0 | &fromlen) == -1) |
461 | 0 | return 0; |
462 | 0 | tolen = sizeof(to); |
463 | 0 | memset(&to, 0, sizeof(to)); |
464 | 0 | if (getpeername(state->connection_out, (struct sockaddr *)&to, |
465 | 0 | &tolen) == -1) |
466 | 0 | return 0; |
467 | 0 | if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0) |
468 | 0 | return 0; |
469 | 0 | if (from.ss_family != AF_INET && from.ss_family != AF_INET6) |
470 | 0 | return 0; |
471 | 0 | return 1; |
472 | 0 | } |
473 | | |
474 | | void |
475 | | ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes) |
476 | 0 | { |
477 | 0 | if (ibytes) |
478 | 0 | *ibytes = ssh->state->p_read.bytes; |
479 | 0 | if (obytes) |
480 | 0 | *obytes = ssh->state->p_send.bytes; |
481 | 0 | } |
482 | | |
483 | | int |
484 | | ssh_packet_connection_af(struct ssh *ssh) |
485 | 0 | { |
486 | 0 | return get_sock_af(ssh->state->connection_out); |
487 | 0 | } |
488 | | |
489 | | /* Sets the connection into non-blocking mode. */ |
490 | | |
491 | | void |
492 | | ssh_packet_set_nonblocking(struct ssh *ssh) |
493 | 0 | { |
494 | | /* Set the socket into non-blocking mode. */ |
495 | 0 | set_nonblock(ssh->state->connection_in); |
496 | |
|
497 | 0 | if (ssh->state->connection_out != ssh->state->connection_in) |
498 | 0 | set_nonblock(ssh->state->connection_out); |
499 | 0 | } |
500 | | |
501 | | /* Returns the socket used for reading. */ |
502 | | |
503 | | int |
504 | | ssh_packet_get_connection_in(struct ssh *ssh) |
505 | 0 | { |
506 | 0 | return ssh->state->connection_in; |
507 | 0 | } |
508 | | |
509 | | /* Returns the descriptor used for writing. */ |
510 | | |
511 | | int |
512 | | ssh_packet_get_connection_out(struct ssh *ssh) |
513 | 0 | { |
514 | 0 | return ssh->state->connection_out; |
515 | 0 | } |
516 | | |
517 | | /* |
518 | | * Returns the IP-address of the remote host as a string. The returned |
519 | | * string must not be freed. |
520 | | */ |
521 | | |
522 | | const char * |
523 | | ssh_remote_ipaddr(struct ssh *ssh) |
524 | 177k | { |
525 | 177k | int sock; |
526 | | |
527 | | /* Check whether we have cached the ipaddr. */ |
528 | 177k | if (ssh->remote_ipaddr == NULL) { |
529 | 177k | if (ssh_packet_connection_is_on_socket(ssh)) { |
530 | 0 | sock = ssh->state->connection_in; |
531 | 0 | ssh->remote_ipaddr = get_peer_ipaddr(sock); |
532 | 0 | ssh->remote_port = get_peer_port(sock); |
533 | 0 | ssh->local_ipaddr = get_local_ipaddr(sock); |
534 | 0 | ssh->local_port = get_local_port(sock); |
535 | 177k | } else { |
536 | 177k | ssh->remote_ipaddr = xstrdup("UNKNOWN"); |
537 | 177k | ssh->remote_port = 65535; |
538 | 177k | ssh->local_ipaddr = xstrdup("UNKNOWN"); |
539 | 177k | ssh->local_port = 65535; |
540 | 177k | } |
541 | 177k | } |
542 | 177k | return ssh->remote_ipaddr; |
543 | 177k | } |
544 | | |
545 | | /* |
546 | | * Returns the remote DNS hostname as a string. The returned string must not |
547 | | * be freed. NB. this will usually trigger a DNS query. Return value is on |
548 | | * heap and no caching is performed. |
549 | | * This function does additional checks on the hostname to mitigate some |
550 | | * attacks based on conflation of hostnames and addresses and will |
551 | | * fall back to returning an address on error. |
552 | | */ |
553 | | |
554 | | char * |
555 | | ssh_remote_hostname(struct ssh *ssh) |
556 | 0 | { |
557 | 0 | struct sockaddr_storage from; |
558 | 0 | socklen_t fromlen; |
559 | 0 | struct addrinfo hints, *ai, *aitop; |
560 | 0 | char name[NI_MAXHOST], ntop2[NI_MAXHOST]; |
561 | 0 | const char *ntop = ssh_remote_ipaddr(ssh); |
562 | | |
563 | | /* Get IP address of client. */ |
564 | 0 | fromlen = sizeof(from); |
565 | 0 | memset(&from, 0, sizeof(from)); |
566 | 0 | if (getpeername(ssh_packet_get_connection_in(ssh), |
567 | 0 | (struct sockaddr *)&from, &fromlen) == -1) { |
568 | 0 | debug_f("getpeername failed: %.100s", strerror(errno)); |
569 | 0 | return xstrdup(ntop); |
570 | 0 | } |
571 | | |
572 | 0 | ipv64_normalise_mapped(&from, &fromlen); |
573 | 0 | if (from.ss_family == AF_INET6) |
574 | 0 | fromlen = sizeof(struct sockaddr_in6); |
575 | |
|
576 | 0 | debug3("trying to reverse map address %.100s.", ntop); |
577 | | /* Map the IP address to a host name. */ |
578 | 0 | if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name), |
579 | 0 | NULL, 0, NI_NAMEREQD) != 0) { |
580 | | /* Host name not found. Use ip address. */ |
581 | 0 | return xstrdup(ntop); |
582 | 0 | } |
583 | | |
584 | | /* |
585 | | * if reverse lookup result looks like a numeric hostname, |
586 | | * someone is trying to trick us by PTR record like following: |
587 | | * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5 |
588 | | */ |
589 | 0 | memset(&hints, 0, sizeof(hints)); |
590 | 0 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
591 | 0 | hints.ai_flags = AI_NUMERICHOST; |
592 | 0 | if (getaddrinfo(name, NULL, &hints, &ai) == 0) { |
593 | 0 | logit("Nasty PTR record \"%s\" is set up for %s, ignoring", |
594 | 0 | name, ntop); |
595 | 0 | freeaddrinfo(ai); |
596 | 0 | return xstrdup(ntop); |
597 | 0 | } |
598 | | |
599 | | /* Names are stored in lowercase. */ |
600 | 0 | lowercase(name); |
601 | | |
602 | | /* |
603 | | * Map it back to an IP address and check that the given |
604 | | * address actually is an address of this host. This is |
605 | | * necessary because anyone with access to a name server can |
606 | | * define arbitrary names for an IP address. Mapping from |
607 | | * name to IP address can be trusted better (but can still be |
608 | | * fooled if the intruder has access to the name server of |
609 | | * the domain). |
610 | | */ |
611 | 0 | memset(&hints, 0, sizeof(hints)); |
612 | 0 | hints.ai_family = from.ss_family; |
613 | 0 | hints.ai_socktype = SOCK_STREAM; |
614 | 0 | if (getaddrinfo(name, NULL, &hints, &aitop) != 0) { |
615 | 0 | logit("reverse mapping checking getaddrinfo for %.700s " |
616 | 0 | "[%s] failed.", name, ntop); |
617 | 0 | return xstrdup(ntop); |
618 | 0 | } |
619 | | /* Look for the address from the list of addresses. */ |
620 | 0 | for (ai = aitop; ai; ai = ai->ai_next) { |
621 | 0 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2, |
622 | 0 | sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 && |
623 | 0 | (strcmp(ntop, ntop2) == 0)) |
624 | 0 | break; |
625 | 0 | } |
626 | 0 | freeaddrinfo(aitop); |
627 | | /* If we reached the end of the list, the address was not there. */ |
628 | 0 | if (ai == NULL) { |
629 | | /* Address not found for the host name. */ |
630 | 0 | logit("Address %.100s maps to %.600s, but this does not " |
631 | 0 | "map back to the address.", ntop, name); |
632 | 0 | return xstrdup(ntop); |
633 | 0 | } |
634 | 0 | return xstrdup(name); |
635 | 0 | } |
636 | | |
637 | | /* Returns the port number of the remote host. */ |
638 | | |
639 | | int |
640 | | ssh_remote_port(struct ssh *ssh) |
641 | 0 | { |
642 | 0 | (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */ |
643 | 0 | return ssh->remote_port; |
644 | 0 | } |
645 | | |
646 | | /* |
647 | | * Returns the IP-address of the local host as a string. The returned |
648 | | * string must not be freed. |
649 | | */ |
650 | | |
651 | | const char * |
652 | | ssh_local_ipaddr(struct ssh *ssh) |
653 | 0 | { |
654 | 0 | (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */ |
655 | 0 | return ssh->local_ipaddr; |
656 | 0 | } |
657 | | |
658 | | /* Returns the port number of the local host. */ |
659 | | |
660 | | int |
661 | | ssh_local_port(struct ssh *ssh) |
662 | 0 | { |
663 | 0 | (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */ |
664 | 0 | return ssh->local_port; |
665 | 0 | } |
666 | | |
667 | | /* Returns the routing domain of the input socket, or NULL if unavailable */ |
668 | | const char * |
669 | | ssh_packet_rdomain_in(struct ssh *ssh) |
670 | 0 | { |
671 | 0 | if (ssh->rdomain_in != NULL) |
672 | 0 | return ssh->rdomain_in; |
673 | 0 | if (!ssh_packet_connection_is_on_socket(ssh)) |
674 | 0 | return NULL; |
675 | 0 | ssh->rdomain_in = get_rdomain(ssh->state->connection_in); |
676 | 0 | return ssh->rdomain_in; |
677 | 0 | } |
678 | | |
679 | | /* Closes the connection and clears and frees internal data structures. */ |
680 | | |
681 | | static void |
682 | | ssh_packet_close_internal(struct ssh *ssh, int do_close) |
683 | 177k | { |
684 | 177k | struct session_state *state = ssh->state; |
685 | 177k | u_int mode; |
686 | 177k | struct packet *p; |
687 | | |
688 | 177k | if (!state->initialized) |
689 | 0 | return; |
690 | 177k | state->initialized = 0; |
691 | 177k | if (do_close) { |
692 | 177k | if (state->connection_in == state->connection_out) { |
693 | 177k | close(state->connection_out); |
694 | 177k | } else { |
695 | 0 | close(state->connection_in); |
696 | 0 | close(state->connection_out); |
697 | 0 | } |
698 | 177k | } |
699 | 177k | sshbuf_free(state->input); |
700 | 177k | sshbuf_free(state->output); |
701 | 177k | sshbuf_free(state->outgoing_packet); |
702 | 177k | sshbuf_free(state->incoming_packet); |
703 | 177k | while ((p = TAILQ_FIRST(&state->outgoing))) { |
704 | 0 | sshbuf_free(p->payload); |
705 | 0 | TAILQ_REMOVE(&state->outgoing, p, next); |
706 | 0 | free(p); |
707 | 0 | } |
708 | 533k | for (mode = 0; mode < MODE_MAX; mode++) { |
709 | 355k | kex_free_newkeys(state->newkeys[mode]); /* current keys */ |
710 | 355k | state->newkeys[mode] = NULL; |
711 | 355k | ssh_clear_newkeys(ssh, mode); /* next keys */ |
712 | 355k | } |
713 | 177k | #ifdef WITH_ZLIB |
714 | | /* compression state is in shared mem, so we can only release it once */ |
715 | 177k | if (do_close && state->compression_buffer) { |
716 | 0 | sshbuf_free(state->compression_buffer); |
717 | 0 | if (state->compression_out_started) { |
718 | 0 | z_streamp stream = &state->compression_out_stream; |
719 | 0 | debug("compress outgoing: " |
720 | 0 | "raw data %llu, compressed %llu, factor %.2f", |
721 | 0 | (unsigned long long)stream->total_in, |
722 | 0 | (unsigned long long)stream->total_out, |
723 | 0 | stream->total_in == 0 ? 0.0 : |
724 | 0 | (double) stream->total_out / stream->total_in); |
725 | 0 | if (state->compression_out_failures == 0) |
726 | 0 | deflateEnd(stream); |
727 | 0 | } |
728 | 0 | if (state->compression_in_started) { |
729 | 0 | z_streamp stream = &state->compression_in_stream; |
730 | 0 | debug("compress incoming: " |
731 | 0 | "raw data %llu, compressed %llu, factor %.2f", |
732 | 0 | (unsigned long long)stream->total_out, |
733 | 0 | (unsigned long long)stream->total_in, |
734 | 0 | stream->total_out == 0 ? 0.0 : |
735 | 0 | (double) stream->total_in / stream->total_out); |
736 | 0 | if (state->compression_in_failures == 0) |
737 | 0 | inflateEnd(stream); |
738 | 0 | } |
739 | 0 | } |
740 | 177k | #endif /* WITH_ZLIB */ |
741 | 177k | cipher_free(state->send_context); |
742 | 177k | cipher_free(state->receive_context); |
743 | 177k | state->send_context = state->receive_context = NULL; |
744 | 177k | if (do_close) { |
745 | 177k | free(ssh->local_ipaddr); |
746 | 177k | ssh->local_ipaddr = NULL; |
747 | 177k | free(ssh->remote_ipaddr); |
748 | 177k | ssh->remote_ipaddr = NULL; |
749 | 177k | free(ssh->state); |
750 | 177k | ssh->state = NULL; |
751 | 177k | kex_free(ssh->kex); |
752 | 177k | ssh->kex = NULL; |
753 | 177k | } |
754 | 177k | } |
755 | | |
756 | | void |
757 | | ssh_packet_free(struct ssh *ssh) |
758 | 0 | { |
759 | 0 | ssh_packet_close_internal(ssh, 1); |
760 | 0 | freezero(ssh, sizeof(*ssh)); |
761 | 0 | } |
762 | | |
763 | | void |
764 | | ssh_packet_close(struct ssh *ssh) |
765 | 177k | { |
766 | 177k | ssh_packet_close_internal(ssh, 1); |
767 | 177k | } |
768 | | |
769 | | void |
770 | | ssh_packet_clear_keys(struct ssh *ssh) |
771 | 0 | { |
772 | 0 | ssh_packet_close_internal(ssh, 0); |
773 | 0 | } |
774 | | |
775 | | /* Sets remote side protocol flags. */ |
776 | | |
777 | | void |
778 | | ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags) |
779 | 0 | { |
780 | 0 | ssh->state->remote_protocol_flags = protocol_flags; |
781 | 0 | } |
782 | | |
783 | | /* Returns the remote protocol flags set earlier by the above function. */ |
784 | | |
785 | | u_int |
786 | | ssh_packet_get_protocol_flags(struct ssh *ssh) |
787 | 0 | { |
788 | 0 | return ssh->state->remote_protocol_flags; |
789 | 0 | } |
790 | | |
791 | | /* |
792 | | * Starts packet compression from the next packet on in both directions. |
793 | | * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip. |
794 | | */ |
795 | | |
796 | | static int |
797 | | ssh_packet_init_compression(struct ssh *ssh) |
798 | 0 | { |
799 | 0 | if (!ssh->state->compression_buffer && |
800 | 0 | ((ssh->state->compression_buffer = sshbuf_new()) == NULL)) |
801 | 0 | return SSH_ERR_ALLOC_FAIL; |
802 | 0 | return 0; |
803 | 0 | } |
804 | | |
805 | | #ifdef WITH_ZLIB |
806 | | static int |
807 | | start_compression_out(struct ssh *ssh, int level) |
808 | 0 | { |
809 | 0 | if (level < 1 || level > 9) |
810 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
811 | 0 | debug("Enabling compression at level %d.", level); |
812 | 0 | if (ssh->state->compression_out_started == 1) |
813 | 0 | deflateEnd(&ssh->state->compression_out_stream); |
814 | 0 | switch (deflateInit(&ssh->state->compression_out_stream, level)) { |
815 | 0 | case Z_OK: |
816 | 0 | ssh->state->compression_out_started = 1; |
817 | 0 | break; |
818 | 0 | case Z_MEM_ERROR: |
819 | 0 | return SSH_ERR_ALLOC_FAIL; |
820 | 0 | default: |
821 | 0 | return SSH_ERR_INTERNAL_ERROR; |
822 | 0 | } |
823 | 0 | return 0; |
824 | 0 | } |
825 | | |
826 | | static int |
827 | | start_compression_in(struct ssh *ssh) |
828 | 0 | { |
829 | 0 | if (ssh->state->compression_in_started == 1) |
830 | 0 | inflateEnd(&ssh->state->compression_in_stream); |
831 | 0 | switch (inflateInit(&ssh->state->compression_in_stream)) { |
832 | 0 | case Z_OK: |
833 | 0 | ssh->state->compression_in_started = 1; |
834 | 0 | break; |
835 | 0 | case Z_MEM_ERROR: |
836 | 0 | return SSH_ERR_ALLOC_FAIL; |
837 | 0 | default: |
838 | 0 | return SSH_ERR_INTERNAL_ERROR; |
839 | 0 | } |
840 | 0 | return 0; |
841 | 0 | } |
842 | | |
843 | | /* XXX remove need for separate compression buffer */ |
844 | | static int |
845 | | compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out) |
846 | 0 | { |
847 | 0 | u_char buf[4096]; |
848 | 0 | int r, status; |
849 | |
|
850 | 0 | if (ssh->state->compression_out_started != 1) |
851 | 0 | return SSH_ERR_INTERNAL_ERROR; |
852 | | |
853 | | /* This case is not handled below. */ |
854 | 0 | if (sshbuf_len(in) == 0) |
855 | 0 | return 0; |
856 | | |
857 | | /* Input is the contents of the input buffer. */ |
858 | 0 | if ((ssh->state->compression_out_stream.next_in = |
859 | 0 | sshbuf_mutable_ptr(in)) == NULL) |
860 | 0 | return SSH_ERR_INTERNAL_ERROR; |
861 | 0 | ssh->state->compression_out_stream.avail_in = sshbuf_len(in); |
862 | | |
863 | | /* Loop compressing until deflate() returns with avail_out != 0. */ |
864 | 0 | do { |
865 | | /* Set up fixed-size output buffer. */ |
866 | 0 | ssh->state->compression_out_stream.next_out = buf; |
867 | 0 | ssh->state->compression_out_stream.avail_out = sizeof(buf); |
868 | | |
869 | | /* Compress as much data into the buffer as possible. */ |
870 | 0 | status = deflate(&ssh->state->compression_out_stream, |
871 | 0 | Z_PARTIAL_FLUSH); |
872 | 0 | switch (status) { |
873 | 0 | case Z_MEM_ERROR: |
874 | 0 | return SSH_ERR_ALLOC_FAIL; |
875 | 0 | case Z_OK: |
876 | | /* Append compressed data to output_buffer. */ |
877 | 0 | if ((r = sshbuf_put(out, buf, sizeof(buf) - |
878 | 0 | ssh->state->compression_out_stream.avail_out)) != 0) |
879 | 0 | return r; |
880 | 0 | break; |
881 | 0 | case Z_STREAM_ERROR: |
882 | 0 | default: |
883 | 0 | ssh->state->compression_out_failures++; |
884 | 0 | return SSH_ERR_INVALID_FORMAT; |
885 | 0 | } |
886 | 0 | } while (ssh->state->compression_out_stream.avail_out == 0); |
887 | 0 | return 0; |
888 | 0 | } |
889 | | |
890 | | static int |
891 | | uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out) |
892 | 0 | { |
893 | 0 | u_char buf[4096]; |
894 | 0 | int r, status; |
895 | |
|
896 | 0 | if (ssh->state->compression_in_started != 1) |
897 | 0 | return SSH_ERR_INTERNAL_ERROR; |
898 | | |
899 | 0 | if ((ssh->state->compression_in_stream.next_in = |
900 | 0 | sshbuf_mutable_ptr(in)) == NULL) |
901 | 0 | return SSH_ERR_INTERNAL_ERROR; |
902 | 0 | ssh->state->compression_in_stream.avail_in = sshbuf_len(in); |
903 | |
|
904 | 0 | for (;;) { |
905 | | /* Set up fixed-size output buffer. */ |
906 | 0 | ssh->state->compression_in_stream.next_out = buf; |
907 | 0 | ssh->state->compression_in_stream.avail_out = sizeof(buf); |
908 | |
|
909 | 0 | status = inflate(&ssh->state->compression_in_stream, |
910 | 0 | Z_SYNC_FLUSH); |
911 | 0 | switch (status) { |
912 | 0 | case Z_OK: |
913 | 0 | if ((r = sshbuf_put(out, buf, sizeof(buf) - |
914 | 0 | ssh->state->compression_in_stream.avail_out)) != 0) |
915 | 0 | return r; |
916 | 0 | break; |
917 | 0 | case Z_BUF_ERROR: |
918 | | /* |
919 | | * Comments in zlib.h say that we should keep calling |
920 | | * inflate() until we get an error. This appears to |
921 | | * be the error that we get. |
922 | | */ |
923 | 0 | return 0; |
924 | 0 | case Z_DATA_ERROR: |
925 | 0 | return SSH_ERR_INVALID_FORMAT; |
926 | 0 | case Z_MEM_ERROR: |
927 | 0 | return SSH_ERR_ALLOC_FAIL; |
928 | 0 | case Z_STREAM_ERROR: |
929 | 0 | default: |
930 | 0 | ssh->state->compression_in_failures++; |
931 | 0 | return SSH_ERR_INTERNAL_ERROR; |
932 | 0 | } |
933 | 0 | } |
934 | | /* NOTREACHED */ |
935 | 0 | } |
936 | | |
937 | | #else /* WITH_ZLIB */ |
938 | | |
939 | | static int |
940 | | start_compression_out(struct ssh *ssh, int level) |
941 | | { |
942 | | return SSH_ERR_INTERNAL_ERROR; |
943 | | } |
944 | | |
945 | | static int |
946 | | start_compression_in(struct ssh *ssh) |
947 | | { |
948 | | return SSH_ERR_INTERNAL_ERROR; |
949 | | } |
950 | | |
951 | | static int |
952 | | compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out) |
953 | | { |
954 | | return SSH_ERR_INTERNAL_ERROR; |
955 | | } |
956 | | |
957 | | static int |
958 | | uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out) |
959 | | { |
960 | | return SSH_ERR_INTERNAL_ERROR; |
961 | | } |
962 | | #endif /* WITH_ZLIB */ |
963 | | |
964 | | void |
965 | | ssh_clear_newkeys(struct ssh *ssh, int mode) |
966 | 355k | { |
967 | 355k | if (ssh->kex && ssh->kex->newkeys[mode]) { |
968 | 25.3k | kex_free_newkeys(ssh->kex->newkeys[mode]); |
969 | 25.3k | ssh->kex->newkeys[mode] = NULL; |
970 | 25.3k | } |
971 | 355k | } |
972 | | |
973 | | int |
974 | | ssh_set_newkeys(struct ssh *ssh, int mode) |
975 | 1.17k | { |
976 | 1.17k | struct session_state *state = ssh->state; |
977 | 1.17k | struct sshenc *enc; |
978 | 1.17k | struct sshmac *mac; |
979 | 1.17k | struct sshcomp *comp; |
980 | 1.17k | struct sshcipher_ctx **ccp; |
981 | 1.17k | struct packet_state *ps; |
982 | 1.17k | u_int64_t *max_blocks; |
983 | 1.17k | const char *wmsg; |
984 | 1.17k | int r, crypt_type; |
985 | 1.17k | const char *dir = mode == MODE_OUT ? "out" : "in"; |
986 | | |
987 | 1.17k | debug2_f("mode %d", mode); |
988 | | |
989 | 1.17k | if (mode == MODE_OUT) { |
990 | 801 | ccp = &state->send_context; |
991 | 801 | crypt_type = CIPHER_ENCRYPT; |
992 | 801 | ps = &state->p_send; |
993 | 801 | max_blocks = &state->max_blocks_out; |
994 | 801 | } else { |
995 | 374 | ccp = &state->receive_context; |
996 | 374 | crypt_type = CIPHER_DECRYPT; |
997 | 374 | ps = &state->p_read; |
998 | 374 | max_blocks = &state->max_blocks_in; |
999 | 374 | } |
1000 | 1.17k | if (state->newkeys[mode] != NULL) { |
1001 | 0 | debug_f("rekeying %s, input %llu bytes %llu blocks, " |
1002 | 0 | "output %llu bytes %llu blocks", dir, |
1003 | 0 | (unsigned long long)state->p_read.bytes, |
1004 | 0 | (unsigned long long)state->p_read.blocks, |
1005 | 0 | (unsigned long long)state->p_send.bytes, |
1006 | 0 | (unsigned long long)state->p_send.blocks); |
1007 | 0 | kex_free_newkeys(state->newkeys[mode]); |
1008 | 0 | state->newkeys[mode] = NULL; |
1009 | 0 | } |
1010 | | /* note that both bytes and the seqnr are not reset */ |
1011 | 1.17k | ps->packets = ps->blocks = 0; |
1012 | | /* move newkeys from kex to state */ |
1013 | 1.17k | if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL) |
1014 | 0 | return SSH_ERR_INTERNAL_ERROR; |
1015 | 1.17k | ssh->kex->newkeys[mode] = NULL; |
1016 | 1.17k | enc = &state->newkeys[mode]->enc; |
1017 | 1.17k | mac = &state->newkeys[mode]->mac; |
1018 | 1.17k | comp = &state->newkeys[mode]->comp; |
1019 | 1.17k | if (cipher_authlen(enc->cipher) == 0) { |
1020 | 1.17k | if ((r = mac_init(mac)) != 0) |
1021 | 0 | return r; |
1022 | 1.17k | } |
1023 | 1.17k | mac->enabled = 1; |
1024 | 1.17k | DBG(debug_f("cipher_init: %s", dir)); |
1025 | 1.17k | cipher_free(*ccp); |
1026 | 1.17k | *ccp = NULL; |
1027 | 1.17k | if ((r = cipher_init(ccp, enc->cipher, enc->key, enc->key_len, |
1028 | 1.17k | enc->iv, enc->iv_len, crypt_type)) != 0) |
1029 | 0 | return r; |
1030 | 1.17k | if (!state->cipher_warning_done && |
1031 | 1.17k | (wmsg = cipher_warning_message(*ccp)) != NULL) { |
1032 | 0 | error("Warning: %s", wmsg); |
1033 | 0 | state->cipher_warning_done = 1; |
1034 | 0 | } |
1035 | | /* Deleting the keys does not gain extra security */ |
1036 | | /* explicit_bzero(enc->iv, enc->block_size); |
1037 | | explicit_bzero(enc->key, enc->key_len); |
1038 | | explicit_bzero(mac->key, mac->key_len); */ |
1039 | 1.17k | if (((comp->type == COMP_DELAYED && state->after_authentication)) && |
1040 | 0 | comp->enabled == 0) { |
1041 | 0 | if ((r = ssh_packet_init_compression(ssh)) < 0) |
1042 | 0 | return r; |
1043 | 0 | if (mode == MODE_OUT) { |
1044 | 0 | if ((r = start_compression_out(ssh, 6)) != 0) |
1045 | 0 | return r; |
1046 | 0 | } else { |
1047 | 0 | if ((r = start_compression_in(ssh)) != 0) |
1048 | 0 | return r; |
1049 | 0 | } |
1050 | 0 | comp->enabled = 1; |
1051 | 0 | } |
1052 | | /* |
1053 | | * The 2^(blocksize*2) limit is too expensive for 3DES, |
1054 | | * so enforce a 1GB limit for small blocksizes. |
1055 | | * See RFC4344 section 3.2. |
1056 | | */ |
1057 | 1.17k | if (enc->block_size >= 16) |
1058 | 0 | *max_blocks = (u_int64_t)1 << (enc->block_size*2); |
1059 | 1.17k | else |
1060 | 1.17k | *max_blocks = ((u_int64_t)1 << 30) / enc->block_size; |
1061 | 1.17k | if (state->rekey_limit) |
1062 | 0 | *max_blocks = MINIMUM(*max_blocks, |
1063 | 1.17k | state->rekey_limit / enc->block_size); |
1064 | 1.17k | debug("rekey %s after %llu blocks", dir, |
1065 | 1.17k | (unsigned long long)*max_blocks); |
1066 | 1.17k | return 0; |
1067 | 1.17k | } |
1068 | | |
1069 | 0 | #define MAX_PACKETS (1U<<31) |
1070 | | static int |
1071 | | ssh_packet_need_rekeying(struct ssh *ssh, u_int outbound_packet_len) |
1072 | 1.33M | { |
1073 | 1.33M | struct session_state *state = ssh->state; |
1074 | 1.33M | u_int32_t out_blocks; |
1075 | | |
1076 | | /* XXX client can't cope with rekeying pre-auth */ |
1077 | 1.33M | if (!state->after_authentication) |
1078 | 1.33M | return 0; |
1079 | | |
1080 | | /* Haven't keyed yet or KEX in progress. */ |
1081 | 4.13k | if (ssh_packet_is_rekeying(ssh)) |
1082 | 4.13k | return 0; |
1083 | | |
1084 | | /* Peer can't rekey */ |
1085 | 0 | if (ssh->compat & SSH_BUG_NOREKEY) |
1086 | 0 | return 0; |
1087 | | |
1088 | | /* |
1089 | | * Permit one packet in or out per rekey - this allows us to |
1090 | | * make progress when rekey limits are very small. |
1091 | | */ |
1092 | 0 | if (state->p_send.packets == 0 && state->p_read.packets == 0) |
1093 | 0 | return 0; |
1094 | | |
1095 | | /* Time-based rekeying */ |
1096 | 0 | if (state->rekey_interval != 0 && |
1097 | 0 | (int64_t)state->rekey_time + state->rekey_interval <= monotime()) |
1098 | 0 | return 1; |
1099 | | |
1100 | | /* |
1101 | | * Always rekey when MAX_PACKETS sent in either direction |
1102 | | * As per RFC4344 section 3.1 we do this after 2^31 packets. |
1103 | | */ |
1104 | 0 | if (state->p_send.packets > MAX_PACKETS || |
1105 | 0 | state->p_read.packets > MAX_PACKETS) |
1106 | 0 | return 1; |
1107 | | |
1108 | | /* Rekey after (cipher-specific) maximum blocks */ |
1109 | 0 | out_blocks = ROUNDUP(outbound_packet_len, |
1110 | 0 | state->newkeys[MODE_OUT]->enc.block_size); |
1111 | 0 | return (state->max_blocks_out && |
1112 | 0 | (state->p_send.blocks + out_blocks > state->max_blocks_out)) || |
1113 | 0 | (state->max_blocks_in && |
1114 | 0 | (state->p_read.blocks > state->max_blocks_in)); |
1115 | 0 | } |
1116 | | |
1117 | | int |
1118 | | ssh_packet_check_rekey(struct ssh *ssh) |
1119 | 1.33M | { |
1120 | 1.33M | if (!ssh_packet_need_rekeying(ssh, 0)) |
1121 | 1.33M | return 0; |
1122 | 0 | debug3_f("rekex triggered"); |
1123 | 0 | return kex_start_rekex(ssh); |
1124 | 1.33M | } |
1125 | | |
1126 | | /* |
1127 | | * Delayed compression for SSH2 is enabled after authentication: |
1128 | | * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent, |
1129 | | * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received. |
1130 | | */ |
1131 | | static int |
1132 | | ssh_packet_enable_delayed_compress(struct ssh *ssh) |
1133 | 345 | { |
1134 | 345 | struct session_state *state = ssh->state; |
1135 | 345 | struct sshcomp *comp = NULL; |
1136 | 345 | int r, mode; |
1137 | | |
1138 | | /* |
1139 | | * Remember that we are past the authentication step, so rekeying |
1140 | | * with COMP_DELAYED will turn on compression immediately. |
1141 | | */ |
1142 | 345 | state->after_authentication = 1; |
1143 | 1.03k | for (mode = 0; mode < MODE_MAX; mode++) { |
1144 | | /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */ |
1145 | 690 | if (state->newkeys[mode] == NULL) |
1146 | 690 | continue; |
1147 | 0 | comp = &state->newkeys[mode]->comp; |
1148 | 0 | if (comp && !comp->enabled && comp->type == COMP_DELAYED) { |
1149 | 0 | if ((r = ssh_packet_init_compression(ssh)) != 0) |
1150 | 0 | return r; |
1151 | 0 | if (mode == MODE_OUT) { |
1152 | 0 | if ((r = start_compression_out(ssh, 6)) != 0) |
1153 | 0 | return r; |
1154 | 0 | } else { |
1155 | 0 | if ((r = start_compression_in(ssh)) != 0) |
1156 | 0 | return r; |
1157 | 0 | } |
1158 | 0 | comp->enabled = 1; |
1159 | 0 | } |
1160 | 0 | } |
1161 | 345 | return 0; |
1162 | 345 | } |
1163 | | |
1164 | | /* Used to mute debug logging for noisy packet types */ |
1165 | | int |
1166 | | ssh_packet_log_type(u_char type) |
1167 | 2.69M | { |
1168 | 2.69M | switch (type) { |
1169 | 115 | case SSH2_MSG_PING: |
1170 | 230 | case SSH2_MSG_PONG: |
1171 | 345 | case SSH2_MSG_CHANNEL_DATA: |
1172 | 494 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
1173 | 699 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
1174 | 699 | return 0; |
1175 | 2.69M | default: |
1176 | 2.69M | return 1; |
1177 | 2.69M | } |
1178 | 2.69M | } |
1179 | | |
1180 | | /* |
1181 | | * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue) |
1182 | | */ |
1183 | | int |
1184 | | ssh_packet_send2_wrapped(struct ssh *ssh) |
1185 | 1.35M | { |
1186 | 1.35M | struct session_state *state = ssh->state; |
1187 | 1.35M | u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH]; |
1188 | 1.35M | u_char tmp, padlen, pad = 0; |
1189 | 1.35M | u_int authlen = 0, aadlen = 0; |
1190 | 1.35M | u_int len; |
1191 | 1.35M | struct sshenc *enc = NULL; |
1192 | 1.35M | struct sshmac *mac = NULL; |
1193 | 1.35M | struct sshcomp *comp = NULL; |
1194 | 1.35M | int r, block_size; |
1195 | | |
1196 | 1.35M | if (state->newkeys[MODE_OUT] != NULL) { |
1197 | 586k | enc = &state->newkeys[MODE_OUT]->enc; |
1198 | 586k | mac = &state->newkeys[MODE_OUT]->mac; |
1199 | 586k | comp = &state->newkeys[MODE_OUT]->comp; |
1200 | | /* disable mac for authenticated encryption */ |
1201 | 586k | if ((authlen = cipher_authlen(enc->cipher)) != 0) |
1202 | 0 | mac = NULL; |
1203 | 586k | } |
1204 | 1.35M | block_size = enc ? enc->block_size : 8; |
1205 | 1.35M | aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; |
1206 | | |
1207 | 1.35M | type = (sshbuf_ptr(state->outgoing_packet))[5]; |
1208 | 1.35M | if (ssh_packet_log_type(type)) |
1209 | 1.35M | debug3("send packet: type %u", type); |
1210 | | #ifdef PACKET_DEBUG |
1211 | | fprintf(stderr, "plain: "); |
1212 | | sshbuf_dump(state->outgoing_packet, stderr); |
1213 | | #endif |
1214 | | |
1215 | 1.35M | if (comp && comp->enabled) { |
1216 | 0 | len = sshbuf_len(state->outgoing_packet); |
1217 | | /* skip header, compress only payload */ |
1218 | 0 | if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0) |
1219 | 0 | goto out; |
1220 | 0 | sshbuf_reset(state->compression_buffer); |
1221 | 0 | if ((r = compress_buffer(ssh, state->outgoing_packet, |
1222 | 0 | state->compression_buffer)) != 0) |
1223 | 0 | goto out; |
1224 | 0 | sshbuf_reset(state->outgoing_packet); |
1225 | 0 | if ((r = sshbuf_put(state->outgoing_packet, |
1226 | 0 | "\0\0\0\0\0", 5)) != 0 || |
1227 | 0 | (r = sshbuf_putb(state->outgoing_packet, |
1228 | 0 | state->compression_buffer)) != 0) |
1229 | 0 | goto out; |
1230 | 0 | DBG(debug("compression: raw %d compressed %zd", len, |
1231 | 0 | sshbuf_len(state->outgoing_packet))); |
1232 | 0 | } |
1233 | | |
1234 | | /* sizeof (packet_len + pad_len + payload) */ |
1235 | 1.35M | len = sshbuf_len(state->outgoing_packet); |
1236 | | |
1237 | | /* |
1238 | | * calc size of padding, alloc space, get random data, |
1239 | | * minimum padding is 4 bytes |
1240 | | */ |
1241 | 1.35M | len -= aadlen; /* packet length is not encrypted for EtM modes */ |
1242 | 1.35M | padlen = block_size - (len % block_size); |
1243 | 1.35M | if (padlen < 4) |
1244 | 448k | padlen += block_size; |
1245 | 1.35M | if (state->extra_pad) { |
1246 | 0 | tmp = state->extra_pad; |
1247 | 0 | state->extra_pad = |
1248 | 0 | ROUNDUP(state->extra_pad, block_size); |
1249 | | /* check if roundup overflowed */ |
1250 | 0 | if (state->extra_pad < tmp) |
1251 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
1252 | 0 | tmp = (len + padlen) % state->extra_pad; |
1253 | | /* Check whether pad calculation below will underflow */ |
1254 | 0 | if (tmp > state->extra_pad) |
1255 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
1256 | 0 | pad = state->extra_pad - tmp; |
1257 | 0 | DBG(debug3_f("adding %d (len %d padlen %d extra_pad %d)", |
1258 | 0 | pad, len, padlen, state->extra_pad)); |
1259 | 0 | tmp = padlen; |
1260 | 0 | padlen += pad; |
1261 | | /* Check whether padlen calculation overflowed */ |
1262 | 0 | if (padlen < tmp) |
1263 | 0 | return SSH_ERR_INVALID_ARGUMENT; /* overflow */ |
1264 | 0 | state->extra_pad = 0; |
1265 | 0 | } |
1266 | 1.35M | if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0) |
1267 | 0 | goto out; |
1268 | 1.35M | if (enc && !cipher_ctx_is_plaintext(state->send_context)) { |
1269 | | /* random padding */ |
1270 | 0 | arc4random_buf(cp, padlen); |
1271 | 1.35M | } else { |
1272 | | /* clear padding */ |
1273 | 1.35M | explicit_bzero(cp, padlen); |
1274 | 1.35M | } |
1275 | | /* sizeof (packet_len + pad_len + payload + padding) */ |
1276 | 1.35M | len = sshbuf_len(state->outgoing_packet); |
1277 | 1.35M | cp = sshbuf_mutable_ptr(state->outgoing_packet); |
1278 | 1.35M | if (cp == NULL) { |
1279 | 0 | r = SSH_ERR_INTERNAL_ERROR; |
1280 | 0 | goto out; |
1281 | 0 | } |
1282 | | /* packet_length includes payload, padding and padding length field */ |
1283 | 1.35M | POKE_U32(cp, len - 4); |
1284 | 1.35M | cp[4] = padlen; |
1285 | 1.35M | DBG(debug("send: len %d (includes padlen %d, aadlen %d)", |
1286 | 1.35M | len, padlen, aadlen)); |
1287 | | |
1288 | | /* compute MAC over seqnr and packet(length fields, payload, padding) */ |
1289 | 1.35M | if (mac && mac->enabled && !mac->etm) { |
1290 | 150k | if ((r = mac_compute(mac, state->p_send.seqnr, |
1291 | 150k | sshbuf_ptr(state->outgoing_packet), len, |
1292 | 150k | macbuf, sizeof(macbuf))) != 0) |
1293 | 0 | goto out; |
1294 | 150k | DBG(debug("done calc MAC out #%d", state->p_send.seqnr)); |
1295 | 150k | } |
1296 | | /* encrypt packet and append to output buffer. */ |
1297 | 1.35M | if ((r = sshbuf_reserve(state->output, |
1298 | 1.35M | sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0) |
1299 | 0 | goto out; |
1300 | 1.35M | if ((r = cipher_crypt(state->send_context, state->p_send.seqnr, cp, |
1301 | 1.35M | sshbuf_ptr(state->outgoing_packet), |
1302 | 1.35M | len - aadlen, aadlen, authlen)) != 0) |
1303 | 0 | goto out; |
1304 | | /* append unencrypted MAC */ |
1305 | 1.35M | if (mac && mac->enabled) { |
1306 | 586k | if (mac->etm) { |
1307 | | /* EtM: compute mac over aadlen + cipher text */ |
1308 | 435k | if ((r = mac_compute(mac, state->p_send.seqnr, |
1309 | 435k | cp, len, macbuf, sizeof(macbuf))) != 0) |
1310 | 0 | goto out; |
1311 | 435k | DBG(debug("done calc MAC(EtM) out #%d", |
1312 | 435k | state->p_send.seqnr)); |
1313 | 435k | } |
1314 | 586k | if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0) |
1315 | 0 | goto out; |
1316 | 586k | } |
1317 | | #ifdef PACKET_DEBUG |
1318 | | fprintf(stderr, "encrypted: "); |
1319 | | sshbuf_dump(state->output, stderr); |
1320 | | #endif |
1321 | | /* increment sequence number for outgoing packets */ |
1322 | 1.35M | if (++state->p_send.seqnr == 0) { |
1323 | 0 | if ((ssh->kex->flags & KEX_INITIAL) != 0) { |
1324 | 0 | ssh_packet_disconnect(ssh, "outgoing sequence number " |
1325 | 0 | "wrapped during initial key exchange"); |
1326 | 0 | } |
1327 | 0 | logit("outgoing seqnr wraps around"); |
1328 | 0 | } |
1329 | 1.35M | if (++state->p_send.packets == 0) |
1330 | 0 | if (!(ssh->compat & SSH_BUG_NOREKEY)) |
1331 | 0 | return SSH_ERR_NEED_REKEY; |
1332 | 1.35M | state->p_send.blocks += len / block_size; |
1333 | 1.35M | state->p_send.bytes += len; |
1334 | 1.35M | sshbuf_reset(state->outgoing_packet); |
1335 | | |
1336 | 1.35M | if (type == SSH2_MSG_NEWKEYS && ssh->kex->kex_strict) { |
1337 | 106 | debug_f("resetting send seqnr %u", state->p_send.seqnr); |
1338 | 106 | state->p_send.seqnr = 0; |
1339 | 106 | } |
1340 | | |
1341 | 1.35M | if (type == SSH2_MSG_NEWKEYS) |
1342 | 801 | r = ssh_set_newkeys(ssh, MODE_OUT); |
1343 | 1.35M | else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side) |
1344 | 0 | r = ssh_packet_enable_delayed_compress(ssh); |
1345 | 1.35M | else |
1346 | 1.35M | r = 0; |
1347 | 1.35M | out: |
1348 | 1.35M | return r; |
1349 | 1.35M | } |
1350 | | |
1351 | | /* returns non-zero if the specified packet type is usec by KEX */ |
1352 | | static int |
1353 | | ssh_packet_type_is_kex(u_char type) |
1354 | 2.11M | { |
1355 | 2.11M | return |
1356 | 2.11M | type >= SSH2_MSG_TRANSPORT_MIN && |
1357 | 2.11M | type <= SSH2_MSG_TRANSPORT_MAX && |
1358 | 2.11M | type != SSH2_MSG_SERVICE_REQUEST && |
1359 | 2.11M | type != SSH2_MSG_SERVICE_ACCEPT && |
1360 | 2.11M | type != SSH2_MSG_EXT_INFO; |
1361 | 2.11M | } |
1362 | | |
1363 | | int |
1364 | | ssh_packet_send2(struct ssh *ssh) |
1365 | 1.35M | { |
1366 | 1.35M | struct session_state *state = ssh->state; |
1367 | 1.35M | struct packet *p; |
1368 | 1.35M | u_char type; |
1369 | 1.35M | int r, need_rekey; |
1370 | | |
1371 | 1.35M | if (sshbuf_len(state->outgoing_packet) < 6) |
1372 | 0 | return SSH_ERR_INTERNAL_ERROR; |
1373 | 1.35M | type = sshbuf_ptr(state->outgoing_packet)[5]; |
1374 | 1.35M | need_rekey = !ssh_packet_type_is_kex(type) && |
1375 | 106 | ssh_packet_need_rekeying(ssh, sshbuf_len(state->outgoing_packet)); |
1376 | | |
1377 | | /* |
1378 | | * During rekeying we can only send key exchange messages. |
1379 | | * Queue everything else. |
1380 | | */ |
1381 | 1.35M | if ((need_rekey || state->rekeying) && !ssh_packet_type_is_kex(type)) { |
1382 | 0 | if (need_rekey) |
1383 | 0 | debug3_f("rekex triggered"); |
1384 | 0 | debug("enqueue packet: %u", type); |
1385 | 0 | p = calloc(1, sizeof(*p)); |
1386 | 0 | if (p == NULL) |
1387 | 0 | return SSH_ERR_ALLOC_FAIL; |
1388 | 0 | p->type = type; |
1389 | 0 | p->payload = state->outgoing_packet; |
1390 | 0 | TAILQ_INSERT_TAIL(&state->outgoing, p, next); |
1391 | 0 | state->outgoing_packet = sshbuf_new(); |
1392 | 0 | if (state->outgoing_packet == NULL) |
1393 | 0 | return SSH_ERR_ALLOC_FAIL; |
1394 | 0 | if (need_rekey) { |
1395 | | /* |
1396 | | * This packet triggered a rekey, so send the |
1397 | | * KEXINIT now. |
1398 | | * NB. reenters this function via kex_start_rekex(). |
1399 | | */ |
1400 | 0 | return kex_start_rekex(ssh); |
1401 | 0 | } |
1402 | 0 | return 0; |
1403 | 0 | } |
1404 | | |
1405 | | /* rekeying starts with sending KEXINIT */ |
1406 | 1.35M | if (type == SSH2_MSG_KEXINIT) |
1407 | 87.3k | state->rekeying = 1; |
1408 | | |
1409 | 1.35M | if ((r = ssh_packet_send2_wrapped(ssh)) != 0) |
1410 | 0 | return r; |
1411 | | |
1412 | | /* after a NEWKEYS message we can send the complete queue */ |
1413 | 1.35M | if (type == SSH2_MSG_NEWKEYS) { |
1414 | 801 | state->rekeying = 0; |
1415 | 801 | state->rekey_time = monotime(); |
1416 | 801 | while ((p = TAILQ_FIRST(&state->outgoing))) { |
1417 | 0 | type = p->type; |
1418 | | /* |
1419 | | * If this packet triggers a rekex, then skip the |
1420 | | * remaining packets in the queue for now. |
1421 | | * NB. re-enters this function via kex_start_rekex. |
1422 | | */ |
1423 | 0 | if (ssh_packet_need_rekeying(ssh, |
1424 | 0 | sshbuf_len(p->payload))) { |
1425 | 0 | debug3_f("queued packet triggered rekex"); |
1426 | 0 | return kex_start_rekex(ssh); |
1427 | 0 | } |
1428 | 0 | debug("dequeue packet: %u", type); |
1429 | 0 | sshbuf_free(state->outgoing_packet); |
1430 | 0 | state->outgoing_packet = p->payload; |
1431 | 0 | TAILQ_REMOVE(&state->outgoing, p, next); |
1432 | 0 | memset(p, 0, sizeof(*p)); |
1433 | 0 | free(p); |
1434 | 0 | if ((r = ssh_packet_send2_wrapped(ssh)) != 0) |
1435 | 0 | return r; |
1436 | 0 | } |
1437 | 801 | } |
1438 | 1.35M | return 0; |
1439 | 1.35M | } |
1440 | | |
1441 | | /* |
1442 | | * Waits until a packet has been received, and returns its type. Note that |
1443 | | * no other data is processed until this returns, so this function should not |
1444 | | * be used during the interactive session. |
1445 | | */ |
1446 | | |
1447 | | int |
1448 | | ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) |
1449 | 0 | { |
1450 | 0 | struct session_state *state = ssh->state; |
1451 | 0 | int len, r, ms_remain = 0; |
1452 | 0 | struct pollfd pfd; |
1453 | 0 | char buf[8192]; |
1454 | 0 | struct timeval start; |
1455 | 0 | struct timespec timespec, *timespecp = NULL; |
1456 | |
|
1457 | 0 | DBG(debug("packet_read()")); |
1458 | | |
1459 | | /* |
1460 | | * Since we are blocking, ensure that all written packets have |
1461 | | * been sent. |
1462 | | */ |
1463 | 0 | if ((r = ssh_packet_write_wait(ssh)) != 0) |
1464 | 0 | goto out; |
1465 | | |
1466 | | /* Stay in the loop until we have received a complete packet. */ |
1467 | 0 | for (;;) { |
1468 | | /* Try to read a packet from the buffer. */ |
1469 | 0 | if ((r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p)) != 0) |
1470 | 0 | break; |
1471 | | /* If we got a packet, return it. */ |
1472 | 0 | if (*typep != SSH_MSG_NONE) |
1473 | 0 | break; |
1474 | | /* |
1475 | | * Otherwise, wait for some data to arrive, add it to the |
1476 | | * buffer, and try again. |
1477 | | */ |
1478 | 0 | pfd.fd = state->connection_in; |
1479 | 0 | pfd.events = POLLIN; |
1480 | |
|
1481 | 0 | if (state->packet_timeout_ms > 0) { |
1482 | 0 | ms_remain = state->packet_timeout_ms; |
1483 | 0 | timespecp = ×pec; |
1484 | 0 | } |
1485 | | /* Wait for some data to arrive. */ |
1486 | 0 | for (;;) { |
1487 | 0 | if (state->packet_timeout_ms > 0) { |
1488 | 0 | ms_to_timespec(×pec, ms_remain); |
1489 | 0 | monotime_tv(&start); |
1490 | 0 | } |
1491 | 0 | if ((r = ppoll(&pfd, 1, timespecp, NULL)) >= 0) |
1492 | 0 | break; |
1493 | 0 | if (errno != EAGAIN && errno != EINTR && |
1494 | 0 | errno != EWOULDBLOCK) { |
1495 | 0 | r = SSH_ERR_SYSTEM_ERROR; |
1496 | 0 | goto out; |
1497 | 0 | } |
1498 | 0 | if (state->packet_timeout_ms <= 0) |
1499 | 0 | continue; |
1500 | 0 | ms_subtract_diff(&start, &ms_remain); |
1501 | 0 | if (ms_remain <= 0) { |
1502 | 0 | r = 0; |
1503 | 0 | break; |
1504 | 0 | } |
1505 | 0 | } |
1506 | 0 | if (r == 0) { |
1507 | 0 | r = SSH_ERR_CONN_TIMEOUT; |
1508 | 0 | goto out; |
1509 | 0 | } |
1510 | | /* Read data from the socket. */ |
1511 | 0 | len = read(state->connection_in, buf, sizeof(buf)); |
1512 | 0 | if (len == 0) { |
1513 | 0 | r = SSH_ERR_CONN_CLOSED; |
1514 | 0 | goto out; |
1515 | 0 | } |
1516 | 0 | if (len == -1) { |
1517 | 0 | r = SSH_ERR_SYSTEM_ERROR; |
1518 | 0 | goto out; |
1519 | 0 | } |
1520 | | |
1521 | | /* Append it to the buffer. */ |
1522 | 0 | if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0) |
1523 | 0 | goto out; |
1524 | 0 | } |
1525 | 0 | out: |
1526 | 0 | return r; |
1527 | 0 | } |
1528 | | |
1529 | | int |
1530 | | ssh_packet_read(struct ssh *ssh) |
1531 | 0 | { |
1532 | 0 | u_char type; |
1533 | 0 | int r; |
1534 | |
|
1535 | 0 | if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0) |
1536 | 0 | fatal_fr(r, "read"); |
1537 | 0 | return type; |
1538 | 0 | } |
1539 | | |
1540 | | static int |
1541 | | ssh_packet_read_poll2_mux(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) |
1542 | 0 | { |
1543 | 0 | struct session_state *state = ssh->state; |
1544 | 0 | const u_char *cp; |
1545 | 0 | size_t need; |
1546 | 0 | int r; |
1547 | |
|
1548 | 0 | if (ssh->kex) |
1549 | 0 | return SSH_ERR_INTERNAL_ERROR; |
1550 | 0 | *typep = SSH_MSG_NONE; |
1551 | 0 | cp = sshbuf_ptr(state->input); |
1552 | 0 | if (state->packlen == 0) { |
1553 | 0 | if (sshbuf_len(state->input) < 4 + 1) |
1554 | 0 | return 0; /* packet is incomplete */ |
1555 | 0 | state->packlen = PEEK_U32(cp); |
1556 | 0 | if (state->packlen < 4 + 1 || |
1557 | 0 | state->packlen > PACKET_MAX_SIZE) |
1558 | 0 | return SSH_ERR_MESSAGE_INCOMPLETE; |
1559 | 0 | } |
1560 | 0 | need = state->packlen + 4; |
1561 | 0 | if (sshbuf_len(state->input) < need) |
1562 | 0 | return 0; /* packet is incomplete */ |
1563 | 0 | sshbuf_reset(state->incoming_packet); |
1564 | 0 | if ((r = sshbuf_put(state->incoming_packet, cp + 4, |
1565 | 0 | state->packlen)) != 0 || |
1566 | 0 | (r = sshbuf_consume(state->input, need)) != 0 || |
1567 | 0 | (r = sshbuf_get_u8(state->incoming_packet, NULL)) != 0 || |
1568 | 0 | (r = sshbuf_get_u8(state->incoming_packet, typep)) != 0) |
1569 | 0 | return r; |
1570 | 0 | if (ssh_packet_log_type(*typep)) |
1571 | 0 | debug3_f("type %u", *typep); |
1572 | | /* sshbuf_dump(state->incoming_packet, stderr); */ |
1573 | | /* reset for next packet */ |
1574 | 0 | state->packlen = 0; |
1575 | 0 | return r; |
1576 | 0 | } |
1577 | | |
1578 | | int |
1579 | | ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) |
1580 | 1.37M | { |
1581 | 1.37M | struct session_state *state = ssh->state; |
1582 | 1.37M | u_int padlen, need; |
1583 | 1.37M | u_char *cp; |
1584 | 1.37M | u_int maclen, aadlen = 0, authlen = 0, block_size; |
1585 | 1.37M | struct sshenc *enc = NULL; |
1586 | 1.37M | struct sshmac *mac = NULL; |
1587 | 1.37M | struct sshcomp *comp = NULL; |
1588 | 1.37M | int r; |
1589 | | |
1590 | 1.37M | if (state->mux) |
1591 | 0 | return ssh_packet_read_poll2_mux(ssh, typep, seqnr_p); |
1592 | | |
1593 | 1.37M | *typep = SSH_MSG_NONE; |
1594 | | |
1595 | 1.37M | if (state->packet_discard) |
1596 | 0 | return 0; |
1597 | | |
1598 | 1.37M | if (state->newkeys[MODE_IN] != NULL) { |
1599 | 410 | enc = &state->newkeys[MODE_IN]->enc; |
1600 | 410 | mac = &state->newkeys[MODE_IN]->mac; |
1601 | 410 | comp = &state->newkeys[MODE_IN]->comp; |
1602 | | /* disable mac for authenticated encryption */ |
1603 | 410 | if ((authlen = cipher_authlen(enc->cipher)) != 0) |
1604 | 0 | mac = NULL; |
1605 | 410 | } |
1606 | 1.37M | maclen = mac && mac->enabled ? mac->mac_len : 0; |
1607 | 1.37M | block_size = enc ? enc->block_size : 8; |
1608 | 1.37M | aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; |
1609 | | |
1610 | 1.37M | if (aadlen && state->packlen == 0) { |
1611 | 210 | if (cipher_get_length(state->receive_context, |
1612 | 210 | &state->packlen, state->p_read.seqnr, |
1613 | 210 | sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0) |
1614 | 0 | return 0; |
1615 | 210 | if (state->packlen < 1 + 4 || |
1616 | 208 | state->packlen > PACKET_MAX_SIZE) { |
1617 | | #ifdef PACKET_DEBUG |
1618 | | sshbuf_dump(state->input, stderr); |
1619 | | #endif |
1620 | 49 | logit("Bad packet length %u.", state->packlen); |
1621 | 49 | if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0) |
1622 | 0 | return r; |
1623 | 49 | return SSH_ERR_CONN_CORRUPT; |
1624 | 49 | } |
1625 | 161 | sshbuf_reset(state->incoming_packet); |
1626 | 1.36M | } else if (state->packlen == 0) { |
1627 | | /* |
1628 | | * check if input size is less than the cipher block size, |
1629 | | * decrypt first block and extract length of incoming packet |
1630 | | */ |
1631 | 1.36M | if (sshbuf_len(state->input) < block_size) |
1632 | 18.0k | return 0; |
1633 | 1.34M | sshbuf_reset(state->incoming_packet); |
1634 | 1.34M | if ((r = sshbuf_reserve(state->incoming_packet, block_size, |
1635 | 1.34M | &cp)) != 0) |
1636 | 0 | goto out; |
1637 | 1.34M | if ((r = cipher_crypt(state->receive_context, |
1638 | 1.34M | state->p_send.seqnr, cp, sshbuf_ptr(state->input), |
1639 | 1.34M | block_size, 0, 0)) != 0) |
1640 | 0 | goto out; |
1641 | 1.34M | state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet)); |
1642 | 1.34M | if (state->packlen < 1 + 4 || |
1643 | 1.34M | state->packlen > PACKET_MAX_SIZE) { |
1644 | | #ifdef PACKET_DEBUG |
1645 | | fprintf(stderr, "input: \n"); |
1646 | | sshbuf_dump(state->input, stderr); |
1647 | | fprintf(stderr, "incoming_packet: \n"); |
1648 | | sshbuf_dump(state->incoming_packet, stderr); |
1649 | | #endif |
1650 | 7.72k | logit("Bad packet length %u.", state->packlen); |
1651 | 7.72k | return ssh_packet_start_discard(ssh, enc, mac, 0, |
1652 | 7.72k | PACKET_MAX_SIZE); |
1653 | 7.72k | } |
1654 | 1.34M | if ((r = sshbuf_consume(state->input, block_size)) != 0) |
1655 | 0 | goto out; |
1656 | 1.34M | } |
1657 | 1.34M | DBG(debug("input: packet len %u", state->packlen+4)); |
1658 | | |
1659 | 1.34M | if (aadlen) { |
1660 | | /* only the payload is encrypted */ |
1661 | 184 | need = state->packlen; |
1662 | 1.34M | } else { |
1663 | | /* |
1664 | | * the payload size and the payload are encrypted, but we |
1665 | | * have a partial packet of block_size bytes |
1666 | | */ |
1667 | 1.34M | need = 4 + state->packlen - block_size; |
1668 | 1.34M | } |
1669 | 1.34M | DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d," |
1670 | 1.34M | " aadlen %d", block_size, need, maclen, authlen, aadlen)); |
1671 | 1.34M | if (need % block_size != 0) { |
1672 | 939 | logit("padding error: need %d block %d mod %d", |
1673 | 939 | need, block_size, need % block_size); |
1674 | 939 | return ssh_packet_start_discard(ssh, enc, mac, 0, |
1675 | 939 | PACKET_MAX_SIZE - block_size); |
1676 | 939 | } |
1677 | | /* |
1678 | | * check if the entire packet has been received and |
1679 | | * decrypt into incoming_packet: |
1680 | | * 'aadlen' bytes are unencrypted, but authenticated. |
1681 | | * 'need' bytes are encrypted, followed by either |
1682 | | * 'authlen' bytes of authentication tag or |
1683 | | * 'maclen' bytes of message authentication code. |
1684 | | */ |
1685 | 1.34M | if (sshbuf_len(state->input) < aadlen + need + authlen + maclen) |
1686 | 4.07k | return 0; /* packet is incomplete */ |
1687 | | #ifdef PACKET_DEBUG |
1688 | | fprintf(stderr, "read_poll enc/full: "); |
1689 | | sshbuf_dump(state->input, stderr); |
1690 | | #endif |
1691 | | /* EtM: check mac over encrypted input */ |
1692 | 1.33M | if (mac && mac->enabled && mac->etm) { |
1693 | 129 | if ((r = mac_check(mac, state->p_read.seqnr, |
1694 | 129 | sshbuf_ptr(state->input), aadlen + need, |
1695 | 129 | sshbuf_ptr(state->input) + aadlen + need + authlen, |
1696 | 129 | maclen)) != 0) { |
1697 | 129 | if (r == SSH_ERR_MAC_INVALID) |
1698 | 129 | logit("Corrupted MAC on input."); |
1699 | 129 | goto out; |
1700 | 129 | } |
1701 | 129 | } |
1702 | 1.33M | if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need, |
1703 | 1.33M | &cp)) != 0) |
1704 | 0 | goto out; |
1705 | 1.33M | if ((r = cipher_crypt(state->receive_context, state->p_read.seqnr, cp, |
1706 | 1.33M | sshbuf_ptr(state->input), need, aadlen, authlen)) != 0) |
1707 | 0 | goto out; |
1708 | 1.33M | if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0) |
1709 | 0 | goto out; |
1710 | 1.33M | if (mac && mac->enabled) { |
1711 | | /* Not EtM: check MAC over cleartext */ |
1712 | 151 | if (!mac->etm && (r = mac_check(mac, state->p_read.seqnr, |
1713 | 151 | sshbuf_ptr(state->incoming_packet), |
1714 | 151 | sshbuf_len(state->incoming_packet), |
1715 | 151 | sshbuf_ptr(state->input), maclen)) != 0) { |
1716 | 151 | if (r != SSH_ERR_MAC_INVALID) |
1717 | 0 | goto out; |
1718 | 151 | logit("Corrupted MAC on input."); |
1719 | 151 | if (need + block_size > PACKET_MAX_SIZE) |
1720 | 0 | return SSH_ERR_INTERNAL_ERROR; |
1721 | 151 | return ssh_packet_start_discard(ssh, enc, mac, |
1722 | 151 | sshbuf_len(state->incoming_packet), |
1723 | 151 | PACKET_MAX_SIZE - need - block_size); |
1724 | 151 | } |
1725 | | /* Remove MAC from input buffer */ |
1726 | 0 | DBG(debug("MAC #%d ok", state->p_read.seqnr)); |
1727 | 0 | if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0) |
1728 | 0 | goto out; |
1729 | 0 | } |
1730 | | |
1731 | 1.33M | if (seqnr_p != NULL) |
1732 | 1.33M | *seqnr_p = state->p_read.seqnr; |
1733 | 1.33M | if (++state->p_read.seqnr == 0) { |
1734 | 0 | if ((ssh->kex->flags & KEX_INITIAL) != 0) { |
1735 | 0 | ssh_packet_disconnect(ssh, "incoming sequence number " |
1736 | 0 | "wrapped during initial key exchange"); |
1737 | 0 | } |
1738 | 0 | logit("incoming seqnr wraps around"); |
1739 | 0 | } |
1740 | 1.33M | if (++state->p_read.packets == 0) |
1741 | 0 | if (!(ssh->compat & SSH_BUG_NOREKEY)) |
1742 | 0 | return SSH_ERR_NEED_REKEY; |
1743 | 1.33M | state->p_read.blocks += (state->packlen + 4) / block_size; |
1744 | 1.33M | state->p_read.bytes += state->packlen + 4; |
1745 | | |
1746 | | /* get padlen */ |
1747 | 1.33M | padlen = sshbuf_ptr(state->incoming_packet)[4]; |
1748 | 1.33M | DBG(debug("input: padlen %d", padlen)); |
1749 | 1.33M | if (padlen < 4) { |
1750 | 163 | if ((r = sshpkt_disconnect(ssh, |
1751 | 163 | "Corrupted padlen %d on input.", padlen)) != 0 || |
1752 | 163 | (r = ssh_packet_write_wait(ssh)) != 0) |
1753 | 163 | return r; |
1754 | 0 | return SSH_ERR_CONN_CORRUPT; |
1755 | 163 | } |
1756 | | |
1757 | | /* skip packet size + padlen, discard padding */ |
1758 | 1.33M | if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 || |
1759 | 1.33M | ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0)) |
1760 | 148 | goto out; |
1761 | | |
1762 | 1.33M | DBG(debug("input: len before de-compress %zd", |
1763 | 1.33M | sshbuf_len(state->incoming_packet))); |
1764 | 1.33M | if (comp && comp->enabled) { |
1765 | 0 | sshbuf_reset(state->compression_buffer); |
1766 | 0 | if ((r = uncompress_buffer(ssh, state->incoming_packet, |
1767 | 0 | state->compression_buffer)) != 0) |
1768 | 0 | goto out; |
1769 | 0 | sshbuf_reset(state->incoming_packet); |
1770 | 0 | if ((r = sshbuf_putb(state->incoming_packet, |
1771 | 0 | state->compression_buffer)) != 0) |
1772 | 0 | goto out; |
1773 | 0 | DBG(debug("input: len after de-compress %zd", |
1774 | 0 | sshbuf_len(state->incoming_packet))); |
1775 | 0 | } |
1776 | | /* |
1777 | | * get packet type, implies consume. |
1778 | | * return length of payload (without type field) |
1779 | | */ |
1780 | 1.33M | if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0) |
1781 | 48 | goto out; |
1782 | 1.33M | if (ssh_packet_log_type(*typep)) |
1783 | 1.33M | debug3("receive packet: type %u", *typep); |
1784 | 1.33M | if (*typep < SSH2_MSG_MIN) { |
1785 | 128 | if ((r = sshpkt_disconnect(ssh, |
1786 | 128 | "Invalid ssh2 packet type: %d", *typep)) != 0 || |
1787 | 128 | (r = ssh_packet_write_wait(ssh)) != 0) |
1788 | 128 | return r; |
1789 | 0 | return SSH_ERR_PROTOCOL_ERROR; |
1790 | 128 | } |
1791 | 1.33M | if (state->hook_in != NULL && |
1792 | 0 | (r = state->hook_in(ssh, state->incoming_packet, typep, |
1793 | 0 | state->hook_in_ctx)) != 0) |
1794 | 0 | return r; |
1795 | 1.33M | if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) |
1796 | 345 | r = ssh_packet_enable_delayed_compress(ssh); |
1797 | 1.33M | else |
1798 | 1.33M | r = 0; |
1799 | | #ifdef PACKET_DEBUG |
1800 | | fprintf(stderr, "read/plain[%d]:\r\n", *typep); |
1801 | | sshbuf_dump(state->incoming_packet, stderr); |
1802 | | #endif |
1803 | | /* reset for next packet */ |
1804 | 1.33M | state->packlen = 0; |
1805 | 1.33M | if (*typep == SSH2_MSG_NEWKEYS && ssh->kex->kex_strict) { |
1806 | 0 | debug_f("resetting read seqnr %u", state->p_read.seqnr); |
1807 | 0 | state->p_read.seqnr = 0; |
1808 | 0 | } |
1809 | | |
1810 | 1.33M | if ((r = ssh_packet_check_rekey(ssh)) != 0) |
1811 | 0 | return r; |
1812 | 1.33M | out: |
1813 | 1.33M | return r; |
1814 | 1.33M | } |
1815 | | |
1816 | | int |
1817 | | ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) |
1818 | 0 | { |
1819 | 0 | struct session_state *state = ssh->state; |
1820 | 0 | u_int reason, seqnr; |
1821 | 0 | int r; |
1822 | 0 | u_char *msg; |
1823 | 0 | const u_char *d; |
1824 | 0 | size_t len; |
1825 | |
|
1826 | 0 | for (;;) { |
1827 | 0 | msg = NULL; |
1828 | 0 | r = ssh_packet_read_poll2(ssh, typep, seqnr_p); |
1829 | 0 | if (r != 0) |
1830 | 0 | return r; |
1831 | 0 | if (*typep == 0) { |
1832 | | /* no message ready */ |
1833 | 0 | return 0; |
1834 | 0 | } |
1835 | 0 | state->keep_alive_timeouts = 0; |
1836 | 0 | DBG(debug("received packet type %d", *typep)); |
1837 | | |
1838 | | /* Always process disconnect messages */ |
1839 | 0 | if (*typep == SSH2_MSG_DISCONNECT) { |
1840 | 0 | if ((r = sshpkt_get_u32(ssh, &reason)) != 0 || |
1841 | 0 | (r = sshpkt_get_string(ssh, &msg, NULL)) != 0) |
1842 | 0 | return r; |
1843 | | /* Ignore normal client exit notifications */ |
1844 | 0 | do_log2(ssh->state->server_side && |
1845 | 0 | reason == SSH2_DISCONNECT_BY_APPLICATION ? |
1846 | 0 | SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR, |
1847 | 0 | "Received disconnect from %s port %d:" |
1848 | 0 | "%u: %.400s", ssh_remote_ipaddr(ssh), |
1849 | 0 | ssh_remote_port(ssh), reason, msg); |
1850 | 0 | free(msg); |
1851 | 0 | return SSH_ERR_DISCONNECTED; |
1852 | 0 | } |
1853 | | |
1854 | | /* |
1855 | | * Do not implicitly handle any messages here during initial |
1856 | | * KEX when in strict mode. They will be need to be allowed |
1857 | | * explicitly by the KEX dispatch table or they will generate |
1858 | | * protocol errors. |
1859 | | */ |
1860 | 0 | if (ssh->kex != NULL && |
1861 | 0 | (ssh->kex->flags & KEX_INITIAL) && ssh->kex->kex_strict) |
1862 | 0 | return 0; |
1863 | | /* Implicitly handle transport-level messages */ |
1864 | 0 | switch (*typep) { |
1865 | 0 | case SSH2_MSG_IGNORE: |
1866 | 0 | debug3("Received SSH2_MSG_IGNORE"); |
1867 | 0 | break; |
1868 | 0 | case SSH2_MSG_DEBUG: |
1869 | 0 | if ((r = sshpkt_get_u8(ssh, NULL)) != 0 || |
1870 | 0 | (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 || |
1871 | 0 | (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) { |
1872 | 0 | free(msg); |
1873 | 0 | return r; |
1874 | 0 | } |
1875 | 0 | debug("Remote: %.900s", msg); |
1876 | 0 | free(msg); |
1877 | 0 | break; |
1878 | 0 | case SSH2_MSG_UNIMPLEMENTED: |
1879 | 0 | if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0) |
1880 | 0 | return r; |
1881 | 0 | debug("Received SSH2_MSG_UNIMPLEMENTED for %u", |
1882 | 0 | seqnr); |
1883 | 0 | break; |
1884 | 0 | case SSH2_MSG_PING: |
1885 | 0 | if ((r = sshpkt_get_string_direct(ssh, &d, &len)) != 0) |
1886 | 0 | return r; |
1887 | 0 | DBG(debug("Received SSH2_MSG_PING len %zu", len)); |
1888 | 0 | if (!ssh->state->after_authentication) { |
1889 | 0 | DBG(debug("Won't reply to PING in preauth")); |
1890 | 0 | break; |
1891 | 0 | } |
1892 | 0 | if (ssh_packet_is_rekeying(ssh)) { |
1893 | 0 | DBG(debug("Won't reply to PING during KEX")); |
1894 | 0 | break; |
1895 | 0 | } |
1896 | 0 | if ((r = sshpkt_start(ssh, SSH2_MSG_PONG)) != 0 || |
1897 | 0 | (r = sshpkt_put_string(ssh, d, len)) != 0 || |
1898 | 0 | (r = sshpkt_send(ssh)) != 0) |
1899 | 0 | return r; |
1900 | 0 | break; |
1901 | 0 | case SSH2_MSG_PONG: |
1902 | 0 | if ((r = sshpkt_get_string_direct(ssh, |
1903 | 0 | NULL, &len)) != 0) |
1904 | 0 | return r; |
1905 | 0 | DBG(debug("Received SSH2_MSG_PONG len %zu", len)); |
1906 | 0 | break; |
1907 | 0 | default: |
1908 | 0 | return 0; |
1909 | 0 | } |
1910 | 0 | } |
1911 | 0 | } |
1912 | | |
1913 | | /* |
1914 | | * Buffers the supplied input data. This is intended to be used together |
1915 | | * with packet_read_poll(). |
1916 | | */ |
1917 | | int |
1918 | | ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len) |
1919 | 0 | { |
1920 | 0 | struct session_state *state = ssh->state; |
1921 | 0 | int r; |
1922 | |
|
1923 | 0 | if (state->packet_discard) { |
1924 | 0 | state->keep_alive_timeouts = 0; /* ?? */ |
1925 | 0 | if (len >= state->packet_discard) { |
1926 | 0 | if ((r = ssh_packet_stop_discard(ssh)) != 0) |
1927 | 0 | return r; |
1928 | 0 | } |
1929 | 0 | state->packet_discard -= len; |
1930 | 0 | return 0; |
1931 | 0 | } |
1932 | 0 | if ((r = sshbuf_put(state->input, buf, len)) != 0) |
1933 | 0 | return r; |
1934 | | |
1935 | 0 | return 0; |
1936 | 0 | } |
1937 | | |
1938 | | /* Reads and buffers data from the specified fd */ |
1939 | | int |
1940 | | ssh_packet_process_read(struct ssh *ssh, int fd) |
1941 | 0 | { |
1942 | 0 | struct session_state *state = ssh->state; |
1943 | 0 | int r; |
1944 | 0 | size_t rlen; |
1945 | |
|
1946 | 0 | if ((r = sshbuf_read(fd, state->input, PACKET_MAX_SIZE, &rlen)) != 0) |
1947 | 0 | return r; |
1948 | | |
1949 | 0 | if (state->packet_discard) { |
1950 | 0 | if ((r = sshbuf_consume_end(state->input, rlen)) != 0) |
1951 | 0 | return r; |
1952 | 0 | state->keep_alive_timeouts = 0; /* ?? */ |
1953 | 0 | if (rlen >= state->packet_discard) { |
1954 | 0 | if ((r = ssh_packet_stop_discard(ssh)) != 0) |
1955 | 0 | return r; |
1956 | 0 | } |
1957 | 0 | state->packet_discard -= rlen; |
1958 | 0 | return 0; |
1959 | 0 | } |
1960 | 0 | return 0; |
1961 | 0 | } |
1962 | | |
1963 | | int |
1964 | | ssh_packet_remaining(struct ssh *ssh) |
1965 | 0 | { |
1966 | 0 | return sshbuf_len(ssh->state->incoming_packet); |
1967 | 0 | } |
1968 | | |
1969 | | /* |
1970 | | * Sends a diagnostic message from the server to the client. This message |
1971 | | * can be sent at any time (but not while constructing another message). The |
1972 | | * message is printed immediately, but only if the client is being executed |
1973 | | * in verbose mode. These messages are primarily intended to ease debugging |
1974 | | * authentication problems. The length of the formatted message must not |
1975 | | * exceed 1024 bytes. This will automatically call ssh_packet_write_wait. |
1976 | | */ |
1977 | | void |
1978 | | ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...) |
1979 | 0 | { |
1980 | 0 | char buf[1024]; |
1981 | 0 | va_list args; |
1982 | 0 | int r; |
1983 | |
|
1984 | 0 | if ((ssh->compat & SSH_BUG_DEBUG)) |
1985 | 0 | return; |
1986 | | |
1987 | 0 | va_start(args, fmt); |
1988 | 0 | vsnprintf(buf, sizeof(buf), fmt, args); |
1989 | 0 | va_end(args); |
1990 | |
|
1991 | 0 | debug3("sending debug message: %s", buf); |
1992 | |
|
1993 | 0 | if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 || |
1994 | 0 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */ |
1995 | 0 | (r = sshpkt_put_cstring(ssh, buf)) != 0 || |
1996 | 0 | (r = sshpkt_put_cstring(ssh, "")) != 0 || |
1997 | 0 | (r = sshpkt_send(ssh)) != 0 || |
1998 | 0 | (r = ssh_packet_write_wait(ssh)) != 0) |
1999 | 0 | fatal_fr(r, "send DEBUG"); |
2000 | 0 | } |
2001 | | |
2002 | | void |
2003 | | sshpkt_fmt_connection_id(struct ssh *ssh, char *s, size_t l) |
2004 | 0 | { |
2005 | 0 | snprintf(s, l, "%.200s%s%s port %d", |
2006 | 0 | ssh->log_preamble ? ssh->log_preamble : "", |
2007 | 0 | ssh->log_preamble ? " " : "", |
2008 | 0 | ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); |
2009 | 0 | } |
2010 | | |
2011 | | /* |
2012 | | * Pretty-print connection-terminating errors and exit. |
2013 | | */ |
2014 | | static void |
2015 | | sshpkt_vfatal(struct ssh *ssh, int r, const char *fmt, va_list ap) |
2016 | 0 | { |
2017 | 0 | char *tag = NULL, remote_id[512]; |
2018 | 0 | int oerrno = errno; |
2019 | |
|
2020 | 0 | sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id)); |
2021 | |
|
2022 | 0 | switch (r) { |
2023 | 0 | case SSH_ERR_CONN_CLOSED: |
2024 | 0 | ssh_packet_clear_keys(ssh); |
2025 | 0 | logdie("Connection closed by %s", remote_id); |
2026 | 0 | case SSH_ERR_CONN_TIMEOUT: |
2027 | 0 | ssh_packet_clear_keys(ssh); |
2028 | 0 | logdie("Connection %s %s timed out", |
2029 | 0 | ssh->state->server_side ? "from" : "to", remote_id); |
2030 | 0 | case SSH_ERR_DISCONNECTED: |
2031 | 0 | ssh_packet_clear_keys(ssh); |
2032 | 0 | logdie("Disconnected from %s", remote_id); |
2033 | 0 | case SSH_ERR_SYSTEM_ERROR: |
2034 | 0 | if (errno == ECONNRESET) { |
2035 | 0 | ssh_packet_clear_keys(ssh); |
2036 | 0 | logdie("Connection reset by %s", remote_id); |
2037 | 0 | } |
2038 | | /* FALLTHROUGH */ |
2039 | 0 | case SSH_ERR_NO_CIPHER_ALG_MATCH: |
2040 | 0 | case SSH_ERR_NO_MAC_ALG_MATCH: |
2041 | 0 | case SSH_ERR_NO_COMPRESS_ALG_MATCH: |
2042 | 0 | case SSH_ERR_NO_KEX_ALG_MATCH: |
2043 | 0 | case SSH_ERR_NO_HOSTKEY_ALG_MATCH: |
2044 | 0 | if (ssh->kex && ssh->kex->failed_choice) { |
2045 | 0 | ssh_packet_clear_keys(ssh); |
2046 | 0 | errno = oerrno; |
2047 | 0 | logdie("Unable to negotiate with %s: %s. " |
2048 | 0 | "Their offer: %s", remote_id, ssh_err(r), |
2049 | 0 | ssh->kex->failed_choice); |
2050 | 0 | } |
2051 | | /* FALLTHROUGH */ |
2052 | 0 | default: |
2053 | 0 | if (vasprintf(&tag, fmt, ap) == -1) { |
2054 | 0 | ssh_packet_clear_keys(ssh); |
2055 | 0 | logdie_f("could not allocate failure message"); |
2056 | 0 | } |
2057 | 0 | ssh_packet_clear_keys(ssh); |
2058 | 0 | errno = oerrno; |
2059 | 0 | logdie_r(r, "%s%sConnection %s %s", |
2060 | 0 | tag != NULL ? tag : "", tag != NULL ? ": " : "", |
2061 | 0 | ssh->state->server_side ? "from" : "to", remote_id); |
2062 | 0 | } |
2063 | 0 | } |
2064 | | |
2065 | | void |
2066 | | sshpkt_fatal(struct ssh *ssh, int r, const char *fmt, ...) |
2067 | 0 | { |
2068 | 0 | va_list ap; |
2069 | |
|
2070 | 0 | va_start(ap, fmt); |
2071 | 0 | sshpkt_vfatal(ssh, r, fmt, ap); |
2072 | | /* NOTREACHED */ |
2073 | 0 | va_end(ap); |
2074 | 0 | logdie_f("should have exited"); |
2075 | 0 | } |
2076 | | |
2077 | | /* |
2078 | | * Logs the error plus constructs and sends a disconnect packet, closes the |
2079 | | * connection, and exits. This function never returns. The error message |
2080 | | * should not contain a newline. The length of the formatted message must |
2081 | | * not exceed 1024 bytes. |
2082 | | */ |
2083 | | void |
2084 | | ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...) |
2085 | 0 | { |
2086 | 0 | char buf[1024], remote_id[512]; |
2087 | 0 | va_list args; |
2088 | 0 | int r; |
2089 | | |
2090 | | /* Guard against recursive invocations. */ |
2091 | 0 | if (ssh->state->disconnecting) |
2092 | 0 | fatal("packet_disconnect called recursively."); |
2093 | 0 | ssh->state->disconnecting = 1; |
2094 | | |
2095 | | /* |
2096 | | * Format the message. Note that the caller must make sure the |
2097 | | * message is of limited size. |
2098 | | */ |
2099 | 0 | sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id)); |
2100 | 0 | va_start(args, fmt); |
2101 | 0 | vsnprintf(buf, sizeof(buf), fmt, args); |
2102 | 0 | va_end(args); |
2103 | | |
2104 | | /* Display the error locally */ |
2105 | 0 | logit("Disconnecting %s: %.100s", remote_id, buf); |
2106 | | |
2107 | | /* |
2108 | | * Send the disconnect message to the other side, and wait |
2109 | | * for it to get sent. |
2110 | | */ |
2111 | 0 | if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0) |
2112 | 0 | sshpkt_fatal(ssh, r, "%s", __func__); |
2113 | | |
2114 | 0 | if ((r = ssh_packet_write_wait(ssh)) != 0) |
2115 | 0 | sshpkt_fatal(ssh, r, "%s", __func__); |
2116 | | |
2117 | | /* Close the connection. */ |
2118 | 0 | ssh_packet_close(ssh); |
2119 | 0 | cleanup_exit(255); |
2120 | 0 | } |
2121 | | |
2122 | | /* |
2123 | | * Checks if there is any buffered output, and tries to write some of |
2124 | | * the output. |
2125 | | */ |
2126 | | int |
2127 | | ssh_packet_write_poll(struct ssh *ssh) |
2128 | 291 | { |
2129 | 291 | struct session_state *state = ssh->state; |
2130 | 291 | int len = sshbuf_len(state->output); |
2131 | 291 | int r; |
2132 | | |
2133 | 291 | if (len > 0) { |
2134 | 291 | len = write(state->connection_out, |
2135 | 291 | sshbuf_ptr(state->output), len); |
2136 | 291 | if (len == -1) { |
2137 | 291 | if (errno == EINTR || errno == EAGAIN || |
2138 | 291 | errno == EWOULDBLOCK) |
2139 | 0 | return 0; |
2140 | 291 | return SSH_ERR_SYSTEM_ERROR; |
2141 | 291 | } |
2142 | 0 | if (len == 0) |
2143 | 0 | return SSH_ERR_CONN_CLOSED; |
2144 | 0 | if ((r = sshbuf_consume(state->output, len)) != 0) |
2145 | 0 | return r; |
2146 | 0 | } |
2147 | 0 | return 0; |
2148 | 291 | } |
2149 | | |
2150 | | /* |
2151 | | * Calls packet_write_poll repeatedly until all pending output data has been |
2152 | | * written. |
2153 | | */ |
2154 | | int |
2155 | | ssh_packet_write_wait(struct ssh *ssh) |
2156 | 291 | { |
2157 | 291 | int ret, r, ms_remain = 0; |
2158 | 291 | struct timeval start; |
2159 | 291 | struct timespec timespec, *timespecp = NULL; |
2160 | 291 | struct session_state *state = ssh->state; |
2161 | 291 | struct pollfd pfd; |
2162 | | |
2163 | 291 | if ((r = ssh_packet_write_poll(ssh)) != 0) |
2164 | 291 | return r; |
2165 | 0 | while (ssh_packet_have_data_to_write(ssh)) { |
2166 | 0 | pfd.fd = state->connection_out; |
2167 | 0 | pfd.events = POLLOUT; |
2168 | |
|
2169 | 0 | if (state->packet_timeout_ms > 0) { |
2170 | 0 | ms_remain = state->packet_timeout_ms; |
2171 | 0 | timespecp = ×pec; |
2172 | 0 | } |
2173 | 0 | for (;;) { |
2174 | 0 | if (state->packet_timeout_ms > 0) { |
2175 | 0 | ms_to_timespec(×pec, ms_remain); |
2176 | 0 | monotime_tv(&start); |
2177 | 0 | } |
2178 | 0 | if ((ret = ppoll(&pfd, 1, timespecp, NULL)) >= 0) |
2179 | 0 | break; |
2180 | 0 | if (errno != EAGAIN && errno != EINTR && |
2181 | 0 | errno != EWOULDBLOCK) |
2182 | 0 | break; |
2183 | 0 | if (state->packet_timeout_ms <= 0) |
2184 | 0 | continue; |
2185 | 0 | ms_subtract_diff(&start, &ms_remain); |
2186 | 0 | if (ms_remain <= 0) { |
2187 | 0 | ret = 0; |
2188 | 0 | break; |
2189 | 0 | } |
2190 | 0 | } |
2191 | 0 | if (ret == 0) |
2192 | 0 | return SSH_ERR_CONN_TIMEOUT; |
2193 | 0 | if ((r = ssh_packet_write_poll(ssh)) != 0) |
2194 | 0 | return r; |
2195 | 0 | } |
2196 | 0 | return 0; |
2197 | 0 | } |
2198 | | |
2199 | | /* Returns true if there is buffered data to write to the connection. */ |
2200 | | |
2201 | | int |
2202 | | ssh_packet_have_data_to_write(struct ssh *ssh) |
2203 | 0 | { |
2204 | 0 | return sshbuf_len(ssh->state->output) != 0; |
2205 | 0 | } |
2206 | | |
2207 | | /* Returns true if there is not too much data to write to the connection. */ |
2208 | | |
2209 | | int |
2210 | | ssh_packet_not_very_much_data_to_write(struct ssh *ssh) |
2211 | 0 | { |
2212 | 0 | if (ssh->state->interactive_mode) |
2213 | 0 | return sshbuf_len(ssh->state->output) < 16384; |
2214 | 0 | else |
2215 | 0 | return sshbuf_len(ssh->state->output) < 128 * 1024; |
2216 | 0 | } |
2217 | | |
2218 | | /* |
2219 | | * returns true when there are at most a few keystrokes of data to write |
2220 | | * and the connection is in interactive mode. |
2221 | | */ |
2222 | | |
2223 | | int |
2224 | | ssh_packet_interactive_data_to_write(struct ssh *ssh) |
2225 | 0 | { |
2226 | 0 | return ssh->state->interactive_mode && |
2227 | 0 | sshbuf_len(ssh->state->output) < 256; |
2228 | 0 | } |
2229 | | |
2230 | | static void |
2231 | | apply_qos(struct ssh *ssh) |
2232 | 0 | { |
2233 | 0 | struct session_state *state = ssh->state; |
2234 | 0 | int qos = state->interactive_mode ? |
2235 | 0 | state->qos_interactive : state->qos_other; |
2236 | |
|
2237 | 0 | if (!ssh_packet_connection_is_on_socket(ssh)) |
2238 | 0 | return; |
2239 | 0 | if (!state->nodelay_set) { |
2240 | 0 | set_nodelay(state->connection_in); |
2241 | 0 | state->nodelay_set = 1; |
2242 | 0 | } |
2243 | 0 | set_sock_tos(ssh->state->connection_in, qos); |
2244 | 0 | } |
2245 | | |
2246 | | /* Informs that the current session is interactive. */ |
2247 | | void |
2248 | | ssh_packet_set_interactive(struct ssh *ssh, int interactive) |
2249 | 0 | { |
2250 | 0 | struct session_state *state = ssh->state; |
2251 | |
|
2252 | 0 | state->interactive_mode = interactive; |
2253 | 0 | apply_qos(ssh); |
2254 | 0 | } |
2255 | | |
2256 | | /* Set QoS flags to be used for interactive and non-interactive sessions */ |
2257 | | void |
2258 | | ssh_packet_set_qos(struct ssh *ssh, int qos_interactive, int qos_other) |
2259 | 0 | { |
2260 | 0 | struct session_state *state = ssh->state; |
2261 | |
|
2262 | 0 | state->qos_interactive = qos_interactive; |
2263 | 0 | state->qos_other = qos_other; |
2264 | 0 | apply_qos(ssh); |
2265 | 0 | } |
2266 | | |
2267 | | int |
2268 | | ssh_packet_set_maxsize(struct ssh *ssh, u_int s) |
2269 | 0 | { |
2270 | 0 | struct session_state *state = ssh->state; |
2271 | |
|
2272 | 0 | if (state->set_maxsize_called) { |
2273 | 0 | logit_f("called twice: old %d new %d", |
2274 | 0 | state->max_packet_size, s); |
2275 | 0 | return -1; |
2276 | 0 | } |
2277 | 0 | if (s < 4 * 1024 || s > 1024 * 1024) { |
2278 | 0 | logit_f("bad size %d", s); |
2279 | 0 | return -1; |
2280 | 0 | } |
2281 | 0 | state->set_maxsize_called = 1; |
2282 | 0 | debug_f("setting to %d", s); |
2283 | 0 | state->max_packet_size = s; |
2284 | 0 | return s; |
2285 | 0 | } |
2286 | | |
2287 | | int |
2288 | | ssh_packet_inc_alive_timeouts(struct ssh *ssh) |
2289 | 0 | { |
2290 | 0 | return ++ssh->state->keep_alive_timeouts; |
2291 | 0 | } |
2292 | | |
2293 | | void |
2294 | | ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka) |
2295 | 0 | { |
2296 | 0 | ssh->state->keep_alive_timeouts = ka; |
2297 | 0 | } |
2298 | | |
2299 | | u_int |
2300 | | ssh_packet_get_maxsize(struct ssh *ssh) |
2301 | 0 | { |
2302 | 0 | return ssh->state->max_packet_size; |
2303 | 0 | } |
2304 | | |
2305 | | void |
2306 | | ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, u_int32_t seconds) |
2307 | 0 | { |
2308 | 0 | debug3("rekey after %llu bytes, %u seconds", (unsigned long long)bytes, |
2309 | 0 | (unsigned int)seconds); |
2310 | 0 | ssh->state->rekey_limit = bytes; |
2311 | 0 | ssh->state->rekey_interval = seconds; |
2312 | 0 | } |
2313 | | |
2314 | | time_t |
2315 | | ssh_packet_get_rekey_timeout(struct ssh *ssh) |
2316 | 0 | { |
2317 | 0 | time_t seconds; |
2318 | |
|
2319 | 0 | seconds = ssh->state->rekey_time + ssh->state->rekey_interval - |
2320 | 0 | monotime(); |
2321 | 0 | return (seconds <= 0 ? 1 : seconds); |
2322 | 0 | } |
2323 | | |
2324 | | void |
2325 | | ssh_packet_set_server(struct ssh *ssh) |
2326 | 88.9k | { |
2327 | 88.9k | ssh->state->server_side = 1; |
2328 | 88.9k | ssh->kex->server = 1; /* XXX unify? */ |
2329 | 88.9k | } |
2330 | | |
2331 | | void |
2332 | | ssh_packet_set_authenticated(struct ssh *ssh) |
2333 | 0 | { |
2334 | 0 | ssh->state->after_authentication = 1; |
2335 | 0 | } |
2336 | | |
2337 | | void * |
2338 | | ssh_packet_get_input(struct ssh *ssh) |
2339 | 355k | { |
2340 | 355k | return (void *)ssh->state->input; |
2341 | 355k | } |
2342 | | |
2343 | | void * |
2344 | | ssh_packet_get_output(struct ssh *ssh) |
2345 | 474k | { |
2346 | 474k | return (void *)ssh->state->output; |
2347 | 474k | } |
2348 | | |
2349 | | /* Reset after_authentication and reset compression in post-auth privsep */ |
2350 | | static int |
2351 | | ssh_packet_set_postauth(struct ssh *ssh) |
2352 | 0 | { |
2353 | 0 | int r; |
2354 | |
|
2355 | 0 | debug_f("called"); |
2356 | | /* This was set in net child, but is not visible in user child */ |
2357 | 0 | ssh->state->after_authentication = 1; |
2358 | 0 | ssh->state->rekeying = 0; |
2359 | 0 | if ((r = ssh_packet_enable_delayed_compress(ssh)) != 0) |
2360 | 0 | return r; |
2361 | 0 | return 0; |
2362 | 0 | } |
2363 | | |
2364 | | /* Packet state (de-)serialization for privsep */ |
2365 | | |
2366 | | /* turn kex into a blob for packet state serialization */ |
2367 | | static int |
2368 | | kex_to_blob(struct sshbuf *m, struct kex *kex) |
2369 | 0 | { |
2370 | 0 | int r; |
2371 | |
|
2372 | 0 | if ((r = sshbuf_put_u32(m, kex->we_need)) != 0 || |
2373 | 0 | (r = sshbuf_put_cstring(m, kex->hostkey_alg)) != 0 || |
2374 | 0 | (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 || |
2375 | 0 | (r = sshbuf_put_u32(m, kex->hostkey_nid)) != 0 || |
2376 | 0 | (r = sshbuf_put_u32(m, kex->kex_type)) != 0 || |
2377 | 0 | (r = sshbuf_put_u32(m, kex->kex_strict)) != 0 || |
2378 | 0 | (r = sshbuf_put_stringb(m, kex->my)) != 0 || |
2379 | 0 | (r = sshbuf_put_stringb(m, kex->peer)) != 0 || |
2380 | 0 | (r = sshbuf_put_stringb(m, kex->client_version)) != 0 || |
2381 | 0 | (r = sshbuf_put_stringb(m, kex->server_version)) != 0 || |
2382 | 0 | (r = sshbuf_put_stringb(m, kex->session_id)) != 0 || |
2383 | 0 | (r = sshbuf_put_u32(m, kex->flags)) != 0) |
2384 | 0 | return r; |
2385 | 0 | return 0; |
2386 | 0 | } |
2387 | | |
2388 | | /* turn key exchange results into a blob for packet state serialization */ |
2389 | | static int |
2390 | | newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode) |
2391 | 0 | { |
2392 | 0 | struct sshbuf *b; |
2393 | 0 | struct sshcipher_ctx *cc; |
2394 | 0 | struct sshcomp *comp; |
2395 | 0 | struct sshenc *enc; |
2396 | 0 | struct sshmac *mac; |
2397 | 0 | struct newkeys *newkey; |
2398 | 0 | int r; |
2399 | |
|
2400 | 0 | if ((newkey = ssh->state->newkeys[mode]) == NULL) |
2401 | 0 | return SSH_ERR_INTERNAL_ERROR; |
2402 | 0 | enc = &newkey->enc; |
2403 | 0 | mac = &newkey->mac; |
2404 | 0 | comp = &newkey->comp; |
2405 | 0 | cc = (mode == MODE_OUT) ? ssh->state->send_context : |
2406 | 0 | ssh->state->receive_context; |
2407 | 0 | if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0) |
2408 | 0 | return r; |
2409 | 0 | if ((b = sshbuf_new()) == NULL) |
2410 | 0 | return SSH_ERR_ALLOC_FAIL; |
2411 | 0 | if ((r = sshbuf_put_cstring(b, enc->name)) != 0 || |
2412 | 0 | (r = sshbuf_put_u32(b, enc->enabled)) != 0 || |
2413 | 0 | (r = sshbuf_put_u32(b, enc->block_size)) != 0 || |
2414 | 0 | (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 || |
2415 | 0 | (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0) |
2416 | 0 | goto out; |
2417 | 0 | if (cipher_authlen(enc->cipher) == 0) { |
2418 | 0 | if ((r = sshbuf_put_cstring(b, mac->name)) != 0 || |
2419 | 0 | (r = sshbuf_put_u32(b, mac->enabled)) != 0 || |
2420 | 0 | (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0) |
2421 | 0 | goto out; |
2422 | 0 | } |
2423 | 0 | if ((r = sshbuf_put_u32(b, comp->type)) != 0 || |
2424 | 0 | (r = sshbuf_put_cstring(b, comp->name)) != 0) |
2425 | 0 | goto out; |
2426 | 0 | r = sshbuf_put_stringb(m, b); |
2427 | 0 | out: |
2428 | 0 | sshbuf_free(b); |
2429 | 0 | return r; |
2430 | 0 | } |
2431 | | |
2432 | | /* serialize packet state into a blob */ |
2433 | | int |
2434 | | ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m) |
2435 | 0 | { |
2436 | 0 | struct session_state *state = ssh->state; |
2437 | 0 | int r; |
2438 | |
|
2439 | 0 | #define ENCODE_INT(v) (((v) < 0) ? 0xFFFFFFFF : (u_int)v) |
2440 | 0 | if ((r = kex_to_blob(m, ssh->kex)) != 0 || |
2441 | 0 | (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 || |
2442 | 0 | (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 || |
2443 | 0 | (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 || |
2444 | 0 | (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 || |
2445 | 0 | (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 || |
2446 | 0 | (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 || |
2447 | 0 | (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 || |
2448 | 0 | (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 || |
2449 | 0 | (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 || |
2450 | 0 | (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 || |
2451 | 0 | (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 || |
2452 | 0 | (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0 || |
2453 | 0 | (r = sshbuf_put_stringb(m, state->input)) != 0 || |
2454 | 0 | (r = sshbuf_put_stringb(m, state->output)) != 0 || |
2455 | 0 | (r = sshbuf_put_u32(m, ENCODE_INT(state->interactive_mode))) != 0 || |
2456 | 0 | (r = sshbuf_put_u32(m, ENCODE_INT(state->qos_interactive))) != 0 || |
2457 | 0 | (r = sshbuf_put_u32(m, ENCODE_INT(state->qos_other))) != 0) |
2458 | 0 | return r; |
2459 | 0 | #undef ENCODE_INT |
2460 | 0 | return 0; |
2461 | 0 | } |
2462 | | |
2463 | | /* restore key exchange results from blob for packet state de-serialization */ |
2464 | | static int |
2465 | | newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode) |
2466 | 0 | { |
2467 | 0 | struct sshbuf *b = NULL; |
2468 | 0 | struct sshcomp *comp; |
2469 | 0 | struct sshenc *enc; |
2470 | 0 | struct sshmac *mac; |
2471 | 0 | struct newkeys *newkey = NULL; |
2472 | 0 | size_t keylen, ivlen, maclen; |
2473 | 0 | int r; |
2474 | |
|
2475 | 0 | if ((newkey = calloc(1, sizeof(*newkey))) == NULL) { |
2476 | 0 | r = SSH_ERR_ALLOC_FAIL; |
2477 | 0 | goto out; |
2478 | 0 | } |
2479 | 0 | if ((r = sshbuf_froms(m, &b)) != 0) |
2480 | 0 | goto out; |
2481 | | #ifdef DEBUG_PK |
2482 | | sshbuf_dump(b, stderr); |
2483 | | #endif |
2484 | 0 | enc = &newkey->enc; |
2485 | 0 | mac = &newkey->mac; |
2486 | 0 | comp = &newkey->comp; |
2487 | |
|
2488 | 0 | if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 || |
2489 | 0 | (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 || |
2490 | 0 | (r = sshbuf_get_u32(b, &enc->block_size)) != 0 || |
2491 | 0 | (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 || |
2492 | 0 | (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0) |
2493 | 0 | goto out; |
2494 | 0 | if ((enc->cipher = cipher_by_name(enc->name)) == NULL) { |
2495 | 0 | r = SSH_ERR_INVALID_FORMAT; |
2496 | 0 | goto out; |
2497 | 0 | } |
2498 | 0 | if (cipher_authlen(enc->cipher) == 0) { |
2499 | 0 | if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0) |
2500 | 0 | goto out; |
2501 | 0 | if ((r = mac_setup(mac, mac->name)) != 0) |
2502 | 0 | goto out; |
2503 | 0 | if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 || |
2504 | 0 | (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0) |
2505 | 0 | goto out; |
2506 | 0 | if (maclen > mac->key_len) { |
2507 | 0 | r = SSH_ERR_INVALID_FORMAT; |
2508 | 0 | goto out; |
2509 | 0 | } |
2510 | 0 | mac->key_len = maclen; |
2511 | 0 | } |
2512 | 0 | if ((r = sshbuf_get_u32(b, &comp->type)) != 0 || |
2513 | 0 | (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0) |
2514 | 0 | goto out; |
2515 | 0 | if (sshbuf_len(b) != 0) { |
2516 | 0 | r = SSH_ERR_INVALID_FORMAT; |
2517 | 0 | goto out; |
2518 | 0 | } |
2519 | 0 | enc->key_len = keylen; |
2520 | 0 | enc->iv_len = ivlen; |
2521 | 0 | ssh->kex->newkeys[mode] = newkey; |
2522 | 0 | newkey = NULL; |
2523 | 0 | r = 0; |
2524 | 0 | out: |
2525 | 0 | free(newkey); |
2526 | 0 | sshbuf_free(b); |
2527 | 0 | return r; |
2528 | 0 | } |
2529 | | |
2530 | | /* restore kex from blob for packet state de-serialization */ |
2531 | | static int |
2532 | | kex_from_blob(struct sshbuf *m, struct kex **kexp) |
2533 | 0 | { |
2534 | 0 | struct kex *kex; |
2535 | 0 | int r; |
2536 | |
|
2537 | 0 | if ((kex = kex_new()) == NULL) |
2538 | 0 | return SSH_ERR_ALLOC_FAIL; |
2539 | 0 | if ((r = sshbuf_get_u32(m, &kex->we_need)) != 0 || |
2540 | 0 | (r = sshbuf_get_cstring(m, &kex->hostkey_alg, NULL)) != 0 || |
2541 | 0 | (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 || |
2542 | 0 | (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_nid)) != 0 || |
2543 | 0 | (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 || |
2544 | 0 | (r = sshbuf_get_u32(m, &kex->kex_strict)) != 0 || |
2545 | 0 | (r = sshbuf_get_stringb(m, kex->my)) != 0 || |
2546 | 0 | (r = sshbuf_get_stringb(m, kex->peer)) != 0 || |
2547 | 0 | (r = sshbuf_get_stringb(m, kex->client_version)) != 0 || |
2548 | 0 | (r = sshbuf_get_stringb(m, kex->server_version)) != 0 || |
2549 | 0 | (r = sshbuf_get_stringb(m, kex->session_id)) != 0 || |
2550 | 0 | (r = sshbuf_get_u32(m, &kex->flags)) != 0) |
2551 | 0 | goto out; |
2552 | 0 | kex->server = 1; |
2553 | 0 | kex->done = 1; |
2554 | 0 | r = 0; |
2555 | 0 | out: |
2556 | 0 | if (r != 0 || kexp == NULL) { |
2557 | 0 | kex_free(kex); |
2558 | 0 | if (kexp != NULL) |
2559 | 0 | *kexp = NULL; |
2560 | 0 | } else { |
2561 | 0 | kex_free(*kexp); |
2562 | 0 | *kexp = kex; |
2563 | 0 | } |
2564 | 0 | return r; |
2565 | 0 | } |
2566 | | |
2567 | | /* |
2568 | | * Restore packet state from content of blob 'm' (de-serialization). |
2569 | | * Note that 'm' will be partially consumed on parsing or any other errors. |
2570 | | */ |
2571 | | int |
2572 | | ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m) |
2573 | 0 | { |
2574 | 0 | struct session_state *state = ssh->state; |
2575 | 0 | const u_char *input, *output; |
2576 | 0 | size_t ilen, olen; |
2577 | 0 | int r; |
2578 | 0 | u_int interactive, qos_interactive, qos_other; |
2579 | |
|
2580 | 0 | if ((r = kex_from_blob(m, &ssh->kex)) != 0 || |
2581 | 0 | (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 || |
2582 | 0 | (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 || |
2583 | 0 | (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 || |
2584 | 0 | (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 || |
2585 | 0 | (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 || |
2586 | 0 | (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 || |
2587 | 0 | (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 || |
2588 | 0 | (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 || |
2589 | 0 | (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 || |
2590 | 0 | (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 || |
2591 | 0 | (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 || |
2592 | 0 | (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0) |
2593 | 0 | return r; |
2594 | | /* |
2595 | | * We set the time here so that in post-auth privsep child we |
2596 | | * count from the completion of the authentication. |
2597 | | */ |
2598 | 0 | state->rekey_time = monotime(); |
2599 | | /* XXX ssh_set_newkeys overrides p_read.packets? XXX */ |
2600 | 0 | if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 || |
2601 | 0 | (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0) |
2602 | 0 | return r; |
2603 | | |
2604 | 0 | if ((r = ssh_packet_set_postauth(ssh)) != 0) |
2605 | 0 | return r; |
2606 | | |
2607 | 0 | sshbuf_reset(state->input); |
2608 | 0 | sshbuf_reset(state->output); |
2609 | 0 | if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 || |
2610 | 0 | (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 || |
2611 | 0 | (r = sshbuf_put(state->input, input, ilen)) != 0 || |
2612 | 0 | (r = sshbuf_put(state->output, output, olen)) != 0) |
2613 | 0 | return r; |
2614 | | |
2615 | 0 | if ((r = sshbuf_get_u32(m, &interactive)) != 0 || |
2616 | 0 | (r = sshbuf_get_u32(m, &qos_interactive)) != 0 || |
2617 | 0 | (r = sshbuf_get_u32(m, &qos_other)) != 0) |
2618 | 0 | return r; |
2619 | 0 | #define DECODE_INT(v) ((v) > INT_MAX ? -1 : (int)(v)) |
2620 | 0 | state->interactive_mode = DECODE_INT(interactive); |
2621 | 0 | state->qos_interactive = DECODE_INT(qos_interactive); |
2622 | 0 | state->qos_other = DECODE_INT(qos_other); |
2623 | 0 | #undef DECODE_INT |
2624 | |
|
2625 | 0 | if (sshbuf_len(m)) |
2626 | 0 | return SSH_ERR_INVALID_FORMAT; |
2627 | 0 | debug3_f("done"); |
2628 | 0 | return 0; |
2629 | 0 | } |
2630 | | |
2631 | | /* NEW API */ |
2632 | | |
2633 | | /* put data to the outgoing packet */ |
2634 | | |
2635 | | int |
2636 | | sshpkt_put(struct ssh *ssh, const void *v, size_t len) |
2637 | 0 | { |
2638 | 0 | return sshbuf_put(ssh->state->outgoing_packet, v, len); |
2639 | 0 | } |
2640 | | |
2641 | | int |
2642 | | sshpkt_putb(struct ssh *ssh, const struct sshbuf *b) |
2643 | 87.4k | { |
2644 | 87.4k | return sshbuf_putb(ssh->state->outgoing_packet, b); |
2645 | 87.4k | } |
2646 | | |
2647 | | int |
2648 | | sshpkt_put_u8(struct ssh *ssh, u_char val) |
2649 | 0 | { |
2650 | 0 | return sshbuf_put_u8(ssh->state->outgoing_packet, val); |
2651 | 0 | } |
2652 | | |
2653 | | int |
2654 | | sshpkt_put_u32(struct ssh *ssh, u_int32_t val) |
2655 | 1.26M | { |
2656 | 1.26M | return sshbuf_put_u32(ssh->state->outgoing_packet, val); |
2657 | 1.26M | } |
2658 | | |
2659 | | int |
2660 | | sshpkt_put_u64(struct ssh *ssh, u_int64_t val) |
2661 | 0 | { |
2662 | 0 | return sshbuf_put_u64(ssh->state->outgoing_packet, val); |
2663 | 0 | } |
2664 | | |
2665 | | int |
2666 | | sshpkt_put_string(struct ssh *ssh, const void *v, size_t len) |
2667 | 801 | { |
2668 | 801 | return sshbuf_put_string(ssh->state->outgoing_packet, v, len); |
2669 | 801 | } |
2670 | | |
2671 | | int |
2672 | | sshpkt_put_cstring(struct ssh *ssh, const void *v) |
2673 | 18.3k | { |
2674 | 18.3k | return sshbuf_put_cstring(ssh->state->outgoing_packet, v); |
2675 | 18.3k | } |
2676 | | |
2677 | | int |
2678 | | sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v) |
2679 | 6.57k | { |
2680 | 6.57k | return sshbuf_put_stringb(ssh->state->outgoing_packet, v); |
2681 | 6.57k | } |
2682 | | |
2683 | | #ifdef WITH_OPENSSL |
2684 | | #ifdef OPENSSL_HAS_ECC |
2685 | | int |
2686 | | sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g) |
2687 | 0 | { |
2688 | 0 | return sshbuf_put_ec(ssh->state->outgoing_packet, v, g); |
2689 | 0 | } |
2690 | | |
2691 | | int |
2692 | | sshpkt_put_ec_pkey(struct ssh *ssh, EVP_PKEY *pkey) |
2693 | 0 | { |
2694 | 0 | return sshbuf_put_ec_pkey(ssh->state->outgoing_packet, pkey); |
2695 | 0 | } |
2696 | | #endif /* OPENSSL_HAS_ECC */ |
2697 | | |
2698 | | int |
2699 | | sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v) |
2700 | 462 | { |
2701 | 462 | return sshbuf_put_bignum2(ssh->state->outgoing_packet, v); |
2702 | 462 | } |
2703 | | #endif /* WITH_OPENSSL */ |
2704 | | |
2705 | | /* fetch data from the incoming packet */ |
2706 | | |
2707 | | int |
2708 | | sshpkt_get(struct ssh *ssh, void *valp, size_t len) |
2709 | 0 | { |
2710 | 0 | return sshbuf_get(ssh->state->incoming_packet, valp, len); |
2711 | 0 | } |
2712 | | |
2713 | | int |
2714 | | sshpkt_get_u8(struct ssh *ssh, u_char *valp) |
2715 | 1.31M | { |
2716 | 1.31M | return sshbuf_get_u8(ssh->state->incoming_packet, valp); |
2717 | 1.31M | } |
2718 | | |
2719 | | int |
2720 | | sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp) |
2721 | 76.3k | { |
2722 | 76.3k | return sshbuf_get_u32(ssh->state->incoming_packet, valp); |
2723 | 76.3k | } |
2724 | | |
2725 | | int |
2726 | | sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp) |
2727 | 0 | { |
2728 | 0 | return sshbuf_get_u64(ssh->state->incoming_packet, valp); |
2729 | 0 | } |
2730 | | |
2731 | | int |
2732 | | sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp) |
2733 | 759k | { |
2734 | 759k | return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp); |
2735 | 759k | } |
2736 | | |
2737 | | int |
2738 | | sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp) |
2739 | 0 | { |
2740 | 0 | return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp); |
2741 | 0 | } |
2742 | | |
2743 | | int |
2744 | | sshpkt_peek_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp) |
2745 | 0 | { |
2746 | 0 | return sshbuf_peek_string_direct(ssh->state->incoming_packet, valp, lenp); |
2747 | 0 | } |
2748 | | |
2749 | | int |
2750 | | sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp) |
2751 | 0 | { |
2752 | 0 | return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp); |
2753 | 0 | } |
2754 | | |
2755 | | int |
2756 | | sshpkt_getb_froms(struct ssh *ssh, struct sshbuf **valp) |
2757 | 2.28k | { |
2758 | 2.28k | return sshbuf_froms(ssh->state->incoming_packet, valp); |
2759 | 2.28k | } |
2760 | | |
2761 | | #ifdef WITH_OPENSSL |
2762 | | #ifdef OPENSSL_HAS_ECC |
2763 | | int |
2764 | | sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g) |
2765 | 0 | { |
2766 | 0 | return sshbuf_get_ec(ssh->state->incoming_packet, v, g); |
2767 | 0 | } |
2768 | | #endif /* OPENSSL_HAS_ECC */ |
2769 | | |
2770 | | int |
2771 | | sshpkt_get_bignum2(struct ssh *ssh, BIGNUM **valp) |
2772 | 238 | { |
2773 | 238 | return sshbuf_get_bignum2(ssh->state->incoming_packet, valp); |
2774 | 238 | } |
2775 | | #endif /* WITH_OPENSSL */ |
2776 | | |
2777 | | int |
2778 | | sshpkt_get_end(struct ssh *ssh) |
2779 | 77.4k | { |
2780 | 77.4k | if (sshbuf_len(ssh->state->incoming_packet) > 0) |
2781 | 569 | return SSH_ERR_UNEXPECTED_TRAILING_DATA; |
2782 | 76.8k | return 0; |
2783 | 77.4k | } |
2784 | | |
2785 | | const u_char * |
2786 | | sshpkt_ptr(struct ssh *ssh, size_t *lenp) |
2787 | 77.6k | { |
2788 | 77.6k | if (lenp != NULL) |
2789 | 77.6k | *lenp = sshbuf_len(ssh->state->incoming_packet); |
2790 | 77.6k | return sshbuf_ptr(ssh->state->incoming_packet); |
2791 | 77.6k | } |
2792 | | |
2793 | | /* start a new packet */ |
2794 | | |
2795 | | int |
2796 | | sshpkt_start(struct ssh *ssh, u_char type) |
2797 | 1.35M | { |
2798 | 1.35M | u_char buf[6]; /* u32 packet length, u8 pad len, u8 type */ |
2799 | | |
2800 | 1.35M | DBG(debug("packet_start[%d]", type)); |
2801 | 1.35M | memset(buf, 0, sizeof(buf)); |
2802 | 1.35M | buf[sizeof(buf) - 1] = type; |
2803 | 1.35M | sshbuf_reset(ssh->state->outgoing_packet); |
2804 | 1.35M | return sshbuf_put(ssh->state->outgoing_packet, buf, sizeof(buf)); |
2805 | 1.35M | } |
2806 | | |
2807 | | static int |
2808 | | ssh_packet_send_mux(struct ssh *ssh) |
2809 | 0 | { |
2810 | 0 | struct session_state *state = ssh->state; |
2811 | 0 | u_char type, *cp; |
2812 | 0 | size_t len; |
2813 | 0 | int r; |
2814 | |
|
2815 | 0 | if (ssh->kex) |
2816 | 0 | return SSH_ERR_INTERNAL_ERROR; |
2817 | 0 | len = sshbuf_len(state->outgoing_packet); |
2818 | 0 | if (len < 6) |
2819 | 0 | return SSH_ERR_INTERNAL_ERROR; |
2820 | 0 | cp = sshbuf_mutable_ptr(state->outgoing_packet); |
2821 | 0 | type = cp[5]; |
2822 | 0 | if (ssh_packet_log_type(type)) |
2823 | 0 | debug3_f("type %u", type); |
2824 | | /* drop everything, but the connection protocol */ |
2825 | 0 | if (type >= SSH2_MSG_CONNECTION_MIN && |
2826 | 0 | type <= SSH2_MSG_CONNECTION_MAX) { |
2827 | 0 | POKE_U32(cp, len - 4); |
2828 | 0 | if ((r = sshbuf_putb(state->output, |
2829 | 0 | state->outgoing_packet)) != 0) |
2830 | 0 | return r; |
2831 | | /* sshbuf_dump(state->output, stderr); */ |
2832 | 0 | } |
2833 | 0 | sshbuf_reset(state->outgoing_packet); |
2834 | 0 | return 0; |
2835 | 0 | } |
2836 | | |
2837 | | /* |
2838 | | * 9.2. Ignored Data Message |
2839 | | * |
2840 | | * byte SSH_MSG_IGNORE |
2841 | | * string data |
2842 | | * |
2843 | | * All implementations MUST understand (and ignore) this message at any |
2844 | | * time (after receiving the protocol version). No implementation is |
2845 | | * required to send them. This message can be used as an additional |
2846 | | * protection measure against advanced traffic analysis techniques. |
2847 | | */ |
2848 | | int |
2849 | | sshpkt_msg_ignore(struct ssh *ssh, u_int nbytes) |
2850 | 0 | { |
2851 | 0 | u_int32_t rnd = 0; |
2852 | 0 | int r; |
2853 | 0 | u_int i; |
2854 | |
|
2855 | 0 | if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 || |
2856 | 0 | (r = sshpkt_put_u32(ssh, nbytes)) != 0) |
2857 | 0 | return r; |
2858 | 0 | for (i = 0; i < nbytes; i++) { |
2859 | 0 | if (i % 4 == 0) |
2860 | 0 | rnd = arc4random(); |
2861 | 0 | if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0) |
2862 | 0 | return r; |
2863 | 0 | rnd >>= 8; |
2864 | 0 | } |
2865 | 0 | return 0; |
2866 | 0 | } |
2867 | | |
2868 | | /* send it */ |
2869 | | |
2870 | | int |
2871 | | sshpkt_send(struct ssh *ssh) |
2872 | 1.35M | { |
2873 | 1.35M | if (ssh->state && ssh->state->mux) |
2874 | 0 | return ssh_packet_send_mux(ssh); |
2875 | 1.35M | return ssh_packet_send2(ssh); |
2876 | 1.35M | } |
2877 | | |
2878 | | int |
2879 | | sshpkt_disconnect(struct ssh *ssh, const char *fmt,...) |
2880 | 9.15k | { |
2881 | 9.15k | char buf[1024]; |
2882 | 9.15k | va_list args; |
2883 | 9.15k | int r; |
2884 | | |
2885 | 9.15k | va_start(args, fmt); |
2886 | 9.15k | vsnprintf(buf, sizeof(buf), fmt, args); |
2887 | 9.15k | va_end(args); |
2888 | | |
2889 | 9.15k | debug2_f("sending SSH2_MSG_DISCONNECT: %s", buf); |
2890 | 9.15k | if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 || |
2891 | 9.15k | (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 || |
2892 | 9.15k | (r = sshpkt_put_cstring(ssh, buf)) != 0 || |
2893 | 9.15k | (r = sshpkt_put_cstring(ssh, "")) != 0 || |
2894 | 9.15k | (r = sshpkt_send(ssh)) != 0) |
2895 | 0 | return r; |
2896 | 9.15k | return 0; |
2897 | 9.15k | } |
2898 | | |
2899 | | /* roundup current message to pad bytes */ |
2900 | | int |
2901 | | sshpkt_add_padding(struct ssh *ssh, u_char pad) |
2902 | 0 | { |
2903 | 0 | ssh->state->extra_pad = pad; |
2904 | 0 | return 0; |
2905 | 0 | } |
2906 | | |
2907 | | static char * |
2908 | | format_traffic_stats(struct packet_state *ps) |
2909 | 0 | { |
2910 | 0 | char *stats = NULL, bytes[FMT_SCALED_STRSIZE]; |
2911 | |
|
2912 | 0 | if (ps->bytes > LLONG_MAX || fmt_scaled(ps->bytes, bytes) != 0) |
2913 | 0 | strlcpy(bytes, "OVERFLOW", sizeof(bytes)); |
2914 | |
|
2915 | 0 | xasprintf(&stats, "%lu pkts %llu blks %sB", |
2916 | 0 | (unsigned long)ps->packets, (unsigned long long)ps->blocks, bytes); |
2917 | 0 | return stats; |
2918 | 0 | } |
2919 | | |
2920 | | static char * |
2921 | | dedupe_alg_names(const char *in, const char *out) |
2922 | 0 | { |
2923 | 0 | char *names = NULL; |
2924 | |
|
2925 | 0 | if (in == NULL) |
2926 | 0 | in = "<implicit>"; |
2927 | 0 | if (out == NULL) |
2928 | 0 | out = "<implicit>"; |
2929 | |
|
2930 | 0 | if (strcmp(in, out) == 0) { |
2931 | 0 | names = xstrdup(in); |
2932 | 0 | } else { |
2933 | 0 | xasprintf(&names, "%s in, %s out", in, out); |
2934 | 0 | } |
2935 | 0 | return names; |
2936 | 0 | } |
2937 | | |
2938 | | static char * |
2939 | | comp_status_message(struct ssh *ssh) |
2940 | 0 | { |
2941 | 0 | #ifdef WITH_ZLIB |
2942 | 0 | char *ret = NULL; |
2943 | 0 | struct session_state *state = ssh->state; |
2944 | 0 | unsigned long long iraw = 0, icmp = 0, oraw = 0, ocmp = 0; |
2945 | 0 | char iraw_f[FMT_SCALED_STRSIZE] = "", oraw_f[FMT_SCALED_STRSIZE] = ""; |
2946 | 0 | char icmp_f[FMT_SCALED_STRSIZE] = "", ocmp_f[FMT_SCALED_STRSIZE] = ""; |
2947 | |
|
2948 | 0 | if (state->compression_buffer) { |
2949 | 0 | if (state->compression_in_started) { |
2950 | 0 | iraw = state->compression_in_stream.total_out; |
2951 | 0 | icmp = state->compression_in_stream.total_in; |
2952 | 0 | if (fmt_scaled(iraw, iraw_f) != 0) |
2953 | 0 | strlcpy(iraw_f, "OVERFLOW", sizeof(iraw_f)); |
2954 | 0 | if (fmt_scaled(icmp, icmp_f) != 0) |
2955 | 0 | strlcpy(icmp_f, "OVERFLOW", sizeof(icmp_f)); |
2956 | 0 | } |
2957 | 0 | if (state->compression_out_started) { |
2958 | 0 | oraw = state->compression_out_stream.total_in; |
2959 | 0 | ocmp = state->compression_out_stream.total_out; |
2960 | 0 | if (fmt_scaled(oraw, oraw_f) != 0) |
2961 | 0 | strlcpy(oraw_f, "OVERFLOW", sizeof(oraw_f)); |
2962 | 0 | if (fmt_scaled(ocmp, ocmp_f) != 0) |
2963 | 0 | strlcpy(ocmp_f, "OVERFLOW", sizeof(ocmp_f)); |
2964 | 0 | } |
2965 | 0 | xasprintf(&ret, |
2966 | 0 | " compressed %s/%s (*%.3f) in," |
2967 | 0 | " %s/%s (*%.3f) out\r\n", |
2968 | 0 | icmp_f, iraw_f, iraw == 0 ? 0.0 : (double)icmp / iraw, |
2969 | 0 | ocmp_f, oraw_f, oraw == 0 ? 0.0 : (double)ocmp / oraw); |
2970 | 0 | return ret; |
2971 | 0 | } |
2972 | 0 | #endif /* WITH_ZLIB */ |
2973 | 0 | return xstrdup(""); |
2974 | 0 | } |
2975 | | |
2976 | | char * |
2977 | | connection_info_message(struct ssh *ssh) |
2978 | 0 | { |
2979 | 0 | char *ret = NULL, *cipher = NULL, *mac = NULL, *comp = NULL; |
2980 | 0 | char *rekey_volume = NULL, *rekey_time = NULL, *comp_info = NULL; |
2981 | 0 | char thishost[NI_MAXHOST] = "unknown", *tcp_info = NULL; |
2982 | 0 | struct kex *kex; |
2983 | 0 | struct session_state *state; |
2984 | 0 | struct newkeys *nk_in, *nk_out; |
2985 | 0 | char *stats_in = NULL, *stats_out = NULL; |
2986 | 0 | u_int64_t epoch = (u_int64_t)time(NULL) - monotime(); |
2987 | |
|
2988 | 0 | if (ssh == NULL) |
2989 | 0 | return NULL; |
2990 | 0 | state = ssh->state; |
2991 | 0 | kex = ssh->kex; |
2992 | |
|
2993 | 0 | (void)gethostname(thishost, sizeof(thishost)); |
2994 | |
|
2995 | 0 | if (ssh_local_port(ssh) != 65535 || |
2996 | 0 | strcmp(ssh_local_ipaddr(ssh), "UNKNOWN") != 0) { |
2997 | 0 | xasprintf(&tcp_info, " tcp %s:%d -> %s:%d\r\n", |
2998 | 0 | ssh_local_ipaddr(ssh), ssh_local_port(ssh), |
2999 | 0 | ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); |
3000 | 0 | } else { |
3001 | 0 | tcp_info = xstrdup(""); |
3002 | 0 | } |
3003 | |
|
3004 | 0 | nk_in = ssh->state->newkeys[MODE_IN]; |
3005 | 0 | nk_out = ssh->state->newkeys[MODE_OUT]; |
3006 | 0 | stats_in = format_traffic_stats(&ssh->state->p_read); |
3007 | 0 | stats_out = format_traffic_stats(&ssh->state->p_send); |
3008 | |
|
3009 | 0 | cipher = dedupe_alg_names(nk_in->enc.name, nk_out->enc.name); |
3010 | 0 | mac = dedupe_alg_names(nk_in->mac.name, nk_out->mac.name); |
3011 | 0 | comp = dedupe_alg_names(nk_in->comp.name, nk_out->comp.name); |
3012 | | |
3013 | | /* Volume based rekeying. */ |
3014 | 0 | if (state->rekey_limit == 0) { |
3015 | 0 | xasprintf(&rekey_volume, "limit none"); |
3016 | 0 | } else { |
3017 | 0 | char *volumes = NULL, in[32], out[32]; |
3018 | |
|
3019 | 0 | snprintf(in, sizeof(in), "%llu", |
3020 | 0 | (unsigned long long)state->max_blocks_in); |
3021 | 0 | snprintf(out, sizeof(out), "%llu", |
3022 | 0 | (unsigned long long)state->max_blocks_out); |
3023 | 0 | volumes = dedupe_alg_names(in, out); |
3024 | 0 | xasprintf(&rekey_volume, "limit blocks %s", volumes); |
3025 | 0 | free(volumes); |
3026 | 0 | } |
3027 | | |
3028 | | /* Time based rekeying. */ |
3029 | 0 | if (state->rekey_interval == 0) { |
3030 | 0 | rekey_time = xstrdup("interval none"); |
3031 | 0 | } else { |
3032 | 0 | char rekey_next[64]; |
3033 | |
|
3034 | 0 | format_absolute_time(epoch + state->rekey_time + |
3035 | 0 | state->rekey_interval, rekey_next, sizeof(rekey_next)); |
3036 | 0 | xasprintf(&rekey_time, "interval %s, next %s", |
3037 | 0 | fmt_timeframe(state->rekey_interval), rekey_next); |
3038 | 0 | } |
3039 | 0 | comp_info = comp_status_message(ssh); |
3040 | |
|
3041 | 0 | xasprintf(&ret, "Connection information for %s pid %lld\r\n" |
3042 | 0 | "%s" |
3043 | 0 | " kexalgorithm %s\r\n hostkeyalgorithm %s\r\n" |
3044 | 0 | " cipher %s\r\n mac %s\r\n compression %s\r\n" |
3045 | 0 | " rekey %s %s\r\n" |
3046 | 0 | " traffic %s in, %s out\r\n" |
3047 | 0 | "%s", |
3048 | 0 | thishost, (long long)getpid(), |
3049 | 0 | tcp_info, |
3050 | 0 | kex->name, kex->hostkey_alg, |
3051 | 0 | cipher, mac, comp, |
3052 | 0 | rekey_volume, rekey_time, |
3053 | 0 | stats_in, stats_out, |
3054 | 0 | comp_info |
3055 | 0 | ); |
3056 | 0 | free(tcp_info); |
3057 | 0 | free(cipher); |
3058 | 0 | free(mac); |
3059 | 0 | free(comp); |
3060 | 0 | free(stats_in); |
3061 | 0 | free(stats_out); |
3062 | 0 | free(rekey_volume); |
3063 | 0 | free(rekey_time); |
3064 | 0 | free(comp_info); |
3065 | 0 | return ret; |
3066 | 0 | } |
3067 | | |