annotate packet.c @ 532:c67c8c0c6c35

merge of 'e1c100e6366c5d607af08f4abdbb0f4281df4fa9' and 'fe8161b0698c9816b98f79e3cab2b9d59f2be71b'
author Matt Johnston <matt@ucc.asn.au>
date Thu, 26 Feb 2009 13:21:14 +0000
parents 164b7c2cd5df 22a0d8355c2c
children 805ae74ec024
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 static void read_packet_init();
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 static void writemac(buffer * outputbuffer, buffer * clearwritebuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 static int checkmac(buffer* hashbuf, buffer* readbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 #define ZLIB_COMPRESS_INCR 20 /* this is 12 bytes + 0.1% of 8000 bytes */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 #define ZLIB_DECOMPRESS_INCR 100
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 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
46 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
47 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
531
164b7c2cd5df disapproval of revision 'a101cbd046507cf723e6362a49196dbd4b924042'
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
49 /* non-blocking function writing out a current encrypted packet */
164b7c2cd5df disapproval of revision 'a101cbd046507cf723e6362a49196dbd4b924042'
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
50 void write_packet() {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 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
53 buffer * writebuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
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
55 TRACE(("enter write_packet"))
531
164b7c2cd5df disapproval of revision 'a101cbd046507cf723e6362a49196dbd4b924042'
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
56 dropbear_assert(!isempty(&ses.writequeue));
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 /* 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
59 writebuf = (buffer*)examine(&ses.writequeue);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 len = writebuf->len - writebuf->pos;
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
62 dropbear_assert(len > 0);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 /* 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
64 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
65
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 if (written < 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 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
68 TRACE(("leave writepacket: EINTR"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 dropbear_exit("error writing");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 }
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
74
513
a3748e54273c Idle timeout patch from Farrell Aultman. Needs testing, unsure if server
Matt Johnston <matt@ucc.asn.au>
parents: 502
diff changeset
75 ses.last_trx_packet_time = time(NULL);
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
76 ses.last_packet_time = time(NULL);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 if (written == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
79 ses.remoteclosed();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 if (written == len) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 /* 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
84 dequeue(&ses.writequeue);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 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
86 writebuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 /* 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
89 buf_incrpos(writebuf, written);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91
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
92 TRACE(("leave write_packet"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 /* 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
96 * 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
97 * full portion if possible */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 void read_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 unsigned int maxlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103
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
104 TRACE(("enter read_packet"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 blocksize = ses.keys->recv_algo_crypt->blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 if (ses.readbuf == NULL || ses.readbuf->len < blocksize) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 /* In the first blocksize of a packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 /* 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
111 * find the length of the whole packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 read_packet_init();
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 /* If we don't have the length of decryptreadbuf, we didn't read
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 * a whole blocksize and should exit */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 if (ses.decryptreadbuf->len == 0) {
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
117 TRACE(("leave read_packet: packetinit done"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 /* 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
123 * mightn't be any available (EAGAIN) */
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
124 dropbear_assert(ses.readbuf != NULL);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 maxlen = ses.readbuf->len - ses.readbuf->pos;
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
126 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
127
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 if (len == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
129 ses.remoteclosed();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 if (len < 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 if (errno == EINTR || errno == EAGAIN) {
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
134 TRACE(("leave read_packet: EINTR or EAGAIN"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 dropbear_exit("error reading: %s", strerror(errno));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 buf_incrpos(ses.readbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 if ((unsigned int)len == maxlen) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 /* The whole packet has been read */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 decrypt_packet();
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 /* The main select() loop process_packet() to
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 * handle the packet contents... */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 }
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
149 TRACE(("leave read_packet"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 /* 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
153 * length. Only called during the first BLOCKSIZE of a packet. */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 static void read_packet_init() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 unsigned int maxlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 unsigned char macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 blocksize = ses.keys->recv_algo_crypt->blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 macsize = ses.keys->recv_algo_mac->hashsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 if (ses.readbuf == NULL) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 /* start of a new packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 ses.readbuf = buf_new(INIT_READBUF);
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
168 dropbear_assert(ses.decryptreadbuf == NULL);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 ses.decryptreadbuf = buf_new(blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 maxlen = blocksize - ses.readbuf->pos;
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 rest of the packet if 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
175 len = 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
176 maxlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 if (len == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
178 ses.remoteclosed();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 if (len < 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 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
182 TRACE(("leave read_packet_init: EINTR"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 return;
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 dropbear_exit("error reading: %s", strerror(errno));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 buf_incrwritepos(ses.readbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 if ((unsigned int)len != maxlen) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 /* don't have enough bytes to determine length, get next time */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 /* 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
196 * 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
197 buf_setpos(ses.readbuf, 0);
502
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
198 if (ses.keys->recv_crypt_mode->decrypt(buf_getptr(ses.readbuf, blocksize),
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
199 buf_getwriteptr(ses.decryptreadbuf,blocksize),
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
200 blocksize,
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
201 &ses.keys->recv_cipher_state) != CRYPT_OK) {
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
202 dropbear_exit("error decrypting");
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 buf_setlen(ses.decryptreadbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 len = buf_getint(ses.decryptreadbuf) + 4 + macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 buf_setpos(ses.readbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 /* 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
210 if ((len > RECV_MAX_PACKET_LEN) ||
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 (len < MIN_PACKET_LEN + macsize) ||
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 ((len - macsize) % blocksize != 0)) {
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
213 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
214 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 buf_resize(ses.readbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 buf_setlen(ses.readbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218
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 /* handle the received packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 void decrypt_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 unsigned char macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 unsigned int padlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 unsigned int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228
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
229 TRACE(("enter decrypt_packet"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 blocksize = ses.keys->recv_algo_crypt->blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 macsize = ses.keys->recv_algo_mac->hashsize;
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 ses.kexstate.datarecv += ses.readbuf->len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 /* 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
236 buf_setpos(ses.readbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 buf_resize(ses.decryptreadbuf, ses.readbuf->len - macsize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 buf_setlen(ses.decryptreadbuf, ses.decryptreadbuf->size);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 buf_setpos(ses.decryptreadbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241
502
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
242 /* decrypt it */
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
243 len = ses.readbuf->len - macsize - ses.readbuf->pos;
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
244 if (ses.keys->recv_crypt_mode->decrypt(
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
245 buf_getptr(ses.readbuf, 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
246 buf_getwriteptr(ses.decryptreadbuf, 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
247 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
248 &ses.keys->recv_cipher_state) != CRYPT_OK) {
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
249 dropbear_exit("error decrypting");
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 }
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
251 buf_incrpos(ses.readbuf, 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
252 buf_incrwritepos(ses.decryptreadbuf, len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 /* check the hmac */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 buf_setpos(ses.readbuf, ses.readbuf->len - macsize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 if (checkmac(ses.readbuf, ses.decryptreadbuf) != DROPBEAR_SUCCESS) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 dropbear_exit("Integrity error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 /* readbuf no longer required */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 buf_free(ses.readbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 ses.readbuf = NULL;
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 /* get padding length */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 buf_setpos(ses.decryptreadbuf, PACKET_PADDING_OFF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 padlen = buf_getbyte(ses.decryptreadbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 /* payload length */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 /* - 4 - 1 is for LEN and PADLEN values */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 len = ses.decryptreadbuf->len - padlen - 4 - 1;
448
9c61e7af0156 Rearrange the channel buffer sizes into three neat use-editable values in
Matt Johnston <matt@ucc.asn.au>
parents: 426
diff changeset
271 if ((len > RECV_MAX_PAYLOAD_LEN) || (len < 1)) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 dropbear_exit("bad packet size");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 buf_setpos(ses.decryptreadbuf, PACKET_PAYLOAD_OFF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277 #ifndef DISABLE_ZLIB
501
d58c478bd399 Add support for [email protected] delayed compression.
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
278 if (is_compress_recv()) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 /* decompress */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 ses.payload = buf_decompress(ses.decryptreadbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 } else
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 /* copy payload */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 ses.payload = buf_new(len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 memcpy(ses.payload->data, buf_getptr(ses.decryptreadbuf, len), len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 buf_incrlen(ses.payload, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 buf_free(ses.decryptreadbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 ses.decryptreadbuf = NULL;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 buf_setpos(ses.payload, 0);
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 ses.recvseq++;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295
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
296 TRACE(("leave decrypt_packet"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 /* Checks the mac in hashbuf, for the data in readbuf.
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 static int checkmac(buffer* macbuf, buffer* sourcebuf) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
303 unsigned int macsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304 hmac_state hmac;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305 unsigned char tempbuf[MAX_MAC_LEN];
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
306 unsigned long bufsize;
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
307 unsigned int len;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 macsize = ses.keys->recv_algo_mac->hashsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 if (macsize == 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 return DROPBEAR_SUCCESS;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 /* calculate the mac */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 if (hmac_init(&hmac,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 find_hash(ses.keys->recv_algo_mac->hashdesc->name),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317 ses.keys->recvmackey,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318 ses.keys->recv_algo_mac->keysize)
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319 != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 /* sequence number */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 STORE32H(ses.recvseq, tempbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325 if (hmac_process(&hmac, tempbuf, 4) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 buf_setpos(sourcebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 len = sourcebuf->len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 if (hmac_process(&hmac, buf_getptr(sourcebuf, len), len) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
335 bufsize = sizeof(tempbuf);
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
336 if (hmac_done(&hmac, tempbuf, &bufsize) != CRYPT_OK) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
338 }
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 /* compare the hash */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341 if (memcmp(tempbuf, buf_getptr(macbuf, macsize), macsize) != 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 return DROPBEAR_FAILURE;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 return DROPBEAR_SUCCESS;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 }
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 /* 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
350 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
351
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352 int result;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 buffer * ret;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 z_streamp zstream;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356 zstream = ses.keys->recv_zstream;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 ret = buf_new(len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 zstream->avail_in = len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360 zstream->next_in = buf_getptr(buf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362 /* 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
363 while (1) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 zstream->avail_out = ret->size - ret->pos;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 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
367
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368 result = inflate(zstream, Z_SYNC_FLUSH);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 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
371 buf_setpos(ret, ret->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
372
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
373 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
374 dropbear_exit("zlib error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
376
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377 if (zstream->avail_in == 0 &&
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378 (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
379 /* 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
380 * and there's no remaining input */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381 return ret;
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 if (zstream->avail_out == 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385 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
386 }
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 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
392 /* 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
393 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
394 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
395 return 0;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
396 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
397 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
398 return 0;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
399 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
400 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
401 /* 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
402 return 0;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
403 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
404 return 1;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
405 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
407 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
408 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
409 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
410 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
411
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
412 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
413 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
414 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
415
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
416 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
417 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
418 } else {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
419 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
420 }
456
f6c999ba31da Fix delayed packet queue handling
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
421 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
422 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
423 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
424
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
425 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
426 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
427 if (!ses.dataallowed)
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
428 {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
429 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
430 return;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
431 }
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 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
434 CHECKCLEARTOWRITE();
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
435 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
436 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
437
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
438 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
439 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
440 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
441 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
442 encrypt_packet();
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
443 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
444 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
445 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 /* 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
448 * to put on the wire */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 void encrypt_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 unsigned char padlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 unsigned char blocksize, macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453 buffer * writebuf; /* the packet which will go on the wire */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454 buffer * clearwritebuf; /* unencrypted, possibly compressed */
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
455 unsigned char type;
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
456 unsigned int len;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
458 type = ses.writepayload->data[0];
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
459 TRACE(("enter encrypt_packet()"))
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
460 TRACE(("encrypt_packet type is %d", type))
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
461
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
462 if (!ses.dataallowed && !packet_is_okay_kex(type)) {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
463 /* During key exchange only particular packets are allowed.
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
464 Since this type isn't OK we just enqueue it to send
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
465 after the KEX, see 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
466 enqueue_reply_packet();
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
467 return;
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
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470 blocksize = ses.keys->trans_algo_crypt->blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471 macsize = ses.keys->trans_algo_mac->hashsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473 /* Encrypted packet len is payload+5, then worst case is if we are 3 away
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 * from a blocksize multiple. In which case we need to pad to the
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 * multiple, then add another blocksize (or MIN_PACKET_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
476 len = (ses.writepayload->len+4+1) + MIN_PACKET_LEN + 3;
501
d58c478bd399 Add support for [email protected] delayed compression.
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
477
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 #ifndef DISABLE_ZLIB
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
479 len += ZLIB_COMPRESS_INCR; /* bit of a kludge, but we can't know len*/
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480 #endif
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
481 clearwritebuf = buf_new(len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 buf_setlen(clearwritebuf, PACKET_PAYLOAD_OFF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 buf_setpos(clearwritebuf, PACKET_PAYLOAD_OFF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485 buf_setpos(ses.writepayload, 0);
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 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 /* compression */
501
d58c478bd399 Add support for [email protected] delayed compression.
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
489 if (is_compress_trans()) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490 buf_compress(clearwritebuf, ses.writepayload, ses.writepayload->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 } else
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 memcpy(buf_getwriteptr(clearwritebuf, ses.writepayload->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 buf_getptr(ses.writepayload, ses.writepayload->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 ses.writepayload->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 buf_incrwritepos(clearwritebuf, ses.writepayload->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 /* finished with payload */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501 buf_setpos(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 buf_setlen(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
503
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 /* 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
505 * with a minimum of 4 bytes of padding */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 padlen = blocksize - (clearwritebuf->len) % blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 if (padlen < 4) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508 padlen += blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510 /* check for min packet length */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 if (clearwritebuf->len + padlen < MIN_PACKET_LEN) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512 padlen += blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515 buf_setpos(clearwritebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516 /* packet length excluding the packetlength uint32 */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517 buf_putint(clearwritebuf, clearwritebuf->len + padlen - 4);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519 /* padding len */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520 buf_putbyte(clearwritebuf, padlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521 /* actual padding */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 buf_setpos(clearwritebuf, clearwritebuf->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 buf_incrlen(clearwritebuf, padlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 genrandom(buf_getptr(clearwritebuf, padlen), padlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
526 /* do the actual encryption */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527 buf_setpos(clearwritebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528 /* create a new writebuffer, this is freed when it has been put on the
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529 * wire by writepacket() */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530 writebuf = buf_new(clearwritebuf->len + macsize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531
502
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
532 /* encrypt it */
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
533 len = clearwritebuf->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
534 if (ses.keys->trans_crypt_mode->encrypt(
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
535 buf_getptr(clearwritebuf, 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
536 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
537 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
538 &ses.keys->trans_cipher_state) != CRYPT_OK) {
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
539 dropbear_exit("error encrypting");
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 }
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
541 buf_incrpos(clearwritebuf, 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
542 buf_incrwritepos(writebuf, len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 /* now add a hmac and we're done */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 writemac(writebuf, clearwritebuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 /* clearwritebuf is finished with */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 buf_free(clearwritebuf);
70
b0316ce64e4b Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents: 33
diff changeset
549 clearwritebuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 /* enqueue the packet for sending */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552 buf_setpos(writebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553 enqueue(&ses.writequeue, (void*)writebuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 /* Update counts */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556 ses.kexstate.datatrans += writebuf->len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557 ses.transseq++;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558
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
559 TRACE(("leave encrypt_packet()"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561
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 /* Create the packet mac, and append H(seqno|clearbuf) to the output */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 static void writemac(buffer * outputbuffer, buffer * clearwritebuf) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
566 unsigned int macsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 unsigned char seqbuf[4];
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
568 unsigned char tempbuf[MAX_MAC_LEN];
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
569 unsigned long bufsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 hmac_state hmac;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571
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
572 TRACE(("enter writemac"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573
232
cbf1165d9386 trans, not recv for macsize
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
574 macsize = ses.keys->trans_algo_mac->hashsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 if (macsize > 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 /* calculate the mac */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 if (hmac_init(&hmac,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 find_hash(ses.keys->trans_algo_mac->hashdesc->name),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579 ses.keys->transmackey,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 ses.keys->trans_algo_mac->keysize) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584 /* sequence number */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585 STORE32H(ses.transseq, seqbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 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
587 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590 /* the actual contents */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591 buf_setpos(clearwritebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592 if (hmac_process(&hmac,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
593 buf_getptr(clearwritebuf,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 clearwritebuf->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
595 clearwritebuf->len) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
599 bufsize = sizeof(tempbuf);
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
600 if (hmac_done(&hmac, tempbuf, &bufsize)
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601 != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
603 }
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
604 buf_putbytes(outputbuffer, tempbuf, macsize);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
605 }
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
606 TRACE(("leave writemac"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610 /* 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
611 * respective current positions. */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
612 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
613
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
614 unsigned int endpos = src->pos + len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615 int result;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
616
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
617 TRACE(("enter buf_compress"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
619 while (1) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
621 ses.keys->trans_zstream->avail_in = endpos - src->pos;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
622 ses.keys->trans_zstream->next_in =
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623 buf_getptr(src, ses.keys->trans_zstream->avail_in);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
624
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625 ses.keys->trans_zstream->avail_out = dest->size - dest->pos;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626 ses.keys->trans_zstream->next_out =
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 buf_getwriteptr(dest, ses.keys->trans_zstream->avail_out);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
629 result = deflate(ses.keys->trans_zstream, Z_SYNC_FLUSH);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631 buf_setpos(src, endpos - ses.keys->trans_zstream->avail_in);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
632 buf_setlen(dest, dest->size - ses.keys->trans_zstream->avail_out);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
633 buf_setpos(dest, dest->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
634
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
635 if (result != Z_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 dropbear_exit("zlib error");
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639 if (ses.keys->trans_zstream->avail_in == 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 break;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
643 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
644
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
645 /* 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
646 * 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
647 * but it is possible */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648 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
649
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 }
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
651 TRACE(("leave buf_compress"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653 #endif