annotate packet.c @ 90:c2ac796b130e DROPBEAR_0.44test2

merge of 00b67a11e33c3ed390556805ed6d1078528bee70 and 42c7bdf484b16e279a0f68604a4346d8fa5ed70c
author Matt Johnston <matt@ucc.asn.au>
date Sat, 04 Sep 2004 14:19:17 +0000
parents b0316ce64e4b
children 0cfba3034be5
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 /* non-blocking function writing out a current encrypted packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 void write_packet() {
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 TRACE(("enter write_packet"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 assert(!isempty(&ses.writequeue));
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;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 assert(len > 0);
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 */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 written = write(ses.sock, buf_getptr(writebuf, len), len);
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) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 TRACE(("leave writepacket: EINTR"));
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 }
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 if (written == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
76 ses.remoteclosed();
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 if (written == len) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 /* 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
81 dequeue(&ses.writequeue);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 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
83 writebuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 /* 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
86 buf_incrpos(writebuf, written);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 TRACE(("leave write_packet"));
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 /* 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
93 * 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
94 * full portion if possible */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 void read_packet() {
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 int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 unsigned int maxlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 TRACE(("enter read_packet"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 blocksize = ses.keys->recv_algo_crypt->blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 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
105 /* In the first blocksize of a packet */
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 /* 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
108 * find the length of the whole packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 read_packet_init();
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 /* 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
112 * a whole blocksize and should exit */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 if (ses.decryptreadbuf->len == 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 TRACE(("leave read_packet: packetinit done"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 /* 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
120 * mightn't be any available (EAGAIN) */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 assert(ses.readbuf != NULL);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 maxlen = ses.readbuf->len - ses.readbuf->pos;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 len = read(ses.sock, buf_getptr(ses.readbuf, maxlen), maxlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 if (len == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
126 ses.remoteclosed();
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 if (len < 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 if (errno == EINTR || errno == EAGAIN) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 TRACE(("leave read_packet: EINTR or EAGAIN"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 dropbear_exit("error reading: %s", strerror(errno));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 buf_incrpos(ses.readbuf, len);
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 if ((unsigned int)len == maxlen) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 /* The whole packet has been read */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 decrypt_packet();
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 /* The main select() loop process_packet() to
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 * handle the packet contents... */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 TRACE(("leave read_packet"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 /* 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
150 * 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
151 static void read_packet_init() {
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 unsigned int maxlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 int len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 unsigned char blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 unsigned char macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 blocksize = ses.keys->recv_algo_crypt->blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 macsize = ses.keys->recv_algo_mac->hashsize;
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 if (ses.readbuf == NULL) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 /* start of a new packet */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 ses.readbuf = buf_new(INIT_READBUF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 assert(ses.decryptreadbuf == NULL);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 ses.decryptreadbuf = buf_new(blocksize);
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 maxlen = blocksize - ses.readbuf->pos;
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 /* read the rest of the packet if possible */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 len = read(ses.sock, buf_getwriteptr(ses.readbuf, maxlen),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 maxlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 if (len == 0) {
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
175 ses.remoteclosed();
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 if (len < 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 if (errno == EINTR) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 TRACE(("leave read_packet_init: EINTR"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 return;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 dropbear_exit("error reading: %s", strerror(errno));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 buf_incrwritepos(ses.readbuf, len);
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 if ((unsigned int)len != maxlen) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 /* 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
189 return;
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 /* 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
193 * 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
194 buf_setpos(ses.readbuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 if (ses.keys->recv_algo_crypt->cipherdesc == NULL) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 /* copy it */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 memcpy(buf_getwriteptr(ses.decryptreadbuf, blocksize),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 buf_getptr(ses.readbuf, blocksize),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 /* decrypt it */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 if (cbc_decrypt(buf_getptr(ses.readbuf, blocksize),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 buf_getwriteptr(ses.decryptreadbuf,blocksize),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 &ses.keys->recv_symmetric_struct) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 dropbear_exit("error decrypting");
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 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 buf_setlen(ses.decryptreadbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 len = buf_getint(ses.decryptreadbuf) + 4 + macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 buf_setpos(ses.readbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 /* check packet length */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 if ((len > MAX_PACKET_LEN) ||
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 (len < MIN_PACKET_LEN + macsize) ||
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 ((len - macsize) % blocksize != 0)) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 dropbear_exit("bad packet size");
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 buf_resize(ses.readbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 buf_setlen(ses.readbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 TRACE(("enter decrypt_packet"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 blocksize = ses.keys->recv_algo_crypt->blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 macsize = ses.keys->recv_algo_mac->hashsize;
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 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
243 buf_setlen(ses.decryptreadbuf, ses.decryptreadbuf->size);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 buf_setpos(ses.decryptreadbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 /* decrypt if encryption is set, memcpy otherwise */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 if (ses.keys->recv_algo_crypt->cipherdesc == NULL) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 /* copy it */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 len = ses.readbuf->len - macsize - blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 memcpy(buf_getwriteptr(ses.decryptreadbuf, len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 buf_getptr(ses.readbuf, len), len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 /* decrypt */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 while (ses.readbuf->pos < ses.readbuf->len - macsize) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 if (cbc_decrypt(buf_getptr(ses.readbuf, blocksize),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 buf_getwriteptr(ses.decryptreadbuf, blocksize),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 &ses.keys->recv_symmetric_struct) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 dropbear_exit("error decrypting");
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 buf_incrpos(ses.readbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 buf_incrwritepos(ses.decryptreadbuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 /* check the hmac */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 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
267 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
268 dropbear_exit("Integrity error");
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 /* readbuf no longer required */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 buf_free(ses.readbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 ses.readbuf = NULL;
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 /* get padding length */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 buf_setpos(ses.decryptreadbuf, PACKET_PADDING_OFF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277 padlen = buf_getbyte(ses.decryptreadbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 /* payload length */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 /* - 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
281 len = ses.decryptreadbuf->len - padlen - 4 - 1;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 if ((len > MAX_PAYLOAD_LEN) || (len < 1)) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 dropbear_exit("bad packet size");
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 buf_setpos(ses.decryptreadbuf, PACKET_PAYLOAD_OFF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 if (ses.keys->recv_algo_comp == DROPBEAR_COMP_ZLIB) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 /* decompress */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 ses.payload = buf_decompress(ses.decryptreadbuf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293 } else
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 /* copy payload */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 ses.payload = buf_new(len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 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
299 buf_incrlen(ses.payload, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 buf_free(ses.decryptreadbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 ses.decryptreadbuf = NULL;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304 buf_setpos(ses.payload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 ses.recvseq++;
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 TRACE(("leave decrypt_packet"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 /* 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
312 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 static int checkmac(buffer* macbuf, buffer* sourcebuf) {
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 unsigned char macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 hmac_state hmac;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317 unsigned char tempbuf[MAX_MAC_LEN];
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318 unsigned long hashsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319 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 macsize = ses.keys->recv_algo_mac->hashsize;
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 if (macsize == 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 return DROPBEAR_SUCCESS;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 /* calculate the mac */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328 if (hmac_init(&hmac,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 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
330 ses.keys->recvmackey,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 ses.keys->recv_algo_mac->keysize)
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
335
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 /* sequence number */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 STORE32H(ses.recvseq, tempbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
338 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
339 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340 }
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 buf_setpos(sourcebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343 len = sourcebuf->len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 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
345 dropbear_exit("HMAC error");
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 hashsize = sizeof(tempbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 if (hmac_done(&hmac, tempbuf, &hashsize) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350 dropbear_exit("HMAC error");
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 /* compare the hash */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 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
355 return DROPBEAR_FAILURE;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 return DROPBEAR_SUCCESS;
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 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362 /* 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
363 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
364
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 int result;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 buffer * ret;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367 z_streamp zstream;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369 zstream = ses.keys->recv_zstream;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 ret = buf_new(len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
372 zstream->avail_in = len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
373 zstream->next_in = buf_getptr(buf, len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
374
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375 /* 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
376 while (1) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378 zstream->avail_out = ret->size - ret->pos;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379 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
380
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381 result = inflate(zstream, Z_SYNC_FLUSH);
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 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
384 buf_setpos(ret, ret->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 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
387 dropbear_exit("zlib error");
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390 if (zstream->avail_in == 0 &&
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391 (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
392 /* 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
393 * and there's no remaining input */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394 return ret;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 if (zstream->avail_out == 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398 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
399 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 /* 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
408 * to put on the wire */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409 void encrypt_packet() {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411 unsigned char padlen;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412 unsigned char blocksize, macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413 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
414 buffer * clearwritebuf; /* unencrypted, possibly compressed */
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 TRACE(("enter encrypt_packet()"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
417 TRACE(("encrypt_packet type is %d", ses.writepayload->data[0]));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418 blocksize = ses.keys->trans_algo_crypt->blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419 macsize = ses.keys->trans_algo_mac->hashsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 /* 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
422 * 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
423 * multiple, then add another blocksize (or MIN_PACKET_LEN) */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 clearwritebuf = buf_new((ses.writepayload->len+4+1) + MIN_PACKET_LEN + 3
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426 + ZLIB_COMPRESS_INCR /* bit of a kludge, but we can't know len*/
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
428 );
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
429 buf_setlen(clearwritebuf, PACKET_PAYLOAD_OFF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
430 buf_setpos(clearwritebuf, PACKET_PAYLOAD_OFF);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
431
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
432 buf_setpos(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
434 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
435 /* compression */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436 if (ses.keys->trans_algo_comp == DROPBEAR_COMP_ZLIB) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
437 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
438 } else
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
439 #endif
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
440 {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
441 memcpy(buf_getwriteptr(clearwritebuf, ses.writepayload->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442 buf_getptr(ses.writepayload, ses.writepayload->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 ses.writepayload->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444 buf_incrwritepos(clearwritebuf, ses.writepayload->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 }
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 /* finished with payload */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448 buf_burn(ses.writepayload); /* XXX This is probably a good idea, and isn't
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 _that_ likely to hurt performance too badly.
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 Buffers can have cleartext passwords etc, or
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 other sensitive data */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 buf_setpos(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453 buf_setlen(ses.writepayload, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455 /* 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
456 * 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
457 padlen = blocksize - (clearwritebuf->len) % blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458 if (padlen < 4) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459 padlen += blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
460 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 /* check for min packet length */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
462 if (clearwritebuf->len + padlen < MIN_PACKET_LEN) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463 padlen += blocksize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
465
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
466 buf_setpos(clearwritebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467 /* packet length excluding the packetlength uint32 */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 buf_putint(clearwritebuf, clearwritebuf->len + padlen - 4);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
469
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470 /* padding len */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471 buf_putbyte(clearwritebuf, padlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 /* actual padding */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473 buf_setpos(clearwritebuf, clearwritebuf->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 buf_incrlen(clearwritebuf, padlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 genrandom(buf_getptr(clearwritebuf, padlen), padlen);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
477 /* do the actual encryption */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 buf_setpos(clearwritebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479 /* 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
480 * wire by writepacket() */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 writebuf = buf_new(clearwritebuf->len + macsize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 if (ses.keys->trans_algo_crypt->cipherdesc == NULL) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 /* copy it */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485 memcpy(buf_getwriteptr(writebuf, clearwritebuf->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486 buf_getptr(clearwritebuf, clearwritebuf->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
487 clearwritebuf->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 buf_incrwritepos(writebuf, clearwritebuf->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 } else {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490 /* encrypt it */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 while (clearwritebuf->pos < clearwritebuf->len) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492 if (cbc_encrypt(buf_getptr(clearwritebuf, blocksize),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 buf_getwriteptr(writebuf, blocksize),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 &ses.keys->trans_symmetric_struct) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 dropbear_exit("error encrypting");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 buf_incrpos(clearwritebuf, blocksize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 buf_incrwritepos(writebuf, blocksize);
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 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 /* 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
503 writemac(writebuf, clearwritebuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505 /* clearwritebuf is finished with */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 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
507 clearwritebuf = NULL;
27
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509 /* enqueue the packet for sending */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510 buf_setpos(writebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 enqueue(&ses.writequeue, (void*)writebuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 /* Update counts */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514 ses.kexstate.datatrans += writebuf->len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515 ses.transseq++;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517 TRACE(("leave encrypt_packet()"));
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521 /* 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
522 static void writemac(buffer * outputbuffer, buffer * clearwritebuf) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 int macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 unsigned char seqbuf[4];
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
526 unsigned long hashsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527 hmac_state hmac;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529 TRACE(("enter writemac"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 macsize = ses.keys->trans_algo_mac->hashsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
532
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 if (macsize > 0) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534 /* calculate the mac */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 if (hmac_init(&hmac,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536 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
537 ses.keys->transmackey,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
538 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
539 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
541
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 /* sequence number */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543 STORE32H(ses.transseq, seqbuf);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 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
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 /* the actual contents */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 buf_setpos(clearwritebuf, 0);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 if (hmac_process(&hmac,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 buf_getptr(clearwritebuf,
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552 clearwritebuf->len),
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553 clearwritebuf->len) != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 dropbear_exit("HMAC error");
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557 hashsize = macsize;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558 if (hmac_done(&hmac, buf_getwriteptr(outputbuffer, macsize), &hashsize)
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 != CRYPT_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 dropbear_exit("HMAC error");
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 buf_incrwritepos(outputbuffer, macsize);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 TRACE(("leave writemac"));
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 #ifndef DISABLE_ZLIB
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 /* 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
569 * respective current positions. */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 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
571
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 unsigned int endpos = src->pos + len;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 int result;
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 TRACE(("enter buf_compress"));
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 while (1) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579 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
580 ses.keys->trans_zstream->next_in =
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581 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
582
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583 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
584 ses.keys->trans_zstream->next_out =
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585 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
586
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587 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
588
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589 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
590 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
591 buf_setpos(dest, dest->len);
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
593 if (result != Z_OK) {
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 dropbear_exit("zlib error");
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
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 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
598 break;
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599 }
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601 assert(ses.keys->trans_zstream->avail_out == 0);
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 /* 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
604 * 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
605 * but it is possible */
08da099e8337 - Rename common-packet.c to packet.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
606 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
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 TRACE(("leave buf_compress"));
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 #endif