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