1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
* Copyright (C) 2000-2012 Free Software Foundation, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/* This file contains the functions which convert the TLS plaintext
* packet to TLS compressed packet.
*/
#include "gnutls_int.h"
#include "compress.h"
#include "errors.h"
#include "constate.h"
#include <algorithms.h>
#include <gnutls/gnutls.h>
/* Compression Section */
#define GNUTLS_COMPRESSION_ENTRY(name, id, wb, ml, cl) \
{ #name, name, id, wb, ml, cl}
#define MAX_COMP_METHODS 5
const int _gnutls_comp_algorithms_size = MAX_COMP_METHODS;
gnutls_compression_entry _gnutls_compression_algorithms[MAX_COMP_METHODS] = {
GNUTLS_COMPRESSION_ENTRY(GNUTLS_COMP_NULL, 0x00, 0, 0, 0),
#ifdef HAVE_LIBZ
/* draft-ietf-tls-compression-02 */
GNUTLS_COMPRESSION_ENTRY(GNUTLS_COMP_DEFLATE, 0x01, 15, 8, 3),
#endif
{0, 0, 0, 0, 0, 0}
};
static const gnutls_compression_method_t supported_compressions[] = {
#ifdef HAVE_LIBZ
GNUTLS_COMP_DEFLATE,
#endif
GNUTLS_COMP_NULL,
0
};
#define GNUTLS_COMPRESSION_LOOP(b) \
const gnutls_compression_entry *p; \
for(p = _gnutls_compression_algorithms; p->name != NULL; p++) { b ; }
#define GNUTLS_COMPRESSION_ALG_LOOP(a) \
GNUTLS_COMPRESSION_LOOP( if(p->id == algorithm) { a; break; } )
#define GNUTLS_COMPRESSION_ALG_LOOP_NUM(a) \
GNUTLS_COMPRESSION_LOOP( if(p->num == num) { a; break; } )
/* Compression Functions */
/**
* gnutls_compression_get_name:
* @algorithm: is a Compression algorithm
*
* Convert a #gnutls_compression_method_t value to a string.
*
* Returns: a pointer to a string that contains the name of the
* specified compression algorithm, or %NULL.
**/
const char *gnutls_compression_get_name(gnutls_compression_method_t
algorithm)
{
const char *ret = NULL;
/* avoid prefix */
GNUTLS_COMPRESSION_ALG_LOOP(ret =
p->name + sizeof("GNUTLS_COMP_") - 1);
return ret;
}
/**
* gnutls_compression_get_id:
* @name: is a compression method name
*
* The names are compared in a case insensitive way.
*
* Returns: an id of the specified in a string compression method, or
* %GNUTLS_COMP_UNKNOWN on error.
**/
gnutls_compression_method_t gnutls_compression_get_id(const char *name)
{
gnutls_compression_method_t ret = GNUTLS_COMP_UNKNOWN;
GNUTLS_COMPRESSION_LOOP(if
(strcasecmp
(p->name + sizeof("GNUTLS_COMP_") - 1,
name) == 0) ret = p->id);
return ret;
}
/**
* gnutls_compression_list:
*
* Get a list of compression methods.
*
* Returns: a zero-terminated list of #gnutls_compression_method_t
* integers indicating the available compression methods.
**/
const gnutls_compression_method_t *gnutls_compression_list(void)
{
return supported_compressions;
}
/* return the tls number of the specified algorithm */
int _gnutls_compression_get_num(gnutls_compression_method_t algorithm)
{
int ret = -1;
/* avoid prefix */
GNUTLS_COMPRESSION_ALG_LOOP(ret = p->num);
return ret;
}
#ifdef HAVE_LIBZ
static int get_wbits(gnutls_compression_method_t algorithm)
{
int ret = -1;
/* avoid prefix */
GNUTLS_COMPRESSION_ALG_LOOP(ret = p->window_bits);
return ret;
}
static int get_mem_level(gnutls_compression_method_t algorithm)
{
int ret = -1;
/* avoid prefix */
GNUTLS_COMPRESSION_ALG_LOOP(ret = p->mem_level);
return ret;
}
static int get_comp_level(gnutls_compression_method_t algorithm)
{
int ret = -1;
/* avoid prefix */
GNUTLS_COMPRESSION_ALG_LOOP(ret = p->comp_level);
return ret;
}
#endif
/* returns the gnutls internal ID of the TLS compression
* method num
*/
gnutls_compression_method_t _gnutls_compression_get_id(int num)
{
gnutls_compression_method_t ret = -1;
/* avoid prefix */
GNUTLS_COMPRESSION_ALG_LOOP_NUM(ret = p->id);
return ret;
}
int _gnutls_compression_is_ok(gnutls_compression_method_t algorithm)
{
ssize_t ret = -1;
GNUTLS_COMPRESSION_ALG_LOOP(ret = p->id);
if (ret >= 0)
ret = 0;
else
ret = 1;
return ret;
}
/* For compression */
#define MIN_PRIVATE_COMP_ALGO 0xEF
/* returns the TLS numbers of the compression methods we support
*/
#define SUPPORTED_COMPRESSION_METHODS session->internals.priorities.compression.algorithms
int
_gnutls_supported_compression_methods(gnutls_session_t session,
uint8_t * comp, size_t comp_size)
{
unsigned int i, j;
int tmp;
if (comp_size < SUPPORTED_COMPRESSION_METHODS)
return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
for (i = j = 0; i < SUPPORTED_COMPRESSION_METHODS; i++) {
if (IS_DTLS(session) && session->internals.priorities.compression.priority[i] != GNUTLS_COMP_NULL) {
gnutls_assert();
continue;
}
tmp =
_gnutls_compression_get_num(session->
internals.priorities.
compression.priority[i]);
/* remove private compression algorithms, if requested.
*/
if (tmp == -1 || (tmp >= MIN_PRIVATE_COMP_ALGO &&
session->internals.enable_private == 0))
{
gnutls_assert();
continue;
}
comp[j] = (uint8_t) tmp;
j++;
}
if (j == 0) {
gnutls_assert();
return GNUTLS_E_NO_COMPRESSION_ALGORITHMS;
}
return j;
}
/* The flag d is the direction (compress, decompress). Non zero is
* decompress.
*/
int _gnutls_comp_init(comp_hd_st * handle,
gnutls_compression_method_t method, int d)
{
handle->algo = method;
handle->handle = NULL;
switch (method) {
case GNUTLS_COMP_DEFLATE:
#ifdef HAVE_LIBZ
{
int window_bits, mem_level;
int comp_level;
z_stream *zhandle;
int err;
window_bits = get_wbits(method);
mem_level = get_mem_level(method);
comp_level = get_comp_level(method);
handle->handle = gnutls_malloc(sizeof(z_stream));
if (handle->handle == NULL)
return
gnutls_assert_val
(GNUTLS_E_MEMORY_ERROR);
zhandle = handle->handle;
zhandle->zalloc = (alloc_func) 0;
zhandle->zfree = (free_func) 0;
zhandle->opaque = (voidpf) 0;
if (d)
err = inflateInit2(zhandle, window_bits);
else {
err = deflateInit2(zhandle,
comp_level, Z_DEFLATED,
window_bits, mem_level,
Z_DEFAULT_STRATEGY);
}
if (err != Z_OK) {
gnutls_assert();
gnutls_free(handle->handle);
return GNUTLS_E_COMPRESSION_FAILED;
}
}
break;
#endif
case GNUTLS_COMP_NULL:
case GNUTLS_COMP_UNKNOWN:
break;
default:
return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM;
}
return 0;
}
/* The flag d is the direction (compress, decompress). Non zero is
* decompress.
*/
void _gnutls_comp_deinit(comp_hd_st * handle, int d)
{
if (handle != NULL) {
switch (handle->algo) {
#ifdef HAVE_LIBZ
case GNUTLS_COMP_DEFLATE:
{
if (d)
inflateEnd(handle->handle);
else
deflateEnd(handle->handle);
break;
}
#endif
default:
break;
}
gnutls_free(handle->handle);
handle->handle = NULL;
}
}
/* These functions are memory consuming
*/
int
_gnutls_compress(comp_hd_st * handle, const uint8_t * plain,
size_t plain_size, uint8_t * compressed,
size_t max_comp_size, unsigned int stateless)
{
int compressed_size = GNUTLS_E_COMPRESSION_FAILED;
/* NULL compression is not handled here
*/
if (handle == NULL) {
gnutls_assert();
return GNUTLS_E_INTERNAL_ERROR;
}
switch (handle->algo) {
#ifdef HAVE_LIBZ
case GNUTLS_COMP_DEFLATE:
{
z_stream *zhandle;
int err;
int type;
if (stateless) {
type = Z_FULL_FLUSH;
} else
type = Z_SYNC_FLUSH;
zhandle = handle->handle;
zhandle->next_in = (Bytef *) plain;
zhandle->avail_in = plain_size;
zhandle->next_out = (Bytef *) compressed;
zhandle->avail_out = max_comp_size;
err = deflate(zhandle, type);
if (err != Z_OK || zhandle->avail_in != 0)
return
gnutls_assert_val
(GNUTLS_E_COMPRESSION_FAILED);
compressed_size =
max_comp_size - zhandle->avail_out;
break;
}
#endif
default:
gnutls_assert();
return GNUTLS_E_INTERNAL_ERROR;
} /* switch */
#ifdef COMPRESSION_DEBUG
_gnutls_debug_log("Compression ratio: %f\n",
(float) ((float) compressed_size /
(float) plain_size));
#endif
return compressed_size;
}
int
_gnutls_decompress(comp_hd_st * handle, uint8_t * compressed,
size_t compressed_size, uint8_t * plain,
size_t max_plain_size)
{
int plain_size = GNUTLS_E_DECOMPRESSION_FAILED;
if (compressed_size > max_plain_size + EXTRA_COMP_SIZE) {
gnutls_assert();
return GNUTLS_E_DECOMPRESSION_FAILED;
}
/* NULL compression is not handled here
*/
if (handle == NULL) {
gnutls_assert();
return GNUTLS_E_INTERNAL_ERROR;
}
switch (handle->algo) {
#ifdef HAVE_LIBZ
case GNUTLS_COMP_DEFLATE:
{
z_stream *zhandle;
int err;
zhandle = handle->handle;
zhandle->next_in = (Bytef *) compressed;
zhandle->avail_in = compressed_size;
zhandle->next_out = (Bytef *) plain;
zhandle->avail_out = max_plain_size;
err = inflate(zhandle, Z_SYNC_FLUSH);
if (err != Z_OK)
return
gnutls_assert_val
(GNUTLS_E_DECOMPRESSION_FAILED);
plain_size = max_plain_size - zhandle->avail_out;
break;
}
#endif
default:
gnutls_assert();
return GNUTLS_E_INTERNAL_ERROR;
} /* switch */
return plain_size;
}
|