annotate packet.c @ 728:f27058078d61

Try using writev() for writing packets out to tcp
author Matt Johnston <matt@ucc.asn.au>
date Sun, 31 Mar 2013 23:15:35 +0800
parents f4232b65b316
children 9a5438271556
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 * Dropbear - a SSH2 server
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 *
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * Copyright (c) 2002,2003 Matt Johnston
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * All rights reserved.
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 *
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * of this software and associated documentation files (the "Software"), to deal
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * in the Software without restriction, including without limitation the rights
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * copies of the Software, and to permit persons to whom the Software is
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * furnished to do so, subject to the following conditions:
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 *
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * The above copyright notice and this permission notice shall be included in
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * all copies or substantial portions of the Software.
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 *
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * SOFTWARE. */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #include "includes.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #include "packet.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #include "session.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #include "dbutil.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 #include "ssh.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #include "algo.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 #include "buffer.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 #include "kex.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #include "random.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #include "service.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 #include "auth.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #include "channel.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
38 static int read_packet_init();
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
39 static void make_mac(unsigned int seqno, const struct key_context_directional * key_state,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
40 buffer * clear_buf, unsigned int clear_len,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
41 unsigned char *output_mac);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
42 static int checkmac();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
605
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
44 #define ZLIB_COMPRESS_INCR 100
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 #define ZLIB_DECOMPRESS_INCR 100
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 static buffer* buf_decompress(buffer* buf, unsigned int len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 static void buf_compress(buffer * dest, buffer * src, unsigned int len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
531
164b7c2cd5df disapproval of revision 'a101cbd046507cf723e6362a49196dbd4b924042'
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
51 /* non-blocking function writing out a current encrypted packet */
164b7c2cd5df disapproval of revision 'a101cbd046507cf723e6362a49196dbd4b924042'
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
52 void write_packet() {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 int len, written;
70
b0316ce64e4b Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents: 33
diff changeset
55 buffer * writebuf = NULL;
592
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
56 time_t now;
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
57 unsigned packet_type;
728
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
58 int all_ignore = 1;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
59 #ifdef HAVE_WRITEV
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
60 struct iovec *iov = NULL;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
61 int i;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
62 struct Link *l;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
63 #endif
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
65 TRACE(("enter write_packet"))
531
164b7c2cd5df disapproval of revision 'a101cbd046507cf723e6362a49196dbd4b924042'
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
66 dropbear_assert(!isempty(&ses.writequeue));
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67
728
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
68 #ifdef HAVE_WRITEV
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
69 iov = m_malloc(sizeof(*iov) * ses.writequeue.count);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
70 for (l = ses.writequeue.head, i = 0; l; l = l->link, i++)
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
71 {
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
72 writebuf = (buffer*)l->item;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
73 packet_type = writebuf->data[writebuf->len-1];
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
74 len = writebuf->len - 1 - writebuf->pos;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
75 dropbear_assert(len > 0);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
76 all_ignore &= (packet_type == SSH_MSG_IGNORE);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
77 iov[i].iov_base = buf_getptr(writebuf, len);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
78 iov[i].iov_len = len;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
79 }
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
80 written = writev(ses.sock_out, iov, ses.writequeue.count);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
81 if (written < 0) {
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
82 if (errno == EINTR) {
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
83 m_free(iov);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
84 TRACE(("leave writepacket: EINTR"))
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
85 return;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
86 } else {
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
87 dropbear_exit("Error writing");
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
88 }
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
89 }
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
90
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
91 if (written == 0) {
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
92 ses.remoteclosed();
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
93 }
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
94
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
95 while (written > 0) {
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
96 writebuf = (buffer*)examine(&ses.writequeue);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
97 len = writebuf->len - 1 - writebuf->pos;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
98 if (len > written) {
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
99 // partial buffer write
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
100 buf_incrpos(writebuf, written);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
101 written = 0;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
102 } else {
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
103 written -= len;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
104 dequeue(&ses.writequeue);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
105 buf_free(writebuf);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
106 }
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
107 }
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
108
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
109 m_free(iov);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
110
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
111 #else
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 /* Get the next buffer in the queue of encrypted packets to write*/
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 writebuf = (buffer*)examine(&ses.writequeue);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114
592
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
115 /* The last byte of the buffer is not to be transmitted, but is
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
116 * a cleartext packet_type indicator */
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
117 packet_type = writebuf->data[writebuf->len-1];
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
118 len = writebuf->len - 1 - writebuf->pos;
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
119 dropbear_assert(len > 0);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 /* Try to write as much as possible */
479
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
121 written = write(ses.sock_out, buf_getptr(writebuf, len), len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 if (written < 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 if (errno == EINTR) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
125 TRACE(("leave writepacket: EINTR"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 } else {
594
a98a2138364a Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
128 dropbear_exit("Error writing");
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 }
728
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
131 all_ignore = (packet_type == SSH_MSG_IGNORE);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 if (written == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
134 ses.remoteclosed();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 if (written == len) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 /* We've finished with the packet, free it */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 dequeue(&ses.writequeue);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 buf_free(writebuf);
70
b0316ce64e4b Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents: 33
diff changeset
141 writebuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 /* More packet left to write, leave it in the queue for later */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 buf_incrpos(writebuf, written);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146
728
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
147 #endif
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
148 now = time(NULL);
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
149 ses.last_trx_packet_time = now;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
150
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
151 if (!all_ignore) {
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
152 ses.last_packet_time = now;
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
153 }
f27058078d61 Try using writev() for writing packets out to tcp
Matt Johnston <matt@ucc.asn.au>
parents: 711
diff changeset
154
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
155 TRACE(("leave write_packet"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 /* Non-blocking function reading available portion of a packet into the
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 * ses's buffer, decrypting the length if encrypted, decrypting the
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 * full portion if possible */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 void read_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 unsigned int maxlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
167 TRACE(("enter read_packet"))
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
168 blocksize = ses.keys->recv.algo_crypt->blocksize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 if (ses.readbuf == NULL || ses.readbuf->len < blocksize) {
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
171 int ret;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 /* In the first blocksize of a packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 /* Read the first blocksize of the packet, so we can decrypt it and
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 * find the length of the whole packet */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
176 ret = read_packet_init();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
178 if (ret == DROPBEAR_FAILURE) {
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
179 /* didn't read enough to determine the length */
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
180 TRACE(("leave read_packet: packetinit done"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 /* Attempt to read the remainder of the packet, note that there
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 * mightn't be any available (EAGAIN) */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 maxlen = ses.readbuf->len - ses.readbuf->pos;
711
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
188 if (maxlen == 0) {
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
189 /* Occurs when the packet is only a single block long and has all
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
190 * been read in read_packet_init(). Usually means that MAC is disabled
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
191 */
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
192 len = 0;
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
193 } else {
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
194 len = read(ses.sock_in, buf_getptr(ses.readbuf, maxlen), maxlen);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195
711
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
196 if (len == 0) {
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
197 ses.remoteclosed();
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
198 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199
711
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
200 if (len < 0) {
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
201 if (errno == EINTR || errno == EAGAIN) {
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
202 TRACE(("leave read_packet: EINTR or EAGAIN"))
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
203 return;
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
204 } else {
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
205 dropbear_exit("Error reading: %s", strerror(errno));
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
206 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 }
711
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
208
f4232b65b316 Fix "-m none" case where an entire packet fits in a block and can be
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
209 buf_incrpos(ses.readbuf, len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 if ((unsigned int)len == maxlen) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 /* The whole packet has been read */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 decrypt_packet();
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 /* The main select() loop process_packet() to
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 * handle the packet contents... */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 }
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
218 TRACE(("leave read_packet"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 /* Function used to read the initial portion of a packet, and determine the
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 * length. Only called during the first BLOCKSIZE of a packet. */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
223 /* Returns DROPBEAR_SUCCESS if the length is determined,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
224 * DROPBEAR_FAILURE otherwise */
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
225 static int read_packet_init() {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 unsigned int maxlen;
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
228 int slen;
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
229 unsigned int len;
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
230 unsigned int blocksize;
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
231 unsigned int macsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
234 blocksize = ses.keys->recv.algo_crypt->blocksize;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
235 macsize = ses.keys->recv.algo_mac->hashsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 if (ses.readbuf == NULL) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 /* start of a new packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 ses.readbuf = buf_new(INIT_READBUF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 maxlen = blocksize - ses.readbuf->pos;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 /* read the rest of the packet if possible */
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
245 slen = read(ses.sock_in, buf_getwriteptr(ses.readbuf, maxlen),
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 maxlen);
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
247 if (slen == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
248 ses.remoteclosed();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 }
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
250 if (slen < 0) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 if (errno == EINTR) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
252 TRACE(("leave read_packet_init: EINTR"))
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
253 return DROPBEAR_FAILURE;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 }
594
a98a2138364a Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
255 dropbear_exit("Error reading: %s", strerror(errno));
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
258 buf_incrwritepos(ses.readbuf, slen);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
260 if ((unsigned int)slen != maxlen) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 /* don't have enough bytes to determine length, get next time */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
262 return DROPBEAR_FAILURE;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 /* now we have the first block, need to get packet length, so we decrypt
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 * the first block (only need first 4 bytes) */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 buf_setpos(ses.readbuf, 0);
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
268 if (ses.keys->recv.crypt_mode->decrypt(buf_getptr(ses.readbuf, blocksize),
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
269 buf_getwriteptr(ses.readbuf, blocksize),
502
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
270 blocksize,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
271 &ses.keys->recv.cipher_state) != CRYPT_OK) {
594
a98a2138364a Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
272 dropbear_exit("Error decrypting");
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 }
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
274 len = buf_getint(ses.readbuf) + 4 + macsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
276 TRACE(("packet size is %d, block %d mac %d", len, blocksize, macsize))
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
277
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 /* check packet length */
448
9c61e7af0156 Rearrange the channel buffer sizes into three neat use-editable values in
Matt Johnston <matt@ucc.asn.au>
parents: 426
diff changeset
280 if ((len > RECV_MAX_PACKET_LEN) ||
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 (len < MIN_PACKET_LEN + macsize) ||
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 ((len - macsize) % blocksize != 0)) {
594
a98a2138364a Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
283 dropbear_exit("Integrity error (bad packet size %d)", len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
286 if (len > ses.readbuf->size) {
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
287 buf_resize(ses.readbuf, len);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
288 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 buf_setlen(ses.readbuf, len);
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
290 buf_setpos(ses.readbuf, blocksize);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
291 return DROPBEAR_SUCCESS;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 /* handle the received packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 void decrypt_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 unsigned char macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 unsigned int padlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 unsigned int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
302 TRACE(("enter decrypt_packet"))
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
303 blocksize = ses.keys->recv.algo_crypt->blocksize;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
304 macsize = ses.keys->recv.algo_mac->hashsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 ses.kexstate.datarecv += ses.readbuf->len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 /* we've already decrypted the first blocksize in read_packet_init */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 buf_setpos(ses.readbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
311 /* decrypt it in-place */
528
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
312 len = ses.readbuf->len - macsize - ses.readbuf->pos;
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
313 if (ses.keys->recv.crypt_mode->decrypt(
528
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
314 buf_getptr(ses.readbuf, len),
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
315 buf_getwriteptr(ses.readbuf, len),
528
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
316 len,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
317 &ses.keys->recv.cipher_state) != CRYPT_OK) {
594
a98a2138364a Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
318 dropbear_exit("Error decrypting");
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319 }
528
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
320 buf_incrpos(ses.readbuf, len);
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
321
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 /* check the hmac */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
323 if (checkmac() != DROPBEAR_SUCCESS) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 dropbear_exit("Integrity error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 /* get padding length */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
328 buf_setpos(ses.readbuf, PACKET_PADDING_OFF);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
329 padlen = buf_getbyte(ses.readbuf);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 /* payload length */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 /* - 4 - 1 is for LEN and PADLEN values */
556
ccdc4c6183c0 - Payload length doesn't include macsize.
Matt Johnston <matt@ucc.asn.au>
parents: 535
diff changeset
333 len = ses.readbuf->len - padlen - 4 - 1 - macsize;
448
9c61e7af0156 Rearrange the channel buffer sizes into three neat use-editable values in
Matt Johnston <matt@ucc.asn.au>
parents: 426
diff changeset
334 if ((len > RECV_MAX_PAYLOAD_LEN) || (len < 1)) {
594
a98a2138364a Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
335 dropbear_exit("Bad packet size %d", len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
338 buf_setpos(ses.readbuf, PACKET_PAYLOAD_OFF);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
339
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340 #ifndef DISABLE_ZLIB
501
d58c478bd399 Add support for [email protected] delayed compression.
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
341 if (is_compress_recv()) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 /* decompress */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
343 ses.payload = buf_decompress(ses.readbuf, len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 } else
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346 {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 /* copy payload */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 ses.payload = buf_new(len);
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
349 memcpy(ses.payload->data, buf_getptr(ses.readbuf, len), len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350 buf_incrlen(ses.payload, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
351 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
353 buf_free(ses.readbuf);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
354 ses.readbuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355 buf_setpos(ses.payload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 ses.recvseq++;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
359 TRACE(("leave decrypt_packet"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
362 /* Checks the mac at the end of a decrypted readbuf.
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
364 static int checkmac() {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
366 unsigned char mac_bytes[MAX_MAC_LEN];
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
367 unsigned int mac_size, contents_len;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
369 mac_size = ses.keys->trans.algo_mac->hashsize;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
370 contents_len = ses.readbuf->len - mac_size;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
372 buf_setpos(ses.readbuf, 0);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
373 make_mac(ses.recvseq, &ses.keys->recv, ses.readbuf, contents_len, mac_bytes);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
374
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375 /* compare the hash */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
376 buf_setpos(ses.readbuf, contents_len);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
377 if (memcmp(mac_bytes, buf_getptr(ses.readbuf, mac_size), mac_size) != 0) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378 return DROPBEAR_FAILURE;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
380 return DROPBEAR_SUCCESS;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
382 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
383
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385 /* returns a pointer to a newly created buffer */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 static buffer* buf_decompress(buffer* buf, unsigned int len) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388 int result;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389 buffer * ret;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390 z_streamp zstream;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
392 zstream = ses.keys->recv.zstream;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
393 ret = buf_new(len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 zstream->avail_in = len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396 zstream->next_in = buf_getptr(buf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398 /* decompress the payload, incrementally resizing the output buffer */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
399 while (1) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 zstream->avail_out = ret->size - ret->pos;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402 zstream->next_out = buf_getwriteptr(ret, zstream->avail_out);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404 result = inflate(zstream, Z_SYNC_FLUSH);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406 buf_setlen(ret, ret->size - zstream->avail_out);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 buf_setpos(ret, ret->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
408
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409 if (result != Z_BUF_ERROR && result != Z_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410 dropbear_exit("zlib error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413 if (zstream->avail_in == 0 &&
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
414 (zstream->avail_out != 0 || result == Z_BUF_ERROR)) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415 /* we can only exit if avail_out hasn't all been used,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 * and there's no remaining input */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
417 return ret;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 if (zstream->avail_out == 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 buf_resize(ret, ret->size + ZLIB_DECOMPRESS_INCR);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
422 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
423 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
428 /* returns 1 if the packet is a valid type during kex (see 7.1 of rfc4253) */
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
429 static int packet_is_okay_kex(unsigned char type) {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
430 if (type >= SSH_MSG_USERAUTH_REQUEST) {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
431 return 0;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
432 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
433 if (type == SSH_MSG_SERVICE_REQUEST || type == SSH_MSG_SERVICE_ACCEPT) {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
434 return 0;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
435 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
436 if (type == SSH_MSG_KEXINIT) {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
437 /* XXX should this die horribly if !dataallowed ?? */
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
438 return 0;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
439 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
440 return 1;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
441 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
443 static void enqueue_reply_packet() {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
444 struct packetlist * new_item = NULL;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
445 new_item = m_malloc(sizeof(struct packetlist));
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
446 new_item->next = NULL;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
447
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
448 new_item->payload = buf_newcopy(ses.writepayload);
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
449 buf_setpos(ses.writepayload, 0);
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
450 buf_setlen(ses.writepayload, 0);
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
451
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
452 if (ses.reply_queue_tail) {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
453 ses.reply_queue_tail->next = new_item;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
454 } else {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
455 ses.reply_queue_head = new_item;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
456 }
456
f6c999ba31da Fix delayed packet queue handling
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
457 ses.reply_queue_tail = new_item;
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
458 TRACE(("leave enqueue_reply_packet"))
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
459 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
460
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
461 void maybe_flush_reply_queue() {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
462 struct packetlist *tmp_item = NULL, *curr_item = NULL;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
463 if (!ses.dataallowed)
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
464 {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
465 TRACE(("maybe_empty_reply_queue - no data allowed"))
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
466 return;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
467 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
468
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
469 for (curr_item = ses.reply_queue_head; curr_item; ) {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
470 CHECKCLEARTOWRITE();
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
471 buf_putbytes(ses.writepayload,
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
472 curr_item->payload->data, curr_item->payload->len);
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
473
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
474 buf_free(curr_item->payload);
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
475 tmp_item = curr_item;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
476 curr_item = curr_item->next;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
477 m_free(tmp_item);
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
478 encrypt_packet();
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
479 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
480 ses.reply_queue_head = ses.reply_queue_tail = NULL;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
481 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 /* encrypt the writepayload, putting into writebuf, ready for write_packet()
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 * to put on the wire */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485 void encrypt_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
487 unsigned char padlen;
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
488 unsigned char blocksize, mac_size;
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
489 buffer * writebuf; /* the packet which will go on the wire. This is
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
490 encrypted in-place. */
592
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
491 unsigned char packet_type;
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
492 unsigned int len, encrypt_buf_size;
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
493 unsigned char mac_bytes[MAX_MAC_LEN];
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
495 TRACE(("enter encrypt_packet()"))
592
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
496
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
497 buf_setpos(ses.writepayload, 0);
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
498 packet_type = buf_getbyte(ses.writepayload);
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
499 buf_setpos(ses.writepayload, 0);
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
500
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
501 TRACE(("encrypt_packet type is %d", packet_type))
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
502
623
895fbe068f2c Fix case where "-K 1" would cause a SSH_MSG_IGNORE packet to be sent
Matt Johnston <matt@ucc.asn.au>
parents: 605
diff changeset
503 if ((!ses.dataallowed && !packet_is_okay_kex(packet_type))
895fbe068f2c Fix case where "-K 1" would cause a SSH_MSG_IGNORE packet to be sent
Matt Johnston <matt@ucc.asn.au>
parents: 605
diff changeset
504 || ses.kexstate.sentnewkeys) {
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
505 /* During key exchange only particular packets are allowed.
592
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
506 Since this packet_type isn't OK we just enqueue it to send
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
507 after the KEX, see maybe_flush_reply_queue */
623
895fbe068f2c Fix case where "-K 1" would cause a SSH_MSG_IGNORE packet to be sent
Matt Johnston <matt@ucc.asn.au>
parents: 605
diff changeset
508
895fbe068f2c Fix case where "-K 1" would cause a SSH_MSG_IGNORE packet to be sent
Matt Johnston <matt@ucc.asn.au>
parents: 605
diff changeset
509 /* We also enqueue packets here when we have sent a MSG_NEWKEYS
895fbe068f2c Fix case where "-K 1" would cause a SSH_MSG_IGNORE packet to be sent
Matt Johnston <matt@ucc.asn.au>
parents: 605
diff changeset
510 * packet but are yet to received one. For simplicity we just switch
895fbe068f2c Fix case where "-K 1" would cause a SSH_MSG_IGNORE packet to be sent
Matt Johnston <matt@ucc.asn.au>
parents: 605
diff changeset
511 * over all the keys at once. This is the 'ses.kexstate.sentnewkeys'
895fbe068f2c Fix case where "-K 1" would cause a SSH_MSG_IGNORE packet to be sent
Matt Johnston <matt@ucc.asn.au>
parents: 605
diff changeset
512 * case. */
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
513 enqueue_reply_packet();
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
514 return;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
515 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
516
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
517 blocksize = ses.keys->trans.algo_crypt->blocksize;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
518 mac_size = ses.keys->trans.algo_mac->hashsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519
605
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
520 /* Encrypted packet len is payload+5. We need to then make sure
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
521 * there is enough space for padding or MIN_PACKET_LEN.
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
522 * Add extra 3 since we need at least 4 bytes of padding */
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
523 encrypt_buf_size = (ses.writepayload->len+4+1)
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
524 + MAX(MIN_PACKET_LEN, blocksize) + 3
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
525 /* add space for the MAC at the end */
592
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
526 + mac_size
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
527 #ifndef DISABLE_ZLIB
605
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
528 /* some extra in case 'compression' makes it larger */
592
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
529 + ZLIB_COMPRESS_INCR
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
530 #endif
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
531 /* and an extra cleartext (stripped before transmission) byte for the
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
532 * packet type */
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
533 + 1;
501
d58c478bd399 Add support for [email protected] delayed compression.
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
534
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
535 writebuf = buf_new(encrypt_buf_size);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
536 buf_setlen(writebuf, PACKET_PAYLOAD_OFF);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
537 buf_setpos(writebuf, PACKET_PAYLOAD_OFF);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
538
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
539 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 /* compression */
501
d58c478bd399 Add support for [email protected] delayed compression.
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
541 if (is_compress_trans()) {
605
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
542 int compress_delta;
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
543 buf_compress(writebuf, ses.writepayload, ses.writepayload->len);
605
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
544 compress_delta = (writebuf->len - PACKET_PAYLOAD_OFF) - ses.writepayload->len;
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
545
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
546 /* Handle the case where 'compress' increased the size. */
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
547 if (compress_delta > ZLIB_COMPRESS_INCR) {
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
548 buf_resize(writebuf, writebuf->size + compress_delta);
53c21d4ec98a - Don't allow setting memLevel since that doesn't work properly
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
549 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 } else
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552 {
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
553 memcpy(buf_getwriteptr(writebuf, ses.writepayload->len),
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 buf_getptr(ses.writepayload, ses.writepayload->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 ses.writepayload->len);
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
556 buf_incrwritepos(writebuf, ses.writepayload->len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 /* finished with payload */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 buf_setpos(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 buf_setlen(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563 /* length of padding - packet length must be a multiple of blocksize,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 * with a minimum of 4 bytes of padding */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
565 padlen = blocksize - (writebuf->len) % blocksize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 if (padlen < 4) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 padlen += blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569 /* check for min packet length */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
570 if (writebuf->len + padlen < MIN_PACKET_LEN) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 padlen += blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
574 buf_setpos(writebuf, 0);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 /* packet length excluding the packetlength uint32 */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
576 buf_putint(writebuf, writebuf->len + padlen - 4);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 /* padding len */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
579 buf_putbyte(writebuf, padlen);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 /* actual padding */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
581 buf_setpos(writebuf, writebuf->len);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
582 buf_incrlen(writebuf, padlen);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
583 genrandom(buf_getptr(writebuf, padlen), padlen);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
585 make_mac(ses.transseq, &ses.keys->trans, writebuf, writebuf->len, mac_bytes);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
587 /* do the actual encryption, in-place */
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
588 buf_setpos(writebuf, 0);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
589 /* encrypt it in-place*/
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
590 len = writebuf->len;
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
591 if (ses.keys->trans.crypt_mode->encrypt(
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
592 buf_getptr(writebuf, len),
528
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
593 buf_getwriteptr(writebuf, len),
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
594 len,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
595 &ses.keys->trans.cipher_state) != CRYPT_OK) {
594
a98a2138364a Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
596 dropbear_exit("Error encrypting");
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 }
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
598 buf_incrpos(writebuf, len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
600 /* stick the MAC on it */
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
601 buf_putbytes(writebuf, mac_bytes, mac_size);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602
592
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
603 /* The last byte of the buffer stores the cleartext packet_type. It is not
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
604 * transmitted but is used for transmit timeout purposes */
afb089e70892 Don't reset last_packet_time when we're transmitting SSH_MSG_IGNORE packets
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
605 buf_putbyte(writebuf, packet_type);
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
606 /* enqueue the packet for sending. It will get freed after transmission. */
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 buf_setpos(writebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608 enqueue(&ses.writequeue, (void*)writebuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610 /* Update counts */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611 ses.kexstate.datatrans += writebuf->len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
612 ses.transseq++;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
613
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
614 TRACE(("leave encrypt_packet()"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
616
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
617
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618 /* Create the packet mac, and append H(seqno|clearbuf) to the output */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
619 /* output_mac must have ses.keys->trans.algo_mac->hashsize bytes. */
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
620 static void make_mac(unsigned int seqno, const struct key_context_directional * key_state,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
621 buffer * clear_buf, unsigned int clear_len,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
622 unsigned char *output_mac) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623 unsigned char seqbuf[4];
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
624 unsigned long bufsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625 hmac_state hmac;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
627 TRACE(("enter writemac"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
629 if (key_state->algo_mac->hashsize > 0) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 /* calculate the mac */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631 if (hmac_init(&hmac,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
632 key_state->hash_index,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
633 key_state->mackey,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
634 key_state->algo_mac->keysize) != CRYPT_OK) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
635 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
637
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638 /* sequence number */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
639 STORE32H(seqno, seqbuf);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 if (hmac_process(&hmac, seqbuf, 4) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644 /* the actual contents */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
645 buf_setpos(clear_buf, 0);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
646 if (hmac_process(&hmac,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
647 buf_getptr(clear_buf, clear_len),
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
648 clear_len) != CRYPT_OK) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
651
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
652 bufsize = MAX_MAC_LEN;
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
653 if (hmac_done(&hmac, output_mac, &bufsize) != CRYPT_OK) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
654 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 }
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
657 TRACE(("leave writemac"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
658 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
661 /* compresses len bytes from src, outputting to dest (starting from the
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
662 * respective current positions. */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663 static void buf_compress(buffer * dest, buffer * src, unsigned int len) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
664
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
665 unsigned int endpos = src->pos + len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666 int result;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
668 TRACE(("enter buf_compress"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
670 while (1) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
672 ses.keys->trans.zstream->avail_in = endpos - src->pos;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
673 ses.keys->trans.zstream->next_in =
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
674 buf_getptr(src, ses.keys->trans.zstream->avail_in);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
675
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
676 ses.keys->trans.zstream->avail_out = dest->size - dest->pos;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
677 ses.keys->trans.zstream->next_out =
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
678 buf_getwriteptr(dest, ses.keys->trans.zstream->avail_out);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
679
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
680 result = deflate(ses.keys->trans.zstream, Z_SYNC_FLUSH);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
681
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
682 buf_setpos(src, endpos - ses.keys->trans.zstream->avail_in);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
683 buf_setlen(dest, dest->size - ses.keys->trans.zstream->avail_out);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
684 buf_setpos(dest, dest->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
685
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
686 if (result != Z_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
687 dropbear_exit("zlib error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
688 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
689
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
690 if (ses.keys->trans.zstream->avail_in == 0) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
691 break;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
692 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
693
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
694 dropbear_assert(ses.keys->trans.zstream->avail_out == 0);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
695
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
696 /* the buffer has been filled, we must extend. This only happens in
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
697 * unusual circumstances where the data grows in size after deflate(),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
698 * but it is possible */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
699 buf_resize(dest, dest->size + ZLIB_COMPRESS_INCR);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
700
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
701 }
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
702 TRACE(("leave buf_compress"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
703 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
704 #endif