Branch data Line data Source code
1 : : /* $OpenBSD: packet.c,v 1.192 2014/02/02 03:44:31 djm 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/param.h>
45 : : #include <sys/socket.h>
46 : : #ifdef HAVE_SYS_TIME_H
47 : : # include <sys/time.h>
48 : : #endif
49 : :
50 : : #include <netinet/in.h>
51 : : #include <netinet/ip.h>
52 : : #include <arpa/inet.h>
53 : :
54 : : #include <errno.h>
55 : : #include <stdarg.h>
56 : : #include <stdio.h>
57 : : #include <stdlib.h>
58 : : #include <string.h>
59 : : #include <unistd.h>
60 : : #include <signal.h>
61 : : #include <time.h>
62 : :
63 : : #include "xmalloc.h"
64 : : #include "buffer.h"
65 : : #include "packet.h"
66 : : #include "crc32.h"
67 : : #include "compress.h"
68 : : #include "deattack.h"
69 : : #include "channels.h"
70 : : #include "compat.h"
71 : : #include "ssh1.h"
72 : : #include "ssh2.h"
73 : : #include "cipher.h"
74 : : #include "key.h"
75 : : #include "kex.h"
76 : : #include "mac.h"
77 : : #include "log.h"
78 : : #include "canohost.h"
79 : : #include "misc.h"
80 : : #include "ssh.h"
81 : : #include "roaming.h"
82 : :
83 : : #ifdef PACKET_DEBUG
84 : : #define DBG(x) x
85 : : #else
86 : : #define DBG(x)
87 : : #endif
88 : :
89 : : #define PACKET_MAX_SIZE (256 * 1024)
90 : :
91 : : struct packet_state {
92 : : u_int32_t seqnr;
93 : : u_int32_t packets;
94 : : u_int64_t blocks;
95 : : u_int64_t bytes;
96 : : };
97 : :
98 : : struct packet {
99 : : TAILQ_ENTRY(packet) next;
100 : : u_char type;
101 : : Buffer payload;
102 : : };
103 : :
104 : : struct session_state {
105 : : /*
106 : : * This variable contains the file descriptors used for
107 : : * communicating with the other side. connection_in is used for
108 : : * reading; connection_out for writing. These can be the same
109 : : * descriptor, in which case it is assumed to be a socket.
110 : : */
111 : : int connection_in;
112 : : int connection_out;
113 : :
114 : : /* Protocol flags for the remote side. */
115 : : u_int remote_protocol_flags;
116 : :
117 : : /* Encryption context for receiving data. Only used for decryption. */
118 : : CipherContext receive_context;
119 : :
120 : : /* Encryption context for sending data. Only used for encryption. */
121 : : CipherContext send_context;
122 : :
123 : : /* Buffer for raw input data from the socket. */
124 : : Buffer input;
125 : :
126 : : /* Buffer for raw output data going to the socket. */
127 : : Buffer output;
128 : :
129 : : /* Buffer for the partial outgoing packet being constructed. */
130 : : Buffer outgoing_packet;
131 : :
132 : : /* Buffer for the incoming packet currently being processed. */
133 : : Buffer incoming_packet;
134 : :
135 : : /* Scratch buffer for packet compression/decompression. */
136 : : Buffer compression_buffer;
137 : : int compression_buffer_ready;
138 : :
139 : : /*
140 : : * Flag indicating whether packet compression/decompression is
141 : : * enabled.
142 : : */
143 : : int packet_compression;
144 : :
145 : : /* default maximum packet size */
146 : : u_int max_packet_size;
147 : :
148 : : /* Flag indicating whether this module has been initialized. */
149 : : int initialized;
150 : :
151 : : /* Set to true if the connection is interactive. */
152 : : int interactive_mode;
153 : :
154 : : /* Set to true if we are the server side. */
155 : : int server_side;
156 : :
157 : : /* Set to true if we are authenticated. */
158 : : int after_authentication;
159 : :
160 : : int keep_alive_timeouts;
161 : :
162 : : /* The maximum time that we will wait to send or receive a packet */
163 : : int packet_timeout_ms;
164 : :
165 : : /* Session key information for Encryption and MAC */
166 : : Newkeys *newkeys[MODE_MAX];
167 : : struct packet_state p_read, p_send;
168 : :
169 : : /* Volume-based rekeying */
170 : : u_int64_t max_blocks_in, max_blocks_out;
171 : : u_int32_t rekey_limit;
172 : :
173 : : /* Time-based rekeying */
174 : : time_t rekey_interval; /* how often in seconds */
175 : : time_t rekey_time; /* time of last rekeying */
176 : :
177 : : /* Session key for protocol v1 */
178 : : u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
179 : : u_int ssh1_keylen;
180 : :
181 : : /* roundup current message to extra_pad bytes */
182 : : u_char extra_pad;
183 : :
184 : : /* XXX discard incoming data after MAC error */
185 : : u_int packet_discard;
186 : : Mac *packet_discard_mac;
187 : :
188 : : /* Used in packet_read_poll2() */
189 : : u_int packlen;
190 : :
191 : : /* Used in packet_send2 */
192 : : int rekeying;
193 : :
194 : : /* Used in packet_set_interactive */
195 : : int set_interactive_called;
196 : :
197 : : /* Used in packet_set_maxsize */
198 : : int set_maxsize_called;
199 : :
200 : : TAILQ_HEAD(, packet) outgoing;
201 : : };
202 : :
203 : : static struct session_state *active_state, *backup_state;
204 : :
205 : : static struct session_state *
206 : 1966 : alloc_session_state(void)
207 : : {
208 : 1966 : struct session_state *s = xcalloc(1, sizeof(*s));
209 : :
210 : 1966 : s->connection_in = -1;
211 : 1966 : s->connection_out = -1;
212 : 1966 : s->max_packet_size = 32768;
213 : 1966 : s->packet_timeout_ms = -1;
214 : 1966 : return s;
215 : : }
216 : :
217 : : /*
218 : : * Sets the descriptors used for communication. Disables encryption until
219 : : * packet_set_encryption_key is called.
220 : : */
221 : : void
222 : 1970 : packet_set_connection(int fd_in, int fd_out)
223 : : {
224 : 1970 : const Cipher *none = cipher_by_name("none");
225 : :
226 [ - + ]: 1970 : if (none == NULL)
227 : 0 : fatal("packet_set_connection: cannot load cipher 'none'");
228 [ + + ]: 1970 : if (active_state == NULL)
229 : 1966 : active_state = alloc_session_state();
230 : 1970 : active_state->connection_in = fd_in;
231 : 1970 : active_state->connection_out = fd_out;
232 : 1970 : cipher_init(&active_state->send_context, none, (const u_char *)"",
233 : : 0, NULL, 0, CIPHER_ENCRYPT);
234 : 1970 : cipher_init(&active_state->receive_context, none, (const u_char *)"",
235 : : 0, NULL, 0, CIPHER_DECRYPT);
236 : 1970 : active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
237 [ + - ]: 1970 : if (!active_state->initialized) {
238 : 1970 : active_state->initialized = 1;
239 : 1970 : buffer_init(&active_state->input);
240 : 1970 : buffer_init(&active_state->output);
241 : 1970 : buffer_init(&active_state->outgoing_packet);
242 : 1970 : buffer_init(&active_state->incoming_packet);
243 : 1970 : TAILQ_INIT(&active_state->outgoing);
244 : 1970 : active_state->p_send.packets = active_state->p_read.packets = 0;
245 : : }
246 : 1970 : }
247 : :
248 : : void
249 : 1559 : packet_set_timeout(int timeout, int count)
250 : : {
251 [ + - ]: 1559 : if (timeout <= 0 || count <= 0) {
252 : 1559 : active_state->packet_timeout_ms = -1;
253 : 1559 : return;
254 : : }
255 [ # # ]: 0 : if ((INT_MAX / 1000) / count < timeout)
256 : 0 : active_state->packet_timeout_ms = INT_MAX;
257 : : else
258 : 0 : active_state->packet_timeout_ms = timeout * count * 1000;
259 : : }
260 : :
261 : : static void
262 : 0 : packet_stop_discard(void)
263 : : {
264 [ # # ]: 0 : if (active_state->packet_discard_mac) {
265 : : char buf[1024];
266 : :
267 : : memset(buf, 'a', sizeof(buf));
268 [ # # ]: 0 : while (buffer_len(&active_state->incoming_packet) <
269 : : PACKET_MAX_SIZE)
270 : 0 : buffer_append(&active_state->incoming_packet, buf,
271 : : sizeof(buf));
272 : 0 : (void) mac_compute(active_state->packet_discard_mac,
273 : 0 : active_state->p_read.seqnr,
274 : 0 : buffer_ptr(&active_state->incoming_packet),
275 : : PACKET_MAX_SIZE);
276 : : }
277 : 0 : logit("Finished discarding for %.200s", get_remote_ipaddr());
278 : 0 : cleanup_exit(255);
279 : : }
280 : :
281 : : static void
282 : 0 : packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
283 : : {
284 [ # # ][ # # ]: 0 : if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm))
[ # # ][ # # ]
285 : 0 : packet_disconnect("Packet corrupt");
286 [ # # ][ # # ]: 0 : if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
287 : 0 : active_state->packet_discard_mac = mac;
288 [ # # ]: 0 : if (buffer_len(&active_state->input) >= discard)
289 : 0 : packet_stop_discard();
290 : 0 : active_state->packet_discard = discard -
291 : 0 : buffer_len(&active_state->input);
292 : 0 : }
293 : :
294 : : /* Returns 1 if remote host is connected via socket, 0 if not. */
295 : :
296 : : int
297 : 11162 : packet_connection_is_on_socket(void)
298 : : {
299 : : struct sockaddr_storage from, to;
300 : : socklen_t fromlen, tolen;
301 : :
302 : : /* filedescriptors in and out are the same, so it's a socket */
303 [ + + ]: 11162 : if (active_state->connection_in == active_state->connection_out)
304 : : return 1;
305 : 9837 : fromlen = sizeof(from);
306 : : memset(&from, 0, sizeof(from));
307 [ - + ]: 9837 : if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
308 : : &fromlen) < 0)
309 : : return 0;
310 : 0 : tolen = sizeof(to);
311 : : memset(&to, 0, sizeof(to));
312 [ # # ]: 0 : if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
313 : : &tolen) < 0)
314 : : return 0;
315 [ # # ][ # # ]: 0 : if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
316 : : return 0;
317 [ # # ]: 0 : if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
318 : : return 0;
319 : 0 : return 1;
320 : : }
321 : :
322 : : /*
323 : : * Exports an IV from the CipherContext required to export the key
324 : : * state back from the unprivileged child to the privileged parent
325 : : * process.
326 : : */
327 : :
328 : : void
329 : 1522 : packet_get_keyiv(int mode, u_char *iv, u_int len)
330 : : {
331 : : CipherContext *cc;
332 : :
333 [ + + ]: 1522 : if (mode == MODE_OUT)
334 : 761 : cc = &active_state->send_context;
335 : : else
336 : 761 : cc = &active_state->receive_context;
337 : :
338 : 1522 : cipher_get_keyiv(cc, iv, len);
339 : 1522 : }
340 : :
341 : : int
342 : 3044 : packet_get_keycontext(int mode, u_char *dat)
343 : : {
344 : : CipherContext *cc;
345 : :
346 [ + + ]: 3044 : if (mode == MODE_OUT)
347 : 1522 : cc = &active_state->send_context;
348 : : else
349 : 1522 : cc = &active_state->receive_context;
350 : :
351 : 3044 : return (cipher_get_keycontext(cc, dat));
352 : : }
353 : :
354 : : void
355 : 1470 : packet_set_keycontext(int mode, u_char *dat)
356 : : {
357 : : CipherContext *cc;
358 : :
359 [ + + ]: 1470 : if (mode == MODE_OUT)
360 : 735 : cc = &active_state->send_context;
361 : : else
362 : 735 : cc = &active_state->receive_context;
363 : :
364 : 1470 : cipher_set_keycontext(cc, dat);
365 : 1470 : }
366 : :
367 : : int
368 : 206 : packet_get_keyiv_len(int mode)
369 : : {
370 : : CipherContext *cc;
371 : :
372 [ + + ]: 206 : if (mode == MODE_OUT)
373 : 103 : cc = &active_state->send_context;
374 : : else
375 : 103 : cc = &active_state->receive_context;
376 : :
377 : 206 : return (cipher_get_keyiv_len(cc));
378 : : }
379 : :
380 : : void
381 : 186 : packet_set_iv(int mode, u_char *dat)
382 : : {
383 : : CipherContext *cc;
384 : :
385 [ + + ]: 186 : if (mode == MODE_OUT)
386 : 93 : cc = &active_state->send_context;
387 : : else
388 : 93 : cc = &active_state->receive_context;
389 : :
390 : 186 : cipher_set_keyiv(cc, dat);
391 : 186 : }
392 : :
393 : : int
394 : 103 : packet_get_ssh1_cipher(void)
395 : : {
396 : 103 : return (cipher_get_number(active_state->receive_context.cipher));
397 : : }
398 : :
399 : : void
400 : 2866 : packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks,
401 : : u_int32_t *packets, u_int64_t *bytes)
402 : : {
403 : : struct packet_state *state;
404 : :
405 : 2866 : state = (mode == MODE_IN) ?
406 [ + + ]: 2866 : &active_state->p_read : &active_state->p_send;
407 [ + + ]: 2866 : if (seqnr)
408 : 1316 : *seqnr = state->seqnr;
409 [ + + ]: 2866 : if (blocks)
410 : 1316 : *blocks = state->blocks;
411 [ + + ]: 2866 : if (packets)
412 : 1316 : *packets = state->packets;
413 [ + - ]: 2866 : if (bytes)
414 : 2866 : *bytes = state->bytes;
415 : 2866 : }
416 : :
417 : : void
418 : 1316 : packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
419 : : u_int64_t bytes)
420 : : {
421 : : struct packet_state *state;
422 : :
423 : 1316 : state = (mode == MODE_IN) ?
424 [ + + ]: 1316 : &active_state->p_read : &active_state->p_send;
425 : 1316 : state->seqnr = seqnr;
426 : 1316 : state->blocks = blocks;
427 : 1316 : state->packets = packets;
428 : 1316 : state->bytes = bytes;
429 : 1316 : }
430 : :
431 : : static int
432 : 96 : packet_connection_af(void)
433 : : {
434 : : struct sockaddr_storage to;
435 : 96 : socklen_t tolen = sizeof(to);
436 : :
437 : : memset(&to, 0, sizeof(to));
438 [ + - ]: 96 : if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
439 : : &tolen) < 0)
440 : : return 0;
441 : : #ifdef IPV4_IN_IPV6
442 [ - + ][ # # ]: 96 : if (to.ss_family == AF_INET6 &&
443 [ # # ][ # # ]: 0 : IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
[ # # ]
444 : : return AF_INET;
445 : : #endif
446 : 96 : return to.ss_family;
447 : : }
448 : :
449 : : /* Sets the connection into non-blocking mode. */
450 : :
451 : : void
452 : 1964 : packet_set_nonblocking(void)
453 : : {
454 : : /* Set the socket into non-blocking mode. */
455 : 1964 : set_nonblock(active_state->connection_in);
456 : :
457 [ + + ]: 1964 : if (active_state->connection_out != active_state->connection_in)
458 : 1785 : set_nonblock(active_state->connection_out);
459 : 1964 : }
460 : :
461 : : /* Returns the socket used for reading. */
462 : :
463 : : int
464 : 6493 : packet_get_connection_in(void)
465 : : {
466 : 6493 : return active_state->connection_in;
467 : : }
468 : :
469 : : /* Returns the descriptor used for writing. */
470 : :
471 : : int
472 : 3779 : packet_get_connection_out(void)
473 : : {
474 : 3779 : return active_state->connection_out;
475 : : }
476 : :
477 : : /* Closes the connection and clears and frees internal data structures. */
478 : :
479 : : void
480 : 781 : packet_close(void)
481 : : {
482 [ + - ]: 781 : if (!active_state->initialized)
483 : 781 : return;
484 : 781 : active_state->initialized = 0;
485 [ + + ]: 781 : if (active_state->connection_in == active_state->connection_out) {
486 : 103 : shutdown(active_state->connection_out, SHUT_RDWR);
487 : 103 : close(active_state->connection_out);
488 : : } else {
489 : 678 : close(active_state->connection_in);
490 : 678 : close(active_state->connection_out);
491 : : }
492 : 781 : buffer_free(&active_state->input);
493 : 781 : buffer_free(&active_state->output);
494 : 781 : buffer_free(&active_state->outgoing_packet);
495 : 781 : buffer_free(&active_state->incoming_packet);
496 [ - + ]: 781 : if (active_state->compression_buffer_ready) {
497 : 0 : buffer_free(&active_state->compression_buffer);
498 : 0 : buffer_compress_uninit();
499 : : }
500 : 781 : cipher_cleanup(&active_state->send_context);
501 : 781 : cipher_cleanup(&active_state->receive_context);
502 : : }
503 : :
504 : : /* Sets remote side protocol flags. */
505 : :
506 : : void
507 : 258 : packet_set_protocol_flags(u_int protocol_flags)
508 : : {
509 : 258 : active_state->remote_protocol_flags = protocol_flags;
510 : 258 : }
511 : :
512 : : /* Returns the remote protocol flags set earlier by the above function. */
513 : :
514 : : u_int
515 : 147 : packet_get_protocol_flags(void)
516 : : {
517 : 147 : return active_state->remote_protocol_flags;
518 : : }
519 : :
520 : : /*
521 : : * Starts packet compression from the next packet on in both directions.
522 : : * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
523 : : */
524 : :
525 : : static void
526 : 0 : packet_init_compression(void)
527 : : {
528 [ # # ]: 0 : if (active_state->compression_buffer_ready == 1)
529 : 0 : return;
530 : 0 : active_state->compression_buffer_ready = 1;
531 : 0 : buffer_init(&active_state->compression_buffer);
532 : : }
533 : :
534 : : void
535 : 0 : packet_start_compression(int level)
536 : : {
537 [ # # ][ # # ]: 0 : if (active_state->packet_compression && !compat20)
538 : 0 : fatal("Compression already enabled.");
539 : 0 : active_state->packet_compression = 1;
540 : 0 : packet_init_compression();
541 : 0 : buffer_compress_init_send(level);
542 : 0 : buffer_compress_init_recv();
543 : 0 : }
544 : :
545 : : /*
546 : : * Causes any further packets to be encrypted using the given key. The same
547 : : * key is used for both sending and reception. However, both directions are
548 : : * encrypted independently of each other.
549 : : */
550 : :
551 : : void
552 : 258 : packet_set_encryption_key(const u_char *key, u_int keylen, int number)
553 : : {
554 : 258 : const Cipher *cipher = cipher_by_number(number);
555 : :
556 [ - + ]: 258 : if (cipher == NULL)
557 : 0 : fatal("packet_set_encryption_key: unknown cipher number %d", number);
558 [ - + ]: 258 : if (keylen < 20)
559 : 0 : fatal("packet_set_encryption_key: keylen too small: %d", keylen);
560 [ - + ]: 258 : if (keylen > SSH_SESSION_KEY_LENGTH)
561 : 0 : fatal("packet_set_encryption_key: keylen too big: %d", keylen);
562 : 258 : memcpy(active_state->ssh1_key, key, keylen);
563 : 258 : active_state->ssh1_keylen = keylen;
564 : 258 : cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
565 : : 0, CIPHER_ENCRYPT);
566 : 258 : cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
567 : : 0, CIPHER_DECRYPT);
568 : 258 : }
569 : :
570 : : u_int
571 : 206 : packet_get_encryption_key(u_char *key)
572 : : {
573 [ + + ]: 206 : if (key == NULL)
574 : 103 : return (active_state->ssh1_keylen);
575 : 103 : memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
576 : 103 : return (active_state->ssh1_keylen);
577 : : }
578 : :
579 : : /* Start constructing a packet to send. */
580 : : void
581 : 28079 : packet_start(u_char type)
582 : : {
583 : : u_char buf[9];
584 : : int len;
585 : :
586 : : DBG(debug("packet_start[%d]", type));
587 [ + + ]: 28079 : len = compat20 ? 6 : 9;
588 : 28079 : memset(buf, 0, len - 1);
589 : 28079 : buf[len - 1] = type;
590 : 28079 : buffer_clear(&active_state->outgoing_packet);
591 : 28079 : buffer_append(&active_state->outgoing_packet, buf, len);
592 : 28079 : }
593 : :
594 : : /* Append payload. */
595 : : void
596 : 5564 : packet_put_char(int value)
597 : : {
598 : 5680 : char ch = value;
599 : :
600 : 5680 : buffer_append(&active_state->outgoing_packet, &ch, 1);
601 : 5564 : }
602 : :
603 : : void
604 : 17711 : packet_put_int(u_int value)
605 : : {
606 : 17711 : buffer_put_int(&active_state->outgoing_packet, value);
607 : 17711 : }
608 : :
609 : : void
610 : 0 : packet_put_int64(u_int64_t value)
611 : : {
612 : 0 : buffer_put_int64(&active_state->outgoing_packet, value);
613 : 0 : }
614 : :
615 : : void
616 : 16338 : packet_put_string(const void *buf, u_int len)
617 : : {
618 : 16338 : buffer_put_string(&active_state->outgoing_packet, buf, len);
619 : 16338 : }
620 : :
621 : : void
622 : 11607 : packet_put_cstring(const char *str)
623 : : {
624 : 11835 : buffer_put_cstring(&active_state->outgoing_packet, str);
625 : 11607 : }
626 : :
627 : : void
628 : 2567 : packet_put_raw(const void *buf, u_int len)
629 : : {
630 : 2567 : buffer_append(&active_state->outgoing_packet, buf, len);
631 : 2567 : }
632 : :
633 : : void
634 : 642 : packet_put_bignum(BIGNUM * value)
635 : : {
636 : 642 : buffer_put_bignum(&active_state->outgoing_packet, value);
637 : 642 : }
638 : :
639 : : void
640 : 199 : packet_put_bignum2(BIGNUM * value)
641 : : {
642 : 199 : buffer_put_bignum2(&active_state->outgoing_packet, value);
643 : 199 : }
644 : :
645 : : #ifdef OPENSSL_HAS_ECC
646 : : void
647 : 96 : packet_put_ecpoint(const EC_GROUP *curve, const EC_POINT *point)
648 : : {
649 : 96 : buffer_put_ecpoint(&active_state->outgoing_packet, curve, point);
650 : 96 : }
651 : : #endif
652 : :
653 : : /*
654 : : * Finalizes and sends the packet. If the encryption key has been set,
655 : : * encrypts the packet before sending.
656 : : */
657 : :
658 : : static void
659 : 3460 : packet_send1(void)
660 : : {
661 : : u_char buf[8], *cp;
662 : : int i, padding, len;
663 : : u_int checksum;
664 : 3460 : u_int32_t rnd = 0;
665 : :
666 : : /*
667 : : * If using packet compression, compress the payload of the outgoing
668 : : * packet.
669 : : */
670 [ - + ]: 3460 : if (active_state->packet_compression) {
671 : 0 : buffer_clear(&active_state->compression_buffer);
672 : : /* Skip padding. */
673 : 0 : buffer_consume(&active_state->outgoing_packet, 8);
674 : : /* padding */
675 : 0 : buffer_append(&active_state->compression_buffer,
676 : : "\0\0\0\0\0\0\0\0", 8);
677 : 0 : buffer_compress(&active_state->outgoing_packet,
678 : 0 : &active_state->compression_buffer);
679 : 0 : buffer_clear(&active_state->outgoing_packet);
680 : 0 : buffer_append(&active_state->outgoing_packet,
681 : 0 : buffer_ptr(&active_state->compression_buffer),
682 : 0 : buffer_len(&active_state->compression_buffer));
683 : : }
684 : : /* Compute packet length without padding (add checksum, remove padding). */
685 : 3460 : len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
686 : :
687 : : /* Insert padding. Initialized to zero in packet_start1() */
688 : 3460 : padding = 8 - len % 8;
689 [ + + ]: 3460 : if (!active_state->send_context.plaintext) {
690 : 3295 : cp = buffer_ptr(&active_state->outgoing_packet);
691 [ + + ]: 21140 : for (i = 0; i < padding; i++) {
692 [ + + ]: 17845 : if (i % 4 == 0)
693 : 5379 : rnd = arc4random();
694 : 17845 : cp[7 - i] = rnd & 0xff;
695 : 17845 : rnd >>= 8;
696 : : }
697 : : }
698 : 3460 : buffer_consume(&active_state->outgoing_packet, 8 - padding);
699 : :
700 : : /* Add check bytes. */
701 : 3460 : checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
702 : 3460 : buffer_len(&active_state->outgoing_packet));
703 : 3460 : put_u32(buf, checksum);
704 : 3460 : buffer_append(&active_state->outgoing_packet, buf, 4);
705 : :
706 : : #ifdef PACKET_DEBUG
707 : : fprintf(stderr, "packet_send plain: ");
708 : : buffer_dump(&active_state->outgoing_packet);
709 : : #endif
710 : :
711 : : /* Append to output. */
712 : 3460 : put_u32(buf, len);
713 : 3460 : buffer_append(&active_state->output, buf, 4);
714 : 3460 : cp = buffer_append_space(&active_state->output,
715 : 3460 : buffer_len(&active_state->outgoing_packet));
716 [ - + ]: 6920 : if (cipher_crypt(&active_state->send_context, 0, cp,
717 : 3460 : buffer_ptr(&active_state->outgoing_packet),
718 : 3460 : buffer_len(&active_state->outgoing_packet), 0, 0) != 0)
719 : 0 : fatal("%s: cipher_crypt failed", __func__);
720 : :
721 : : #ifdef PACKET_DEBUG
722 : : fprintf(stderr, "encrypted: ");
723 : : buffer_dump(&active_state->output);
724 : : #endif
725 : 3460 : active_state->p_send.packets++;
726 : 6920 : active_state->p_send.bytes += len +
727 : 3460 : buffer_len(&active_state->outgoing_packet);
728 : 3460 : buffer_clear(&active_state->outgoing_packet);
729 : :
730 : : /*
731 : : * Note that the packet is now only buffered in output. It won't be
732 : : * actually sent until packet_write_wait or packet_write_poll is
733 : : * called.
734 : : */
735 : 3460 : }
736 : :
737 : : void
738 : 5028 : set_newkeys(int mode)
739 : : {
740 : : Enc *enc;
741 : : Mac *mac;
742 : : Comp *comp;
743 : : CipherContext *cc;
744 : : u_int64_t *max_blocks;
745 : : int crypt_type;
746 : :
747 : 5028 : debug2("set_newkeys: mode %d", mode);
748 : :
749 [ + + ]: 5028 : if (mode == MODE_OUT) {
750 : 2514 : cc = &active_state->send_context;
751 : 2514 : crypt_type = CIPHER_ENCRYPT;
752 : 2514 : active_state->p_send.packets = active_state->p_send.blocks = 0;
753 : 2514 : max_blocks = &active_state->max_blocks_out;
754 : : } else {
755 : 2514 : cc = &active_state->receive_context;
756 : 2514 : crypt_type = CIPHER_DECRYPT;
757 : 2514 : active_state->p_read.packets = active_state->p_read.blocks = 0;
758 : 2514 : max_blocks = &active_state->max_blocks_in;
759 : : }
760 [ + + ]: 5028 : if (active_state->newkeys[mode] != NULL) {
761 : 904 : debug("set_newkeys: rekeying");
762 : 904 : cipher_cleanup(cc);
763 : 904 : enc = &active_state->newkeys[mode]->enc;
764 : 904 : mac = &active_state->newkeys[mode]->mac;
765 : 904 : comp = &active_state->newkeys[mode]->comp;
766 : 904 : mac_clear(mac);
767 : 904 : explicit_bzero(enc->iv, enc->iv_len);
768 : 904 : explicit_bzero(enc->key, enc->key_len);
769 : 904 : explicit_bzero(mac->key, mac->key_len);
770 : 904 : free(enc->name);
771 : 904 : free(enc->iv);
772 : 904 : free(enc->key);
773 : 904 : free(mac->name);
774 : 904 : free(mac->key);
775 : 904 : free(comp->name);
776 : 904 : free(active_state->newkeys[mode]);
777 : : }
778 : 5028 : active_state->newkeys[mode] = kex_get_newkeys(mode);
779 [ - + ]: 5028 : if (active_state->newkeys[mode] == NULL)
780 : 0 : fatal("newkeys: no keys for mode %d", mode);
781 : 5028 : enc = &active_state->newkeys[mode]->enc;
782 : 5028 : mac = &active_state->newkeys[mode]->mac;
783 : 5028 : comp = &active_state->newkeys[mode]->comp;
784 [ + + ][ + - ]: 5028 : if (cipher_authlen(enc->cipher) == 0 && mac_init(mac) == 0)
785 : 4590 : mac->enabled = 1;
786 : : DBG(debug("cipher_init_context: %d", mode));
787 : 5028 : cipher_init(cc, enc->cipher, enc->key, enc->key_len,
788 : 5028 : enc->iv, enc->iv_len, crypt_type);
789 : : /* Deleting the keys does not gain extra security */
790 : : /* explicit_bzero(enc->iv, enc->block_size);
791 : : explicit_bzero(enc->key, enc->key_len);
792 : : explicit_bzero(mac->key, mac->key_len); */
793 [ + - ][ - + ]: 5028 : if ((comp->type == COMP_ZLIB ||
794 [ # # ]: 0 : (comp->type == COMP_DELAYED &&
795 [ # # ]: 0 : active_state->after_authentication)) && comp->enabled == 0) {
796 : 0 : packet_init_compression();
797 [ # # ]: 0 : if (mode == MODE_OUT)
798 : 0 : buffer_compress_init_send(6);
799 : : else
800 : 0 : buffer_compress_init_recv();
801 : 0 : comp->enabled = 1;
802 : : }
803 : : /*
804 : : * The 2^(blocksize*2) limit is too expensive for 3DES,
805 : : * blowfish, etc, so enforce a 1GB limit for small blocksizes.
806 : : */
807 [ + + ]: 5028 : if (enc->block_size >= 16)
808 : 4114 : *max_blocks = (u_int64_t)1 << (enc->block_size*2);
809 : : else
810 : 914 : *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
811 [ + + ]: 5028 : if (active_state->rekey_limit)
812 : 1036 : *max_blocks = MIN(*max_blocks,
813 : : active_state->rekey_limit / enc->block_size);
814 : 5028 : }
815 : :
816 : : /*
817 : : * Delayed compression for SSH2 is enabled after authentication:
818 : : * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
819 : : * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
820 : : */
821 : : static void
822 : 1420 : packet_enable_delayed_compress(void)
823 : : {
824 : 1420 : Comp *comp = NULL;
825 : : int mode;
826 : :
827 : : /*
828 : : * Remember that we are past the authentication step, so rekeying
829 : : * with COMP_DELAYED will turn on compression immediately.
830 : : */
831 : 1420 : active_state->after_authentication = 1;
832 [ + + ]: 4260 : for (mode = 0; mode < MODE_MAX; mode++) {
833 : : /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
834 [ - + ]: 2840 : if (active_state->newkeys[mode] == NULL)
835 : 0 : continue;
836 : 2840 : comp = &active_state->newkeys[mode]->comp;
837 [ + - ][ - + ]: 2840 : if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
838 : 0 : packet_init_compression();
839 [ # # ]: 0 : if (mode == MODE_OUT)
840 : 0 : buffer_compress_init_send(6);
841 : : else
842 : 0 : buffer_compress_init_recv();
843 : 0 : comp->enabled = 1;
844 : : }
845 : : }
846 : 1420 : }
847 : :
848 : : /*
849 : : * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
850 : : */
851 : : static void
852 : 24619 : packet_send2_wrapped(void)
853 : : {
854 : 24619 : u_char type, *cp, *macbuf = NULL;
855 : 24619 : u_char padlen, pad = 0;
856 : 24619 : u_int i, len, authlen = 0, aadlen = 0;
857 : 24619 : u_int32_t rnd = 0;
858 : 24619 : Enc *enc = NULL;
859 : 24619 : Mac *mac = NULL;
860 : 24619 : Comp *comp = NULL;
861 : : int block_size;
862 : :
863 [ + + ]: 24619 : if (active_state->newkeys[MODE_OUT] != NULL) {
864 : 20318 : enc = &active_state->newkeys[MODE_OUT]->enc;
865 : 20318 : mac = &active_state->newkeys[MODE_OUT]->mac;
866 : 20318 : comp = &active_state->newkeys[MODE_OUT]->comp;
867 : : /* disable mac for authenticated encryption */
868 [ + + ]: 20318 : if ((authlen = cipher_authlen(enc->cipher)) != 0)
869 : 2136 : mac = NULL;
870 : : }
871 [ + + ]: 24619 : block_size = enc ? enc->block_size : 8;
872 [ + + ][ + - ]: 24619 : aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
[ + + ][ + + ]
873 : :
874 : 24619 : cp = buffer_ptr(&active_state->outgoing_packet);
875 : 24619 : type = cp[5];
876 : :
877 : : #ifdef PACKET_DEBUG
878 : : fprintf(stderr, "plain: ");
879 : : buffer_dump(&active_state->outgoing_packet);
880 : : #endif
881 : :
882 [ + + ][ - + ]: 24619 : if (comp && comp->enabled) {
883 : 0 : len = buffer_len(&active_state->outgoing_packet);
884 : : /* skip header, compress only payload */
885 : 0 : buffer_consume(&active_state->outgoing_packet, 5);
886 : 0 : buffer_clear(&active_state->compression_buffer);
887 : 0 : buffer_compress(&active_state->outgoing_packet,
888 : 0 : &active_state->compression_buffer);
889 : 0 : buffer_clear(&active_state->outgoing_packet);
890 : 0 : buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
891 : 0 : buffer_append(&active_state->outgoing_packet,
892 : 0 : buffer_ptr(&active_state->compression_buffer),
893 : 0 : buffer_len(&active_state->compression_buffer));
894 : : DBG(debug("compression: raw %d compressed %d", len,
895 : : buffer_len(&active_state->outgoing_packet)));
896 : : }
897 : :
898 : : /* sizeof (packet_len + pad_len + payload) */
899 : 24619 : len = buffer_len(&active_state->outgoing_packet);
900 : :
901 : : /*
902 : : * calc size of padding, alloc space, get random data,
903 : : * minimum padding is 4 bytes
904 : : */
905 : 24619 : len -= aadlen; /* packet length is not encrypted for EtM modes */
906 : 24619 : padlen = block_size - (len % block_size);
907 [ + + ]: 24619 : if (padlen < 4)
908 : 5006 : padlen += block_size;
909 [ - + ]: 24619 : if (active_state->extra_pad) {
910 : : /* will wrap if extra_pad+padlen > 255 */
911 [ # # ]: 0 : active_state->extra_pad =
912 [ # # ]: 0 : roundup(active_state->extra_pad, block_size);
913 : 0 : pad = active_state->extra_pad -
914 : 0 : ((len + padlen) % active_state->extra_pad);
915 : 0 : debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
916 : : pad, len, padlen, active_state->extra_pad);
917 : 0 : padlen += pad;
918 : 0 : active_state->extra_pad = 0;
919 : : }
920 : 24619 : cp = buffer_append_space(&active_state->outgoing_packet, padlen);
921 [ + + ][ + - ]: 24619 : if (enc && !active_state->send_context.plaintext) {
922 : : /* random padding */
923 [ + + ]: 237078 : for (i = 0; i < padlen; i++) {
924 [ + + ]: 216760 : if (i % 4 == 0)
925 : 62421 : rnd = arc4random();
926 : 216760 : cp[i] = rnd & 0xff;
927 : 216760 : rnd >>= 8;
928 : : }
929 : : } else {
930 : : /* clear padding */
931 : 4301 : explicit_bzero(cp, padlen);
932 : : }
933 : : /* sizeof (packet_len + pad_len + payload + padding) */
934 : 24619 : len = buffer_len(&active_state->outgoing_packet);
935 : 24619 : cp = buffer_ptr(&active_state->outgoing_packet);
936 : : /* packet_length includes payload, padding and padding length field */
937 : 24619 : put_u32(cp, len - 4);
938 : 24619 : cp[4] = padlen;
939 : : DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
940 : : len, padlen, aadlen));
941 : :
942 : : /* compute MAC over seqnr and packet(length fields, payload, padding) */
943 [ + + ][ + - ]: 24619 : if (mac && mac->enabled && !mac->etm) {
[ + + ]
944 : 2538 : macbuf = mac_compute(mac, active_state->p_send.seqnr,
945 : 2538 : buffer_ptr(&active_state->outgoing_packet), len);
946 : : DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
947 : : }
948 : : /* encrypt packet and append to output buffer. */
949 : 24619 : cp = buffer_append_space(&active_state->output, len + authlen);
950 [ - + ]: 24619 : if (cipher_crypt(&active_state->send_context, active_state->p_send.seqnr,
951 : 24619 : cp, buffer_ptr(&active_state->outgoing_packet),
952 : : len - aadlen, aadlen, authlen) != 0)
953 : 0 : fatal("%s: cipher_crypt failed", __func__);
954 : : /* append unencrypted MAC */
955 [ + + ][ + - ]: 24619 : if (mac && mac->enabled) {
956 [ + + ]: 18182 : if (mac->etm) {
957 : : /* EtM: compute mac over aadlen + cipher text */
958 : 15644 : macbuf = mac_compute(mac,
959 : 15644 : active_state->p_send.seqnr, cp, len);
960 : : DBG(debug("done calc MAC(EtM) out #%d",
961 : : active_state->p_send.seqnr));
962 : : }
963 : 18182 : buffer_append(&active_state->output, macbuf, mac->mac_len);
964 : : }
965 : : #ifdef PACKET_DEBUG
966 : : fprintf(stderr, "encrypted: ");
967 : : buffer_dump(&active_state->output);
968 : : #endif
969 : : /* increment sequence number for outgoing packets */
970 [ - + ]: 24619 : if (++active_state->p_send.seqnr == 0)
971 : 0 : logit("outgoing seqnr wraps around");
972 [ - + ]: 24619 : if (++active_state->p_send.packets == 0)
973 [ # # ]: 0 : if (!(datafellows & SSH_BUG_NOREKEY))
974 : 0 : fatal("XXX too many packets with same key");
975 : 24619 : active_state->p_send.blocks += len / block_size;
976 : 24619 : active_state->p_send.bytes += len;
977 : 24619 : buffer_clear(&active_state->outgoing_packet);
978 : :
979 [ + + ]: 24619 : if (type == SSH2_MSG_NEWKEYS)
980 : 1872 : set_newkeys(MODE_OUT);
981 [ + + ][ + - ]: 22747 : else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
982 : 732 : packet_enable_delayed_compress();
983 : 24619 : }
984 : :
985 : : static void
986 : 24619 : packet_send2(void)
987 : : {
988 : : struct packet *p;
989 : : u_char type, *cp;
990 : :
991 : 24619 : cp = buffer_ptr(&active_state->outgoing_packet);
992 : 24619 : type = cp[5];
993 : :
994 : : /* during rekeying we can only send key exchange messages */
995 [ + + ]: 24619 : if (active_state->rekeying) {
996 [ + + ]: 3818 : if ((type < SSH2_MSG_TRANSPORT_MIN) ||
997 : 3818 : (type > SSH2_MSG_TRANSPORT_MAX) ||
998 [ - + ]: 3816 : (type == SSH2_MSG_SERVICE_REQUEST) ||
999 : : (type == SSH2_MSG_SERVICE_ACCEPT)) {
1000 : 2 : debug("enqueue packet: %u", type);
1001 : 2 : p = xcalloc(1, sizeof(*p));
1002 : 2 : p->type = type;
1003 : 2 : memcpy(&p->payload, &active_state->outgoing_packet,
1004 : : sizeof(Buffer));
1005 : 2 : buffer_init(&active_state->outgoing_packet);
1006 : 2 : TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
1007 : 2 : return;
1008 : : }
1009 : : }
1010 : :
1011 : : /* rekeying starts with sending KEXINIT */
1012 [ + + ]: 24617 : if (type == SSH2_MSG_KEXINIT)
1013 : 1879 : active_state->rekeying = 1;
1014 : :
1015 : 24617 : packet_send2_wrapped();
1016 : :
1017 : : /* after a NEWKEYS message we can send the complete queue */
1018 [ + + ]: 24617 : if (type == SSH2_MSG_NEWKEYS) {
1019 : 1872 : active_state->rekeying = 0;
1020 : 1872 : active_state->rekey_time = monotime();
1021 [ + + ]: 1874 : while ((p = TAILQ_FIRST(&active_state->outgoing))) {
1022 : 2 : type = p->type;
1023 : 2 : debug("dequeue packet: %u", type);
1024 : 2 : buffer_free(&active_state->outgoing_packet);
1025 : 2 : memcpy(&active_state->outgoing_packet, &p->payload,
1026 : : sizeof(Buffer));
1027 [ - + ]: 2 : TAILQ_REMOVE(&active_state->outgoing, p, next);
1028 : 2 : free(p);
1029 : 2 : packet_send2_wrapped();
1030 : : }
1031 : : }
1032 : : }
1033 : :
1034 : : void
1035 : 28079 : packet_send(void)
1036 : : {
1037 [ + + ]: 28079 : if (compat20)
1038 : 24619 : packet_send2();
1039 : : else
1040 : 3460 : packet_send1();
1041 : : DBG(debug("packet_send done"));
1042 : 28079 : }
1043 : :
1044 : : /*
1045 : : * Waits until a packet has been received, and returns its type. Note that
1046 : : * no other data is processed until this returns, so this function should not
1047 : : * be used during the interactive session.
1048 : : */
1049 : :
1050 : : int
1051 : 12096 : packet_read_seqnr(u_int32_t *seqnr_p)
1052 : : {
1053 : 12096 : int type, len, ret, cont, ms_remain = 0;
1054 : : fd_set *setp;
1055 : : char buf[8192];
1056 : 12096 : struct timeval timeout, start, *timeoutp = NULL;
1057 : :
1058 : : DBG(debug("packet_read()"));
1059 : :
1060 : 12096 : setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1061 : : NFDBITS), sizeof(fd_mask));
1062 : :
1063 : : /* Since we are blocking, ensure that all written packets have been sent. */
1064 : 12096 : packet_write_wait();
1065 : :
1066 : : /* Stay in the loop until we have received a complete packet. */
1067 : : for (;;) {
1068 : : /* Try to read a packet from the buffer. */
1069 : 21817 : type = packet_read_poll_seqnr(seqnr_p);
1070 [ + + ][ + + ]: 21817 : if (!compat20 && (
1071 : : type == SSH_SMSG_SUCCESS
1072 : 1823 : || type == SSH_SMSG_FAILURE
1073 : 1823 : || type == SSH_CMSG_EOF
1074 [ + + ]: 1623 : || type == SSH_CMSG_EXIT_CONFIRMATION))
1075 [ - + ]: 223 : packet_check_eom();
1076 : : /* If we got a packet, return it. */
1077 [ + + ]: 21817 : if (type != SSH_MSG_NONE) {
1078 : 12096 : free(setp);
1079 : 12096 : return type;
1080 : : }
1081 : : /*
1082 : : * Otherwise, wait for some data to arrive, add it to the
1083 : : * buffer, and try again.
1084 : : */
1085 : 9721 : memset(setp, 0, howmany(active_state->connection_in + 1,
1086 : : NFDBITS) * sizeof(fd_mask));
1087 [ - + ][ # # ]: 9721 : FD_SET(active_state->connection_in, setp);
1088 : :
1089 [ - + ]: 9721 : if (active_state->packet_timeout_ms > 0) {
1090 : 0 : ms_remain = active_state->packet_timeout_ms;
1091 : 9721 : timeoutp = &timeout;
1092 : : }
1093 : : /* Wait for some data to arrive. */
1094 : : for (;;) {
1095 [ - + ]: 9721 : if (active_state->packet_timeout_ms != -1) {
1096 : 0 : ms_to_timeval(&timeout, ms_remain);
1097 : 0 : gettimeofday(&start, NULL);
1098 : : }
1099 [ - + ]: 9721 : if ((ret = select(active_state->connection_in + 1, setp,
1100 : : NULL, NULL, timeoutp)) >= 0)
1101 : : break;
1102 [ # # ][ # # ]: 0 : if (errno != EAGAIN && errno != EINTR &&
1103 : : errno != EWOULDBLOCK)
1104 : : break;
1105 [ # # ]: 0 : if (active_state->packet_timeout_ms == -1)
1106 : 0 : continue;
1107 : 0 : ms_subtract_diff(&start, &ms_remain);
1108 [ # # ]: 0 : if (ms_remain <= 0) {
1109 : : ret = 0;
1110 : : break;
1111 : : }
1112 : : }
1113 [ - + ]: 9721 : if (ret == 0) {
1114 : 0 : logit("Connection to %.200s timed out while "
1115 : : "waiting to read", get_remote_ipaddr());
1116 : 9721 : cleanup_exit(255);
1117 : : }
1118 : : /* Read data from the socket. */
1119 : : do {
1120 : 9721 : cont = 0;
1121 : 9721 : len = roaming_read(active_state->connection_in, buf,
1122 : : sizeof(buf), &cont);
1123 [ - + ][ # # ]: 9721 : } while (len == 0 && cont);
1124 [ - + ]: 9721 : if (len == 0) {
1125 : 0 : logit("Connection closed by %.200s", get_remote_ipaddr());
1126 : 0 : cleanup_exit(255);
1127 : : }
1128 [ - + ]: 9721 : if (len < 0)
1129 : 0 : fatal("Read from socket failed: %.100s", strerror(errno));
1130 : : /* Append it to the buffer. */
1131 : 9721 : packet_process_incoming(buf, len);
1132 : 9721 : }
1133 : : /* NOTREACHED */
1134 : : }
1135 : :
1136 : : int
1137 : 1151 : packet_read(void)
1138 : : {
1139 : 5380 : return packet_read_seqnr(NULL);
1140 : : }
1141 : :
1142 : : /*
1143 : : * Waits until a packet has been received, verifies that its type matches
1144 : : * that given, and gives a fatal error and exits if there is a mismatch.
1145 : : */
1146 : :
1147 : : void
1148 : 4229 : packet_read_expect(int expected_type)
1149 : : {
1150 : : int type;
1151 : :
1152 : 4229 : type = packet_read();
1153 [ - + ]: 4229 : if (type != expected_type)
1154 : 0 : packet_disconnect("Protocol error: expected packet type %d, got %d",
1155 : : expected_type, type);
1156 : 4229 : }
1157 : :
1158 : : /* Checks if a full packet is available in the data received so far via
1159 : : * packet_process_incoming. If so, reads the packet; otherwise returns
1160 : : * SSH_MSG_NONE. This does not wait for data from the connection.
1161 : : *
1162 : : * SSH_MSG_DISCONNECT is handled specially here. Also,
1163 : : * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1164 : : * to higher levels.
1165 : : */
1166 : :
1167 : : static int
1168 : 9703 : packet_read_poll1(void)
1169 : : {
1170 : : u_int len, padded_len;
1171 : : u_char *cp, type;
1172 : : u_int checksum, stored_checksum;
1173 : :
1174 : : /* Check if input size is less than minimum packet size. */
1175 [ + + ]: 9703 : if (buffer_len(&active_state->input) < 4 + 8)
1176 : : return SSH_MSG_NONE;
1177 : : /* Get length of incoming packet. */
1178 : 3760 : cp = buffer_ptr(&active_state->input);
1179 : 3760 : len = get_u32(cp);
1180 [ - + ]: 3760 : if (len < 1 + 2 + 2 || len > 256 * 1024)
1181 : 0 : packet_disconnect("Bad packet length %u.", len);
1182 : 3760 : padded_len = (len + 8) & ~7;
1183 : :
1184 : : /* Check if the packet has been entirely received. */
1185 [ + + ]: 3760 : if (buffer_len(&active_state->input) < 4 + padded_len)
1186 : : return SSH_MSG_NONE;
1187 : :
1188 : : /* The entire packet is in buffer. */
1189 : :
1190 : : /* Consume packet length. */
1191 : 2391 : buffer_consume(&active_state->input, 4);
1192 : :
1193 : : /*
1194 : : * Cryptographic attack detector for ssh
1195 : : * (C)1998 CORE-SDI, Buenos Aires Argentina
1196 : : * Ariel Futoransky(futo@core-sdi.com)
1197 : : */
1198 [ + + ]: 2391 : if (!active_state->receive_context.plaintext) {
1199 [ - - + ]: 2226 : switch (detect_attack(buffer_ptr(&active_state->input),
1200 : : padded_len)) {
1201 : : case DEATTACK_DETECTED:
1202 : 0 : packet_disconnect("crc32 compensation attack: "
1203 : : "network attack detected");
1204 : : case DEATTACK_DOS_DETECTED:
1205 : 0 : packet_disconnect("deattack denial of "
1206 : : "service detected");
1207 : : }
1208 : : }
1209 : :
1210 : : /* Decrypt data to incoming_packet. */
1211 : 2391 : buffer_clear(&active_state->incoming_packet);
1212 : 2391 : cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1213 [ - + ]: 2391 : if (cipher_crypt(&active_state->receive_context, 0, cp,
1214 : 2391 : buffer_ptr(&active_state->input), padded_len, 0, 0) != 0)
1215 : 0 : fatal("%s: cipher_crypt failed", __func__);
1216 : :
1217 : 2391 : buffer_consume(&active_state->input, padded_len);
1218 : :
1219 : : #ifdef PACKET_DEBUG
1220 : : fprintf(stderr, "read_poll plain: ");
1221 : : buffer_dump(&active_state->incoming_packet);
1222 : : #endif
1223 : :
1224 : : /* Compute packet checksum. */
1225 : 2391 : checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1226 : 2391 : buffer_len(&active_state->incoming_packet) - 4);
1227 : :
1228 : : /* Skip padding. */
1229 : 2391 : buffer_consume(&active_state->incoming_packet, 8 - len % 8);
1230 : :
1231 : : /* Test check bytes. */
1232 [ - + ]: 2391 : if (len != buffer_len(&active_state->incoming_packet))
1233 : 0 : packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
1234 : 0 : len, buffer_len(&active_state->incoming_packet));
1235 : :
1236 : 2391 : cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
1237 : 2391 : stored_checksum = get_u32(cp);
1238 [ - + ]: 2391 : if (checksum != stored_checksum)
1239 : 0 : packet_disconnect("Corrupted check bytes on input.");
1240 : 2391 : buffer_consume_end(&active_state->incoming_packet, 4);
1241 : :
1242 [ - + ]: 2391 : if (active_state->packet_compression) {
1243 : 0 : buffer_clear(&active_state->compression_buffer);
1244 : 0 : buffer_uncompress(&active_state->incoming_packet,
1245 : 0 : &active_state->compression_buffer);
1246 : 0 : buffer_clear(&active_state->incoming_packet);
1247 : 0 : buffer_append(&active_state->incoming_packet,
1248 : 0 : buffer_ptr(&active_state->compression_buffer),
1249 : 0 : buffer_len(&active_state->compression_buffer));
1250 : : }
1251 : 2391 : active_state->p_read.packets++;
1252 : 2391 : active_state->p_read.bytes += padded_len + 4;
1253 : 2391 : type = buffer_get_char(&active_state->incoming_packet);
1254 [ - + ]: 2391 : if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1255 : 0 : packet_disconnect("Invalid ssh1 packet type: %d", type);
1256 : 2391 : return type;
1257 : : }
1258 : :
1259 : : static int
1260 : 65646 : packet_read_poll2(u_int32_t *seqnr_p)
1261 : : {
1262 : : u_int padlen, need;
1263 : 65646 : u_char *macbuf = NULL, *cp, type;
1264 : 65646 : u_int maclen, authlen = 0, aadlen = 0, block_size;
1265 : 65646 : Enc *enc = NULL;
1266 : 65646 : Mac *mac = NULL;
1267 : 65646 : Comp *comp = NULL;
1268 : :
1269 [ + - ]: 65646 : if (active_state->packet_discard)
1270 : : return SSH_MSG_NONE;
1271 : :
1272 [ + + ]: 65646 : if (active_state->newkeys[MODE_IN] != NULL) {
1273 : 58407 : enc = &active_state->newkeys[MODE_IN]->enc;
1274 : 58407 : mac = &active_state->newkeys[MODE_IN]->mac;
1275 : 58407 : comp = &active_state->newkeys[MODE_IN]->comp;
1276 : : /* disable mac for authenticated encryption */
1277 [ + + ]: 58407 : if ((authlen = cipher_authlen(enc->cipher)) != 0)
1278 : 6320 : mac = NULL;
1279 : : }
1280 [ + + ][ + - ]: 65646 : maclen = mac && mac->enabled ? mac->mac_len : 0;
1281 [ + + ]: 65646 : block_size = enc ? enc->block_size : 8;
1282 [ + + ][ + - ]: 65646 : aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
[ + + ][ + + ]
1283 : :
1284 [ + + ][ + + ]: 65646 : if (aadlen && active_state->packlen == 0) {
1285 [ + + ]: 96642 : if (cipher_get_length(&active_state->receive_context,
1286 : : &active_state->packlen,
1287 : 48321 : active_state->p_read.seqnr,
1288 : 48321 : buffer_ptr(&active_state->input),
1289 : 48321 : buffer_len(&active_state->input)) != 0)
1290 : : return SSH_MSG_NONE;
1291 [ - + ]: 15319 : if (active_state->packlen < 1 + 4 ||
1292 : : active_state->packlen > PACKET_MAX_SIZE) {
1293 : : #ifdef PACKET_DEBUG
1294 : : buffer_dump(&active_state->input);
1295 : : #endif
1296 : 0 : logit("Bad packet length %u.", active_state->packlen);
1297 : 0 : packet_disconnect("Packet corrupt");
1298 : : }
1299 : 15319 : buffer_clear(&active_state->incoming_packet);
1300 [ + + ]: 17325 : } else if (active_state->packlen == 0) {
1301 : : /*
1302 : : * check if input size is less than the cipher block size,
1303 : : * decrypt first block and extract length of incoming packet
1304 : : */
1305 [ + + ]: 13669 : if (buffer_len(&active_state->input) < block_size)
1306 : : return SSH_MSG_NONE;
1307 : 7059 : buffer_clear(&active_state->incoming_packet);
1308 : 7059 : cp = buffer_append_space(&active_state->incoming_packet,
1309 : : block_size);
1310 [ - + ]: 7059 : if (cipher_crypt(&active_state->receive_context,
1311 : 7059 : active_state->p_read.seqnr, cp,
1312 : 7059 : buffer_ptr(&active_state->input), block_size, 0, 0) != 0)
1313 : 0 : fatal("Decryption integrity check failed");
1314 : 7059 : cp = buffer_ptr(&active_state->incoming_packet);
1315 : 7059 : active_state->packlen = get_u32(cp);
1316 [ - + ]: 7059 : if (active_state->packlen < 1 + 4 ||
1317 : : active_state->packlen > PACKET_MAX_SIZE) {
1318 : : #ifdef PACKET_DEBUG
1319 : : buffer_dump(&active_state->incoming_packet);
1320 : : #endif
1321 : 0 : logit("Bad packet length %u.", active_state->packlen);
1322 : 0 : packet_start_discard(enc, mac, active_state->packlen,
1323 : : PACKET_MAX_SIZE);
1324 : 0 : return SSH_MSG_NONE;
1325 : : }
1326 : 7059 : buffer_consume(&active_state->input, block_size);
1327 : : }
1328 : : DBG(debug("input: packet len %u", active_state->packlen+4));
1329 [ + + ]: 26034 : if (aadlen) {
1330 : : /* only the payload is encrypted */
1331 : 18975 : need = active_state->packlen;
1332 : : } else {
1333 : : /*
1334 : : * the payload size and the payload are encrypted, but we
1335 : : * have a partial packet of block_size bytes
1336 : : */
1337 : 7059 : need = 4 + active_state->packlen - block_size;
1338 : : }
1339 : : DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1340 : : " aadlen %d", block_size, need, maclen, authlen, aadlen));
1341 [ - + ]: 26034 : if (need % block_size != 0) {
1342 : 0 : logit("padding error: need %d block %d mod %d",
1343 : : need, block_size, need % block_size);
1344 : 0 : packet_start_discard(enc, mac, active_state->packlen,
1345 : : PACKET_MAX_SIZE - block_size);
1346 : 0 : return SSH_MSG_NONE;
1347 : : }
1348 : : /*
1349 : : * check if the entire packet has been received and
1350 : : * decrypt into incoming_packet:
1351 : : * 'aadlen' bytes are unencrypted, but authenticated.
1352 : : * 'need' bytes are encrypted, followed by either
1353 : : * 'authlen' bytes of authentication tag or
1354 : : * 'maclen' bytes of message authentication code.
1355 : : */
1356 [ + + ]: 26034 : if (buffer_len(&active_state->input) < aadlen + need + authlen + maclen)
1357 : : return SSH_MSG_NONE;
1358 : : #ifdef PACKET_DEBUG
1359 : : fprintf(stderr, "read_poll enc/full: ");
1360 : : buffer_dump(&active_state->input);
1361 : : #endif
1362 : : /* EtM: compute mac over encrypted input */
1363 [ + + ][ + - ]: 22378 : if (mac && mac->enabled && mac->etm)
[ + + ]
1364 : 13996 : macbuf = mac_compute(mac, active_state->p_read.seqnr,
1365 : 13996 : buffer_ptr(&active_state->input), aadlen + need);
1366 : 22378 : cp = buffer_append_space(&active_state->incoming_packet, aadlen + need);
1367 [ - + ]: 22378 : if (cipher_crypt(&active_state->receive_context,
1368 : 22378 : active_state->p_read.seqnr, cp,
1369 : 22378 : buffer_ptr(&active_state->input), need, aadlen, authlen) != 0)
1370 : 0 : fatal("Decryption integrity check failed");
1371 : 22378 : buffer_consume(&active_state->input, aadlen + need + authlen);
1372 : : /*
1373 : : * compute MAC over seqnr and packet,
1374 : : * increment sequence number for incoming packet
1375 : : */
1376 [ + + ][ + - ]: 22378 : if (mac && mac->enabled) {
1377 [ + + ]: 16754 : if (!mac->etm)
1378 : 2758 : macbuf = mac_compute(mac, active_state->p_read.seqnr,
1379 : 2758 : buffer_ptr(&active_state->incoming_packet),
1380 : 2758 : buffer_len(&active_state->incoming_packet));
1381 [ - + ]: 16754 : if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input),
1382 : 16754 : mac->mac_len) != 0) {
1383 : 0 : logit("Corrupted MAC on input.");
1384 [ # # ]: 0 : if (need > PACKET_MAX_SIZE)
1385 : 0 : fatal("internal error need %d", need);
1386 : 0 : packet_start_discard(enc, mac, active_state->packlen,
1387 : : PACKET_MAX_SIZE - need);
1388 : 0 : return SSH_MSG_NONE;
1389 : : }
1390 : :
1391 : : DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1392 : 16754 : buffer_consume(&active_state->input, mac->mac_len);
1393 : : }
1394 : : /* XXX now it's safe to use fatal/packet_disconnect */
1395 [ + + ]: 22378 : if (seqnr_p != NULL)
1396 : 17879 : *seqnr_p = active_state->p_read.seqnr;
1397 [ - + ]: 22378 : if (++active_state->p_read.seqnr == 0)
1398 : 0 : logit("incoming seqnr wraps around");
1399 [ - + ]: 22378 : if (++active_state->p_read.packets == 0)
1400 [ # # ]: 0 : if (!(datafellows & SSH_BUG_NOREKEY))
1401 : 0 : fatal("XXX too many packets with same key");
1402 : 22378 : active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1403 : 22378 : active_state->p_read.bytes += active_state->packlen + 4;
1404 : :
1405 : : /* get padlen */
1406 : 22378 : cp = buffer_ptr(&active_state->incoming_packet);
1407 : 22378 : padlen = cp[4];
1408 : : DBG(debug("input: padlen %d", padlen));
1409 [ - + ]: 22378 : if (padlen < 4)
1410 : 0 : packet_disconnect("Corrupted padlen %d on input.", padlen);
1411 : :
1412 : : /* skip packet size + padlen, discard padding */
1413 : 22378 : buffer_consume(&active_state->incoming_packet, 4 + 1);
1414 : 22378 : buffer_consume_end(&active_state->incoming_packet, padlen);
1415 : :
1416 : : DBG(debug("input: len before de-compress %d",
1417 : : buffer_len(&active_state->incoming_packet)));
1418 [ + + ][ - + ]: 22378 : if (comp && comp->enabled) {
1419 : 0 : buffer_clear(&active_state->compression_buffer);
1420 : 0 : buffer_uncompress(&active_state->incoming_packet,
1421 : 0 : &active_state->compression_buffer);
1422 : 0 : buffer_clear(&active_state->incoming_packet);
1423 : 0 : buffer_append(&active_state->incoming_packet,
1424 : 0 : buffer_ptr(&active_state->compression_buffer),
1425 : 0 : buffer_len(&active_state->compression_buffer));
1426 : : DBG(debug("input: len after de-compress %d",
1427 : : buffer_len(&active_state->incoming_packet)));
1428 : : }
1429 : : /*
1430 : : * get packet type, implies consume.
1431 : : * return length of payload (without type field)
1432 : : */
1433 : 22378 : type = buffer_get_char(&active_state->incoming_packet);
1434 [ - + ]: 22378 : if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1435 : 0 : packet_disconnect("Invalid ssh2 packet type: %d", type);
1436 [ + + ]: 22378 : if (type == SSH2_MSG_NEWKEYS)
1437 : 1872 : set_newkeys(MODE_IN);
1438 [ + + ][ + - ]: 20506 : else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1439 : 688 : !active_state->server_side)
1440 : 688 : packet_enable_delayed_compress();
1441 : : #ifdef PACKET_DEBUG
1442 : : fprintf(stderr, "read/plain[%d]:\r\n", type);
1443 : : buffer_dump(&active_state->incoming_packet);
1444 : : #endif
1445 : : /* reset for next packet */
1446 : 22378 : active_state->packlen = 0;
1447 : 22378 : return type;
1448 : : }
1449 : :
1450 : : int
1451 : 150328 : packet_read_poll_seqnr(u_int32_t *seqnr_p)
1452 : : {
1453 : : u_int reason, seqnr;
1454 : : u_char type;
1455 : : char *msg;
1456 : :
1457 : : for (;;) {
1458 [ + + ]: 75349 : if (compat20) {
1459 : 65646 : type = packet_read_poll2(seqnr_p);
1460 [ + + ]: 65646 : if (type) {
1461 : 22378 : active_state->keep_alive_timeouts = 0;
1462 : : DBG(debug("received packet type %d", type));
1463 : : }
1464 [ - + - - : 65646 : switch (type) {
+ ]
1465 : : case SSH2_MSG_IGNORE:
1466 : 0 : debug3("Received SSH2_MSG_IGNORE");
1467 : 185 : break;
1468 : : case SSH2_MSG_DEBUG:
1469 : : packet_get_char();
1470 : 116 : msg = packet_get_string(NULL);
1471 : 116 : debug("Remote: %.900s", msg);
1472 : 116 : free(msg);
1473 : 116 : msg = packet_get_string(NULL);
1474 : 116 : free(msg);
1475 : 116 : break;
1476 : : case SSH2_MSG_DISCONNECT:
1477 : 0 : reason = packet_get_int();
1478 : 0 : msg = packet_get_string(NULL);
1479 : : /* Ignore normal client exit notifications */
1480 [ # # ][ # # ]: 0 : do_log2(active_state->server_side &&
1481 : : reason == SSH2_DISCONNECT_BY_APPLICATION ?
1482 : : SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1483 : : "Received disconnect from %s: %u: %.400s",
1484 : : get_remote_ipaddr(), reason, msg);
1485 : 0 : free(msg);
1486 : 0 : cleanup_exit(255);
1487 : : break;
1488 : : case SSH2_MSG_UNIMPLEMENTED:
1489 : 0 : seqnr = packet_get_int();
1490 : 0 : debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1491 : : seqnr);
1492 : 0 : break;
1493 : : default:
1494 : : return type;
1495 : : }
1496 : : } else {
1497 : 9703 : type = packet_read_poll1();
1498 [ - + - + : 9703 : switch (type) {
+ ]
1499 : : case SSH_MSG_NONE:
1500 : : return SSH_MSG_NONE;
1501 : : case SSH_MSG_IGNORE:
1502 : : break;
1503 : : case SSH_MSG_DEBUG:
1504 : 69 : msg = packet_get_string(NULL);
1505 : 69 : debug("Remote: %.900s", msg);
1506 : 69 : free(msg);
1507 : 69 : break;
1508 : : case SSH_MSG_DISCONNECT:
1509 : 0 : msg = packet_get_string(NULL);
1510 : 0 : error("Received disconnect from %s: %.400s",
1511 : : get_remote_ipaddr(), msg);
1512 : 0 : cleanup_exit(255);
1513 : : break;
1514 : : default:
1515 : : DBG(debug("received packet type %d", type));
1516 : 2322 : return type;
1517 : : }
1518 : : }
1519 : : }
1520 : : }
1521 : :
1522 : : /*
1523 : : * Buffers the given amount of input characters. This is intended to be used
1524 : : * together with packet_read_poll.
1525 : : */
1526 : :
1527 : : void
1528 : 22160 : packet_process_incoming(const char *buf, u_int len)
1529 : : {
1530 [ - + ]: 22160 : if (active_state->packet_discard) {
1531 : 0 : active_state->keep_alive_timeouts = 0; /* ?? */
1532 [ # # ]: 0 : if (len >= active_state->packet_discard)
1533 : 0 : packet_stop_discard();
1534 : 0 : active_state->packet_discard -= len;
1535 : 0 : return;
1536 : : }
1537 : 22160 : buffer_append(&active_state->input, buf, len);
1538 : : }
1539 : :
1540 : : /* Returns a character from the packet. */
1541 : :
1542 : : u_int
1543 : 40280 : packet_get_char(void)
1544 : : {
1545 : : char ch;
1546 : :
1547 : 40396 : buffer_get(&active_state->incoming_packet, &ch, 1);
1548 : 40280 : return (u_char) ch;
1549 : : }
1550 : :
1551 : : /* Returns an integer from the packet data. */
1552 : :
1553 : : u_int
1554 : 19529 : packet_get_int(void)
1555 : : {
1556 : 19529 : return buffer_get_int(&active_state->incoming_packet);
1557 : : }
1558 : :
1559 : : /* Returns an 64 bit integer from the packet data. */
1560 : :
1561 : : u_int64_t
1562 : 0 : packet_get_int64(void)
1563 : : {
1564 : 0 : return buffer_get_int64(&active_state->incoming_packet);
1565 : : }
1566 : :
1567 : : /*
1568 : : * Returns an arbitrary precision integer from the packet data. The integer
1569 : : * must have been initialized before this call.
1570 : : */
1571 : :
1572 : : void
1573 : 513 : packet_get_bignum(BIGNUM * value)
1574 : : {
1575 : 513 : buffer_get_bignum(&active_state->incoming_packet, value);
1576 : 513 : }
1577 : :
1578 : : void
1579 : 263 : packet_get_bignum2(BIGNUM * value)
1580 : : {
1581 : 263 : buffer_get_bignum2(&active_state->incoming_packet, value);
1582 : 263 : }
1583 : :
1584 : : #ifdef OPENSSL_HAS_ECC
1585 : : void
1586 : 96 : packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point)
1587 : : {
1588 : 96 : buffer_get_ecpoint(&active_state->incoming_packet, curve, point);
1589 : 96 : }
1590 : : #endif
1591 : :
1592 : : void *
1593 : 1878 : packet_get_raw(u_int *length_ptr)
1594 : : {
1595 : 1878 : u_int bytes = buffer_len(&active_state->incoming_packet);
1596 : :
1597 [ + - ]: 1878 : if (length_ptr != NULL)
1598 : 1878 : *length_ptr = bytes;
1599 : 1878 : return buffer_ptr(&active_state->incoming_packet);
1600 : : }
1601 : :
1602 : : int
1603 : 23482 : packet_remaining(void)
1604 : : {
1605 : 23705 : return buffer_len(&active_state->incoming_packet);
1606 : : }
1607 : :
1608 : : /*
1609 : : * Returns a string from the packet data. The string is allocated using
1610 : : * xmalloc; it is the responsibility of the calling program to free it when
1611 : : * no longer needed. The length_ptr argument may be NULL, or point to an
1612 : : * integer into which the length of the string is stored.
1613 : : */
1614 : :
1615 : : void *
1616 : 34930 : packet_get_string(u_int *length_ptr)
1617 : : {
1618 : 35231 : return buffer_get_string(&active_state->incoming_packet, length_ptr);
1619 : : }
1620 : :
1621 : : void *
1622 : 2697 : packet_get_string_ptr(u_int *length_ptr)
1623 : : {
1624 : 2697 : return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
1625 : : }
1626 : :
1627 : : /* Ensures the returned string has no embedded \0 characters in it. */
1628 : : char *
1629 : 7982 : packet_get_cstring(u_int *length_ptr)
1630 : : {
1631 : 7982 : return buffer_get_cstring(&active_state->incoming_packet, length_ptr);
1632 : : }
1633 : :
1634 : : /*
1635 : : * Sends a diagnostic message from the server to the client. This message
1636 : : * can be sent at any time (but not while constructing another message). The
1637 : : * message is printed immediately, but only if the client is being executed
1638 : : * in verbose mode. These messages are primarily intended to ease debugging
1639 : : * authentication problems. The length of the formatted message must not
1640 : : * exceed 1024 bytes. This will automatically call packet_write_wait.
1641 : : */
1642 : :
1643 : : void
1644 : 228 : packet_send_debug(const char *fmt,...)
1645 : : {
1646 : : char buf[1024];
1647 : : va_list args;
1648 : :
1649 [ + + ][ + - ]: 228 : if (compat20 && (datafellows & SSH_BUG_DEBUG))
1650 : 0 : return;
1651 : :
1652 : 228 : va_start(args, fmt);
1653 : : vsnprintf(buf, sizeof(buf), fmt, args);
1654 : 228 : va_end(args);
1655 : :
1656 [ + + ]: 228 : if (compat20) {
1657 : 116 : packet_start(SSH2_MSG_DEBUG);
1658 : : packet_put_char(0); /* bool: always display */
1659 : : packet_put_cstring(buf);
1660 : : packet_put_cstring("");
1661 : : } else {
1662 : 112 : packet_start(SSH_MSG_DEBUG);
1663 : : packet_put_cstring(buf);
1664 : : }
1665 : 228 : packet_send();
1666 : 228 : packet_write_wait();
1667 : : }
1668 : :
1669 : : /*
1670 : : * Logs the error plus constructs and sends a disconnect packet, closes the
1671 : : * connection, and exits. This function never returns. The error message
1672 : : * should not contain a newline. The length of the formatted message must
1673 : : * not exceed 1024 bytes.
1674 : : */
1675 : :
1676 : : void
1677 : 0 : packet_disconnect(const char *fmt,...)
1678 : : {
1679 : : char buf[1024];
1680 : : va_list args;
1681 : : static int disconnecting = 0;
1682 : :
1683 [ # # ]: 0 : if (disconnecting) /* Guard against recursive invocations. */
1684 : 0 : fatal("packet_disconnect called recursively.");
1685 : 0 : disconnecting = 1;
1686 : :
1687 : : /*
1688 : : * Format the message. Note that the caller must make sure the
1689 : : * message is of limited size.
1690 : : */
1691 : 0 : va_start(args, fmt);
1692 : : vsnprintf(buf, sizeof(buf), fmt, args);
1693 : 0 : va_end(args);
1694 : :
1695 : : /* Display the error locally */
1696 : 0 : logit("Disconnecting: %.100s", buf);
1697 : :
1698 : : /* Send the disconnect message to the other side, and wait for it to get sent. */
1699 [ # # ]: 0 : if (compat20) {
1700 : 0 : packet_start(SSH2_MSG_DISCONNECT);
1701 : : packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1702 : : packet_put_cstring(buf);
1703 : : packet_put_cstring("");
1704 : : } else {
1705 : 0 : packet_start(SSH_MSG_DISCONNECT);
1706 : : packet_put_cstring(buf);
1707 : : }
1708 : 0 : packet_send();
1709 : 0 : packet_write_wait();
1710 : :
1711 : : /* Stop listening for connections. */
1712 : 0 : channel_close_all();
1713 : :
1714 : : /* Close the connection. */
1715 : 0 : packet_close();
1716 : 0 : cleanup_exit(255);
1717 : : }
1718 : :
1719 : : /* Checks if there is any buffered output, and tries to write some of the output. */
1720 : :
1721 : : void
1722 : 31960 : packet_write_poll(void)
1723 : : {
1724 : 31960 : int len = buffer_len(&active_state->output);
1725 : : int cont;
1726 : :
1727 [ + + ]: 31960 : if (len > 0) {
1728 : 26540 : cont = 0;
1729 : 26540 : len = roaming_write(active_state->connection_out,
1730 : 26540 : buffer_ptr(&active_state->output), len, &cont);
1731 [ + + ]: 26540 : if (len == -1) {
1732 [ - + ][ # # ]: 1 : if (errno == EINTR || errno == EAGAIN ||
1733 : : errno == EWOULDBLOCK)
1734 : 1 : return;
1735 : 0 : fatal("Write failed: %.100s", strerror(errno));
1736 : : }
1737 [ - + ][ # # ]: 26539 : if (len == 0 && !cont)
1738 : 0 : fatal("Write connection closed");
1739 : 31959 : buffer_consume(&active_state->output, len);
1740 : : }
1741 : : }
1742 : :
1743 : : /*
1744 : : * Calls packet_write_poll repeatedly until all pending output data has been
1745 : : * written.
1746 : : */
1747 : :
1748 : : void
1749 : 17818 : packet_write_wait(void)
1750 : : {
1751 : : fd_set *setp;
1752 : 17818 : int ret, ms_remain = 0;
1753 : 17818 : struct timeval start, timeout, *timeoutp = NULL;
1754 : :
1755 : 17818 : setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1756 : : NFDBITS), sizeof(fd_mask));
1757 : 17818 : packet_write_poll();
1758 [ + + ]: 35639 : while (packet_have_data_to_write()) {
1759 : 3 : memset(setp, 0, howmany(active_state->connection_out + 1,
1760 : : NFDBITS) * sizeof(fd_mask));
1761 [ - + ][ # # ]: 3 : FD_SET(active_state->connection_out, setp);
1762 : :
1763 [ - + ]: 3 : if (active_state->packet_timeout_ms > 0) {
1764 : 0 : ms_remain = active_state->packet_timeout_ms;
1765 : 3 : timeoutp = &timeout;
1766 : : }
1767 : : for (;;) {
1768 [ - + ]: 3 : if (active_state->packet_timeout_ms != -1) {
1769 : 0 : ms_to_timeval(&timeout, ms_remain);
1770 : 0 : gettimeofday(&start, NULL);
1771 : : }
1772 [ - + ]: 3 : if ((ret = select(active_state->connection_out + 1,
1773 : : NULL, setp, NULL, timeoutp)) >= 0)
1774 : : break;
1775 [ # # ][ # # ]: 0 : if (errno != EAGAIN && errno != EINTR &&
1776 : : errno != EWOULDBLOCK)
1777 : : break;
1778 [ # # ]: 0 : if (active_state->packet_timeout_ms == -1)
1779 : 0 : continue;
1780 : 0 : ms_subtract_diff(&start, &ms_remain);
1781 [ # # ]: 0 : if (ms_remain <= 0) {
1782 : : ret = 0;
1783 : : break;
1784 : : }
1785 : : }
1786 [ - + ]: 3 : if (ret == 0) {
1787 : 0 : logit("Connection to %.200s timed out while "
1788 : : "waiting to write", get_remote_ipaddr());
1789 : 0 : cleanup_exit(255);
1790 : : }
1791 : 3 : packet_write_poll();
1792 : : }
1793 : 17818 : free(setp);
1794 : 17818 : }
1795 : :
1796 : : /* Returns true if there is buffered data to write to the connection. */
1797 : :
1798 : : int
1799 : 40861 : packet_have_data_to_write(void)
1800 : : {
1801 : 58682 : return buffer_len(&active_state->output) != 0;
1802 : : }
1803 : :
1804 : : /* Returns true if there is not too much data to write to the connection. */
1805 : :
1806 : : int
1807 : 43303 : packet_not_very_much_data_to_write(void)
1808 : : {
1809 [ - + ]: 43303 : if (active_state->interactive_mode)
1810 : 0 : return buffer_len(&active_state->output) < 16384;
1811 : : else
1812 : 43303 : return buffer_len(&active_state->output) < 128 * 1024;
1813 : : }
1814 : :
1815 : : static void
1816 : 96 : packet_set_tos(int tos)
1817 : : {
1818 : : #ifndef IP_TOS_IS_BROKEN
1819 [ + - ]: 96 : if (!packet_connection_is_on_socket())
1820 : 96 : return;
1821 [ + - - ]: 96 : switch (packet_connection_af()) {
1822 : : # ifdef IP_TOS
1823 : : case AF_INET:
1824 : 96 : debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1825 [ - + ]: 96 : if (setsockopt(active_state->connection_in,
1826 : : IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
1827 : 0 : error("setsockopt IP_TOS %d: %.100s:",
1828 : 0 : tos, strerror(errno));
1829 : : break;
1830 : : # endif /* IP_TOS */
1831 : : # ifdef IPV6_TCLASS
1832 : : case AF_INET6:
1833 : 0 : debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1834 [ # # ]: 0 : if (setsockopt(active_state->connection_in,
1835 : : IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
1836 : 0 : error("setsockopt IPV6_TCLASS %d: %.100s:",
1837 : 0 : tos, strerror(errno));
1838 : : break;
1839 : : # endif /* IPV6_TCLASS */
1840 : : }
1841 : : #endif /* IP_TOS_IS_BROKEN */
1842 : : }
1843 : :
1844 : : /* Informs that the current session is interactive. Sets IP flags for that. */
1845 : :
1846 : : void
1847 : 1489 : packet_set_interactive(int interactive, int qos_interactive, int qos_bulk)
1848 : : {
1849 [ + + ]: 1489 : if (active_state->set_interactive_called)
1850 : : return;
1851 : 774 : active_state->set_interactive_called = 1;
1852 : :
1853 : : /* Record that we are in interactive mode. */
1854 : 774 : active_state->interactive_mode = interactive;
1855 : :
1856 : : /* Only set socket options if using a socket. */
1857 [ + + ]: 774 : if (!packet_connection_is_on_socket())
1858 : : return;
1859 : 96 : set_nodelay(active_state->connection_in);
1860 [ + - ]: 96 : packet_set_tos(interactive ? qos_interactive : qos_bulk);
1861 : : }
1862 : :
1863 : : /* Returns true if the current connection is interactive. */
1864 : :
1865 : : int
1866 : 1244 : packet_is_interactive(void)
1867 : : {
1868 : 1244 : return active_state->interactive_mode;
1869 : : }
1870 : :
1871 : : int
1872 : 0 : packet_set_maxsize(u_int s)
1873 : : {
1874 [ # # ]: 0 : if (active_state->set_maxsize_called) {
1875 : 0 : logit("packet_set_maxsize: called twice: old %d new %d",
1876 : : active_state->max_packet_size, s);
1877 : 0 : return -1;
1878 : : }
1879 [ # # ]: 0 : if (s < 4 * 1024 || s > 1024 * 1024) {
1880 : 0 : logit("packet_set_maxsize: bad size %d", s);
1881 : 0 : return -1;
1882 : : }
1883 : 0 : active_state->set_maxsize_called = 1;
1884 : 0 : debug("packet_set_maxsize: setting to %d", s);
1885 : 0 : active_state->max_packet_size = s;
1886 : 0 : return s;
1887 : : }
1888 : :
1889 : : int
1890 : 0 : packet_inc_alive_timeouts(void)
1891 : : {
1892 : 0 : return ++active_state->keep_alive_timeouts;
1893 : : }
1894 : :
1895 : : void
1896 : 722 : packet_set_alive_timeouts(int ka)
1897 : : {
1898 : 722 : active_state->keep_alive_timeouts = ka;
1899 : 722 : }
1900 : :
1901 : : u_int
1902 : 67965 : packet_get_maxsize(void)
1903 : : {
1904 : 67965 : return active_state->max_packet_size;
1905 : : }
1906 : :
1907 : : /* roundup current message to pad bytes */
1908 : : void
1909 : 0 : packet_add_padding(u_char pad)
1910 : : {
1911 : 0 : active_state->extra_pad = pad;
1912 : 0 : }
1913 : :
1914 : : /*
1915 : : * 9.2. Ignored Data Message
1916 : : *
1917 : : * byte SSH_MSG_IGNORE
1918 : : * string data
1919 : : *
1920 : : * All implementations MUST understand (and ignore) this message at any
1921 : : * time (after receiving the protocol version). No implementation is
1922 : : * required to send them. This message can be used as an additional
1923 : : * protection measure against advanced traffic analysis techniques.
1924 : : */
1925 : : void
1926 : 0 : packet_send_ignore(int nbytes)
1927 : : {
1928 : 0 : u_int32_t rnd = 0;
1929 : : int i;
1930 : :
1931 [ # # ]: 0 : packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
1932 : 0 : packet_put_int(nbytes);
1933 [ # # ]: 0 : for (i = 0; i < nbytes; i++) {
1934 [ # # ]: 0 : if (i % 4 == 0)
1935 : 0 : rnd = arc4random();
1936 : 0 : packet_put_char((u_char)rnd & 0xff);
1937 : 0 : rnd >>= 8;
1938 : : }
1939 : 0 : }
1940 : :
1941 : : #define MAX_PACKETS (1U<<31)
1942 : : int
1943 : 36282 : packet_need_rekeying(void)
1944 : : {
1945 [ + - ]: 36282 : if (datafellows & SSH_BUG_NOREKEY)
1946 : : return 0;
1947 : 36282 : return
1948 [ + - ]: 36282 : (active_state->p_send.packets > MAX_PACKETS) ||
1949 [ + + ]: 36282 : (active_state->p_read.packets > MAX_PACKETS) ||
1950 [ + + ]: 31723 : (active_state->max_blocks_out &&
1951 [ + + ]: 35838 : (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1952 [ + - ]: 31279 : (active_state->max_blocks_in &&
1953 [ + - ][ + + ]: 72120 : (active_state->p_read.blocks > active_state->max_blocks_in)) ||
1954 [ + + ]: 341 : (active_state->rekey_interval != 0 && active_state->rekey_time +
1955 : 341 : active_state->rekey_interval <= monotime());
1956 : : }
1957 : :
1958 : : void
1959 : 83 : packet_set_rekey_limits(u_int32_t bytes, time_t seconds)
1960 : : {
1961 : 83 : debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
1962 : : (int)seconds);
1963 : 83 : active_state->rekey_limit = bytes;
1964 : 83 : active_state->rekey_interval = seconds;
1965 : : /*
1966 : : * We set the time here so that in post-auth privsep slave we count
1967 : : * from the completion of the authentication.
1968 : : */
1969 : 83 : active_state->rekey_time = monotime();
1970 : 83 : }
1971 : :
1972 : : time_t
1973 : 676 : packet_get_rekey_timeout(void)
1974 : : {
1975 : : time_t seconds;
1976 : :
1977 : 1352 : seconds = active_state->rekey_time + active_state->rekey_interval -
1978 : 676 : monotime();
1979 : 676 : return (seconds <= 0 ? 1 : seconds);
1980 : : }
1981 : :
1982 : : void
1983 : 1215 : packet_set_server(void)
1984 : : {
1985 : 1215 : active_state->server_side = 1;
1986 : 1215 : }
1987 : :
1988 : : void
1989 : 735 : packet_set_authenticated(void)
1990 : : {
1991 : 735 : active_state->after_authentication = 1;
1992 : 735 : }
1993 : :
1994 : : void *
1995 : 2231 : packet_get_input(void)
1996 : : {
1997 : 2231 : return (void *)&active_state->input;
1998 : : }
1999 : :
2000 : : void *
2001 : 2231 : packet_get_output(void)
2002 : : {
2003 : 2231 : return (void *)&active_state->output;
2004 : : }
2005 : :
2006 : : void *
2007 : 2632 : packet_get_newkeys(int mode)
2008 : : {
2009 : 2632 : return (void *)active_state->newkeys[mode];
2010 : : }
2011 : :
2012 : : /*
2013 : : * Save the state for the real connection, and use a separate state when
2014 : : * resuming a suspended connection.
2015 : : */
2016 : : void
2017 : 0 : packet_backup_state(void)
2018 : : {
2019 : : struct session_state *tmp;
2020 : :
2021 : 0 : close(active_state->connection_in);
2022 : 0 : active_state->connection_in = -1;
2023 : 0 : close(active_state->connection_out);
2024 : 0 : active_state->connection_out = -1;
2025 [ # # ]: 0 : if (backup_state)
2026 : : tmp = backup_state;
2027 : : else
2028 : 0 : tmp = alloc_session_state();
2029 : 0 : backup_state = active_state;
2030 : 0 : active_state = tmp;
2031 : 0 : }
2032 : :
2033 : : /*
2034 : : * Swap in the old state when resuming a connecion.
2035 : : */
2036 : : void
2037 : 0 : packet_restore_state(void)
2038 : : {
2039 : : struct session_state *tmp;
2040 : : void *buf;
2041 : : u_int len;
2042 : :
2043 : 0 : tmp = backup_state;
2044 : 0 : backup_state = active_state;
2045 : 0 : active_state = tmp;
2046 : 0 : active_state->connection_in = backup_state->connection_in;
2047 : 0 : backup_state->connection_in = -1;
2048 : 0 : active_state->connection_out = backup_state->connection_out;
2049 : 0 : backup_state->connection_out = -1;
2050 : 0 : len = buffer_len(&backup_state->input);
2051 [ # # ]: 0 : if (len > 0) {
2052 : 0 : buf = buffer_ptr(&backup_state->input);
2053 : 0 : buffer_append(&active_state->input, buf, len);
2054 : 0 : buffer_clear(&backup_state->input);
2055 : 0 : add_recv_bytes(len);
2056 : : }
2057 : 0 : }
|