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