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
|
/*
* Copyright (C) 2012 Free Software Foundation, Inc.
*
* Author: Simon Josefsson
*
* 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 3 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/>
*
*/
/*
Status Request (OCSP) TLS extension. See RFC 6066 section 8:
https://tools.ietf.org/html/rfc6066#section-8
*/
#include "gnutls_int.h"
#include "gnutls_errors.h"
#include <gnutls_extensions.h>
#include <ext/status_request.h>
typedef struct
{
gnutls_datum_t *responder_id;
size_t responder_id_size;
gnutls_datum_t request_extensions;
int dealloc;
gnutls_status_request_ocsp_func ocsp_func;
void *ocsp_func_ptr;
int expect_certificate_status;
} status_request_ext_st;
/*
From RFC 6066. Client sends:
struct {
CertificateStatusType status_type;
select (status_type) {
case ocsp: OCSPStatusRequest;
} request;
} CertificateStatusRequest;
enum { ocsp(1), (255) } CertificateStatusType;
struct {
ResponderID responder_id_list<0..2^16-1>;
Extensions request_extensions;
} OCSPStatusRequest;
opaque ResponderID<1..2^16-1>;
opaque Extensions<0..2^16-1>;
*/
static int
client_send (gnutls_session_t session,
gnutls_buffer_st* extdata,
status_request_ext_st *priv)
{
int ret_len = 1 + 2;
int ret;
size_t i;
ret = _gnutls_buffer_append_prefix (extdata, 8, 1);
if (ret < 0)
return gnutls_assert_val (ret);
ret = _gnutls_buffer_append_prefix (extdata, 16, priv->responder_id_size);
if (ret < 0)
return gnutls_assert_val (ret);
for (i = 0; i < priv->responder_id_size; i++)
{
if (priv->responder_id[i].size <= 0)
return gnutls_assert_val (GNUTLS_E_INVALID_REQUEST);
ret = _gnutls_buffer_append_data_prefix (extdata, 16,
priv->responder_id[i].data,
priv->responder_id[i].size);
if (ret < 0)
return gnutls_assert_val (ret);
ret_len += 2 + priv->responder_id[i].size;
}
ret = _gnutls_buffer_append_data_prefix (extdata, 16,
priv->request_extensions.data,
priv->request_extensions.size);
if (ret < 0)
return gnutls_assert_val (ret);
ret_len += 2 + priv->request_extensions.size;
return ret_len;
}
static int
server_recv (gnutls_session_t session,
status_request_ext_st *priv,
const uint8_t * data,
size_t size)
{
size_t i;
_gnutls_handshake_log ("EXT[%p]: got ocsp\n", session);
/* minimum message is type (1) + responder_id_list (2) +
request_extension (2) = 5 */
if (size < 5)
return gnutls_assert_val (GNUTLS_E_UNEXPECTED_PACKET_LENGTH);
/* We ignore non-ocsp CertificateStatusType. The spec is unclear
what should be done. */
if (data[0] != '\x01')
{
gnutls_assert ();
_gnutls_handshake_log ("EXT[%p]: unknown status_type %d\n",
session, data[0]);
return 0;
}
size--, data++;
priv->dealloc = 1;
priv->responder_id_size = _gnutls_read_uint16 (data);
size -= 2, data += 2;
if (size <= priv->responder_id_size * 2)
return gnutls_assert_val (GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
priv->responder_id = gnutls_malloc (priv->responder_id_size
* sizeof (*priv->responder_id));
if (priv->responder_id == NULL)
return gnutls_assert_val (GNUTLS_E_MEMORY_ERROR);
for (i = 0; i < priv->responder_id_size; i++)
{
size_t l;
if (size <= 2)
return gnutls_assert_val (GNUTLS_E_UNEXPECTED_PACKET_LENGTH);
l = _gnutls_read_uint16 (data);
size -= 2, data += 2;
if (size <= l)
return gnutls_assert_val (GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
priv->responder_id[i].data = gnutls_malloc (l);
if (priv->responder_id[i].data == NULL)
return gnutls_assert_val (GNUTLS_E_MEMORY_ERROR);
memcpy (priv->responder_id[i].data, data, l);
priv->responder_id[i].size = l;
size -= l, data += l;
}
return 0;
}
/*
Servers return a certificate response along with their certificate
by sending a "CertificateStatus" message immediately after the
"Certificate" message (and before any "ServerKeyExchange" or
"CertificateRequest" messages). If a server returns a
"CertificateStatus" message, then the server MUST have included an
extension of type "status_request" with empty "extension_data" in
the extended server hello.
*/
static int
server_send (gnutls_session_t session,
gnutls_buffer_st* extdata,
status_request_ext_st *priv)
{
int ret;
if (priv->ocsp_func == NULL)
return gnutls_assert_val (GNUTLS_E_SUCCESS);
ret = priv->ocsp_func (session, priv->ocsp_func_ptr, NULL);
if (ret == GNUTLS_E_NO_CERTIFICATE_STATUS)
return 0;
else if (ret != GNUTLS_E_SUCCESS)
return gnutls_assert_val (ret);
ret = _gnutls_buffer_append_data (extdata, "", 0);
if (ret < 0)
return gnutls_assert_val (ret);
priv->expect_certificate_status = 1;
return 0;
}
static int
client_recv (gnutls_session_t session,
status_request_ext_st *priv,
const uint8_t * data,
size_t size)
{
if (size != 0)
return gnutls_assert_val (GNUTLS_E_UNEXPECTED_PACKET_LENGTH);
priv->expect_certificate_status = 1;
return 0;
}
static int
_gnutls_status_request_send_params (gnutls_session_t session,
gnutls_buffer_st* extdata)
{
extension_priv_data_t epriv;
status_request_ext_st *priv;
int ret;
ret = _gnutls_ext_get_session_data (session,
GNUTLS_EXTENSION_STATUS_REQUEST,
&epriv);
if (ret < 0) /* it is ok not to have it */
return 0;
priv = epriv.ptr;
if (session->security_parameters.entity == GNUTLS_CLIENT)
return client_send (session, extdata, priv);
return server_send (session, extdata, priv);
}
static int
_gnutls_status_request_recv_params (gnutls_session_t session,
const uint8_t * data,
size_t size)
{
extension_priv_data_t epriv;
status_request_ext_st *priv;
int ret;
ret = _gnutls_ext_get_session_data (session,
GNUTLS_EXTENSION_STATUS_REQUEST,
&epriv);
if (ret < 0) /* it is ok not to have it */
return 0;
priv = epriv.ptr;
if (session->security_parameters.entity == GNUTLS_CLIENT)
return client_recv (session, priv, data, size);
return server_recv (session, priv, data, size);
}
/**
* gnutls_status_request_ocsp_client:
* @session: is a #gnutls_session_t structure.
* @responder_id: array with #gnutls_datum_t with DER data of responder id
* @responder_id_size: number of members in @responder_id array
* @request_extensions: a #gnutls_datum_t with DER encoded OCSP extensions
*
* This function is to be used by clients to request OCSP response
* from the server, using the "status_request" TLS extension. Only
* OCSP status type is supported.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
* otherwise a negative error code is returned.
**/
int
gnutls_status_request_ocsp_client (gnutls_session_t session,
gnutls_datum_t *responder_id,
size_t responder_id_size,
gnutls_datum_t *request_extensions)
{
status_request_ext_st *priv;
extension_priv_data_t epriv;
if (session->security_parameters.entity == GNUTLS_SERVER)
return gnutls_assert_val (GNUTLS_E_INVALID_REQUEST);
epriv.ptr = priv = gnutls_calloc (1, sizeof (*priv));
if (priv == NULL)
return gnutls_assert_val (GNUTLS_E_MEMORY_ERROR);
priv->responder_id = responder_id;
priv->responder_id_size = responder_id_size;
if (request_extensions)
{
priv->request_extensions.data = request_extensions->data;
priv->request_extensions.size = request_extensions->size;
}
_gnutls_ext_set_session_data (session,
GNUTLS_EXTENSION_STATUS_REQUEST,
epriv);
return 0;
}
/**
* gnutls_status_request_ocsp_server:
* @session: is a #gnutls_session_t structure.
* @ocsp_func: function pointer to OCSP status request callback.
* @ptr: opaque pointer passed to callback function
*
* This function is to be used by server to register a callback to
* handle OCSP status requests from the client. The callback will be
* invoked if the client supplied a status-request OCSP extension.
* The callback function prototype is:
*
* typedef int (*gnutls_status_request_ocsp_func)
* (gnutls_session_t session, void *ptr, gnutls_datum_t *ocsp_response);
*
* The callback may be invoked up to two times for each handshake. It
* is called the first time when the client hello requested the status
* request extension. In this case, ocsp_response is NULL. The
* purpose of the first callback invocation is to determine whether
* the server will acknowledge the client's request to use the
* extension. The callback may return
* %GNUTLS_E_NO_CERTIFICATE_STATUS, in which case the server will not
* enable the extension. If the callback returns %GNUTLS_E_SUCCESS,
* the server enable the extension. If the callback returns another
* error code, the handshake will terminate.
*
* If the first call to the callback enabled the extension, there will
* usually be a second phase, with a non-NULL ocsp_response. Now the
* server is ready to send the CertificateStatus, and it expects the
* callback to provide the OCSP response data. The callback can at
* this point return %GNUTLS_E_NO_CERTIFICATE_STATUS to avoid sending
* a CertificateStatus message. If the callback returns
* %GNUTLS_E_SUCCESS the ocsp_response will be sent off to the client.
* If the callback returns an error, the handshake will terminate.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
* otherwise a negative error code is returned.
**/
int
gnutls_status_request_ocsp_server (gnutls_session_t session,
gnutls_status_request_ocsp_func ocsp_func,
void *ptr)
{
status_request_ext_st *priv;
extension_priv_data_t epriv;
if (session->security_parameters.entity == GNUTLS_CLIENT)
return gnutls_assert_val (GNUTLS_E_INVALID_REQUEST);
epriv.ptr = priv = gnutls_calloc (1, sizeof (*priv));
if (priv == NULL)
return gnutls_assert_val (GNUTLS_E_MEMORY_ERROR);
priv->ocsp_func = ocsp_func;
priv->ocsp_func_ptr = ptr;
_gnutls_ext_set_session_data (session,
GNUTLS_EXTENSION_STATUS_REQUEST,
epriv);
return 0;
}
static void
_gnutls_status_request_deinit_data (extension_priv_data_t epriv)
{
status_request_ext_st *priv = epriv.ptr;
size_t i;
if (priv->dealloc)
{
for (i = 0; i < priv->responder_id_size; i++)
gnutls_free (priv->responder_id[i].data);
gnutls_free (priv->responder_id);
gnutls_free (priv->request_extensions.data);
}
gnutls_free (priv);
}
static int
_gnutls_status_request_pack (extension_priv_data_t epriv,
gnutls_buffer_st * ps)
{
return -1;
}
static int
_gnutls_status_request_unpack (gnutls_buffer_st * ps,
extension_priv_data_t * _priv)
{
return -1;
}
extension_entry_st ext_mod_status_request = {
.name = "STATUS REQUEST",
.type = GNUTLS_EXTENSION_STATUS_REQUEST,
.parse_type = GNUTLS_EXT_TLS,
.recv_func = _gnutls_status_request_recv_params,
.send_func = _gnutls_status_request_send_params,
.pack_func = _gnutls_status_request_pack,
.unpack_func = _gnutls_status_request_unpack,
.deinit_func = _gnutls_status_request_deinit_data
};
|