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