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