Deleted Added
full compact
packet.c (116791) packet.c (124208)
1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * This file contains code implementing the packet protocol and communication
6 * with the other side. This same code is used both on client and server side.
7 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * SSH2 packet format added by Markus Friedl.
16 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include "includes.h"
1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * This file contains code implementing the packet protocol and communication
6 * with the other side. This same code is used both on client and server side.
7 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * SSH2 packet format added by Markus Friedl.
16 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include "includes.h"
40RCSID("$OpenBSD: packet.c,v 1.104 2003/04/01 10:22:21 markus Exp $");
40RCSID("$OpenBSD: packet.c,v 1.110 2003/09/19 09:02:02 markus Exp $");
41
41
42#include "openbsd-compat/sys-queue.h"
43
42#include "xmalloc.h"
43#include "buffer.h"
44#include "packet.h"
45#include "bufaux.h"
46#include "crc32.h"
47#include "getput.h"
48
49#include "compress.h"
50#include "deattack.h"
51#include "channels.h"
52
53#include "compat.h"
54#include "ssh1.h"
55#include "ssh2.h"
56
57#include "cipher.h"
58#include "kex.h"
59#include "mac.h"
60#include "log.h"
61#include "canohost.h"
62#include "misc.h"
63#include "ssh.h"
64
65#ifdef PACKET_DEBUG
66#define DBG(x) x
67#else
68#define DBG(x)
69#endif
70
71/*
72 * This variable contains the file descriptors used for communicating with
73 * the other side. connection_in is used for reading; connection_out for
74 * writing. These can be the same descriptor, in which case it is assumed to
75 * be a socket.
76 */
77static int connection_in = -1;
78static int connection_out = -1;
79
80/* Protocol flags for the remote side. */
81static u_int remote_protocol_flags = 0;
82
83/* Encryption context for receiving data. This is only used for decryption. */
84static CipherContext receive_context;
85
86/* Encryption context for sending data. This is only used for encryption. */
87static CipherContext send_context;
88
89/* Buffer for raw input data from the socket. */
90Buffer input;
91
92/* Buffer for raw output data going to the socket. */
93Buffer output;
94
95/* Buffer for the partial outgoing packet being constructed. */
96static Buffer outgoing_packet;
97
98/* Buffer for the incoming packet currently being processed. */
99static Buffer incoming_packet;
100
101/* Scratch buffer for packet compression/decompression. */
102static Buffer compression_buffer;
103static int compression_buffer_ready = 0;
104
105/* Flag indicating whether packet compression/decompression is enabled. */
106static int packet_compression = 0;
107
108/* default maximum packet size */
44#include "xmalloc.h"
45#include "buffer.h"
46#include "packet.h"
47#include "bufaux.h"
48#include "crc32.h"
49#include "getput.h"
50
51#include "compress.h"
52#include "deattack.h"
53#include "channels.h"
54
55#include "compat.h"
56#include "ssh1.h"
57#include "ssh2.h"
58
59#include "cipher.h"
60#include "kex.h"
61#include "mac.h"
62#include "log.h"
63#include "canohost.h"
64#include "misc.h"
65#include "ssh.h"
66
67#ifdef PACKET_DEBUG
68#define DBG(x) x
69#else
70#define DBG(x)
71#endif
72
73/*
74 * This variable contains the file descriptors used for communicating with
75 * the other side. connection_in is used for reading; connection_out for
76 * writing. These can be the same descriptor, in which case it is assumed to
77 * be a socket.
78 */
79static int connection_in = -1;
80static int connection_out = -1;
81
82/* Protocol flags for the remote side. */
83static u_int remote_protocol_flags = 0;
84
85/* Encryption context for receiving data. This is only used for decryption. */
86static CipherContext receive_context;
87
88/* Encryption context for sending data. This is only used for encryption. */
89static CipherContext send_context;
90
91/* Buffer for raw input data from the socket. */
92Buffer input;
93
94/* Buffer for raw output data going to the socket. */
95Buffer output;
96
97/* Buffer for the partial outgoing packet being constructed. */
98static Buffer outgoing_packet;
99
100/* Buffer for the incoming packet currently being processed. */
101static Buffer incoming_packet;
102
103/* Scratch buffer for packet compression/decompression. */
104static Buffer compression_buffer;
105static int compression_buffer_ready = 0;
106
107/* Flag indicating whether packet compression/decompression is enabled. */
108static int packet_compression = 0;
109
110/* default maximum packet size */
109int max_packet_size = 32768;
111u_int max_packet_size = 32768;
110
111/* Flag indicating whether this module has been initialized. */
112static int initialized = 0;
113
114/* Set to true if the connection is interactive. */
115static int interactive_mode = 0;
116
117/* Session key information for Encryption and MAC */
118Newkeys *newkeys[MODE_MAX];
112
113/* Flag indicating whether this module has been initialized. */
114static int initialized = 0;
115
116/* Set to true if the connection is interactive. */
117static int interactive_mode = 0;
118
119/* Session key information for Encryption and MAC */
120Newkeys *newkeys[MODE_MAX];
119static u_int32_t read_seqnr = 0;
120static u_int32_t send_seqnr = 0;
121static struct packet_state {
122 u_int32_t seqnr;
123 u_int32_t packets;
124 u_int64_t blocks;
125} p_read, p_send;
121
126
127static u_int64_t max_blocks_in, max_blocks_out;
128static u_int32_t rekey_limit;
129
122/* Session key for protocol v1 */
123static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
124static u_int ssh1_keylen;
125
126/* roundup current message to extra_pad bytes */
127static u_char extra_pad = 0;
128
130/* Session key for protocol v1 */
131static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
132static u_int ssh1_keylen;
133
134/* roundup current message to extra_pad bytes */
135static u_char extra_pad = 0;
136
137struct packet {
138 TAILQ_ENTRY(packet) next;
139 u_char type;
140 Buffer payload;
141};
142TAILQ_HEAD(, packet) outgoing;
143
129/*
130 * Sets the descriptors used for communication. Disables encryption until
131 * packet_set_encryption_key is called.
132 */
133void
134packet_set_connection(int fd_in, int fd_out)
135{
136 Cipher *none = cipher_by_name("none");
137
138 if (none == NULL)
139 fatal("packet_set_connection: cannot load cipher 'none'");
140 connection_in = fd_in;
141 connection_out = fd_out;
142 cipher_init(&send_context, none, "", 0, NULL, 0, CIPHER_ENCRYPT);
143 cipher_init(&receive_context, none, "", 0, NULL, 0, CIPHER_DECRYPT);
144 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
145 if (!initialized) {
146 initialized = 1;
147 buffer_init(&input);
148 buffer_init(&output);
149 buffer_init(&outgoing_packet);
150 buffer_init(&incoming_packet);
144/*
145 * Sets the descriptors used for communication. Disables encryption until
146 * packet_set_encryption_key is called.
147 */
148void
149packet_set_connection(int fd_in, int fd_out)
150{
151 Cipher *none = cipher_by_name("none");
152
153 if (none == NULL)
154 fatal("packet_set_connection: cannot load cipher 'none'");
155 connection_in = fd_in;
156 connection_out = fd_out;
157 cipher_init(&send_context, none, "", 0, NULL, 0, CIPHER_ENCRYPT);
158 cipher_init(&receive_context, none, "", 0, NULL, 0, CIPHER_DECRYPT);
159 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
160 if (!initialized) {
161 initialized = 1;
162 buffer_init(&input);
163 buffer_init(&output);
164 buffer_init(&outgoing_packet);
165 buffer_init(&incoming_packet);
166 TAILQ_INIT(&outgoing);
151 }
152 /* Kludge: arrange the close function to be called from fatal(). */
153 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
154}
155
156/* Returns 1 if remote host is connected via socket, 0 if not. */
157
158int
159packet_connection_is_on_socket(void)
160{
161 struct sockaddr_storage from, to;
162 socklen_t fromlen, tolen;
163
164 /* filedescriptors in and out are the same, so it's a socket */
165 if (connection_in == connection_out)
166 return 1;
167 fromlen = sizeof(from);
168 memset(&from, 0, sizeof(from));
169 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
170 return 0;
171 tolen = sizeof(to);
172 memset(&to, 0, sizeof(to));
173 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
174 return 0;
175 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
176 return 0;
177 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
178 return 0;
179 return 1;
180}
181
182/*
183 * Exports an IV from the CipherContext required to export the key
184 * state back from the unprivileged child to the privileged parent
185 * process.
186 */
187
188void
189packet_get_keyiv(int mode, u_char *iv, u_int len)
190{
191 CipherContext *cc;
192
193 if (mode == MODE_OUT)
194 cc = &send_context;
195 else
196 cc = &receive_context;
197
198 cipher_get_keyiv(cc, iv, len);
199}
200
201int
202packet_get_keycontext(int mode, u_char *dat)
203{
204 CipherContext *cc;
205
206 if (mode == MODE_OUT)
207 cc = &send_context;
208 else
209 cc = &receive_context;
210
211 return (cipher_get_keycontext(cc, dat));
212}
213
214void
215packet_set_keycontext(int mode, u_char *dat)
216{
217 CipherContext *cc;
218
219 if (mode == MODE_OUT)
220 cc = &send_context;
221 else
222 cc = &receive_context;
223
224 cipher_set_keycontext(cc, dat);
225}
226
227int
228packet_get_keyiv_len(int mode)
229{
230 CipherContext *cc;
231
232 if (mode == MODE_OUT)
233 cc = &send_context;
234 else
235 cc = &receive_context;
236
237 return (cipher_get_keyiv_len(cc));
238}
239void
240packet_set_iv(int mode, u_char *dat)
241{
242 CipherContext *cc;
243
244 if (mode == MODE_OUT)
245 cc = &send_context;
246 else
247 cc = &receive_context;
248
249 cipher_set_keyiv(cc, dat);
250}
251int
167 }
168 /* Kludge: arrange the close function to be called from fatal(). */
169 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
170}
171
172/* Returns 1 if remote host is connected via socket, 0 if not. */
173
174int
175packet_connection_is_on_socket(void)
176{
177 struct sockaddr_storage from, to;
178 socklen_t fromlen, tolen;
179
180 /* filedescriptors in and out are the same, so it's a socket */
181 if (connection_in == connection_out)
182 return 1;
183 fromlen = sizeof(from);
184 memset(&from, 0, sizeof(from));
185 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
186 return 0;
187 tolen = sizeof(to);
188 memset(&to, 0, sizeof(to));
189 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
190 return 0;
191 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
192 return 0;
193 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
194 return 0;
195 return 1;
196}
197
198/*
199 * Exports an IV from the CipherContext required to export the key
200 * state back from the unprivileged child to the privileged parent
201 * process.
202 */
203
204void
205packet_get_keyiv(int mode, u_char *iv, u_int len)
206{
207 CipherContext *cc;
208
209 if (mode == MODE_OUT)
210 cc = &send_context;
211 else
212 cc = &receive_context;
213
214 cipher_get_keyiv(cc, iv, len);
215}
216
217int
218packet_get_keycontext(int mode, u_char *dat)
219{
220 CipherContext *cc;
221
222 if (mode == MODE_OUT)
223 cc = &send_context;
224 else
225 cc = &receive_context;
226
227 return (cipher_get_keycontext(cc, dat));
228}
229
230void
231packet_set_keycontext(int mode, u_char *dat)
232{
233 CipherContext *cc;
234
235 if (mode == MODE_OUT)
236 cc = &send_context;
237 else
238 cc = &receive_context;
239
240 cipher_set_keycontext(cc, dat);
241}
242
243int
244packet_get_keyiv_len(int mode)
245{
246 CipherContext *cc;
247
248 if (mode == MODE_OUT)
249 cc = &send_context;
250 else
251 cc = &receive_context;
252
253 return (cipher_get_keyiv_len(cc));
254}
255void
256packet_set_iv(int mode, u_char *dat)
257{
258 CipherContext *cc;
259
260 if (mode == MODE_OUT)
261 cc = &send_context;
262 else
263 cc = &receive_context;
264
265 cipher_set_keyiv(cc, dat);
266}
267int
252packet_get_ssh1_cipher()
268packet_get_ssh1_cipher(void)
253{
254 return (cipher_get_number(receive_context.cipher));
255}
256
269{
270 return (cipher_get_number(receive_context.cipher));
271}
272
257
258u_int32_t
259packet_get_seqnr(int mode)
273void
274packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
260{
275{
261 return (mode == MODE_IN ? read_seqnr : send_seqnr);
276 struct packet_state *state;
277
278 state = (mode == MODE_IN) ? &p_read : &p_send;
279 *seqnr = state->seqnr;
280 *blocks = state->blocks;
281 *packets = state->packets;
262}
263
264void
282}
283
284void
265packet_set_seqnr(int mode, u_int32_t seqnr)
285packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
266{
286{
267 if (mode == MODE_IN)
268 read_seqnr = seqnr;
269 else if (mode == MODE_OUT)
270 send_seqnr = seqnr;
271 else
272 fatal("packet_set_seqnr: bad mode %d", mode);
287 struct packet_state *state;
288
289 state = (mode == MODE_IN) ? &p_read : &p_send;
290 state->seqnr = seqnr;
291 state->blocks = blocks;
292 state->packets = packets;
273}
274
275/* returns 1 if connection is via ipv4 */
276
277int
278packet_connection_is_ipv4(void)
279{
280 struct sockaddr_storage to;
281 socklen_t tolen = sizeof(to);
282
283 memset(&to, 0, sizeof(to));
284 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
285 return 0;
286 if (to.ss_family == AF_INET)
287 return 1;
288#ifdef IPV4_IN_IPV6
289 if (to.ss_family == AF_INET6 &&
290 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
291 return 1;
292#endif
293 return 0;
294}
295
296/* Sets the connection into non-blocking mode. */
297
298void
299packet_set_nonblocking(void)
300{
301 /* Set the socket into non-blocking mode. */
302 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
303 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
304
305 if (connection_out != connection_in) {
306 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
307 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
308 }
309}
310
311/* Returns the socket used for reading. */
312
313int
314packet_get_connection_in(void)
315{
316 return connection_in;
317}
318
319/* Returns the descriptor used for writing. */
320
321int
322packet_get_connection_out(void)
323{
324 return connection_out;
325}
326
327/* Closes the connection and clears and frees internal data structures. */
328
329void
330packet_close(void)
331{
332 if (!initialized)
333 return;
334 initialized = 0;
335 if (connection_in == connection_out) {
336 shutdown(connection_out, SHUT_RDWR);
337 close(connection_out);
338 } else {
339 close(connection_in);
340 close(connection_out);
341 }
342 buffer_free(&input);
343 buffer_free(&output);
344 buffer_free(&outgoing_packet);
345 buffer_free(&incoming_packet);
346 if (compression_buffer_ready) {
347 buffer_free(&compression_buffer);
348 buffer_compress_uninit();
349 }
350 cipher_cleanup(&send_context);
351 cipher_cleanup(&receive_context);
352}
353
354/* Sets remote side protocol flags. */
355
356void
357packet_set_protocol_flags(u_int protocol_flags)
358{
359 remote_protocol_flags = protocol_flags;
360}
361
362/* Returns the remote protocol flags set earlier by the above function. */
363
364u_int
365packet_get_protocol_flags(void)
366{
367 return remote_protocol_flags;
368}
369
370/*
371 * Starts packet compression from the next packet on in both directions.
372 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
373 */
374
375static void
376packet_init_compression(void)
377{
378 if (compression_buffer_ready == 1)
379 return;
380 compression_buffer_ready = 1;
381 buffer_init(&compression_buffer);
382}
383
384void
385packet_start_compression(int level)
386{
387 if (packet_compression && !compat20)
388 fatal("Compression already enabled.");
389 packet_compression = 1;
390 packet_init_compression();
391 buffer_compress_init_send(level);
392 buffer_compress_init_recv();
393}
394
395/*
396 * Causes any further packets to be encrypted using the given key. The same
397 * key is used for both sending and reception. However, both directions are
398 * encrypted independently of each other.
399 */
400
401void
402packet_set_encryption_key(const u_char *key, u_int keylen,
403 int number)
404{
405 Cipher *cipher = cipher_by_number(number);
406
407 if (cipher == NULL)
408 fatal("packet_set_encryption_key: unknown cipher number %d", number);
409 if (keylen < 20)
410 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
411 if (keylen > SSH_SESSION_KEY_LENGTH)
412 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
413 memcpy(ssh1_key, key, keylen);
414 ssh1_keylen = keylen;
415 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
416 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
417}
418
419u_int
420packet_get_encryption_key(u_char *key)
421{
422 if (key == NULL)
423 return (ssh1_keylen);
424 memcpy(key, ssh1_key, ssh1_keylen);
425 return (ssh1_keylen);
426}
427
428/* Start constructing a packet to send. */
429void
430packet_start(u_char type)
431{
432 u_char buf[9];
433 int len;
434
435 DBG(debug("packet_start[%d]", type));
436 len = compat20 ? 6 : 9;
437 memset(buf, 0, len - 1);
438 buf[len - 1] = type;
439 buffer_clear(&outgoing_packet);
440 buffer_append(&outgoing_packet, buf, len);
441}
442
443/* Append payload. */
444void
445packet_put_char(int value)
446{
447 char ch = value;
448
449 buffer_append(&outgoing_packet, &ch, 1);
450}
451void
452packet_put_int(u_int value)
453{
454 buffer_put_int(&outgoing_packet, value);
455}
456void
457packet_put_string(const void *buf, u_int len)
458{
459 buffer_put_string(&outgoing_packet, buf, len);
460}
461void
462packet_put_cstring(const char *str)
463{
464 buffer_put_cstring(&outgoing_packet, str);
465}
466void
467packet_put_raw(const void *buf, u_int len)
468{
469 buffer_append(&outgoing_packet, buf, len);
470}
471void
472packet_put_bignum(BIGNUM * value)
473{
474 buffer_put_bignum(&outgoing_packet, value);
475}
476void
477packet_put_bignum2(BIGNUM * value)
478{
479 buffer_put_bignum2(&outgoing_packet, value);
480}
481
482/*
483 * Finalizes and sends the packet. If the encryption key has been set,
484 * encrypts the packet before sending.
485 */
486
487static void
488packet_send1(void)
489{
490 u_char buf[8], *cp;
491 int i, padding, len;
492 u_int checksum;
493 u_int32_t rand = 0;
494
495 /*
496 * If using packet compression, compress the payload of the outgoing
497 * packet.
498 */
499 if (packet_compression) {
500 buffer_clear(&compression_buffer);
501 /* Skip padding. */
502 buffer_consume(&outgoing_packet, 8);
503 /* padding */
504 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
505 buffer_compress(&outgoing_packet, &compression_buffer);
506 buffer_clear(&outgoing_packet);
507 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
508 buffer_len(&compression_buffer));
509 }
510 /* Compute packet length without padding (add checksum, remove padding). */
511 len = buffer_len(&outgoing_packet) + 4 - 8;
512
513 /* Insert padding. Initialized to zero in packet_start1() */
514 padding = 8 - len % 8;
515 if (!send_context.plaintext) {
516 cp = buffer_ptr(&outgoing_packet);
517 for (i = 0; i < padding; i++) {
518 if (i % 4 == 0)
519 rand = arc4random();
520 cp[7 - i] = rand & 0xff;
521 rand >>= 8;
522 }
523 }
524 buffer_consume(&outgoing_packet, 8 - padding);
525
526 /* Add check bytes. */
527 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
528 buffer_len(&outgoing_packet));
529 PUT_32BIT(buf, checksum);
530 buffer_append(&outgoing_packet, buf, 4);
531
532#ifdef PACKET_DEBUG
533 fprintf(stderr, "packet_send plain: ");
534 buffer_dump(&outgoing_packet);
535#endif
536
537 /* Append to output. */
538 PUT_32BIT(buf, len);
539 buffer_append(&output, buf, 4);
540 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
541 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
542 buffer_len(&outgoing_packet));
543
544#ifdef PACKET_DEBUG
545 fprintf(stderr, "encrypted: ");
546 buffer_dump(&output);
547#endif
548
549 buffer_clear(&outgoing_packet);
550
551 /*
552 * Note that the packet is now only buffered in output. It won\'t be
553 * actually sent until packet_write_wait or packet_write_poll is
554 * called.
555 */
556}
557
558void
559set_newkeys(int mode)
560{
561 Enc *enc;
562 Mac *mac;
563 Comp *comp;
564 CipherContext *cc;
293}
294
295/* returns 1 if connection is via ipv4 */
296
297int
298packet_connection_is_ipv4(void)
299{
300 struct sockaddr_storage to;
301 socklen_t tolen = sizeof(to);
302
303 memset(&to, 0, sizeof(to));
304 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
305 return 0;
306 if (to.ss_family == AF_INET)
307 return 1;
308#ifdef IPV4_IN_IPV6
309 if (to.ss_family == AF_INET6 &&
310 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
311 return 1;
312#endif
313 return 0;
314}
315
316/* Sets the connection into non-blocking mode. */
317
318void
319packet_set_nonblocking(void)
320{
321 /* Set the socket into non-blocking mode. */
322 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
323 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
324
325 if (connection_out != connection_in) {
326 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
327 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
328 }
329}
330
331/* Returns the socket used for reading. */
332
333int
334packet_get_connection_in(void)
335{
336 return connection_in;
337}
338
339/* Returns the descriptor used for writing. */
340
341int
342packet_get_connection_out(void)
343{
344 return connection_out;
345}
346
347/* Closes the connection and clears and frees internal data structures. */
348
349void
350packet_close(void)
351{
352 if (!initialized)
353 return;
354 initialized = 0;
355 if (connection_in == connection_out) {
356 shutdown(connection_out, SHUT_RDWR);
357 close(connection_out);
358 } else {
359 close(connection_in);
360 close(connection_out);
361 }
362 buffer_free(&input);
363 buffer_free(&output);
364 buffer_free(&outgoing_packet);
365 buffer_free(&incoming_packet);
366 if (compression_buffer_ready) {
367 buffer_free(&compression_buffer);
368 buffer_compress_uninit();
369 }
370 cipher_cleanup(&send_context);
371 cipher_cleanup(&receive_context);
372}
373
374/* Sets remote side protocol flags. */
375
376void
377packet_set_protocol_flags(u_int protocol_flags)
378{
379 remote_protocol_flags = protocol_flags;
380}
381
382/* Returns the remote protocol flags set earlier by the above function. */
383
384u_int
385packet_get_protocol_flags(void)
386{
387 return remote_protocol_flags;
388}
389
390/*
391 * Starts packet compression from the next packet on in both directions.
392 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
393 */
394
395static void
396packet_init_compression(void)
397{
398 if (compression_buffer_ready == 1)
399 return;
400 compression_buffer_ready = 1;
401 buffer_init(&compression_buffer);
402}
403
404void
405packet_start_compression(int level)
406{
407 if (packet_compression && !compat20)
408 fatal("Compression already enabled.");
409 packet_compression = 1;
410 packet_init_compression();
411 buffer_compress_init_send(level);
412 buffer_compress_init_recv();
413}
414
415/*
416 * Causes any further packets to be encrypted using the given key. The same
417 * key is used for both sending and reception. However, both directions are
418 * encrypted independently of each other.
419 */
420
421void
422packet_set_encryption_key(const u_char *key, u_int keylen,
423 int number)
424{
425 Cipher *cipher = cipher_by_number(number);
426
427 if (cipher == NULL)
428 fatal("packet_set_encryption_key: unknown cipher number %d", number);
429 if (keylen < 20)
430 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
431 if (keylen > SSH_SESSION_KEY_LENGTH)
432 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
433 memcpy(ssh1_key, key, keylen);
434 ssh1_keylen = keylen;
435 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
436 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
437}
438
439u_int
440packet_get_encryption_key(u_char *key)
441{
442 if (key == NULL)
443 return (ssh1_keylen);
444 memcpy(key, ssh1_key, ssh1_keylen);
445 return (ssh1_keylen);
446}
447
448/* Start constructing a packet to send. */
449void
450packet_start(u_char type)
451{
452 u_char buf[9];
453 int len;
454
455 DBG(debug("packet_start[%d]", type));
456 len = compat20 ? 6 : 9;
457 memset(buf, 0, len - 1);
458 buf[len - 1] = type;
459 buffer_clear(&outgoing_packet);
460 buffer_append(&outgoing_packet, buf, len);
461}
462
463/* Append payload. */
464void
465packet_put_char(int value)
466{
467 char ch = value;
468
469 buffer_append(&outgoing_packet, &ch, 1);
470}
471void
472packet_put_int(u_int value)
473{
474 buffer_put_int(&outgoing_packet, value);
475}
476void
477packet_put_string(const void *buf, u_int len)
478{
479 buffer_put_string(&outgoing_packet, buf, len);
480}
481void
482packet_put_cstring(const char *str)
483{
484 buffer_put_cstring(&outgoing_packet, str);
485}
486void
487packet_put_raw(const void *buf, u_int len)
488{
489 buffer_append(&outgoing_packet, buf, len);
490}
491void
492packet_put_bignum(BIGNUM * value)
493{
494 buffer_put_bignum(&outgoing_packet, value);
495}
496void
497packet_put_bignum2(BIGNUM * value)
498{
499 buffer_put_bignum2(&outgoing_packet, value);
500}
501
502/*
503 * Finalizes and sends the packet. If the encryption key has been set,
504 * encrypts the packet before sending.
505 */
506
507static void
508packet_send1(void)
509{
510 u_char buf[8], *cp;
511 int i, padding, len;
512 u_int checksum;
513 u_int32_t rand = 0;
514
515 /*
516 * If using packet compression, compress the payload of the outgoing
517 * packet.
518 */
519 if (packet_compression) {
520 buffer_clear(&compression_buffer);
521 /* Skip padding. */
522 buffer_consume(&outgoing_packet, 8);
523 /* padding */
524 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
525 buffer_compress(&outgoing_packet, &compression_buffer);
526 buffer_clear(&outgoing_packet);
527 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
528 buffer_len(&compression_buffer));
529 }
530 /* Compute packet length without padding (add checksum, remove padding). */
531 len = buffer_len(&outgoing_packet) + 4 - 8;
532
533 /* Insert padding. Initialized to zero in packet_start1() */
534 padding = 8 - len % 8;
535 if (!send_context.plaintext) {
536 cp = buffer_ptr(&outgoing_packet);
537 for (i = 0; i < padding; i++) {
538 if (i % 4 == 0)
539 rand = arc4random();
540 cp[7 - i] = rand & 0xff;
541 rand >>= 8;
542 }
543 }
544 buffer_consume(&outgoing_packet, 8 - padding);
545
546 /* Add check bytes. */
547 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
548 buffer_len(&outgoing_packet));
549 PUT_32BIT(buf, checksum);
550 buffer_append(&outgoing_packet, buf, 4);
551
552#ifdef PACKET_DEBUG
553 fprintf(stderr, "packet_send plain: ");
554 buffer_dump(&outgoing_packet);
555#endif
556
557 /* Append to output. */
558 PUT_32BIT(buf, len);
559 buffer_append(&output, buf, 4);
560 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
561 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
562 buffer_len(&outgoing_packet));
563
564#ifdef PACKET_DEBUG
565 fprintf(stderr, "encrypted: ");
566 buffer_dump(&output);
567#endif
568
569 buffer_clear(&outgoing_packet);
570
571 /*
572 * Note that the packet is now only buffered in output. It won\'t be
573 * actually sent until packet_write_wait or packet_write_poll is
574 * called.
575 */
576}
577
578void
579set_newkeys(int mode)
580{
581 Enc *enc;
582 Mac *mac;
583 Comp *comp;
584 CipherContext *cc;
585 u_int64_t *max_blocks;
565 int encrypt;
566
567 debug2("set_newkeys: mode %d", mode);
568
569 if (mode == MODE_OUT) {
570 cc = &send_context;
571 encrypt = CIPHER_ENCRYPT;
586 int encrypt;
587
588 debug2("set_newkeys: mode %d", mode);
589
590 if (mode == MODE_OUT) {
591 cc = &send_context;
592 encrypt = CIPHER_ENCRYPT;
593 p_send.packets = p_send.blocks = 0;
594 max_blocks = &max_blocks_out;
572 } else {
573 cc = &receive_context;
574 encrypt = CIPHER_DECRYPT;
595 } else {
596 cc = &receive_context;
597 encrypt = CIPHER_DECRYPT;
598 p_read.packets = p_read.blocks = 0;
599 max_blocks = &max_blocks_in;
575 }
576 if (newkeys[mode] != NULL) {
577 debug("set_newkeys: rekeying");
578 cipher_cleanup(cc);
579 enc = &newkeys[mode]->enc;
580 mac = &newkeys[mode]->mac;
581 comp = &newkeys[mode]->comp;
582 memset(mac->key, 0, mac->key_len);
583 xfree(enc->name);
584 xfree(enc->iv);
585 xfree(enc->key);
586 xfree(mac->name);
587 xfree(mac->key);
588 xfree(comp->name);
589 xfree(newkeys[mode]);
590 }
591 newkeys[mode] = kex_get_newkeys(mode);
592 if (newkeys[mode] == NULL)
593 fatal("newkeys: no keys for mode %d", mode);
594 enc = &newkeys[mode]->enc;
595 mac = &newkeys[mode]->mac;
596 comp = &newkeys[mode]->comp;
597 if (mac->md != NULL)
598 mac->enabled = 1;
599 DBG(debug("cipher_init_context: %d", mode));
600 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
601 enc->iv, enc->block_size, encrypt);
602 /* Deleting the keys does not gain extra security */
603 /* memset(enc->iv, 0, enc->block_size);
604 memset(enc->key, 0, enc->key_len); */
605 if (comp->type != 0 && comp->enabled == 0) {
606 packet_init_compression();
607 if (mode == MODE_OUT)
608 buffer_compress_init_send(6);
609 else
610 buffer_compress_init_recv();
611 comp->enabled = 1;
612 }
600 }
601 if (newkeys[mode] != NULL) {
602 debug("set_newkeys: rekeying");
603 cipher_cleanup(cc);
604 enc = &newkeys[mode]->enc;
605 mac = &newkeys[mode]->mac;
606 comp = &newkeys[mode]->comp;
607 memset(mac->key, 0, mac->key_len);
608 xfree(enc->name);
609 xfree(enc->iv);
610 xfree(enc->key);
611 xfree(mac->name);
612 xfree(mac->key);
613 xfree(comp->name);
614 xfree(newkeys[mode]);
615 }
616 newkeys[mode] = kex_get_newkeys(mode);
617 if (newkeys[mode] == NULL)
618 fatal("newkeys: no keys for mode %d", mode);
619 enc = &newkeys[mode]->enc;
620 mac = &newkeys[mode]->mac;
621 comp = &newkeys[mode]->comp;
622 if (mac->md != NULL)
623 mac->enabled = 1;
624 DBG(debug("cipher_init_context: %d", mode));
625 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
626 enc->iv, enc->block_size, encrypt);
627 /* Deleting the keys does not gain extra security */
628 /* memset(enc->iv, 0, enc->block_size);
629 memset(enc->key, 0, enc->key_len); */
630 if (comp->type != 0 && comp->enabled == 0) {
631 packet_init_compression();
632 if (mode == MODE_OUT)
633 buffer_compress_init_send(6);
634 else
635 buffer_compress_init_recv();
636 comp->enabled = 1;
637 }
638 /*
639 * The 2^(blocksize*2) limit is too expensive for 3DES,
640 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
641 */
642 if (enc->block_size >= 16)
643 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
644 else
645 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
646 if (rekey_limit)
647 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
613}
614
615/*
616 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
617 */
618static void
648}
649
650/*
651 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
652 */
653static void
619packet_send2(void)
654packet_send2_wrapped(void)
620{
621 u_char type, *cp, *macbuf = NULL;
622 u_char padlen, pad;
623 u_int packet_length = 0;
624 u_int i, len;
625 u_int32_t rand = 0;
626 Enc *enc = NULL;
627 Mac *mac = NULL;
628 Comp *comp = NULL;
629 int block_size;
630
631 if (newkeys[MODE_OUT] != NULL) {
632 enc = &newkeys[MODE_OUT]->enc;
633 mac = &newkeys[MODE_OUT]->mac;
634 comp = &newkeys[MODE_OUT]->comp;
635 }
636 block_size = enc ? enc->block_size : 8;
637
638 cp = buffer_ptr(&outgoing_packet);
639 type = cp[5];
640
641#ifdef PACKET_DEBUG
642 fprintf(stderr, "plain: ");
643 buffer_dump(&outgoing_packet);
644#endif
645
646 if (comp && comp->enabled) {
647 len = buffer_len(&outgoing_packet);
648 /* skip header, compress only payload */
649 buffer_consume(&outgoing_packet, 5);
650 buffer_clear(&compression_buffer);
651 buffer_compress(&outgoing_packet, &compression_buffer);
652 buffer_clear(&outgoing_packet);
653 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
654 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
655 buffer_len(&compression_buffer));
656 DBG(debug("compression: raw %d compressed %d", len,
657 buffer_len(&outgoing_packet)));
658 }
659
660 /* sizeof (packet_len + pad_len + payload) */
661 len = buffer_len(&outgoing_packet);
662
663 /*
664 * calc size of padding, alloc space, get random data,
665 * minimum padding is 4 bytes
666 */
667 padlen = block_size - (len % block_size);
668 if (padlen < 4)
669 padlen += block_size;
670 if (extra_pad) {
671 /* will wrap if extra_pad+padlen > 255 */
672 extra_pad = roundup(extra_pad, block_size);
673 pad = extra_pad - ((len + padlen) % extra_pad);
674 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
675 pad, len, padlen, extra_pad);
676 padlen += pad;
677 extra_pad = 0;
678 }
679 cp = buffer_append_space(&outgoing_packet, padlen);
680 if (enc && !send_context.plaintext) {
681 /* random padding */
682 for (i = 0; i < padlen; i++) {
683 if (i % 4 == 0)
684 rand = arc4random();
685 cp[i] = rand & 0xff;
686 rand >>= 8;
687 }
688 } else {
689 /* clear padding */
690 memset(cp, 0, padlen);
691 }
692 /* packet_length includes payload, padding and padding length field */
693 packet_length = buffer_len(&outgoing_packet) - 4;
694 cp = buffer_ptr(&outgoing_packet);
695 PUT_32BIT(cp, packet_length);
696 cp[4] = padlen;
697 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
698
699 /* compute MAC over seqnr and packet(length fields, payload, padding) */
700 if (mac && mac->enabled) {
655{
656 u_char type, *cp, *macbuf = NULL;
657 u_char padlen, pad;
658 u_int packet_length = 0;
659 u_int i, len;
660 u_int32_t rand = 0;
661 Enc *enc = NULL;
662 Mac *mac = NULL;
663 Comp *comp = NULL;
664 int block_size;
665
666 if (newkeys[MODE_OUT] != NULL) {
667 enc = &newkeys[MODE_OUT]->enc;
668 mac = &newkeys[MODE_OUT]->mac;
669 comp = &newkeys[MODE_OUT]->comp;
670 }
671 block_size = enc ? enc->block_size : 8;
672
673 cp = buffer_ptr(&outgoing_packet);
674 type = cp[5];
675
676#ifdef PACKET_DEBUG
677 fprintf(stderr, "plain: ");
678 buffer_dump(&outgoing_packet);
679#endif
680
681 if (comp && comp->enabled) {
682 len = buffer_len(&outgoing_packet);
683 /* skip header, compress only payload */
684 buffer_consume(&outgoing_packet, 5);
685 buffer_clear(&compression_buffer);
686 buffer_compress(&outgoing_packet, &compression_buffer);
687 buffer_clear(&outgoing_packet);
688 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
689 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
690 buffer_len(&compression_buffer));
691 DBG(debug("compression: raw %d compressed %d", len,
692 buffer_len(&outgoing_packet)));
693 }
694
695 /* sizeof (packet_len + pad_len + payload) */
696 len = buffer_len(&outgoing_packet);
697
698 /*
699 * calc size of padding, alloc space, get random data,
700 * minimum padding is 4 bytes
701 */
702 padlen = block_size - (len % block_size);
703 if (padlen < 4)
704 padlen += block_size;
705 if (extra_pad) {
706 /* will wrap if extra_pad+padlen > 255 */
707 extra_pad = roundup(extra_pad, block_size);
708 pad = extra_pad - ((len + padlen) % extra_pad);
709 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
710 pad, len, padlen, extra_pad);
711 padlen += pad;
712 extra_pad = 0;
713 }
714 cp = buffer_append_space(&outgoing_packet, padlen);
715 if (enc && !send_context.plaintext) {
716 /* random padding */
717 for (i = 0; i < padlen; i++) {
718 if (i % 4 == 0)
719 rand = arc4random();
720 cp[i] = rand & 0xff;
721 rand >>= 8;
722 }
723 } else {
724 /* clear padding */
725 memset(cp, 0, padlen);
726 }
727 /* packet_length includes payload, padding and padding length field */
728 packet_length = buffer_len(&outgoing_packet) - 4;
729 cp = buffer_ptr(&outgoing_packet);
730 PUT_32BIT(cp, packet_length);
731 cp[4] = padlen;
732 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
733
734 /* compute MAC over seqnr and packet(length fields, payload, padding) */
735 if (mac && mac->enabled) {
701 macbuf = mac_compute(mac, send_seqnr,
736 macbuf = mac_compute(mac, p_send.seqnr,
702 buffer_ptr(&outgoing_packet),
703 buffer_len(&outgoing_packet));
737 buffer_ptr(&outgoing_packet),
738 buffer_len(&outgoing_packet));
704 DBG(debug("done calc MAC out #%d", send_seqnr));
739 DBG(debug("done calc MAC out #%d", p_send.seqnr));
705 }
706 /* encrypt packet and append to output buffer. */
707 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
708 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
709 buffer_len(&outgoing_packet));
710 /* append unencrypted MAC */
711 if (mac && mac->enabled)
712 buffer_append(&output, (char *)macbuf, mac->mac_len);
713#ifdef PACKET_DEBUG
714 fprintf(stderr, "encrypted: ");
715 buffer_dump(&output);
716#endif
717 /* increment sequence number for outgoing packets */
740 }
741 /* encrypt packet and append to output buffer. */
742 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
743 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
744 buffer_len(&outgoing_packet));
745 /* append unencrypted MAC */
746 if (mac && mac->enabled)
747 buffer_append(&output, (char *)macbuf, mac->mac_len);
748#ifdef PACKET_DEBUG
749 fprintf(stderr, "encrypted: ");
750 buffer_dump(&output);
751#endif
752 /* increment sequence number for outgoing packets */
718 if (++send_seqnr == 0)
719 log("outgoing seqnr wraps around");
753 if (++p_send.seqnr == 0)
754 logit("outgoing seqnr wraps around");
755 if (++p_send.packets == 0)
756 if (!(datafellows & SSH_BUG_NOREKEY))
757 fatal("XXX too many packets with same key");
758 p_send.blocks += (packet_length + 4) / block_size;
720 buffer_clear(&outgoing_packet);
721
722 if (type == SSH2_MSG_NEWKEYS)
723 set_newkeys(MODE_OUT);
724}
725
759 buffer_clear(&outgoing_packet);
760
761 if (type == SSH2_MSG_NEWKEYS)
762 set_newkeys(MODE_OUT);
763}
764
765static void
766packet_send2(void)
767{
768 static int rekeying = 0;
769 struct packet *p;
770 u_char type, *cp;
771
772 cp = buffer_ptr(&outgoing_packet);
773 type = cp[5];
774
775 /* during rekeying we can only send key exchange messages */
776 if (rekeying) {
777 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
778 (type <= SSH2_MSG_TRANSPORT_MAX))) {
779 debug("enqueue packet: %u", type);
780 p = xmalloc(sizeof(*p));
781 p->type = type;
782 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
783 buffer_init(&outgoing_packet);
784 TAILQ_INSERT_TAIL(&outgoing, p, next);
785 return;
786 }
787 }
788
789 /* rekeying starts with sending KEXINIT */
790 if (type == SSH2_MSG_KEXINIT)
791 rekeying = 1;
792
793 packet_send2_wrapped();
794
795 /* after a NEWKEYS message we can send the complete queue */
796 if (type == SSH2_MSG_NEWKEYS) {
797 rekeying = 0;
798 while ((p = TAILQ_FIRST(&outgoing))) {
799 type = p->type;
800 debug("dequeue packet: %u", type);
801 buffer_free(&outgoing_packet);
802 memcpy(&outgoing_packet, &p->payload,
803 sizeof(Buffer));
804 TAILQ_REMOVE(&outgoing, p, next);
805 xfree(p);
806 packet_send2_wrapped();
807 }
808 }
809}
810
726void
727packet_send(void)
728{
729 if (compat20)
730 packet_send2();
731 else
732 packet_send1();
733 DBG(debug("packet_send done"));
734}
735
736/*
737 * Waits until a packet has been received, and returns its type. Note that
738 * no other data is processed until this returns, so this function should not
739 * be used during the interactive session.
740 */
741
742int
743packet_read_seqnr(u_int32_t *seqnr_p)
744{
745 int type, len;
746 fd_set *setp;
747 char buf[8192];
748 DBG(debug("packet_read()"));
749
750 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
751 sizeof(fd_mask));
752
753 /* Since we are blocking, ensure that all written packets have been sent. */
754 packet_write_wait();
755
756 /* Stay in the loop until we have received a complete packet. */
757 for (;;) {
758 /* Try to read a packet from the buffer. */
759 type = packet_read_poll_seqnr(seqnr_p);
760 if (!compat20 && (
761 type == SSH_SMSG_SUCCESS
762 || type == SSH_SMSG_FAILURE
763 || type == SSH_CMSG_EOF
764 || type == SSH_CMSG_EXIT_CONFIRMATION))
765 packet_check_eom();
766 /* If we got a packet, return it. */
767 if (type != SSH_MSG_NONE) {
768 xfree(setp);
769 return type;
770 }
771 /*
772 * Otherwise, wait for some data to arrive, add it to the
773 * buffer, and try again.
774 */
775 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
776 sizeof(fd_mask));
777 FD_SET(connection_in, setp);
778
779 /* Wait for some data to arrive. */
780 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
781 (errno == EAGAIN || errno == EINTR))
782 ;
783
784 /* Read data from the socket. */
785 len = read(connection_in, buf, sizeof(buf));
786 if (len == 0) {
811void
812packet_send(void)
813{
814 if (compat20)
815 packet_send2();
816 else
817 packet_send1();
818 DBG(debug("packet_send done"));
819}
820
821/*
822 * Waits until a packet has been received, and returns its type. Note that
823 * no other data is processed until this returns, so this function should not
824 * be used during the interactive session.
825 */
826
827int
828packet_read_seqnr(u_int32_t *seqnr_p)
829{
830 int type, len;
831 fd_set *setp;
832 char buf[8192];
833 DBG(debug("packet_read()"));
834
835 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
836 sizeof(fd_mask));
837
838 /* Since we are blocking, ensure that all written packets have been sent. */
839 packet_write_wait();
840
841 /* Stay in the loop until we have received a complete packet. */
842 for (;;) {
843 /* Try to read a packet from the buffer. */
844 type = packet_read_poll_seqnr(seqnr_p);
845 if (!compat20 && (
846 type == SSH_SMSG_SUCCESS
847 || type == SSH_SMSG_FAILURE
848 || type == SSH_CMSG_EOF
849 || type == SSH_CMSG_EXIT_CONFIRMATION))
850 packet_check_eom();
851 /* If we got a packet, return it. */
852 if (type != SSH_MSG_NONE) {
853 xfree(setp);
854 return type;
855 }
856 /*
857 * Otherwise, wait for some data to arrive, add it to the
858 * buffer, and try again.
859 */
860 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
861 sizeof(fd_mask));
862 FD_SET(connection_in, setp);
863
864 /* Wait for some data to arrive. */
865 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
866 (errno == EAGAIN || errno == EINTR))
867 ;
868
869 /* Read data from the socket. */
870 len = read(connection_in, buf, sizeof(buf));
871 if (len == 0) {
787 log("Connection closed by %.200s", get_remote_ipaddr());
872 logit("Connection closed by %.200s", get_remote_ipaddr());
788 fatal_cleanup();
789 }
790 if (len < 0)
791 fatal("Read from socket failed: %.100s", strerror(errno));
792 /* Append it to the buffer. */
793 packet_process_incoming(buf, len);
794 }
795 /* NOTREACHED */
796}
797
798int
799packet_read(void)
800{
801 return packet_read_seqnr(NULL);
802}
803
804/*
805 * Waits until a packet has been received, verifies that its type matches
806 * that given, and gives a fatal error and exits if there is a mismatch.
807 */
808
809void
810packet_read_expect(int expected_type)
811{
812 int type;
813
814 type = packet_read();
815 if (type != expected_type)
816 packet_disconnect("Protocol error: expected packet type %d, got %d",
817 expected_type, type);
818}
819
820/* Checks if a full packet is available in the data received so far via
821 * packet_process_incoming. If so, reads the packet; otherwise returns
822 * SSH_MSG_NONE. This does not wait for data from the connection.
823 *
824 * SSH_MSG_DISCONNECT is handled specially here. Also,
825 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
826 * to higher levels.
827 */
828
829static int
830packet_read_poll1(void)
831{
832 u_int len, padded_len;
833 u_char *cp, type;
834 u_int checksum, stored_checksum;
835
836 /* Check if input size is less than minimum packet size. */
837 if (buffer_len(&input) < 4 + 8)
838 return SSH_MSG_NONE;
839 /* Get length of incoming packet. */
840 cp = buffer_ptr(&input);
841 len = GET_32BIT(cp);
842 if (len < 1 + 2 + 2 || len > 256 * 1024)
843 packet_disconnect("Bad packet length %u.", len);
844 padded_len = (len + 8) & ~7;
845
846 /* Check if the packet has been entirely received. */
847 if (buffer_len(&input) < 4 + padded_len)
848 return SSH_MSG_NONE;
849
850 /* The entire packet is in buffer. */
851
852 /* Consume packet length. */
853 buffer_consume(&input, 4);
854
855 /*
856 * Cryptographic attack detector for ssh
857 * (C)1998 CORE-SDI, Buenos Aires Argentina
858 * Ariel Futoransky(futo@core-sdi.com)
859 */
860 if (!receive_context.plaintext &&
861 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
862 packet_disconnect("crc32 compensation attack: network attack detected");
863
864 /* Decrypt data to incoming_packet. */
865 buffer_clear(&incoming_packet);
866 cp = buffer_append_space(&incoming_packet, padded_len);
867 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
868
869 buffer_consume(&input, padded_len);
870
871#ifdef PACKET_DEBUG
872 fprintf(stderr, "read_poll plain: ");
873 buffer_dump(&incoming_packet);
874#endif
875
876 /* Compute packet checksum. */
877 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
878 buffer_len(&incoming_packet) - 4);
879
880 /* Skip padding. */
881 buffer_consume(&incoming_packet, 8 - len % 8);
882
883 /* Test check bytes. */
884 if (len != buffer_len(&incoming_packet))
885 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
886 len, buffer_len(&incoming_packet));
887
888 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
889 stored_checksum = GET_32BIT(cp);
890 if (checksum != stored_checksum)
891 packet_disconnect("Corrupted check bytes on input.");
892 buffer_consume_end(&incoming_packet, 4);
893
894 if (packet_compression) {
895 buffer_clear(&compression_buffer);
896 buffer_uncompress(&incoming_packet, &compression_buffer);
897 buffer_clear(&incoming_packet);
898 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
899 buffer_len(&compression_buffer));
900 }
901 type = buffer_get_char(&incoming_packet);
902 return type;
903}
904
905static int
906packet_read_poll2(u_int32_t *seqnr_p)
907{
908 static u_int packet_length = 0;
909 u_int padlen, need;
910 u_char *macbuf, *cp, type;
911 int maclen, block_size;
912 Enc *enc = NULL;
913 Mac *mac = NULL;
914 Comp *comp = NULL;
915
916 if (newkeys[MODE_IN] != NULL) {
917 enc = &newkeys[MODE_IN]->enc;
918 mac = &newkeys[MODE_IN]->mac;
919 comp = &newkeys[MODE_IN]->comp;
920 }
921 maclen = mac && mac->enabled ? mac->mac_len : 0;
922 block_size = enc ? enc->block_size : 8;
923
924 if (packet_length == 0) {
925 /*
926 * check if input size is less than the cipher block size,
927 * decrypt first block and extract length of incoming packet
928 */
929 if (buffer_len(&input) < block_size)
930 return SSH_MSG_NONE;
931 buffer_clear(&incoming_packet);
932 cp = buffer_append_space(&incoming_packet, block_size);
933 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
934 block_size);
935 cp = buffer_ptr(&incoming_packet);
936 packet_length = GET_32BIT(cp);
937 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
873 fatal_cleanup();
874 }
875 if (len < 0)
876 fatal("Read from socket failed: %.100s", strerror(errno));
877 /* Append it to the buffer. */
878 packet_process_incoming(buf, len);
879 }
880 /* NOTREACHED */
881}
882
883int
884packet_read(void)
885{
886 return packet_read_seqnr(NULL);
887}
888
889/*
890 * Waits until a packet has been received, verifies that its type matches
891 * that given, and gives a fatal error and exits if there is a mismatch.
892 */
893
894void
895packet_read_expect(int expected_type)
896{
897 int type;
898
899 type = packet_read();
900 if (type != expected_type)
901 packet_disconnect("Protocol error: expected packet type %d, got %d",
902 expected_type, type);
903}
904
905/* Checks if a full packet is available in the data received so far via
906 * packet_process_incoming. If so, reads the packet; otherwise returns
907 * SSH_MSG_NONE. This does not wait for data from the connection.
908 *
909 * SSH_MSG_DISCONNECT is handled specially here. Also,
910 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
911 * to higher levels.
912 */
913
914static int
915packet_read_poll1(void)
916{
917 u_int len, padded_len;
918 u_char *cp, type;
919 u_int checksum, stored_checksum;
920
921 /* Check if input size is less than minimum packet size. */
922 if (buffer_len(&input) < 4 + 8)
923 return SSH_MSG_NONE;
924 /* Get length of incoming packet. */
925 cp = buffer_ptr(&input);
926 len = GET_32BIT(cp);
927 if (len < 1 + 2 + 2 || len > 256 * 1024)
928 packet_disconnect("Bad packet length %u.", len);
929 padded_len = (len + 8) & ~7;
930
931 /* Check if the packet has been entirely received. */
932 if (buffer_len(&input) < 4 + padded_len)
933 return SSH_MSG_NONE;
934
935 /* The entire packet is in buffer. */
936
937 /* Consume packet length. */
938 buffer_consume(&input, 4);
939
940 /*
941 * Cryptographic attack detector for ssh
942 * (C)1998 CORE-SDI, Buenos Aires Argentina
943 * Ariel Futoransky(futo@core-sdi.com)
944 */
945 if (!receive_context.plaintext &&
946 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
947 packet_disconnect("crc32 compensation attack: network attack detected");
948
949 /* Decrypt data to incoming_packet. */
950 buffer_clear(&incoming_packet);
951 cp = buffer_append_space(&incoming_packet, padded_len);
952 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
953
954 buffer_consume(&input, padded_len);
955
956#ifdef PACKET_DEBUG
957 fprintf(stderr, "read_poll plain: ");
958 buffer_dump(&incoming_packet);
959#endif
960
961 /* Compute packet checksum. */
962 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
963 buffer_len(&incoming_packet) - 4);
964
965 /* Skip padding. */
966 buffer_consume(&incoming_packet, 8 - len % 8);
967
968 /* Test check bytes. */
969 if (len != buffer_len(&incoming_packet))
970 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
971 len, buffer_len(&incoming_packet));
972
973 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
974 stored_checksum = GET_32BIT(cp);
975 if (checksum != stored_checksum)
976 packet_disconnect("Corrupted check bytes on input.");
977 buffer_consume_end(&incoming_packet, 4);
978
979 if (packet_compression) {
980 buffer_clear(&compression_buffer);
981 buffer_uncompress(&incoming_packet, &compression_buffer);
982 buffer_clear(&incoming_packet);
983 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
984 buffer_len(&compression_buffer));
985 }
986 type = buffer_get_char(&incoming_packet);
987 return type;
988}
989
990static int
991packet_read_poll2(u_int32_t *seqnr_p)
992{
993 static u_int packet_length = 0;
994 u_int padlen, need;
995 u_char *macbuf, *cp, type;
996 int maclen, block_size;
997 Enc *enc = NULL;
998 Mac *mac = NULL;
999 Comp *comp = NULL;
1000
1001 if (newkeys[MODE_IN] != NULL) {
1002 enc = &newkeys[MODE_IN]->enc;
1003 mac = &newkeys[MODE_IN]->mac;
1004 comp = &newkeys[MODE_IN]->comp;
1005 }
1006 maclen = mac && mac->enabled ? mac->mac_len : 0;
1007 block_size = enc ? enc->block_size : 8;
1008
1009 if (packet_length == 0) {
1010 /*
1011 * check if input size is less than the cipher block size,
1012 * decrypt first block and extract length of incoming packet
1013 */
1014 if (buffer_len(&input) < block_size)
1015 return SSH_MSG_NONE;
1016 buffer_clear(&incoming_packet);
1017 cp = buffer_append_space(&incoming_packet, block_size);
1018 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
1019 block_size);
1020 cp = buffer_ptr(&incoming_packet);
1021 packet_length = GET_32BIT(cp);
1022 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
1023#ifdef PACKET_DEBUG
938 buffer_dump(&incoming_packet);
1024 buffer_dump(&incoming_packet);
1025#endif
939 packet_disconnect("Bad packet length %u.", packet_length);
940 }
941 DBG(debug("input: packet len %u", packet_length+4));
942 buffer_consume(&input, block_size);
943 }
944 /* we have a partial packet of block_size bytes */
945 need = 4 + packet_length - block_size;
946 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
947 need, maclen));
948 if (need % block_size != 0)
949 fatal("padding error: need %d block %d mod %d",
950 need, block_size, need % block_size);
951 /*
952 * check if the entire packet has been received and
953 * decrypt into incoming_packet
954 */
955 if (buffer_len(&input) < need + maclen)
956 return SSH_MSG_NONE;
957#ifdef PACKET_DEBUG
958 fprintf(stderr, "read_poll enc/full: ");
959 buffer_dump(&input);
960#endif
961 cp = buffer_append_space(&incoming_packet, need);
962 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
963 buffer_consume(&input, need);
964 /*
965 * compute MAC over seqnr and packet,
966 * increment sequence number for incoming packet
967 */
968 if (mac && mac->enabled) {
1026 packet_disconnect("Bad packet length %u.", packet_length);
1027 }
1028 DBG(debug("input: packet len %u", packet_length+4));
1029 buffer_consume(&input, block_size);
1030 }
1031 /* we have a partial packet of block_size bytes */
1032 need = 4 + packet_length - block_size;
1033 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1034 need, maclen));
1035 if (need % block_size != 0)
1036 fatal("padding error: need %d block %d mod %d",
1037 need, block_size, need % block_size);
1038 /*
1039 * check if the entire packet has been received and
1040 * decrypt into incoming_packet
1041 */
1042 if (buffer_len(&input) < need + maclen)
1043 return SSH_MSG_NONE;
1044#ifdef PACKET_DEBUG
1045 fprintf(stderr, "read_poll enc/full: ");
1046 buffer_dump(&input);
1047#endif
1048 cp = buffer_append_space(&incoming_packet, need);
1049 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
1050 buffer_consume(&input, need);
1051 /*
1052 * compute MAC over seqnr and packet,
1053 * increment sequence number for incoming packet
1054 */
1055 if (mac && mac->enabled) {
969 macbuf = mac_compute(mac, read_seqnr,
1056 macbuf = mac_compute(mac, p_read.seqnr,
970 buffer_ptr(&incoming_packet),
971 buffer_len(&incoming_packet));
972 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
973 packet_disconnect("Corrupted MAC on input.");
1057 buffer_ptr(&incoming_packet),
1058 buffer_len(&incoming_packet));
1059 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
1060 packet_disconnect("Corrupted MAC on input.");
974 DBG(debug("MAC #%d ok", read_seqnr));
1061 DBG(debug("MAC #%d ok", p_read.seqnr));
975 buffer_consume(&input, mac->mac_len);
976 }
977 if (seqnr_p != NULL)
1062 buffer_consume(&input, mac->mac_len);
1063 }
1064 if (seqnr_p != NULL)
978 *seqnr_p = read_seqnr;
979 if (++read_seqnr == 0)
980 log("incoming seqnr wraps around");
1065 *seqnr_p = p_read.seqnr;
1066 if (++p_read.seqnr == 0)
1067 logit("incoming seqnr wraps around");
1068 if (++p_read.packets == 0)
1069 if (!(datafellows & SSH_BUG_NOREKEY))
1070 fatal("XXX too many packets with same key");
1071 p_read.blocks += (packet_length + 4) / block_size;
981
982 /* get padlen */
983 cp = buffer_ptr(&incoming_packet);
984 padlen = cp[4];
985 DBG(debug("input: padlen %d", padlen));
986 if (padlen < 4)
987 packet_disconnect("Corrupted padlen %d on input.", padlen);
988
989 /* skip packet size + padlen, discard padding */
990 buffer_consume(&incoming_packet, 4 + 1);
991 buffer_consume_end(&incoming_packet, padlen);
992
993 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
994 if (comp && comp->enabled) {
995 buffer_clear(&compression_buffer);
996 buffer_uncompress(&incoming_packet, &compression_buffer);
997 buffer_clear(&incoming_packet);
998 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
999 buffer_len(&compression_buffer));
1000 DBG(debug("input: len after de-compress %d",
1001 buffer_len(&incoming_packet)));
1002 }
1003 /*
1004 * get packet type, implies consume.
1005 * return length of payload (without type field)
1006 */
1007 type = buffer_get_char(&incoming_packet);
1008 if (type == SSH2_MSG_NEWKEYS)
1009 set_newkeys(MODE_IN);
1010#ifdef PACKET_DEBUG
1011 fprintf(stderr, "read/plain[%d]:\r\n", type);
1012 buffer_dump(&incoming_packet);
1013#endif
1014 /* reset for next packet */
1015 packet_length = 0;
1016 return type;
1017}
1018
1019int
1020packet_read_poll_seqnr(u_int32_t *seqnr_p)
1021{
1022 u_int reason, seqnr;
1023 u_char type;
1024 char *msg;
1025
1026 for (;;) {
1027 if (compat20) {
1028 type = packet_read_poll2(seqnr_p);
1029 if (type)
1030 DBG(debug("received packet type %d", type));
1031 switch (type) {
1032 case SSH2_MSG_IGNORE:
1033 break;
1034 case SSH2_MSG_DEBUG:
1035 packet_get_char();
1036 msg = packet_get_string(NULL);
1037 debug("Remote: %.900s", msg);
1038 xfree(msg);
1039 msg = packet_get_string(NULL);
1040 xfree(msg);
1041 break;
1042 case SSH2_MSG_DISCONNECT:
1043 reason = packet_get_int();
1044 msg = packet_get_string(NULL);
1072
1073 /* get padlen */
1074 cp = buffer_ptr(&incoming_packet);
1075 padlen = cp[4];
1076 DBG(debug("input: padlen %d", padlen));
1077 if (padlen < 4)
1078 packet_disconnect("Corrupted padlen %d on input.", padlen);
1079
1080 /* skip packet size + padlen, discard padding */
1081 buffer_consume(&incoming_packet, 4 + 1);
1082 buffer_consume_end(&incoming_packet, padlen);
1083
1084 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1085 if (comp && comp->enabled) {
1086 buffer_clear(&compression_buffer);
1087 buffer_uncompress(&incoming_packet, &compression_buffer);
1088 buffer_clear(&incoming_packet);
1089 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1090 buffer_len(&compression_buffer));
1091 DBG(debug("input: len after de-compress %d",
1092 buffer_len(&incoming_packet)));
1093 }
1094 /*
1095 * get packet type, implies consume.
1096 * return length of payload (without type field)
1097 */
1098 type = buffer_get_char(&incoming_packet);
1099 if (type == SSH2_MSG_NEWKEYS)
1100 set_newkeys(MODE_IN);
1101#ifdef PACKET_DEBUG
1102 fprintf(stderr, "read/plain[%d]:\r\n", type);
1103 buffer_dump(&incoming_packet);
1104#endif
1105 /* reset for next packet */
1106 packet_length = 0;
1107 return type;
1108}
1109
1110int
1111packet_read_poll_seqnr(u_int32_t *seqnr_p)
1112{
1113 u_int reason, seqnr;
1114 u_char type;
1115 char *msg;
1116
1117 for (;;) {
1118 if (compat20) {
1119 type = packet_read_poll2(seqnr_p);
1120 if (type)
1121 DBG(debug("received packet type %d", type));
1122 switch (type) {
1123 case SSH2_MSG_IGNORE:
1124 break;
1125 case SSH2_MSG_DEBUG:
1126 packet_get_char();
1127 msg = packet_get_string(NULL);
1128 debug("Remote: %.900s", msg);
1129 xfree(msg);
1130 msg = packet_get_string(NULL);
1131 xfree(msg);
1132 break;
1133 case SSH2_MSG_DISCONNECT:
1134 reason = packet_get_int();
1135 msg = packet_get_string(NULL);
1045 log("Received disconnect from %s: %u: %.400s",
1136 logit("Received disconnect from %s: %u: %.400s",
1046 get_remote_ipaddr(), reason, msg);
1047 xfree(msg);
1048 fatal_cleanup();
1049 break;
1050 case SSH2_MSG_UNIMPLEMENTED:
1051 seqnr = packet_get_int();
1052 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1053 seqnr);
1054 break;
1055 default:
1056 return type;
1057 break;
1058 }
1059 } else {
1060 type = packet_read_poll1();
1061 switch (type) {
1062 case SSH_MSG_IGNORE:
1063 break;
1064 case SSH_MSG_DEBUG:
1065 msg = packet_get_string(NULL);
1066 debug("Remote: %.900s", msg);
1067 xfree(msg);
1068 break;
1069 case SSH_MSG_DISCONNECT:
1070 msg = packet_get_string(NULL);
1137 get_remote_ipaddr(), reason, msg);
1138 xfree(msg);
1139 fatal_cleanup();
1140 break;
1141 case SSH2_MSG_UNIMPLEMENTED:
1142 seqnr = packet_get_int();
1143 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1144 seqnr);
1145 break;
1146 default:
1147 return type;
1148 break;
1149 }
1150 } else {
1151 type = packet_read_poll1();
1152 switch (type) {
1153 case SSH_MSG_IGNORE:
1154 break;
1155 case SSH_MSG_DEBUG:
1156 msg = packet_get_string(NULL);
1157 debug("Remote: %.900s", msg);
1158 xfree(msg);
1159 break;
1160 case SSH_MSG_DISCONNECT:
1161 msg = packet_get_string(NULL);
1071 log("Received disconnect from %s: %.400s",
1162 logit("Received disconnect from %s: %.400s",
1072 get_remote_ipaddr(), msg);
1073 fatal_cleanup();
1074 xfree(msg);
1075 break;
1076 default:
1077 if (type)
1078 DBG(debug("received packet type %d", type));
1079 return type;
1080 break;
1081 }
1082 }
1083 }
1084}
1085
1086int
1087packet_read_poll(void)
1088{
1089 return packet_read_poll_seqnr(NULL);
1090}
1091
1092/*
1093 * Buffers the given amount of input characters. This is intended to be used
1094 * together with packet_read_poll.
1095 */
1096
1097void
1098packet_process_incoming(const char *buf, u_int len)
1099{
1100 buffer_append(&input, buf, len);
1101}
1102
1103/* Returns a character from the packet. */
1104
1105u_int
1106packet_get_char(void)
1107{
1108 char ch;
1109
1110 buffer_get(&incoming_packet, &ch, 1);
1111 return (u_char) ch;
1112}
1113
1114/* Returns an integer from the packet data. */
1115
1116u_int
1117packet_get_int(void)
1118{
1119 return buffer_get_int(&incoming_packet);
1120}
1121
1122/*
1123 * Returns an arbitrary precision integer from the packet data. The integer
1124 * must have been initialized before this call.
1125 */
1126
1127void
1128packet_get_bignum(BIGNUM * value)
1129{
1130 buffer_get_bignum(&incoming_packet, value);
1131}
1132
1133void
1134packet_get_bignum2(BIGNUM * value)
1135{
1136 buffer_get_bignum2(&incoming_packet, value);
1137}
1138
1139void *
1140packet_get_raw(int *length_ptr)
1141{
1142 int bytes = buffer_len(&incoming_packet);
1143
1144 if (length_ptr != NULL)
1145 *length_ptr = bytes;
1146 return buffer_ptr(&incoming_packet);
1147}
1148
1149int
1150packet_remaining(void)
1151{
1152 return buffer_len(&incoming_packet);
1153}
1154
1155/*
1156 * Returns a string from the packet data. The string is allocated using
1157 * xmalloc; it is the responsibility of the calling program to free it when
1158 * no longer needed. The length_ptr argument may be NULL, or point to an
1159 * integer into which the length of the string is stored.
1160 */
1161
1162void *
1163packet_get_string(u_int *length_ptr)
1164{
1165 return buffer_get_string(&incoming_packet, length_ptr);
1166}
1167
1168/*
1169 * Sends a diagnostic message from the server to the client. This message
1170 * can be sent at any time (but not while constructing another message). The
1171 * message is printed immediately, but only if the client is being executed
1172 * in verbose mode. These messages are primarily intended to ease debugging
1173 * authentication problems. The length of the formatted message must not
1174 * exceed 1024 bytes. This will automatically call packet_write_wait.
1175 */
1176
1177void
1178packet_send_debug(const char *fmt,...)
1179{
1180 char buf[1024];
1181 va_list args;
1182
1183 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1184 return;
1185
1186 va_start(args, fmt);
1187 vsnprintf(buf, sizeof(buf), fmt, args);
1188 va_end(args);
1189
1190 if (compat20) {
1191 packet_start(SSH2_MSG_DEBUG);
1192 packet_put_char(0); /* bool: always display */
1193 packet_put_cstring(buf);
1194 packet_put_cstring("");
1195 } else {
1196 packet_start(SSH_MSG_DEBUG);
1197 packet_put_cstring(buf);
1198 }
1199 packet_send();
1200 packet_write_wait();
1201}
1202
1203/*
1204 * Logs the error plus constructs and sends a disconnect packet, closes the
1205 * connection, and exits. This function never returns. The error message
1206 * should not contain a newline. The length of the formatted message must
1207 * not exceed 1024 bytes.
1208 */
1209
1210void
1211packet_disconnect(const char *fmt,...)
1212{
1213 char buf[1024];
1214 va_list args;
1215 static int disconnecting = 0;
1216
1217 if (disconnecting) /* Guard against recursive invocations. */
1218 fatal("packet_disconnect called recursively.");
1219 disconnecting = 1;
1220
1221 /*
1222 * Format the message. Note that the caller must make sure the
1223 * message is of limited size.
1224 */
1225 va_start(args, fmt);
1226 vsnprintf(buf, sizeof(buf), fmt, args);
1227 va_end(args);
1228
1229 /* Display the error locally */
1163 get_remote_ipaddr(), msg);
1164 fatal_cleanup();
1165 xfree(msg);
1166 break;
1167 default:
1168 if (type)
1169 DBG(debug("received packet type %d", type));
1170 return type;
1171 break;
1172 }
1173 }
1174 }
1175}
1176
1177int
1178packet_read_poll(void)
1179{
1180 return packet_read_poll_seqnr(NULL);
1181}
1182
1183/*
1184 * Buffers the given amount of input characters. This is intended to be used
1185 * together with packet_read_poll.
1186 */
1187
1188void
1189packet_process_incoming(const char *buf, u_int len)
1190{
1191 buffer_append(&input, buf, len);
1192}
1193
1194/* Returns a character from the packet. */
1195
1196u_int
1197packet_get_char(void)
1198{
1199 char ch;
1200
1201 buffer_get(&incoming_packet, &ch, 1);
1202 return (u_char) ch;
1203}
1204
1205/* Returns an integer from the packet data. */
1206
1207u_int
1208packet_get_int(void)
1209{
1210 return buffer_get_int(&incoming_packet);
1211}
1212
1213/*
1214 * Returns an arbitrary precision integer from the packet data. The integer
1215 * must have been initialized before this call.
1216 */
1217
1218void
1219packet_get_bignum(BIGNUM * value)
1220{
1221 buffer_get_bignum(&incoming_packet, value);
1222}
1223
1224void
1225packet_get_bignum2(BIGNUM * value)
1226{
1227 buffer_get_bignum2(&incoming_packet, value);
1228}
1229
1230void *
1231packet_get_raw(int *length_ptr)
1232{
1233 int bytes = buffer_len(&incoming_packet);
1234
1235 if (length_ptr != NULL)
1236 *length_ptr = bytes;
1237 return buffer_ptr(&incoming_packet);
1238}
1239
1240int
1241packet_remaining(void)
1242{
1243 return buffer_len(&incoming_packet);
1244}
1245
1246/*
1247 * Returns a string from the packet data. The string is allocated using
1248 * xmalloc; it is the responsibility of the calling program to free it when
1249 * no longer needed. The length_ptr argument may be NULL, or point to an
1250 * integer into which the length of the string is stored.
1251 */
1252
1253void *
1254packet_get_string(u_int *length_ptr)
1255{
1256 return buffer_get_string(&incoming_packet, length_ptr);
1257}
1258
1259/*
1260 * Sends a diagnostic message from the server to the client. This message
1261 * can be sent at any time (but not while constructing another message). The
1262 * message is printed immediately, but only if the client is being executed
1263 * in verbose mode. These messages are primarily intended to ease debugging
1264 * authentication problems. The length of the formatted message must not
1265 * exceed 1024 bytes. This will automatically call packet_write_wait.
1266 */
1267
1268void
1269packet_send_debug(const char *fmt,...)
1270{
1271 char buf[1024];
1272 va_list args;
1273
1274 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1275 return;
1276
1277 va_start(args, fmt);
1278 vsnprintf(buf, sizeof(buf), fmt, args);
1279 va_end(args);
1280
1281 if (compat20) {
1282 packet_start(SSH2_MSG_DEBUG);
1283 packet_put_char(0); /* bool: always display */
1284 packet_put_cstring(buf);
1285 packet_put_cstring("");
1286 } else {
1287 packet_start(SSH_MSG_DEBUG);
1288 packet_put_cstring(buf);
1289 }
1290 packet_send();
1291 packet_write_wait();
1292}
1293
1294/*
1295 * Logs the error plus constructs and sends a disconnect packet, closes the
1296 * connection, and exits. This function never returns. The error message
1297 * should not contain a newline. The length of the formatted message must
1298 * not exceed 1024 bytes.
1299 */
1300
1301void
1302packet_disconnect(const char *fmt,...)
1303{
1304 char buf[1024];
1305 va_list args;
1306 static int disconnecting = 0;
1307
1308 if (disconnecting) /* Guard against recursive invocations. */
1309 fatal("packet_disconnect called recursively.");
1310 disconnecting = 1;
1311
1312 /*
1313 * Format the message. Note that the caller must make sure the
1314 * message is of limited size.
1315 */
1316 va_start(args, fmt);
1317 vsnprintf(buf, sizeof(buf), fmt, args);
1318 va_end(args);
1319
1320 /* Display the error locally */
1230 log("Disconnecting: %.100s", buf);
1321 logit("Disconnecting: %.100s", buf);
1231
1232 /* Send the disconnect message to the other side, and wait for it to get sent. */
1233 if (compat20) {
1234 packet_start(SSH2_MSG_DISCONNECT);
1235 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1236 packet_put_cstring(buf);
1237 packet_put_cstring("");
1238 } else {
1239 packet_start(SSH_MSG_DISCONNECT);
1240 packet_put_cstring(buf);
1241 }
1242 packet_send();
1243 packet_write_wait();
1244
1245 /* Stop listening for connections. */
1246 channel_close_all();
1247
1248 /* Close the connection. */
1249 packet_close();
1250
1251 fatal_cleanup();
1252}
1253
1254/* Checks if there is any buffered output, and tries to write some of the output. */
1255
1256void
1257packet_write_poll(void)
1258{
1259 int len = buffer_len(&output);
1260
1261 if (len > 0) {
1262 len = write(connection_out, buffer_ptr(&output), len);
1263 if (len <= 0) {
1264 if (errno == EAGAIN)
1265 return;
1266 else
1267 fatal("Write failed: %.100s", strerror(errno));
1268 }
1269 buffer_consume(&output, len);
1270 }
1271}
1272
1273/*
1274 * Calls packet_write_poll repeatedly until all pending output data has been
1275 * written.
1276 */
1277
1278void
1279packet_write_wait(void)
1280{
1281 fd_set *setp;
1282
1283 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1284 sizeof(fd_mask));
1285 packet_write_poll();
1286 while (packet_have_data_to_write()) {
1287 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1288 sizeof(fd_mask));
1289 FD_SET(connection_out, setp);
1290 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
1291 (errno == EAGAIN || errno == EINTR))
1292 ;
1293 packet_write_poll();
1294 }
1295 xfree(setp);
1296}
1297
1298/* Returns true if there is buffered data to write to the connection. */
1299
1300int
1301packet_have_data_to_write(void)
1302{
1303 return buffer_len(&output) != 0;
1304}
1305
1306/* Returns true if there is not too much data to write to the connection. */
1307
1308int
1309packet_not_very_much_data_to_write(void)
1310{
1311 if (interactive_mode)
1312 return buffer_len(&output) < 16384;
1313 else
1314 return buffer_len(&output) < 128 * 1024;
1315}
1316
1322
1323 /* Send the disconnect message to the other side, and wait for it to get sent. */
1324 if (compat20) {
1325 packet_start(SSH2_MSG_DISCONNECT);
1326 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1327 packet_put_cstring(buf);
1328 packet_put_cstring("");
1329 } else {
1330 packet_start(SSH_MSG_DISCONNECT);
1331 packet_put_cstring(buf);
1332 }
1333 packet_send();
1334 packet_write_wait();
1335
1336 /* Stop listening for connections. */
1337 channel_close_all();
1338
1339 /* Close the connection. */
1340 packet_close();
1341
1342 fatal_cleanup();
1343}
1344
1345/* Checks if there is any buffered output, and tries to write some of the output. */
1346
1347void
1348packet_write_poll(void)
1349{
1350 int len = buffer_len(&output);
1351
1352 if (len > 0) {
1353 len = write(connection_out, buffer_ptr(&output), len);
1354 if (len <= 0) {
1355 if (errno == EAGAIN)
1356 return;
1357 else
1358 fatal("Write failed: %.100s", strerror(errno));
1359 }
1360 buffer_consume(&output, len);
1361 }
1362}
1363
1364/*
1365 * Calls packet_write_poll repeatedly until all pending output data has been
1366 * written.
1367 */
1368
1369void
1370packet_write_wait(void)
1371{
1372 fd_set *setp;
1373
1374 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1375 sizeof(fd_mask));
1376 packet_write_poll();
1377 while (packet_have_data_to_write()) {
1378 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1379 sizeof(fd_mask));
1380 FD_SET(connection_out, setp);
1381 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
1382 (errno == EAGAIN || errno == EINTR))
1383 ;
1384 packet_write_poll();
1385 }
1386 xfree(setp);
1387}
1388
1389/* Returns true if there is buffered data to write to the connection. */
1390
1391int
1392packet_have_data_to_write(void)
1393{
1394 return buffer_len(&output) != 0;
1395}
1396
1397/* Returns true if there is not too much data to write to the connection. */
1398
1399int
1400packet_not_very_much_data_to_write(void)
1401{
1402 if (interactive_mode)
1403 return buffer_len(&output) < 16384;
1404 else
1405 return buffer_len(&output) < 128 * 1024;
1406}
1407
1408
1409#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
1317static void
1318packet_set_tos(int interactive)
1319{
1320 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1321
1322 if (!packet_connection_is_on_socket() ||
1323 !packet_connection_is_ipv4())
1324 return;
1325 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1326 sizeof(tos)) < 0)
1327 error("setsockopt IP_TOS %d: %.100s:",
1328 tos, strerror(errno));
1329}
1410static void
1411packet_set_tos(int interactive)
1412{
1413 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1414
1415 if (!packet_connection_is_on_socket() ||
1416 !packet_connection_is_ipv4())
1417 return;
1418 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1419 sizeof(tos)) < 0)
1420 error("setsockopt IP_TOS %d: %.100s:",
1421 tos, strerror(errno));
1422}
1423#endif
1330
1331/* Informs that the current session is interactive. Sets IP flags for that. */
1332
1333void
1334packet_set_interactive(int interactive)
1335{
1336 static int called = 0;
1337
1338 if (called)
1339 return;
1340 called = 1;
1341
1342 /* Record that we are in interactive mode. */
1343 interactive_mode = interactive;
1344
1345 /* Only set socket options if using a socket. */
1346 if (!packet_connection_is_on_socket())
1347 return;
1348 if (interactive)
1349 set_nodelay(connection_in);
1350#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
1351 packet_set_tos(interactive);
1352#endif
1353
1354}
1355
1356/* Returns true if the current connection is interactive. */
1357
1358int
1359packet_is_interactive(void)
1360{
1361 return interactive_mode;
1362}
1363
1424
1425/* Informs that the current session is interactive. Sets IP flags for that. */
1426
1427void
1428packet_set_interactive(int interactive)
1429{
1430 static int called = 0;
1431
1432 if (called)
1433 return;
1434 called = 1;
1435
1436 /* Record that we are in interactive mode. */
1437 interactive_mode = interactive;
1438
1439 /* Only set socket options if using a socket. */
1440 if (!packet_connection_is_on_socket())
1441 return;
1442 if (interactive)
1443 set_nodelay(connection_in);
1444#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
1445 packet_set_tos(interactive);
1446#endif
1447
1448}
1449
1450/* Returns true if the current connection is interactive. */
1451
1452int
1453packet_is_interactive(void)
1454{
1455 return interactive_mode;
1456}
1457
1364int
1365packet_set_maxsize(int s)
1458u_int
1459packet_set_maxsize(u_int s)
1366{
1367 static int called = 0;
1368
1369 if (called) {
1460{
1461 static int called = 0;
1462
1463 if (called) {
1370 log("packet_set_maxsize: called twice: old %d new %d",
1464 logit("packet_set_maxsize: called twice: old %d new %d",
1371 max_packet_size, s);
1372 return -1;
1373 }
1374 if (s < 4 * 1024 || s > 1024 * 1024) {
1465 max_packet_size, s);
1466 return -1;
1467 }
1468 if (s < 4 * 1024 || s > 1024 * 1024) {
1375 log("packet_set_maxsize: bad size %d", s);
1469 logit("packet_set_maxsize: bad size %d", s);
1376 return -1;
1377 }
1378 called = 1;
1379 debug("packet_set_maxsize: setting to %d", s);
1380 max_packet_size = s;
1381 return s;
1382}
1383
1384/* roundup current message to pad bytes */
1385void
1386packet_add_padding(u_char pad)
1387{
1388 extra_pad = pad;
1389}
1390
1391/*
1392 * 9.2. Ignored Data Message
1393 *
1394 * byte SSH_MSG_IGNORE
1395 * string data
1396 *
1397 * All implementations MUST understand (and ignore) this message at any
1398 * time (after receiving the protocol version). No implementation is
1399 * required to send them. This message can be used as an additional
1400 * protection measure against advanced traffic analysis techniques.
1401 */
1402void
1403packet_send_ignore(int nbytes)
1404{
1405 u_int32_t rand = 0;
1406 int i;
1407
1408 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
1409 packet_put_int(nbytes);
1410 for (i = 0; i < nbytes; i++) {
1411 if (i % 4 == 0)
1412 rand = arc4random();
1413 packet_put_char(rand & 0xff);
1414 rand >>= 8;
1415 }
1416}
1470 return -1;
1471 }
1472 called = 1;
1473 debug("packet_set_maxsize: setting to %d", s);
1474 max_packet_size = s;
1475 return s;
1476}
1477
1478/* roundup current message to pad bytes */
1479void
1480packet_add_padding(u_char pad)
1481{
1482 extra_pad = pad;
1483}
1484
1485/*
1486 * 9.2. Ignored Data Message
1487 *
1488 * byte SSH_MSG_IGNORE
1489 * string data
1490 *
1491 * All implementations MUST understand (and ignore) this message at any
1492 * time (after receiving the protocol version). No implementation is
1493 * required to send them. This message can be used as an additional
1494 * protection measure against advanced traffic analysis techniques.
1495 */
1496void
1497packet_send_ignore(int nbytes)
1498{
1499 u_int32_t rand = 0;
1500 int i;
1501
1502 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
1503 packet_put_int(nbytes);
1504 for (i = 0; i < nbytes; i++) {
1505 if (i % 4 == 0)
1506 rand = arc4random();
1507 packet_put_char(rand & 0xff);
1508 rand >>= 8;
1509 }
1510}
1511
1512#define MAX_PACKETS (1<<31)
1513int
1514packet_need_rekeying(void)
1515{
1516 if (datafellows & SSH_BUG_NOREKEY)
1517 return 0;
1518 return
1519 (p_send.packets > MAX_PACKETS) ||
1520 (p_read.packets > MAX_PACKETS) ||
1521 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1522 (max_blocks_in && (p_read.blocks > max_blocks_in));
1523}
1524
1525void
1526packet_set_rekey_limit(u_int32_t bytes)
1527{
1528 rekey_limit = bytes;
1529}