/src/openssl/ssl/statem/extensions_cust.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2014-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* Custom extension utility functions */ |
11 | | |
12 | | #include <openssl/ct.h> |
13 | | #include "../ssl_local.h" |
14 | | #include "internal/cryptlib.h" |
15 | | #include "internal/ssl_unwrap.h" |
16 | | #include "statem_local.h" |
17 | | |
18 | | typedef struct { |
19 | | void *add_arg; |
20 | | custom_ext_add_cb add_cb; |
21 | | custom_ext_free_cb free_cb; |
22 | | } custom_ext_add_cb_wrap; |
23 | | |
24 | | typedef struct { |
25 | | void *parse_arg; |
26 | | custom_ext_parse_cb parse_cb; |
27 | | } custom_ext_parse_cb_wrap; |
28 | | |
29 | | /* |
30 | | * Provide thin wrapper callbacks which convert new style arguments to old style |
31 | | */ |
32 | | static int custom_ext_add_old_cb_wrap(SSL *s, unsigned int ext_type, |
33 | | unsigned int context, |
34 | | const unsigned char **out, |
35 | | size_t *outlen, X509 *x, size_t chainidx, |
36 | | int *al, void *add_arg) |
37 | 0 | { |
38 | 0 | custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg; |
39 | |
|
40 | 0 | if (add_cb_wrap->add_cb == NULL) |
41 | 0 | return 1; |
42 | | |
43 | 0 | return add_cb_wrap->add_cb(s, ext_type, out, outlen, al, |
44 | 0 | add_cb_wrap->add_arg); |
45 | 0 | } |
46 | | |
47 | | static void custom_ext_free_old_cb_wrap(SSL *s, unsigned int ext_type, |
48 | | unsigned int context, |
49 | | const unsigned char *out, void *add_arg) |
50 | 0 | { |
51 | 0 | custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg; |
52 | |
|
53 | 0 | if (add_cb_wrap->free_cb == NULL) |
54 | 0 | return; |
55 | | |
56 | 0 | add_cb_wrap->free_cb(s, ext_type, out, add_cb_wrap->add_arg); |
57 | 0 | } |
58 | | |
59 | | static int custom_ext_parse_old_cb_wrap(SSL *s, unsigned int ext_type, |
60 | | unsigned int context, |
61 | | const unsigned char *in, |
62 | | size_t inlen, X509 *x, size_t chainidx, |
63 | | int *al, void *parse_arg) |
64 | 0 | { |
65 | 0 | custom_ext_parse_cb_wrap *parse_cb_wrap = (custom_ext_parse_cb_wrap *)parse_arg; |
66 | |
|
67 | 0 | if (parse_cb_wrap->parse_cb == NULL) |
68 | 0 | return 1; |
69 | | |
70 | 0 | return parse_cb_wrap->parse_cb(s, ext_type, in, inlen, al, |
71 | 0 | parse_cb_wrap->parse_arg); |
72 | 0 | } |
73 | | |
74 | | /* |
75 | | * Find a custom extension from the list. The |role| param is there to |
76 | | * support the legacy API where custom extensions for client and server could |
77 | | * be set independently on the same SSL_CTX. It is set to ENDPOINT_SERVER if we |
78 | | * are trying to find a method relevant to the server, ENDPOINT_CLIENT for the |
79 | | * client, or ENDPOINT_BOTH for either |
80 | | */ |
81 | | custom_ext_method *custom_ext_find(const custom_ext_methods *exts, |
82 | | ENDPOINT role, unsigned int ext_type, |
83 | | size_t *idx) |
84 | 0 | { |
85 | 0 | size_t i; |
86 | 0 | custom_ext_method *meth = exts->meths; |
87 | |
|
88 | 0 | for (i = 0; i < exts->meths_count; i++, meth++) { |
89 | 0 | if (ext_type == meth->ext_type |
90 | 0 | && (role == ENDPOINT_BOTH || role == meth->role |
91 | 0 | || meth->role == ENDPOINT_BOTH)) { |
92 | 0 | if (idx != NULL) |
93 | 0 | *idx = i; |
94 | 0 | return meth; |
95 | 0 | } |
96 | 0 | } |
97 | 0 | return NULL; |
98 | 0 | } |
99 | | |
100 | | /* |
101 | | * Initialise custom extensions flags to indicate neither sent nor received. |
102 | | */ |
103 | | void custom_ext_init(custom_ext_methods *exts) |
104 | 0 | { |
105 | 0 | size_t i; |
106 | 0 | custom_ext_method *meth = exts->meths; |
107 | |
|
108 | 0 | for (i = 0; i < exts->meths_count; i++, meth++) |
109 | 0 | meth->ext_flags &= ~(SSL_EXT_FLAG_SENT | SSL_EXT_FLAG_RECEIVED); |
110 | 0 | } |
111 | | |
112 | | /* Pass received custom extension data to the application for parsing. */ |
113 | | int custom_ext_parse(SSL_CONNECTION *s, unsigned int context, |
114 | | unsigned int ext_type, |
115 | | const unsigned char *ext_data, size_t ext_size, X509 *x, |
116 | | size_t chainidx) |
117 | 0 | { |
118 | 0 | int al = 0; |
119 | 0 | custom_ext_methods *exts = &s->cert->custext; |
120 | 0 | custom_ext_method *meth; |
121 | 0 | ENDPOINT role = ENDPOINT_BOTH; |
122 | |
|
123 | 0 | if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0) |
124 | 0 | role = s->server ? ENDPOINT_SERVER : ENDPOINT_CLIENT; |
125 | |
|
126 | 0 | meth = custom_ext_find(exts, role, ext_type, NULL); |
127 | | /* If not found return success */ |
128 | 0 | if (!meth) |
129 | 0 | return 1; |
130 | | |
131 | | /* Check if extension is defined for our protocol. If not, skip */ |
132 | 0 | if (!extension_is_relevant(s, meth->context, context)) |
133 | 0 | return 1; |
134 | | |
135 | 0 | if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) { |
136 | | /* |
137 | | * If it's ServerHello or EncryptedExtensions we can't have any |
138 | | * extensions not sent in ClientHello. |
139 | | */ |
140 | 0 | if ((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0) { |
141 | 0 | SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
142 | 0 | return 0; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | /* |
147 | | * Extensions received in the ClientHello or CertificateRequest are marked |
148 | | * with the SSL_EXT_FLAG_RECEIVED. This is so we know to add the equivalent |
149 | | * extensions in the response messages |
150 | | */ |
151 | 0 | if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)) |
152 | 0 | != 0) |
153 | 0 | meth->ext_flags |= SSL_EXT_FLAG_RECEIVED; |
154 | | |
155 | | /* If no parse function set return success */ |
156 | 0 | if (meth->parse_cb == NULL) |
157 | 0 | return 1; |
158 | | |
159 | 0 | if (meth->parse_cb(SSL_CONNECTION_GET_USER_SSL(s), ext_type, context, ext_data, |
160 | 0 | ext_size, x, chainidx, &al, meth->parse_arg) |
161 | 0 | <= 0) { |
162 | 0 | SSLfatal(s, al, SSL_R_BAD_EXTENSION); |
163 | 0 | return 0; |
164 | 0 | } |
165 | | |
166 | 0 | return 1; |
167 | 0 | } |
168 | | |
169 | | /* |
170 | | * Request custom extension data from the application and add to the return |
171 | | * buffer. |
172 | | */ |
173 | | int custom_ext_add(SSL_CONNECTION *s, int context, WPACKET *pkt, X509 *x, |
174 | | size_t chainidx, int maxversion) |
175 | 0 | { |
176 | 0 | custom_ext_methods *exts = &s->cert->custext; |
177 | 0 | custom_ext_method *meth; |
178 | 0 | size_t i; |
179 | 0 | int al; |
180 | 0 | int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0; |
181 | |
|
182 | 0 | for (i = 0; i < exts->meths_count; i++) { |
183 | 0 | const unsigned char *out = NULL; |
184 | 0 | size_t outlen = 0; |
185 | |
|
186 | 0 | meth = exts->meths + i; |
187 | |
|
188 | 0 | if (!should_add_extension(s, meth->context, context, maxversion)) |
189 | 0 | continue; |
190 | | |
191 | 0 | if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_RAW_PUBLIC_KEY | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)) != 0) { |
192 | | /* Only send extensions present in ClientHello/CertificateRequest */ |
193 | 0 | if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED)) |
194 | 0 | continue; |
195 | 0 | } |
196 | | |
197 | 0 | #ifndef OPENSSL_NO_ECH |
198 | 0 | if ((context & SSL_EXT_CLIENT_HELLO) != 0 |
199 | 0 | && s->ext.ech.attempted == 1) { |
200 | 0 | if (s->ext.ech.ch_depth == 1) { |
201 | | /* mark custom CH ext for ECH compression, if doing ECH */ |
202 | 0 | if (s->ext.ech.n_outer_only >= OSSL_ECH_OUTERS_MAX) { |
203 | 0 | OSSL_TRACE_BEGIN(TLS) |
204 | 0 | { |
205 | 0 | BIO_printf(trc_out, |
206 | 0 | "Too many outers to compress (max=%d)\n", |
207 | 0 | OSSL_ECH_OUTERS_MAX); |
208 | 0 | } |
209 | 0 | OSSL_TRACE_END(TLS); |
210 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 0 | s->ext.ech.outer_only[s->ext.ech.n_outer_only] = meth->ext_type; |
214 | 0 | s->ext.ech.n_outer_only++; |
215 | 0 | OSSL_TRACE_BEGIN(TLS) |
216 | 0 | { |
217 | 0 | BIO_printf(trc_out, "ECH compressing type " |
218 | 0 | "0x%04x (tot: %d)\n", |
219 | 0 | (int)meth->ext_type, |
220 | 0 | (int)s->ext.ech.n_outer_only); |
221 | 0 | } |
222 | 0 | OSSL_TRACE_END(TLS); |
223 | 0 | } |
224 | 0 | if (s->ext.ech.ch_depth == 0) { |
225 | | /* |
226 | | * We store/access the index of the extension handler in |
227 | | * s->ext.ech.ext_ind, as we'd otherwise not know it here. |
228 | | * Be nice were there a better way to handle that. |
229 | | */ |
230 | | /* copy over the extension octets (if any) to outer */ |
231 | 0 | int j, tind = -1; |
232 | 0 | RAW_EXTENSION *raws = NULL; |
233 | | |
234 | | /* we gotta find the relevant index to copy over this ext */ |
235 | 0 | if (s->clienthello == NULL |
236 | 0 | || s->clienthello->pre_proc_exts == NULL) { |
237 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION); |
238 | 0 | return 0; |
239 | 0 | } |
240 | 0 | raws = s->clienthello->pre_proc_exts; |
241 | 0 | for (j = 0; j != (int)s->clienthello->pre_proc_exts_len; j++) { |
242 | 0 | if (raws[j].type == meth->ext_type) { |
243 | 0 | tind = j; |
244 | 0 | break; |
245 | 0 | } |
246 | 0 | } |
247 | 0 | if (tind == -1) { |
248 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION); |
249 | 0 | return 0; |
250 | 0 | } |
251 | 0 | if (ossl_ech_copy_inner2outer(s, meth->ext_type, tind, |
252 | 0 | pkt) |
253 | 0 | != OSSL_ECH_SAME_EXT_DONE) { |
254 | | /* for custom exts, we really should have found it */ |
255 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION); |
256 | 0 | return 0; |
257 | 0 | } |
258 | | /* we're done with that one now */ |
259 | 0 | continue; |
260 | 0 | } |
261 | 0 | } |
262 | 0 | #endif |
263 | | |
264 | | /* |
265 | | * We skip it if the callback is absent - except for a ClientHello where |
266 | | * we add an empty extension. |
267 | | */ |
268 | 0 | if ((context & SSL_EXT_CLIENT_HELLO) == 0 && meth->add_cb == NULL) |
269 | 0 | continue; |
270 | | |
271 | 0 | if (meth->add_cb != NULL) { |
272 | 0 | int cb_retval = meth->add_cb(SSL_CONNECTION_GET_USER_SSL(s), |
273 | 0 | meth->ext_type, context, &out, |
274 | 0 | &outlen, x, chainidx, &al, |
275 | 0 | meth->add_arg); |
276 | |
|
277 | 0 | if (cb_retval < 0) { |
278 | 0 | if (!for_comp) |
279 | 0 | SSLfatal(s, al, SSL_R_CALLBACK_FAILED); |
280 | 0 | return 0; /* error */ |
281 | 0 | } |
282 | 0 | if (cb_retval == 0) |
283 | 0 | continue; /* skip this extension */ |
284 | 0 | } |
285 | | |
286 | 0 | if (!WPACKET_put_bytes_u16(pkt, meth->ext_type) |
287 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
288 | 0 | || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen)) |
289 | 0 | || !WPACKET_close(pkt)) { |
290 | 0 | if (meth->free_cb != NULL) |
291 | 0 | meth->free_cb(SSL_CONNECTION_GET_USER_SSL(s), meth->ext_type, |
292 | 0 | context, out, meth->add_arg); |
293 | 0 | if (!for_comp) |
294 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
295 | 0 | return 0; |
296 | 0 | } |
297 | 0 | if ((context & SSL_EXT_CLIENT_HELLO) != 0) { |
298 | | /* |
299 | | * We can't send duplicates: code logic should prevent this. |
300 | | */ |
301 | 0 | if (!ossl_assert((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0)) { |
302 | 0 | if (meth->free_cb != NULL) |
303 | 0 | meth->free_cb(SSL_CONNECTION_GET_USER_SSL(s), meth->ext_type, |
304 | 0 | context, out, meth->add_arg); |
305 | 0 | if (!for_comp) |
306 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
307 | 0 | return 0; |
308 | 0 | } |
309 | | /* |
310 | | * Indicate extension has been sent: this is both a sanity check to |
311 | | * ensure we don't send duplicate extensions and indicates that it |
312 | | * is not an error if the extension is present in ServerHello. |
313 | | */ |
314 | 0 | meth->ext_flags |= SSL_EXT_FLAG_SENT; |
315 | 0 | } |
316 | 0 | if (meth->free_cb != NULL) |
317 | 0 | meth->free_cb(SSL_CONNECTION_GET_USER_SSL(s), meth->ext_type, |
318 | 0 | context, out, meth->add_arg); |
319 | 0 | } |
320 | 0 | return 1; |
321 | 0 | } |
322 | | |
323 | | /* Copy the flags from src to dst for any extensions that exist in both */ |
324 | | int custom_exts_copy_flags(custom_ext_methods *dst, |
325 | | const custom_ext_methods *src) |
326 | 0 | { |
327 | 0 | size_t i; |
328 | 0 | custom_ext_method *methsrc = src->meths; |
329 | |
|
330 | 0 | for (i = 0; i < src->meths_count; i++, methsrc++) { |
331 | 0 | custom_ext_method *methdst = custom_ext_find(dst, methsrc->role, |
332 | 0 | methsrc->ext_type, NULL); |
333 | |
|
334 | 0 | if (methdst == NULL) |
335 | 0 | continue; |
336 | | |
337 | 0 | methdst->ext_flags = methsrc->ext_flags; |
338 | 0 | } |
339 | |
|
340 | 0 | return 1; |
341 | 0 | } |
342 | | |
343 | | /* Copy old style API wrapper arguments */ |
344 | | static void custom_ext_copy_old_cb(custom_ext_method *methdst, |
345 | | const custom_ext_method *methsrc, |
346 | | int *err) |
347 | 0 | { |
348 | 0 | if (methsrc->add_cb != custom_ext_add_old_cb_wrap) |
349 | 0 | return; |
350 | | |
351 | 0 | if (*err) { |
352 | 0 | methdst->add_arg = NULL; |
353 | 0 | methdst->parse_arg = NULL; |
354 | 0 | return; |
355 | 0 | } |
356 | | |
357 | 0 | methdst->add_arg = OPENSSL_memdup(methsrc->add_arg, |
358 | 0 | sizeof(custom_ext_add_cb_wrap)); |
359 | 0 | methdst->parse_arg = OPENSSL_memdup(methsrc->parse_arg, |
360 | 0 | sizeof(custom_ext_parse_cb_wrap)); |
361 | |
|
362 | 0 | if (methdst->add_arg == NULL || methdst->parse_arg == NULL) |
363 | 0 | *err = 1; |
364 | |
|
365 | 0 | return; |
366 | 0 | } |
367 | | |
368 | | /* Copy table of custom extensions */ |
369 | | int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src) |
370 | 0 | { |
371 | 0 | size_t i; |
372 | 0 | int err = 0; |
373 | |
|
374 | 0 | if (src->meths_count > 0) { |
375 | 0 | dst->meths = OPENSSL_memdup(src->meths, |
376 | 0 | sizeof(*src->meths) * src->meths_count); |
377 | 0 | if (dst->meths == NULL) |
378 | 0 | return 0; |
379 | 0 | dst->meths_count = src->meths_count; |
380 | |
|
381 | 0 | for (i = 0; i < src->meths_count; i++) |
382 | 0 | custom_ext_copy_old_cb(&dst->meths[i], &src->meths[i], &err); |
383 | 0 | } |
384 | | |
385 | 0 | if (err) { |
386 | 0 | custom_exts_free(dst); |
387 | 0 | return 0; |
388 | 0 | } |
389 | | |
390 | 0 | return 1; |
391 | 0 | } |
392 | | |
393 | | /* Copy custom extensions that were set on connection */ |
394 | | int custom_exts_copy_conn(custom_ext_methods *dst, |
395 | | const custom_ext_methods *src) |
396 | 0 | { |
397 | 0 | size_t i; |
398 | 0 | int err = 0; |
399 | |
|
400 | 0 | if (src->meths_count > 0) { |
401 | 0 | size_t meths_count = 0; |
402 | |
|
403 | 0 | for (i = 0; i < src->meths_count; i++) |
404 | 0 | if ((src->meths[i].ext_flags & SSL_EXT_FLAG_CONN) != 0) |
405 | 0 | meths_count++; |
406 | |
|
407 | 0 | if (meths_count > 0) { |
408 | 0 | custom_ext_method *methdst = OPENSSL_realloc(dst->meths, |
409 | 0 | (dst->meths_count + meths_count) * sizeof(custom_ext_method)); |
410 | |
|
411 | 0 | if (methdst == NULL) |
412 | 0 | return 0; |
413 | | |
414 | 0 | for (i = 0; i < dst->meths_count; i++) |
415 | 0 | custom_ext_copy_old_cb(&methdst[i], &dst->meths[i], &err); |
416 | |
|
417 | 0 | dst->meths = methdst; |
418 | 0 | methdst += dst->meths_count; |
419 | |
|
420 | 0 | for (i = 0; i < src->meths_count; i++) { |
421 | 0 | custom_ext_method *methsrc = &src->meths[i]; |
422 | |
|
423 | 0 | if ((methsrc->ext_flags & SSL_EXT_FLAG_CONN) == 0) |
424 | 0 | continue; |
425 | | |
426 | 0 | memcpy(methdst, methsrc, sizeof(custom_ext_method)); |
427 | 0 | custom_ext_copy_old_cb(methdst, methsrc, &err); |
428 | 0 | methdst++; |
429 | 0 | } |
430 | |
|
431 | 0 | dst->meths_count += meths_count; |
432 | 0 | } |
433 | 0 | } |
434 | | |
435 | 0 | if (err) { |
436 | 0 | custom_exts_free(dst); |
437 | 0 | return 0; |
438 | 0 | } |
439 | | |
440 | 0 | return 1; |
441 | 0 | } |
442 | | |
443 | | void custom_exts_free(custom_ext_methods *exts) |
444 | 0 | { |
445 | 0 | size_t i; |
446 | 0 | custom_ext_method *meth; |
447 | |
|
448 | 0 | for (i = 0, meth = exts->meths; i < exts->meths_count; i++, meth++) { |
449 | 0 | if (meth->add_cb != custom_ext_add_old_cb_wrap) |
450 | 0 | continue; |
451 | | |
452 | | /* Old style API wrapper. Need to free the arguments too */ |
453 | 0 | OPENSSL_free(meth->add_arg); |
454 | 0 | OPENSSL_free(meth->parse_arg); |
455 | 0 | } |
456 | 0 | OPENSSL_free(exts->meths); |
457 | 0 | exts->meths = NULL; |
458 | 0 | exts->meths_count = 0; |
459 | 0 | } |
460 | | |
461 | | /* Return true if a client custom extension exists, false otherwise */ |
462 | | int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type) |
463 | 0 | { |
464 | 0 | return custom_ext_find(&ctx->cert->custext, ENDPOINT_CLIENT, ext_type, |
465 | 0 | NULL) |
466 | 0 | != NULL; |
467 | 0 | } |
468 | | |
469 | | int ossl_tls_add_custom_ext_intern(SSL_CTX *ctx, custom_ext_methods *exts, |
470 | | ENDPOINT role, unsigned int ext_type, |
471 | | unsigned int context, |
472 | | SSL_custom_ext_add_cb_ex add_cb, |
473 | | SSL_custom_ext_free_cb_ex free_cb, |
474 | | void *add_arg, |
475 | | SSL_custom_ext_parse_cb_ex parse_cb, |
476 | | void *parse_arg) |
477 | 0 | { |
478 | 0 | custom_ext_method *meth, *tmp; |
479 | | |
480 | | /* |
481 | | * Check application error: if add_cb is not set free_cb will never be |
482 | | * called. |
483 | | */ |
484 | 0 | if (add_cb == NULL && free_cb != NULL) |
485 | 0 | return 0; |
486 | | |
487 | 0 | if (exts == NULL) |
488 | 0 | exts = &ctx->cert->custext; |
489 | |
|
490 | 0 | #ifndef OPENSSL_NO_CT |
491 | | /* |
492 | | * We don't want applications registering callbacks for SCT extensions |
493 | | * whilst simultaneously using the built-in SCT validation features, as |
494 | | * these two things may not play well together. |
495 | | */ |
496 | 0 | if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp |
497 | 0 | && (context & SSL_EXT_CLIENT_HELLO) != 0 |
498 | 0 | && ctx != NULL |
499 | 0 | && SSL_CTX_ct_is_enabled(ctx)) |
500 | 0 | return 0; |
501 | 0 | #endif |
502 | | |
503 | | /* |
504 | | * Don't add if extension supported internally, but make exception |
505 | | * for extension types that previously were not supported, but now are. |
506 | | */ |
507 | 0 | if (SSL_extension_supported(ext_type) |
508 | 0 | && ext_type != TLSEXT_TYPE_signed_certificate_timestamp) |
509 | 0 | return 0; |
510 | | |
511 | | /* Extension type must fit in 16 bits */ |
512 | 0 | if (ext_type > 0xffff) |
513 | 0 | return 0; |
514 | | /* Search for duplicate */ |
515 | 0 | if (custom_ext_find(exts, role, ext_type, NULL)) |
516 | 0 | return 0; |
517 | 0 | tmp = OPENSSL_realloc(exts->meths, |
518 | 0 | (exts->meths_count + 1) * sizeof(custom_ext_method)); |
519 | 0 | if (tmp == NULL) |
520 | 0 | return 0; |
521 | | |
522 | 0 | exts->meths = tmp; |
523 | 0 | meth = exts->meths + exts->meths_count; |
524 | 0 | memset(meth, 0, sizeof(*meth)); |
525 | 0 | meth->role = role; |
526 | 0 | meth->context = context; |
527 | 0 | meth->parse_cb = parse_cb; |
528 | 0 | meth->add_cb = add_cb; |
529 | 0 | meth->free_cb = free_cb; |
530 | 0 | meth->ext_type = ext_type; |
531 | 0 | meth->ext_flags = (ctx == NULL) ? SSL_EXT_FLAG_CONN : 0; |
532 | 0 | meth->add_arg = add_arg; |
533 | 0 | meth->parse_arg = parse_arg; |
534 | 0 | exts->meths_count++; |
535 | 0 | return 1; |
536 | 0 | } |
537 | | |
538 | | static int add_old_custom_ext(SSL_CTX *ctx, ENDPOINT role, |
539 | | unsigned int ext_type, |
540 | | unsigned int context, |
541 | | custom_ext_add_cb add_cb, |
542 | | custom_ext_free_cb free_cb, |
543 | | void *add_arg, |
544 | | custom_ext_parse_cb parse_cb, void *parse_arg) |
545 | 0 | { |
546 | 0 | custom_ext_add_cb_wrap *add_cb_wrap |
547 | 0 | = OPENSSL_malloc(sizeof(*add_cb_wrap)); |
548 | 0 | custom_ext_parse_cb_wrap *parse_cb_wrap |
549 | 0 | = OPENSSL_malloc(sizeof(*parse_cb_wrap)); |
550 | 0 | int ret; |
551 | |
|
552 | 0 | if (add_cb_wrap == NULL || parse_cb_wrap == NULL) { |
553 | 0 | OPENSSL_free(add_cb_wrap); |
554 | 0 | OPENSSL_free(parse_cb_wrap); |
555 | 0 | return 0; |
556 | 0 | } |
557 | | |
558 | 0 | add_cb_wrap->add_arg = add_arg; |
559 | 0 | add_cb_wrap->add_cb = add_cb; |
560 | 0 | add_cb_wrap->free_cb = free_cb; |
561 | 0 | parse_cb_wrap->parse_arg = parse_arg; |
562 | 0 | parse_cb_wrap->parse_cb = parse_cb; |
563 | |
|
564 | 0 | ret = ossl_tls_add_custom_ext_intern(ctx, NULL, role, ext_type, |
565 | 0 | context, |
566 | 0 | custom_ext_add_old_cb_wrap, |
567 | 0 | custom_ext_free_old_cb_wrap, |
568 | 0 | add_cb_wrap, |
569 | 0 | custom_ext_parse_old_cb_wrap, |
570 | 0 | parse_cb_wrap); |
571 | |
|
572 | 0 | if (!ret) { |
573 | 0 | OPENSSL_free(add_cb_wrap); |
574 | 0 | OPENSSL_free(parse_cb_wrap); |
575 | 0 | } |
576 | |
|
577 | 0 | return ret; |
578 | 0 | } |
579 | | |
580 | | /* Application level functions to add the old custom extension callbacks */ |
581 | | int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type, |
582 | | custom_ext_add_cb add_cb, |
583 | | custom_ext_free_cb free_cb, |
584 | | void *add_arg, |
585 | | custom_ext_parse_cb parse_cb, void *parse_arg) |
586 | 0 | { |
587 | 0 | return add_old_custom_ext(ctx, ENDPOINT_CLIENT, ext_type, |
588 | 0 | SSL_EXT_TLS1_2_AND_BELOW_ONLY |
589 | 0 | | SSL_EXT_CLIENT_HELLO |
590 | 0 | | SSL_EXT_TLS1_2_SERVER_HELLO |
591 | 0 | | SSL_EXT_IGNORE_ON_RESUMPTION, |
592 | 0 | add_cb, free_cb, add_arg, parse_cb, parse_arg); |
593 | 0 | } |
594 | | |
595 | | int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type, |
596 | | custom_ext_add_cb add_cb, |
597 | | custom_ext_free_cb free_cb, |
598 | | void *add_arg, |
599 | | custom_ext_parse_cb parse_cb, void *parse_arg) |
600 | 0 | { |
601 | 0 | return add_old_custom_ext(ctx, ENDPOINT_SERVER, ext_type, |
602 | 0 | SSL_EXT_TLS1_2_AND_BELOW_ONLY |
603 | 0 | | SSL_EXT_CLIENT_HELLO |
604 | 0 | | SSL_EXT_TLS1_2_SERVER_HELLO |
605 | 0 | | SSL_EXT_IGNORE_ON_RESUMPTION, |
606 | 0 | add_cb, free_cb, add_arg, parse_cb, parse_arg); |
607 | 0 | } |
608 | | |
609 | | int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type, |
610 | | unsigned int context, |
611 | | SSL_custom_ext_add_cb_ex add_cb, |
612 | | SSL_custom_ext_free_cb_ex free_cb, |
613 | | void *add_arg, |
614 | | SSL_custom_ext_parse_cb_ex parse_cb, void *parse_arg) |
615 | 0 | { |
616 | 0 | return ossl_tls_add_custom_ext_intern(ctx, NULL, ENDPOINT_BOTH, ext_type, |
617 | 0 | context, add_cb, free_cb, add_arg, |
618 | 0 | parse_cb, parse_arg); |
619 | 0 | } |
620 | | |
621 | | int SSL_extension_supported(unsigned int ext_type) |
622 | 0 | { |
623 | 0 | switch (ext_type) { |
624 | | /* Internally supported extensions. */ |
625 | 0 | case TLSEXT_TYPE_application_layer_protocol_negotiation: |
626 | 0 | case TLSEXT_TYPE_ec_point_formats: |
627 | 0 | case TLSEXT_TYPE_supported_groups: |
628 | 0 | case TLSEXT_TYPE_key_share: |
629 | 0 | #ifndef OPENSSL_NO_NEXTPROTONEG |
630 | 0 | case TLSEXT_TYPE_next_proto_neg: |
631 | 0 | #endif |
632 | 0 | case TLSEXT_TYPE_padding: |
633 | 0 | case TLSEXT_TYPE_renegotiate: |
634 | 0 | case TLSEXT_TYPE_max_fragment_length: |
635 | 0 | case TLSEXT_TYPE_server_name: |
636 | 0 | case TLSEXT_TYPE_session_ticket: |
637 | 0 | case TLSEXT_TYPE_signature_algorithms: |
638 | 0 | #ifndef OPENSSL_NO_SRP |
639 | 0 | case TLSEXT_TYPE_srp: |
640 | 0 | #endif |
641 | 0 | #ifndef OPENSSL_NO_OCSP |
642 | 0 | case TLSEXT_TYPE_status_request: |
643 | 0 | #endif |
644 | 0 | #ifndef OPENSSL_NO_CT |
645 | 0 | case TLSEXT_TYPE_signed_certificate_timestamp: |
646 | 0 | #endif |
647 | 0 | #ifndef OPENSSL_NO_SRTP |
648 | 0 | case TLSEXT_TYPE_use_srtp: |
649 | 0 | #endif |
650 | 0 | case TLSEXT_TYPE_encrypt_then_mac: |
651 | 0 | case TLSEXT_TYPE_supported_versions: |
652 | 0 | case TLSEXT_TYPE_extended_master_secret: |
653 | 0 | case TLSEXT_TYPE_psk_kex_modes: |
654 | 0 | case TLSEXT_TYPE_cookie: |
655 | 0 | case TLSEXT_TYPE_early_data: |
656 | 0 | case TLSEXT_TYPE_certificate_authorities: |
657 | 0 | case TLSEXT_TYPE_psk: |
658 | 0 | case TLSEXT_TYPE_post_handshake_auth: |
659 | 0 | case TLSEXT_TYPE_compress_certificate: |
660 | 0 | case TLSEXT_TYPE_client_cert_type: |
661 | 0 | case TLSEXT_TYPE_server_cert_type: |
662 | 0 | #ifndef OPENSSL_NO_ECH |
663 | 0 | case TLSEXT_TYPE_ech: |
664 | 0 | case TLSEXT_TYPE_outer_extensions: |
665 | 0 | #endif |
666 | 0 | return 1; |
667 | 0 | default: |
668 | 0 | return 0; |
669 | 0 | } |
670 | 0 | } |