Line | Count | Source |
1 | | /* |
2 | | * gzip.c - hooks for compression of packets |
3 | | * |
4 | | * This file is part of the SSH Library |
5 | | * |
6 | | * Copyright (c) 2003 by Aris Adamantiadis |
7 | | * Copyright (c) 2009 by Andreas Schneider <asn@cryptomilk.org> |
8 | | * |
9 | | * The SSH Library is free software; you can redistribute it and/or modify |
10 | | * it under the terms of the GNU Lesser General Public License as published by |
11 | | * the Free Software Foundation; either version 2.1 of the License, or (at your |
12 | | * option) any later version. |
13 | | * |
14 | | * The SSH Library is distributed in the hope that it will be useful, but |
15 | | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 | | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | | * License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with the SSH Library; see the file COPYING. If not, write to |
21 | | * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
22 | | * MA 02111-1307, USA. |
23 | | */ |
24 | | |
25 | | #include "config.h" |
26 | | |
27 | | #include <stdlib.h> |
28 | | #include <string.h> |
29 | | |
30 | | #include "libssh/buffer.h" |
31 | | #include "libssh/crypto.h" |
32 | | #include "libssh/priv.h" |
33 | | #include "libssh/session.h" |
34 | | |
35 | | #ifdef WITH_ZLIB |
36 | | #include <zlib.h> |
37 | | |
38 | | #ifndef BLOCKSIZE |
39 | 0 | #define BLOCKSIZE 4092 |
40 | | #endif |
41 | | |
42 | | static z_stream *initcompress(ssh_session session) |
43 | 0 | { |
44 | 0 | z_stream *stream = NULL; |
45 | 0 | int status; |
46 | |
|
47 | 0 | stream = calloc(1, sizeof(z_stream)); |
48 | 0 | if (stream == NULL) { |
49 | 0 | return NULL; |
50 | 0 | } |
51 | | |
52 | 0 | status = deflateInit2(stream, Z_BEST_SPEED, Z_DEFLATED, 15, 8, Z_HUFFMAN_ONLY); |
53 | 0 | if (status != Z_OK) { |
54 | 0 | deflateEnd(stream); |
55 | 0 | SAFE_FREE(stream); |
56 | 0 | ssh_set_error(session, |
57 | 0 | SSH_FATAL, |
58 | 0 | "status %d initialising zlib deflate", |
59 | 0 | status); |
60 | 0 | return NULL; |
61 | 0 | } |
62 | | |
63 | 0 | return stream; |
64 | 0 | } |
65 | | |
66 | | static ssh_buffer gzip_compress(ssh_session session, ssh_buffer source) |
67 | 0 | { |
68 | 0 | struct ssh_crypto_struct *crypto = NULL; |
69 | 0 | z_stream *zout = NULL; |
70 | 0 | void *in_ptr = ssh_buffer_get(source); |
71 | 0 | uint32_t in_size = ssh_buffer_get_len(source); |
72 | 0 | ssh_buffer dest = NULL; |
73 | 0 | unsigned char out_buf[BLOCKSIZE] = {0}; |
74 | 0 | uint32_t len; |
75 | 0 | int status; |
76 | |
|
77 | 0 | crypto = ssh_packet_get_current_crypto(session, SSH_DIRECTION_OUT); |
78 | 0 | if (crypto == NULL) { |
79 | 0 | return NULL; |
80 | 0 | } |
81 | 0 | zout = crypto->compress_out_ctx; |
82 | 0 | if (zout == NULL) { |
83 | 0 | zout = crypto->compress_out_ctx = initcompress(session); |
84 | 0 | if (zout == NULL) { |
85 | 0 | return NULL; |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | 0 | dest = ssh_buffer_new(); |
90 | 0 | if (dest == NULL) { |
91 | 0 | return NULL; |
92 | 0 | } |
93 | | |
94 | 0 | zout->next_out = out_buf; |
95 | 0 | zout->next_in = in_ptr; |
96 | 0 | zout->avail_in = in_size; |
97 | 0 | do { |
98 | 0 | zout->avail_out = BLOCKSIZE; |
99 | 0 | status = deflate(zout, Z_PARTIAL_FLUSH); |
100 | 0 | if (status != Z_OK) { |
101 | 0 | SSH_BUFFER_FREE(dest); |
102 | 0 | ssh_set_error(session, |
103 | 0 | SSH_FATAL, |
104 | 0 | "status %d deflating zlib packet", |
105 | 0 | status); |
106 | 0 | return NULL; |
107 | 0 | } |
108 | 0 | len = BLOCKSIZE - zout->avail_out; |
109 | 0 | if (ssh_buffer_add_data(dest, out_buf, len) < 0) { |
110 | 0 | SSH_BUFFER_FREE(dest); |
111 | 0 | return NULL; |
112 | 0 | } |
113 | 0 | zout->next_out = out_buf; |
114 | 0 | } while (zout->avail_out == 0); |
115 | | |
116 | 0 | return dest; |
117 | 0 | } |
118 | | |
119 | | int |
120 | | compress_buffer(ssh_session session, ssh_buffer buf) |
121 | 0 | { |
122 | 0 | ssh_buffer dest = NULL; |
123 | 0 | int rv; |
124 | |
|
125 | 0 | dest = gzip_compress(session, buf); |
126 | 0 | if (dest == NULL) { |
127 | 0 | return -1; |
128 | 0 | } |
129 | | |
130 | 0 | if (ssh_buffer_reinit(buf) < 0) { |
131 | 0 | SSH_BUFFER_FREE(dest); |
132 | 0 | return -1; |
133 | 0 | } |
134 | | |
135 | 0 | rv = ssh_buffer_add_data(buf, |
136 | 0 | ssh_buffer_get(dest), |
137 | 0 | ssh_buffer_get_len(dest)); |
138 | 0 | if (rv < 0) { |
139 | 0 | SSH_BUFFER_FREE(dest); |
140 | 0 | return -1; |
141 | 0 | } |
142 | | |
143 | 0 | SSH_BUFFER_FREE(dest); |
144 | 0 | return 0; |
145 | 0 | } |
146 | | |
147 | | /* decompression */ |
148 | | |
149 | | static z_stream * |
150 | | initdecompress(ssh_session session) |
151 | 0 | { |
152 | 0 | z_stream *stream = NULL; |
153 | 0 | int status; |
154 | |
|
155 | 0 | stream = calloc(1, sizeof(z_stream)); |
156 | 0 | if (stream == NULL) { |
157 | 0 | return NULL; |
158 | 0 | } |
159 | | |
160 | 0 | status = inflateInit(stream); |
161 | 0 | if (status != Z_OK) { |
162 | 0 | inflateEnd(stream); |
163 | 0 | SAFE_FREE(stream); |
164 | 0 | ssh_set_error(session, |
165 | 0 | SSH_FATAL, |
166 | 0 | "Status = %d initiating inflate context!", |
167 | 0 | status); |
168 | 0 | return NULL; |
169 | 0 | } |
170 | | |
171 | 0 | return stream; |
172 | 0 | } |
173 | | |
174 | | static ssh_buffer |
175 | | gzip_decompress(ssh_session session, ssh_buffer source, size_t maxlen) |
176 | 0 | { |
177 | 0 | struct ssh_crypto_struct *crypto = NULL; |
178 | 0 | z_stream *zin = NULL; |
179 | 0 | void *in_ptr = ssh_buffer_get(source); |
180 | 0 | uint32_t in_size = ssh_buffer_get_len(source); |
181 | 0 | unsigned char out_buf[BLOCKSIZE] = {0}; |
182 | 0 | ssh_buffer dest = NULL; |
183 | 0 | uint32_t len; |
184 | 0 | int status; |
185 | |
|
186 | 0 | crypto = ssh_packet_get_current_crypto(session, SSH_DIRECTION_IN); |
187 | 0 | if (crypto == NULL) { |
188 | 0 | return NULL; |
189 | 0 | } |
190 | | |
191 | 0 | zin = crypto->compress_in_ctx; |
192 | 0 | if (zin == NULL) { |
193 | 0 | zin = crypto->compress_in_ctx = initdecompress(session); |
194 | 0 | if (zin == NULL) { |
195 | 0 | return NULL; |
196 | 0 | } |
197 | 0 | } |
198 | | |
199 | 0 | dest = ssh_buffer_new(); |
200 | 0 | if (dest == NULL) { |
201 | 0 | return NULL; |
202 | 0 | } |
203 | | |
204 | 0 | zin->next_out = out_buf; |
205 | 0 | zin->next_in = in_ptr; |
206 | 0 | zin->avail_in = in_size; |
207 | |
|
208 | 0 | do { |
209 | 0 | zin->avail_out = BLOCKSIZE; |
210 | 0 | status = inflate(zin, Z_PARTIAL_FLUSH); |
211 | 0 | if (status != Z_OK && status != Z_BUF_ERROR) { |
212 | 0 | ssh_set_error(session, |
213 | 0 | SSH_FATAL, |
214 | 0 | "status %d inflating zlib packet", |
215 | 0 | status); |
216 | 0 | SSH_BUFFER_FREE(dest); |
217 | 0 | return NULL; |
218 | 0 | } |
219 | | |
220 | 0 | len = BLOCKSIZE - zin->avail_out; |
221 | 0 | if (ssh_buffer_add_data(dest, out_buf, len) < 0) { |
222 | 0 | SSH_BUFFER_FREE(dest); |
223 | 0 | return NULL; |
224 | 0 | } |
225 | 0 | if (ssh_buffer_get_len(dest) > maxlen) { |
226 | | /* Size of packet exceeded, avoid a denial of service attack */ |
227 | 0 | SSH_BUFFER_FREE(dest); |
228 | 0 | return NULL; |
229 | 0 | } |
230 | 0 | zin->next_out = out_buf; |
231 | 0 | } while (zin->avail_out == 0); |
232 | | |
233 | 0 | return dest; |
234 | 0 | } |
235 | | |
236 | | int |
237 | | decompress_buffer(ssh_session session, ssh_buffer buf, size_t maxlen) |
238 | 0 | { |
239 | 0 | ssh_buffer dest = NULL; |
240 | 0 | int rv; |
241 | |
|
242 | 0 | dest = gzip_decompress(session, buf, maxlen); |
243 | 0 | if (dest == NULL) { |
244 | 0 | return -1; |
245 | 0 | } |
246 | | |
247 | 0 | if (ssh_buffer_reinit(buf) < 0) { |
248 | 0 | SSH_BUFFER_FREE(dest); |
249 | 0 | return -1; |
250 | 0 | } |
251 | | |
252 | 0 | rv = ssh_buffer_add_data(buf, |
253 | 0 | ssh_buffer_get(dest), |
254 | 0 | ssh_buffer_get_len(dest)); |
255 | 0 | if (rv < 0) { |
256 | 0 | SSH_BUFFER_FREE(dest); |
257 | 0 | return -1; |
258 | 0 | } |
259 | | |
260 | 0 | SSH_BUFFER_FREE(dest); |
261 | 0 | return 0; |
262 | 0 | } |
263 | | |
264 | | void |
265 | | compress_cleanup(struct ssh_crypto_struct *crypto) |
266 | 0 | { |
267 | 0 | if (crypto->compress_out_ctx) { |
268 | 0 | deflateEnd(crypto->compress_out_ctx); |
269 | 0 | } |
270 | 0 | SAFE_FREE(crypto->compress_out_ctx); |
271 | |
|
272 | 0 | if (crypto->compress_in_ctx) { |
273 | 0 | inflateEnd(crypto->compress_in_ctx); |
274 | 0 | } |
275 | | SAFE_FREE(crypto->compress_in_ctx); |
276 | 0 | } |
277 | | #else /* WITH_ZLIB */ |
278 | | |
279 | | int |
280 | | compress_buffer(UNUSED_PARAM(ssh_session session), UNUSED_PARAM(ssh_buffer buf)) |
281 | | { |
282 | | /* without zlib compiled in, this should never happen */ |
283 | | return -1; |
284 | | } |
285 | | int |
286 | | decompress_buffer(UNUSED_PARAM(ssh_session session), |
287 | | UNUSED_PARAM(ssh_buffer buf), |
288 | | UNUSED_PARAM(size_t maxlen)) |
289 | | { |
290 | | /* without zlib compiled in, this should never happen */ |
291 | | return -1; |
292 | | } |
293 | | |
294 | | void |
295 | | compress_cleanup(UNUSED_PARAM(struct ssh_crypto_struct *crypto)) |
296 | | { |
297 | | /* no-op */ |
298 | | } |
299 | | |
300 | | #endif /* WITH_ZLIB */ |