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