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