annotate packet.c @ 534:0431915df79f

- Get rid of decryptreadbuf, just decrypt in-place with readbuf - Share make_mac function for both packet creation and validation - Split recv/trans parts of key_context into their own structures
author Matt Johnston <matt@ucc.asn.au>
date Sun, 01 Mar 2009 16:15:57 +0000
parents 805ae74ec024
children 21490eea261d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 * Dropbear - a SSH2 server
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 *
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * Copyright (c) 2002,2003 Matt Johnston
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * All rights reserved.
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 *
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * of this software and associated documentation files (the "Software"), to deal
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * in the Software without restriction, including without limitation the rights
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * copies of the Software, and to permit persons to whom the Software is
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * furnished to do so, subject to the following conditions:
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 *
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * The above copyright notice and this permission notice shall be included in
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * all copies or substantial portions of the Software.
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 *
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * SOFTWARE. */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #include "includes.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #include "packet.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #include "session.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #include "dbutil.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 #include "ssh.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #include "algo.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 #include "buffer.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 #include "kex.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #include "random.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #include "service.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 #include "auth.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #include "channel.h"
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
38 static int read_packet_init();
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
39 static void make_mac(unsigned int seqno, const struct key_context_directional * key_state,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
40 buffer * clear_buf, unsigned int clear_len,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
41 unsigned char *output_mac);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
42 static int checkmac();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 #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
45 #define ZLIB_DECOMPRESS_INCR 100
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 static buffer* buf_decompress(buffer* buf, unsigned int len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 static void buf_compress(buffer * dest, buffer * src, unsigned int len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
531
164b7c2cd5df disapproval of revision 'a101cbd046507cf723e6362a49196dbd4b924042'
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
51 /* non-blocking function writing out a current encrypted packet */
164b7c2cd5df disapproval of revision 'a101cbd046507cf723e6362a49196dbd4b924042'
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
52 void write_packet() {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 int len, written;
70
b0316ce64e4b Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents: 33
diff changeset
55 buffer * writebuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
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
57 TRACE(("enter write_packet"))
531
164b7c2cd5df disapproval of revision 'a101cbd046507cf723e6362a49196dbd4b924042'
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
58 dropbear_assert(!isempty(&ses.writequeue));
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 /* 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
61 writebuf = (buffer*)examine(&ses.writequeue);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 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
64 dropbear_assert(len > 0);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 /* 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
66 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
67
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 if (written < 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 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
70 TRACE(("leave writepacket: EINTR"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 dropbear_exit("error writing");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
76
513
a3748e54273c Idle timeout patch from Farrell Aultman. Needs testing, unsure if server
Matt Johnston <matt@ucc.asn.au>
parents: 502
diff changeset
77 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
78 ses.last_packet_time = time(NULL);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 if (written == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
81 ses.remoteclosed();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 if (written == len) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 /* 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
86 dequeue(&ses.writequeue);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 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
88 writebuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 /* 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
91 buf_incrpos(writebuf, written);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93
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
94 TRACE(("leave write_packet"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 /* 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
98 * 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
99 * full portion if possible */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 void read_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 unsigned int maxlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
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
106 TRACE(("enter read_packet"))
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
107 blocksize = ses.keys->recv.algo_crypt->blocksize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 if (ses.readbuf == NULL || ses.readbuf->len < blocksize) {
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
110 int ret;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 /* In the first blocksize of a packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 /* 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
114 * find the length of the whole packet */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
115 ret = read_packet_init();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
117 if (ret == DROPBEAR_FAILURE) {
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
118 /* didn't read enough to determine the length */
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
119 TRACE(("leave read_packet: packetinit done"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 return;
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 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 /* 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
125 * mightn't be any available (EAGAIN) */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 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
127 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
128
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 if (len == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
130 ses.remoteclosed();
27
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 if (len < 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 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
135 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
136 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 dropbear_exit("error reading: %s", strerror(errno));
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 buf_incrpos(ses.readbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 if ((unsigned int)len == maxlen) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 /* The whole packet has been read */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 decrypt_packet();
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 /* The main select() loop process_packet() to
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 * handle the packet contents... */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 }
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
150 TRACE(("leave read_packet"))
27
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 /* 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
154 * length. Only called during the first BLOCKSIZE of a packet. */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
155 /* Returns DROPBEAR_SUCCESS if the length is determined,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
156 * DROPBEAR_FAILURE otherwise */
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
157 static int read_packet_init() {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 unsigned int maxlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 unsigned char macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
165 blocksize = ses.keys->recv.algo_crypt->blocksize;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
166 macsize = ses.keys->recv.algo_mac->hashsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 if (ses.readbuf == NULL) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 /* start of a new packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 ses.readbuf = buf_new(INIT_READBUF);
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 maxlen = blocksize - ses.readbuf->pos;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 /* 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
176 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
177 maxlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 if (len == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
179 ses.remoteclosed();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 if (len < 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 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
183 TRACE(("leave read_packet_init: EINTR"))
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
184 return DROPBEAR_FAILURE;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 dropbear_exit("error reading: %s", strerror(errno));
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 buf_incrwritepos(ses.readbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 if ((unsigned int)len != maxlen) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 /* don't have enough bytes to determine length, get next time */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
193 return DROPBEAR_FAILURE;
27
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 /* 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
197 * 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
198 buf_setpos(ses.readbuf, 0);
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
199 if (ses.keys->recv.crypt_mode->decrypt(buf_getptr(ses.readbuf, blocksize),
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
200 buf_getwriteptr(ses.readbuf, blocksize),
502
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
201 blocksize,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
202 &ses.keys->recv.cipher_state) != CRYPT_OK) {
502
43bbe17d6ba0 - Add Counter Mode support
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
203 dropbear_exit("error decrypting");
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 }
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
205 len = buf_getint(ses.readbuf) + 4 + macsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
207 TRACE(("packet size is %d, block %d mac %d", len, blocksize, macsize))
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
208
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 /* 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
211 if ((len > RECV_MAX_PACKET_LEN) ||
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 (len < MIN_PACKET_LEN + macsize) ||
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 ((len - macsize) % blocksize != 0)) {
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
214 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
215 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
217 if (len > ses.readbuf->size) {
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
218 buf_resize(ses.readbuf, len);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
219 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 buf_setlen(ses.readbuf, len);
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
221 buf_setpos(ses.readbuf, blocksize);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
222 return DROPBEAR_SUCCESS;
27
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 /* handle the received packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 void decrypt_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 unsigned char macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 unsigned int padlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 unsigned int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232
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
233 TRACE(("enter decrypt_packet"))
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
234 blocksize = ses.keys->recv.algo_crypt->blocksize;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
235 macsize = ses.keys->recv.algo_mac->hashsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 ses.kexstate.datarecv += ses.readbuf->len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 /* 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
240 buf_setpos(ses.readbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
242 /* decrypt it in-place */
528
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
243 len = ses.readbuf->len - macsize - ses.readbuf->pos;
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
244 if (ses.keys->recv.crypt_mode->decrypt(
528
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
245 buf_getptr(ses.readbuf, len),
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
246 buf_getwriteptr(ses.readbuf, len),
528
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
247 len,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
248 &ses.keys->recv.cipher_state) != CRYPT_OK) {
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
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);
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
252
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
253 printhex("readbuf decrypted", ses.readbuf->data, ses.readbuf->len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 /* check the hmac */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
256 if (checkmac() != DROPBEAR_SUCCESS) {
27
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 /* get padding length */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
261 buf_setpos(ses.readbuf, PACKET_PADDING_OFF);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
262 padlen = buf_getbyte(ses.readbuf);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 /* payload length */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 /* - 4 - 1 is for LEN and PADLEN values */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
266 len = ses.readbuf->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
267 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
268 dropbear_exit("bad packet size");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
271 buf_setpos(ses.readbuf, PACKET_PAYLOAD_OFF);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 #ifndef DISABLE_ZLIB
501
d58c478bd399 Add support for [email protected] delayed compression.
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
274 if (is_compress_recv()) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 /* decompress */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
276 ses.payload = buf_decompress(ses.readbuf, len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277 } else
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 /* copy payload */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 ses.payload = buf_new(len);
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
282 memcpy(ses.payload->data, buf_getptr(ses.readbuf, len), len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 buf_incrlen(ses.payload, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
286 buf_free(ses.readbuf);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
287 ses.readbuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 buf_setpos(ses.payload, 0);
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 ses.recvseq++;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291
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
292 TRACE(("leave decrypt_packet"))
27
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
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
295 /* Checks the mac at the end of a decrypted readbuf.
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
297 static int checkmac() {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
299 unsigned char mac_bytes[MAX_MAC_LEN];
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
300 unsigned int mac_size, contents_len;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
302 mac_size = ses.keys->trans.algo_mac->hashsize;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
303 contents_len = ses.readbuf->len - mac_size;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
305 buf_setpos(ses.readbuf, 0);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
306 make_mac(ses.recvseq, &ses.keys->recv, ses.readbuf, contents_len, mac_bytes);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 /* compare the hash */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
309 buf_setpos(ses.readbuf, contents_len);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
310 if (memcmp(mac_bytes, buf_getptr(ses.readbuf, mac_size), mac_size) != 0) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 return DROPBEAR_FAILURE;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 return DROPBEAR_SUCCESS;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318 /* 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
319 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
320
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321 int result;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 buffer * ret;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 z_streamp zstream;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
325 zstream = ses.keys->recv.zstream;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 ret = buf_new(len);
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 zstream->avail_in = len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 zstream->next_in = buf_getptr(buf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 /* 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
332 while (1) {
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 zstream->avail_out = ret->size - ret->pos;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
335 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
336
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 result = inflate(zstream, Z_SYNC_FLUSH);
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 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
340 buf_setpos(ret, ret->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 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
343 dropbear_exit("zlib error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 }
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 if (zstream->avail_in == 0 &&
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 (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
348 /* 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
349 * and there's no remaining input */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350 return ret;
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 if (zstream->avail_out == 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 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
355 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
361 /* 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
362 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
363 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
364 return 0;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
365 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
366 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
367 return 0;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
368 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
369 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
370 /* 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
371 return 0;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
372 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
373 return 1;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
374 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
376 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
377 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
378 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
379 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
380
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
381 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
382 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
383 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
384
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
385 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
386 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
387 } else {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
388 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
389 }
456
f6c999ba31da Fix delayed packet queue handling
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
390 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
391 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
392 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
393
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
394 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
395 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
396 if (!ses.dataallowed)
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
397 {
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
398 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
399 return;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
400 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
401
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
402 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
403 CHECKCLEARTOWRITE();
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
404 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
405 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
406
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
407 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
408 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
409 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
410 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
411 encrypt_packet();
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
412 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
413 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
414 }
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 /* 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
417 * to put on the wire */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418 void encrypt_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 unsigned char padlen;
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
421 unsigned char blocksize, mac_size;
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
422 buffer * writebuf; /* the packet which will go on the wire. This is
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
423 encrypted in-place. */
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
424 unsigned char type;
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
425 unsigned int len, encrypt_buf_size;
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
426 unsigned char mac_bytes[MAX_MAC_LEN];
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
428 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
429 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
430 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
431
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
432 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
433 /* 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
434 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
435 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
436 enqueue_reply_packet();
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
437 return;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
438 }
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
439
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
440 blocksize = ses.keys->trans.algo_crypt->blocksize;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
441 mac_size = ses.keys->trans.algo_mac->hashsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 /* 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
444 * 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
445 * multiple, then add another blocksize (or MIN_PACKET_LEN) */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
446 encrypt_buf_size = (ses.writepayload->len+4+1) + MIN_PACKET_LEN + 3;
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
447 /* add space for the MAC at the end */
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
448 encrypt_buf_size += mac_size;
501
d58c478bd399 Add support for [email protected] delayed compression.
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
449
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 #ifndef DISABLE_ZLIB
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
451 encrypt_buf_size += 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
452 #endif
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
453 writebuf = buf_new(encrypt_buf_size);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
454 buf_setlen(writebuf, PACKET_PAYLOAD_OFF);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
455 buf_setpos(writebuf, PACKET_PAYLOAD_OFF);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457 buf_setpos(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
460 /* compression */
501
d58c478bd399 Add support for [email protected] delayed compression.
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
461 if (is_compress_trans()) {
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
462 buf_compress(writebuf, ses.writepayload, ses.writepayload->len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463 } else
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
465 {
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
466 memcpy(buf_getwriteptr(writebuf, ses.writepayload->len),
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467 buf_getptr(ses.writepayload, ses.writepayload->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 ses.writepayload->len);
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
469 buf_incrwritepos(writebuf, ses.writepayload->len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 /* finished with payload */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473 buf_setpos(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 buf_setlen(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476 /* 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
477 * with a minimum of 4 bytes of padding */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
478 padlen = blocksize - (writebuf->len) % blocksize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479 if (padlen < 4) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480 padlen += blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 /* check for min packet length */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
483 if (writebuf->len + padlen < MIN_PACKET_LEN) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 padlen += blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
487 buf_setpos(writebuf, 0);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 /* packet length excluding the packetlength uint32 */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
489 buf_putint(writebuf, writebuf->len + padlen - 4);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 /* padding len */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
492 buf_putbyte(writebuf, padlen);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 /* actual padding */
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
494 buf_setpos(writebuf, writebuf->len);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
495 buf_incrlen(writebuf, padlen);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
496 genrandom(buf_getptr(writebuf, padlen), padlen);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
498 make_mac(ses.transseq, &ses.keys->trans, writebuf, writebuf->len, mac_bytes);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
500 /* do the actual encryption, in-place */
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
501 buf_setpos(writebuf, 0);
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
502 /* encrypt it in-place*/
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
503 len = writebuf->len;
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
504 if (ses.keys->trans.crypt_mode->encrypt(
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
505 buf_getptr(writebuf, len),
528
378a6389f88e - Don't be dumb and encrypt/decrypt in a while() loop - why did I do this??
Matt Johnston <matt@ucc.asn.au>
parents: 513
diff changeset
506 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
507 len,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
508 &ses.keys->trans.cipher_state) != CRYPT_OK) {
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
509 dropbear_exit("error encrypting");
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510 }
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
511 buf_incrpos(writebuf, len);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
513 /* stick the MAC on it */
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
514 buf_putbytes(writebuf, mac_bytes, mac_size);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
516 /* enqueue the packet for sending. It will get freed after transmission. */
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517 buf_setpos(writebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518 enqueue(&ses.writequeue, (void*)writebuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520 /* Update counts */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521 ses.kexstate.datatrans += writebuf->len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 ses.transseq++;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523
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
524 TRACE(("leave encrypt_packet()"))
27
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528 /* Create the packet mac, and append H(seqno|clearbuf) to the output */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
529 /* output_mac must have ses.keys->trans.algo_mac->hashsize bytes. */
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
530 static void make_mac(unsigned int seqno, const struct key_context_directional * key_state,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
531 buffer * clear_buf, unsigned int clear_len,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
532 unsigned char *output_mac) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 unsigned char seqbuf[4];
228
5e4110bb753a - Fixed twofish algorithm naming so it actually works.
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
534 unsigned long bufsize;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 hmac_state hmac;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536
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
537 TRACE(("enter writemac"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
538
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
539 if (key_state->algo_mac->hashsize > 0) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 /* calculate the mac */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
541 if (hmac_init(&hmac,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
542 key_state->hash_index,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
543 key_state->mackey,
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
544 key_state->algo_mac->keysize) != CRYPT_OK) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 dropbear_exit("HMAC error");
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 /* sequence number */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
549 STORE32H(seqno, seqbuf);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 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
551 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 /* the actual contents */
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
555 buf_setpos(clear_buf, 0);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556 if (hmac_process(&hmac,
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
557 buf_getptr(clear_buf, clear_len),
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
558 clear_len) != CRYPT_OK) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 dropbear_exit("HMAC error");
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
533
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
562 bufsize = MAX_MAC_LEN;
805ae74ec024 Encrypt in-place, avoid an extra malloc
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
563 if (hmac_done(&hmac, output_mac, &bufsize) != CRYPT_OK) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 }
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
567 TRACE(("leave writemac"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 /* 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
572 * respective current positions. */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 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
574
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 unsigned int endpos = src->pos + len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 int result;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577
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
578 TRACE(("enter buf_compress"))
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 while (1) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
582 ses.keys->trans.zstream->avail_in = endpos - src->pos;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
583 ses.keys->trans.zstream->next_in =
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
584 buf_getptr(src, ses.keys->trans.zstream->avail_in);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
586 ses.keys->trans.zstream->avail_out = dest->size - dest->pos;
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
587 ses.keys->trans.zstream->next_out =
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
588 buf_getwriteptr(dest, ses.keys->trans.zstream->avail_out);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
590 result = deflate(ses.keys->trans.zstream, Z_SYNC_FLUSH);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
592 buf_setpos(src, endpos - ses.keys->trans.zstream->avail_in);
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
593 buf_setlen(dest, dest->size - ses.keys->trans.zstream->avail_out);
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 buf_setpos(dest, dest->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
595
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596 if (result != Z_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 dropbear_exit("zlib error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
600 if (ses.keys->trans.zstream->avail_in == 0) {
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601 break;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
603
534
0431915df79f - Get rid of decryptreadbuf, just decrypt in-place with readbuf
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
604 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
605
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
606 /* 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
607 * 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
608 * but it is possible */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609 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
610
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611 }
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
612 TRACE(("leave buf_compress"))
27
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 #endif