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
|
/*
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008 Free Software Foundation
*
* Author: Nikos Mavrogiannopoulos, Simon Josefsson
*
* This file is part of GNUTLS.
*
* The GNUTLS library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA
*
*/
/* Functions that relate to the TLS hello extension parsing.
* Hello extensions are packets appended in the TLS hello packet, and
* allow for extra functionality.
*/
#include "gnutls_int.h"
#include "gnutls_extensions.h"
#include "gnutls_errors.h"
#include "ext_max_record.h"
#include <ext_cert_type.h>
#include <ext_server_name.h>
#include <ext_oprfi.h>
#include <ext_srp.h>
#include <gnutls_num.h>
typedef struct
{
const char *name;
uint16_t type;
gnutls_ext_parse_type_t parse_type;
gnutls_ext_recv_func recv_func;
gnutls_ext_send_func send_func;
} gnutls_extension_entry;
size_t extfunc_size = 0;
static gnutls_extension_entry *extfunc = NULL;
static gnutls_ext_recv_func
_gnutls_ext_func_recv (uint16_t type, gnutls_ext_parse_type_t parse_type)
{
size_t i;
for (i = 0; i < extfunc_size; i++)
if (extfunc[i].type == type)
if (parse_type == GNUTLS_EXT_ANY || extfunc[i].parse_type == parse_type)
return extfunc[i].recv_func;
return NULL;
}
static const char *
_gnutls_extension_get_name (uint16_t type)
{
size_t i;
for (i = 0; i < extfunc_size; i++)
if (extfunc[i].type == type)
return extfunc[i].name;
return NULL;
}
/* Checks if the extension we just received is one of the
* requested ones. Otherwise it's a fatal error.
*/
static int
_gnutls_extension_list_check (gnutls_session_t session, uint16_t type)
{
if (session->security_parameters.entity == GNUTLS_CLIENT)
{
int i;
for (i = 0; i < session->internals.extensions_sent_size; i++)
{
if (type == session->internals.extensions_sent[i])
return 0; /* ok found */
}
return GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION;
}
return 0;
}
int
_gnutls_parse_extensions (gnutls_session_t session,
gnutls_ext_parse_type_t parse_type,
const opaque * data, int data_size)
{
int next, ret;
int pos = 0;
uint16_t type;
const opaque *sdata;
gnutls_ext_recv_func ext_recv;
uint16_t size;
#ifdef DEBUG
int i;
if (session->security_parameters.entity == GNUTLS_CLIENT)
for (i = 0; i < session->internals.extensions_sent_size; i++)
{
_gnutls_debug_log ("EXT[%d]: expecting extension '%s'\n",
session,
_gnutls_extension_get_name
(session->internals.extensions_sent[i]));
}
#endif
DECR_LENGTH_RET (data_size, 2, 0);
next = _gnutls_read_uint16 (data);
pos += 2;
DECR_LENGTH_RET (data_size, next, 0);
do
{
DECR_LENGTH_RET (next, 2, 0);
type = _gnutls_read_uint16 (&data[pos]);
pos += 2;
_gnutls_debug_log ("EXT[%x]: Received extension '%s/%d'\n", session,
_gnutls_extension_get_name (type), type);
if ((ret = _gnutls_extension_list_check (session, type)) < 0)
{
gnutls_assert ();
return ret;
}
DECR_LENGTH_RET (next, 2, 0);
size = _gnutls_read_uint16 (&data[pos]);
pos += 2;
DECR_LENGTH_RET (next, size, 0);
sdata = &data[pos];
pos += size;
ext_recv = _gnutls_ext_func_recv (type, parse_type);
if (ext_recv == NULL)
continue;
if ((ret = ext_recv (session, sdata, size)) < 0)
{
gnutls_assert ();
return ret;
}
}
while (next > 2);
return 0;
}
/* Adds the extension we want to send in the extensions list.
* This list is used to check whether the (later) received
* extensions are the ones we requested.
*/
static void
_gnutls_extension_list_add (gnutls_session_t session, uint16_t type)
{
if (session->security_parameters.entity == GNUTLS_CLIENT)
{
if (session->internals.extensions_sent_size < MAX_EXT_TYPES)
{
session->internals.extensions_sent[session->
internals.extensions_sent_size] =
type;
session->internals.extensions_sent_size++;
}
else
{
_gnutls_debug_log ("extensions: Increase MAX_EXT_TYPES\n");
}
}
}
int
_gnutls_gen_extensions (gnutls_session_t session, opaque * data,
size_t data_size)
{
int size;
uint16_t pos = 0;
opaque *sdata;
int sdata_size;
size_t i;
if (data_size < 2)
{
gnutls_assert ();
return GNUTLS_E_INTERNAL_ERROR;
}
/* allocate enough data for each extension.
*/
sdata_size = data_size;
sdata = gnutls_malloc (sdata_size);
if (sdata == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
pos += 2;
for (i = 0; i < extfunc_size; i++)
{
gnutls_extension_entry *p = &extfunc[i];
if (p->send_func == NULL)
continue;
size = p->send_func (session, sdata, sdata_size);
if (size > 0)
{
if (data_size < pos + (size_t) size + 4)
{
gnutls_assert ();
gnutls_free (sdata);
return GNUTLS_E_INTERNAL_ERROR;
}
/* write extension type */
_gnutls_write_uint16 (p->type, &data[pos]);
pos += 2;
/* write size */
_gnutls_write_uint16 (size, &data[pos]);
pos += 2;
memcpy (&data[pos], sdata, size);
pos += size;
/* add this extension to the extension list
*/
_gnutls_extension_list_add (session, p->type);
_gnutls_debug_log ("EXT[%x]: Sending extension %s\n",
session, p->name);
}
else if (size < 0)
{
gnutls_assert ();
gnutls_free (sdata);
return size;
}
}
size = pos;
pos -= 2; /* remove the size of the size header! */
_gnutls_write_uint16 (pos, data);
if (size == 2)
{ /* empty */
size = 0;
}
gnutls_free (sdata);
return size;
}
int
_gnutls_ext_init (void)
{
int ret;
ret = gnutls_ext_register (GNUTLS_EXTENSION_MAX_RECORD_SIZE,
"MAX_RECORD_SIZE",
GNUTLS_EXT_TLS,
_gnutls_max_record_recv_params,
_gnutls_max_record_send_params);
if (ret != GNUTLS_E_SUCCESS)
return ret;
ret = gnutls_ext_register (GNUTLS_EXTENSION_CERT_TYPE,
"CERT_TYPE",
GNUTLS_EXT_TLS,
_gnutls_cert_type_recv_params,
_gnutls_cert_type_send_params);
if (ret != GNUTLS_E_SUCCESS)
return ret;
ret = gnutls_ext_register (GNUTLS_EXTENSION_SERVER_NAME,
"SERVER_NAME",
GNUTLS_EXT_APPLICATION,
_gnutls_server_name_recv_params,
_gnutls_server_name_send_params);
if (ret != GNUTLS_E_SUCCESS)
return ret;
#ifdef ENABLE_OPRFI
ret = gnutls_ext_register (GNUTLS_EXTENSION_OPAQUE_PRF_INPUT,
"OPAQUE_PRF_INPUT",
GNUTLS_EXT_TLS,
_gnutls_oprfi_recv_params,
_gnutls_oprfi_send_params);
if (ret != GNUTLS_E_SUCCESS)
return ret;
#endif
#ifdef ENABLE_SRP
ret = gnutls_ext_register (GNUTLS_EXTENSION_SRP,
"SRP",
GNUTLS_EXT_TLS,
_gnutls_srp_recv_params,
_gnutls_srp_send_params);
if (ret != GNUTLS_E_SUCCESS)
return ret;
#endif
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ext_register - Register a handler for a TLS extension
* @type: the 16-bit integer referring to the extension type
* @name: human printable name of the extension used for debugging
* @parse_type: either #GNUTLS_EXT_TLS or %GNUTLS_EXT_APPLICATION.
* @recv_func: a function to receive extension data
* @send_func: a function to send extension data
*
* This function is used to register a new TLS extension handler.
*
* Returns: %GNUTLS_E_SUCCESS on success, or an error code.
*
* Since: 2.6.0
**/
int
gnutls_ext_register (int type,
const char *name,
gnutls_ext_parse_type_t parse_type,
gnutls_ext_recv_func recv_func,
gnutls_ext_send_func send_func)
{
gnutls_extension_entry *p;
p = gnutls_realloc (extfunc, sizeof (*extfunc) * (extfunc_size + 1));
if (!p)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
extfunc = p;
extfunc[extfunc_size].type = type;
extfunc[extfunc_size].name = name;
extfunc[extfunc_size].parse_type = parse_type;
extfunc[extfunc_size].recv_func = recv_func;
extfunc[extfunc_size].send_func = send_func;
extfunc_size++;
return GNUTLS_E_SUCCESS;
}
|