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